598ba2c83bbee769f5f61d09b0d5be03149af4a6
[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 #include <rtm-glib/rtm-glib.h>
27
28 #include "milk-main-window.h"
29 #include "milk-auth.h"
30 #include "milk-task-model.h"
31
32 G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW)
33
34 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
35 #define MILK_MAIN_WINDOW_PRIVATE(o) ((MILK_MAIN_WINDOW ((o)))->priv)
36
37 static GtkWidget *default_window = NULL;
38
39 struct _MilkMainWindowPrivate
40 {
41         MilkAuth *auth;
42
43         GtkWidget *app_menu;
44
45         GtkWidget *main_vbox;
46
47         GtkWidget *new_task_entry;
48         GtkWidget *task_view;
49         GtkWidget *task_selector;
50 };
51
52 enum {
53         TASK_VIEW_COLUMN_TITLE,
54         N_VIEW_COLUMNS
55 };
56
57 typedef struct {
58         const char *display_name;
59         const char *id;
60         gpointer    callback;
61 } MenuItem;
62
63 static void
64 milk_main_window_get_property (GObject    *object,
65                                guint       property_id,
66                                GValue     *value,
67                                GParamSpec *pspec)
68 {
69         switch (property_id)
70         {
71                 default:
72                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
73                                         pspec);
74         }
75 }
76
77 static void
78 milk_main_window_set_property (GObject      *object,
79                                guint         property_id,
80                                const GValue *value,
81                                GParamSpec   *pspec)
82 {
83         switch (property_id)
84         {
85                 default:
86                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
87                                         pspec);
88         }
89 }
90
91 static void
92 milk_main_window_dispose (GObject *object)
93 {
94         G_OBJECT_CLASS (milk_main_window_parent_class)->dispose (object);
95 }
96
97 static void
98 new_task_clicked_cb (GtkButton      *button,
99                      MilkMainWindow *window)
100 {
101         g_debug ("FIXME: implement 'new task' action");
102 }
103
104 static void
105 complete_clicked_cb (GtkButton      *button,
106                      MilkMainWindow *window)
107 {
108         g_debug ("FIXME: implement 'complete' action");
109 }
110
111 static void
112 delete_clicked_cb (GtkButton      *button,
113                    MilkMainWindow *window)
114 {
115         g_debug ("FIXME: implement 'delete' action");
116 }
117
118 static void
119 priority_plus_clicked_cb (GtkButton      *button,
120                           MilkMainWindow *window)
121 {
122         g_debug ("FIXME: implement 'priority plus' action");
123 }
124
125 static void
126 priority_minus_clicked_cb (GtkButton      *button,
127                            MilkMainWindow *window)
128 {
129         g_debug ("FIXME: implement 'priority minus' action");
130 }
131
132 static void
133 edit_clicked_cb (GtkButton      *button,
134                  MilkMainWindow *window)
135 {
136         g_debug ("FIXME: implement 'edit' action");
137 }
138
139 static MenuItem menu_items_always_shown[] = {
140         {"New Task",   "menu-item-new-task",       new_task_clicked_cb},
141 };
142
143 static MenuItem menu_items_selection_required[] = {
144         {"Edit",       "menu-item-edit",           edit_clicked_cb},
145         {"Priority +", "menu-item-priority_plus",  priority_plus_clicked_cb},
146         {"Priority -", "menu-item-priority_minus", priority_minus_clicked_cb},
147         {"Complete",   "menu-item-complete",       complete_clicked_cb},
148         {"Delete",     "menu-item-delete",         delete_clicked_cb},
149 };
150
151 static void
152 task_view_selection_changed_cb (HildonTouchSelector *view,
153                                 gint                 column,
154                                 MilkMainWindow      *window)
155 {
156         MilkMainWindowPrivate *priv;
157         GList *rows;
158         gboolean show = FALSE;
159         gint i;
160
161         priv = MILK_MAIN_WINDOW_PRIVATE (window);
162
163         rows = hildon_touch_selector_get_selected_rows (view, column);
164         show = (g_list_length (rows) > 0);
165
166         for (i = 0; i < G_N_ELEMENTS (menu_items_selection_required); i++) {
167                 GtkWidget *w;
168
169                 w = g_object_get_data (
170                                 G_OBJECT (priv->app_menu),
171                                 menu_items_selection_required[i].id);
172
173                 if (show)
174                         gtk_widget_show (w);
175                 else
176                         gtk_widget_hide (w);
177         }
178
179         g_list_free (rows);
180 }
181
182 static GtkWidget*
183 create_menu (gpointer user_data)
184 {
185         HildonAppMenu *menu;
186         MenuItem *menu_array;
187         gint i, length;
188         GtkWidget *w;
189
190         menu = HILDON_APP_MENU (hildon_app_menu_new ());
191
192         menu_array = menu_items_always_shown;
193         length = G_N_ELEMENTS (menu_items_always_shown);
194         for (i = 0; i < length; i++) {
195                 w = hildon_button_new_with_text (
196                                 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
197                                 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
198                                 _(menu_array[i].display_name), "");
199                 g_signal_connect (w, "clicked",
200                                 G_CALLBACK (menu_array[i].callback), user_data);
201                 g_object_set_data (G_OBJECT (menu), menu_array[i].id, w);
202                 hildon_app_menu_append (menu, GTK_BUTTON (w));
203                 gtk_widget_show (w);
204         }
205
206         menu_array = menu_items_selection_required;
207         length = G_N_ELEMENTS (menu_items_selection_required);
208         for (i = 0; i < length; i++) {
209                 w = hildon_button_new_with_text (
210                                 HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH,
211                                 HILDON_BUTTON_ARRANGEMENT_VERTICAL,
212                                 menu_array[i].display_name, "");
213                 g_signal_connect (w, "clicked",
214                                 G_CALLBACK (menu_array[i].callback), user_data);
215                 g_object_set_data (G_OBJECT (menu), menu_array[i].id, w);
216                 hildon_app_menu_append (menu, GTK_BUTTON (w));
217                 gtk_widget_hide (w);
218         }
219
220         gtk_widget_show (GTK_WIDGET (menu));
221
222         return GTK_WIDGET (menu);
223 }
224
225 static void
226 contact_column_render_func (GtkCellLayout   *cell_layout,
227                             GtkCellRenderer *renderer,
228                             GtkTreeModel    *model,
229                             GtkTreeIter     *iter,
230                             gpointer         user_data)
231 {
232         RtmTask *task;
233
234         gtk_tree_model_get (
235                         model, iter, MILK_TASK_MODEL_COLUMN_TASK, &task, -1);
236         g_object_set (renderer, "text", rtm_task_get_name (task), NULL);
237
238         g_object_unref (task);
239 }
240
241 static gboolean
242 begin_auth_idle (MilkMainWindow *window)
243 {
244         MilkMainWindowPrivate *priv;
245
246         priv = MILK_MAIN_WINDOW_PRIVATE (window);
247
248         /* FIXME: cut this */
249         g_debug ("trying to run the milk auth demo");
250         milk_auth_log_in (priv->auth);
251
252         return FALSE;
253 }
254
255 static void
256 milk_main_window_constructed (GObject* object)
257 {
258         MilkMainWindow *self = MILK_MAIN_WINDOW (object);
259         MilkMainWindowPrivate *priv = MILK_MAIN_WINDOW_PRIVATE (object);
260         GtkWidget *w;
261         GtkTreeModel *model;
262         GtkCellRenderer *renderer;
263         HildonTouchSelectorColumn *col;
264
265         w = gtk_vbox_new (FALSE, HILDON_MARGIN_DEFAULT);
266         gtk_container_add (GTK_CONTAINER (self), w);
267         priv->main_vbox = w;
268
269         /*
270          * New Task entry
271          */
272         w = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT);
273         gtk_box_pack_start (GTK_BOX (priv->main_vbox), w, FALSE, FALSE, 0);
274
275         /* FIXME: change this to hildon_gtk_entry_set_placeholder_text() is
276          * fixed, since this is deprecated */
277         hildon_entry_set_placeholder (HILDON_ENTRY (w),
278                         _("Enter a new task..."));
279         priv->new_task_entry = w;
280
281         /*
282          * Task List
283          */
284         priv->auth = milk_auth_get_default ();
285         model = GTK_TREE_MODEL (milk_task_model_new (priv->auth));
286         w = hildon_touch_selector_new ();
287
288
289         renderer = gtk_cell_renderer_text_new ();
290         g_object_set (renderer,
291                         "ellipsize", PANGO_ELLIPSIZE_END,
292                         NULL);
293
294         col = hildon_touch_selector_append_column (
295                         HILDON_TOUCH_SELECTOR (w), model, NULL, NULL);
296         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (col), renderer, TRUE);
297         gtk_cell_layout_set_cell_data_func (
298                         GTK_CELL_LAYOUT (col), renderer,
299                         (GtkCellLayoutDataFunc) contact_column_render_func,
300                         self, NULL);
301         g_object_unref (model);
302
303         hildon_touch_selector_set_column_selection_mode (
304                         HILDON_TOUCH_SELECTOR (w),
305                         HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
306         hildon_touch_selector_set_hildon_ui_mode (
307                         HILDON_TOUCH_SELECTOR (w), HILDON_UI_MODE_EDIT);
308         hildon_touch_selector_unselect_all (
309                         HILDON_TOUCH_SELECTOR (w), TASK_VIEW_COLUMN_TITLE);
310
311         gtk_box_pack_start (GTK_BOX (priv->main_vbox), w, TRUE, TRUE, 0);
312         g_object_set (w, "can-focus", TRUE, NULL);
313         gtk_widget_grab_focus (w);
314
315         g_signal_connect (
316                         G_OBJECT (w), "changed",
317                         G_CALLBACK (task_view_selection_changed_cb), self);
318         priv->task_view = w;
319
320         priv->app_menu = create_menu (self);
321         hildon_window_set_app_menu (
322                         HILDON_WINDOW (self), HILDON_APP_MENU (priv->app_menu));
323
324         /* break a cyclical dependency by doing this after the window is
325          * constructed */
326         g_idle_add ((GSourceFunc) begin_auth_idle, self);
327 }
328
329 static void
330 milk_main_window_class_init (MilkMainWindowClass *klass)
331 {
332         GObjectClass *object_class = G_OBJECT_CLASS (klass);
333
334         g_type_class_add_private (klass, sizeof (MilkMainWindowPrivate));
335
336         object_class->get_property = milk_main_window_get_property;
337         object_class->set_property = milk_main_window_set_property;
338         object_class->constructed = milk_main_window_constructed;
339         object_class->dispose = milk_main_window_dispose;
340 }
341
342 static void
343 milk_main_window_init (MilkMainWindow *self)
344 {
345         self->priv = G_TYPE_INSTANCE_GET_PRIVATE (
346                         self, MILK_TYPE_MAIN_WINDOW, MilkMainWindowPrivate);
347 }
348
349 GtkWidget*
350 milk_main_window_get_default ()
351 {
352         if (!default_window) {
353                 default_window = g_object_new (MILK_TYPE_MAIN_WINDOW, NULL);
354         }
355
356         return default_window;
357 }