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>
36 #include <tny-maemo-conic-device.h>
38 #include <gtk/gtktreeview.h>
39 #include <gtk/gtkcellrenderertext.h>
40 #include <gtk/gtkliststore.h>
41 #include <gtk/gtkscrolledwindow.h>
42 #include <gtk/gtkbutton.h>
43 #include <gtk/gtkhbox.h>
44 #include <gtk/gtkvbox.h>
45 #include <gtk/gtkstock.h>
47 #include "modest-hildon-includes.h"
48 #include "modest-platform.h"
49 #include "modest-maemo-utils.h"
51 #include <glib/gi18n.h>
54 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window,
57 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
58 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
60 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
62 struct _ModestConnectionSpecificSmtpWindowPrivate
64 GtkTreeView *treeview;
66 GtkWidget *button_edit;
68 ModestAccountMgr *account_manager;
71 static void on_response (GtkDialog *dialog,
75 /* static gboolean on_key_pressed (GtkWidget *self, GdkEventKey *event, gpointer user_data); */
78 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
79 GValue *value, GParamSpec *pspec)
81 switch (property_id) {
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
89 const GValue *value, GParamSpec *pspec)
91 switch (property_id) {
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
98 modest_connection_specific_smtp_window_dispose (GObject *object)
100 if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
101 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
105 MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
106 MODEL_COL_ID = 1, /* libconic IAP ID: a string */
107 MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
108 MODEL_COL_SERVER_NAME = 3, /* a string */
109 MODEL_COL_SERVER_ACCOUNT_SETTINGS = 4 /* a gpointer */
113 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self);
116 modest_connection_specific_smtp_window_finalize (GObject *object)
118 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
120 /* Free all the data items from the treemodel: */
122 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
124 ModestServerAccountSettings *server_settings = NULL;
126 gtk_tree_model_get (priv->model, &iter,
127 MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
131 g_object_unref (server_settings);
134 valid = gtk_tree_model_iter_next (priv->model, &iter);
137 g_object_unref (G_OBJECT (priv->model));
139 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
143 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
145 GObjectClass *object_class = G_OBJECT_CLASS (klass);
147 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
149 object_class->get_property = modest_connection_specific_smtp_window_get_property;
150 object_class->set_property = modest_connection_specific_smtp_window_set_property;
151 object_class->dispose = modest_connection_specific_smtp_window_dispose;
152 object_class->finalize = modest_connection_specific_smtp_window_finalize;
155 /* libconic does not return a list of connections in scratchbox,
156 * so enable this to put a fake row in the list,
157 * so we can test other parts of the code. */
158 /* #define DEBUG_WITHOUT_LIBCONIC 1 */
161 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self,
162 ModestAccountMgr *account_manager)
164 #ifdef MODEST_HAVE_CONIC
165 ModestConnectionSpecificSmtpWindowPrivate *priv =
166 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
167 priv->account_manager = account_manager;
169 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
171 TnyDevice *device = modest_runtime_get_device ();
172 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
174 /* Get the list of Internet Access Points: */
175 #ifdef DEBUG_WITHOUT_LIBCONIC
176 GSList *list_iaps = g_slist_append(NULL, (gpointer)1);
178 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
179 GSList *list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
182 /* printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps)); */
184 GSList* iter = list_iaps;
186 ConIcIap *iap = (ConIcIap*)iter->data;
188 #ifdef DEBUG_WITHOUT_LIBCONIC
189 const gchar *connection_name = "debug name";
190 const gchar *connection_id = "debug id";
192 const gchar *connection_name = con_ic_iap_get_name (iap);
193 const gchar *connection_id = con_ic_iap_get_id (iap);
196 printf ("debug: iac name=%s, id=%s\n", connection_name, connection_id);
198 /* Get any already-associated connection-specific server account: */
199 gchar *server_account_name = NULL;
200 server_account_name = modest_account_mgr_get_connection_specific_smtp (
201 priv->account_manager, connection_id);
203 /* Add the row to the model: */
205 gtk_list_store_append (liststore, &iter);
206 gtk_list_store_set(liststore, &iter,
207 MODEL_COL_ID, connection_id,
208 MODEL_COL_NAME, connection_name,
209 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
212 if (server_account_name)
213 g_free (server_account_name);
216 iter = g_slist_next (iter);
219 #ifndef DEBUG_WITHOUT_LIBCONIC
221 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
224 update_model_server_names (self);
225 #endif /*MODEST_HAVE_CONIC */
229 on_button_edit (ModestConnectionSpecificSmtpWindow *self)
231 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
232 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
235 gchar *connection_name = NULL;
236 gchar *server_account_name = NULL;
237 ModestServerAccountSettings *server_settings = NULL;
238 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
240 GtkTreeModel *model = 0;
241 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
242 gtk_tree_model_get (priv->model, &iter,
244 MODEL_COL_NAME, &connection_name,
245 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
246 MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
249 /* printf("DEBUG: %s: BEFORE: connection-specific server_account_name=%s\n", __FUNCTION__, server_account_name); */
250 /* TODO: Is 0 an allowed libconic IAP ID?
251 * If not then we should check for it. */
253 /* Get existing server account data if a server account is already specified: */
254 gboolean settings_were_retrieved = FALSE;
255 if (server_account_name && !server_settings) {
256 server_settings = modest_account_mgr_load_server_settings(mgr, server_account_name, TRUE);
258 settings_were_retrieved = TRUE;
261 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
262 modest_connection_specific_smtp_edit_window_set_connection (
263 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, server_settings);
265 /* Delete data, unless it was data from the rowmodel: */
266 if (settings_were_retrieved) {
267 g_object_unref (server_settings);
268 server_settings = NULL;
271 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
275 gint response = gtk_dialog_run (GTK_DIALOG (window));
276 if (response == GTK_RESPONSE_OK) {
278 /* Delete any previous data for this row: */
279 if (server_settings) {
280 g_object_unref (server_settings);
281 server_settings = NULL;
284 /* Get the new account data and save it in the row for later:
285 * We free this in finalize(),
286 * and save it to our configuration in
287 * modest_connection_specific_smtp_window_save_server_accounts(). */
288 server_settings = modest_connection_specific_smtp_edit_window_get_settings (
289 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window));
291 if (server_settings) {
292 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
293 MODEL_COL_SERVER_ACCOUNT_SETTINGS, server_settings,
294 MODEL_COL_SERVER_NAME, modest_server_account_settings_get_hostname (server_settings),
297 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
298 MODEL_COL_SERVER_ACCOUNT_SETTINGS, NULL,
299 MODEL_COL_SERVER_NAME, NULL,
300 MODEL_COL_SERVER_ACCOUNT_NAME, NULL,
304 gtk_widget_destroy (window);
306 g_free (connection_name);
308 g_free (server_account_name);
309 update_model_server_names (self);
313 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
315 ModestConnectionSpecificSmtpWindowPrivate *priv =
316 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
318 GtkTreeModel *model = NULL;
320 const gboolean has_selection =
321 gtk_tree_selection_get_selected (sel, &model, &iter);
323 gtk_widget_set_sensitive (priv->button_edit, has_selection);
327 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
329 ModestWindowMgr *mgr;
331 /* Specify a default size, because the GtkTreeView's default requested size
332 * is not big enough: */
333 gtk_window_set_default_size (GTK_WINDOW (self), 500, 300);
335 /* This seems to be necessary to make the window show at the front with decoration.
336 * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration,
337 * then the window will be below the others. */
338 gtk_window_set_type_hint (GTK_WINDOW (self),
339 GDK_WINDOW_TYPE_HINT_DIALOG);
341 ModestConnectionSpecificSmtpWindowPrivate *priv =
342 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
344 /* Create a tree model for the tree view:
345 * with a string for the name, a string for the server name, and an int for the ID.
346 * This must match our MODEL_COLS enum constants.
348 priv->model = GTK_TREE_MODEL (gtk_list_store_new (5,
349 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
351 /* Setup the tree view: */
352 priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
354 /* Show the column headers,
355 * which does not seem to be the default on Maemo.
357 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
360 * The ID model column in not shown in the view. */
361 GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
362 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
363 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
364 gtk_tree_view_column_set_attributes (view_column, renderer,
365 "text", MODEL_COL_NAME, NULL);
366 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
367 gtk_tree_view_append_column (priv->treeview, view_column);
370 /* server name column: */
371 view_column = gtk_tree_view_column_new ();
372 renderer = gtk_cell_renderer_text_new ();
373 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
374 gtk_tree_view_column_set_attributes (view_column, renderer,
375 "text", MODEL_COL_SERVER_NAME, NULL);
376 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
377 gtk_tree_view_append_column (priv->treeview, view_column);
379 /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
381 GtkWidget *vbox = GTK_DIALOG(self)->vbox;
382 //gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
384 /* Introductory note: */
385 /* TODO: For some reason this label does not wrap. It is truncated. */
386 GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
387 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
388 /* So that it is shown without being truncated: */
389 gtk_label_set_max_width_chars (GTK_LABEL (label), 20);
390 /* The documentation for gtk_label_set_line_wrap() says that we must
391 * call gtk_widget_set_size_request() with a hard-coded width,
392 * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
393 gtk_widget_set_size_request (label, 400, -1);
394 gtk_widget_show (label);
395 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
397 /* Put the treeview in a scrolled window and add it to the box: */
398 GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
399 gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
400 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER,
401 GTK_POLICY_AUTOMATIC);
402 gtk_widget_show (scrolled_window);
403 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
404 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
405 gtk_widget_show (GTK_WIDGET (priv->treeview));
406 gtk_widget_show (vbox);
408 /* Hack: we use the response apply to identify the click on the edit button */
409 priv->button_edit = gtk_dialog_add_button (GTK_DIALOG(self), _("mcen_bd_edit"), GTK_RESPONSE_APPLY);
410 gtk_dialog_add_button (GTK_DIALOG(self), _("mcen_bd_close"), GTK_RESPONSE_CLOSE);
412 /* Disable the Edit button when nothing is selected: */
413 GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
414 g_signal_connect (sel, "changed",
415 G_CALLBACK(on_selection_changed), self);
416 on_selection_changed (sel, self);
418 /* When this window is shown, hibernation should not be possible,
419 * because there is no sensible way to save the state: */
420 mgr = modest_runtime_get_window_mgr ();
421 modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr,
424 /* Set window title */
425 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
427 g_signal_connect (self, "response", G_CALLBACK (on_response), NULL);
429 hildon_help_dialog_help_enable (GTK_DIALOG(self),
430 "email_connectionsspecificsmtpconf",
431 modest_maemo_utils_get_osso_context());
434 ModestConnectionSpecificSmtpWindow*
435 modest_connection_specific_smtp_window_new (void)
437 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
440 /** The application should call this when the user changes should be saved.
443 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self)
445 ModestAccountMgr *mgr = modest_runtime_get_account_mgr ();
446 ModestConnectionSpecificSmtpWindowPrivate *priv =
447 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
450 /* Get the first iter in the list */
452 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
454 /* Walk through the list, reading each row */
457 gchar *connection_name = NULL;
458 gchar *server_account_name = NULL;
459 gchar *server_name = NULL;
460 ModestServerAccountSettings *server_settings = NULL;
462 gtk_tree_model_get (priv->model, &iter,
464 MODEL_COL_NAME, &connection_name,
465 MODEL_COL_SERVER_NAME, &server_name,
466 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
467 MODEL_COL_SERVER_ACCOUNT_SETTINGS, &server_settings,
470 gboolean success = TRUE;
471 if (id && server_settings) { /* The presence of data suggests that there is something to save. */
472 if (!server_account_name) {
473 /* Add a new server account, building a (non-human-visible) name: */
474 gchar *name_start = g_strdup_printf("specific_%s", connection_name);
475 server_account_name = modest_account_mgr_get_unused_account_name (
476 priv->account_manager, name_start, TRUE /* server account. */);
477 g_assert (server_account_name);
480 modest_server_account_settings_set_account_name (server_settings, server_account_name);
481 success = modest_account_mgr_save_server_settings (mgr, server_settings);
483 TnyAccount *account = TNY_ACCOUNT (modest_tny_account_store_new_connection_specific_transport_account
484 (modest_runtime_get_account_store (),
485 server_account_name));
487 g_object_unref (account);
490 /* associate the specific server account with this connection for this account: */
491 success = success && modest_account_mgr_set_connection_specific_smtp (
492 priv->account_manager, id, server_account_name);
494 /* Save the new name in the treemodel, so it can be edited again later: */
495 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
496 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
499 modest_account_mgr_save_server_settings (mgr, server_settings);
501 } else if (id && server_name &&
502 !strcmp (server_name, _("mcen_ia_optionalsmtp_notdefined"))) {
503 modest_account_mgr_remove_connection_specific_smtp (priv->account_manager,
505 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
506 MODEL_COL_SERVER_ACCOUNT_NAME, NULL, -1);
509 g_free (connection_name);
511 g_free (server_account_name);
512 g_free (server_name);
518 valid = gtk_tree_model_iter_next (priv->model, &iter);
521 update_model_server_names (self);
526 void 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,
571 case GTK_RESPONSE_APPLY:
572 /* We use it for the edit button */
573 on_button_edit (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog));
574 g_signal_stop_emission_by_name (dialog, "response");
576 case GTK_RESPONSE_CLOSE:
577 case GTK_RESPONSE_NONE:
578 case GTK_RESPONSE_DELETE_EVENT:
579 /* Generated as a response to delete-event, i.e,
580 pressin Esc, or by pressing the Close button */
581 modest_connection_specific_smtp_window_save_server_accounts (MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (dialog));
582 gtk_widget_destroy (GTK_WIDGET (dialog));