configwindow.c 11.4 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>
4
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
5
6
7
 *                                                                              
 *  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
8
 *  the Free Software Foundation; either version 3 of the License, or
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
9
10
11
12
13
14
15
16
17
18
19
 *  (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.
 */
20

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
21
#include <accountlist.h>
22
23
#include <accountwindow.h>
#include <actions.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
24
25
26
#include <config.h>
#include <dbus.h>
#include <mainwindow.h>
27

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
28
29
30
#include <gtk/gtk.h>

/** Local variables */
31
gboolean  dialogOpen = FALSE;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
32
GtkListStore *account_store;
33
GtkListStore *codec_store;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
34
35
36
GtkWidget * addButton;
GtkWidget * editButton;
GtkWidget * deleteButton;
37
GtkWidget * defaultButton;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
38
39
40
41

account_t * selectedAccount;

/** Fills the treelist with accounts */
42
	void 
43
config_window_fill_account_list ()
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
44
{
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
	if(dialogOpen)
	{
		GtkTreeIter iter;

		gtk_list_store_clear(account_store);
		int i;
		for( i = 0; i < account_list_get_size(); i++)
		{
			account_t  * a = account_list_get_nth (i);
			if (a)
			{
				gtk_list_store_append (account_store, &iter);

				gtk_list_store_set(account_store, &iter,
						0, g_hash_table_lookup(a->properties, ACCOUNT_ALIAS),  // Name
						1, g_hash_table_lookup(a->properties, ACCOUNT_TYPE),   // Protocol
						2, account_state_name(a->state),      // Status
						3, a,                                 // Pointer
						-1);

			}
		} 

		gtk_widget_set_sensitive( GTK_WIDGET(editButton),   FALSE);
		gtk_widget_set_sensitive( GTK_WIDGET(deleteButton), FALSE);
		gtk_widget_set_sensitive( GTK_WIDGET(defaultButton), FALSE);
	}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
72
}
73
74
75
76


config_window_fill_codec_list()
{
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  if(dialogOpen)
  {
    GtkTreeIter iter;
    int i;
    gtk_list_store_clear(codec_store);
    gchar * description = "Select a codec:";
    //gtk_list_store_append(codec_store, &iter);
    //gtk_list_store_set(codec_store, &iter, 0, description, -1);
    for(i=0; i<codec_list_get_size(); i++)
    {
      codec_t* c = codec_list_get_nth(i); 
      printf("%s\n",c->name);
      if(c)
      {
        gtk_list_store_append (codec_store, &iter);
        gtk_list_store_set(codec_store, &iter,0,c->name,-1);
      }
    }
  }
96
97
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
98
99
100
/**
 * Delete an account
 */
101
	static void 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
102
103
delete_account( GtkWidget *widget, gpointer   data )
{
104
105
106
107
	if(selectedAccount)
	{
		dbus_remove_account(selectedAccount->accountID);
	}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
108
109
110
}

/**
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
111
 * Edit an account
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
112
 */
113
	static void 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
114
115
edit_account( GtkWidget *widget, gpointer   data )
{
116
117
118
119
	if(selectedAccount)
	{
		show_account_window(selectedAccount);
	}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
120
121
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
122
123
124
125

/**
 * Add an account
 */
126
	static void 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
127
128
add_account( GtkWidget *widget, gpointer   data )
{
129
130
131
132
133
134
	show_account_window(NULL);
}

/*
 * Should mark the account as default
 */
135
void
136
137
138
139
140
default_account(GtkWidget *widget, gpointer data)
{
	// set account as default	
	if(selectedAccount)
	{
141
		account_list_set_default(selectedAccount->accountID);
142
143
		dbus_set_default_account(selectedAccount->accountID);
	}	
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
144
145
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
146
/* Call back when the user click on an account in the list */
147
static void 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
148
149
150
151
select_account(GtkTreeSelection *sel, GtkTreeModel *model) 
{
	GtkTreeIter  iter;
	GValue val;
152

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
153
154
	if (! gtk_tree_selection_get_selected (sel, &model, &iter))
	{
155
		selectedAccount = NULL;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
156
		return;
157
158
	}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
159
160
	val.g_type = G_TYPE_POINTER;
	gtk_tree_model_get_value (model, &iter, 3, &val);
161

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
162
	selectedAccount = (account_t*) g_value_get_pointer(&val);
163
164
	g_value_unset(&val);

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
165
166
	if(selectedAccount)
	{
167
168
169
170
171
172
		gtk_widget_set_sensitive( GTK_WIDGET(editButton),   TRUE);
		gtk_widget_set_sensitive( GTK_WIDGET(deleteButton), TRUE); 
		gtk_widget_set_sensitive( GTK_WIDGET(defaultButton), TRUE);
	}
	g_print("select");

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
173
174
}

175
176
177
178
179
180
static void
select_codec( GtkComboBox* wid)
{
	guint item = gtk_combo_box_get_active(wid);
	/* now we want this selected codec to be used as the preferred codec */
	/* ie first in the list in the user config */
181
182
	codec_set_prefered_order(item);
	dbus_set_prefered_codec(codec_list_get_nth(0)->name);  
183
184
}

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
void
bold_if_default_account(GtkTreeViewColumn *col, 
			GtkCellRenderer *rend, 
			GtkTreeModel *tree_model, 
			GtkTreeIter *iter,
			gpointer data)
{
	GValue val;
	val.g_type = G_TYPE_POINTER;
	gtk_tree_model_get_value(tree_model, iter, 3, &val);
	account_t* current = (account_t*) g_value_get_pointer(&val);
	g_value_unset(&val);
	if(g_strcasecmp(current->accountID,account_list_get_default())==0)
		g_object_set (G_OBJECT(rend),"weight",800 ,NULL);
	else
		g_object_set(G_OBJECT(rend), "weight", 400, NULL);

}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
203
204


205
	GtkWidget *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
206
207
create_accounts_tab()
{
208
	GtkWidget *ret;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
209
210
211
212
213
214
215
	GtkWidget *sw;
	GtkWidget *view;
	GtkWidget *bbox;
	GtkCellRenderer *rend;
	GtkTreeViewColumn *col;
	GtkTreeSelection *sel;
	GtkWidget *label;
216

217
	GtkTreeIter iter;
218
219
220
        GValue val;
        val.g_type = G_TYPE_POINTER;

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	selectedAccount = NULL;

	ret = gtk_vbox_new(FALSE, 10); 
	gtk_container_set_border_width (GTK_CONTAINER (ret), 10);

	label = gtk_label_new("This is the list of accounts previously setup.");

	gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
	gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);

	gtk_box_pack_start(GTK_BOX(ret), label, FALSE, TRUE, 0);
	gtk_widget_show(label);

	sw = gtk_scrolled_window_new(NULL,NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
236
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
237
238
239
240
	gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_IN);

	gtk_box_pack_start(GTK_BOX(ret), sw, TRUE, TRUE, 0);
	account_store = gtk_list_store_new (4, 
241
242
243
244
245
			G_TYPE_STRING,  // Name
			G_TYPE_STRING,  // Protocol
			G_TYPE_STRING,  // Status
			G_TYPE_POINTER  // Pointer to the Object
			);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
246
247
248
249

	view = gtk_tree_view_new_with_model (GTK_TREE_MODEL(account_store));

	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
250
	g_signal_connect (G_OBJECT (sel), "changed",
251
252
253
			G_CALLBACK (select_account),
			account_store);

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
254
	gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(account_store),
255
256
257
			2, GTK_SORT_ASCENDING);

	rend = gtk_cell_renderer_text_new();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
258
	col = gtk_tree_view_column_new_with_attributes ("Alias",
259
260
261
			rend,
			"markup", 0,
			NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
262
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);
263
	gtk_tree_view_column_set_cell_data_func(col, rend, bold_if_default_account, NULL,NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
264

265
266
267
268
269
	rend = gtk_cell_renderer_text_new();
	col = gtk_tree_view_column_new_with_attributes ("Protocol",
			rend,
			"markup", 1,
			NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
270
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);
271
	gtk_tree_view_column_set_cell_data_func(col, rend, bold_if_default_account, NULL,NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
272
273
274

	rend = gtk_cell_renderer_text_new();
	col = gtk_tree_view_column_new_with_attributes ("Status",
275
276
277
			rend,
			"markup", 2,
			NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
278
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);
279
	gtk_tree_view_column_set_cell_data_func(col, rend, bold_if_default_account, NULL,NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
280
281
282
283
284
285
286
287
	g_object_unref(G_OBJECT(account_store));
	gtk_container_add(GTK_CONTAINER(sw), view);

	/* The buttons to press! */
	bbox = gtk_hbutton_box_new();
	gtk_box_set_spacing(GTK_BOX(bbox), 10); //GAIM_HIG_BOX_SPACE
	gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_START);
	gtk_box_pack_start(GTK_BOX(ret), bbox, FALSE, FALSE, 0);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
288
	gtk_widget_show (bbox); 
289

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
290
	addButton = gtk_button_new_from_stock (GTK_STOCK_ADD);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
291
	g_signal_connect_swapped(G_OBJECT(addButton), "clicked",
292
			G_CALLBACK(add_account), NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
293
294
	gtk_box_pack_start(GTK_BOX(bbox), addButton, FALSE, FALSE, 0);
	gtk_widget_show(addButton);
295

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
296
297
	editButton = gtk_button_new_from_stock (GTK_STOCK_EDIT);
	g_signal_connect_swapped(G_OBJECT(editButton), "clicked",
298
			G_CALLBACK(edit_account), NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
299
300
301
302
303
	gtk_box_pack_start(GTK_BOX(bbox), editButton, FALSE, FALSE, 0);
	gtk_widget_show(editButton);
	
	deleteButton = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
	g_signal_connect_swapped(G_OBJECT(deleteButton), "clicked",
304
			G_CALLBACK(delete_account), NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
305
306
307
	gtk_box_pack_start(GTK_BOX(bbox), deleteButton, FALSE, FALSE, 0);
	gtk_widget_show(deleteButton);
	
308
309
310
311
312
313
	defaultButton = gtk_button_new_with_mnemonic("Set as Default");
	g_signal_connect_swapped(G_OBJECT(defaultButton), "clicked", 
			G_CALLBACK(default_account), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), defaultButton, FALSE, FALSE, 0);
	gtk_widget_show(defaultButton);

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
314
	gtk_widget_show_all(ret);
315

316
	config_window_fill_account_list();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
317
318
319
320

	return ret;
}

321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
GtkWidget*
create_audio_tab ()
{
	GtkWidget* ret;
	GtkWidget* label;
	GtkWidget* combo_box;
	GtkWidget* hbox1;
	GtkTreeIter iter;
	GtkCellRenderer *renderer;
	

	ret = gtk_vbox_new(FALSE, 10);
        gtk_container_set_border_width (GTK_CONTAINER (ret), 10);
	
	label = gtk_label_new("Set your audio preferences.");
        gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
        gtk_box_pack_start(GTK_BOX(ret), label, FALSE, TRUE, 0);
        gtk_widget_show(label);
	
	hbox1 = gtk_label_new("Available Codecs");
        gtk_misc_set_alignment(GTK_MISC(hbox1), 0, 0.5);
	gtk_box_pack_start(GTK_BOX(ret), hbox1, FALSE, TRUE, 0);
	gtk_widget_show(hbox1);
	codec_store = gtk_list_store_new(1, G_TYPE_STRING);
	
  	combo_box = gtk_combo_box_new_with_model(GTK_TREE_MODEL (codec_store));
	gtk_label_set_mnemonic_widget(GTK_LABEL(hbox1), combo_box);
	renderer = gtk_cell_renderer_text_new();
	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), renderer, TRUE);
	gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), renderer, "text",0,NULL);
	gtk_box_pack_start(GTK_BOX(ret), combo_box, FALSE, TRUE,0);

	
	g_signal_connect (G_OBJECT (combo_box), "changed",
                        G_CALLBACK (select_codec),
                        NULL);
	gtk_widget_show(combo_box);
	
	gtk_widget_show_all(ret);
	config_window_fill_codec_list();
363
	gtk_combo_box_set_active(combo_box, 0);
364
365
366
367
368
369
370
	

	return ret;
	
}


371
	void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
372
373
show_config_window ()
{
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
	GtkDialog * dialog;
	GtkWidget * notebook;
	GtkWidget * tab;

	dialogOpen = TRUE;

	dialog = GTK_DIALOG(gtk_dialog_new_with_buttons ("Preferences",
				GTK_WINDOW(get_main_window()),
				GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				GTK_STOCK_CLOSE,
				GTK_RESPONSE_ACCEPT,
				NULL));

	gtk_dialog_set_has_separator(dialog, FALSE);
	gtk_window_set_default_size( GTK_WINDOW(dialog), 400, 400);
	gtk_container_set_border_width (GTK_CONTAINER(dialog), 0);

	notebook = gtk_notebook_new ();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
392
	gtk_box_pack_start (GTK_BOX (dialog->vbox), notebook, TRUE, TRUE, 0);
393
	gtk_container_set_border_width (GTK_CONTAINER(notebook), 10);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
394
	gtk_widget_show(notebook);
395
396

	/* Create tabs */ 
397
	// Accounts tab
398
399
	tab = create_accounts_tab();
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), tab, gtk_label_new("Accounts"));
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
400
	gtk_notebook_page_num(GTK_NOTEBOOK(notebook), tab);
401
	
402
403
404
405
406
	// Audio tab
	tab = create_audio_tab();	
	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), tab, gtk_label_new("Audio Settings"));
	gtk_notebook_page_num(GTK_NOTEBOOK(notebook), tab);	

407
408
409
	gtk_dialog_run (dialog);

	dialogOpen = FALSE;
410

411
	gtk_widget_destroy (GTK_WIDGET(dialog));
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
412
413
}