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_setup_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 presets = empathy_status_presets_get (states[i], -1);
112 icon_name = empathy_icon_name_for_presence (states[i]);
114 for (l = presets; l; l = l->next) {
115 char *preset = (char *) l->data;
117 gtk_list_store_insert_with_values (store,
119 PRESETS_STORE_STATE, states[i],
120 PRESETS_STORE_ICON_NAME, icon_name,
121 PRESETS_STORE_STATUS, preset,
125 g_list_free (presets);
130 status_preset_add_combo_reset (EmpathyStatusPresetDialog *self)
132 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
134 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->add_combobox),
135 &priv->selected_iter);
139 status_preset_dialog_setup_add_combobox (EmpathyStatusPresetDialog *self)
141 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
142 GtkWidget *combobox = priv->add_combobox;
144 GtkCellRenderer *renderer;
147 store = gtk_list_store_new (ADD_COMBO_N_COLS,
148 MC_TYPE_PRESENCE, /* ADD_COMBO_STATE */
149 G_TYPE_STRING, /* ADD_COMBO_ICON_NAME */
150 G_TYPE_STRING, /* ADD_COMBO_STATUS */
151 G_TYPE_STRING); /* ADD_COMBO_DEFAULT_TEXT */
153 gtk_combo_box_set_model (GTK_COMBO_BOX (combobox),
154 GTK_TREE_MODEL (store));
155 g_object_unref (store);
157 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combobox),
158 ADD_COMBO_DEFAULT_TEXT);
160 for (i = 0; i < G_N_ELEMENTS (states); i++) {
161 gtk_list_store_insert_with_values (store, NULL, -1,
162 ADD_COMBO_STATE, states[i],
163 ADD_COMBO_ICON_NAME, empathy_icon_name_for_presence (states[i]),
164 ADD_COMBO_STATUS, empathy_presence_get_default_message (states[i]),
165 ADD_COMBO_DEFAULT_TEXT, "",
169 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
171 renderer = gtk_cell_renderer_pixbuf_new ();
172 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
173 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combobox), renderer,
174 "icon-name", ADD_COMBO_ICON_NAME);
176 renderer = gtk_cell_renderer_text_new ();
177 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
178 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combobox), renderer,
179 "text", ADD_COMBO_STATUS);
180 g_object_set (renderer,
181 "style", PANGO_STYLE_ITALIC,
182 "foreground", "Gray", /* FIXME - theme */
185 gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
189 status_preset_dialog_setup_presets_treeview (EmpathyStatusPresetDialog *self)
191 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
192 GtkWidget *treeview = priv->presets_treeview;
194 GtkTreeViewColumn *column;
195 GtkCellRenderer *renderer;
197 store = gtk_list_store_new (PRESETS_STORE_N_COLS,
198 MC_TYPE_PRESENCE, /* PRESETS_STORE_STATE */
199 G_TYPE_STRING, /* PRESETS_STORE_ICON_NAME */
200 G_TYPE_STRING); /* PRESETS_STORE_STATUS */
202 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
203 GTK_TREE_MODEL (store));
204 g_object_unref (store);
206 status_preset_dialog_setup_presets_update (self);
208 column = gtk_tree_view_column_new ();
209 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
211 renderer = gtk_cell_renderer_pixbuf_new ();
212 gtk_tree_view_column_pack_start (column, renderer, FALSE);
213 gtk_tree_view_column_add_attribute (column, renderer,
214 "icon-name", PRESETS_STORE_ICON_NAME);
216 renderer = gtk_cell_renderer_text_new ();
217 gtk_tree_view_column_pack_start (column, renderer, TRUE);
218 gtk_tree_view_column_add_attribute (column, renderer,
219 "text", PRESETS_STORE_STATUS);
223 status_preset_dialog_preset_selection_changed (GtkTreeSelection *selection,
224 GtkWidget *remove_button)
226 /* update the sensitivity of the Remove button */
227 gtk_widget_set_sensitive (remove_button,
228 gtk_tree_selection_get_selected (selection, NULL, NULL));
232 status_preset_dialog_preset_remove (GtkButton *button,
233 EmpathyStatusPresetDialog *self)
235 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
236 GtkTreeSelection *selection;
242 selection = gtk_tree_view_get_selection (
243 GTK_TREE_VIEW (priv->presets_treeview));
245 g_return_if_fail (gtk_tree_selection_get_selected (selection,
248 gtk_tree_model_get (model, &iter,
249 PRESETS_STORE_STATE, &state,
250 PRESETS_STORE_STATUS, &status,
253 DEBUG ("REMOVE PRESET (%i, %s)\n", state, status);
254 empathy_status_presets_remove (state, status);
258 status_preset_dialog_setup_presets_update (self);
262 status_preset_dialog_set_add_combo_changed (EmpathyStatusPresetDialog *self,
266 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
269 entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
271 priv->add_combo_changed = state;
272 gtk_widget_set_sensitive (priv->add_button, state);
275 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, NULL);
279 gdk_color_parse ("Gray", &colour); /* FIXME - theme */
280 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, &colour);
283 priv->block_add_combo_changed++;
284 gtk_entry_set_text (GTK_ENTRY (entry),
285 _("Enter Custom Message"));
286 priv->block_add_combo_changed--;
292 status_preset_dialog_add_combo_changed (GtkComboBox *combo,
293 EmpathyStatusPresetDialog *self)
295 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
300 if (priv->block_add_combo_changed) return;
302 model = gtk_combo_box_get_model (combo);
303 entry = gtk_bin_get_child (GTK_BIN (combo));
305 if (gtk_combo_box_get_active_iter (combo, &iter)) {
308 priv->selected_iter = iter;
309 gtk_tree_model_get (model, &iter,
310 PRESETS_STORE_ICON_NAME, &icon_name,
313 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
314 GTK_ENTRY_ICON_PRIMARY,
319 status_preset_dialog_set_add_combo_changed (self, FALSE, TRUE);
323 status = gtk_entry_get_text (GTK_ENTRY (entry));
324 status_preset_dialog_set_add_combo_changed (self,
325 strlen (status) > 0, FALSE);
330 status_preset_dialog_add_preset (GtkWidget *widget,
331 EmpathyStatusPresetDialog *self)
333 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
339 g_return_if_fail (priv->add_combo_changed);
341 model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->add_combobox));
342 entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
344 status = gtk_entry_get_text (GTK_ENTRY (entry));
345 gtk_tree_model_get (model, &priv->selected_iter,
346 PRESETS_STORE_STATE, &state,
349 DEBUG ("ADD PRESET (%i, %s)\n", state, status);
350 empathy_status_presets_set_last (state, status);
352 status_preset_dialog_setup_presets_update (self);
353 status_preset_add_combo_reset (self);
357 status_preset_dialog_add_combo_press_event (GtkWidget *widget,
358 GdkEventButton *event,
359 EmpathyStatusPresetDialog *self)
361 if (!GTK_WIDGET_HAS_FOCUS (widget)) {
362 /* if the widget isn't focused, focus it and select the text */
363 gtk_widget_grab_focus (widget);
364 gtk_editable_select_region (GTK_EDITABLE (widget), 0, -1);
373 status_preset_dialog_add_combo_focus_out (GtkWidget *widget,
374 GdkEventFocus *event,
375 EmpathyStatusPresetDialog *self)
377 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
380 gtk_editable_set_position (GTK_EDITABLE (widget), 0);
382 status = gtk_entry_get_text (GTK_ENTRY (widget));
383 status_preset_dialog_set_add_combo_changed (self,
384 priv->add_combo_changed && strlen (status) > 0,
391 empathy_status_preset_dialog_init (EmpathyStatusPresetDialog *self)
393 EmpathyStatusPresetDialogPriv *priv = self->priv =
394 G_TYPE_INSTANCE_GET_PRIVATE (self,
395 EMPATHY_TYPE_STATUS_PRESET_DIALOG,
396 EmpathyStatusPresetDialogPriv);
398 GtkWidget *toplevel_vbox, *remove_button, *entry;
401 gtk_window_set_title (GTK_WINDOW (self),
402 _("Edit Custom Messages"));
403 gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
404 gtk_dialog_add_button (GTK_DIALOG (self),
405 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
407 filename = empathy_file_lookup ("empathy-status-preset-dialog.ui",
409 gui = empathy_builder_get_file (filename,
410 "toplevel-vbox", &toplevel_vbox,
411 "presets-treeview", &priv->presets_treeview,
412 "remove-button", &remove_button,
413 "add-combobox", &priv->add_combobox,
414 "add-button", &priv->add_button,
418 g_signal_connect (gtk_tree_view_get_selection (
419 GTK_TREE_VIEW (priv->presets_treeview)),
421 G_CALLBACK (status_preset_dialog_preset_selection_changed),
424 entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
425 g_signal_connect (entry, "activate",
426 G_CALLBACK (status_preset_dialog_add_preset), self);
427 g_signal_connect (entry, "button-press-event",
428 G_CALLBACK (status_preset_dialog_add_combo_press_event),
430 g_signal_connect (entry, "focus-out-event",
431 G_CALLBACK (status_preset_dialog_add_combo_focus_out),
434 empathy_builder_connect (gui, self,
435 "remove-button", "clicked", status_preset_dialog_preset_remove,
436 "add-combobox", "changed", status_preset_dialog_add_combo_changed,
437 "add-button", "clicked", status_preset_dialog_add_preset,
440 status_preset_dialog_setup_presets_treeview (self);
441 status_preset_dialog_setup_add_combobox (self);
443 gtk_box_pack_start(GTK_BOX (GTK_DIALOG (self)->vbox), toplevel_vbox,
446 g_object_unref (gui);
450 empathy_status_preset_dialog_new (GtkWindow *parent)
452 GtkWidget *self = g_object_new (EMPATHY_TYPE_STATUS_PRESET_DIALOG,
456 gtk_window_set_transient_for (GTK_WINDOW (self), parent);