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