cc-application.c 10.1 KB
Newer Older
Rodrigo Moya's avatar
Rodrigo Moya committed
1
/*
2
 * Copyright © 2013 Red Hat, Inc.
Rodrigo Moya's avatar
Rodrigo Moya committed
3
 *
4 5 6 7
 * This program 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
8
 *
9 10 11 12
 * This program 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
13
 *
14 15 16 17
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
Rodrigo Moya's avatar
Rodrigo Moya committed
18 19 20 21
 */

#include "config.h"

22
#include <stdlib.h>
23

24 25 26
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
Rodrigo Moya's avatar
Rodrigo Moya committed
27

28
#include "cc-application.h"
29
#include "cc-log.h"
30
#include "cc-object-storage.h"
31
#include "cc-panel-loader.h"
32
#include "cc-window.h"
33

34
#if defined(HAVE_WACOM) || defined(HAVE_CHEESE)
35
#include <clutter-gtk/clutter-gtk.h>
36
#endif /* HAVE_WACOM || HAVE_CHEESE */
37

38
struct _CcApplication
39
{
40 41
  GtkApplication  parent;

42 43
  CcShellModel   *model;

44
  CcWindow       *window;
45 46
};

47 48 49 50 51 52 53 54 55 56 57 58
static void cc_application_quit    (GSimpleAction *simple,
                                    GVariant      *parameter,
                                    gpointer       user_data);

static void launch_panel_activated (GSimpleAction *action,
                                    GVariant      *parameter,
                                    gpointer       user_data);

static void help_activated         (GSimpleAction *action,
                                    GVariant      *parameter,
                                    gpointer       user_data);

59
G_DEFINE_TYPE (CcApplication, cc_application, GTK_TYPE_APPLICATION)
60

61
const GOptionEntry all_options[] = {
62
  { "version", 0, 0, G_OPTION_ARG_NONE, NULL, N_("Display version number"), NULL },
63 64 65 66
  { "verbose", 'v', 0, G_OPTION_ARG_NONE, NULL, N_("Enable verbose mode"), NULL },
  { "search", 's', 0, G_OPTION_ARG_STRING, NULL, N_("Search for the string"), "SEARCH" },
  { "list", 'l', 0, G_OPTION_ARG_NONE, NULL, N_("List possible panel names and exit"), NULL },
  { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, NULL, N_("Panel to display"), N_("[PANEL] [ARGUMENT…]") },
67
  { NULL, 0, 0, 0, NULL, NULL, NULL } /* end the list */
68
};
69

70 71 72 73 74 75
static const GActionEntry cc_app_actions[] = {
  { "launch-panel", launch_panel_activated, "(sav)", NULL, NULL, { 0 } },
  { "help", help_activated, NULL, NULL, NULL, { 0 } },
  { "quit", cc_application_quit, NULL, NULL, NULL, { 0 } }
};

76 77 78 79 80 81 82 83 84 85
static void
help_activated (GSimpleAction *action,
                GVariant      *parameter,
                gpointer       user_data)
{
  CcApplication *self = CC_APPLICATION (user_data);
  CcPanel *panel;
  GtkWidget *window;
  const char *uri = NULL;

86
  panel = cc_shell_get_active_panel (CC_SHELL (self->window));
87 88 89
  if (panel)
    uri = cc_panel_get_help_uri (panel);

90
  window = cc_shell_get_toplevel (CC_SHELL (self->window));
91 92 93 94
  gtk_show_uri_on_window (GTK_WINDOW (window),
                          uri ? uri : "help:gnome-help/prefs",
                          GDK_CURRENT_TIME,
                          NULL);
95 96
}

97 98 99 100 101 102
static void
launch_panel_activated (GSimpleAction *action,
                        GVariant      *parameter,
                        gpointer       user_data)
{
  CcApplication *self = CC_APPLICATION (user_data);
103 104
  g_autoptr (GVariant) parameters = NULL;
  g_autoptr (GError) error = NULL;
105 106 107
  gchar *panel_id;

  g_variant_get (parameter, "(&s@av)", &panel_id, &parameters);
108

109
  g_debug ("gnome-control-center: 'launch-panel' activated for panel '%s' with %"G_GSIZE_FORMAT" arguments",
110 111 112
           panel_id,
           g_variant_n_children (parameters));

113
  if (!cc_shell_set_active_panel_from_id (CC_SHELL (self->window), panel_id, parameters, &error))
114
    g_warning ("Failed to activate the '%s' panel: %s", panel_id, error->message);
115 116 117

  /* Now present the window */
  g_application_activate (G_APPLICATION (self));
118 119
}

120
static gint
121 122
cc_application_handle_local_options (GApplication *application,
                                     GVariantDict *options)
123
{
124 125 126 127 128 129
  if (g_variant_dict_contains (options, "version"))
    {
      g_print ("%s %s\n", PACKAGE, VERSION);
      return 0;
    }

130 131
  if (g_variant_dict_contains (options, "list"))
    {
132 133
      g_autoptr(GList) panels = NULL;
      g_autoptr(GList) l = NULL;
134 135 136 137 138 139 140 141 142 143

      panels = cc_panel_loader_get_panels ();

      g_print ("%s\n", _("Available panels:"));
      for (l = panels; l != NULL; l = l->next)
        g_print ("\t%s\n", (char *) l->data);

      return 0;
    }

144 145 146
  return -1;
}

147
static int
148
cc_application_command_line (GApplication            *application,
149
                             GApplicationCommandLine *command_line)
150
{
151 152
  CcApplication *self;
  g_autofree GStrv start_panels = NULL;
153
  GVariantDict *options;
154
  int retval = 0;
155 156
  char *search_str;
  gboolean debug;
157

158
  self = CC_APPLICATION (application);
159
  options = g_application_command_line_get_options_dict (command_line);
160

161
  debug = g_variant_dict_contains (options, "verbose");
162 163 164

  if (debug)
    cc_log_init ();
165

166
  gtk_window_present (GTK_WINDOW (self->window));
167

168
  if (g_variant_dict_lookup (options, "search", "&s", &search_str))
169
    {
170
      cc_window_set_search_item (self->window, search_str);
171
    }
172
  else if (g_variant_dict_lookup (options, G_OPTION_REMAINING, "^a&ay", &start_panels))
173
    {
174
      const char *start_id;
175
      GError *err = NULL;
176
      GVariant *parameters;
177
      GVariantBuilder builder;
178
      int i;
179

180
      g_return_val_if_fail (start_panels[0] != NULL, 1);
181
      start_id = start_panels[0];
182

183
      if (start_panels[1])
184
        g_debug ("Extra argument: %s", start_panels[1]);
185
      else
186
        g_debug ("No extra argument");
187

188 189
      g_variant_builder_init (&builder, G_VARIANT_TYPE ("av"));

190
      for (i = 1; start_panels[i] != NULL; i++)
191 192
        g_variant_builder_add (&builder, "v", g_variant_new_string (start_panels[i]));
      parameters = g_variant_builder_end (&builder);
193
      if (!cc_shell_set_active_panel_from_id (CC_SHELL (self->window), start_id, parameters, &err))
194
        {
195 196
          g_warning ("Could not load setting panel \"%s\": %s", start_id,
                     (err) ? err->message : "Unknown error");
197
          retval = 1;
198

199
          if (err)
200
            g_clear_error (&err);
201 202
        }
    }
203

204
  return retval;
205 206
}

207
static void
208
cc_application_quit (GSimpleAction *simple,
209 210
                     GVariant      *parameter,
                     gpointer       user_data)
211
{
212
  CcApplication *self = CC_APPLICATION (user_data);
213

214
  gtk_widget_destroy (GTK_WIDGET (self->window));
215 216
}

217

218
static void
219
cc_application_activate (GApplication *application)
220
{
221 222
  CcApplication *self = CC_APPLICATION (application);

223
  gtk_window_present (GTK_WINDOW (self->window));
224 225
}

226
static void
227
cc_application_startup (GApplication *application)
228
{
229 230
  CcApplication *self = CC_APPLICATION (application);
  GMenu *section;
231
  GMenu *menu;
232
  const gchar *help_accels[] = { "F1", NULL };
233

234 235 236 237 238
  g_action_map_add_action_entries (G_ACTION_MAP (self),
                                   cc_app_actions,
                                   G_N_ELEMENTS (cc_app_actions),
                                   self);

239 240
  G_APPLICATION_CLASS (cc_application_parent_class)->startup (application);

241
#if defined(HAVE_WACOM) || defined(HAVE_CHEESE)
242 243 244 245 246
  if (gtk_clutter_init (NULL, NULL) != CLUTTER_INIT_SUCCESS)
    {
      g_critical ("Unable to initialize Clutter");
      return;
    }
247
#endif /* HAVE_WACOM || HAVE_CHEESE */
248 249 250 251

  menu = g_menu_new ();

  section = g_menu_new ();
252
  g_menu_append (section, _("Keyboard Shortcuts"), "win.show-help-overlay");
253 254 255 256 257 258 259 260
  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));

261 262
  gtk_application_set_accels_for_action (GTK_APPLICATION (application),
                                         "app.help", help_accels);
263

264 265
  self->model = cc_shell_model_new ();
  self->window = cc_window_new (GTK_APPLICATION (application), self->model);
266 267
}

268 269 270 271 272 273 274 275 276
static void
cc_application_finalize (GObject *object)
{
  /* Destroy the object storage cache when finalizing */
  cc_object_storage_destroy ();

  G_OBJECT_CLASS (cc_application_parent_class)->finalize (object);
}

277
static GObject *
278 279
cc_application_constructor (GType                  type,
                            guint                  n_construct_params,
280
                            GObjectConstructParam *construct_params)
Rodrigo Moya's avatar
Rodrigo Moya committed
281
{
282
  static GObject *self = NULL;
283

284 285 286 287 288 289 290 291
  if (self == NULL)
    {
      self = G_OBJECT_CLASS (cc_application_parent_class)->constructor (type,
                                                                        n_construct_params,
                                                                        construct_params);
      g_object_add_weak_pointer (self, (gpointer) &self);
      return self;
    }
292

293 294
  return g_object_ref (self);
}
295

296
static void
297
cc_application_class_init (CcApplicationClass *klass)
298
{
299 300
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);
301

302
  object_class->finalize = cc_application_finalize;
303 304 305 306
  object_class->constructor = cc_application_constructor;
  application_class->activate = cc_application_activate;
  application_class->startup = cc_application_startup;
  application_class->command_line = cc_application_command_line;
307
  application_class->handle_local_options = cc_application_handle_local_options;
308
}
309

310 311 312
static void
cc_application_init (CcApplication *self)
{
313 314
  cc_object_storage_initialize ();

315 316
  g_application_add_main_option_entries (G_APPLICATION (self), all_options);
}
317

318 319 320 321 322 323 324
GtkApplication *
cc_application_new (void)
{
  return g_object_new (CC_TYPE_APPLICATION,
                       "application-id", "org.gnome.ControlCenter",
                       "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
                       NULL);
325
}
326 327 328 329 330 331 332 333

CcShellModel *
cc_application_get_model (CcApplication *self)
{
  g_return_val_if_fail (CC_IS_APPLICATION (self), NULL);

  return self->model;
}