|
| 1 | +#include "panel.h" |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +#include <gio/gio.h> |
| 7 | +#include <gtk/gtk.h> |
| 8 | + |
| 9 | +typedef struct { |
| 10 | + OMPShell* shell; |
| 11 | + GCancellable* cancellable; |
| 12 | + |
| 13 | + gchar* subpage; |
| 14 | +} OMPPanelPrivate; |
| 15 | + |
| 16 | +G_DEFINE_ABSTRACT_TYPE_WITH_CODE ( |
| 17 | + OMPPanel, omp_panel, ADW_TYPE_NAVIGATION_PAGE, G_ADD_PRIVATE (OMPPanel) |
| 18 | +) |
| 19 | + |
| 20 | +enum { PROP_0, PROP_SHELL, PROP_PARAMETERS, PROP_SUBPAGE, N_PROPS }; |
| 21 | + |
| 22 | +static GParamSpec* properties[N_PROPS]; |
| 23 | + |
| 24 | +/* GtkBuildable interface */ |
| 25 | + |
| 26 | +/* GObject overrides */ |
| 27 | + |
| 28 | +static void |
| 29 | +omp_panel_set_property ( |
| 30 | + GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec |
| 31 | +) |
| 32 | +{ |
| 33 | + OMPPanelPrivate* priv = omp_panel_get_instance_private (OMP_PANEL (object)); |
| 34 | + |
| 35 | + switch (prop_id) { |
| 36 | + case PROP_SHELL: |
| 37 | + /* construct only property */ |
| 38 | + priv->shell = g_value_get_object (value); |
| 39 | + break; |
| 40 | + |
| 41 | + case PROP_PARAMETERS: { |
| 42 | + g_autoptr (GVariant) v = NULL; |
| 43 | + GVariant* parameters; |
| 44 | + gsize n_parameters; |
| 45 | + |
| 46 | + parameters = g_value_get_variant (value); |
| 47 | + |
| 48 | + if (parameters == NULL) |
| 49 | + return; |
| 50 | + |
| 51 | + n_parameters = g_variant_n_children (parameters); |
| 52 | + if (n_parameters == 0) |
| 53 | + return; |
| 54 | + |
| 55 | + g_variant_get_child (parameters, 0, "v", &v); |
| 56 | + |
| 57 | + if (g_variant_is_of_type (v, G_VARIANT_TYPE_STRING)) { |
| 58 | + g_set_str (&priv->subpage, g_variant_get_string (v, NULL)); |
| 59 | + g_object_notify_by_pspec (object, properties[PROP_SUBPAGE]); |
| 60 | + } |
| 61 | + else if (!g_variant_is_of_type (v, G_VARIANT_TYPE_DICTIONARY)) |
| 62 | + g_warning ( |
| 63 | + "Wrong type for the first argument GVariant, expected " |
| 64 | + "'a{sv}' but got '%s'", |
| 65 | + (gchar*)g_variant_get_type (v) |
| 66 | + ); |
| 67 | + else if (g_variant_n_children (v) > 0) |
| 68 | + g_warning ("Ignoring additional flags"); |
| 69 | + |
| 70 | + if (n_parameters > 1) |
| 71 | + g_warning ("Ignoring additional parameters"); |
| 72 | + |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + default: |
| 77 | + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 78 | + break; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +static void |
| 83 | +omp_panel_get_property ( |
| 84 | + GObject* object, guint prop_id, GValue* value, GParamSpec* pspec |
| 85 | +) |
| 86 | +{ |
| 87 | + OMPPanelPrivate* priv = omp_panel_get_instance_private (OMP_PANEL (object)); |
| 88 | + |
| 89 | + switch (prop_id) { |
| 90 | + case PROP_SHELL: |
| 91 | + g_value_set_object (value, priv->shell); |
| 92 | + break; |
| 93 | + |
| 94 | + case PROP_SUBPAGE: |
| 95 | + g_value_set_string (value, priv->subpage); |
| 96 | + break; |
| 97 | + |
| 98 | + default: |
| 99 | + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 100 | + break; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +static void |
| 105 | +omp_panel_finalize (GObject* object) |
| 106 | +{ |
| 107 | + OMPPanelPrivate* priv = omp_panel_get_instance_private (OMP_PANEL (object)); |
| 108 | + |
| 109 | + g_cancellable_cancel (priv->cancellable); |
| 110 | + g_clear_object (&priv->cancellable); |
| 111 | + g_clear_pointer (&priv->subpage, g_free); |
| 112 | + |
| 113 | + G_OBJECT_CLASS (omp_panel_parent_class)->finalize (object); |
| 114 | +} |
| 115 | + |
| 116 | +static void |
| 117 | +omp_panel_class_init (OMPPanelClass* klass) |
| 118 | +{ |
| 119 | + GObjectClass* object_class = G_OBJECT_CLASS (klass); |
| 120 | + |
| 121 | + object_class->get_property = omp_panel_get_property; |
| 122 | + object_class->set_property = omp_panel_set_property; |
| 123 | + object_class->finalize = omp_panel_finalize; |
| 124 | + |
| 125 | + properties[PROP_SHELL] = g_param_spec_object ( |
| 126 | + "shell", "Shell", "Shell the Panel resides in", OMP_TYPE_SHELL, |
| 127 | + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS |
| 128 | + ); |
| 129 | + |
| 130 | + properties[PROP_PARAMETERS] = g_param_spec_variant ( |
| 131 | + "parameters", "Structured parameters", |
| 132 | + "Additional parameters passed externally (ie. command line, D-Bus " |
| 133 | + "activation)", |
| 134 | + G_VARIANT_TYPE ("av"), NULL, G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS |
| 135 | + ); |
| 136 | + |
| 137 | + properties[PROP_SUBPAGE] = g_param_spec_string ( |
| 138 | + "subpage", "Subpage parameters", |
| 139 | + "Additional parameter extracted from the parameters property to launch " |
| 140 | + "a panel subpage", |
| 141 | + NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS |
| 142 | + ); |
| 143 | + |
| 144 | + g_object_class_install_properties (object_class, N_PROPS, properties); |
| 145 | +} |
| 146 | + |
| 147 | +static void |
| 148 | +omp_panel_init (OMPPanel* panel) |
| 149 | +{ |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * omp_panel_get_shell: |
| 154 | + * @panel: A #OMPPanel |
| 155 | + * |
| 156 | + * Get the shell that the panel resides in |
| 157 | + * |
| 158 | + * Returns: a #OMPShell |
| 159 | + */ |
| 160 | +OMPShell* |
| 161 | +omp_panel_get_shell (OMPPanel* panel) |
| 162 | +{ |
| 163 | + OMPPanelPrivate* priv; |
| 164 | + |
| 165 | + g_return_val_if_fail (OMP_IS_PANEL (panel), NULL); |
| 166 | + |
| 167 | + priv = omp_panel_get_instance_private (panel); |
| 168 | + |
| 169 | + return priv->shell; |
| 170 | +} |
| 171 | + |
| 172 | +const gchar* |
| 173 | +omp_panel_get_help_uri (OMPPanel* panel) |
| 174 | +{ |
| 175 | + OMPPanelClass* class = OMP_PANEL_GET_CLASS (panel); |
| 176 | + |
| 177 | + if (class->get_help_uri) |
| 178 | + return class->get_help_uri (panel); |
| 179 | + |
| 180 | + return NULL; |
| 181 | +} |
| 182 | + |
| 183 | +GCancellable* |
| 184 | +omp_panel_get_cancellable (OMPPanel* panel) |
| 185 | +{ |
| 186 | + OMPPanelPrivate* priv = omp_panel_get_instance_private (panel); |
| 187 | + |
| 188 | + g_return_val_if_fail (OMP_IS_PANEL (panel), NULL); |
| 189 | + |
| 190 | + if (priv->cancellable == NULL) |
| 191 | + priv->cancellable = g_cancellable_new (); |
| 192 | + |
| 193 | + return priv->cancellable; |
| 194 | +} |
| 195 | + |
| 196 | +void |
| 197 | +omp_panel_deactivate (OMPPanel* panel) |
| 198 | +{ |
| 199 | + OMPPanelPrivate* priv = omp_panel_get_instance_private (panel); |
| 200 | + |
| 201 | + g_cancellable_cancel (priv->cancellable); |
| 202 | +} |
0 commit comments