1 /* connection-specific-smtp-window.c */
3 #include "modest-connection-specific-smtp-window.h"
4 #include "modest-connection-specific-smtp-edit-window.h"
6 #include <modest-runtime.h>
7 #include <tny-maemo-conic-device.h>
9 #include <gtk/gtktreeview.h>
10 #include <gtk/gtkcellrenderertext.h>
11 #include <gtk/gtkliststore.h>
12 #include <gtk/gtkscrolledwindow.h>
13 #include <gtk/gtkbutton.h>
14 #include <gtk/gtkhbox.h>
15 #include <gtk/gtkvbox.h>
16 #include <gtk/gtkstock.h>
18 #include <glib/gi18n.h>
20 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window, GTK_TYPE_WINDOW);
22 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
23 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
25 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
27 struct _ModestConnectionSpecificSmtpWindowPrivate
29 GtkTreeView *treeview;
34 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
35 GValue *value, GParamSpec *pspec)
37 switch (property_id) {
39 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
44 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
45 const GValue *value, GParamSpec *pspec)
47 switch (property_id) {
49 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
54 modest_connection_specific_smtp_window_dispose (GObject *object)
56 if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
57 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
61 modest_connection_specific_smtp_window_finalize (GObject *object)
63 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
65 g_object_unref (G_OBJECT (priv->model));
67 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
71 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
73 GObjectClass *object_class = G_OBJECT_CLASS (klass);
75 g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
77 object_class->get_property = modest_connection_specific_smtp_window_get_property;
78 object_class->set_property = modest_connection_specific_smtp_window_set_property;
79 object_class->dispose = modest_connection_specific_smtp_window_dispose;
80 object_class->finalize = modest_connection_specific_smtp_window_finalize;
84 MODEL_COL_NAME = 0, /* libconic IAP Name: a string */
85 MODEL_COL_SERVER_NAME = 1, /* a string */
86 MODEL_COL_ID = 2 /* libconic IAP ID: a string */
90 fill_with_connections (ModestConnectionSpecificSmtpWindow *self)
92 printf("debug: fill_with_connections()\n");
93 ModestConnectionSpecificSmtpWindowPrivate *priv =
94 CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
95 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
97 TnyDevice *device = modest_runtime_get_device ();
98 g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
100 TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
102 /* Get the list of Internet Access Points: */
103 GSList* list_iaps = tny_maemo_conic_device_get_iap_list (maemo_device);
104 printf("debug: list_iaps=%p, list_iaps size = %d\n", list_iaps, g_slist_length(list_iaps));
106 GSList* iter = list_iaps;
108 ConIcIap *iap = (ConIcIap*)iter->data;
110 const gchar *name = con_ic_iap_get_name (iap);
111 const gchar *id = con_ic_iap_get_id (iap);
112 printf ("debug: iac name=%s, id=%s\n", name, id);
114 /* Add the row to the model: */
116 gtk_list_store_append (liststore, &iter);
117 gtk_list_store_set(liststore, &iter, MODEL_COL_ID, id, MODEL_COL_NAME, name, -1);
120 iter = g_slist_next (iter);
124 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
129 on_button_edit (GtkButton *button, gpointer user_data)
131 ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
132 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
136 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
138 GtkTreeModel *model = 0;
139 if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
140 gtk_tree_model_get (priv->model, &iter,
142 MODEL_COL_NAME, &name,
145 /* TODO: Is 0 an allowed libconic IAP ID?
146 * If not then we should check for it. */
148 GtkWidget * window = GTK_WIDGET (modest_connection_specific_smtp_edit_window_new ());
149 modest_connection_specific_smtp_edit_window_set_connection (
150 MODEST_CONNECTION_SPECIFIC_SMTP_EDIT_WINDOW (window), id, name);
152 gtk_window_set_transient_for (GTK_WINDOW (self), GTK_WINDOW (window));
153 gint response = gtk_dialog_run (GTK_DIALOG (window));
154 gtk_widget_hide (window);
155 if (response == GTK_RESPONSE_OK) {
162 on_button_cancel (GtkButton *button, gpointer user_data)
164 ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
167 * The code that showed it will respond to the hide signal. */
168 gtk_widget_hide (GTK_WIDGET (self));
172 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
174 /* This seems to be necessary to make the window show at the front with decoration.
175 * If we use property type=GTK_WINDOW_TOPLEVEL instead of the default GTK_WINDOW_POPUP+decoration,
176 * then the window will be below the others. */
177 gtk_window_set_type_hint (GTK_WINDOW (self),
178 GDK_WINDOW_TYPE_HINT_DIALOG);
180 ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
182 /* Create a tree model for the tree view:
183 * with a string for the name, a string for the server name, and an int for the ID.
184 * This must match our MODEL_COLS enum constants.
186 priv->model = GTK_TREE_MODEL (gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING));
188 /* Setup the tree view: */
189 priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
192 * The ID model column in not shown in the view. */
193 GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
194 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
195 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
196 gtk_tree_view_column_set_attributes (view_column, renderer,
197 "text", MODEL_COL_NAME, NULL);
198 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
199 gtk_tree_view_append_column (priv->treeview, view_column);
202 /* server name column: */
203 view_column = gtk_tree_view_column_new ();
204 renderer = gtk_cell_renderer_text_new ();
205 gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
206 gtk_tree_view_column_set_attributes (view_column, renderer,
207 "text", MODEL_COL_SERVER_NAME, NULL);
208 gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
209 gtk_tree_view_append_column (priv->treeview, view_column);
211 /* Fill the model with rows: */
212 fill_with_connections (self);
214 GtkWidget *vbox = gtk_vbox_new (FALSE, 2);
216 /* Put the treeview in a scrolled window and add it to the box: */
217 GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
218 gtk_widget_show (scrolled_window);
219 gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
220 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, 2);
221 gtk_widget_show (GTK_WIDGET (priv->treeview));
223 /* Add the buttons: */
224 GtkWidget *hbox = gtk_hbox_new (FALSE, 2);
225 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 2);
226 gtk_widget_show (hbox);
228 GtkWidget *button_edit = gtk_button_new_from_stock (GTK_STOCK_EDIT);
229 gtk_box_pack_start (GTK_BOX (hbox), button_edit, TRUE, FALSE, 2);
230 gtk_widget_show (button_edit);
231 g_signal_connect (G_OBJECT (button_edit), "clicked",
232 G_CALLBACK (on_button_edit), self);
234 GtkWidget *button_cancel = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
235 gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, 2);
236 gtk_widget_show (button_cancel);
237 g_signal_connect (G_OBJECT (button_cancel), "clicked",
238 G_CALLBACK (on_button_cancel), self);
240 gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
241 gtk_widget_show (vbox);
244 ModestConnectionSpecificSmtpWindow*
245 modest_connection_specific_smtp_window_new (void)
247 return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);