~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to shell/control-center.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "gnome-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
 
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
 
68
application_command_line_cb (GApplication  *application,
 
69
                             GApplicationCommandLine  *command_line,
 
70
                             GnomeControlCenter      *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
  gnome_control_center_show (shell, GTK_APPLICATION (application));
 
122
 
 
123
  if (show_overview)
 
124
    {
 
125
      gnome_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
 
 
132
      start_id = start_panels[0];
 
133
 
 
134
      if (start_panels[1])
 
135
        g_debug ("Extra argument: %s", start_panels[1]);
 
136
      else
 
137
        g_debug ("No extra argument");
 
138
 
 
139
      if (!cc_shell_set_active_panel_from_id (CC_SHELL (shell), start_id, (const gchar**)start_panels+1, &err))
 
140
        {
 
141
          g_warning ("Could not load setting panel \"%s\": %s", start_id,
 
142
                     (err) ? err->message : "Unknown error");
 
143
          retval = 1;
 
144
          if (err)
 
145
            {
 
146
              g_error_free (err);
 
147
              err = NULL;
 
148
            }
 
149
        }
 
150
    }
 
151
 
 
152
  gnome_control_center_present (shell);
 
153
  gdk_notify_startup_complete ();
 
154
 
 
155
  g_strfreev (argv);
 
156
  if (start_panels != NULL)
 
157
    {
 
158
      g_strfreev (start_panels);
 
159
      start_panels = NULL;
 
160
    }
 
161
  show_overview = FALSE;
 
162
 
 
163
  return retval;
 
164
}
 
165
 
 
166
static void
 
167
help_activated (GSimpleAction *action,
 
168
                GVariant      *parameter,
 
169
                gpointer       user_data)
 
170
{
 
171
  GnomeControlCenter *shell = user_data;
 
172
  CcPanel *panel = cc_shell_get_active_panel (CC_SHELL (shell));
 
173
  GtkWidget *window = cc_shell_get_toplevel (CC_SHELL (shell));
 
174
  const char *uri = NULL;
 
175
 
 
176
  if (panel)
 
177
    uri = cc_panel_get_help_uri (panel);
 
178
    if (!g_strcmp0(g_getenv("XDG_CURRENT_DESKTOP"), "Unity"))
 
179
      gtk_show_uri (gtk_widget_get_screen (window),
 
180
                uri ? uri : "help:ubuntu-help/prefs",
 
181
                GDK_CURRENT_TIME, NULL);
 
182
    else
 
183
      gtk_show_uri (gtk_widget_get_screen (window),
 
184
                uri ? uri : "help:gnome-help/prefs",
 
185
                GDK_CURRENT_TIME, NULL);
 
186
}
 
187
 
 
188
static void
 
189
contents_activated (GSimpleAction *action,
 
190
                    GVariant      *parameter,
 
191
                    gpointer       user_data)
 
192
{
 
193
  GnomeControlCenter *shell = user_data;
 
194
  GtkWidget *window = cc_shell_get_toplevel (CC_SHELL (shell));
 
195
  
 
196
  if (!g_strcmp0(g_getenv("XDG_CURRENT_DESKTOP"), "Unity"))
 
197
    gtk_show_uri (gtk_widget_get_screen (window),
 
198
                  "help:ubuntu-help/prefs",
 
199
                  GDK_CURRENT_TIME, NULL);
 
200
  else
 
201
    gtk_show_uri (gtk_widget_get_screen (window),
 
202
                  "help:gnome-help/prefs",
 
203
                  GDK_CURRENT_TIME, NULL);
 
204
}
 
205
 
 
206
static void
 
207
quit_activated (GSimpleAction *action,
 
208
                GVariant      *parameter,
 
209
                gpointer       user_data)
 
210
{
 
211
  GnomeControlCenter *shell = user_data;
 
212
  g_object_unref (shell);
 
213
}
 
214
 
 
215
static void
 
216
active_panel_changed_cb (GObject    *gobject,
 
217
                         GParamSpec *pspec,
 
218
                         GMenu      *menu) 
 
219
{
 
220
  CcPanel *panel = cc_shell_get_active_panel (CC_SHELL (gobject));
 
221
  const char *uri = NULL;
 
222
 
 
223
  if (panel)
 
224
    uri = cc_panel_get_help_uri (panel);
 
225
 
 
226
  gint n_items = g_menu_model_get_n_items (G_MENU_MODEL (menu));
 
227
 
 
228
  if (!uri && n_items > 1)
 
229
  {
 
230
    g_menu_remove (menu, 1);
 
231
  }
 
232
  else if (uri && n_items == 1)
 
233
  {
 
234
    g_menu_append (menu, cc_panel_get_display_name(panel), "app.help");
 
235
  }
 
236
  else if (uri && n_items > 1)
 
237
  {
 
238
    g_menu_remove (menu, 1);
 
239
    g_menu_append (menu, cc_panel_get_display_name(panel), "app.help");
 
240
  }
 
241
}
 
242
 
 
243
static void
 
244
application_startup_cb (GApplication       *application,
 
245
                        GnomeControlCenter *shell)
 
246
{
 
247
  GMenu *menubar, *menu, *section;
 
248
  GMenu *menuitem;
 
249
  GAction *action;
 
250
 
 
251
  action = G_ACTION (g_simple_action_new ("help", NULL));
 
252
  g_action_map_add_action (G_ACTION_MAP (application), action);
 
253
  g_signal_connect (action, "activate", G_CALLBACK (help_activated), shell);
 
254
 
 
255
  action = G_ACTION (g_simple_action_new ("contents", NULL));
 
256
  g_action_map_add_action (G_ACTION_MAP (application), action);
 
257
  g_signal_connect (action, "activate", G_CALLBACK (contents_activated), shell);
 
258
 
 
259
  action = G_ACTION (g_simple_action_new ("quit", NULL));
 
260
  g_action_map_add_action (G_ACTION_MAP (application), action);
 
261
  g_signal_connect (action, "activate", G_CALLBACK (quit_activated), shell);
 
262
 
 
263
  if (!g_strcmp0(g_getenv("XDG_CURRENT_DESKTOP"), "Unity"))
 
264
  {
 
265
    menubar = g_menu_new ();
 
266
    menu = g_menu_new ();
 
267
 
 
268
    section = g_menu_new ();
 
269
    g_menu_append (section, _("Contents"), "app.contents");
 
270
    g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
 
271
 
 
272
    g_menu_append (menu, _("Quit"), "app.quit");
 
273
 
 
274
    g_menu_append_submenu (menubar, _("Help"), G_MENU_MODEL (menu));
 
275
 
 
276
    gtk_application_set_menubar (GTK_APPLICATION (application),
 
277
                                 G_MENU_MODEL (menubar));
 
278
 
 
279
    g_signal_connect (shell, "notify::active-panel",
 
280
                      G_CALLBACK (active_panel_changed_cb), section);
 
281
  }
 
282
  else
 
283
  {
 
284
    menu = g_menu_new ();
 
285
 
 
286
    section = g_menu_new ();
 
287
    g_menu_append (section, _("Help"), "app.help");
 
288
    g_menu_append (section, _("Quit"), "app.quit");
 
289
 
 
290
    g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
 
291
 
 
292
    gtk_application_set_app_menu (GTK_APPLICATION (application),
 
293
                                  G_MENU_MODEL (menu));
 
294
  }
 
295
 
 
296
  gtk_application_add_accelerator (GTK_APPLICATION (application),
 
297
                                   "F1", "app.help", NULL);
 
298
 
 
299
  /* nothing else to do here, we don't want to show a window before
 
300
   * we've looked at the commandline
 
301
   */
 
302
}
 
303
 
 
304
int
 
305
main (int argc, char **argv)
 
306
{
 
307
  GnomeControlCenter *shell;
 
308
  GtkApplication *application;
 
309
  int status;
 
310
 
 
311
  bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
 
312
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
313
  textdomain (GETTEXT_PACKAGE);
 
314
 
 
315
#ifdef GDK_WINDOWING_X11
 
316
  XInitThreads ();
 
317
#endif
 
318
 
 
319
  gtk_init (&argc, &argv);
 
320
  cc_shell_log_init ();
 
321
 
 
322
  /* register a symbolic icon size for use in sidebar lists */
 
323
  gtk_icon_size_register ("cc-sidebar-list", 24, 24);
 
324
 
 
325
  notify_init ("gnome-control-center");
 
326
 
 
327
  shell = gnome_control_center_new ();
 
328
 
 
329
  /* enforce single instance of this application */
 
330
  application = gtk_application_new ("org.gnome.ControlCenter", G_APPLICATION_HANDLES_COMMAND_LINE);
 
331
  g_signal_connect (application, "startup",
 
332
                    G_CALLBACK (application_startup_cb), shell);
 
333
  g_signal_connect (application, "command-line",
 
334
                    G_CALLBACK (application_command_line_cb), shell);
 
335
 
 
336
  status = g_application_run (G_APPLICATION (application), argc, argv);
 
337
 
 
338
  g_object_unref (application);
 
339
 
 
340
  return status;
 
341
}