accountwindow.c 13.5 KB
Newer Older
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
1
2
/*
 *  Copyright (C) 2007 Savoir-Faire Linux inc.
3
 *  Author: Pierre-Luc Beaudoin <pierre-luc@squidy.info>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *                                                                              
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *                                                                                
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *                                                                              
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
#include <config.h>
#include <mainwindow.h>
#include <accountlist.h>
23
24
#include <string.h>
#include <dbus.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
25
26
27
28
29
#include <gtk/gtk.h>

/** Local variables */
account_t * currentAccount;

30
31
32
33
34
35
36
GtkDialog * dialog;
GtkWidget * table;
GtkWidget * label;
GtkWidget * entryID;
GtkWidget * entryName;
GtkWidget * entryProtocol;
GtkWidget * entryEnabled;
37
//GtkWidget * entryRegister;
38
39
40
41
42
43
44
GtkWidget * entryFullName;
GtkWidget * entryUserPart;
GtkWidget * entryHostPart;
GtkWidget * entryUsername;
GtkWidget * entryPassword;


Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
45
46
47
48
49
50
51
52
53
54
/**
 * Delete an account
 */
/*static void 
delete_account( GtkWidget *widget, gpointer   data )
{
  sflphone_remove_account(currentAccount);
}*/


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Signal to entryProtocol 'changed' */
void
change_protocol (account_t * currentAccount)
{
  gchar* proto = (gchar *)gtk_combo_box_get_active_text(GTK_COMBO_BOX(entryProtocol));
  //g_print("Protocol changed\n");

  // toggle sensitivity for: entryUserPart 
  if (strcmp(proto, "SIP") == 0) {
    gtk_widget_set_sensitive( GTK_WIDGET(entryUserPart),   TRUE);
  }
  else if (strcmp(proto, "IAX") == 0) {
    gtk_widget_set_sensitive( GTK_WIDGET(entryUserPart),   FALSE);
  }
  else {
    // Should not get here.
    g_print("Unknown protocol: %s\n", proto);
  }
}


Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
76
77
78
79
void
show_account_window (account_t * a)
{
  guint response;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
80
 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
81
  currentAccount = a;
82

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
83
84
85
86
87
88
89
90
91
  // Default settings
  gchar * curAccountID = "";
  gchar * curAccountEnabled = "TRUE";
  gchar * curAccountType = "SIP";
  gchar * curUserPart = "";
  gchar * curHostPart = "";
  gchar * curPassword = "";
  gchar * curUsername = "";
  gchar * curFullName = "";
92
93
94
  /* TODO: add curProxy, and add boxes for Proxy support */

  // Load from SIP/IAX/Unknown ?
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
95
96
97
98
99
  if(a)
  {
    curAccountID = a->accountID;
    curAccountType = g_hash_table_lookup(currentAccount->properties, ACCOUNT_TYPE);
    curAccountEnabled = g_hash_table_lookup(currentAccount->properties, ACCOUNT_ENABLED);
100

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
101
102
103
104
105
106
107
108
109
110
111
112
113
    if (strcmp(curAccountType, "IAX") == 0) {
      curHostPart = g_hash_table_lookup(currentAccount->properties, ACCOUNT_IAX_HOST);
      curPassword = g_hash_table_lookup(currentAccount->properties, ACCOUNT_IAX_PASS);
      curUsername = g_hash_table_lookup(currentAccount->properties, ACCOUNT_IAX_USER);
      curFullName = g_hash_table_lookup(currentAccount->properties, ACCOUNT_IAX_FULL_NAME);
    }
    else if (strcmp(curAccountType, "SIP") == 0) {
      curHostPart = g_hash_table_lookup(currentAccount->properties, ACCOUNT_SIP_HOST_PART);
      curPassword = g_hash_table_lookup(currentAccount->properties, ACCOUNT_SIP_PASSWORD);
      curUsername = g_hash_table_lookup(currentAccount->properties, ACCOUNT_SIP_AUTH_NAME);
      curFullName = g_hash_table_lookup(currentAccount->properties, ACCOUNT_SIP_FULL_NAME);
      curUserPart = g_hash_table_lookup(currentAccount->properties, ACCOUNT_SIP_USER_PART);
    }
114
  }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
115
116
117
118
119
  else
  {
    currentAccount = g_new0(account_t, 1);
    currentAccount->properties = g_hash_table_new(NULL, g_str_equal);
    curAccountID = "test";
120
  }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
121

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  dialog = GTK_DIALOG(gtk_dialog_new_with_buttons ("Account settings",
                                        GTK_WINDOW(get_main_window()),
                                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_STOCK_CANCEL,
                                        GTK_RESPONSE_CANCEL,
                                        GTK_STOCK_SAVE,
                                        GTK_RESPONSE_ACCEPT,
                                        NULL));
                                        
  gtk_dialog_set_has_separator(dialog, TRUE);
  gtk_container_set_border_width (GTK_CONTAINER(dialog), 0);
  
  table = gtk_table_new ( 8, 2  , FALSE /* homogeneous */);
  gtk_table_set_row_spacings( GTK_TABLE(table), 10);
  gtk_table_set_col_spacings( GTK_TABLE(table), 10);
  
#ifdef DEBUG  
  label = gtk_label_new_with_mnemonic ("ID:");
  gtk_table_attach ( GTK_TABLE( table ), label, 0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_misc_set_alignment(GTK_MISC (label), 0, 0.5);
  entryID = gtk_entry_new();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entryID);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
144
  gtk_entry_set_text(GTK_ENTRY(entryID), curAccountID);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
145
146
147
148
149
150
  gtk_widget_set_sensitive( GTK_WIDGET(entryID), FALSE);
  gtk_table_attach ( GTK_TABLE( table ), entryID, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
#endif 
  
  entryEnabled = gtk_check_button_new_with_mnemonic("_Enabled");
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(entryEnabled), 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
151
    strcmp(curAccountEnabled,"TRUE") == 0 ? TRUE: FALSE); 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
152
153
  gtk_table_attach ( GTK_TABLE( table ), entryEnabled, 0, 2, 1, 2, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  
154
155
156
157
  //entryRegister = gtk_check_button_new_with_mnemonic("_Register on startup ");
  //gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(entryRegister), 
  //  strcmp(g_hash_table_lookup(currentAccount->properties, ACCOUNT_REGISTER),"TRUE") == 0 ? TRUE: FALSE); 
  //gtk_table_attach ( GTK_TABLE( table ), entryRegister, 0, 2, 2, 3, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  
  label = gtk_label_new_with_mnemonic ("_Alias:");
  gtk_table_attach ( GTK_TABLE( table ), label, 0, 1, 3, 4, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_misc_set_alignment(GTK_MISC (label), 0, 0.5);
  entryName = gtk_entry_new();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entryName);
  gtk_entry_set_text(GTK_ENTRY(entryName), g_hash_table_lookup(currentAccount->properties, ACCOUNT_ALIAS));
  gtk_table_attach ( GTK_TABLE( table ), entryName, 1, 2, 3, 4, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  
  label = gtk_label_new_with_mnemonic ("_Protocol:");
  gtk_table_attach ( GTK_TABLE( table ), label, 0, 1, 4, 5, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_misc_set_alignment(GTK_MISC (label), 0, 0.5);
  entryProtocol = gtk_combo_box_new_text();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entryProtocol);
172
  //gtk_widget_set_sensitive( GTK_WIDGET(entryProtocol), TRUE);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
173
174
  gtk_combo_box_append_text(GTK_COMBO_BOX(entryProtocol), "SIP");
  gtk_combo_box_append_text(GTK_COMBO_BOX(entryProtocol), "IAX");
175
  if(strcmp(curAccountType, "SIP") == 0)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
176
177
178
  {
    gtk_combo_box_set_active(GTK_COMBO_BOX(entryProtocol),0);
  }
179
  else if(strcmp(curAccountType, "IAX") == 0)
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
180
181
  {
    gtk_combo_box_set_active(GTK_COMBO_BOX(entryProtocol),1);
182
183
184
185
186
187
188
  }
  else
  {
    /* Should never come here, add debug message. */
    gtk_combo_box_append_text(GTK_COMBO_BOX(entryProtocol), "Unknown");
    gtk_combo_box_set_active(GTK_COMBO_BOX(entryProtocol),2);  
  }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
189
  gtk_table_attach ( GTK_TABLE( table ), entryProtocol, 1, 2, 4, 5, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
190
191
192
193
194

  /* Link signal 'changed' */
  g_signal_connect (G_OBJECT (GTK_COMBO_BOX(entryProtocol)), "changed",
		    G_CALLBACK (change_protocol),
		    currentAccount);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
195
196
197
198
199
200
  
  label = gtk_label_new_with_mnemonic ("_Full Name:");
  gtk_table_attach ( GTK_TABLE( table ), label, 0, 1, 5, 6, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_misc_set_alignment(GTK_MISC (label), 0, 0.5);
  entryFullName = gtk_entry_new();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entryFullName);
201
  gtk_entry_set_text(GTK_ENTRY(entryFullName), curFullName);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
202
203
204
205
206
207
208
  gtk_table_attach ( GTK_TABLE( table ), entryFullName, 1, 2, 5, 6, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  
  label = gtk_label_new_with_mnemonic ("_User part:");
  gtk_table_attach ( GTK_TABLE( table ), label, 0, 1, 6, 7, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_misc_set_alignment(GTK_MISC (label), 0, 0.5);
  entryUserPart = gtk_entry_new();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entryUserPart);
209
  gtk_entry_set_text(GTK_ENTRY(entryUserPart), curUserPart);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
210
211
212
213
214
215
216
  gtk_table_attach ( GTK_TABLE( table ), entryUserPart, 1, 2, 6, 7, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  
  label = gtk_label_new_with_mnemonic ("_Host part:");
  gtk_table_attach ( GTK_TABLE( table ), label, 0, 1, 7, 8, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_misc_set_alignment(GTK_MISC (label), 0, 0.5);
  entryHostPart = gtk_entry_new();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entryHostPart);
217
  gtk_entry_set_text(GTK_ENTRY(entryHostPart), curHostPart);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
218
219
220
221
222
223
224
  gtk_table_attach ( GTK_TABLE( table ), entryHostPart, 1, 2, 7, 8, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  
  label = gtk_label_new_with_mnemonic ("U_sername:");
  gtk_table_attach ( GTK_TABLE( table ), label, 0, 1, 8, 9, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_misc_set_alignment(GTK_MISC (label), 0, 0.5);
  entryUsername = gtk_entry_new();
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entryUsername);
225
  gtk_entry_set_text(GTK_ENTRY(entryUsername), curUsername);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
226
227
228
229
230
231
232
233
  gtk_table_attach ( GTK_TABLE( table ), entryUsername, 1, 2, 8, 9, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  
  label = gtk_label_new_with_mnemonic ("_Password:");
  gtk_table_attach ( GTK_TABLE( table ), label, 0, 1, 9, 10, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_misc_set_alignment(GTK_MISC (label), 0, 0.5);
  entryPassword = gtk_entry_new();
  gtk_entry_set_visibility(GTK_ENTRY(entryPassword), FALSE);
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entryPassword);
234
  gtk_entry_set_text(GTK_ENTRY(entryPassword), curPassword);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
235
236
  gtk_table_attach ( GTK_TABLE( table ), entryPassword, 1, 2, 9, 10, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  
237
238
239
240
241
242
243
244
245
246
247
248

  // Toggle enabled/disabled widgets
  if (strcmp(curAccountType, "SIP") == 0) {
    //gtk_widget_set_sensitive( GTK_WIDGET(entryUserPart), TRUE);<    
  }
  else if (strcmp(curAccountType, "IAX") == 0) {
    gtk_widget_set_sensitive( GTK_WIDGET(entryUserPart), FALSE);
  }
  else {
    // Disable everything ! ouch!
    // Shouldn't get there.
  }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
249
250
251
252
253
254
255
256
257
  
  
  gtk_box_pack_start (GTK_BOX (dialog->vbox), table, TRUE, TRUE, 0);
  gtk_container_set_border_width (GTK_CONTAINER(table), 10);
  
  gtk_widget_show_all(table);
  
  response = gtk_dialog_run (GTK_DIALOG (dialog));
  if(response == GTK_RESPONSE_ACCEPT)
258
259
260
  {
    gchar* proto = (gchar *)gtk_combo_box_get_active_text(GTK_COMBO_BOX(entryProtocol));

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
261
262
263
    g_hash_table_replace(currentAccount->properties, 
      g_strdup(ACCOUNT_ENABLED), 
      g_strdup(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(entryEnabled)) ? "TRUE": "FALSE"));
264
265
266
    // g_hash_table_replace(currentAccount->properties, 
    //  g_strdup(ACCOUNT_REGISTER), 
    //  g_strdup(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(entryRegister)) ? "TRUE": "FALSE"));
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
267
268
269
270
    /* TODO Add SIP/IAX when IAX is ok */  
    g_hash_table_replace(currentAccount->properties, 
      g_strdup(ACCOUNT_ALIAS), 
      g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryName))));
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
271
272
273
    g_hash_table_replace(currentAccount->properties, 
      g_strdup(ACCOUNT_TYPE), 
      g_strdup(proto));
274

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
275
    
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    if (strcmp(proto, "SIP") == 0) { /* Protocol = SIP */
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_SIP_FULL_NAME), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryFullName))));
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_SIP_USER_PART), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryUserPart))));
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_SIP_HOST_PART), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryHostPart))));
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_SIP_AUTH_NAME), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryUsername))));
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_SIP_PASSWORD), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryPassword))));
    }
    else if (strcmp(proto, "IAX") == 0) { /* Protocol = IAX */
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_IAX_FULL_NAME), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryFullName))));
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_IAX_HOST), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryHostPart))));
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_IAX_USER), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryUsername))));
      g_hash_table_replace(currentAccount->properties, 
        g_strdup(ACCOUNT_IAX_PASS), 
        g_strdup((gchar *)gtk_entry_get_text(GTK_ENTRY(entryPassword))));
    }
    else {
      
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
310
      
311
312
313
314
315
316
317
    /** @todo Verify if it's the best condition to check */
    if (currentAccount->accountID == NULL) {
      dbus_add_account(currentAccount);
    }
    else {
      dbus_set_account_details(currentAccount);
    }
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
318
319
320
321
322
323
  }
  gtk_widget_destroy (GTK_WIDGET(dialog));
  
  
}