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 GtkWidget *presets_treeview;
58 GtkWidget *add_combobox;
59 GtkWidget *add_button;
61 McPresence selected_state;
62 gboolean add_combo_changed;
68 PRESETS_STORE_ICON_NAME,
78 ADD_COMBO_DEFAULT_TEXT,
83 empathy_status_preset_dialog_class_init (EmpathyStatusPresetDialogClass *class)
85 GObjectClass *gobject_class;
87 gobject_class = G_OBJECT_CLASS (class);
89 g_type_class_add_private (gobject_class,
90 sizeof (EmpathyStatusPresetDialogPriv));
94 status_preset_dialog_setup_presets_update (EmpathyStatusPresetDialog *self)
96 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
100 store = GTK_LIST_STORE (gtk_tree_view_get_model (
101 GTK_TREE_VIEW (priv->presets_treeview)));
103 gtk_list_store_clear (store);
105 for (i = 0; i < G_N_ELEMENTS (states); i++) {
107 const char *icon_name;
109 presets = empathy_status_presets_get (states[i], -1);
110 icon_name = empathy_icon_name_for_presence (states[i]);
112 for (l = presets; l; l = l->next) {
113 char *preset = (char *) l->data;
115 gtk_list_store_insert_with_values (store,
117 PRESETS_STORE_STATE, states[i],
118 PRESETS_STORE_ICON_NAME, icon_name,
119 PRESETS_STORE_STATUS, preset,
123 g_list_free (presets);
128 status_preset_add_combo_reset (EmpathyStatusPresetDialog *self)
130 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
132 gtk_combo_box_set_active (GTK_COMBO_BOX (priv->add_combobox), 0);
136 status_preset_dialog_setup_add_combobox (EmpathyStatusPresetDialog *self)
138 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
139 GtkWidget *combobox = priv->add_combobox;
141 GtkCellRenderer *renderer;
144 store = gtk_list_store_new (ADD_COMBO_N_COLS,
145 MC_TYPE_PRESENCE, /* ADD_COMBO_STATE */
146 G_TYPE_STRING, /* ADD_COMBO_ICON_NAME */
147 G_TYPE_STRING, /* ADD_COMBO_STATUS */
148 G_TYPE_STRING); /* ADD_COMBO_DEFAULT_TEXT */
150 gtk_combo_box_set_model (GTK_COMBO_BOX (combobox),
151 GTK_TREE_MODEL (store));
152 g_object_unref (store);
154 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combobox),
155 ADD_COMBO_DEFAULT_TEXT);
157 for (i = 0; i < G_N_ELEMENTS (states); i++) {
158 gtk_list_store_insert_with_values (store, NULL, -1,
159 ADD_COMBO_STATE, states[i],
160 ADD_COMBO_ICON_NAME, empathy_icon_name_for_presence (states[i]),
161 ADD_COMBO_STATUS, empathy_presence_get_default_message (states[i]),
162 ADD_COMBO_DEFAULT_TEXT, _("Enter Custom Message"),
166 gtk_cell_layout_clear (GTK_CELL_LAYOUT (combobox));
168 renderer = gtk_cell_renderer_pixbuf_new ();
169 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE);
170 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combobox), renderer,
171 "icon-name", ADD_COMBO_ICON_NAME);
173 renderer = gtk_cell_renderer_text_new ();
174 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE);
175 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combobox), renderer,
176 "text", ADD_COMBO_STATUS);
177 g_object_set (renderer,
178 "style", PANGO_STYLE_ITALIC,
179 "foreground", "Gray", /* FIXME - theme */
182 status_preset_add_combo_reset (self);
186 status_preset_dialog_setup_presets_treeview (EmpathyStatusPresetDialog *self)
188 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
189 GtkWidget *treeview = priv->presets_treeview;
191 GtkTreeViewColumn *column;
192 GtkCellRenderer *renderer;
194 store = gtk_list_store_new (PRESETS_STORE_N_COLS,
195 MC_TYPE_PRESENCE, /* PRESETS_STORE_STATE */
196 G_TYPE_STRING, /* PRESETS_STORE_ICON_NAME */
197 G_TYPE_STRING); /* PRESETS_STORE_STATUS */
199 gtk_tree_view_set_model (GTK_TREE_VIEW (treeview),
200 GTK_TREE_MODEL (store));
201 g_object_unref (store);
203 status_preset_dialog_setup_presets_update (self);
205 column = gtk_tree_view_column_new ();
206 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
208 renderer = gtk_cell_renderer_pixbuf_new ();
209 gtk_tree_view_column_pack_start (column, renderer, FALSE);
210 gtk_tree_view_column_add_attribute (column, renderer,
211 "icon-name", PRESETS_STORE_ICON_NAME);
213 renderer = gtk_cell_renderer_text_new ();
214 gtk_tree_view_column_pack_start (column, renderer, TRUE);
215 gtk_tree_view_column_add_attribute (column, renderer,
216 "text", PRESETS_STORE_STATUS);
220 status_preset_dialog_preset_selection_changed (GtkTreeSelection *selection,
221 GtkWidget *remove_button)
223 /* update the sensitivity of the Remove button */
224 gtk_widget_set_sensitive (remove_button,
225 gtk_tree_selection_get_selected (selection, NULL, NULL));
229 status_preset_dialog_preset_remove (GtkButton *button,
230 EmpathyStatusPresetDialog *self)
232 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
233 GtkTreeSelection *selection;
239 selection = gtk_tree_view_get_selection (
240 GTK_TREE_VIEW (priv->presets_treeview));
242 g_return_if_fail (gtk_tree_selection_get_selected (selection,
245 gtk_tree_model_get (model, &iter,
246 PRESETS_STORE_STATE, &state,
247 PRESETS_STORE_STATUS, &status,
250 DEBUG ("REMOVE PRESET (%i, %s)\n", state, status);
251 empathy_status_presets_remove (state, status);
255 status_preset_dialog_setup_presets_update (self);
259 status_preset_dialog_add_combo_changed (GtkComboBox *combo,
260 EmpathyStatusPresetDialog *self)
262 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
267 model = gtk_combo_box_get_model (combo);
268 entry = gtk_bin_get_child (GTK_BIN (combo));
270 if (gtk_combo_box_get_active_iter (combo, &iter)) {
274 gtk_tree_model_get (model, &iter,
275 PRESETS_STORE_STATE, &priv->selected_state,
276 PRESETS_STORE_ICON_NAME, &icon_name,
279 gtk_entry_set_icon_from_icon_name (GTK_ENTRY (entry),
280 GTK_ENTRY_ICON_PRIMARY,
285 gtk_widget_grab_focus (entry);
286 gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
288 priv->add_combo_changed = FALSE;
289 gtk_widget_set_sensitive (priv->add_button, FALSE);
291 gdk_color_parse ("Gray", &colour); /* FIXME - theme */
292 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, &colour);
294 priv->add_combo_changed = TRUE;
295 gtk_widget_set_sensitive (priv->add_button, TRUE);
296 gtk_widget_modify_text (entry, GTK_STATE_NORMAL, NULL);
301 status_preset_dialog_add_preset (GtkWidget *widget,
302 EmpathyStatusPresetDialog *self)
304 EmpathyStatusPresetDialogPriv *priv = GET_PRIV (self);
308 g_return_if_fail (priv->add_combo_changed);
310 entry = gtk_bin_get_child (GTK_BIN (priv->add_combobox));
311 status = gtk_entry_get_text (GTK_ENTRY (entry));
313 DEBUG ("ADD PRESET (%i, %s)\n", priv->selected_state, status);
314 empathy_status_presets_set_last (priv->selected_state, status);
316 status_preset_dialog_setup_presets_update (self);
317 status_preset_add_combo_reset (self);
321 empathy_status_preset_dialog_init (EmpathyStatusPresetDialog *self)
323 EmpathyStatusPresetDialogPriv *priv = self->priv =
324 G_TYPE_INSTANCE_GET_PRIVATE (self,
325 EMPATHY_TYPE_STATUS_PRESET_DIALOG,
326 EmpathyStatusPresetDialogPriv);
328 GtkWidget *toplevel_vbox, *remove_button;
331 gtk_window_set_title (GTK_WINDOW (self),
332 _("Edit Custom Messages"));
333 gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
334 gtk_dialog_add_button (GTK_DIALOG (self),
335 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
337 filename = empathy_file_lookup ("empathy-status-preset-dialog.ui",
339 gui = empathy_builder_get_file (filename,
340 "toplevel-vbox", &toplevel_vbox,
341 "presets-treeview", &priv->presets_treeview,
342 "remove-button", &remove_button,
343 "add-combobox", &priv->add_combobox,
344 "add-button", &priv->add_button,
348 g_signal_connect (gtk_tree_view_get_selection (
349 GTK_TREE_VIEW (priv->presets_treeview)),
351 G_CALLBACK (status_preset_dialog_preset_selection_changed),
354 g_signal_connect (gtk_bin_get_child (GTK_BIN (priv->add_combobox)),
356 G_CALLBACK (status_preset_dialog_add_preset),
359 empathy_builder_connect (gui, self,
360 "remove-button", "clicked", status_preset_dialog_preset_remove,
361 "add-combobox", "changed", status_preset_dialog_add_combo_changed,
362 "add-button", "clicked", status_preset_dialog_add_preset,
365 status_preset_dialog_setup_presets_treeview (self);
366 status_preset_dialog_setup_add_combobox (self);
368 gtk_box_pack_start(GTK_BOX (GTK_DIALOG (self)->vbox), toplevel_vbox,
371 g_object_unref (gui);
375 empathy_status_preset_dialog_new (GtkWindow *parent)
377 GtkWidget *self = g_object_new (EMPATHY_TYPE_STATUS_PRESET_DIALOG,
381 gtk_window_set_transient_for (GTK_WINDOW (self), parent);