]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-status-preset-dialog.c
Merge remote-tracking branch 'origin/gnome-3-8'
[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., 51 Franklin St, Fifth Floor,
23  * Boston, MA  02110-1301  USA
24  *
25  * Authors: Danielle Madeley <danielle.madeley@collabora.co.uk>
26  */
27 /**
28  * SECTION:empathy-status-preset-dialog
29  * @title: EmpathyStatusPresetDialog
30  * @short_description: a dialog for editing the saved status messages
31  * @include: libempathy-gtk/empathy-status-preset-dialog.h
32  *
33  * #EmpathyStatusPresetDialog is a dialog allowing the user to add/remove/edit
34  * their saved status messages.
35  */
36
37 #include "config.h"
38 #include "empathy-status-preset-dialog.h"
39
40 #include <glib/gi18n-lib.h>
41
42 #include "empathy-status-presets.h"
43 #include "empathy-ui-utils.h"
44 #include "empathy-utils.h"
45
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include "empathy-debug.h"
48
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusPresetDialog)
50
51 G_DEFINE_TYPE (EmpathyStatusPresetDialog, empathy_status_preset_dialog, GTK_TYPE_DIALOG);
52
53 static TpConnectionPresenceType states[] = {
54         TP_CONNECTION_PRESENCE_TYPE_AVAILABLE,
55         TP_CONNECTION_PRESENCE_TYPE_BUSY,
56         TP_CONNECTION_PRESENCE_TYPE_AWAY,
57 };
58
59 typedef struct _EmpathyStatusPresetDialogPriv EmpathyStatusPresetDialogPriv;
60 struct _EmpathyStatusPresetDialogPriv
61 {
62         /* block status_preset_dialog_add_combo_changed () when > 0 */
63         int block_add_combo_changed;
64
65         GtkWidget *presets_treeview;
66         GtkTreeViewColumn *column;
67         GtkCellRenderer *text_cell;
68
69         GtkTreeIter selected_iter;
70         gboolean add_combo_changed;
71         char *saved_status;
72 };
73
74 enum
75 {
76         PRESETS_STORE_STATE,
77         PRESETS_STORE_ICON_NAME,
78         PRESETS_STORE_STATUS,
79         PRESETS_STORE_N_COLS
80 };
81
82 enum
83 {
84         ADD_COMBO_STATE,
85         ADD_COMBO_ICON_NAME,
86         ADD_COMBO_STATUS,
87         ADD_COMBO_DEFAULT_TEXT,
88         ADD_COMBO_N_COLS
89 };
90
91 static void
92 empathy_status_preset_dialog_finalize (GObject *self)
93 {
94         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
95
96         g_free (priv->saved_status);
97
98         G_OBJECT_CLASS (empathy_status_preset_dialog_parent_class)->finalize (self);
99 }
100
101 static void
102 empathy_status_preset_dialog_class_init (EmpathyStatusPresetDialogClass *class)
103 {
104         GObjectClass *gobject_class;
105
106         gobject_class = G_OBJECT_CLASS (class);
107         gobject_class->finalize = empathy_status_preset_dialog_finalize;
108
109         g_type_class_add_private (gobject_class,
110                         sizeof (EmpathyStatusPresetDialogPriv));
111 }
112
113 static void
114 status_preset_dialog_presets_update (EmpathyStatusPresetDialog *self)
115 {
116         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
117         GtkListStore *store;
118         guint i;
119
120         store = GTK_LIST_STORE (gtk_tree_view_get_model (
121                                 GTK_TREE_VIEW (priv->presets_treeview)));
122
123         gtk_list_store_clear (store);
124
125         for (i = 0; i < G_N_ELEMENTS (states); i++) {
126                 GList *presets, *l;
127                 const char *icon_name;
128
129                 icon_name = empathy_icon_name_for_presence (states[i]);
130                 presets = empathy_status_presets_get (states[i], -1);
131                 presets = g_list_sort (presets, (GCompareFunc) g_utf8_collate);
132
133                 for (l = presets; l; l = l->next) {
134                         char *preset = (char *) l->data;
135
136                         gtk_list_store_insert_with_values (store,
137                                         NULL, -1,
138                                         PRESETS_STORE_STATE, states[i],
139                                         PRESETS_STORE_ICON_NAME, icon_name,
140                                         PRESETS_STORE_STATUS, preset,
141                                         -1);
142                 }
143
144                 g_list_free (presets);
145         }
146 }
147
148 static void
149 status_preset_dialog_status_edited (GtkCellRendererText *renderer,
150                                     char *path_str,
151                                     char *new_status,
152                                     EmpathyStatusPresetDialog *self)
153 {
154         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
155         GtkTreeModel *model;
156         GtkTreePath *path;
157         GtkTreeIter iter;
158         TpConnectionPresenceType state;
159         char *old_status;
160         gboolean valid;
161
162         if (strlen (new_status) == 0) {
163                 /* status is empty, ignore */
164                 return;
165         }
166
167         model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->presets_treeview));
168         path = gtk_tree_path_new_from_string (path_str);
169         valid = gtk_tree_model_get_iter (model, &iter, path);
170         gtk_tree_path_free (path);
171
172         if (!valid) return;
173
174         gtk_tree_model_get (model, &iter,
175                         PRESETS_STORE_STATE, &state,
176                         PRESETS_STORE_STATUS, &old_status,
177                         -1);
178
179         if (!strcmp (old_status, new_status)) {
180                 /* statuses are the same */
181                 g_free (old_status);
182                 return;
183         }
184
185         DEBUG ("EDITED STATUS (%s) -> (%s)\n", old_status, new_status);
186
187         empathy_status_presets_remove (state, old_status);
188         empathy_status_presets_set_last (state, new_status);
189
190         g_free (old_status);
191
192         status_preset_dialog_presets_update (self);
193 }
194
195 static void
196 status_preset_dialog_setup_presets_treeview (EmpathyStatusPresetDialog *self)
197 {
198         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
199         GtkWidget *treeview = priv->presets_treeview;
200         GtkListStore *store;
201         GtkTreeViewColumn *column;
202         GtkCellRenderer *renderer;
203
204         store = gtk_list_store_new (PRESETS_STORE_N_COLS,
205                         G_TYPE_UINT,            /* PRESETS_STORE_STATE */
206                         G_TYPE_STRING,          /* PRESETS_STORE_ICON_NAME */
207                         G_TYPE_STRING);         /* PRESETS_STORE_STATUS */
208
209         gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
210                                  GTK_TREE_MODEL (store));
211         g_object_unref (store);
212
213         status_preset_dialog_presets_update (self);
214
215         column = gtk_tree_view_column_new ();
216         priv->column = column;
217         gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
218
219         renderer = gtk_cell_renderer_pixbuf_new ();
220         gtk_tree_view_column_pack_start (column, renderer, FALSE);
221         gtk_tree_view_column_add_attribute (column, renderer,
222                         "icon-name", PRESETS_STORE_ICON_NAME);
223
224         renderer = gtk_cell_renderer_text_new ();
225         priv->text_cell = renderer;
226         gtk_tree_view_column_pack_start (column, renderer, TRUE);
227         gtk_tree_view_column_add_attribute (column, renderer,
228                         "text", PRESETS_STORE_STATUS);
229         g_object_set (renderer,
230                         "editable", TRUE,
231                         NULL);
232         g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
233
234         g_signal_connect (renderer, "edited",
235                         G_CALLBACK (status_preset_dialog_status_edited), self);
236 }
237
238 static void
239 status_preset_dialog_preset_selection_changed (GtkTreeSelection *selection,
240                                                GtkWidget *remove_button)
241 {
242         /* update the sensitivity of the Remove button */
243         gtk_widget_set_sensitive (remove_button,
244                         gtk_tree_selection_count_selected_rows (selection) != 0);
245 }
246
247 static void
248 foreach_removed_status (GtkTreeModel *model,
249                         GtkTreePath *path,
250                         GtkTreeIter *iter,
251                         gpointer data)
252 {
253         TpConnectionPresenceType state;
254         char *status;
255
256         gtk_tree_model_get (model, iter,
257                         PRESETS_STORE_STATE, &state,
258                         PRESETS_STORE_STATUS, &status,
259                         -1);
260
261         DEBUG ("REMOVE PRESET (%i, %s)\n", state, status);
262         empathy_status_presets_remove (state, status);
263
264         g_free (status);
265 }
266
267 static void
268 status_preset_dialog_preset_remove (GtkButton *button,
269                                     EmpathyStatusPresetDialog *self)
270 {
271         EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
272         GtkTreeSelection *selection;
273
274         selection = gtk_tree_view_get_selection (
275                         GTK_TREE_VIEW (priv->presets_treeview));
276         gtk_tree_selection_selected_foreach (selection, foreach_removed_status, NULL);
277         status_preset_dialog_presets_update (self);
278 }
279
280 static void
281 empathy_status_preset_dialog_init (EmpathyStatusPresetDialog *self)
282 {
283         EmpathyStatusPresetDialogPriv *priv = self->priv =
284                 G_TYPE_INSTANCE_GET_PRIVATE (self,
285                         EMPATHY_TYPE_STATUS_PRESET_DIALOG,
286                         EmpathyStatusPresetDialogPriv);
287         GtkBuilder *gui;
288         GtkWidget *toplevel_vbox, *presets_sw, *remove_toolbar, *remove_button;
289         GtkTreeSelection *selection;
290         char *filename;
291         GtkStyleContext *context;
292
293         gtk_window_set_title (GTK_WINDOW (self),
294                         _("Edit Custom Messages"));
295         gtk_dialog_add_button (GTK_DIALOG (self),
296                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
297         gtk_window_set_resizable (GTK_WINDOW (self), FALSE);
298
299         filename = empathy_file_lookup ("empathy-status-preset-dialog.ui",
300                         "libempathy-gtk");
301         gui = empathy_builder_get_file (filename,
302                         "toplevel-vbox", &toplevel_vbox,
303                         "presets-sw", &presets_sw,
304                         "presets-treeview", &priv->presets_treeview,
305                         "remove-toolbar", &remove_toolbar,
306                         "remove-button", &remove_button,
307                         NULL);
308         g_free (filename);
309
310         /* join the remove toolbar to the treeview */
311         context = gtk_widget_get_style_context (presets_sw);
312         gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
313         context = gtk_widget_get_style_context (remove_toolbar);
314         gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
315
316         selection = gtk_tree_view_get_selection (
317                 GTK_TREE_VIEW (priv->presets_treeview));
318         g_signal_connect (selection,
319                         "changed",
320                         G_CALLBACK (status_preset_dialog_preset_selection_changed),
321                         remove_button);
322         gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
323
324         empathy_builder_connect (gui, self,
325                         "remove-button", "clicked", status_preset_dialog_preset_remove,
326                         NULL);
327
328         status_preset_dialog_setup_presets_treeview (self);
329
330         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))),
331             toplevel_vbox, TRUE, TRUE, 0);
332
333         g_object_unref (gui);
334 }
335
336 /**
337  * empathy_status_preset_dialog_new:
338  * @parent: the parent window of this dialog (or NULL)
339  *
340  * Creates a new #EmpathyStatusPresetDialog that allows the user to
341  * add/remove/edit their saved status messages.
342  *
343  * Returns: the newly constructed dialog.
344  */
345 GtkWidget *
346 empathy_status_preset_dialog_new (GtkWindow *parent)
347 {
348         GtkWidget *self = g_object_new (EMPATHY_TYPE_STATUS_PRESET_DIALOG,
349                         NULL);
350
351         if (parent) {
352                 gtk_window_set_transient_for (GTK_WINDOW (self), parent);
353         }
354
355         return self;
356 }