2007-04-05 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 <gtk/gtktreeview.h>
5 #include <gtk/gtkcellrenderertext.h>
6 #include <gtk/gtkliststore.h>
7 #include <gtk/gtkscrolledwindow.h>
8 #include <gtk/gtkbutton.h>
9 #include <gtk/gtkhbox.h>
10 #include <gtk/gtkvbox.h>
11 #include <gtk/gtkstock.h>
12
13 #include <glib/gi18n.h>
14
15 G_DEFINE_TYPE (ModestConnectionSpecificSmtpWindow, modest_connection_specific_smtp_window, GTK_TYPE_WINDOW);
16
17 #define CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE(o) \
18         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, ModestConnectionSpecificSmtpWindowPrivate))
19
20 typedef struct _ModestConnectionSpecificSmtpWindowPrivate ModestConnectionSpecificSmtpWindowPrivate;
21
22 struct _ModestConnectionSpecificSmtpWindowPrivate
23 {
24         GtkTreeView *treeview;
25         GtkTreeModel *model;
26 };
27
28 static void
29 modest_connection_specific_smtp_window_get_property (GObject *object, guint property_id,
30                                                                                                                         GValue *value, GParamSpec *pspec)
31 {
32         switch (property_id) {
33         default:
34                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
35         }
36 }
37
38 static void
39 modest_connection_specific_smtp_window_set_property (GObject *object, guint property_id,
40                                                                                                                         const GValue *value, GParamSpec *pspec)
41 {
42         switch (property_id) {
43         default:
44                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
45         }
46 }
47
48 static void
49 modest_connection_specific_smtp_window_dispose (GObject *object)
50 {
51         if (G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose)
52                 G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->dispose (object);
53 }
54
55 static void
56 modest_connection_specific_smtp_window_finalize (GObject *object)
57 {
58         G_OBJECT_CLASS (modest_connection_specific_smtp_window_parent_class)->finalize (object);
59 }
60
61 static void
62 modest_connection_specific_smtp_window_class_init (ModestConnectionSpecificSmtpWindowClass *klass)
63 {
64         GObjectClass *object_class = G_OBJECT_CLASS (klass);
65
66         g_type_class_add_private (klass, sizeof (ModestConnectionSpecificSmtpWindowPrivate));
67
68         object_class->get_property = modest_connection_specific_smtp_window_get_property;
69         object_class->set_property = modest_connection_specific_smtp_window_set_property;
70         object_class->dispose = modest_connection_specific_smtp_window_dispose;
71         object_class->finalize = modest_connection_specific_smtp_window_finalize;
72 }
73
74 enum MODEL_COLS {
75         MODEL_COL_NAME = 0,
76         MODEL_COL_SERVER_NAME = 1,
77         MODEL_COL_ID = 2
78 };
79
80 static void
81 fill_with_connections (ModestConnectionSpecificSmtpWindow *self)
82 {
83         /* TODO: 
84         * When TnyMaemoDevice provides enough of the libconic API to implement this. */
85 }
86
87 static void
88 on_button_edit (GtkButton *button, gpointer user_data)
89 {
90         
91 }
92
93 static void
94 on_button_cancel (GtkButton *button, gpointer user_data)
95 {
96         ModestConnectionSpecificSmtpWindow *self = MODEST_CONNECTION_SPECIFIC_SMTP_WINDOW (user_data);
97
98         /* Hide the window.
99          * The code that showed it will respond to the hide signal. */  
100         gtk_widget_hide (GTK_WIDGET (self));
101 }
102
103 static void
104 modest_connection_specific_smtp_window_init (ModestConnectionSpecificSmtpWindow *self)
105 {
106         ModestConnectionSpecificSmtpWindowPrivate *priv = CONNECTION_SPECIFIC_SMTP_WINDOW_GET_PRIVATE (self);
107
108         /* Create a tree model for the tree view:
109          * with a string for the name, a string for the server name, and an int for the ID.
110          * This must match our MODEL_COLS enum constants.
111          */
112         priv->model = GTK_TREE_MODEL (gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT));
113
114         /* Setup the tree view: */
115         priv->treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (priv->model));
116
117         /* name column:
118          * The ID model column in not shown in the view. */
119         GtkTreeViewColumn *view_column = gtk_tree_view_column_new ();
120         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
121         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
122         gtk_tree_view_column_set_attributes (view_column, renderer, 
123         "text", MODEL_COL_NAME, NULL);
124         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_connection_name"));
125         gtk_tree_view_append_column (priv->treeview, view_column);
126
127         
128         /* server name column: */
129         view_column = gtk_tree_view_column_new ();
130         renderer = gtk_cell_renderer_text_new ();
131         gtk_tree_view_column_pack_start(view_column, renderer, TRUE);
132         gtk_tree_view_column_set_attributes (view_column, renderer, 
133         "text", MODEL_COL_SERVER_NAME, NULL);
134         gtk_tree_view_column_set_title (view_column, _("mcen_ia_optionalsmtp_servername"));
135         gtk_tree_view_append_column (priv->treeview, view_column);
136         
137         /* Fill the model with rows: */
138         fill_with_connections (self);
139         
140         GtkWidget *vbox = gtk_vbox_new (FALSE, 2);
141         
142         /* Put the treeview in a scrolled window and add it to the box: */
143         GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
144         gtk_widget_show (scrolled_window);
145         gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (priv->treeview));
146         gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (scrolled_window), TRUE, TRUE, 2);
147         gtk_widget_show (GTK_WIDGET (priv->treeview));
148         
149         /* Add the buttons: */
150         GtkWidget *hbox = gtk_hbox_new (FALSE, 2);
151         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 2);
152         gtk_widget_show (hbox);
153         
154         GtkWidget *button_edit = gtk_button_new_from_stock (GTK_STOCK_EDIT);
155         gtk_box_pack_start (GTK_BOX (hbox), button_edit, TRUE, FALSE, 2);
156         gtk_widget_show (button_edit);
157         g_signal_connect (G_OBJECT (button_edit), "clicked",
158                 G_CALLBACK (on_button_edit), self);
159         
160         GtkWidget *button_cancel = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
161         gtk_box_pack_start (GTK_BOX (hbox), button_cancel, TRUE, FALSE, 2);
162         gtk_widget_show (button_cancel);
163         g_signal_connect (G_OBJECT (button_edit), "clicked",
164                 G_CALLBACK (on_button_cancel), self);
165         
166         gtk_widget_show (vbox);
167 }
168
169 ModestConnectionSpecificSmtpWindow*
170 modest_connection_specific_smtp_window_new (void)
171 {
172         return g_object_new (MODEST_TYPE_CONNECTION_SPECIFIC_SMTP_WINDOW, NULL);
173 }
174