1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * empathy-status-preset-dialog.c
5 * EmpathyStatusPresetDialog - a dialog for adding and removing preset status
8 * Copyright (C) 2009 Collabora Ltd.
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.
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.
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
25 * Authors: Danielle Madeley <danielle.madeley@collabora.co.uk>
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
33 * #EmpathyStatusPresetDialog is a dialog allowing the user to add/remove/edit
34 * their saved status messages.
38 #include "empathy-status-preset-dialog.h"
40 #include <glib/gi18n-lib.h>
42 #include "empathy-status-presets.h"
43 #include "empathy-ui-utils.h"
44 #include "empathy-utils.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include "empathy-debug.h"
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusPresetDialog)
51 G_DEFINE_TYPE (EmpathyStatusPresetDialog, empathy_status_preset_dialog, GTK_TYPE_DIALOG);
53 static TpConnectionPresenceType states[] = {
54 TP_CONNECTION_PRESENCE_TYPE_AVAILABLE,
55 TP_CONNECTION_PRESENCE_TYPE_BUSY,
56 TP_CONNECTION_PRESENCE_TYPE_AWAY,
59 typedef struct _EmpathyStatusPresetDialogPriv EmpathyStatusPresetDialogPriv;
60 struct _EmpathyStatusPresetDialogPriv
62 /* block status_preset_dialog_add_combo_changed () when > 0 */
63 int block_add_combo_changed;
65 GtkWidget *presets_treeview;
66 GtkTreeViewColumn *column;
67 GtkCellRenderer *text_cell;
69 GtkTreeIter selected_iter;
70 gboolean add_combo_changed;
77 PRESETS_STORE_ICON_NAME,
87 ADD_COMBO_DEFAULT_TEXT,
92 empathy_status_preset_dialog_finalize (GObject *self)
94 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
96 g_free (priv->saved_status);
98 G_OBJECT_CLASS (empathy_status_preset_dialog_parent_class)->finalize (self);
102 empathy_status_preset_dialog_class_init (EmpathyStatusPresetDialogClass *class)
104 GObjectClass *gobject_class;
106 gobject_class = G_OBJECT_CLASS (class);
107 gobject_class->finalize = empathy_status_preset_dialog_finalize;
109 g_type_class_add_private (gobject_class,
110 sizeof (EmpathyStatusPresetDialogPriv));
114 status_preset_dialog_presets_update (EmpathyStatusPresetDialog *self)
116 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
120 store = GTK_LIST_STORE (gtk_tree_view_get_model (
121 GTK_TREE_VIEW (priv->presets_treeview)));
123 gtk_list_store_clear (store);
125 for (i = 0; i < G_N_ELEMENTS (states); i++) {
127 const char *icon_name;
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);
133 for (l = presets; l; l = l->next) {
134 char *preset = (char *) l->data;
136 gtk_list_store_insert_with_values (store,
138 PRESETS_STORE_STATE, states[i],
139 PRESETS_STORE_ICON_NAME, icon_name,
140 PRESETS_STORE_STATUS, preset,
144 g_list_free (presets);
149 status_preset_dialog_status_edited (GtkCellRendererText *renderer,
152 EmpathyStatusPresetDialog *self)
154 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
158 TpConnectionPresenceType state;
162 if (strlen (new_status) == 0) {
163 /* status is empty, ignore */
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);
174 gtk_tree_model_get (model, &iter,
175 PRESETS_STORE_STATE, &state,
176 PRESETS_STORE_STATUS, &old_status,
179 if (!strcmp (old_status, new_status)) {
180 /* statuses are the same */
185 DEBUG ("EDITED STATUS (%s) -> (%s)\n", old_status, new_status);
187 empathy_status_presets_remove (state, old_status);
188 empathy_status_presets_set_last (state, new_status);
192 status_preset_dialog_presets_update (self);
196 status_preset_dialog_setup_presets_treeview (EmpathyStatusPresetDialog *self)
198 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
199 GtkWidget *treeview = priv->presets_treeview;
201 GtkTreeViewColumn *column;
202 GtkCellRenderer *renderer;
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 */
209 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
210 GTK_TREE_MODEL (store));
211 g_object_unref (store);
213 status_preset_dialog_presets_update (self);
215 column = gtk_tree_view_column_new ();
216 priv->column = column;
217 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
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);
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,
232 g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
234 g_signal_connect (renderer, "edited",
235 G_CALLBACK (status_preset_dialog_status_edited), self);
239 status_preset_dialog_preset_selection_changed (GtkTreeSelection *selection,
240 GtkWidget *remove_button)
242 /* update the sensitivity of the Remove button */
243 gtk_widget_set_sensitive (remove_button,
244 gtk_tree_selection_count_selected_rows (selection) != 0);
248 foreach_removed_status (GtkTreeModel *model,
253 TpConnectionPresenceType state;
256 gtk_tree_model_get (model, iter,
257 PRESETS_STORE_STATE, &state,
258 PRESETS_STORE_STATUS, &status,
261 DEBUG ("REMOVE PRESET (%i, %s)\n", state, status);
262 empathy_status_presets_remove (state, status);
268 status_preset_dialog_preset_remove (GtkButton *button,
269 EmpathyStatusPresetDialog *self)
271 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
272 GtkTreeSelection *selection;
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);
281 empathy_status_preset_dialog_init (EmpathyStatusPresetDialog *self)
283 EmpathyStatusPresetDialogPriv *priv = self->priv =
284 G_TYPE_INSTANCE_GET_PRIVATE (self,
285 EMPATHY_TYPE_STATUS_PRESET_DIALOG,
286 EmpathyStatusPresetDialogPriv);
288 GtkWidget *toplevel_vbox, *presets_sw, *remove_toolbar, *remove_button;
289 GtkTreeSelection *selection;
291 GtkStyleContext *context;
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);
299 filename = empathy_file_lookup ("empathy-status-preset-dialog.ui",
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,
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);
316 selection = gtk_tree_view_get_selection (
317 GTK_TREE_VIEW (priv->presets_treeview));
318 g_signal_connect (selection,
320 G_CALLBACK (status_preset_dialog_preset_selection_changed),
322 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
324 empathy_builder_connect (gui, self,
325 "remove-button", "clicked", status_preset_dialog_preset_remove,
328 status_preset_dialog_setup_presets_treeview (self);
330 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))),
331 toplevel_vbox, TRUE, TRUE, 0);
333 g_object_unref (gui);
337 * empathy_status_preset_dialog_new:
338 * @parent: the parent window of this dialog (or NULL)
340 * Creates a new #EmpathyStatusPresetDialog that allows the user to
341 * add/remove/edit their saved status messages.
343 * Returns: the newly constructed dialog.
346 empathy_status_preset_dialog_new (GtkWindow *parent)
348 GtkWidget *self = g_object_new (EMPATHY_TYPE_STATUS_PRESET_DIALOG,
352 gtk_window_set_transient_for (GTK_WINDOW (self), parent);