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;
69 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
70 GValue *value, GParamSpec *pspec)
72 switch (property_id) {
74 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
79 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
80 const GValue *value, GParamSpec *pspec)
82 switch (property_id) {
84 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
89 modest_connection_specific_smtp_window_dispose (GObject *object)
91 if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
92 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
96 MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
97 MODEL_COL_ID = 1, /* libconic IAP ID: a string */
98 MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
99 MODEL_COL_SERVER_NAME = 3, /* a string */
100 MODEL_COL_SERVER_ACCOUNT_DATA = 4 /* a gpointer */
104 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self);
107 modest_connection_specific_smtp_window_finalize (GObject *object)
109 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
111 /* Free all the data items from the treemodel: */
113 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
115 ModestServerAccountData *data = NULL;
117 gtk_tree_model_get (priv->model, &iter,
118 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
122 modest_account_mgr_free_server_account_data (priv->account_manager, data);
125 valid = gtk_tree_model_iter_next (priv->model, &iter);
128 g_object_unref (G_OBJECT (priv->model));
130 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
134 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
136 GObjectClass *object_class = G_OBJECT_CLASS (klass);
138 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
140 object_class->get_property = modest_connection_specific_smtp_window_get_property;
141 object_class->set_property = modest_connection_specific_smtp_window_set_property;
142 object_class->dispose = modest_connection_specific_smtp_window_dispose;
143 object_class->finalize = modest_connection_specific_smtp_window_finalize;
146 /* libconic does not return a list of connections in scratchbox,
147 * so enable this to put a fake row in the list,
148 * so we can test other parts of the code. */
149 /* #define DEBUG_WITHOUT_LIBCONIC 1 */
152 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager)
154 ModestConnectionSpecificSmtpWindowPrivate *priv =
155 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
156 priv->account_manager = account_manager;
158 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
160 TnyDevice *device = modest_runtime_get_device ();
161 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
163 /* Get the list of Internet Access Points: */
164 #ifdef DEBUG_WITHOUT_LIBCONIC
165 GSList *list_iaps = g_slist_append(NULL, (gpointer)1);
167 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
168 GSList *list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
171 /* printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps)); */
173 GSList* iter = list_iaps;
175 ConIcIap *iap = (ConIcIap*)iter->data;
177 #ifdef DEBUG_WITHOUT_LIBCONIC
178 const gchar *connection_name = "debug name";
179 const gchar *connection_id = "debug id";
181 const gchar *connection_name = con_ic_iap_get_name (iap);
182 const gchar *connection_id = con_ic_iap_get_id (iap);
185 printf ("debug: iac name=%s, id=%s\n", connection_name, connection_id);
187 /* Get any already-associated connection-specific server account: */
188 gchar *server_account_name = NULL;
189 server_account_name = modest_account_mgr_get_connection_specific_smtp (
190 priv->account_manager, connection_name);
192 /* Add the row to the model: */
194 gtk_list_store_append (liststore, &iter);
195 gtk_list_store_set(liststore, &iter,
196 MODEL_COL_ID, connection_id,
197 MODEL_COL_NAME, connection_name,
198 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
201 g_free (server_account_name);
204 iter = g_slist_next (iter);
207 #ifndef DEBUG_WITHOUT_LIBCONIC
209 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
212 update_model_server_names (self);
216 on_button_edit (GtkButton *button, gpointer user_data)
218 ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
219 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
222 gchar *connection_name = NULL;
223 gchar *server_account_name = NULL;
224 ModestServerAccountData *data = NULL;
225 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
227 GtkTreeModel *model = 0;
228 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
229 gtk_tree_model_get (priv->model, &iter,
231 MODEL_COL_NAME, &connection_name,
232 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
233 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
236 /* printf("DEBUG: %s: BEFORE: connection-specific server_account_name=%s\n", __FUNCTION__, server_account_name); */
237 /* TODO: Is 0 an allowed libconic IAP ID?
238 * If not then we should check for it. */
240 /* Get existing server account data if a server account is already specified: */
241 gboolean data_was_retrieved = FALSE;
242 if (server_account_name && !data) {
243 data = modest_account_mgr_get_server_account_data (priv->account_manager,
244 server_account_name);
246 data_was_retrieved = TRUE;
249 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
250 modest_connection_specific_smtp_edit_window_set_connection (
251 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
253 /* Delete data, unless it was data from the rowmodel: */
254 if (data_was_retrieved) {
255 modest_account_mgr_free_server_account_data (priv->account_manager, data);
259 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
261 gboolean dialog_finished = FALSE;
262 while (!dialog_finished)
264 gint response = gtk_dialog_run (GTK_DIALOG (window));
265 if (response == GTK_RESPONSE_OK) {
266 gtk_widget_hide (window);
267 dialog_finished = TRUE;
268 /* Delete any previous data for this row: */
271 modest_account_mgr_free_server_account_data (priv->account_manager, data);
275 /* Get the new account data and save it in the row for later:
276 * We free this in finalize(),
277 * and save it to our configuration in
278 * modest_connection_specific_smtp_window_save_server_accounts(). */
279 data = modest_connection_specific_smtp_edit_window_get_settings (
280 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window),
281 priv->account_manager);
283 const gchar* server_name = data ? data->hostname : NULL;
284 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
285 MODEL_COL_SERVER_ACCOUNT_DATA, data,
286 MODEL_COL_SERVER_NAME, server_name,
291 if (!modest_connection_specific_smtp_edit_window_is_dirty(
292 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW(window)))
294 gtk_widget_hide(window);
295 dialog_finished = TRUE;
301 response = modest_platform_run_confirmation_dialog (GTK_WINDOW (window),
302 _("imum_nc_wizard_confirm_lose_changes"));
303 if (response == GTK_RESPONSE_OK)
305 gtk_widget_hide(window);
306 dialog_finished = TRUE;
312 g_free (connection_name);
314 g_free (server_account_name);
318 on_button_cancel (GtkButton *button, gpointer user_data)
320 ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
323 * The code that showed it will respond to the hide signal. */
324 gtk_widget_hide (GTK_WIDGET (self));
328 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
330 ModestConnectionSpecificSmtpWindowPrivate *priv =
331 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
333 GtkTreeModel *model = NULL;
335 const gboolean has_selection =
336 gtk_tree_selection_get_selected (sel, &model, &iter);
338 gtk_widget_set_sensitive (priv->button_edit, has_selection);
342 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
344 ModestWindowMgr *mgr;
346 /* Specify a default size, because the GtkTreeView's default requested size
347 * is not big enough: */
348 gtk_window_set_default_size (GTK_WINDOW (self), 500, 300);
350 /* This seems to be necessary to make the window show at the front with decoration.
351 * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration,
352 * then the window will be below the others. */
353 gtk_window_set_type_hint (GTK_WINDOW (self),
354 GDK_WINDOW_TYPE_HINT_DIALOG);
356 ModestConnectionSpecificSmtpWindowPrivate *priv =
357 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
359 /* Create a tree model for the tree view:
360 * with a string for the name, a string for the server name, and an int for the ID.
361 * This must match our MODEL_COLS enum constants.
363 priv->model = GTK_TREE_MODEL (gtk_list_store_new (5,
364 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
366 /* Setup the tree view: */
367 priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
369 /* Show the column headers,
370 * which does not seem to be the default on Maemo.
372 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(priv->treeview), TRUE);
375 * The ID model column in not shown in the view. */
376 GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
377 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
378 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
379 gtk_tree_view_column_set_attributes (view_column, renderer,
380 "text", MODEL_COL_NAME, NULL);
381 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
382 gtk_tree_view_append_column (priv->treeview, view_column);
385 /* server name column: */
386 view_column = gtk_tree_view_column_new ();
387 renderer = gtk_cell_renderer_text_new ();
388 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
389 gtk_tree_view_column_set_attributes (view_column, renderer,
390 "text", MODEL_COL_SERVER_NAME, NULL);
391 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
392 gtk_tree_view_append_column (priv->treeview, view_column);
394 /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
396 GtkWidget *vbox = gtk_vbox_new (FALSE, MODEST_MARGIN_DEFAULT);
398 /* Introductory note: */
399 /* TODO: For some reason this label does not wrap. It is truncated. */
400 GtkWidget *label = gtk_label_new(_("mcen_ia_optionalsmtp_note"));
401 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
402 /* So that it is shown without being truncated: */
403 gtk_label_set_max_width_chars (GTK_LABEL (label), 20);
404 /* The documentation for gtk_label_set_line_wrap() says that we must
405 * call gtk_widget_set_size_request() with a hard-coded width,
406 * though I wonder why gtk_label_set_max_width_chars() isn't enough. */
407 gtk_widget_set_size_request (label, 400, -1);
408 gtk_widget_show (label);
409 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, MODEST_MARGIN_HALF);
411 /* Put the treeview in a scrolled window and add it to the box: */
412 GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
413 gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), MODEST_MARGIN_DEFAULT);
414 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_NEVER,
415 GTK_POLICY_AUTOMATIC);
416 gtk_widget_show (scrolled_window);
417 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
418 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, MODEST_MARGIN_HALF);
419 gtk_widget_show (GTK_WIDGET (priv->treeview));
421 /* Add the buttons: */
422 GtkWidget *hbox = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
423 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, MODEST_MARGIN_HALF);
424 gtk_widget_show (hbox);
426 priv->button_edit = gtk_button_new_from_stock (_("mcen_bd_edit"));
427 gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, MODEST_MARGIN_HALF);
428 gtk_widget_show (priv->button_edit);
429 g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
430 G_CALLBACK (on_button_edit), self);
432 GtkWidget *button_cancel = gtk_button_new_from_stock (_("mcen_bd_close"));
433 gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, MODEST_MARGIN_HALF);
434 gtk_widget_show (button_cancel);
435 g_signal_connect (G_OBJECT (button_cancel), "clicked",
436 G_CALLBACK (on_button_cancel), self);
438 gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
439 gtk_widget_show (vbox);
441 /* Disable the Edit button when nothing is selected: */
442 GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
443 g_signal_connect (sel, "changed",
444 G_CALLBACK(on_selection_changed), self);
445 on_selection_changed (sel, self);
447 /* When this window is shown, hibernation should not be possible,
448 * because there is no sensible way to save the state: */
449 mgr = modest_runtime_get_window_mgr ();
450 modest_window_mgr_prevent_hibernation_while_window_is_shown (mgr,
453 /* Set window title */
454 gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_optionalsmtp_servers"));
457 ModestConnectionSpecificSmtpWindow*
458 modest_connection_specific_smtp_window_new (void)
460 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
463 /** The application should call this when the user changes should be saved.
466 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self)
468 ModestConnectionSpecificSmtpWindowPrivate *priv =
469 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
472 /* Get the first iter in the list */
474 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
476 /* Walk through the list, reading each row */
479 gchar *connection_name = NULL;
480 gchar *server_account_name = NULL;
481 ModestServerAccountData *data = NULL;
483 gtk_tree_model_get (priv->model, &iter,
485 MODEL_COL_NAME, &connection_name,
486 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
487 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
490 gboolean success = TRUE;
491 if (id && data) { /* The presence of data suggests that there is something to save. */
492 if (!server_account_name) {
493 /* Add a new server account, building a (non-human-visible) name: */
494 gchar *name_start = g_strdup_printf("specific_%s", connection_name);
495 server_account_name = modest_account_mgr_get_unused_account_name (
496 priv->account_manager, name_start, TRUE /* server account. */);
497 g_assert (server_account_name);
500 success = modest_account_mgr_add_server_account (priv->account_manager,
503 data->username, data->password,
504 MODEST_PROTOCOL_TRANSPORT_SMTP,
508 /* associate the specific server account with this connection for this account: */
509 success = success && modest_account_mgr_set_connection_specific_smtp (
510 priv->account_manager, connection_name, server_account_name);
512 /* Save the new name in the treemodel, so it can be edited again later: */
513 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
514 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
517 /* Change an existing server account: */
518 success = modest_account_mgr_set_string (priv->account_manager, server_account_name,
519 MODEST_ACCOUNT_HOSTNAME, data->hostname, TRUE /* server account */);
521 modest_server_account_set_username (priv->account_manager, server_account_name,
524 modest_server_account_set_password (priv->account_manager, server_account_name,
527 modest_server_account_set_secure_auth (priv->account_manager, server_account_name,
530 modest_server_account_set_security (priv->account_manager, server_account_name,
533 modest_account_mgr_set_int (priv->account_manager, server_account_name,
534 MODEST_ACCOUNT_PORT, data->port, TRUE /* server account */);
538 g_free (connection_name);
540 g_free (server_account_name);
546 valid = gtk_tree_model_iter_next (priv->model, &iter);
549 update_model_server_names (self);
554 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
556 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
559 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
562 gchar *server_account_name = NULL;
563 gtk_tree_model_get (priv->model, &iter,
564 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
567 if (server_account_name) {
568 /* Get the server hostname and show it in the treemodel: */
569 gchar *hostname = modest_account_mgr_get_string (priv->account_manager,
570 server_account_name, MODEST_ACCOUNT_HOSTNAME, TRUE /* server account */);
571 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
572 MODEL_COL_SERVER_NAME, hostname,
576 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
577 MODEL_COL_SERVER_NAME, _("mcen_ia_optionalsmtp_notdefined"),
582 valid = gtk_tree_model_iter_next (priv->model, &iter);