]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-status-preset-dialog.c
085332f35f38887923339ce234b3e6ff2b52f81e
[empathy.git] / libempathy-gtk / empathy-status-preset-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * empathy-status-preset-dialog.c
4  *
5  * EmpathyStatusPresetDialog - a dialog for adding and removing preset status
6  * messages.
7  *
8  * Copyright (C) 2009 Collabora Ltd.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of the
13  * License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * Authors: Davyd Madeley <davyd.madeley@collabora.co.uk>
26  */
27
28 #include "config.h"
29
30 #include <glib/gi18n-lib.h>
31 #include <gtk/gtk.h>
32
33 #include <libmissioncontrol/mc-enum-types.h>
34
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-status-presets.h>
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
40
41 #include "empathy-ui-utils.h"
42 #include "empathy-status-preset-dialog.h"
43
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusPresetDialog)
45
46 G_DEFINE_TYPE (EmpathyStatusPresetDialog, empathy_status_preset_dialog, GTK_TYPE_DIALOG);
47
48 static McPresence states[] = {
49         MC_PRESENCE_AVAILABLE,
50         MC_PRESENCE_DO_NOT_DISTURB,
51         MC_PRESENCE_AWAY
52 };
53
54 typedef struct _EmpathyStatusPresetDialogPriv EmpathyStatusPresetDialogPriv;
55 struct _EmpathyStatusPresetDialogPriv
56 {
57         int block_add_combo_changed;
58
59         GtkWidget *presets_treeview;
60         GtkWidget *add_combobox;
61         GtkWidget *add_button;
62
63         GtkTreeIter selected_iter;
64         gboolean add_combo_changed;
65 };
66
67 enum
68 {
69         PRESETS_STORE_STATE,
70         PRESETS_STORE_ICON_NAME,
71         PRESETS_STORE_STATUS,
72         PRESETS_STORE_N_COLS
73 };
74
75 enum
76 {
77         ADD_COMBO_STATE,
78         ADD_COMBO_ICON_NAME,
79         ADD_COMBO_STATUS,
80         ADD_COMBO_DEFAULT_TEXT,
81         ADD_COMBO_N_COLS
82 };
83
84 static void
85 empathy_status_preset_dialog_class_init (EmpathyStatusPresetDialogClass *class)
86 {
87         GObjectClass *gobject_class;
88
89         gobject_class = G_OBJECT_CLASS (class);
90
91         g_type_class_add_private (gobject_class,
92                         sizeof (EmpathyStatusPresetDialogPriv));
93 }
94
95 static void
96 status_preset_dialog_setup_presets_update (EmpathyStatusPresetDialog *self)
97 {
98         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
99         GtkListStore *store;
100         int i;
101
102         store = GTK_LIST_STORE (gtk_tree_view_get_model (
103                                 GTK_TREE_VIEW (priv->presets_treeview)));
104
105         gtk_list_store_clear (store);
106
107         for (i = 0; i < G_N_ELEMENTS (states); i++) {
108                 GList *presets, *l;
109                 const char *icon_name;
110
111                 presets = empathy_status_presets_get (states[i], -1);
112                 icon_name = empathy_icon_name_for_presence (states[i]);
113
114                 for (l = presets; l; l = l->next) {
115                         char *preset = (char *) l->data;
116
117                         gtk_list_store_insert_with_values (store,
118                                         NULL, -1,
119                                         PRESETS_STORE_STATE, states[i],
120                                         PRESETS_STORE_ICON_NAME, icon_name,
121                                         PRESETS_STORE_STATUS, preset,
122                                         -1);
123                 }
124
125                 g_list_free (presets);
126         }
127 }
128
129 static void
130 status_preset_add_combo_reset (EmpathyStatusPresetDialog *self)
131 {
132         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
133
134         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->add_combobox),
135                         &priv->selected_iter);
136 }
137
138 static void
139 status_preset_dialog_setup_add_combobox (EmpathyStatusPresetDialog *self)
140 {
141         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
142         GtkWidget *combobox = priv->add_combobox;
143         GtkListStore *store;
144         GtkCellRenderer *renderer;
145         int i;
146
147         store = gtk_list_store_new (ADD_COMBO_N_COLS,
148                         MC_TYPE_PRESENCE,       /* ADD_COMBO_STATE */
149                         G_TYPE_STRING,          /* ADD_COMBO_ICON_NAME */
150                         G_TYPE_STRING,          /* ADD_COMBO_STATUS */
151                         G_TYPE_STRING);         /* ADD_COMBO_DEFAULT_TEXT */
152
153         gtk_combo_box_set_model (GTK_COMBO_BOX (combobox),
154                                  GTK_TREE_MODEL (store));
155         g_object_unref (store);
156
157         gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combobox),
158                         ADD_COMBO_DEFAULT_TEXT);
159
160         for (i = 0; i < G_N_ELEMENTS (states); i++) {
161                 gtk_list_store_insert_with_values (store, NULL, -1,
162                                 ADD_COMBO_STATE, states[i],
163                                 ADD_COMBO_ICON_NAME, empathy_icon_name_for_presence (states[i]),
164                                 ADD_COMBO_STATUS, empathy_presence_get_default_message (states[i]),
165                                 ADD_COMBO_DEFAULT_TEXT, "",
166                                 -1);
167         }
168
169         gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
170
171         renderer = gtk_cell_renderer_pixbuf_new ();
172         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
173         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combobox), renderer,
174                         "icon-name", ADD_COMBO_ICON_NAME);
175
176         renderer = gtk_cell_renderer_text_new ();
177         gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
178         gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combobox), renderer,
179                         "text", ADD_COMBO_STATUS);
180         g_object_set (renderer,
181                         "style", PANGO_STYLE_ITALIC,
182                         "foreground", "Gray", /* FIXME - theme */
183                         NULL);
184
185         gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
186 }
187
188 static void
189 status_preset_dialog_setup_presets_treeview (EmpathyStatusPresetDialog *self)
190 {
191         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
192         GtkWidget *treeview = priv->presets_treeview;
193         GtkListStore *store;
194         GtkTreeViewColumn *column;
195         GtkCellRenderer *renderer;
196
197         store = gtk_list_store_new (PRESETS_STORE_N_COLS,
198                         MC_TYPE_PRESENCE,       /* PRESETS_STORE_STATE */
199                         G_TYPE_STRING,          /* PRESETS_STORE_ICON_NAME */
200                         G_TYPE_STRING);         /* PRESETS_STORE_STATUS */
201
202         gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
203                                  GTK_TREE_MODEL (store));
204         g_object_unref (store);
205
206         status_preset_dialog_setup_presets_update (self);
207
208         column = gtk_tree_view_column_new ();
209         gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
210
211         renderer = gtk_cell_renderer_pixbuf_new ();
212         gtk_tree_view_column_pack_start (column, renderer, FALSE);
213         gtk_tree_view_column_add_attribute (column, renderer,
214                         "icon-name", PRESETS_STORE_ICON_NAME);
215
216         renderer = gtk_cell_renderer_text_new ();
217         gtk_tree_view_column_pack_start (column, renderer, TRUE);
218         gtk_tree_view_column_add_attribute (column, renderer,
219                         "text", PRESETS_STORE_STATUS);
220 }
221
222 static void
223 status_preset_dialog_preset_selection_changed (GtkTreeSelection *selection,
224                                                GtkWidget *remove_button)
225 {
226         /* update the sensitivity of the Remove button */
227         gtk_widget_set_sensitive (remove_button,
228                         gtk_tree_selection_get_selected (selection, NULL, NULL));
229 }
230
231 static void
232 status_preset_dialog_preset_remove (GtkButton *button,
233                                     EmpathyStatusPresetDialog *self)
234 {
235         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
236         GtkTreeSelection *selection;
237         GtkTreeModel *model;
238         GtkTreeIter iter;
239         McPresence state;
240         char *status;
241
242         selection = gtk_tree_view_get_selection (
243                         GTK_TREE_VIEW (priv->presets_treeview));
244
245         g_return_if_fail (gtk_tree_selection_get_selected (selection,
246                                 &model, &iter));
247
248         gtk_tree_model_get (model, &iter,
249                         PRESETS_STORE_STATE, &state,
250                         PRESETS_STORE_STATUS, &status,
251                         -1);
252
253         DEBUG ("REMOVE PRESET (%i, %s)\n", state, status);
254         empathy_status_presets_remove (state, status);
255
256         g_free (status);
257
258         status_preset_dialog_setup_presets_update (self);
259 }
260
261 static void
262 status_preset_dialog_set_add_combo_changed (EmpathyStatusPresetDialog *self,
263                                             gboolean state,
264                                             gboolean reset_text)
265 {
266         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
267         GtkWidget *entry;
268
269         entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
270
271         priv->add_combo_changed = state;
272         gtk_widget_set_sensitive (priv->add_button, state);
273
274         if (state) {
275                 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, NULL);
276         } else {
277                 GdkColor colour;
278
279                 gdk_color_parse ("Gray", &colour); /* FIXME - theme */
280                 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, &colour);
281
282                 if (reset_text) {
283                         priv->block_add_combo_changed++;
284                         gtk_entry_set_text (GTK_ENTRY (entry),
285                                         _("Enter Custom Message"));
286                         priv->block_add_combo_changed--;
287                 }
288         }
289 }
290
291 static void
292 status_preset_dialog_add_combo_changed (GtkComboBox *combo,
293                                         EmpathyStatusPresetDialog *self)
294 {
295         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
296         GtkWidget *entry;
297         GtkTreeModel *model;
298         GtkTreeIter iter;
299
300         if (priv->block_add_combo_changed) return;
301
302         model = gtk_combo_box_get_model (combo);
303         entry = gtk_bin_get_child (GTK_BIN (combo));
304
305         if (gtk_combo_box_get_active_iter (combo, &iter)) {
306                 char *icon_name;
307
308                 priv->selected_iter = iter;
309                 gtk_tree_model_get (model, &iter,
310                                 PRESETS_STORE_ICON_NAME, &icon_name,
311                                 -1);
312
313                 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
314                                 GTK_ENTRY_ICON_PRIMARY,
315                                 icon_name);
316
317                 g_free (icon_name);
318
319                 status_preset_dialog_set_add_combo_changed (self, FALSE, TRUE);
320         } else {
321                 const char *status;
322
323                 status = gtk_entry_get_text (GTK_ENTRY (entry));
324                 status_preset_dialog_set_add_combo_changed (self,
325                                 strlen (status) > 0, FALSE);
326         }
327 }
328
329 static void
330 status_preset_dialog_add_preset (GtkWidget *widget,
331                                  EmpathyStatusPresetDialog *self)
332 {
333         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
334         GtkTreeModel *model;
335         GtkWidget *entry;
336         McPresence state;
337         const char *status;
338
339         g_return_if_fail (priv->add_combo_changed);
340
341         model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->add_combobox));
342         entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
343
344         status = gtk_entry_get_text (GTK_ENTRY (entry));
345         gtk_tree_model_get (model, &priv->selected_iter,
346                         PRESETS_STORE_STATE, &state,
347                         -1);
348
349         DEBUG ("ADD PRESET (%i, %s)\n", state, status);
350         empathy_status_presets_set_last (state, status);
351
352         status_preset_dialog_setup_presets_update (self);
353         status_preset_add_combo_reset (self);
354 }
355
356 static gboolean
357 status_preset_dialog_add_combo_press_event (GtkWidget *widget,
358                                             GdkEventButton *event,
359                                             EmpathyStatusPresetDialog *self)
360 {
361         if (!GTK_WIDGET_HAS_FOCUS (widget)) {
362                 /* if the widget isn't focused, focus it and select the text */
363                 gtk_widget_grab_focus (widget);
364                 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
365
366                 return TRUE;
367         }
368
369         return FALSE;
370 }
371
372 static gboolean
373 status_preset_dialog_add_combo_focus_out (GtkWidget *widget,
374                                           GdkEventFocus *event,
375                                           EmpathyStatusPresetDialog *self)
376 {
377         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
378         const char *status;
379
380         gtk_editable_set_position (GTK_EDITABLE (widget), 0);
381
382         status = gtk_entry_get_text (GTK_ENTRY (widget));
383         status_preset_dialog_set_add_combo_changed (self,
384                         priv->add_combo_changed && strlen (status) > 0,
385                         TRUE);
386
387         return FALSE;
388 }
389
390 static void
391 empathy_status_preset_dialog_init (EmpathyStatusPresetDialog *self)
392 {
393         EmpathyStatusPresetDialogPriv *priv = self->priv =
394                 G_TYPE_INSTANCE_GET_PRIVATE (self,
395                         EMPATHY_TYPE_STATUS_PRESET_DIALOG,
396                         EmpathyStatusPresetDialogPriv);
397         GtkBuilder *gui;
398         GtkWidget *toplevel_vbox, *remove_button, *entry;
399         char *filename;
400
401         gtk_window_set_title (GTK_WINDOW (self),
402                         _("Edit Custom Messages"));
403         gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
404         gtk_dialog_add_button (GTK_DIALOG (self),
405                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
406
407         filename = empathy_file_lookup ("empathy-status-preset-dialog.ui",
408                         "libempathy-gtk");
409         gui = empathy_builder_get_file (filename,
410                         "toplevel-vbox", &toplevel_vbox,
411                         "presets-treeview", &priv->presets_treeview,
412                         "remove-button", &remove_button,
413                         "add-combobox", &priv->add_combobox,
414                         "add-button", &priv->add_button,
415                         NULL);
416         g_free (filename);
417
418         g_signal_connect (gtk_tree_view_get_selection (
419                                 GTK_TREE_VIEW (priv->presets_treeview)),
420                         "changed",
421                         G_CALLBACK (status_preset_dialog_preset_selection_changed),
422                         remove_button);
423
424         entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
425         g_signal_connect (entry, "activate",
426                         G_CALLBACK (status_preset_dialog_add_preset), self);
427         g_signal_connect (entry, "button-press-event",
428                         G_CALLBACK (status_preset_dialog_add_combo_press_event),
429                         self);
430         g_signal_connect (entry, "focus-out-event",
431                         G_CALLBACK (status_preset_dialog_add_combo_focus_out),
432                         self);
433
434         empathy_builder_connect (gui, self,
435                         "remove-button", "clicked", status_preset_dialog_preset_remove,
436                         "add-combobox", "changed", status_preset_dialog_add_combo_changed,
437                         "add-button", "clicked", status_preset_dialog_add_preset,
438                         NULL);
439
440         status_preset_dialog_setup_presets_treeview (self);
441         status_preset_dialog_setup_add_combobox (self);
442
443         gtk_box_pack_start(GTK_BOX (GTK_DIALOG (self)->vbox), toplevel_vbox,
444                         TRUE, TRUE, 0);
445
446         g_object_unref (gui);
447 }
448
449 GtkWidget *
450 empathy_status_preset_dialog_new (GtkWindow *parent)
451 {
452         GtkWidget *self = g_object_new (EMPATHY_TYPE_STATUS_PRESET_DIALOG,
453                         NULL);
454
455         if (parent) {
456                 gtk_window_set_transient_for (GTK_WINDOW (self), parent);
457         }
458
459         return self;
460 }