35a8b38260d67e2e6ff733d679a0cf98add56a6a
[modest] / src / maemo / modest-connection-specific-smtp-window.c
1 /* connection-specific-smtp-window.c */
2
3 #include "modest-connection-specific-smtp-window.h"
4 #include "modest-connection-specific-smtp-edit-window.h"
5 #include <modest-account-mgr-helpers.h>
6 #include <maemo/modest-maemo-ui-constants.h>
7
8 #include <modest-runtime.h>
9 #include <tny-maemo-conic-device.h>
10
11 #include <gtk/gtktreeview.h>
12 #include <gtk/gtkcellrenderertext.h>
13 #include <gtk/gtkliststore.h>
14 #include <gtk/gtkscrolledwindow.h>
15 #include <gtk/gtkbutton.h>
16 #include <gtk/gtkhbox.h>
17 #include <gtk/gtkvbox.h>
18 #include <gtk/gtkstock.h>
19
20 #include <glib/gi18n.h>
21
22 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window, GTK_TYPE_WINDOW);
23
24 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
25         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
26
27 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
28
29 struct _ModestConnectionSpecificSmtpWindowPrivate
30 {
31         GtkTreeView *treeview;
32         GtkTreeModel *model;
33         GtkWidget *button_edit;
34         
35         ModestAccountMgr *account_manager;
36         gchar* account_name;
37 };
38
39 static void
40 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
41                                                                                                                         GValue *value, GParamSpec *pspec)
42 {
43         switch (property_id) {
44         default:
45                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
46         }
47 }
48
49 static void
50 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
51                                                                                                                         const GValue *value, GParamSpec *pspec)
52 {
53         switch (property_id) {
54         default:
55                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
56         }
57 }
58
59 static void
60 modest_connection_specific_smtp_window_dispose (GObject *object)
61 {
62         if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
63                 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
64 }
65
66 enum MODEL_COLS {
67         MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
68         MODEL_COL_ID = 1, /* libconic IAP ID: a string */
69         MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
70         MODEL_COL_SERVER_NAME = 3, /* a string */
71         MODEL_COL_SERVER_ACCOUNT_DATA = 4 /* a gpointer */
72 };
73
74
75 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self);
76
77 static void
78 modest_connection_specific_smtp_window_finalize (GObject *object)
79 {
80         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
81         
82         /* Free all the data items from the treemodel: */
83         GtkTreeIter iter;
84         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
85         while (valid) {
86                 ModestServerAccountData *data = NULL;
87                 
88                 gtk_tree_model_get (priv->model, &iter, 
89                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
90                                     -1);
91                                  
92                 if (data)
93                         modest_account_mgr_free_server_account_data (priv->account_manager, data);
94                         
95                 /* Get next row: */
96                 valid = gtk_tree_model_iter_next (priv->model, &iter);
97         }
98         
99         g_object_unref (G_OBJECT (priv->model));
100         g_free (priv->account_name);
101         
102         G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
103 }
104
105 static void
106 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
107 {
108         GObjectClass *object_class = G_OBJECT_CLASS (klass);
109
110         g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
111
112         object_class->get_property = modest_connection_specific_smtp_window_get_property;
113         object_class->set_property = modest_connection_specific_smtp_window_set_property;
114         object_class->dispose = modest_connection_specific_smtp_window_dispose;
115         object_class->finalize = modest_connection_specific_smtp_window_finalize;
116 }
117
118 void
119 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager,
120         const gchar* account_name)
121 {
122         ModestConnectionSpecificSmtpWindowPrivate *priv = 
123                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
124         priv->account_manager = account_manager;
125         priv->account_name = account_name ? g_strdup (account_name) : NULL;
126         
127         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
128         
129         TnyDevice *device = modest_runtime_get_device ();
130         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
131         
132         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
133         
134         /* Get the list of Internet Access Points: */
135         GSList* list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
136         printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps));
137         
138         GSList* iter = list_iaps;
139         while (iter) {
140                 ConIcIap *iap = (ConIcIap*)iter->data;
141                 if (iap) {
142                         const gchar *name = con_ic_iap_get_name (iap);
143                         const gchar *id = con_ic_iap_get_id (iap);
144                         printf ("debug: iac name=%s, id=%s\n", name, id);
145                         
146                         /* Get any already-associated connection-specific server account: */
147                         gchar *server_account_name = NULL;
148                         if (priv->account_name)
149                                 server_account_name = modest_account_mgr_get_connection_specific_smtp (
150                                         priv->account_manager, priv->account_name, name);
151                                         
152                         /* Add the row to the model: */
153                         GtkTreeIter iter;
154                         gtk_list_store_append (liststore, &iter);
155                         gtk_list_store_set(liststore, &iter, 
156                                 MODEL_COL_ID, id, 
157                                 MODEL_COL_NAME, name,
158                                 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
159                                 -1);
160                                 
161                         g_free (server_account_name);
162                 }
163                 
164                 iter = g_slist_next (iter);     
165         }
166                 
167         if (list_iaps)
168                 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
169                 
170         update_model_server_names (self);
171 }
172         
173 static void
174 on_button_edit (GtkButton *button, gpointer user_data)
175 {
176         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
177         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
178         
179         gchar *id = NULL;
180         gchar *connection_name = NULL;
181         gchar *server_account_name = NULL;
182         ModestServerAccountData *data = NULL;
183         GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
184         GtkTreeIter iter;
185         GtkTreeModel *model = 0;
186         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
187                 gtk_tree_model_get (priv->model, &iter, 
188                                     MODEL_COL_ID, &id, 
189                                     MODEL_COL_NAME, &connection_name, 
190                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
191                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
192                                     -1);
193         
194                 /* TODO: Is 0 an allowed libconic IAP ID? 
195                  * If not then we should check for it. */
196                 
197                 /* Get existing server account data if a server account is already specified: */
198                 ModestServerAccountData *data_retrieved = NULL;
199                 if (server_account_name && !data) {
200                         data_retrieved = modest_account_mgr_get_server_account_data (priv->account_manager, 
201                                 server_account_name);
202                         data =  data_retrieved;
203                 }
204                 
205                 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
206                 modest_connection_specific_smtp_edit_window_set_connection (
207                         MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
208                         
209                 if (data_retrieved)
210                         modest_account_mgr_free_server_account_data (priv->account_manager, data_retrieved);
211                         
212                 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
213                 gint response = gtk_dialog_run (GTK_DIALOG (window));
214                 gtk_widget_hide (window);
215                 
216                 if (response == GTK_RESPONSE_OK) {
217                         /* Delete any previous data for this row: */
218                         if (data) 
219                         {
220                                 modest_account_mgr_free_server_account_data (priv->account_manager, data);
221                                 data = NULL;
222                         }
223                         
224                         /* Get the new account data and save it in the row for later:
225                          * We free this in finalize(). */
226                         data = modest_connection_specific_smtp_edit_window_get_settings (
227                                                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), 
228                                                 priv->account_manager, server_account_name);    
229                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
230                                             MODEL_COL_SERVER_ACCOUNT_DATA, data,
231                                             -1);
232                 }
233         }
234         
235         g_free (connection_name);
236         g_free (id);
237         g_free (server_account_name);
238 }
239
240 static void
241 on_button_cancel (GtkButton *button, gpointer user_data)
242 {
243         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
244
245         /* Hide the window.
246          * The code that showed it will respond to the hide signal. */  
247         gtk_widget_hide (GTK_WIDGET (self));
248 }
249
250 static void
251 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
252 {
253         ModestConnectionSpecificSmtpWindowPrivate *priv = 
254                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
255
256         GtkTreeModel *model = NULL;
257         GtkTreeIter iter;
258         const gboolean has_selection =
259                 gtk_tree_selection_get_selected (sel, &model, &iter);
260
261         gtk_widget_set_sensitive (priv->button_edit, has_selection);
262 }
263
264 static void
265 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
266 {
267         /* This seems to be necessary to make the window show at the front with decoration.
268          * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration, 
269          * then the window will be below the others. */
270         gtk_window_set_type_hint (GTK_WINDOW (self),
271                             GDK_WINDOW_TYPE_HINT_DIALOG);
272                             
273         ModestConnectionSpecificSmtpWindowPrivate *priv = 
274                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
275
276         /* Create a tree model for the tree view:
277          * with a string for the name, a string for the server name, and an int for the ID.
278          * This must match our MODEL_COLS enum constants.
279          */
280         priv->model = GTK_TREE_MODEL (gtk_list_store_new (4, 
281                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
282
283         /* Setup the tree view: */
284         priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
285
286         /* name column:
287          * The ID model column in not shown in the view. */
288         GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
289         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
290         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
291         gtk_tree_view_column_set_attributes (view_column, renderer, 
292         "text", MODEL_COL_NAME, NULL);
293         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
294         gtk_tree_view_append_column (priv->treeview, view_column);
295
296         
297         /* server name column: */
298         view_column = gtk_tree_view_column_new ();
299         renderer = gtk_cell_renderer_text_new ();
300         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
301         gtk_tree_view_column_set_attributes (view_column, renderer, 
302         "text", MODEL_COL_SERVER_NAME, NULL);
303         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
304         gtk_tree_view_append_column (priv->treeview, view_column);
305         
306         /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
307         
308         GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
309         
310         /* Put the treeview in a scrolled window and add it to the box: */
311         GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
312         gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
313         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, 
314                 GTK_POLICY_AUTOMATIC);
315         gtk_widget_show (scrolled_window);
316         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
317         gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
318         gtk_widget_show (GTK_WIDGET (priv->treeview));
319         
320         /* Add the buttons: */
321         GtkWidget *hbox = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
322         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, MODEST_MARGIN_HALF);
323         gtk_widget_show (hbox);
324         
325         priv->button_edit = gtk_button_new_from_stock (GTK_STOCK_EDIT);
326         gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, MODEST_MARGIN_HALF);
327         gtk_widget_show (priv->button_edit);
328         g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
329                 G_CALLBACK (on_button_edit), self);
330         
331         GtkWidget *button_cancel = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
332         gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, MODEST_MARGIN_HALF);
333         gtk_widget_show (button_cancel);
334         g_signal_connect (G_OBJECT (button_cancel), "clicked",
335                 G_CALLBACK (on_button_cancel), self);
336         
337         gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
338         gtk_widget_show (vbox);
339         
340         /* Disable the Edit button when nothing is selected: */
341         GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
342         g_signal_connect (sel, "changed",
343                           G_CALLBACK(on_selection_changed), self);
344         on_selection_changed (sel, self);
345 }
346
347 ModestConnectionSpecificSmtpWindow*
348 modest_connection_specific_smtp_window_new (void)
349 {
350         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
351 }
352
353 /** The application should call this when the user changes should be saved.
354  * @account_name: Specify this again in case it was not previously known.
355  */
356 gboolean
357 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self, 
358         const gchar* account_name)
359 {
360         ModestConnectionSpecificSmtpWindowPrivate *priv = 
361                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
362         
363         
364         /* Get the first iter in the list */
365         GtkTreeIter iter;
366         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
367
368         /* Walk through the list, reading each row */
369         while (valid) {
370         gchar *id = NULL;
371                 gchar *connection_name = NULL;
372                 gchar *server_account_name = NULL;
373                 ModestServerAccountData *data = NULL;
374                 
375                 gtk_tree_model_get (priv->model, &iter, 
376                                     MODEL_COL_ID, &id, 
377                                     MODEL_COL_NAME, &connection_name, 
378                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
379                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
380                                     -1);
381                                  
382                 gboolean success = TRUE;   
383                 if (id && data) { /* The presence of data suggests that there is something to save. */
384                         if (!server_account_name) {
385                                 /* Add a new server account, building a (non-human-visible) name: */
386                                 gchar *name_start = g_strdup_printf("%s_specific_%s", 
387                                         priv->account_name, connection_name);
388                                 server_account_name = modest_account_mgr_get_unused_account_name (
389                                         priv->account_manager, name_start, TRUE /* server account. */);
390                                 g_free (name_start);
391                                 
392                                 success = modest_account_mgr_add_server_account (priv->account_manager,
393                                         server_account_name,
394                                         data->hostname,
395                                         data->username, data->password,
396                                         MODEST_PROTOCOL_TRANSPORT_SMTP,
397                                         data->security,
398                                         data->secure_auth);
399                                         
400                                 /* associate the specific server account with this connection for this account: */
401                                 success = success && modest_account_mgr_set_connection_specific_smtp (
402                                         priv->account_manager, priv->account_name,
403                                          connection_name, server_account_name);
404         
405                                 /* Save the new name in the treemodel, so it can be edited again later: */
406                                 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
407                                         MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
408                                 
409                         } else {
410                                 /* Change an existing server account: */
411                                 success = modest_account_mgr_set_string (priv->account_manager, server_account_name,
412                                         MODEST_ACCOUNT_HOSTNAME, data->hostname, TRUE /* server account */);
413                                                         
414                                 success = success &&  modest_account_mgr_set_string (priv->account_manager, server_account_name,
415                                         MODEST_ACCOUNT_USERNAME, data->username, TRUE /* server account */);
416                                                         
417                                 success = success &&  modest_account_mgr_set_string (priv->account_manager, server_account_name,
418                                         MODEST_ACCOUNT_PASSWORD, data->password, TRUE /*  server account */);
419                                                 
420                                 modest_server_account_set_secure_auth (priv->account_manager, server_account_name, 
421                                         data->secure_auth);
422                                                 
423                                 modest_server_account_set_security (priv->account_manager, server_account_name, 
424                                         data->security);
425                                 
426                                 modest_account_mgr_set_int (priv->account_manager, server_account_name,
427                                                 MODEST_ACCOUNT_PORT, data->port, TRUE /* server account */);
428                         }
429                 }
430                 
431                 g_free (connection_name);
432                 g_free (id);
433                 g_free (server_account_name);
434                 
435                 if (!success)
436                         return FALSE;
437                         
438                 /* Get next row: */
439                 valid = gtk_tree_model_iter_next (priv->model, &iter);
440         }
441         
442         update_model_server_names (self);
443         
444         return TRUE;
445 }
446
447 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
448 {
449         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
450
451         GtkTreeIter iter;
452         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
453         while (valid) {
454                 
455                 gchar *server_account_name = NULL;
456                 gtk_tree_model_get (priv->model, &iter, 
457                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
458                                     -1);
459                                  
460                 if (server_account_name) {
461                         /* Get the server hostname and show it in the treemodel: */     
462                         gchar *hostname = modest_account_mgr_get_string (priv->account_manager, 
463                                 server_account_name, MODEST_ACCOUNT_HOSTNAME, TRUE /* server account */);
464                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
465                                             MODEL_COL_SERVER_NAME, hostname,
466                                             -1);
467                         g_free (hostname);
468                 }
469                         
470                 /* Get next row: */
471                 valid = gtk_tree_model_iter_next (priv->model, &iter);
472         }
473 }
474