From 95057c5edbcd953c34e643e8fd5b5b8d86f18a04 Mon Sep 17 00:00:00 2001 From: Rodrigo Moya Date: Fri, 8 Oct 2010 11:57:10 +0200 Subject: [PATCH] Add CcSettingEditor widget --- libgnome-control-center/Makefile.am | 11 + libgnome-control-center/cc-setting-editor.c | 213 ++++++++++++++++++ libgnome-control-center/cc-setting-editor.h | 75 ++++++ libgnome-control-center/test-setting-editor.c | 48 ++++ 4 files changed, 347 insertions(+) create mode 100644 libgnome-control-center/cc-setting-editor.c create mode 100644 libgnome-control-center/cc-setting-editor.h create mode 100644 libgnome-control-center/test-setting-editor.c diff --git a/libgnome-control-center/Makefile.am b/libgnome-control-center/Makefile.am index 71719cf68..17af87082 100644 --- a/libgnome-control-center/Makefile.am +++ b/libgnome-control-center/Makefile.am @@ -10,6 +10,7 @@ lib_LTLIBRARIES = libgnome-control-center.la libgnome_control_center_include_HEADERS = \ cc-panel.h \ + cc-setting-editor.h \ cc-shell.h \ gconf-property-editor.h \ gconf-property-editor-marshal.h \ @@ -18,6 +19,8 @@ libgnome_control_center_include_HEADERS = \ libgnome_control_center_la_SOURCES = \ cc-panel.c \ cc-panel.h \ + cc-setting-editor.c \ + cc-setting-editor.h \ cc-shell.c \ cc-shell.h \ gconf-property-editor-marshal.c \ @@ -39,6 +42,14 @@ libgnome_control_center_la_LIBTOOLFLAGS = --tag=disable-static libgnome_control_center_includedir = $(includedir)/gnome-control-center-1/libgnome-control-center +noinst_PROGRAMS = test-setting-editor + +test_setting_editor_SOURCES = test-setting-editor.c +test_setting_editor_LDADD = \ + $(CAPPLET_LIBS) \ + $(top_builddir)/libgnome-control-center/libgnome-control-center.la \ + $(NULL) + pkgconfigdir=$(libdir)/pkgconfig pkgconfig_DATA=libgnome-control-center.pc diff --git a/libgnome-control-center/cc-setting-editor.c b/libgnome-control-center/cc-setting-editor.c new file mode 100644 index 000000000..c427c0bc1 --- /dev/null +++ b/libgnome-control-center/cc-setting-editor.c @@ -0,0 +1,213 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2010 Rodrigo Moya + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Authors: Rodrigo Moya + */ + +#include "cc-setting-editor.h" + +enum { + PIXBUF_COL, + TEXT_COL, + APP_INFO_COL, + N_COLUMNS +}; + +typedef enum { + SETTING_TYPE_GSETTINGS, + SETTING_TYPE_APPLICATION +} SettingType; + +struct _CcSettingEditorPrivate +{ + SettingType s_type; + + GSettings *settings; + gchar *settings_prefix; + gchar *key; + GtkWidget *ui_control; +}; + +G_DEFINE_TYPE(CcSettingEditor, cc_setting_editor, G_TYPE_OBJECT) + +static void +cc_setting_editor_finalize (GObject *object) +{ + CcSettingEditor *seditor = CC_SETTING_EDITOR (object); + + if (seditor->priv != NULL) { + if (seditor->priv->settings != NULL) + g_object_unref (seditor->priv->settings); + + if (seditor->priv->settings_prefix != NULL) + g_free (seditor->priv->settings_prefix); + + if (seditor->priv->key != NULL) + g_free (seditor->priv->key); + + g_free (seditor->priv); + } + + G_OBJECT_CLASS (cc_setting_editor_parent_class)->finalize (object); +} + +static void +cc_setting_editor_class_init (CcSettingEditorClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = cc_setting_editor_finalize; +} + +static void +cc_setting_editor_init (CcSettingEditor *seditor) +{ + seditor->priv = g_new0 (CcSettingEditorPrivate, 1); +} + +static CcSettingEditor * +cc_setting_editor_new (SettingType s_type, + const gchar *settings_prefix, + const gchar *key, + GtkWidget *ui_control) +{ + CcSettingEditor *seditor; + + seditor = g_object_new (CC_TYPE_SETTING_EDITOR, NULL); + seditor->priv->s_type = s_type; + seditor->priv->key = g_strdup (key); + seditor->priv->ui_control = ui_control; + + if (settings_prefix != NULL) { + seditor->priv->settings = g_settings_new (settings_prefix); + seditor->priv->settings_prefix = g_strdup (settings_prefix); + } + + return seditor; +} + +static void +application_selection_changed_cb (GtkComboBox *combobox, CcSettingEditor *seditor) +{ + GtkTreeIter selected_row; + + if (gtk_combo_box_get_active_iter (combobox, &selected_row)) { + GAppInfo *app_info; + GError *error = NULL; + + gtk_tree_model_get (gtk_combo_box_get_model (combobox), &selected_row, + APP_INFO_COL, &app_info, + -1); + if (!g_app_info_set_as_default_for_type (app_info, seditor->priv->key, &error)) { + g_warning ("Could not set %s as default app for %s: %s", + g_app_info_get_display_name (app_info), + seditor->priv->key, + error->message); + } + } +} + +/** + * cc_setting_editor_new_application: + * @mime_type: The MIME type to configure application for. + * @combobox: The combo box widget used to display the list of applications for the given type. + * + * Create a new #CCSettingEditor object to configure the default application + * for the given MIME type. + * + * Return value: A newly created #CCSettingEditor object. + */ +GObject * +cc_setting_editor_new_application (const gchar *mime_type, GtkWidget *combobox) +{ + CcSettingEditor *seditor; + GList *app_list; + GtkListStore *model; + GtkCellRenderer *renderer; + GAppInfo *default_app; + + seditor = cc_setting_editor_new (SETTING_TYPE_APPLICATION, + NULL, + mime_type, + combobox); + + model = gtk_list_store_new (3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_POINTER); + + renderer = gtk_cell_renderer_pixbuf_new (); + /* not all cells have a pixbuf, this prevents the combo box to shrink */ + gtk_cell_renderer_set_fixed_size (renderer, -1, 22); + gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, FALSE); + gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, + "pixbuf", PIXBUF_COL, + NULL); + + renderer = gtk_cell_renderer_text_new (); + gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combobox), renderer, TRUE); + gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, + "text", TEXT_COL, + NULL); + + /* Retrieve list of applications for the given MIME type */ + default_app = g_app_info_get_default_for_type (mime_type, FALSE); + + app_list = g_app_info_get_all_for_type (mime_type); + while (app_list != NULL) { + GtkTreeIter new_row; + GAppInfo *app_info = (GAppInfo *) app_list->data; + + g_debug ("Adding application %s", g_app_info_get_display_name (app_info)); + + gtk_list_store_append (model, &new_row); + gtk_list_store_set (model, &new_row, + PIXBUF_COL, /* FIXME */ NULL, + TEXT_COL, g_app_info_get_display_name (app_info), + APP_INFO_COL, app_info, + -1); + + /* If it's the default one, select it */ + if (g_str_equal (g_app_info_get_name (default_app), + g_app_info_get_name (app_info))) { + gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &new_row); + } + + app_list = g_list_remove (app_list, app_info); + } + + gtk_combo_box_set_model (GTK_COMBO_BOX (combobox), GTK_TREE_MODEL (model)); + + g_signal_connect (combobox, "changed", + G_CALLBACK (application_selection_changed_cb), seditor); + + return (GObject *) seditor; +} + +/** + * cc_setting_editor_get_ui_control: + * @seditor: a #CCSettingEditor object. + * + * Retrieve the UI control associated with the given #CCSettingEditor object. + * + * Return value: The UI control associated with the given #CCSettingEditor. + */ +GtkWidget * +cc_setting_editor_get_ui_control (CcSettingEditor *seditor) +{ + g_return_val_if_fail (CC_IS_SETTING_EDITOR (seditor), NULL); + + return seditor->priv->ui_control; +} diff --git a/libgnome-control-center/cc-setting-editor.h b/libgnome-control-center/cc-setting-editor.h new file mode 100644 index 000000000..9e08151c1 --- /dev/null +++ b/libgnome-control-center/cc-setting-editor.h @@ -0,0 +1,75 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2010 Rodrigo Moya + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Authors: Rodrigo Moya + */ + +#ifndef __CC_SETTING_EDITOR_H +#define __CC_SETTING_EDITOR_H + +#include +#include +#include + +G_BEGIN_DECLS + +#define CC_TYPE_SETTING_EDITOR (cc_setting_editor_get_type ()) +#define CC_SETTING_EDITOR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), CC_TYPE_SETTING_EDITOR, CcSettingEditor)) +#define CC_SETTING_EDITOR_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), CC_TYPE_SETTING_EDITOR, CcSettingEditorClass)) +#define CC_IS_SETTING_EDITOR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), CC_TYPE_SETTING_EDITOR)) +#define CC_IS_SETTING_EDITOR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), CC_TYPE_SETTING_EDITOR)) +#define CC_SETTING_EDITOR_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), CC_TYPE_SETTING_EDITOR, CcSettingEditorClass)) + +typedef struct _CcSettingEditorPrivate CcSettingEditorPrivate; +typedef struct _CcSettingEditor CcSettingEditor; +typedef struct _CcSettingEditorClass CcSettingEditorClass; + +/** + * CcSettingEditor: + * + * The contents of this struct are private and should not be accessed directly. + */ +struct _CcSettingEditor +{ + /*< private >*/ + GObject parent; + CcSettingEditorPrivate *priv; +}; + +/** + * CcSettingEditorClass: + * + * The contents of this struct are private and should not be accessed directly. + */ +struct _CcSettingEditorClass +{ + /*< private >*/ + GObjectClass parent_class; + + void (* value_changed) (CcSettingEditor *seditor, const gchar *key, GVariant *value); +}; + +GType cc_setting_editor_get_type (void); + +GObject *cc_setting_editor_new_application (const gchar *mime_type, GtkWidget *combobox); + +GtkWidget *cc_setting_editor_get_ui_control (CcSettingEditor *seditor); + +G_END_DECLS + +#endif diff --git a/libgnome-control-center/test-setting-editor.c b/libgnome-control-center/test-setting-editor.c new file mode 100644 index 000000000..6620f1b90 --- /dev/null +++ b/libgnome-control-center/test-setting-editor.c @@ -0,0 +1,48 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2010 Rodrigo Moya + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Authors: Rodrigo Moya + */ + +#include "cc-setting-editor.h" + +int +main (int argc, char *argv[]) +{ + GtkWidget *dialog, *w; + GObject *seditor; + + gtk_init (&argc, &argv); + + dialog = gtk_dialog_new_with_buttons ("Test Setting Editor", NULL, 0, + GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, + NULL); + g_signal_connect (dialog, "response", G_CALLBACK (gtk_main_quit), NULL); + + /* Create a default application selector */ + w = gtk_combo_box_new (); + //seditor = cc_setting_editor_new_application ("x-scheme-handler/http", w); + seditor = cc_setting_editor_new_application ("text/plain", w); + gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), w, TRUE, FALSE, 3); + + /* Run the dialog */ + gtk_widget_show_all (dialog); + gtk_main (); + + return 0; +}