control-center.c 8.02 KB
Newer Older
Rodrigo Moya's avatar
Rodrigo Moya committed
1
/*
Thomas Wood's avatar
Thomas Wood committed
2
3
 * Copyright (c) 2009, 2010 Intel, Inc.
 * Copyright (c) 2010 Red Hat, Inc.
Rodrigo Moya's avatar
Rodrigo Moya committed
4
 *
Thomas Wood's avatar
Thomas Wood committed
5
6
7
8
 * The Control Center 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.
Rodrigo Moya's avatar
Rodrigo Moya committed
9
 *
Thomas Wood's avatar
Thomas Wood committed
10
11
12
13
 * The Control Center 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.
Rodrigo Moya's avatar
Rodrigo Moya committed
14
 *
Thomas Wood's avatar
Thomas Wood committed
15
16
17
 * You should have received a copy of the GNU General Public License along
 * with the Control Center; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Rodrigo Moya's avatar
Rodrigo Moya committed
18
 *
Thomas Wood's avatar
Thomas Wood committed
19
 * Author: Thomas Wood <thos@gnome.org>
Rodrigo Moya's avatar
Rodrigo Moya committed
20
21
22
23
 */

#include "config.h"

24
#include <glib/gi18n.h>
25
#include <stdlib.h>
26

Thomas Wood's avatar
Thomas Wood committed
27
28
#include "gnome-control-center.h"

Rodrigo Moya's avatar
Rodrigo Moya committed
29
#include <gtk/gtk.h>
Thomas Wood's avatar
Thomas Wood committed
30
#include <string.h>
31
#include <libnotify/notify.h>
32
#include <egg-list-box.h>
Rodrigo Moya's avatar
Rodrigo Moya committed
33

34
35
36
37
#ifdef GDK_WINDOWING_X11
#include <X11/Xlib.h>
#endif

38
39
40
41
#ifdef HAVE_CHEESE
#include <cheese-gtk.h>
#endif /* HAVE_CHEESE */

42
43
#include "cc-shell-log.h"

44
45
46
47
48
49
50
51
52
53
54
55
G_GNUC_NORETURN static gboolean
option_version_cb (const gchar *option_name,
                   const gchar *value,
                   gpointer     data,
                   GError     **error)
{
  g_print ("%s %s\n", PACKAGE, VERSION);
  exit (0);
}

static char **start_panels = NULL;
static gboolean show_overview = FALSE;
56
static gboolean verbose = FALSE;
57
static gboolean show_help = FALSE;
58
59
static gboolean show_help_gtk = FALSE;
static gboolean show_help_all = FALSE;
60
61
62

const GOptionEntry all_options[] = {
  { "version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
63
  { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, N_("Enable verbose mode"), NULL },
64
  { "overview", 'o', 0, G_OPTION_ARG_NONE, &show_overview, N_("Show the overview"), NULL },
65
66
67
  { "help", 'h', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &show_help, N_("Show help options"), NULL },
  { "help-all", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &show_help_all, N_("Show help options"), NULL },
  { "help-gtk", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &show_help_gtk, N_("Show help options"), NULL },
68
  { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &start_panels, N_("Panel to display"), N_("[PANEL] [ARGUMENT…]") },
Robert Ancell's avatar
Robert Ancell committed
69
  { NULL, 0, 0, 0, NULL, NULL, NULL } /* end the list */
70
};
71

72
static int
73
application_command_line_cb (GApplication  *application,
74
75
                             GApplicationCommandLine  *command_line,
                             GnomeControlCenter      *shell)
76
{
77
78
  int argc;
  char **argv;
79
  int retval = 0;
80
81
82
  GOptionContext *context;
  GError *error = NULL;

83
84
85
86
87
  verbose = FALSE;
  show_overview = FALSE;
  show_help = FALSE;
  start_panels = NULL;

88
89
  argv = g_application_command_line_get_arguments (command_line, &argc);

Bastien Nocera's avatar
Bastien Nocera committed
90
  context = g_option_context_new (N_("- Settings"));
91
92
93
  g_option_context_add_main_entries (context, all_options, GETTEXT_PACKAGE);
  g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
  g_option_context_add_group (context, gtk_get_option_group (TRUE));
94
  g_option_context_set_help_enabled (context, FALSE);
95
96
97
98
99
100
101

  if (g_option_context_parse (context, &argc, &argv, &error) == FALSE)
    {
      g_print (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
               error->message, argv[0]);
      g_error_free (error);
      g_option_context_free (context);
102
      return 1;
103
    }
104

105
  if (show_help || show_help_all || show_help_gtk)
106
107
    {
      gchar *help;
108
      GOptionGroup *group;
109

110
111
112
113
114
115
      if (show_help || show_help_all)
        group = NULL;
      else
        group = gtk_get_option_group (FALSE);

      help = g_option_context_get_help (context, FALSE, group);
Rodrigo Moya's avatar
Rodrigo Moya committed
116
      g_print ("%s", help);
117
118
119
120
121
      g_free (help);
      g_option_context_free (context);
      return 0;
    }

122
  g_option_context_free (context);
Thomas Wood's avatar
Thomas Wood committed
123

124
125
126
127
#ifdef HAVE_CHEESE
  cheese_gtk_init (&argc, &argv);
#endif /* HAVE_CHEESE */

128
129
  cc_shell_log_set_debug (verbose);

130
131
  gnome_control_center_show (shell, GTK_APPLICATION (application));

132
133
134
135
136
  if (show_overview)
    {
      gnome_control_center_set_overview_page (shell);
    }
  else if (start_panels != NULL && start_panels[0] != NULL)
Thomas Wood's avatar
Thomas Wood committed
137
    {
138
      const char *start_id;
Thomas Wood's avatar
Thomas Wood committed
139
      GError *err = NULL;
140

141
      start_id = start_panels[0];
142

143
144
145
146
147
148
      if (start_panels[1])
	g_debug ("Extra argument: %s", start_panels[1]);
      else
	g_debug ("No extra argument");

      if (!cc_shell_set_active_panel_from_id (CC_SHELL (shell), start_id, (const gchar**)start_panels+1, &err))
Thomas Wood's avatar
Thomas Wood committed
149
        {
150
151
          g_warning ("Could not load setting panel \"%s\": %s", start_id,
                     (err) ? err->message : "Unknown error");
152
          retval = 1;
Thomas Wood's avatar
Thomas Wood committed
153
154
155
          if (err)
            {
              g_error_free (err);
156
              err = NULL;
Thomas Wood's avatar
Thomas Wood committed
157
158
159
            }
        }
    }
160
161

  gnome_control_center_present (shell);
162
  gdk_notify_startup_complete ();
163

164
  g_strfreev (argv);
165
166
167
168
169
170
171
  if (start_panels != NULL)
    {
      g_strfreev (start_panels);
      start_panels = NULL;
    }
  show_overview = FALSE;

172
  return retval;
173
174
}

Florian Müllner's avatar
Florian Müllner committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
static void
help_activated (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
{
  GnomeControlCenter *shell = user_data;
  CcPanel *panel = cc_shell_get_active_panel (CC_SHELL (shell));
  GtkWidget *window = cc_shell_get_toplevel (CC_SHELL (shell));
  const char *uri = NULL;

  if (panel)
    uri = cc_panel_get_help_uri (panel);

  gtk_show_uri (gtk_widget_get_screen (window),
                uri ? uri : "help:gnome-help/prefs",
                GDK_CURRENT_TIME, NULL);
}

static void
quit_activated (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
{
  GnomeControlCenter *shell = user_data;
  g_object_unref (shell);
}

202
203
static void
application_startup_cb (GApplication       *application,
204
                        GnomeControlCenter *shell)
205
{
Florian Müllner's avatar
Florian Müllner committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  GMenu *menu, *section;
  GAction *action;

  action = G_ACTION (g_simple_action_new ("help", NULL));
  g_action_map_add_action (G_ACTION_MAP (application), action);
  g_signal_connect (action, "activate", G_CALLBACK (help_activated), shell);

  action = G_ACTION (g_simple_action_new ("quit", NULL));
  g_action_map_add_action (G_ACTION_MAP (application), action);
  g_signal_connect (action, "activate", G_CALLBACK (quit_activated), shell);

  menu = g_menu_new ();

  section = g_menu_new ();
  g_menu_append (section, _("Help"), "app.help");
  g_menu_append (section, _("Quit"), "app.quit");

  g_menu_append_section (menu, NULL, G_MENU_MODEL (section));

  gtk_application_set_app_menu (GTK_APPLICATION (application),
                                G_MENU_MODEL (menu));

228
229
230
  gtk_application_add_accelerator (GTK_APPLICATION (application),
                                   "F1", "app.help", NULL);

Florian Müllner's avatar
Florian Müllner committed
231
  /* nothing else to do here, we don't want to show a window before
232
233
   * we've looked at the commandline
   */
234
235
}

Rodrigo Moya's avatar
Rodrigo Moya committed
236
int
Thomas Wood's avatar
Thomas Wood committed
237
main (int argc, char **argv)
Rodrigo Moya's avatar
Rodrigo Moya committed
238
{
Thomas Wood's avatar
Thomas Wood committed
239
  GnomeControlCenter *shell;
240
  GtkApplication *application;
241
  int status;
Thomas Wood's avatar
Thomas Wood committed
242

243
244
245
246
  bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);

247
248
249
250
#ifdef GDK_WINDOWING_X11
  XInitThreads ();
#endif

Thomas Wood's avatar
Thomas Wood committed
251
  gtk_init (&argc, &argv);
252
  cc_shell_log_init ();
Thomas Wood's avatar
Thomas Wood committed
253

254
255
  g_type_ensure (egg_list_box_get_type ());

256
257
258
  /* register a symbolic icon size for use in sidebar lists */
  gtk_icon_size_register ("cc-sidebar-list", 24, 24);

259
260
  notify_init ("gnome-control-center");

Thomas Wood's avatar
Thomas Wood committed
261
262
  shell = gnome_control_center_new ();

263
  /* enforce single instance of this application */
264
265
266
267
268
  application = gtk_application_new ("org.gnome.ControlCenter", G_APPLICATION_HANDLES_COMMAND_LINE);
  g_signal_connect (application, "startup",
                    G_CALLBACK (application_startup_cb), shell);
  g_signal_connect (application, "command-line",
                    G_CALLBACK (application_command_line_cb), shell);
Thomas Wood's avatar
Thomas Wood committed
269

270
  status = g_application_run (G_APPLICATION (application), argc, argv);
Thomas Wood's avatar
Thomas Wood committed
271

272
  g_object_unref (application);
Thomas Wood's avatar
Thomas Wood committed
273

274
  return status;
Thomas Wood's avatar
Thomas Wood committed
275
}