1 /*
2  * Copyright (c) 2009, 2010 Intel, Inc.
3  * Copyright (c) 2010 Red Hat, Inc.
4  *
5  * The Control Center is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * The Control Center is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with the Control Center; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Author: Thomas Wood <thos@gnome.org>
20  */
21 
22 #include "config.h"
23 
24 #include <glib/gi18n.h>
25 #include <stdlib.h>
26 
27 #include "cinnamon-control-center.h"
28 
29 #include <gtk/gtk.h>
30 #include <string.h>
31 #include <libnotify/notify.h>
32 
33 #ifdef GDK_WINDOWING_X11
34 #include <X11/Xlib.h>
35 #endif
36 
37 #include "cc-shell-log.h"
38 
39 G_GNUC_NORETURN static gboolean
option_version_cb(const gchar * option_name,const gchar * value,gpointer data,GError ** error)40 option_version_cb (const gchar *option_name,
41                    const gchar *value,
42                    gpointer     data,
43                    GError     **error)
44 {
45   g_print ("%s %s\n", PACKAGE, VERSION);
46   exit (0);
47 }
48 
49 static char **start_panels = NULL;
50 static gboolean show_overview = FALSE;
51 static gboolean verbose = FALSE;
52 static gboolean show_help = FALSE;
53 static gboolean show_help_gtk = FALSE;
54 static gboolean show_help_all = FALSE;
55 
56 const GOptionEntry all_options[] = {
57   { "version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
58   { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, N_("Enable verbose mode"), NULL },
59   { "overview", 'o', 0, G_OPTION_ARG_NONE, &show_overview, N_("Show the overview"), NULL },
60   { "help", 'h', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &show_help, N_("Show help options"), NULL },
61   { "help-all", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &show_help_all, N_("Show help options"), NULL },
62   { "help-gtk", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &show_help_gtk, N_("Show help options"), NULL },
63   { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &start_panels, N_("Panel to display"), NULL },
64   { NULL, 0, 0, 0, NULL, NULL, NULL } /* end the list */
65 };
66 
67 static int
application_command_line_cb(GApplication * application,GApplicationCommandLine * command_line,CinnamonControlCenter * shell)68 application_command_line_cb (GApplication  *application,
69                              GApplicationCommandLine  *command_line,
70                              CinnamonControlCenter      *shell)
71 {
72   int argc;
73   char **argv;
74   int retval = 0;
75   GOptionContext *context;
76   GError *error = NULL;
77 
78   verbose = FALSE;
79   show_overview = FALSE;
80   show_help = FALSE;
81   start_panels = NULL;
82 
83   argv = g_application_command_line_get_arguments (command_line, &argc);
84 
85   context = g_option_context_new (N_("- System Settings"));
86   g_option_context_add_main_entries (context, all_options, GETTEXT_PACKAGE);
87   g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
88   g_option_context_add_group (context, gtk_get_option_group (TRUE));
89   g_option_context_set_help_enabled (context, FALSE);
90 
91   if (g_option_context_parse (context, &argc, &argv, &error) == FALSE)
92     {
93       g_print (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
94                error->message, argv[0]);
95       g_error_free (error);
96       g_option_context_free (context);
97       return 1;
98     }
99 
100   if (show_help || show_help_all || show_help_gtk)
101     {
102       gchar *help;
103       GOptionGroup *group;
104 
105       if (show_help || show_help_all)
106         group = NULL;
107       else
108         group = gtk_get_option_group (FALSE);
109 
110       help = g_option_context_get_help (context, FALSE, group);
111       g_print ("%s", help);
112       g_free (help);
113       g_option_context_free (context);
114       return 0;
115     }
116 
117   g_option_context_free (context);
118 
119   cc_shell_log_set_debug (verbose);
120 
121   cinnamon_control_center_show (shell, GTK_APPLICATION (application));
122 
123   if (show_overview)
124     {
125       cinnamon_control_center_set_overview_page (shell);
126     }
127   else if (start_panels != NULL && start_panels[0] != NULL)
128     {
129       const char *start_id;
130       GError *err = NULL;
131       GVariant *parameters;
132       GVariantBuilder *builder;
133       int i;
134 
135       start_id = start_panels[0];
136 
137       if (start_panels[1])
138         g_debug ("Extra argument: %s", start_panels[1]);
139       else
140         g_debug ("No extra argument");
141 
142       builder = g_variant_builder_new (G_VARIANT_TYPE ("av"));
143       for (i = 1; start_panels[i] != NULL; i++)
144         g_variant_builder_add (builder, "v", g_variant_new_string (start_panels[i]));
145       parameters = g_variant_builder_end (builder);
146       if (!cc_shell_set_active_panel_from_id (CC_SHELL (shell), start_id, parameters, &err))
147         {
148           g_warning ("Could not load setting panel \"%s\": %s", start_id,
149                      (err) ? err->message : "Unknown error");
150           retval = 1;
151           if (err)
152             {
153               g_error_free (err);
154               err = NULL;
155             }
156         }
157     }
158 
159   cinnamon_control_center_present (shell);
160   gdk_notify_startup_complete ();
161 
162   g_strfreev (argv);
163   if (start_panels != NULL)
164     {
165       g_strfreev (start_panels);
166       start_panels = NULL;
167     }
168   show_overview = FALSE;
169 
170   return retval;
171 }
172 
173 static void
help_activated(GSimpleAction * action,GVariant * parameter,gpointer user_data)174 help_activated (GSimpleAction *action,
175                 GVariant      *parameter,
176                 gpointer       user_data)
177 {
178   CinnamonControlCenter *shell = user_data;
179   CcPanel *panel = cc_shell_get_active_panel (CC_SHELL (shell));
180   GtkWidget *window = cc_shell_get_toplevel (CC_SHELL (shell));
181   const char *uri = NULL;
182 
183   if (panel)
184     uri = cc_panel_get_help_uri (panel);
185     if (!g_strcmp0(g_getenv("XDG_CURRENT_DESKTOP"), "Unity"))
186       gtk_show_uri (gtk_widget_get_screen (window),
187                 uri ? uri : "help:ubuntu-help/prefs",
188                 GDK_CURRENT_TIME, NULL);
189     else
190       gtk_show_uri (gtk_widget_get_screen (window),
191                 uri ? uri : "help:gnome-help/prefs",
192                 GDK_CURRENT_TIME, NULL);
193 }
194 
195 static void
quit_activated(GSimpleAction * action,GVariant * parameter,gpointer user_data)196 quit_activated (GSimpleAction *action,
197                 GVariant      *parameter,
198                 gpointer       user_data)
199 {
200   CinnamonControlCenter *shell = user_data;
201   g_object_unref (shell);
202 }
203 
204 static void
application_startup_cb(GApplication * application,CinnamonControlCenter * shell)205 application_startup_cb (GApplication       *application,
206                         CinnamonControlCenter *shell)
207 {
208   GMenu *menu, *section;
209   GAction *action;
210 
211   action = G_ACTION (g_simple_action_new ("help", NULL));
212   g_action_map_add_action (G_ACTION_MAP (application), action);
213   g_signal_connect (action, "activate", G_CALLBACK (help_activated), shell);
214 
215   action = G_ACTION (g_simple_action_new ("quit", NULL));
216   g_action_map_add_action (G_ACTION_MAP (application), action);
217   g_signal_connect (action, "activate", G_CALLBACK (quit_activated), shell);
218 
219   menu = g_menu_new ();
220 
221   section = g_menu_new ();
222   g_menu_append (section, _("Help"), "app.help");
223   g_menu_append (section, _("Quit"), "app.quit");
224 
225   g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
226 
227   gtk_application_set_app_menu (GTK_APPLICATION (application),
228                                 G_MENU_MODEL (menu));
229 
230   gtk_application_add_accelerator (GTK_APPLICATION (application),
231                                    "F1", "app.help", NULL);
232 
233   /* nothing else to do here, we don't want to show a window before
234    * we've looked at the commandline
235    */
236 }
237 
238 int
main(int argc,char ** argv)239 main (int argc, char **argv)
240 {
241   CinnamonControlCenter *shell;
242   GtkApplication *application;
243   int status;
244 
245   bindtextdomain (GETTEXT_PACKAGE, "/usr/share/locale");
246   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
247   textdomain (GETTEXT_PACKAGE);
248 
249 #ifdef GDK_WINDOWING_X11
250   XInitThreads ();
251 #endif
252 
253   gtk_init (&argc, &argv);
254   cc_shell_log_init ();
255 
256   /* register a symbolic icon size for use in sidebar lists */
257   gtk_icon_size_register ("cc-sidebar-list", 24, 24);
258 
259   notify_init ("cinnamon-control-center");
260 
261   shell = cinnamon_control_center_new ();
262 
263   /* enforce single instance of this application */
264   application = gtk_application_new ("org.cinnamon.ControlCenter", G_APPLICATION_HANDLES_COMMAND_LINE);
265   g_signal_connect (application, "startup",
266                     G_CALLBACK (application_startup_cb), shell);
267   g_signal_connect (application, "command-line",
268                     G_CALLBACK (application_command_line_cb), shell);
269 
270   status = g_application_run (G_APPLICATION (application), argc, argv);
271 
272   g_object_unref (application);
273 
274   return status;
275 }
276