configwindow.c 7.63 KB
Newer Older
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 *  Copyright (C) 2007 Savoir-Faire Linux inc.
 *  Author: Pierre-Luc Beaudoin <pierre-luc.beaudoin@savoirfairelinux.com>
 *                                                                              
 *  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 <accountlist.h>
21
22
#include <accountwindow.h>
#include <actions.h>
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
23
24
25
#include <config.h>
#include <dbus.h>
#include <mainwindow.h>
26

Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <gtk/gtk.h>

/** Local variables */
GtkListStore *account_store;
GtkWidget * addButton;
GtkWidget * editButton;
GtkWidget * deleteButton;

account_t * selectedAccount;

/** Fills the treelist with accounts */
void 
fill_account_list ()
{
	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,
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
53
  				   0, g_hash_table_lookup(a->properties, ACCOUNT_ALIAS),  // Name
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
54
55
  				   1, g_hash_table_lookup(a->properties, ACCOUNT_TYPE),   // Protocol
  				   2, account_state_name(a->state),      // Status
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
56
  				   3, a,                                 // Pointer
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  				   -1);

  	}
  } 
  
  gtk_widget_set_sensitive( GTK_WIDGET(editButton),   FALSE);
  gtk_widget_set_sensitive( GTK_WIDGET(deleteButton), FALSE);
}
/**
 * Delete an account
 */
static void 
delete_account( GtkWidget *widget, gpointer   data )
{
  if(selectedAccount)
  {
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
73
    dbus_remove_account(selectedAccount->accountID);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    fill_account_list ();
  }
}

/**
 * Delete an account
 */
static void 
edit_account( GtkWidget *widget, gpointer   data )
{
  if(selectedAccount)
  {
    show_account_window(selectedAccount);
    fill_account_list ();
  }
}

/* Call back when the user click on an account in the list */
static void 
select_account(GtkTreeSelection *sel, GtkTreeModel *model) 
{
	GtkTreeIter  iter;
	GValue val;
	
	if (! gtk_tree_selection_get_selected (sel, &model, &iter))
	{
	  selectedAccount = NULL;
		return;
  }
  
	val.g_type = G_TYPE_POINTER;
	gtk_tree_model_get_value (model, &iter, 3, &val);
	
	selectedAccount = (account_t*) g_value_get_pointer(&val);
  g_value_unset(&val);
	
	if(selectedAccount)
	{
    gtk_widget_set_sensitive( GTK_WIDGET(editButton),   TRUE);
    /*TODO Set to TRUE when removeAccount is implemented */
    gtk_widget_set_sensitive( GTK_WIDGET(deleteButton), FALSE); 
  }
116
  g_print("select");
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  
}



GtkWidget *
create_accounts_tab()
{
  GtkWidget *ret;
	GtkWidget *sw;
	GtkWidget *view;
	GtkWidget *bbox;
	GtkCellRenderer *rend;
	GtkTreeViewColumn *col;
	GtkTreeSelection *sel;
	GtkWidget *label;
	
	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
149
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
150
151
152
153
154
155
156
157
158
159
160
161
162
	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, 
	  G_TYPE_STRING,  // Name
	  G_TYPE_STRING,  // Protocol
	  G_TYPE_STRING,  // Status
	  G_TYPE_POINTER  // Pointer to the Object
	  );

	view = gtk_tree_view_new_with_model (GTK_TREE_MODEL(account_store));

	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
163
	g_signal_connect (G_OBJECT (sel), "changed",
Pierre-Luc Beaudoin's avatar
Pierre-Luc Beaudoin committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
			  G_CALLBACK (select_account),
			  account_store);
	
	gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(account_store),
										 2, GTK_SORT_ASCENDING);
  
  rend = gtk_cell_renderer_text_new();
	col = gtk_tree_view_column_new_with_attributes ("Alias",
							rend,
							"markup", 0,
							NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW(view), col);

  rend = gtk_cell_renderer_text_new();
  col = gtk_tree_view_column_new_with_attributes ("Protocol",
							rend,
							"markup", 1,
							NULL);
	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",
							rend,
							"markup", 2,
							NULL);
	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);
	gtk_widget_show (bbox);
	
	addButton = gtk_button_new_from_stock (GTK_STOCK_ADD);
	/*g_signal_connect_swapped(G_OBJECT(addButton), "clicked",
							 G_CALLBACK(config_module), _gtkWindow);*/
	gtk_box_pack_start(GTK_BOX(bbox), addButton, FALSE, FALSE, 0);
	gtk_widget_show(addButton);
	/* TODO Set to TRUE when AddAccount is implemented */
	gtk_widget_set_sensitive( GTK_WIDGET(addButton), FALSE); 
	
	editButton = gtk_button_new_from_stock (GTK_STOCK_EDIT);
	g_signal_connect_swapped(G_OBJECT(editButton), "clicked",
							 G_CALLBACK(edit_account), NULL);
	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",
							 G_CALLBACK(delete_account), NULL);
	gtk_box_pack_start(GTK_BOX(bbox), deleteButton, FALSE, FALSE, 0);
	gtk_widget_show(deleteButton);
	
	gtk_widget_show_all(ret);
	
	fill_account_list();

	return ret;
}

void
show_config_window ()
{
  GtkDialog * dialog;
  GtkWidget * notebook;
  GtkWidget * tab;
  
  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 ();
	gtk_box_pack_start (GTK_BOX (dialog->vbox), notebook, TRUE, TRUE, 0);
  gtk_container_set_border_width (GTK_CONTAINER(notebook), 10);
	gtk_widget_show(notebook);
  
  /* Create tabs */ 
  tab = create_accounts_tab();
  gtk_notebook_append_page(GTK_NOTEBOOK(notebook), tab, gtk_label_new("Accounts"));
	gtk_notebook_page_num(GTK_NOTEBOOK(notebook), tab);
  
  gtk_dialog_run (dialog);
  
  gtk_widget_destroy (GTK_WIDGET(dialog));
}