1 /* connection-specific-smtp-window.c */
3 #include "modest-connection-specific-smtp-window.h"
4 #include "modest-connection-specific-smtp-edit-window.h"
5 #include <modest-account-mgr-helpers.h>
7 #include <modest-runtime.h>
8 #include <tny-maemo-conic-device.h>
10 #include <gtk/gtktreeview.h>
11 #include <gtk/gtkcellrenderertext.h>
12 #include <gtk/gtkliststore.h>
13 #include <gtk/gtkscrolledwindow.h>
14 #include <gtk/gtkbutton.h>
15 #include <gtk/gtkhbox.h>
16 #include <gtk/gtkvbox.h>
17 #include <gtk/gtkstock.h>
19 #include <glib/gi18n.h>
21 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window, GTK_TYPE_WINDOW);
23 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
24 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
26 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
28 struct _ModestConnectionSpecificSmtpWindowPrivate
30 GtkTreeView *treeview;
32 GtkWidget *button_edit;
34 ModestAccountMgr *account_manager;
39 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
40 GValue *value, GParamSpec *pspec)
42 switch (property_id) {
44 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
49 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
50 const GValue *value, GParamSpec *pspec)
52 switch (property_id) {
54 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
59 modest_connection_specific_smtp_window_dispose (GObject *object)
61 if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
62 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
66 MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
67 MODEL_COL_ID = 1, /* libconic IAP ID: a string */
68 MODEL_COL_SERVER_ACCOUNT_NAME = 2, /* a string */
69 MODEL_COL_SERVER_NAME = 3, /* a string */
70 MODEL_COL_SERVER_ACCOUNT_DATA = 4 /* a gpointer */
74 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self);
77 modest_connection_specific_smtp_window_finalize (GObject *object)
79 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
81 /* Free all the data items from the treemodel: */
83 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
85 ModestServerAccountData *data = NULL;
87 gtk_tree_model_get (priv->model, &iter,
88 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
92 modest_account_mgr_free_server_account_data (priv->account_manager, data);
95 valid = gtk_tree_model_iter_next (priv->model, &iter);
98 g_object_unref (G_OBJECT (priv->model));
99 g_free (priv->account_name);
101 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
105 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
107 GObjectClass *object_class = G_OBJECT_CLASS (klass);
109 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
111 object_class->get_property = modest_connection_specific_smtp_window_get_property;
112 object_class->set_property = modest_connection_specific_smtp_window_set_property;
113 object_class->dispose = modest_connection_specific_smtp_window_dispose;
114 object_class->finalize = modest_connection_specific_smtp_window_finalize;
118 modest_connection_specific_smtp_window_fill_with_connections (ModestConnectionSpecificSmtpWindow *self, ModestAccountMgr *account_manager,
119 const gchar* account_name)
121 ModestConnectionSpecificSmtpWindowPrivate *priv =
122 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
123 priv->account_manager = account_manager;
124 priv->account_name = account_name ? g_strdup (account_name) : NULL;
126 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
128 TnyDevice *device = modest_runtime_get_device ();
129 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
131 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
133 /* Get the list of Internet Access Points: */
134 GSList* list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
135 printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps));
137 GSList* iter = list_iaps;
139 ConIcIap *iap = (ConIcIap*)iter->data;
141 const gchar *name = con_ic_iap_get_name (iap);
142 const gchar *id = con_ic_iap_get_id (iap);
143 printf ("debug: iac name=%s, id=%s\n", name, id);
145 /* Get any already-associated connection-specific server account: */
146 gchar *server_account_name = NULL;
147 if (priv->account_name)
148 server_account_name = modest_account_mgr_get_connection_specific_smtp (
149 priv->account_manager, priv->account_name, name);
151 /* Add the row to the model: */
153 gtk_list_store_append (liststore, &iter);
154 gtk_list_store_set(liststore, &iter,
156 MODEL_COL_NAME, name,
157 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name,
160 g_free (server_account_name);
163 iter = g_slist_next (iter);
167 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
169 update_model_server_names (self);
173 on_button_edit (GtkButton *button, gpointer user_data)
175 ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
176 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
179 gchar *connection_name = NULL;
180 gchar *server_account_name = NULL;
181 ModestServerAccountData *data = NULL;
182 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->treeview));
184 GtkTreeModel *model = 0;
185 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
186 gtk_tree_model_get (priv->model, &iter,
188 MODEL_COL_NAME, &connection_name,
189 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
190 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
193 /* TODO: Is 0 an allowed libconic IAP ID?
194 * If not then we should check for it. */
196 /* Get existing server account data if a server account is already specified: */
197 ModestServerAccountData *data_retrieved = NULL;
198 if (server_account_name && !data) {
199 data_retrieved = modest_account_mgr_get_server_account_data (priv->account_manager,
200 server_account_name);
201 data = data_retrieved;
204 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
205 modest_connection_specific_smtp_edit_window_set_connection (
206 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, connection_name, data);
209 modest_account_mgr_free_server_account_data (priv->account_manager, data_retrieved);
211 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
212 gint response = gtk_dialog_run (GTK_DIALOG (window));
213 gtk_widget_hide (window);
215 if (response == GTK_RESPONSE_OK) {
216 /* Delete any previous data for this row: */
219 modest_account_mgr_free_server_account_data (priv->account_manager, data);
223 /* Get the new account data and save it in the row for later:
224 * We free this in finalize(). */
225 data = modest_connection_specific_smtp_edit_window_get_settings (
226 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window),
227 priv->account_manager, server_account_name);
228 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
229 MODEL_COL_SERVER_ACCOUNT_DATA, data,
234 g_free (connection_name);
236 g_free (server_account_name);
240 on_button_cancel (GtkButton *button, gpointer user_data)
242 ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
245 * The code that showed it will respond to the hide signal. */
246 gtk_widget_hide (GTK_WIDGET (self));
250 on_selection_changed (GtkTreeSelection *sel, ModestConnectionSpecificSmtpWindow *self)
252 ModestConnectionSpecificSmtpWindowPrivate *priv =
253 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
255 GtkTreeModel *model = NULL;
257 const gboolean has_selection =
258 gtk_tree_selection_get_selected (sel, &model, &iter);
260 gtk_widget_set_sensitive (priv->button_edit, has_selection);
264 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
266 /* This seems to be necessary to make the window show at the front with decoration.
267 * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration,
268 * then the window will be below the others. */
269 gtk_window_set_type_hint (GTK_WINDOW (self),
270 GDK_WINDOW_TYPE_HINT_DIALOG);
272 ModestConnectionSpecificSmtpWindowPrivate *priv =
273 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
275 /* Create a tree model for the tree view:
276 * with a string for the name, a string for the server name, and an int for the ID.
277 * This must match our MODEL_COLS enum constants.
279 priv->model = GTK_TREE_MODEL (gtk_list_store_new (4,
280 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER));
282 /* Setup the tree view: */
283 priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
286 * The ID model column in not shown in the view. */
287 GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
288 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
289 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
290 gtk_tree_view_column_set_attributes (view_column, renderer,
291 "text", MODEL_COL_NAME, NULL);
292 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
293 gtk_tree_view_append_column (priv->treeview, view_column);
296 /* server name column: */
297 view_column = gtk_tree_view_column_new ();
298 renderer = gtk_cell_renderer_text_new ();
299 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
300 gtk_tree_view_column_set_attributes (view_column, renderer,
301 "text", MODEL_COL_SERVER_NAME, NULL);
302 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
303 gtk_tree_view_append_column (priv->treeview, view_column);
305 /* The application must call modest_connection_specific_smtp_window_fill_with_connections(). */
307 GtkWidget *vbox = gtk_vbox_new (FALSE, 2);
309 /* Put the treeview in a scrolled window and add it to the box: */
310 GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
311 gtk_widget_show (scrolled_window);
312 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
313 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, 2);
314 gtk_widget_show (GTK_WIDGET (priv->treeview));
316 /* Add the buttons: */
317 GtkWidget *hbox = gtk_hbox_new (FALSE, 2);
318 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
319 gtk_widget_show (hbox);
321 priv->button_edit = gtk_button_new_from_stock (GTK_STOCK_EDIT);
322 gtk_box_pack_start (GTK_BOX (hbox), priv->button_edit, TRUE, FALSE, 2);
323 gtk_widget_show (priv->button_edit);
324 g_signal_connect (G_OBJECT (priv->button_edit), "clicked",
325 G_CALLBACK (on_button_edit), self);
327 GtkWidget *button_cancel = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
328 gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, 2);
329 gtk_widget_show (button_cancel);
330 g_signal_connect (G_OBJECT (button_cancel), "clicked",
331 G_CALLBACK (on_button_cancel), self);
333 gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
334 gtk_widget_show (vbox);
336 /* Disable the Edit button when nothing is selected: */
337 GtkTreeSelection *sel = gtk_tree_view_get_selection (priv->treeview);
338 g_signal_connect (sel, "changed",
339 G_CALLBACK(on_selection_changed), self);
340 on_selection_changed (sel, self);
343 ModestConnectionSpecificSmtpWindow*
344 modest_connection_specific_smtp_window_new (void)
346 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
349 /** The application should call this when the user changes should be saved.
350 * @account_name: Specify this again in case it was not previously known.
353 modest_connection_specific_smtp_window_save_server_accounts (ModestConnectionSpecificSmtpWindow *self,
354 const gchar* account_name)
356 ModestConnectionSpecificSmtpWindowPrivate *priv =
357 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
360 /* Get the first iter in the list */
362 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
364 /* Walk through the list, reading each row */
367 gchar *connection_name = NULL;
368 gchar *server_account_name = NULL;
369 ModestServerAccountData *data = NULL;
371 gtk_tree_model_get (priv->model, &iter,
373 MODEL_COL_NAME, &connection_name,
374 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
375 MODEL_COL_SERVER_ACCOUNT_DATA, &data,
378 gboolean success = TRUE;
379 if (id && data) { /* The presence of data suggests that there is something to save. */
380 if (!server_account_name) {
381 /* Add a new server account, building a (non-human-visible) name: */
382 gchar *name_start = g_strdup_printf("%s_specific_%s",
383 priv->account_name, connection_name);
384 server_account_name = modest_account_mgr_get_unused_account_name (
385 priv->account_manager, name_start, TRUE /* server account. */);
388 success = modest_account_mgr_add_server_account (priv->account_manager,
391 data->username, data->password,
392 MODEST_PROTOCOL_TRANSPORT_SMTP,
396 /* associate the specific server account with this connection for this account: */
397 success = success && modest_account_mgr_set_connection_specific_smtp (
398 priv->account_manager, priv->account_name,
399 connection_name, server_account_name);
401 /* Save the new name in the treemodel, so it can be edited again later: */
402 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
403 MODEL_COL_SERVER_ACCOUNT_NAME, server_account_name, -1);
406 /* Change an existing server account: */
407 success = modest_account_mgr_set_string (priv->account_manager, server_account_name,
408 MODEST_ACCOUNT_HOSTNAME, data->hostname, TRUE /* server account */);
410 success = success && modest_account_mgr_set_string (priv->account_manager, server_account_name,
411 MODEST_ACCOUNT_USERNAME, data->username, TRUE /* server account */);
413 success = success && modest_account_mgr_set_string (priv->account_manager, server_account_name,
414 MODEST_ACCOUNT_PASSWORD, data->password, TRUE /* server account */);
416 modest_server_account_set_secure_auth (priv->account_manager, server_account_name,
419 modest_server_account_set_security (priv->account_manager, server_account_name,
422 modest_account_mgr_set_int (priv->account_manager, server_account_name,
423 MODEST_ACCOUNT_PORT, data->port, TRUE /* server account */);
427 g_free (connection_name);
429 g_free (server_account_name);
435 valid = gtk_tree_model_iter_next (priv->model, &iter);
438 update_model_server_names (self);
443 void update_model_server_names (ModestConnectionSpecificSmtpWindow *self)
445 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
448 gboolean valid = gtk_tree_model_get_iter_first (priv->model, &iter);
451 gchar *server_account_name = NULL;
452 gtk_tree_model_get (priv->model, &iter,
453 MODEL_COL_SERVER_ACCOUNT_NAME, &server_account_name,
456 if (server_account_name) {
457 /* Get the server hostname and show it in the treemodel: */
458 gchar *hostname = modest_account_mgr_get_string (priv->account_manager,
459 server_account_name, MODEST_ACCOUNT_HOSTNAME, TRUE /* server account */);
460 gtk_list_store_set (GTK_LIST_STORE (priv->model), &iter,
461 MODEL_COL_SERVER_NAME, hostname,
467 valid = gtk_tree_model_iter_next (priv->model, &iter);