]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-status-preset-dialog.c
gtk-doc entry
[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_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_status_edited (GtkCellRendererText *renderer,
190                                     char *path_str,
191                                     char *new_status,
192                                     EmpathyStatusPresetDialog *self)
193 {
194         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
195         GtkTreeModel *model;
196         GtkTreePath *path;
197         GtkTreeIter iter;
198         McPresence state;
199         char *old_status;
200         gboolean valid;
201
202         if (strlen (new_status) == 0) {
203                 /* status is empty, ignore */
204                 return;
205         }
206
207         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->presets_treeview));
208         path = gtk_tree_path_new_from_string (path_str);
209         valid = gtk_tree_model_get_iter (model, &iter, path);
210         gtk_tree_path_free (path);
211
212         if (!valid) return;
213
214         gtk_tree_model_get (model, &iter,
215                         PRESETS_STORE_STATE, &state,
216                         PRESETS_STORE_STATUS, &old_status,
217                         -1);
218
219         if (!strcmp (old_status, new_status)) {
220                 /* statuses are the same */
221                 g_free (old_status);
222                 return;
223         }
224
225         DEBUG ("EDITED STATUS (%s) -> (%s)\n", old_status, new_status);
226
227         empathy_status_presets_remove (state, old_status);
228         empathy_status_presets_set_last (state, new_status);
229
230         g_free (old_status);
231
232         status_preset_dialog_presets_update (self);
233 }
234
235 static void
236 status_preset_dialog_setup_presets_treeview (EmpathyStatusPresetDialog *self)
237 {
238         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
239         GtkWidget *treeview = priv->presets_treeview;
240         GtkListStore *store;
241         GtkTreeViewColumn *column;
242         GtkCellRenderer *renderer;
243
244         store = gtk_list_store_new (PRESETS_STORE_N_COLS,
245                         MC_TYPE_PRESENCE,       /* PRESETS_STORE_STATE */
246                         G_TYPE_STRING,          /* PRESETS_STORE_ICON_NAME */
247                         G_TYPE_STRING);         /* PRESETS_STORE_STATUS */
248
249         gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
250                                  GTK_TREE_MODEL (store));
251         g_object_unref (store);
252
253         status_preset_dialog_presets_update (self);
254
255         column = gtk_tree_view_column_new ();
256         gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
257
258         renderer = gtk_cell_renderer_pixbuf_new ();
259         gtk_tree_view_column_pack_start (column, renderer, FALSE);
260         gtk_tree_view_column_add_attribute (column, renderer,
261                         "icon-name", PRESETS_STORE_ICON_NAME);
262
263         renderer = gtk_cell_renderer_text_new ();
264         gtk_tree_view_column_pack_start (column, renderer, TRUE);
265         gtk_tree_view_column_add_attribute (column, renderer,
266                         "text", PRESETS_STORE_STATUS);
267         g_object_set (renderer,
268                         "editable", TRUE,
269                         NULL);
270         g_signal_connect (renderer, "edited",
271                         G_CALLBACK (status_preset_dialog_status_edited), self);
272 }
273
274 static void
275 status_preset_dialog_preset_selection_changed (GtkTreeSelection *selection,
276                                                GtkWidget *remove_button)
277 {
278         /* update the sensitivity of the Remove button */
279         gtk_widget_set_sensitive (remove_button,
280                         gtk_tree_selection_get_selected (selection, NULL, NULL));
281 }
282
283 static void
284 status_preset_dialog_preset_remove (GtkButton *button,
285                                     EmpathyStatusPresetDialog *self)
286 {
287         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
288         GtkTreeSelection *selection;
289         GtkTreeModel *model;
290         GtkTreeIter iter;
291         McPresence state;
292         char *status;
293
294         selection = gtk_tree_view_get_selection (
295                         GTK_TREE_VIEW (priv->presets_treeview));
296
297         g_return_if_fail (gtk_tree_selection_get_selected (selection,
298                                 &model, &iter));
299
300         gtk_tree_model_get (model, &iter,
301                         PRESETS_STORE_STATE, &state,
302                         PRESETS_STORE_STATUS, &status,
303                         -1);
304
305         DEBUG ("REMOVE PRESET (%i, %s)\n", state, status);
306         empathy_status_presets_remove (state, status);
307
308         g_free (status);
309
310         status_preset_dialog_presets_update (self);
311 }
312
313 static void
314 status_preset_dialog_set_add_combo_changed (EmpathyStatusPresetDialog *self,
315                                             gboolean state,
316                                             gboolean reset_text)
317 {
318         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
319         GtkWidget *entry;
320
321         entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
322
323         priv->add_combo_changed = state;
324         gtk_widget_set_sensitive (priv->add_button, state);
325
326         if (state) {
327                 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, NULL);
328         } else {
329                 GdkColor colour;
330
331                 gdk_color_parse ("Gray", &colour); /* FIXME - theme */
332                 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, &colour);
333
334                 if (reset_text) {
335                         priv->block_add_combo_changed++;
336                         gtk_entry_set_text (GTK_ENTRY (entry),
337                                         _("Enter Custom Message"));
338                         priv->block_add_combo_changed--;
339                 }
340         }
341 }
342
343 static void
344 status_preset_dialog_add_combo_changed (GtkComboBox *combo,
345                                         EmpathyStatusPresetDialog *self)
346 {
347         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
348         GtkWidget *entry;
349         GtkTreeModel *model;
350         GtkTreeIter iter;
351
352         if (priv->block_add_combo_changed) return;
353
354         model = gtk_combo_box_get_model (combo);
355         entry = gtk_bin_get_child (GTK_BIN (combo));
356
357         if (gtk_combo_box_get_active_iter (combo, &iter)) {
358                 char *icon_name;
359
360                 priv->selected_iter = iter;
361                 gtk_tree_model_get (model, &iter,
362                                 PRESETS_STORE_ICON_NAME, &icon_name,
363                                 -1);
364
365                 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
366                                 GTK_ENTRY_ICON_PRIMARY,
367                                 icon_name);
368
369                 g_free (icon_name);
370
371                 status_preset_dialog_set_add_combo_changed (self, FALSE, TRUE);
372         } else {
373                 const char *status;
374
375                 status = gtk_entry_get_text (GTK_ENTRY (entry));
376                 status_preset_dialog_set_add_combo_changed (self,
377                                 strlen (status) > 0, FALSE);
378         }
379 }
380
381 static void
382 status_preset_dialog_add_preset (GtkWidget *widget,
383                                  EmpathyStatusPresetDialog *self)
384 {
385         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
386         GtkTreeModel *model;
387         GtkWidget *entry;
388         McPresence state;
389         const char *status;
390
391         g_return_if_fail (priv->add_combo_changed);
392
393         model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->add_combobox));
394         entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
395
396         status = gtk_entry_get_text (GTK_ENTRY (entry));
397         gtk_tree_model_get (model, &priv->selected_iter,
398                         PRESETS_STORE_STATE, &state,
399                         -1);
400
401         DEBUG ("ADD PRESET (%i, %s)\n", state, status);
402         empathy_status_presets_set_last (state, status);
403
404         status_preset_dialog_presets_update (self);
405         status_preset_add_combo_reset (self);
406 }
407
408 static gboolean
409 status_preset_dialog_add_combo_press_event (GtkWidget *widget,
410                                             GdkEventButton *event,
411                                             EmpathyStatusPresetDialog *self)
412 {
413         if (!GTK_WIDGET_HAS_FOCUS (widget)) {
414                 /* if the widget isn't focused, focus it and select the text */
415                 gtk_widget_grab_focus (widget);
416                 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
417
418                 return TRUE;
419         }
420
421         return FALSE;
422 }
423
424 static gboolean
425 status_preset_dialog_add_combo_focus_out (GtkWidget *widget,
426                                           GdkEventFocus *event,
427                                           EmpathyStatusPresetDialog *self)
428 {
429         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
430         const char *status;
431
432         gtk_editable_set_position (GTK_EDITABLE (widget), 0);
433
434         status = gtk_entry_get_text (GTK_ENTRY (widget));
435         status_preset_dialog_set_add_combo_changed (self,
436                         priv->add_combo_changed && strlen (status) > 0,
437                         TRUE);
438
439         return FALSE;
440 }
441
442 static void
443 empathy_status_preset_dialog_init (EmpathyStatusPresetDialog *self)
444 {
445         EmpathyStatusPresetDialogPriv *priv = self->priv =
446                 G_TYPE_INSTANCE_GET_PRIVATE (self,
447                         EMPATHY_TYPE_STATUS_PRESET_DIALOG,
448                         EmpathyStatusPresetDialogPriv);
449         GtkBuilder *gui;
450         GtkWidget *toplevel_vbox, *remove_button, *entry;
451         char *filename;
452
453         gtk_window_set_title (GTK_WINDOW (self),
454                         _("Edit Custom Messages"));
455         gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
456         gtk_dialog_add_button (GTK_DIALOG (self),
457                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
458
459         filename = empathy_file_lookup ("empathy-status-preset-dialog.ui",
460                         "libempathy-gtk");
461         gui = empathy_builder_get_file (filename,
462                         "toplevel-vbox", &toplevel_vbox,
463                         "presets-treeview", &priv->presets_treeview,
464                         "remove-button", &remove_button,
465                         "add-combobox", &priv->add_combobox,
466                         "add-button", &priv->add_button,
467                         NULL);
468         g_free (filename);
469
470         g_signal_connect (gtk_tree_view_get_selection (
471                                 GTK_TREE_VIEW (priv->presets_treeview)),
472                         "changed",
473                         G_CALLBACK (status_preset_dialog_preset_selection_changed),
474                         remove_button);
475
476         entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
477         g_signal_connect (entry, "activate",
478                         G_CALLBACK (status_preset_dialog_add_preset), self);
479         g_signal_connect (entry, "button-press-event",
480                         G_CALLBACK (status_preset_dialog_add_combo_press_event),
481                         self);
482         g_signal_connect (entry, "focus-out-event",
483                         G_CALLBACK (status_preset_dialog_add_combo_focus_out),
484                         self);
485
486         empathy_builder_connect (gui, self,
487                         "remove-button", "clicked", status_preset_dialog_preset_remove,
488                         "add-combobox", "changed", status_preset_dialog_add_combo_changed,
489                         "add-button", "clicked", status_preset_dialog_add_preset,
490                         NULL);
491
492         status_preset_dialog_setup_presets_treeview (self);
493         status_preset_dialog_setup_add_combobox (self);
494
495         gtk_box_pack_start(GTK_BOX (GTK_DIALOG (self)->vbox), toplevel_vbox,
496                         TRUE, TRUE, 0);
497
498         g_object_unref (gui);
499 }
500
501 /**
502  * empathy_status_preset_dialog_new:
503  * @parent: the parent window of this dialog (or NULL)
504  *
505  * Creates a new #EmpathyStatusPresetDialog that allows the user to
506  * add/remove/edit their saved status messages.
507  *
508  * Returns: the newly constructed dialog.
509  */
510 GtkWidget *
511 empathy_status_preset_dialog_new (GtkWindow *parent)
512 {
513         GtkWidget *self = g_object_new (EMPATHY_TYPE_STATUS_PRESET_DIALOG,
514                         NULL);
515
516         if (parent) {
517                 gtk_window_set_transient_for (GTK_WINDOW (self), parent);
518         }
519
520         return self;
521 }