2007-04-18 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / modest-connection-specific-smtp-window.c
1 /* connection-specific-smtp-window.c */
2
3 #include "modest-connection-specific-smtp-window.h"
4 #include "modest-connection-specific-smtp-edit-window.h"
5
6 #include <modest-runtime.h>
7 #include <tny-maemo-conic-device.h>
8
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>
17
18 #include <glib/gi18n.h>
19
20 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window, GTK_TYPE_WINDOW);
21
22 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
23         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
24
25 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
26
27 struct _ModestConnectionSpecificSmtpWindowPrivate
28 {
29         GtkTreeView *treeview;
30         GtkTreeModel *model;
31 };
32
33 static void
34 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
35                                                                                                                         GValue *value, GParamSpec *pspec)
36 {
37         switch (property_id) {
38         default:
39                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
40         }
41 }
42
43 static void
44 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
45                                                                                                                         const GValue *value, GParamSpec *pspec)
46 {
47         switch (property_id) {
48         default:
49                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
50         }
51 }
52
53 static void
54 modest_connection_specific_smtp_window_dispose (GObject *object)
55 {
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);
58 }
59
60 static void
61 modest_connection_specific_smtp_window_finalize (GObject *object)
62 {
63         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (object);
64
65         g_object_unref (G_OBJECT (priv->model));
66         
67         G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
68 }
69
70 static void
71 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
72 {
73         GObjectClass *object_class = G_OBJECT_CLASS (klass);
74
75         g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
76
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;
81 }
82
83 enum MODEL_COLS {
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 */
87 };
88
89 static void
90 fill_with_connections (ModestConnectionSpecificSmtpWindow *self)
91 {
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);
96         
97         TnyDevice *device = modest_runtime_get_device ();
98         g_assert (TNY_IS_MAEMO_CONIC_DEVICE (device));
99         
100         TnyMaemoConicDevice *maemo_device = TNY_MAEMO_CONIC_DEVICE (device);
101         
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));
105         
106         GSList* iter = list_iaps;
107         while (iter) {
108                 ConIcIap *iap = (ConIcIap*)iter->data;
109                 if (iap) {
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);
113                         
114                         /* Add the row to the model: */
115                         GtkTreeIter iter;
116                         gtk_list_store_append (liststore, &iter);
117                         gtk_list_store_set(liststore, &iter, MODEL_COL_ID, id, MODEL_COL_NAME, name, -1);
118                 }
119                 
120                 iter = g_slist_next (iter);     
121         }
122                 
123         if (list_iaps)
124                 tny_maemo_conic_device_free_iap_list (maemo_device, list_iaps);
125 }
126
127         
128 static void
129 on_button_edit (GtkButton *button, gpointer user_data)
130 {
131         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
132         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
133         
134         gchar *id = NULL;
135         gchar *name = NULL;
136         GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (self));
137         GtkTreeIter iter;
138         GtkTreeModel *model = 0;
139         if (gtk_tree_selection_get_selected (sel, &model, &iter)) {
140                 gtk_tree_model_get (priv->model, &iter, 
141                                     MODEL_COL_ID, &id, 
142                                     MODEL_COL_NAME, &name, 
143                                     -1);
144         
145                 /* TODO: Is 0 an allowed libconic IAP ID? 
146                  * If not then we should check for it. */
147                 
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);
151                         
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) {
156
157                 }               
158         }
159 }
160
161 static void
162 on_button_cancel (GtkButton *button, gpointer user_data)
163 {
164         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
165
166         /* Hide the window.
167          * The code that showed it will respond to the hide signal. */  
168         gtk_widget_hide (GTK_WIDGET (self));
169 }
170
171 static void
172 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
173 {
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);
179                             
180         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
181
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.
185          */
186         priv->model = GTK_TREE_MODEL (gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING));
187
188         /* Setup the tree view: */
189         priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
190
191         /* name column:
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);
200
201         
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);
210         
211         /* Fill the model with rows: */
212         fill_with_connections (self);
213         
214         GtkWidget *vbox = gtk_vbox_new (FALSE, 2);
215         
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));
222         
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);
227         
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);
233         
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);
239         
240         gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (vbox));
241         gtk_widget_show (vbox);
242 }
243
244 ModestConnectionSpecificSmtpWindow*
245 modest_connection_specific_smtp_window_new (void)
246 {
247         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
248 }
249