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., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
25 * Authors: Davyd Madeley <davyd.madeley@collabora.co.uk>
30 #include <glib/gi18n-lib.h>
33 #include <libmissioncontrol/mc-enum-types.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-status-presets.h>
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include <libempathy/empathy-debug.h>
41 #include "empathy-ui-utils.h"
42 #include "empathy-status-preset-dialog.h"
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStatusPresetDialog)
46 G_DEFINE_TYPE (EmpathyStatusPresetDialog, empathy_status_preset_dialog, GTK_TYPE_DIALOG);
48 static McPresence states[] = {
49 MC_PRESENCE_AVAILABLE,
50 MC_PRESENCE_DO_NOT_DISTURB,
54 typedef struct _EmpathyStatusPresetDialogPriv EmpathyStatusPresetDialogPriv;
55 struct _EmpathyStatusPresetDialogPriv
57 int block_add_combo_changed;
59 GtkWidget *presets_treeview;
60 GtkWidget *add_combobox;
61 GtkWidget *add_button;
63 GtkTreeIter selected_iter;
64 gboolean add_combo_changed;
70 PRESETS_STORE_ICON_NAME,
80 ADD_COMBO_DEFAULT_TEXT,
85 empathy_status_preset_dialog_class_init (EmpathyStatusPresetDialogClass *class)
87 GObjectClass *gobject_class;
89 gobject_class = G_OBJECT_CLASS (class);
91 g_type_class_add_private (gobject_class,
92 sizeof (EmpathyStatusPresetDialogPriv));
96 status_preset_dialog_presets_update (EmpathyStatusPresetDialog *self)
98 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
102 store = GTK_LIST_STORE (gtk_tree_view_get_model (
103 GTK_TREE_VIEW (priv->presets_treeview)));
105 gtk_list_store_clear (store);
107 for (i = 0; i < G_N_ELEMENTS (states); i++) {
109 const char *icon_name;
111 icon_name = empathy_icon_name_for_presence (states[i]);
112 presets = empathy_status_presets_get (states[i], -1);
113 presets = g_list_sort (presets, (GCompareFunc) g_utf8_collate);
115 for (l = presets; l; l = l->next) {
116 char *preset = (char *) l->data;
118 gtk_list_store_insert_with_values (store,
120 PRESETS_STORE_STATE, states[i],
121 PRESETS_STORE_ICON_NAME, icon_name,
122 PRESETS_STORE_STATUS, preset,
126 g_list_free (presets);
131 status_preset_add_combo_reset (EmpathyStatusPresetDialog *self)
133 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
135 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->add_combobox),
136 &priv->selected_iter);
140 status_preset_dialog_setup_add_combobox (EmpathyStatusPresetDialog *self)
142 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
143 GtkWidget *combobox = priv->add_combobox;
145 GtkCellRenderer *renderer;
148 store = gtk_list_store_new (ADD_COMBO_N_COLS,
149 MC_TYPE_PRESENCE, /* ADD_COMBO_STATE */
150 G_TYPE_STRING, /* ADD_COMBO_ICON_NAME */
151 G_TYPE_STRING, /* ADD_COMBO_STATUS */
152 G_TYPE_STRING); /* ADD_COMBO_DEFAULT_TEXT */
154 gtk_combo_box_set_model (GTK_COMBO_BOX (combobox),
155 GTK_TREE_MODEL (store));
156 g_object_unref (store);
158 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combobox),
159 ADD_COMBO_DEFAULT_TEXT);
161 for (i = 0; i < G_N_ELEMENTS (states); i++) {
162 gtk_list_store_insert_with_values (store, NULL, -1,
163 ADD_COMBO_STATE, states[i],
164 ADD_COMBO_ICON_NAME, empathy_icon_name_for_presence (states[i]),
165 ADD_COMBO_STATUS, empathy_presence_get_default_message (states[i]),
166 ADD_COMBO_DEFAULT_TEXT, "",
170 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
172 renderer = gtk_cell_renderer_pixbuf_new ();
173 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
174 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combobox), renderer,
175 "icon-name", ADD_COMBO_ICON_NAME);
177 renderer = gtk_cell_renderer_text_new ();
178 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
179 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combobox), renderer,
180 "text", ADD_COMBO_STATUS);
181 g_object_set (renderer,
182 "style", PANGO_STYLE_ITALIC,
183 "foreground", "Gray", /* FIXME - theme */
186 gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
190 status_preset_dialog_status_edited (GtkCellRendererText *renderer,
193 EmpathyStatusPresetDialog *self)
195 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
203 if (strlen (new_status) == 0) {
204 /* status is empty, ignore */
208 model = gtk_tree_view_get_model (GTK_TREE_VIEW (priv->presets_treeview));
209 path = gtk_tree_path_new_from_string (path_str);
210 valid = gtk_tree_model_get_iter (model, &iter, path);
211 gtk_tree_path_free (path);
215 gtk_tree_model_get (model, &iter,
216 PRESETS_STORE_STATE, &state,
217 PRESETS_STORE_STATUS, &old_status,
220 if (!strcmp (old_status, new_status)) {
221 /* statuses are the same */
226 DEBUG ("EDITED STATUS (%s) -> (%s)\n", old_status, new_status);
228 empathy_status_presets_remove (state, old_status);
229 empathy_status_presets_set_last (state, new_status);
233 status_preset_dialog_presets_update (self);
237 status_preset_dialog_setup_presets_treeview (EmpathyStatusPresetDialog *self)
239 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
240 GtkWidget *treeview = priv->presets_treeview;
242 GtkTreeViewColumn *column;
243 GtkCellRenderer *renderer;
245 store = gtk_list_store_new (PRESETS_STORE_N_COLS,
246 MC_TYPE_PRESENCE, /* PRESETS_STORE_STATE */
247 G_TYPE_STRING, /* PRESETS_STORE_ICON_NAME */
248 G_TYPE_STRING); /* PRESETS_STORE_STATUS */
250 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
251 GTK_TREE_MODEL (store));
252 g_object_unref (store);
254 status_preset_dialog_presets_update (self);
256 column = gtk_tree_view_column_new ();
257 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
259 renderer = gtk_cell_renderer_pixbuf_new ();
260 gtk_tree_view_column_pack_start (column, renderer, FALSE);
261 gtk_tree_view_column_add_attribute (column, renderer,
262 "icon-name", PRESETS_STORE_ICON_NAME);
264 renderer = gtk_cell_renderer_text_new ();
265 gtk_tree_view_column_pack_start (column, renderer, TRUE);
266 gtk_tree_view_column_add_attribute (column, renderer,
267 "text", PRESETS_STORE_STATUS);
268 g_object_set (renderer,
271 g_signal_connect (renderer, "edited",
272 G_CALLBACK (status_preset_dialog_status_edited), self);
276 status_preset_dialog_preset_selection_changed (GtkTreeSelection *selection,
277 GtkWidget *remove_button)
279 /* update the sensitivity of the Remove button */
280 gtk_widget_set_sensitive (remove_button,
281 gtk_tree_selection_get_selected (selection, NULL, NULL));
285 status_preset_dialog_preset_remove (GtkButton *button,
286 EmpathyStatusPresetDialog *self)
288 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
289 GtkTreeSelection *selection;
295 selection = gtk_tree_view_get_selection (
296 GTK_TREE_VIEW (priv->presets_treeview));
298 g_return_if_fail (gtk_tree_selection_get_selected (selection,
301 gtk_tree_model_get (model, &iter,
302 PRESETS_STORE_STATE, &state,
303 PRESETS_STORE_STATUS, &status,
306 DEBUG ("REMOVE PRESET (%i, %s)\n", state, status);
307 empathy_status_presets_remove (state, status);
311 status_preset_dialog_presets_update (self);
315 status_preset_dialog_set_add_combo_changed (EmpathyStatusPresetDialog *self,
319 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
322 entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
324 priv->add_combo_changed = state;
325 gtk_widget_set_sensitive (priv->add_button, state);
328 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, NULL);
332 gdk_color_parse ("Gray", &colour); /* FIXME - theme */
333 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, &colour);
336 priv->block_add_combo_changed++;
337 gtk_entry_set_text (GTK_ENTRY (entry),
338 _("Enter Custom Message"));
339 priv->block_add_combo_changed--;
345 status_preset_dialog_add_combo_changed (GtkComboBox *combo,
346 EmpathyStatusPresetDialog *self)
348 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
353 if (priv->block_add_combo_changed) return;
355 model = gtk_combo_box_get_model (combo);
356 entry = gtk_bin_get_child (GTK_BIN (combo));
358 if (gtk_combo_box_get_active_iter (combo, &iter)) {
361 priv->selected_iter = iter;
362 gtk_tree_model_get (model, &iter,
363 PRESETS_STORE_ICON_NAME, &icon_name,
366 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
367 GTK_ENTRY_ICON_PRIMARY,
372 status_preset_dialog_set_add_combo_changed (self, FALSE, TRUE);
376 status = gtk_entry_get_text (GTK_ENTRY (entry));
377 status_preset_dialog_set_add_combo_changed (self,
378 strlen (status) > 0, FALSE);
383 status_preset_dialog_add_preset (GtkWidget *widget,
384 EmpathyStatusPresetDialog *self)
386 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
392 g_return_if_fail (priv->add_combo_changed);
394 model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->add_combobox));
395 entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
397 status = gtk_entry_get_text (GTK_ENTRY (entry));
398 gtk_tree_model_get (model, &priv->selected_iter,
399 PRESETS_STORE_STATE, &state,
402 DEBUG ("ADD PRESET (%i, %s)\n", state, status);
403 empathy_status_presets_set_last (state, status);
405 status_preset_dialog_presets_update (self);
406 status_preset_add_combo_reset (self);
410 status_preset_dialog_add_combo_press_event (GtkWidget *widget,
411 GdkEventButton *event,
412 EmpathyStatusPresetDialog *self)
414 if (!GTK_WIDGET_HAS_FOCUS (widget)) {
415 /* if the widget isn't focused, focus it and select the text */
416 gtk_widget_grab_focus (widget);
417 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
426 status_preset_dialog_add_combo_focus_out (GtkWidget *widget,
427 GdkEventFocus *event,
428 EmpathyStatusPresetDialog *self)
430 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
433 gtk_editable_set_position (GTK_EDITABLE (widget), 0);
435 status = gtk_entry_get_text (GTK_ENTRY (widget));
436 status_preset_dialog_set_add_combo_changed (self,
437 priv->add_combo_changed && strlen (status) > 0,
444 empathy_status_preset_dialog_init (EmpathyStatusPresetDialog *self)
446 EmpathyStatusPresetDialogPriv *priv = self->priv =
447 G_TYPE_INSTANCE_GET_PRIVATE (self,
448 EMPATHY_TYPE_STATUS_PRESET_DIALOG,
449 EmpathyStatusPresetDialogPriv);
451 GtkWidget *toplevel_vbox, *remove_button, *entry;
454 gtk_window_set_title (GTK_WINDOW (self),
455 _("Edit Custom Messages"));
456 gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
457 gtk_dialog_add_button (GTK_DIALOG (self),
458 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
460 filename = empathy_file_lookup ("empathy-status-preset-dialog.ui",
462 gui = empathy_builder_get_file (filename,
463 "toplevel-vbox", &toplevel_vbox,
464 "presets-treeview", &priv->presets_treeview,
465 "remove-button", &remove_button,
466 "add-combobox", &priv->add_combobox,
467 "add-button", &priv->add_button,
471 g_signal_connect (gtk_tree_view_get_selection (
472 GTK_TREE_VIEW (priv->presets_treeview)),
474 G_CALLBACK (status_preset_dialog_preset_selection_changed),
477 entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
478 g_signal_connect (entry, "activate",
479 G_CALLBACK (status_preset_dialog_add_preset), self);
480 g_signal_connect (entry, "button-press-event",
481 G_CALLBACK (status_preset_dialog_add_combo_press_event),
483 g_signal_connect (entry, "focus-out-event",
484 G_CALLBACK (status_preset_dialog_add_combo_focus_out),
487 empathy_builder_connect (gui, self,
488 "remove-button", "clicked", status_preset_dialog_preset_remove,
489 "add-combobox", "changed", status_preset_dialog_add_combo_changed,
490 "add-button", "clicked", status_preset_dialog_add_preset,
493 status_preset_dialog_setup_presets_treeview (self);
494 status_preset_dialog_setup_add_combobox (self);
496 gtk_box_pack_start(GTK_BOX (GTK_DIALOG (self)->vbox), toplevel_vbox,
499 g_object_unref (gui);
503 * empathy_status_preset_dialog_new:
504 * @parent: the parent window of this dialog (or NULL)
506 * Creates a new #EmpathyStatusPresetDialog that allows the user to
507 * add/remove/edit their saved status messages.
509 * Returns: the newly constructed dialog.
512 empathy_status_preset_dialog_new (GtkWindow *parent)
514 GtkWidget *self = g_object_new (EMPATHY_TYPE_STATUS_PRESET_DIALOG,
518 gtk_window_set_transient_for (GTK_WINDOW (self), parent);