1 /*
2  * Copyright © 2013 Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 #include "config.h"
21 
22 #include <stdlib.h>
23 
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gio/gio.h>
27 #include <handy.h>
28 
29 #include "cc-application.h"
30 #include "cc-log.h"
31 #include "cc-object-storage.h"
32 #include "cc-panel-loader.h"
33 #include "cc-window.h"
34 
35 struct _CcApplication
36 {
37   GtkApplication  parent;
38 
39   CcShellModel   *model;
40 
41   CcWindow       *window;
42 };
43 
44 static void cc_application_quit    (GSimpleAction *simple,
45                                     GVariant      *parameter,
46                                     gpointer       user_data);
47 
48 static void launch_panel_activated (GSimpleAction *action,
49                                     GVariant      *parameter,
50                                     gpointer       user_data);
51 
52 static void help_activated         (GSimpleAction *action,
53                                     GVariant      *parameter,
54                                     gpointer       user_data);
55 
56 G_DEFINE_TYPE (CcApplication, cc_application, GTK_TYPE_APPLICATION)
57 
58 const GOptionEntry all_options[] = {
59   { "version", 0, 0, G_OPTION_ARG_NONE, NULL, N_("Display version number"), NULL },
60   { "verbose", 'v', 0, G_OPTION_ARG_NONE, NULL, N_("Enable verbose mode"), NULL },
61   { "search", 's', 0, G_OPTION_ARG_STRING, NULL, N_("Search for the string"), "SEARCH" },
62   { "list", 'l', 0, G_OPTION_ARG_NONE, NULL, N_("List possible panel names and exit"), NULL },
63   { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, NULL, N_("Panel to display"), N_("[PANEL] [ARGUMENT…]") },
64   { NULL, 0, 0, 0, NULL, NULL, NULL } /* end the list */
65 };
66 
67 static const GActionEntry cc_app_actions[] = {
68   { "launch-panel", launch_panel_activated, "(sav)", NULL, NULL, { 0 } },
69   { "help", help_activated, NULL, NULL, NULL, { 0 } },
70   { "quit", cc_application_quit, NULL, NULL, NULL, { 0 } }
71 };
72 
73 static void
help_activated(GSimpleAction * action,GVariant * parameter,gpointer user_data)74 help_activated (GSimpleAction *action,
75                 GVariant      *parameter,
76                 gpointer       user_data)
77 {
78   CcApplication *self = CC_APPLICATION (user_data);
79   CcPanel *panel;
80   GtkWidget *window;
81   const char *uri = NULL;
82 
83   panel = cc_shell_get_active_panel (CC_SHELL (self->window));
84   if (panel)
85     uri = cc_panel_get_help_uri (panel);
86 
87   window = cc_shell_get_toplevel (CC_SHELL (self->window));
88   gtk_show_uri_on_window (GTK_WINDOW (window),
89                           uri ? uri : "help:gnome-help/prefs",
90                           GDK_CURRENT_TIME,
91                           NULL);
92 }
93 
94 static void
launch_panel_activated(GSimpleAction * action,GVariant * parameter,gpointer user_data)95 launch_panel_activated (GSimpleAction *action,
96                         GVariant      *parameter,
97                         gpointer       user_data)
98 {
99   CcApplication *self = CC_APPLICATION (user_data);
100   g_autoptr(GVariant) parameters = NULL;
101   g_autoptr(GError) error = NULL;
102   gchar *panel_id;
103 
104   g_variant_get (parameter, "(&s@av)", &panel_id, &parameters);
105 
106   g_debug ("gnome-control-center: 'launch-panel' activated for panel '%s' with %"G_GSIZE_FORMAT" arguments",
107            panel_id,
108            g_variant_n_children (parameters));
109 
110   if (!cc_shell_set_active_panel_from_id (CC_SHELL (self->window), panel_id, parameters, &error))
111     g_warning ("Failed to activate the '%s' panel: %s", panel_id, error->message);
112 
113   /* Now present the window */
114   g_application_activate (G_APPLICATION (self));
115 }
116 
117 static gint
cc_application_handle_local_options(GApplication * application,GVariantDict * options)118 cc_application_handle_local_options (GApplication *application,
119                                      GVariantDict *options)
120 {
121   if (g_variant_dict_contains (options, "version"))
122     {
123       g_print ("%s %s\n", PACKAGE, VERSION);
124       return 0;
125     }
126 
127   if (g_variant_dict_contains (options, "list"))
128     {
129       cc_panel_loader_list_panels ();
130       return 0;
131     }
132 
133   return -1;
134 }
135 
136 static int
cc_application_command_line(GApplication * application,GApplicationCommandLine * command_line)137 cc_application_command_line (GApplication            *application,
138                              GApplicationCommandLine *command_line)
139 {
140   CcApplication *self;
141   g_autofree GStrv start_panels = NULL;
142   GVariantDict *options;
143   int retval = 0;
144   char *search_str;
145   gboolean debug;
146 
147   self = CC_APPLICATION (application);
148   options = g_application_command_line_get_options_dict (command_line);
149 
150   debug = g_variant_dict_contains (options, "verbose");
151 
152   if (debug)
153     cc_log_init ();
154 
155   gtk_window_present (GTK_WINDOW (self->window));
156 
157   if (g_variant_dict_lookup (options, "search", "&s", &search_str))
158     {
159       cc_window_set_search_item (self->window, search_str);
160     }
161   else if (g_variant_dict_lookup (options, G_OPTION_REMAINING, "^a&ay", &start_panels))
162     {
163       const char *start_id;
164       GError *err = NULL;
165       GVariant *parameters;
166       GVariantBuilder builder;
167       int i;
168 
169       g_return_val_if_fail (start_panels[0] != NULL, 1);
170       start_id = start_panels[0];
171 
172       if (start_panels[1])
173         g_debug ("Extra argument: %s", start_panels[1]);
174       else
175         g_debug ("No extra argument");
176 
177       g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));
178 
179       for (i = 1; start_panels[i] != NULL; i++)
180         g_variant_builder_add (&builder, "v", g_variant_new_string (start_panels[i]));
181       parameters = g_variant_builder_end (&builder);
182       if (!cc_shell_set_active_panel_from_id (CC_SHELL (self->window), start_id, parameters, &err))
183         {
184           g_warning ("Could not load setting panel \"%s\": %s", start_id,
185                      (err) ? err->message : "Unknown error");
186           retval = 1;
187 
188           if (err)
189             g_clear_error (&err);
190         }
191     }
192 
193   return retval;
194 }
195 
196 static void
cc_application_quit(GSimpleAction * simple,GVariant * parameter,gpointer user_data)197 cc_application_quit (GSimpleAction *simple,
198                      GVariant      *parameter,
199                      gpointer       user_data)
200 {
201   CcApplication *self = CC_APPLICATION (user_data);
202 
203   gtk_widget_destroy (GTK_WIDGET (self->window));
204 }
205 
206 
207 static void
cc_application_activate(GApplication * application)208 cc_application_activate (GApplication *application)
209 {
210   CcApplication *self = CC_APPLICATION (application);
211 
212   gtk_window_present (GTK_WINDOW (self->window));
213 }
214 
215 static void
cc_application_startup(GApplication * application)216 cc_application_startup (GApplication *application)
217 {
218   CcApplication *self = CC_APPLICATION (application);
219   const gchar *help_accels[] = { "F1", NULL };
220 
221   g_action_map_add_action_entries (G_ACTION_MAP (self),
222                                    cc_app_actions,
223                                    G_N_ELEMENTS (cc_app_actions),
224                                    self);
225 
226   G_APPLICATION_CLASS (cc_application_parent_class)->startup (application);
227 
228   hdy_init ();
229 
230   gtk_application_set_accels_for_action (GTK_APPLICATION (application),
231                                          "app.help", help_accels);
232 
233   self->model = cc_shell_model_new ();
234   self->window = cc_window_new (GTK_APPLICATION (application), self->model);
235 }
236 
237 static void
cc_application_finalize(GObject * object)238 cc_application_finalize (GObject *object)
239 {
240   /* Destroy the object storage cache when finalizing */
241   cc_object_storage_destroy ();
242 
243   G_OBJECT_CLASS (cc_application_parent_class)->finalize (object);
244 }
245 
246 static GObject *
cc_application_constructor(GType type,guint n_construct_params,GObjectConstructParam * construct_params)247 cc_application_constructor (GType                  type,
248                             guint                  n_construct_params,
249                             GObjectConstructParam *construct_params)
250 {
251   static GObject *self = NULL;
252 
253   if (self == NULL)
254     {
255       self = G_OBJECT_CLASS (cc_application_parent_class)->constructor (type,
256                                                                         n_construct_params,
257                                                                         construct_params);
258       g_object_add_weak_pointer (self, (gpointer) &self);
259       return self;
260     }
261 
262   return g_object_ref (self);
263 }
264 
265 static void
cc_application_class_init(CcApplicationClass * klass)266 cc_application_class_init (CcApplicationClass *klass)
267 {
268   GObjectClass *object_class = G_OBJECT_CLASS (klass);
269   GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
270 
271   object_class->finalize = cc_application_finalize;
272   object_class->constructor = cc_application_constructor;
273   application_class->activate = cc_application_activate;
274   application_class->startup = cc_application_startup;
275   application_class->command_line = cc_application_command_line;
276   application_class->handle_local_options = cc_application_handle_local_options;
277 }
278 
279 static void
cc_application_init(CcApplication * self)280 cc_application_init (CcApplication *self)
281 {
282   g_autoptr(GtkCssProvider) provider = NULL;
283 
284   cc_object_storage_initialize ();
285 
286   g_application_add_main_option_entries (G_APPLICATION (self), all_options);
287 
288   provider = gtk_css_provider_new ();
289   gtk_css_provider_load_from_resource (provider, "/org/gnome/ControlCenter/gtk/style.css");
290   gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
291                                              GTK_STYLE_PROVIDER (provider),
292                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
293 }
294 
295 GtkApplication *
cc_application_new(void)296 cc_application_new (void)
297 {
298   return g_object_new (CC_TYPE_APPLICATION,
299                        "application-id", "org.gnome.ControlCenter",
300                        "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
301                        NULL);
302 }
303 
304 CcShellModel *
cc_application_get_model(CcApplication * self)305 cc_application_get_model (CcApplication *self)
306 {
307   g_return_val_if_fail (CC_IS_APPLICATION (self), NULL);
308 
309   return self->model;
310 }
311