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