Add initial version of MilkAuth, to manage the authentication with the server.
[milk] / src / milk-main-window.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License as
4  * published by the Free Software Foundation; either version 2 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public
13  * License along with this program; if not, write to the
14  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
15  * Boston, MA  02110-1301  USA
16  *
17  * Authors: Travis Reitter <treitter@gmail.com>
18  */
19
20 #include <config.h>
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
25 #include <hildon/hildon.h>
26
27 #include "milk-main-window.h"
28 #include "milk-task.h"
29 #include "milk-task-model.h"
30
31 G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW)
32
33 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
34 #define MILK_MAIN_WINDOW_PRIVATE(o) ((MILK_MAIN_WINDOW ((o)))->priv)
35
36 struct _MilkMainWindowPrivate
37 {
38         GtkWidget *app_menu;
39
40         GtkWidget *main_vbox;
41
42         GtkWidget *new_task_entry;
43         GtkWidget *task_view;
44         GtkWidget *task_selector;
45 };
46
47 enum {
48         TASK_VIEW_COLUMN_TITLE,
49         N_VIEW_COLUMNS
50 };
51
52 typedef struct {
53         const char *display_name;
54         const char *id;
55         gpointer    callback;
56 } MenuItem;
57
58 static void
59 milk_main_window_get_property (GObject    *object,
60                                guint       property_id,
61                                GValue     *value,
62                                GParamSpec *pspec)
63 {
64         switch (property_id)
65         {
66                 default:
67                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
68                                         pspec);
69         }
70 }
71
72 static void
73 milk_main_window_set_property (GObject      *object,
74                                guint         property_id,
75                                const GValue *value,
76                                GParamSpec   *pspec)
77 {
78         switch (property_id)
79         {
80                 default:
81                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
82                                         pspec);
83         }
84 }
85
86 static void
87 milk_main_window_dispose (GObject *object)
88 {
89         G_OBJECT_CLASS (milk_main_window_parent_class)->dispose (object);
90 }
91
92 static void
93 new_task_clicked_cb (GtkButton      *button,
94                      MilkMainWindow *window)
95 {
96         g_debug ("FIXME: implement 'new task' action");
97 }
98
99 static void
100 complete_clicked_cb (GtkButton      *button,
101                      MilkMainWindow *window)
102 {
103         g_debug ("FIXME: implement 'complete' action");
104 }
105
106 static void
107 delete_clicked_cb (GtkButton      *button,
108                    MilkMainWindow *window)
109 {
110         g_debug ("FIXME: implement 'delete' action");
111 }
112
113 static void
114 priority_plus_clicked_cb (GtkButton      *button,
115                           MilkMainWindow *window)
116 {
117         g_debug ("FIXME: implement 'priority plus' action");
118 }
119
120 static void
121 priority_minus_clicked_cb (GtkButton      *button,
122                            MilkMainWindow *window)
123 {
124         g_debug ("FIXME: implement 'priority minus' action");
125 }
126
127 static void
128 edit_clicked_cb (GtkButton      *button,
129                  MilkMainWindow *window)
130 {
131         g_debug ("FIXME: implement 'edit' action");
132 }
133
134 static MenuItem menu_items_always_shown[] = {
135         {"New Task",   "menu-item-new-task",       new_task_clicked_cb},
136 };
137
138 static MenuItem menu_items_selection_required[] = {
139         {"Edit",       "menu-item-edit",           edit_clicked_cb},
140         {"Priority +", "menu-item-priority_plus",  priority_plus_clicked_cb},
141         {"Priority -", "menu-item-priority_minus", priority_minus_clicked_cb},
142         {"Complete",   "menu-item-complete",       complete_clicked_cb},
143         {"Delete",     "menu-item-delete",         delete_clicked_cb},
144 };
145
146 static void
147 task_view_selection_changed_cb (HildonTouchSelector *view,
148                                 gint                 column,
149                                 MilkMainWindow      *window)
150 {
151         MilkMainWindowPrivate *priv;
152         GList *rows;
153         gboolean show = FALSE;
154         gint i;
155
156         priv = MILK_MAIN_WINDOW_PRIVATE (window);
157
158         rows = hildon_touch_selector_get_selected_rows (view, column);
159         show = (g_list_length (rows) > 0);
160
161         for (i = 0; i < G_N_ELEMENTS (menu_items_selection_required); i++) {
162                 GtkWidget *w;
163
164                 w = g_object_get_data (
165                                 G_OBJECT (priv->app_menu),
166                                 menu_items_selection_required[i].id);
167
168                 if (show)
169                         gtk_widget_show (w);
170                 else
171                         gtk_widget_hide (w);
172         }
173 }
174
175 static GtkWidget*
176 create_menu (gpointer user_data)
177 {
178         HildonAppMenu *menu;
179         MenuItem *menu_array;
180         gint i, length;
181         GtkWidget *w;
182
183         menu = HILDON_APP_MENU (hildon_app_menu_new ());
184
185         menu_array = menu_items_always_shown;
186         length = G_N_ELEMENTS (menu_items_always_shown);
187         for (i = 0; i < length; i++) {
188                 w = hildon_button_new_with_text (
189                                 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
190                                 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
191                                 _(menu_array[i].display_name), "");
192                 g_signal_connect (w, "clicked",
193                                 G_CALLBACK (menu_array[i].callback), user_data);
194                 g_object_set_data (G_OBJECT (menu), menu_array[i].id, w);
195                 hildon_app_menu_append (menu, GTK_BUTTON (w));
196                 gtk_widget_show (w);
197         }
198
199         menu_array = menu_items_selection_required;
200         length = G_N_ELEMENTS (menu_items_selection_required);
201         for (i = 0; i < length; i++) {
202                 w = hildon_button_new_with_text (
203                                 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
204                                 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
205                                 menu_array[i].display_name, "");
206                 g_signal_connect (w, "clicked",
207                                 G_CALLBACK (menu_array[i].callback), user_data);
208                 g_object_set_data (G_OBJECT (menu), menu_array[i].id, w);
209                 hildon_app_menu_append (menu, GTK_BUTTON (w));
210                 gtk_widget_hide (w);
211         }
212
213         gtk_widget_show (GTK_WIDGET (menu));
214
215         return GTK_WIDGET (menu);
216 }
217
218 static void
219 contact_column_render_func (GtkCellLayout   *cell_layout,
220                             GtkCellRenderer *renderer,
221                             GtkTreeModel    *model,
222                             GtkTreeIter     *iter,
223                             gpointer         user_data)
224 {
225         MilkTask *task;
226         char *title;
227
228         gtk_tree_model_get (
229                         model, iter, MILK_TASK_MODEL_COLUMN_TASK, &task, -1);
230
231         g_object_get (task, "title", &title, NULL);
232         g_object_set (renderer, "text", title, NULL);
233
234         g_free (title);
235         g_object_unref (task);
236 }
237
238 static void
239 milk_main_window_constructed (GObject* object)
240 {
241         MilkMainWindow *self = MILK_MAIN_WINDOW (object);
242         MilkMainWindowPrivate *priv = MILK_MAIN_WINDOW_PRIVATE (object);
243         GtkWidget *w;
244         GtkTreeModel *model;
245         GtkCellRenderer *renderer;
246         HildonTouchSelectorColumn *col;
247
248         w = gtk_vbox_new (FALSE, HILDON_MARGIN_DEFAULT);
249         gtk_container_add (GTK_CONTAINER (self), w);
250         priv->main_vbox = w;
251
252         /*
253          * New Task entry
254          */
255         w = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
256         gtk_box_pack_start (GTK_BOX (priv->main_vbox), w, FALSE, FALSE, 0);
257
258         /* FIXME: change this to hildon_gtk_entry_set_placeholder_text() is
259          * fixed, since this is deprecated */
260         hildon_entry_set_placeholder (HILDON_ENTRY (w),
261                         _("Enter a new task..."));
262         priv->new_task_entry = w;
263
264         /*
265          * Task List
266          */
267         model = GTK_TREE_MODEL (milk_task_model_new ());
268         w = hildon_touch_selector_new ();
269
270
271         renderer = gtk_cell_renderer_text_new ();
272         g_object_set (renderer,
273                         "ellipsize", PANGO_ELLIPSIZE_END,
274                         NULL);
275
276         col = hildon_touch_selector_append_column (
277                         HILDON_TOUCH_SELECTOR (w), model, NULL, NULL);
278         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (col), renderer, TRUE);
279         gtk_cell_layout_set_cell_data_func (
280                         GTK_CELL_LAYOUT (col), renderer,
281                         (GtkTreeCellDataFunc) contact_column_render_func, self,
282                         NULL);
283         g_object_unref (model);
284
285         hildon_touch_selector_set_column_selection_mode (
286                         HILDON_TOUCH_SELECTOR (w),
287                         HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
288         hildon_touch_selector_set_hildon_ui_mode (
289                         HILDON_TOUCH_SELECTOR (w), HILDON_UI_MODE_EDIT);
290         hildon_touch_selector_unselect_all (
291                         HILDON_TOUCH_SELECTOR (w), TASK_VIEW_COLUMN_TITLE);
292
293         gtk_box_pack_start (GTK_BOX (priv->main_vbox), w, TRUE, TRUE, 0);
294         g_object_set (w, "can-focus", TRUE, NULL);
295         gtk_widget_grab_focus (w);
296
297         g_signal_connect (
298                         G_OBJECT (w), "changed",
299                         G_CALLBACK (task_view_selection_changed_cb), self);
300         priv->task_view = w;
301
302         priv->app_menu = create_menu (self);
303         hildon_window_set_app_menu (
304                         HILDON_WINDOW (self), HILDON_APP_MENU (priv->app_menu));
305 }
306
307 static void
308 milk_main_window_class_init (MilkMainWindowClass *klass)
309 {
310         GObjectClass *object_class = G_OBJECT_CLASS (klass);
311
312         g_type_class_add_private (klass, sizeof (MilkMainWindowPrivate));
313
314         object_class->get_property = milk_main_window_get_property;
315         object_class->set_property = milk_main_window_set_property;
316         object_class->constructed = milk_main_window_constructed;
317         object_class->dispose = milk_main_window_dispose;
318 }
319
320 static void
321 milk_main_window_init (MilkMainWindow *self)
322 {
323         self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
324                         self, MILK_TYPE_MAIN_WINDOW, MilkMainWindowPrivate);
325 }
326
327 GtkWidget*
328 milk_main_window_new ()
329 {
330         return g_object_new (MILK_TYPE_MAIN_WINDOW,
331                              NULL);
332 }