[gnome-software/wip/rancell/permissions] Add a GsPermissionComboBox



commit 8cca656a7620d3b3ed844ff72cc94af09c022d12
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Aug 15 16:57:26 2017 +1200

    Add a GsPermissionComboBox

 src/gs-permission-combo-box.c |  125 +++++++++++++++++++++++++++++++++++++++++
 src/gs-permission-combo-box.h |   41 +++++++++++++
 src/gs-permission-dialog.c    |   29 ++++++++--
 src/gs-permission-switch.h    |    4 +-
 src/meson.build               |    1 +
 5 files changed, 192 insertions(+), 8 deletions(-)
---
diff --git a/src/gs-permission-combo-box.c b/src/gs-permission-combo-box.c
new file mode 100644
index 0000000..f79e6a2
--- /dev/null
+++ b/src/gs-permission-combo-box.c
@@ -0,0 +1,125 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 Canonical Ltd.
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "gs-permission-combo-box.h"
+
+struct _GsPermissionComboBox
+{
+       GtkComboBox      parent_instance;
+
+       GsPermission    *permission;
+};
+
+G_DEFINE_TYPE (GsPermissionComboBox, gs_permission_combo_box, GTK_TYPE_COMBO_BOX)
+
+enum {
+       SIGNAL_CHANGED,
+       SIGNAL_LAST
+};
+
+static guint signals [SIGNAL_LAST] = { 0 };
+
+GsPermission *
+gs_permission_combo_box_get_permission (GsPermissionComboBox *combo)
+{
+       g_return_val_if_fail (GS_IS_PERMISSION_COMBO_BOX (combo), NULL);
+       return combo->permission;
+}
+
+static void
+changed_cb (GsPermissionComboBox *combo)
+{
+       GtkTreeIter iter;
+       GsPermissionValue *value = NULL;
+
+       if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
+               gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (combo)), &iter, 1, &value, -1);
+
+       g_signal_emit (combo, signals[SIGNAL_CHANGED], 0, value);
+}
+
+static void
+gs_permission_combo_box_dispose (GObject *object)
+{
+       GsPermissionComboBox *combo = GS_PERMISSION_COMBO_BOX (object);
+
+       g_clear_object (&combo->permission);
+
+       G_OBJECT_CLASS (gs_permission_combo_box_parent_class)->dispose (object);
+}
+
+static void
+gs_permission_combo_box_class_init (GsPermissionComboBoxClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->dispose = gs_permission_combo_box_dispose;
+
+       signals [SIGNAL_CHANGED] =
+               g_signal_new ("changed",
+                                 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
+                                 0,
+                                 NULL, NULL, g_cclosure_marshal_generic,
+                                 G_TYPE_NONE, 1, GS_TYPE_PERMISSION_VALUE);
+}
+
+static void
+gs_permission_combo_box_init (GsPermissionComboBox *combo)
+{
+}
+
+GsPermissionComboBox *
+gs_permission_combo_box_new (GsPermission *permission)
+{
+       GsPermissionComboBox *combo;
+       GtkListStore *store;
+       GtkCellRenderer *renderer;
+       guint i;
+       GtkTreeIter iter;
+       GPtrArray *values;
+
+       combo = g_object_new (GS_TYPE_PERMISSION_COMBO_BOX, NULL);
+       combo->permission = g_object_ref (permission);
+
+       store = gtk_list_store_new (2, G_TYPE_STRING, GS_TYPE_PERMISSION_VALUE);
+       gtk_combo_box_set_model (GTK_COMBO_BOX (combo), GTK_TREE_MODEL (store));
+
+       renderer = gtk_cell_renderer_text_new ();
+       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
+       gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo), renderer, "text", 0);
+
+       gtk_list_store_append (store, &iter);
+       gtk_list_store_set (store, &iter, 0, "(disconnected)", 1, NULL, -1);
+       values = gs_permission_get_values (permission);
+       for (i = 0; i < values->len; i++) {
+               GsPermissionValue *value = g_ptr_array_index (values, i);
+
+               gtk_list_store_append (store, &iter);
+               gtk_list_store_set (store, &iter, 0, gs_permission_value_get_label (value), 1, value, -1);
+
+               if (value == gs_permission_get_value (permission))
+                       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
+       }
+
+       g_signal_connect (combo, "changed", G_CALLBACK (changed_cb), NULL);
+
+       return combo;
+}
diff --git a/src/gs-permission-combo-box.h b/src/gs-permission-combo-box.h
new file mode 100644
index 0000000..5b1f647
--- /dev/null
+++ b/src/gs-permission-combo-box.h
@@ -0,0 +1,41 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2017 Canonical Ltd.
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef GS_PERMISSION_COMBO_BOX_H
+#define GS_PERMISSION_COMBO_BOX_H
+
+#include <gtk/gtk.h>
+
+#include "gnome-software-private.h"
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_PERMISSION_COMBO_BOX (gs_permission_combo_box_get_type ())
+
+G_DECLARE_FINAL_TYPE (GsPermissionComboBox, gs_permission_combo_box, GS, PERMISSION_COMBO_BOX, GtkComboBox)
+
+GsPermissionComboBox   *gs_permission_combo_box_new            (GsPermission *permission);
+
+GsPermission           *gs_permission_combo_box_get_permission (GsPermissionComboBox *combo);
+
+G_END_DECLS
+
+#endif /* GS_PERMISSION_COMBO_BOX_H */
diff --git a/src/gs-permission-dialog.c b/src/gs-permission-dialog.c
index 24e2127..50c330c 100644
--- a/src/gs-permission-dialog.c
+++ b/src/gs-permission-dialog.c
@@ -23,6 +23,7 @@
 
 #include "gs-permission-dialog.h"
 #include "gs-permission-switch.h"
+#include "gs-permission-combo-box.h"
 
 struct _GsPermissionDialog
 {
@@ -88,8 +89,10 @@ gs_permission_dialog_class_init (GsPermissionDialogClass *klass)
        gtk_widget_class_bind_template_child (widget_class, GsPermissionDialog, close_button);
 }
 
+// FIXME: Make a GsPermissionControl interfaces that can be shared between GsPermissionSwitch and 
GsPermissionComboBox
+
 static void
-switch_changed_cb (GsPermissionSwitch *sw, GsPermissionValue *value, GsPermissionDialog *dialog)
+permission_switch_changed_cb (GsPermissionSwitch *sw, GsPermissionValue *value, GsPermissionDialog *dialog)
 {
        g_signal_emit (dialog, signals[SIGNAL_PERMISSION_CHANGED], 0,
                       gs_permission_switch_get_permission (sw),
@@ -97,10 +100,18 @@ switch_changed_cb (GsPermissionSwitch *sw, GsPermissionValue *value, GsPermissio
 }
 
 static void
+permission_combo_box_changed_cb (GsPermissionComboBox *combo, GsPermissionValue *value, GsPermissionDialog 
*dialog)
+{
+       g_signal_emit (dialog, signals[SIGNAL_PERMISSION_CHANGED], 0,
+                      gs_permission_combo_box_get_permission (combo),
+                      value);
+}
+
+static void
 set_row (GsPermissionDialog *dialog, int row, GsPermission *permission)
 {
        GtkWidget *label;
-       GsPermissionSwitch *sw;
+       GtkWidget *control;
 
        label = gtk_label_new (gs_permission_get_label (permission));
        gtk_label_set_xalign (GTK_LABEL (label), 1.0);
@@ -108,10 +119,16 @@ set_row (GsPermissionDialog *dialog, int row, GsPermission *permission)
        gtk_widget_show (label);
        gtk_grid_attach (GTK_GRID (dialog->permission_grid), label, 0, row, 1, 1);
 
-       sw = gs_permission_switch_new (permission);
-       gtk_widget_show (GTK_WIDGET (sw));
-       g_signal_connect (sw, "changed", G_CALLBACK (switch_changed_cb), dialog);
-       gtk_grid_attach (GTK_GRID (dialog->permission_grid), GTK_WIDGET (sw), 1, row, 1, 1);
+       if (gs_permission_get_values (permission)->len == 1) {
+               control = GTK_WIDGET (gs_permission_switch_new (permission));
+               g_signal_connect (control, "changed", G_CALLBACK (permission_switch_changed_cb), dialog);
+       }
+       else {
+               control = GTK_WIDGET (gs_permission_combo_box_new (permission));
+               g_signal_connect (control, "changed", G_CALLBACK (permission_combo_box_changed_cb), dialog);
+       }
+       gtk_widget_show (control);
+       gtk_grid_attach (GTK_GRID (dialog->permission_grid), control, 1, row, 1, 1);
 }
 
 GtkWidget *
diff --git a/src/gs-permission-switch.h b/src/gs-permission-switch.h
index 9c1e7d6..7240686 100644
--- a/src/gs-permission-switch.h
+++ b/src/gs-permission-switch.h
@@ -32,9 +32,9 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GsPermissionSwitch, gs_permission_switch, GS, PERMISSION_SWITCH, GtkSwitch)
 
-GsPermissionSwitch *gs_permission_switch_new (GsPermission *permission);
+GsPermissionSwitch     *gs_permission_switch_new               (GsPermission *permission);
 
-GsPermission *gs_permission_switch_get_permission (GsPermissionSwitch *sw);
+GsPermission           *gs_permission_switch_get_permission    (GsPermissionSwitch *sw);
 
 G_END_DECLS
 
diff --git a/src/meson.build b/src/meson.build
index ca2e551..9db07d2 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -43,6 +43,7 @@ gnome_software_sources = [
   'gs-moderate-page.c',
   'gs-overview-page.c',
   'gs-page.c',
+  'gs-permission-combo-box.c',
   'gs-permission-dialog.c',
   'gs-permission-switch.c',
   'gs-popular-tile.c',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]