1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "modest-connection-specific-smtp-window.h"
31 #include "modest-connection-specific-smtp-edit-window.h"
32 #include <modest-account-mgr-helpers.h>
33 #include "widgets/modest-ui-constants.h"
35 #include <modest-runtime.h>
38 #include <tny-maemo-conic-device.h>
41 #include <gtk/gtktreeview.h>
42 #include <gtk/gtkcellrenderertext.h>
43 #include <gtk/gtkliststore.h>
44 #include <modest-scrollable.h>
45 #include <modest-toolkit-factory.h>
46 #include <gtk/gtkbutton.h>
47 #include <gtk/gtkhbox.h>
48 #include <gtk/gtkvbox.h>
49 #include <gtk/gtkstock.h>
51 #include "modest-platform.h"
53 #ifdef MODEST_TOOLKIT_HILDON2
54 #include <hildon/hildon.h>
57 #include <glib/gi18n.h>
60 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window,
63 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
64 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
66 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
68 struct _ModestConnectionSpecificSmtpWindowPrivate
70 GtkTreeView *treeview;
72 GtkWidget *no_connection_label;
73 GtkWidget *scrollable;
74 ModestAccountMgr *account_manager;
77 static void on_response (GtkDialog *dialog,
81 /* static gboolean on_key_pressed (GtkWidget *self, GdkEventKey *event, gpointer user_data); */
84 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
85 GValue *value, GParamSpec *pspec)
87 switch (property_id) {
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
94 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
95 const GValue *value, GParamSpec *pspec)
97 switch (property_id) {
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
104 modest_connection_specific_smtp_window_dispose (GObject *object)
106 if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
107 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
111 MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
112 MODEL_COL_ID = 1, /* libconic IAP ID: a string */
113 MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
114 MODEL_COL_SERVER_NAME = 3, /* a string */
115 MODEL_COL_SERVER_ACCOUNT_SETTINGS = 4 /* a gpointer */
119 static void update_model_server_names (ModestConnectionSpecificSmtpWindow *self);
122 modest_connection_specific_smtp_window_finalize (GObject *object)
124 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
126 /* Free all the data items from the treemodel: */
128 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
130 ModestServerAccountSettings *server_settings = NULL;
132 gtk_tree_model_get (priv->model, &iter,
133 MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
137 g_object_unref (server_settings);
140 valid = gtk_tree_model_iter_next (priv->model, &iter);
143 g_object_unref (G_OBJECT (priv->model));
145 g_object_unref (priv->treeview);
146 g_object_unref (priv->no_connection_label);
148 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
152 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
154 GObjectClass *object_class = G_OBJECT_CLASS (klass);
156 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
158 object_class->get_property = modest_connection_specific_smtp_window_get_property;
159 object_class->set_property = modest_connection_specific_smtp_window_set_property;
160 object_class->dispose = modest_connection_specific_smtp_window_dispose;
161 object_class->finalize = modest_connection_specific_smtp_window_finalize;
164 /* libconic does not return a list of connections in scratchbox,
165 * so enable this to put a fake row in the list,
166 * so we can test other parts of the code. */
167 /* #define DEBUG_WITHOUT_LIBCONIC 1 */
170 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self,
171 ModestAccountMgr *account_manager)
173 gboolean empty = TRUE;
174 ModestConnectionSpecificSmtpWindowPrivate *priv =
175 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
176 priv->account_manager = account_manager;
177 #ifdef MODEST_HAVE_CONIC
178 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
180 TnyDevice *device = modest_runtime_get_device ();
181 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
183 /* Get the list of Internet Access Points: */
184 #ifdef DEBUG_WITHOUT_LIBCONIC
185 GSList *list_iaps = g_slist_append(NULL, (gpointer)1);
187 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
188 GSList *list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
191 /* printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps)); */
193 GSList* iter = list_iaps;
194 if (list_iaps != NULL)
198 ConIcIap *iap = (ConIcIap*)iter->data;
200 #ifdef DEBUG_WITHOUT_LIBCONIC
201 const gchar *connection_name = "debug name";
202 const gchar *connection_id = "debug id";
204 const gchar *connection_name = con_ic_iap_get_name (iap);
205 const gchar *connection_id = con_ic_iap_get_id (iap);
208 printf ("debug: iac name=%s, id=%s\n", connection_name, connection_id);
210 /* Get any already-associated connection-specific server account: */
211 gchar *server_account_name = NULL;
212 server_account_name = modest_account_mgr_get_connection_specific_smtp (
213 priv->account_manager, connection_id);
215 /* Add the row to the model: */
217 gtk_list_store_append (liststore, &iter);
218 gtk_list_store_set(liststore, &iter,
219 MODEL_COL_ID, connection_id,
220 MODEL_COL_NAME, connection_name,
221 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
224 if (server_account_name)
225 g_free (server_account_name);
228 iter = g_slist_next (iter);
231 #ifndef DEBUG_WITHOUT_LIBCONIC
233 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
236 update_model_server_names (self);
237 #endif /*MODEST_HAVE_CONIC */
240 child = gtk_bin_get_child (GTK_BIN (priv->scrollable));
242 gtk_container_remove (GTK_CONTAINER (priv->scrollable), child);
246 modest_scrollable_add_with_viewport (MODEST_SCROLLABLE (priv->scrollable),
247 priv->no_connection_label);
248 gtk_widget_show (priv->no_connection_label);
250 gtk_container_add (GTK_CONTAINER (priv->scrollable), GTK_WIDGET (priv->treeview));
251 gtk_widget_show (GTK_WIDGET (priv->treeview));
256 edit_account (ModestConnectionSpecificSmtpWindow *self, GtkTreePath *path)
258 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
259 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
262 gchar *connection_name = NULL;
263 gchar *server_account_name = NULL;
264 ModestServerAccountSettings *server_settings = NULL;
266 if (gtk_tree_model_get_iter (priv->model, &iter, path)) {
267 gtk_tree_model_get (priv->model, &iter,
269 MODEL_COL_NAME, &connection_name,
270 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
271 MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
274 /* Get existing server account data if a server account is already specified: */
275 gboolean settings_were_retrieved = FALSE;
276 if (server_account_name && !server_settings) {
277 server_settings = modest_account_mgr_load_server_settings(mgr, server_account_name, TRUE);
279 settings_were_retrieved = TRUE;
282 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
283 modest_connection_specific_smtp_edit_window_set_connection (
284 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, server_settings);
286 /* Delete data, unless it was data from the rowmodel: */
287 if (settings_were_retrieved) {
288 g_object_unref (server_settings);
289 server_settings = NULL;
292 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), GTK_WINDOW (window), GTK_WINDOW (self));
294 gint response = gtk_dialog_run (GTK_DIALOG (window));
295 if (response == GTK_RESPONSE_OK) {
297 /* Delete any previous data for this row: */
298 if (server_settings) {
299 g_object_unref (server_settings);
300 server_settings = NULL;
303 /* Get the new account data and save it in the row for later:
304 * We free this in finalize(),
305 * and save it to our configuration in
306 * modest_connection_specific_smtp_window_save_server_accounts(). */
307 server_settings = modest_connection_specific_smtp_edit_window_get_settings (
308 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window));
310 if (server_settings) {
311 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
312 MODEL_COL_SERVER_ACCOUNT_SETTINGS, server_settings,
313 MODEL_COL_SERVER_NAME, modest_server_account_settings_get_hostname (server_settings),
316 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
317 MODEL_COL_SERVER_ACCOUNT_SETTINGS, NULL,
318 MODEL_COL_SERVER_NAME, NULL,
319 MODEL_COL_SERVER_ACCOUNT_NAME, NULL,
323 gtk_widget_destroy (window);
325 g_free (connection_name);
327 g_free (server_account_name);
328 update_model_server_names (self);
332 on_row_activated (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *column,
333 ModestConnectionSpecificSmtpWindow *self)
335 edit_account (self, path);
339 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
341 ModestWindowMgr *mgr;
344 /* Specify a default size */
345 gtk_window_set_default_size (GTK_WINDOW (self), -1, MODEST_DIALOG_WINDOW_MAX_HEIGHT);
347 /* This seems to be necessary to make the window show at the front with decoration.
348 * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration,
349 * then the window will be below the others. */
350 gtk_window_set_type_hint (GTK_WINDOW (self),
351 GDK_WINDOW_TYPE_HINT_DIALOG);
353 ModestConnectionSpecificSmtpWindowPrivate *priv =
354 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
356 /* Create a tree model for the tree view:
357 * with a string for the name, a string for the server name, and an int for the ID.
358 * This must match our MODEL_COLS enum constants.
360 priv->model = GTK_TREE_MODEL (gtk_list_store_new (5,
361 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
363 /* Setup the tree view: */
364 #ifdef MODEST_TOOLKIT_HILDON2
365 priv->treeview = GTK_TREE_VIEW (hildon_gtk_tree_view_new_with_model (HILDON_UI_MODE_NORMAL, priv->model));
367 priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
369 g_object_ref_sink (G_OBJECT (priv->treeview));
371 /* No connections label */
372 priv->no_connection_label = gtk_label_new (_("mcen_ia_optionalsmtp_noconnection"));
373 g_object_ref_sink (G_OBJECT (priv->no_connection_label));
376 * The ID model column in not shown in the view. */
377 GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
378 gtk_tree_view_column_set_expand (view_column, TRUE);
379 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
380 g_object_set (G_OBJECT (renderer), "xpad", MODEST_MARGIN_DOUBLE, NULL);
381 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
382 gtk_tree_view_column_set_attributes (view_column, renderer,
383 "text", MODEL_COL_NAME, NULL);
384 gtk_tree_view_append_column (priv->treeview, view_column);
386 /* server name column: */
387 view_column = gtk_tree_view_column_new ();
388 gtk_tree_view_column_set_expand (view_column, TRUE);
389 renderer = gtk_cell_renderer_text_new ();
390 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
391 gtk_tree_view_column_set_attributes (view_column, renderer,
392 "text", MODEL_COL_SERVER_NAME, NULL);
393 gtk_tree_view_append_column (priv->treeview, view_column);
395 /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
397 GtkWidget *vbox = GTK_DIALOG(self)->vbox;
399 /* Introductory note: */
400 GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
401 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
402 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
403 gtk_misc_set_padding (GTK_MISC (label), MODEST_MARGIN_DOUBLE + MODEST_MARGIN_DOUBLE, MODEST_MARGIN_DOUBLE);
404 gtk_widget_set_size_request (label, 600, -1);
405 gtk_widget_show (label);
406 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
408 /* Put the treeview in a scrollable and add it to the box: */
409 priv->scrollable = modest_toolkit_factory_create_scrollable (modest_runtime_get_toolkit_factory ());
410 align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
411 gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, 0);
412 gtk_widget_show (priv->scrollable);
413 gtk_widget_show (align);
414 gtk_container_add (GTK_CONTAINER (align), GTK_WIDGET (priv->scrollable));
415 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (align), TRUE, TRUE, 0);
416 gtk_widget_show (vbox);
418 g_signal_connect (G_OBJECT (priv->treeview), "row-activated", G_CALLBACK (on_row_activated), self);
420 /* When this window is shown, hibernation should not be possible,
421 * because there is no sensible way to save the state: */
422 mgr = modest_runtime_get_window_mgr ();
423 modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr,
426 /* Set window title */
427 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
429 g_signal_connect (self, "response", G_CALLBACK (on_response), NULL);
432 ModestConnectionSpecificSmtpWindow*
433 modest_connection_specific_smtp_window_new (void)
435 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
438 /** The application should call this when the user changes should be saved.
441 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self)
443 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
444 ModestConnectionSpecificSmtpWindowPrivate *priv =
445 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
448 /* Get the first iter in the list */
450 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
452 /* Walk through the list, reading each row */
455 gchar *connection_name = NULL;
456 gchar *server_account_name = NULL;
457 gchar *server_name = NULL;
458 ModestServerAccountSettings *server_settings = NULL;
460 gtk_tree_model_get (priv->model, &iter,
462 MODEL_COL_NAME, &connection_name,
463 MODEL_COL_SERVER_NAME, &server_name,
464 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
465 MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
468 gboolean success = TRUE;
469 if (id && server_settings) { /* The presence of data suggests that there is something to save. */
470 if (!server_account_name) {
471 /* Add a new server account, building a (non-human-visible) name: */
472 gchar *name_start = g_strdup_printf("specific_%s", connection_name);
473 server_account_name = modest_account_mgr_get_unused_account_name (
474 priv->account_manager, name_start, TRUE /* server account. */);
475 g_assert (server_account_name);
478 modest_server_account_settings_set_account_name (server_settings, server_account_name);
479 success = modest_account_mgr_save_server_settings (mgr, server_settings);
481 TnyAccount *account = TNY_ACCOUNT (modest_tny_account_store_new_connection_specific_transport_account
482 (modest_runtime_get_account_store (),
483 server_account_name));
485 g_object_unref (account);
488 /* associate the specific server account with this connection for this account: */
489 success = success && modest_account_mgr_set_connection_specific_smtp (
490 priv->account_manager, id, server_account_name);
492 /* Save the new name in the treemodel, so it can be edited again later: */
493 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
494 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
497 /* If the account already exists then update it and notify */
498 modest_account_mgr_save_server_settings (mgr, server_settings);
500 } else if (id && server_name &&
501 !strcmp (server_name, _("mcen_ia_optionalsmtp_notdefined"))) {
502 modest_account_mgr_remove_connection_specific_smtp (priv->account_manager,
504 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
505 MODEL_COL_SERVER_ACCOUNT_NAME, NULL, -1);
508 g_free (connection_name);
510 g_free (server_account_name);
511 g_free (server_name);
517 valid = gtk_tree_model_iter_next (priv->model, &iter);
520 update_model_server_names (self);
526 update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
528 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
531 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
534 gchar *server_account_name = NULL;
535 ModestServerAccountSettings *server_settings = NULL;
536 gtk_tree_model_get (priv->model, &iter,
537 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
538 MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
540 if (server_settings && modest_server_account_settings_get_hostname (server_settings)
541 && (modest_server_account_settings_get_hostname (server_settings) [0] != '\0')) {
542 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
543 MODEL_COL_SERVER_NAME, modest_server_account_settings_get_hostname (server_settings),
545 } else if (server_account_name) {
547 /* Get the server hostname and show it in the treemodel: */
548 gchar *hostname = modest_account_mgr_get_server_account_hostname (priv->account_manager,
549 server_account_name);
550 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
551 MODEL_COL_SERVER_NAME, hostname,
555 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
556 MODEL_COL_SERVER_NAME, _("mcen_ia_optionalsmtp_notdefined"),
561 valid = gtk_tree_model_iter_next (priv->model, &iter);
566 on_response (GtkDialog *dialog,
570 modest_connection_specific_smtp_window_save_server_accounts (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog));
571 gtk_widget_destroy (GTK_WIDGET (dialog));