summaryrefslogtreecommitdiffstats
path: root/shell/cc-application.c
blob: a51568bae95e9fb023099a873d16e5612463ad71 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright © 2013 Red Hat, Inc.
 *
 * 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.
 *
 * 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.
 *
 * 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.
 */

#include "config.h"

#include <stdlib.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <adwaita.h>

#include "cc-application.h"
#include "cc-log.h"
#include "cc-object-storage.h"
#include "cc-panel-loader.h"
#include "cc-window.h"

struct _CcApplication
{
  AdwApplication  parent;

  CcShellModel   *model;

  CcWindow       *window;
};

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);

G_DEFINE_TYPE (CcApplication, cc_application, ADW_TYPE_APPLICATION)

const GOptionEntry all_options[] = {
  { "version", 0, 0, G_OPTION_ARG_NONE, NULL, N_("Display version number"), NULL },
  { "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…]") },
  { NULL, 0, 0, 0, NULL, NULL, NULL } /* end the list */
};

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 } }
};

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;

  panel = cc_shell_get_active_panel (CC_SHELL (self->window));
  if (panel)
    uri = cc_panel_get_help_uri (panel);

  window = cc_shell_get_toplevel (CC_SHELL (self->window));
  gtk_show_uri (GTK_WINDOW (window),
                uri ? uri : "help:gnome-help/prefs",
                GDK_CURRENT_TIME);
}

static void
launch_panel_activated (GSimpleAction *action,
                        GVariant      *parameter,
                        gpointer       user_data)
{
  CcApplication *self = CC_APPLICATION (user_data);
  g_autoptr(GVariant) parameters = NULL;
  g_autoptr(GError) error = NULL;
  gchar *panel_id;

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

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

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

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

static gint
cc_application_handle_local_options (GApplication *application,
                                     GVariantDict *options)
{
  if (g_variant_dict_contains (options, "version"))
    {
      g_print ("%s %s\n", PACKAGE, VERSION);
      return 0;
    }

  if (g_variant_dict_contains (options, "list"))
    {
      cc_panel_loader_list_panels ();
      return 0;
    }

  return -1;
}

static int
cc_application_command_line (GApplication            *application,
                             GApplicationCommandLine *command_line)
{
  CcApplication *self;
  g_autofree GStrv start_panels = NULL;
  GVariantDict *options;
  int retval = 0;
  char *search_str;
  gboolean debug;

  self = CC_APPLICATION (application);
  options = g_application_command_line_get_options_dict (command_line);

  debug = g_variant_dict_contains (options, "verbose");

  if (debug)
    cc_log_init ();

  gtk_window_present (GTK_WINDOW (self->window));

  if (g_variant_dict_lookup (options, "search", "&s", &search_str))
    {
      cc_window_set_search_item (self->window, search_str);
    }
  else if (g_variant_dict_lookup (options, G_OPTION_REMAINING, "^a&ay", &start_panels))
    {
      const char *start_id;
      GError *err = NULL;
      GVariant *parameters;
      GVariantBuilder builder;
      int i;

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

      if (start_panels[1])
        g_debug ("Extra argument: %s", start_panels[1]);
      else
        g_debug ("No extra argument");

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

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

          if (err)
            g_clear_error (&err);
        }
    }

  return retval;
}

static void
cc_application_quit (GSimpleAction *simple,
                     GVariant      *parameter,
                     gpointer       user_data)
{
  CcApplication *self = CC_APPLICATION (user_data);

  gtk_window_destroy (GTK_WINDOW (self->window));
}


static void
cc_application_activate (GApplication *application)
{
  CcApplication *self = CC_APPLICATION (application);

  gtk_window_present (GTK_WINDOW (self->window));
}

static void
cc_application_startup (GApplication *application)
{
  CcApplication *self = CC_APPLICATION (application);
  const gchar *help_accels[] = { "F1", NULL };
  g_autoptr(GtkCssProvider) provider = NULL;

  g_action_map_add_action_entries (G_ACTION_MAP (self),
                                   cc_app_actions,
                                   G_N_ELEMENTS (cc_app_actions),
                                   self);

  G_APPLICATION_CLASS (cc_application_parent_class)->startup (application);

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

  self->model = cc_shell_model_new ();
  self->window = cc_window_new (GTK_APPLICATION (application), self->model);

  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_resource (provider, "/org/gnome/Settings/gtk/style.css");
  gtk_style_context_add_provider_for_display (gdk_display_get_default (),
                                              GTK_STYLE_PROVIDER (provider),
                                              GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}

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

static GObject *
cc_application_constructor (GType                  type,
                            guint                  n_construct_params,
                            GObjectConstructParam *construct_params)
{
  static GObject *self = NULL;

  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;
    }

  return g_object_ref (self);
}

static void
cc_application_class_init (CcApplicationClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GApplicationClass *application_class = G_APPLICATION_CLASS (klass);

  object_class->finalize = cc_application_finalize;
  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;
  application_class->handle_local_options = cc_application_handle_local_options;
}

static void
cc_application_init (CcApplication *self)
{
  cc_object_storage_initialize ();

  g_application_add_main_option_entries (G_APPLICATION (self), all_options);
}

GtkApplication *
cc_application_new (void)
{
  return g_object_new (CC_TYPE_APPLICATION,
                       "application-id", "org.gnome.Settings",
                       "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
                       NULL);
}

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

  return self->model;
}