summaryrefslogtreecommitdiff
path: root/shell/control-center.c
blob: 06b0727b441462161bea1aade98380facebd4e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2009, 2010 Intel, Inc.
 * Copyright (c) 2010 Red Hat, Inc.
 *
 * 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.
 *
 * 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.
 *
 * 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
 *
 * Author: Thomas Wood <thos@gnome.org>
 */

#include "config.h"

#include <glib/gi18n.h>
#include <stdlib.h>

#include "gnome-control-center.h"

#include <gtk/gtk.h>
#include <string.h>

#include "cc-shell-log.h"

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;
static gboolean verbose = FALSE;
static gboolean show_help = FALSE;

const GOptionEntry all_options[] = {
  { "version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
  { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, N_("Enable verbose mode"), NULL },
  { "overview", 'o', 0, G_OPTION_ARG_NONE, &show_overview, N_("Show the overview"), NULL },
  { "help", '?', 0, G_OPTION_ARG_NONE, &show_help, N_("Show help options"), NULL },
  { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &start_panels, N_("Panel to display"), NULL },
  { NULL } /* end the list */
};

static int
application_command_line_cb (GApplication  *application,
                             GApplicationCommandLine  *command_line,
                             GnomeControlCenter      *shell)
{
  int argc;
  char **argv;
  int retval = 0;
  GOptionContext *context;
  GError *error = NULL;

  verbose = FALSE;
  show_overview = FALSE;
  show_help = FALSE;
  start_panels = NULL;

  argv = g_application_command_line_get_arguments (command_line, &argc);

  context = g_option_context_new (N_("- System Settings"));
  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));
  g_option_context_set_help_enabled (context, FALSE);

  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);
      return 1;
    }

  if (show_help)
    {
      gchar *help;

      help = g_option_context_get_help (context, FALSE, NULL);
      g_print (help);
      g_free (help);
      g_option_context_free (context);
      return 0;
    }

  g_option_context_free (context);

  cc_shell_log_set_debug (verbose);

  gnome_control_center_show (shell, GTK_APPLICATION (application));

  if (show_overview)
    {
      gnome_control_center_set_overview_page (shell);
    }
  else if (start_panels != NULL && start_panels[0] != NULL)
    {
      const char *start_id;
      GError *err = NULL;

      start_id = start_panels[0];

      if (!cc_shell_set_active_panel_from_id (CC_SHELL (shell), start_id, &err))
        {
          g_warning ("Could not load setting panel \"%s\": %s", start_id,
                     (err) ? err->message : "Unknown error");
          retval = 1;
          if (err)
            {
              g_error_free (err);
              err = NULL;
            }
        }
    }

  gnome_control_center_present (shell);
  gdk_notify_startup_complete ();

  g_strfreev (argv);
  if (start_panels != NULL)
    {
      g_strfreev (start_panels);
      start_panels = NULL;
    }
  show_overview = FALSE;

  return retval;
}

static void
application_startup_cb (GApplication       *application,
                        GnomeControlCenter *shell)
{
  /* nothing to do here, we don't want to show a window before
   * we've looked at the commandline
   */
}

int
main (int argc, char **argv)
{
  GnomeControlCenter *shell;
  GtkApplication *application;
  int status;

  bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);


  g_thread_init (NULL);
  gtk_init (&argc, &argv);
  cc_shell_log_init ();

  shell = gnome_control_center_new ();

  /* enforce single instance of this application */
  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);

  status = g_application_run (G_APPLICATION (application), argc, argv);

  g_object_unref (application);

  return status;
}