2007-06-02 Armin Burgmeier <armin@openismus.com>
[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 "widgets/modest-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 /* libconic does not return a list of connections in scratchbox,
119  * so enable this to put a fake row in the list,
120  * so we can test other parts of the code. */
121 /* #define DEBUG_WITHOUT_LIBCONIC 1 */
122
123 void
124 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager,
125         const gchar* account_name)
126 {
127         ModestConnectionSpecificSmtpWindowPrivate *priv = 
128                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
129         priv->account_manager = account_manager;
130         priv->account_name = account_name ? g_strdup (account_name) : NULL;
131         
132         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
133         
134         TnyDevice *device = modest_runtime_get_device ();
135         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
136         
137         /* Get the list of Internet Access Points: */
138         #ifdef DEBUG_WITHOUT_LIBCONIC
139         GSList *list_iaps = g_slist_append(NULL, (gpointer)1);
140         #else
141         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
142         GSList *list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
143         #endif
144         
145         /* printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps)); */
146         
147         GSList* iter = list_iaps;
148         while (iter) {
149                 ConIcIap *iap = (ConIcIap*)iter->data;
150                 if (iap) {
151                         #ifdef DEBUG_WITHOUT_LIBCONIC
152                         const gchar *name = "debug name";
153                         const gchar *id = "debug id";
154                         #else
155                         const gchar *name = con_ic_iap_get_name (iap);
156                         const gchar *id = con_ic_iap_get_id (iap);
157                         #endif
158                         
159                         printf ("debug: iac name=%s, id=%s\n", name, id);
160                         
161                         /* Get any already-associated connection-specific server account: */
162                         gchar *server_account_name = NULL;
163                         if (priv->account_name)
164                                 server_account_name = modest_account_mgr_get_connection_specific_smtp (
165                                         priv->account_manager, priv->account_name, name);
166                                         
167                         /* Add the row to the model: */
168                         GtkTreeIter iter;
169                         gtk_list_store_append (liststore, &iter);
170                         gtk_list_store_set(liststore, &iter, 
171                                 MODEL_COL_ID, id, 
172                                 MODEL_COL_NAME, name,
173                                 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
174                                 -1);
175                                 
176                         g_free (server_account_name);
177                 }
178                 
179                 iter = g_slist_next (iter);     
180         }
181                 
182         #ifndef DEBUG_WITHOUT_LIBCONIC
183         if (list_iaps)
184                 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
185         #endif
186                 
187         update_model_server_names (self);
188 }
189         
190 static void
191 on_button_edit (GtkButton *button, gpointer user_data)
192 {
193         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
194         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
195         
196         gchar *id = NULL;
197         gchar *connection_name = NULL;
198         gchar *server_account_name = NULL;
199         ModestServerAccountData *data = NULL;
200         GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
201         GtkTreeIter iter;
202         GtkTreeModel *model = 0;
203         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
204                 gtk_tree_model_get (priv->model, &iter, 
205                                     MODEL_COL_ID, &id, 
206                                     MODEL_COL_NAME, &connection_name, 
207                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
208                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
209                                     -1);
210         
211                 /* printf("DEBUG: %s: BEFORE: connection-specific server_account_name=%s\n", __FUNCTION__, server_account_name); */
212                 /* TODO: Is 0 an allowed libconic IAP ID? 
213                  * If not then we should check for it. */
214                 
215                 /* Get existing server account data if a server account is already specified: */
216                 gboolean data_was_retrieved = FALSE;
217                 if (server_account_name && !data) {
218                         data = modest_account_mgr_get_server_account_data (priv->account_manager, 
219                                 server_account_name);
220                         if (data)
221                                 data_was_retrieved = TRUE;
222                 }
223                 
224                 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
225                 modest_connection_specific_smtp_edit_window_set_connection (
226                         MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
227                         
228                 /* Delete data, unless it was data from the rowmodel: */
229                 if (data_was_retrieved) {
230                         modest_account_mgr_free_server_account_data (priv->account_manager, data);
231                         data = NULL;
232                 }
233                         
234                 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
235                 gint response = gtk_dialog_run (GTK_DIALOG (window));
236                 gtk_widget_hide (window);
237                 
238                 if (response == GTK_RESPONSE_OK) {
239                         /* Delete any previous data for this row: */
240                         if (data) 
241                         {
242                                 modest_account_mgr_free_server_account_data (priv->account_manager, data);
243                                 data = NULL;
244                         }
245                         
246                         /* Get the new account data and save it in the row for later:
247                          * We free this in finalize(),
248                          * and save it to our configuration in 
249                          * modest_connection_specific_smtp_window_save_server_accounts(). */
250                         data = modest_connection_specific_smtp_edit_window_get_settings (
251                                                 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), 
252                                                 priv->account_manager);
253                         
254                         const gchar* server_name = data ? data->hostname : NULL;
255                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
256                                         MODEL_COL_SERVER_ACCOUNT_DATA, data,
257                                         MODEL_COL_SERVER_NAME, server_name,
258                                         -1);
259                 }
260         }
261         
262         g_free (connection_name);
263         g_free (id);
264         g_free (server_account_name);
265 }
266
267 static void
268 on_button_cancel (GtkButton *button, gpointer user_data)
269 {
270         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
271
272         /* Hide the window.
273          * The code that showed it will respond to the hide signal. */  
274         gtk_widget_hide (GTK_WIDGET (self));
275 }
276
277 static void
278 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
279 {
280         ModestConnectionSpecificSmtpWindowPrivate *priv = 
281                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
282
283         GtkTreeModel *model = NULL;
284         GtkTreeIter iter;
285         const gboolean has_selection =
286                 gtk_tree_selection_get_selected (sel, &model, &iter);
287
288         gtk_widget_set_sensitive (priv->button_edit, has_selection);
289 }
290
291 static void
292 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
293 {
294         ModestWindowMgr *mgr;
295
296         /* Specify a default size, because the GtkTreeView's default requested size  
297          * is not big enough: */
298         gtk_window_set_default_size (GTK_WINDOW (self), 500, 300);
299         
300         /* This seems to be necessary to make the window show at the front with decoration.
301          * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration, 
302          * then the window will be below the others. */
303         gtk_window_set_type_hint (GTK_WINDOW (self),
304                             GDK_WINDOW_TYPE_HINT_DIALOG);
305                             
306         ModestConnectionSpecificSmtpWindowPrivate *priv = 
307                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
308
309         /* Create a tree model for the tree view:
310          * with a string for the name, a string for the server name, and an int for the ID.
311          * This must match our MODEL_COLS enum constants.
312          */
313         priv->model = GTK_TREE_MODEL (gtk_list_store_new (5, 
314                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
315
316         /* Setup the tree view: */
317         priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
318
319         /* Show the column headers,
320          * which does not seem to be the default on Maemo.
321          */                     
322         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
323         
324         /* name column:
325          * The ID model column in not shown in the view. */
326         GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
327         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
328         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
329         gtk_tree_view_column_set_attributes (view_column, renderer, 
330         "text", MODEL_COL_NAME, NULL);
331         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
332         gtk_tree_view_append_column (priv->treeview, view_column);
333
334         
335         /* server name column: */
336         view_column = gtk_tree_view_column_new ();
337         renderer = gtk_cell_renderer_text_new ();
338         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
339         gtk_tree_view_column_set_attributes (view_column, renderer, 
340         "text", MODEL_COL_SERVER_NAME, NULL);
341         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
342         gtk_tree_view_append_column (priv->treeview, view_column);
343         
344         /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
345         
346         GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
347
348         /* Introductory note: */
349         GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
350         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
351         /* So that it is shown without being truncated: */
352         gtk_label_set_max_width_chars (GTK_LABEL (label), 40);
353         gtk_widget_show (label);
354         gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
355         
356         /* Put the treeview in a scrolled window and add it to the box: */
357         GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
358         gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
359         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER, 
360                 GTK_POLICY_AUTOMATIC);
361         gtk_widget_show (scrolled_window);
362         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
363         gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
364         gtk_widget_show (GTK_WIDGET (priv->treeview));
365         
366         /* Add the buttons: */
367         GtkWidget *hbox = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
368         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, MODEST_MARGIN_HALF);
369         gtk_widget_show (hbox);
370         
371         priv->button_edit = gtk_button_new_from_stock (GTK_STOCK_EDIT);
372         gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, MODEST_MARGIN_HALF);
373         gtk_widget_show (priv->button_edit);
374         g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
375                 G_CALLBACK (on_button_edit), self);
376         
377         GtkWidget *button_cancel = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
378         gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, MODEST_MARGIN_HALF);
379         gtk_widget_show (button_cancel);
380         g_signal_connect (G_OBJECT (button_cancel), "clicked",
381                 G_CALLBACK (on_button_cancel), self);
382         
383         gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
384         gtk_widget_show (vbox);
385         
386         /* Disable the Edit button when nothing is selected: */
387         GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
388         g_signal_connect (sel, "changed",
389                           G_CALLBACK(on_selection_changed), self);
390         on_selection_changed (sel, self);
391         
392         /* When this window is shown, hibernation should not be possible, 
393          * because there is no sensible way to save the state: */
394         mgr = modest_runtime_get_window_mgr ();
395         modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr, 
396                                                                      GTK_WINDOW (self)); 
397
398         /* Set window title */
399         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
400 }
401
402 ModestConnectionSpecificSmtpWindow*
403 modest_connection_specific_smtp_window_new (void)
404 {
405         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
406 }
407
408 /** The application should call this when the user changes should be saved.
409  * @account_name: Specify this again in case it was not previously known.
410  */
411 gboolean
412 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self, 
413         const gchar* account_name)
414 {
415         ModestConnectionSpecificSmtpWindowPrivate *priv = 
416                 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
417         
418         
419         /* Get the first iter in the list */
420         GtkTreeIter iter;
421         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
422
423         /* Walk through the list, reading each row */
424         while (valid) {
425         gchar *id = NULL;
426                 gchar *connection_name = NULL;
427                 gchar *server_account_name = NULL;
428                 ModestServerAccountData *data = NULL;
429                 
430                 gtk_tree_model_get (priv->model, &iter, 
431                                     MODEL_COL_ID, &id, 
432                                     MODEL_COL_NAME, &connection_name, 
433                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
434                                     MODEL_COL_SERVER_ACCOUNT_DATA, &data,
435                                     -1);
436                                  
437                 gboolean success = TRUE;   
438                 if (id && data) { /* The presence of data suggests that there is something to save. */
439                         if (!server_account_name) {
440                                 /* Add a new server account, building a (non-human-visible) name: */
441                                 gchar *name_start = g_strdup_printf("%s_specific_%s", 
442                                         priv->account_name, connection_name);
443                                 server_account_name = modest_account_mgr_get_unused_account_name (
444                                         priv->account_manager, name_start, TRUE /* server account. */);
445                                 g_assert (server_account_name);
446                                 g_free (name_start);
447                                 
448                                 success = modest_account_mgr_add_server_account (priv->account_manager,
449                                                                                  server_account_name,
450                                                                                  data->hostname, 0,
451                                                                                  data->username, data->password,
452                                                                                  MODEST_PROTOCOL_TRANSPORT_SMTP,
453                                                                                  data->security,
454                                                                                  data->secure_auth);
455                                         
456                                 /* associate the specific server account with this connection for this account: */
457                                 success = success && modest_account_mgr_set_connection_specific_smtp (
458                                         priv->account_manager, priv->account_name,
459                                          connection_name, server_account_name);
460         
461                                 /* Save the new name in the treemodel, so it can be edited again later: */
462                                 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
463                                         MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
464                                 
465                         } else {
466                                 /* Change an existing server account: */
467                                 success = modest_account_mgr_set_string (priv->account_manager, server_account_name,
468                                         MODEST_ACCOUNT_HOSTNAME, data->hostname, TRUE /* server account */);
469                                                 
470                                 modest_server_account_set_username (priv->account_manager, server_account_name,
471                                         data->username);
472                                                         
473                                 modest_server_account_set_password (priv->account_manager, server_account_name,
474                                         data->password);
475                                                 
476                                 modest_server_account_set_secure_auth (priv->account_manager, server_account_name, 
477                                         data->secure_auth);
478                                                 
479                                 modest_server_account_set_security (priv->account_manager, server_account_name, 
480                                         data->security);
481                                 
482                                 modest_account_mgr_set_int (priv->account_manager, server_account_name,
483                                                 MODEST_ACCOUNT_PORT, data->port, TRUE /* server account */);
484                         }
485                 }
486                 
487                 g_free (connection_name);
488                 g_free (id);
489                 g_free (server_account_name);
490                 
491                 if (!success)
492                         return FALSE;
493                         
494                 /* Get next row: */
495                 valid = gtk_tree_model_iter_next (priv->model, &iter);
496         }
497         
498         update_model_server_names (self);
499         
500         return TRUE;
501 }
502
503 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
504 {
505         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
506
507         GtkTreeIter iter;
508         gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
509         while (valid) {
510                 
511                 gchar *server_account_name = NULL;
512                 gtk_tree_model_get (priv->model, &iter, 
513                                     MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
514                                     -1);
515                                  
516                 if (server_account_name) {
517                         /* Get the server hostname and show it in the treemodel: */     
518                         gchar *hostname = modest_account_mgr_get_string (priv->account_manager, 
519                                 server_account_name, MODEST_ACCOUNT_HOSTNAME, TRUE /* server account */);
520                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter, 
521                                             MODEL_COL_SERVER_NAME, hostname,
522                                             -1);
523                         g_free (hostname);
524                 } else {
525                         gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
526                                             MODEL_COL_SERVER_NAME, _("mcen_ia_optionalsmtp_notdefined"),
527                                             -1);
528                 }
529                         
530                 /* Get next row: */
531                 valid = gtk_tree_model_iter_next (priv->model, &iter);
532         }
533 }
534