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"
50 #include <glib/gi18n.h>
52 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window, GTK_TYPE_WINDOW);
54 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
55 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
57 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
59 struct _ModestConnectionSpecificSmtpWindowPrivate
61 GtkTreeView *treeview;
63 GtkWidget *button_edit;
65 ModestAccountMgr *account_manager;
68 static gboolean on_key_pressed (GtkWidget *self, GdkEventKey *event, gpointer user_data);
71 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
72 GValue *value, GParamSpec *pspec)
74 switch (property_id) {
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
81 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
82 const GValue *value, GParamSpec *pspec)
84 switch (property_id) {
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91 modest_connection_specific_smtp_window_dispose (GObject *object)
93 if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
94 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
98 MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
99 MODEL_COL_ID = 1, /* libconic IAP ID: a string */
100 MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
101 MODEL_COL_SERVER_NAME = 3, /* a string */
102 MODEL_COL_SERVER_ACCOUNT_DATA = 4 /* a gpointer */
106 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self);
109 modest_connection_specific_smtp_window_finalize (GObject *object)
111 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
113 /* Free all the data items from the treemodel: */
115 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
117 ModestServerAccountData *data = NULL;
119 gtk_tree_model_get (priv->model, &iter,
120 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
124 modest_account_mgr_free_server_account_data (priv->account_manager, data);
127 valid = gtk_tree_model_iter_next (priv->model, &iter);
130 g_object_unref (G_OBJECT (priv->model));
132 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
136 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
138 GObjectClass *object_class = G_OBJECT_CLASS (klass);
140 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
142 object_class->get_property = modest_connection_specific_smtp_window_get_property;
143 object_class->set_property = modest_connection_specific_smtp_window_set_property;
144 object_class->dispose = modest_connection_specific_smtp_window_dispose;
145 object_class->finalize = modest_connection_specific_smtp_window_finalize;
148 /* libconic does not return a list of connections in scratchbox,
149 * so enable this to put a fake row in the list,
150 * so we can test other parts of the code. */
151 /* #define DEBUG_WITHOUT_LIBCONIC 1 */
154 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager)
156 ModestConnectionSpecificSmtpWindowPrivate *priv =
157 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
158 priv->account_manager = account_manager;
160 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
162 TnyDevice *device = modest_runtime_get_device ();
163 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
165 /* Get the list of Internet Access Points: */
166 #ifdef DEBUG_WITHOUT_LIBCONIC
167 GSList *list_iaps = g_slist_append(NULL, (gpointer)1);
169 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
170 GSList *list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
173 /* printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps)); */
175 GSList* iter = list_iaps;
177 ConIcIap *iap = (ConIcIap*)iter->data;
179 #ifdef DEBUG_WITHOUT_LIBCONIC
180 const gchar *connection_name = "debug name";
181 const gchar *connection_id = "debug id";
183 const gchar *connection_name = con_ic_iap_get_name (iap);
184 const gchar *connection_id = con_ic_iap_get_id (iap);
187 printf ("debug: iac name=%s, id=%s\n", connection_name, connection_id);
189 /* Get any already-associated connection-specific server account: */
190 gchar *server_account_name = NULL;
191 server_account_name = modest_account_mgr_get_connection_specific_smtp (
192 priv->account_manager, connection_name);
194 /* Add the row to the model: */
196 gtk_list_store_append (liststore, &iter);
197 gtk_list_store_set(liststore, &iter,
198 MODEL_COL_ID, connection_id,
199 MODEL_COL_NAME, connection_name,
200 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
203 g_free (server_account_name);
206 iter = g_slist_next (iter);
209 #ifndef DEBUG_WITHOUT_LIBCONIC
211 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
214 update_model_server_names (self);
218 on_button_edit (GtkButton *button, gpointer user_data)
220 ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
221 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
224 gchar *connection_name = NULL;
225 gchar *server_account_name = NULL;
226 ModestServerAccountData *data = NULL;
227 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
229 GtkTreeModel *model = 0;
230 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
231 gtk_tree_model_get (priv->model, &iter,
233 MODEL_COL_NAME, &connection_name,
234 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
235 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
238 /* printf("DEBUG: %s: BEFORE: connection-specific server_account_name=%s\n", __FUNCTION__, server_account_name); */
239 /* TODO: Is 0 an allowed libconic IAP ID?
240 * If not then we should check for it. */
242 /* Get existing server account data if a server account is already specified: */
243 gboolean data_was_retrieved = FALSE;
244 if (server_account_name && !data) {
245 data = modest_account_mgr_get_server_account_data (priv->account_manager,
246 server_account_name);
248 data_was_retrieved = TRUE;
251 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
252 modest_connection_specific_smtp_edit_window_set_connection (
253 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
255 /* Delete data, unless it was data from the rowmodel: */
256 if (data_was_retrieved) {
257 modest_account_mgr_free_server_account_data (priv->account_manager, data);
261 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
263 gboolean dialog_finished = FALSE;
264 while (!dialog_finished)
266 gint response = gtk_dialog_run (GTK_DIALOG (window));
267 if (response == GTK_RESPONSE_OK) {
268 gtk_widget_hide (window);
269 dialog_finished = TRUE;
270 /* Delete any previous data for this row: */
273 modest_account_mgr_free_server_account_data (priv->account_manager, data);
277 /* Get the new account data and save it in the row for later:
278 * We free this in finalize(),
279 * and save it to our configuration in
280 * modest_connection_specific_smtp_window_save_server_accounts(). */
281 data = modest_connection_specific_smtp_edit_window_get_settings (
282 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window),
283 priv->account_manager);
285 const gchar* server_name = data ? data->hostname : NULL;
286 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
287 MODEL_COL_SERVER_ACCOUNT_DATA, data,
288 MODEL_COL_SERVER_NAME, server_name,
293 if (!modest_connection_specific_smtp_edit_window_is_dirty(
294 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW(window)))
296 gtk_widget_hide(window);
297 dialog_finished = TRUE;
303 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
304 _("imum_nc_wizard_confirm_lose_changes"));
305 if (response == GTK_RESPONSE_OK)
307 gtk_widget_hide(window);
308 dialog_finished = TRUE;
314 g_free (connection_name);
316 g_free (server_account_name);
320 on_button_cancel (GtkButton *button, gpointer user_data)
322 ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
325 * The code that showed it will respond to the hide signal. */
326 gtk_widget_hide (GTK_WIDGET (self));
330 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
332 ModestConnectionSpecificSmtpWindowPrivate *priv =
333 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
335 GtkTreeModel *model = NULL;
337 const gboolean has_selection =
338 gtk_tree_selection_get_selected (sel, &model, &iter);
340 gtk_widget_set_sensitive (priv->button_edit, has_selection);
344 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
346 ModestWindowMgr *mgr;
348 /* Specify a default size, because the GtkTreeView's default requested size
349 * is not big enough: */
350 gtk_window_set_default_size (GTK_WINDOW (self), 500, 300);
352 /* This seems to be necessary to make the window show at the front with decoration.
353 * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration,
354 * then the window will be below the others. */
355 gtk_window_set_type_hint (GTK_WINDOW (self),
356 GDK_WINDOW_TYPE_HINT_DIALOG);
358 ModestConnectionSpecificSmtpWindowPrivate *priv =
359 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
361 /* Create a tree model for the tree view:
362 * with a string for the name, a string for the server name, and an int for the ID.
363 * This must match our MODEL_COLS enum constants.
365 priv->model = GTK_TREE_MODEL (gtk_list_store_new (5,
366 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
368 /* Setup the tree view: */
369 priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
371 /* Show the column headers,
372 * which does not seem to be the default on Maemo.
374 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
377 * The ID model column in not shown in the view. */
378 GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
379 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
380 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
381 gtk_tree_view_column_set_attributes (view_column, renderer,
382 "text", MODEL_COL_NAME, NULL);
383 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
384 gtk_tree_view_append_column (priv->treeview, view_column);
387 /* server name column: */
388 view_column = gtk_tree_view_column_new ();
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_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
394 gtk_tree_view_append_column (priv->treeview, view_column);
396 /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
398 GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
400 /* Introductory note: */
401 /* TODO: For some reason this label does not wrap. It is truncated. */
402 GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
403 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
404 /* So that it is shown without being truncated: */
405 gtk_label_set_max_width_chars (GTK_LABEL (label), 20);
406 /* The documentation for gtk_label_set_line_wrap() says that we must
407 * call gtk_widget_set_size_request() with a hard-coded width,
408 * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
409 gtk_widget_set_size_request (label, 400, -1);
410 gtk_widget_show (label);
411 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
413 /* Put the treeview in a scrolled window and add it to the box: */
414 GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
415 gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
416 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER,
417 GTK_POLICY_AUTOMATIC);
418 gtk_widget_show (scrolled_window);
419 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
420 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
421 gtk_widget_show (GTK_WIDGET (priv->treeview));
423 /* Add the buttons: */
424 GtkWidget *hbox = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
425 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, MODEST_MARGIN_HALF);
426 gtk_widget_show (hbox);
428 priv->button_edit = gtk_button_new_from_stock (_("mcen_bd_edit"));
429 gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, MODEST_MARGIN_HALF);
430 gtk_widget_show (priv->button_edit);
431 g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
432 G_CALLBACK (on_button_edit), self);
434 GtkWidget *button_cancel = gtk_button_new_from_stock (_("mcen_bd_close"));
435 gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, MODEST_MARGIN_HALF);
436 gtk_widget_show (button_cancel);
437 g_signal_connect (G_OBJECT (button_cancel), "clicked",
438 G_CALLBACK (on_button_cancel), self);
440 gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
441 gtk_widget_show (vbox);
443 /* Disable the Edit button when nothing is selected: */
444 GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
445 g_signal_connect (sel, "changed",
446 G_CALLBACK(on_selection_changed), self);
447 on_selection_changed (sel, self);
449 /* When this window is shown, hibernation should not be possible,
450 * because there is no sensible way to save the state: */
451 mgr = modest_runtime_get_window_mgr ();
452 modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr,
455 /* Set window title */
456 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
458 /* Track key presses to close the window if the Escape is pressed */
459 g_signal_connect (G_OBJECT (self),
461 G_CALLBACK (on_key_pressed), NULL);
464 ModestConnectionSpecificSmtpWindow*
465 modest_connection_specific_smtp_window_new (void)
467 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
470 /** The application should call this when the user changes should be saved.
473 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self)
475 ModestConnectionSpecificSmtpWindowPrivate *priv =
476 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
479 /* Get the first iter in the list */
481 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
483 /* Walk through the list, reading each row */
486 gchar *connection_name = NULL;
487 gchar *server_account_name = NULL;
488 ModestServerAccountData *data = NULL;
490 gtk_tree_model_get (priv->model, &iter,
492 MODEL_COL_NAME, &connection_name,
493 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
494 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
497 gboolean success = TRUE;
498 if (id && data) { /* The presence of data suggests that there is something to save. */
499 if (!server_account_name) {
500 /* Add a new server account, building a (non-human-visible) name: */
501 gchar *name_start = g_strdup_printf("specific_%s", connection_name);
502 server_account_name = modest_account_mgr_get_unused_account_name (
503 priv->account_manager, name_start, TRUE /* server account. */);
504 g_assert (server_account_name);
507 success = modest_account_mgr_add_server_account (priv->account_manager,
510 data->username, data->password,
511 MODEST_PROTOCOL_TRANSPORT_SMTP,
515 /* associate the specific server account with this connection for this account: */
516 success = success && modest_account_mgr_set_connection_specific_smtp (
517 priv->account_manager, connection_name, server_account_name);
519 /* Save the new name in the treemodel, so it can be edited again later: */
520 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
521 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
524 /* Change an existing server account: */
525 modest_account_mgr_set_server_account_hostname (priv->account_manager, server_account_name,
528 modest_account_mgr_set_server_account_username (priv->account_manager, server_account_name,
531 modest_account_mgr_set_server_account_password (priv->account_manager, server_account_name,
534 modest_account_mgr_set_server_account_secure_auth (priv->account_manager, server_account_name,
537 modest_account_mgr_set_server_account_security (priv->account_manager, server_account_name,
540 modest_account_mgr_set_server_account_port (priv->account_manager, server_account_name, data->port);
544 g_free (connection_name);
546 g_free (server_account_name);
552 valid = gtk_tree_model_iter_next (priv->model, &iter);
555 update_model_server_names (self);
560 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
562 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
565 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
568 gchar *server_account_name = NULL;
569 gtk_tree_model_get (priv->model, &iter,
570 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
573 if (server_account_name) {
574 /* Get the server hostname and show it in the treemodel: */
575 gchar *hostname = modest_account_mgr_get_server_account_hostname (priv->account_manager,
576 server_account_name);
577 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
578 MODEL_COL_SERVER_NAME, hostname,
582 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
583 MODEL_COL_SERVER_NAME, _("mcen_ia_optionalsmtp_notdefined"),
588 valid = gtk_tree_model_iter_next (priv->model, &iter);
593 on_key_pressed (GtkWidget *self,
597 if (event->keyval == GDK_Escape) {
598 /* Simulate a press on Cancel to close the dialog */
599 on_button_cancel (NULL, self);