configwindow.c 8.18 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
33
34
35
GtkListStore *account_store;
GtkWidget * addButton;
GtkWidget * editButton;
GtkWidget * deleteButton;
36
GtkWidget * defaultButton;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
37
38

account_t * selectedAccount;
39
account_t * defaultAccount;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
40
41

/** 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
}
/**
 * Delete an account
 */
76
	static void 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
77
78
delete_account( GtkWidget *widget, gpointer   data )
{
79
80
81
82
	if(selectedAccount)
	{
		dbus_remove_account(selectedAccount->accountID);
	}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
83
84
85
}

/**
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
86
 * Edit an account
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
87
 */
88
	static void 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
89
90
edit_account( GtkWidget *widget, gpointer   data )
{
91
92
93
94
	if(selectedAccount)
	{
		show_account_window(selectedAccount);
	}
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
95
96
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
97
98
99
100

/**
 * Add an account
 */
101
	static void 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
102
103
add_account( GtkWidget *widget, gpointer   data )
{
104
105
106
107
108
109
	show_account_window(NULL);
}

/*
 * Should mark the account as default
 */
110
void
111
112
113
114
115
116
117
default_account(GtkWidget *widget, gpointer data)
{
	// set account as default	
	if(selectedAccount)
	{
		dbus_set_default_account(selectedAccount->accountID);
	}	
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
118
119
}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
120
/* Call back when the user click on an account in the list */
121
static void 
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
122
123
124
125
select_account(GtkTreeSelection *sel, GtkTreeModel *model) 
{
	GtkTreeIter  iter;
	GValue val;
126

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
127
128
	if (! gtk_tree_selection_get_selected (sel, &model, &iter))
	{
129
		selectedAccount = NULL;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
130
		return;
131
132
	}

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
133
134
	val.g_type = G_TYPE_POINTER;
	gtk_tree_model_get_value (model, &iter, 3, &val);
135

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
136
	selectedAccount = (account_t*) g_value_get_pointer(&val);
137
138
	g_value_unset(&val);

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
139
140
	if(selectedAccount)
	{
141
142
143
144
145
146
		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
147
148
149
150
}



151
	GtkWidget *
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
152
153
create_accounts_tab()
{
154
	GtkWidget *ret;
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
155
156
157
158
159
160
161
	GtkWidget *sw;
	GtkWidget *view;
	GtkWidget *bbox;
	GtkCellRenderer *rend;
	GtkTreeViewColumn *col;
	GtkTreeSelection *sel;
	GtkWidget *label;
162

163
164
165
166
	 GtkTreeIter iter;
        GValue val;
        val.g_type = G_TYPE_POINTER;

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
	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
182
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
183
184
185
186
	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, 
187
188
189
190
191
			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
192
193
194
195

	view = gtk_tree_view_new_with_model (GTK_TREE_MODEL(account_store));

	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
196
	g_signal_connect (G_OBJECT (sel), "changed",
197
198
199
			G_CALLBACK (select_account),
			account_store);

200
201
202
	gtk_tree_model_get_value(GTK_TREE_MODEL(account_store), &iter, 3, &val);


Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
203
	gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(account_store),
204
205
206
			2, GTK_SORT_ASCENDING);

	rend = gtk_cell_renderer_text_new();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
207
	col = gtk_tree_view_column_new_with_attributes ("Alias",
208
209
210
			rend,
			"markup", 0,
			NULL);
211
	g_object_set(G_OBJECT(rend), "weight", "bold", NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
212
213
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);

214
215
216
217
218
	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
219
220
221
222
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);

	rend = gtk_cell_renderer_text_new();
	col = gtk_tree_view_column_new_with_attributes ("Status",
223
224
225
			rend,
			"markup", 2,
			NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
226
227
228
229
230
231
232
233
234
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);
	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
235
	gtk_widget_show (bbox); 
236

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
237
	addButton = gtk_button_new_from_stock (GTK_STOCK_ADD);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
238
	g_signal_connect_swapped(G_OBJECT(addButton), "clicked",
239
			G_CALLBACK(add_account), NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
240
241
	gtk_box_pack_start(GTK_BOX(bbox), addButton, FALSE, FALSE, 0);
	gtk_widget_show(addButton);
242

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
243
244
	editButton = gtk_button_new_from_stock (GTK_STOCK_EDIT);
	g_signal_connect_swapped(G_OBJECT(editButton), "clicked",
245
			G_CALLBACK(edit_account), NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
246
247
248
249
250
	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",
251
			G_CALLBACK(delete_account), NULL);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
252
253
254
	gtk_box_pack_start(GTK_BOX(bbox), deleteButton, FALSE, FALSE, 0);
	gtk_widget_show(deleteButton);
	
255
256
257
258
259
260
	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
261
	gtk_widget_show_all(ret);
262

263
	config_window_fill_account_list();
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
264
265
266
267

	return ret;
}

268
	void
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
269
270
show_config_window ()
{
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
	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
289
	gtk_box_pack_start (GTK_BOX (dialog->vbox), notebook, TRUE, TRUE, 0);
290
	gtk_container_set_border_width (GTK_CONTAINER(notebook), 10);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
291
	gtk_widget_show(notebook);
292
293
294
295

	/* Create tabs */ 
	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
296
	gtk_notebook_page_num(GTK_NOTEBOOK(notebook), tab);
297
298
299
300
	
	gtk_dialog_run (dialog);

	dialogOpen = FALSE;
301

302
	gtk_widget_destroy (GTK_WIDGET(dialog));
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
303
304
}