summaryrefslogtreecommitdiff
path: root/shell/cc-panel-loader.c
blob: df3a8020ca045d40616b91f0e00bce777870fc3b (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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
 * Copyright (c) 2012 Giovanni Campagna <scampa.giovanni@gmail.com>
 *
 * 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 <string.h>
#include <gio/gdesktopappinfo.h>

#include "cc-panel-loader.h"

#ifndef CC_PANEL_LOADER_NO_GTYPES

/* Extension points */
extern GType cc_background_panel_get_type (void);
#ifdef BUILD_BLUETOOTH
extern GType cc_bluetooth_panel_get_type (void);
#endif /* BUILD_BLUETOOTH */
extern GType cc_color_panel_get_type (void);
extern GType cc_date_time_panel_get_type (void);
extern GType cc_display_panel_get_type (void);
extern GType cc_info_overview_panel_get_type (void);
extern GType cc_info_default_apps_panel_get_type (void);
extern GType cc_info_removable_media_panel_get_type (void);
extern GType cc_keyboard_panel_get_type (void);
extern GType cc_mouse_panel_get_type (void);
#ifdef BUILD_NETWORK
extern GType cc_network_panel_get_type (void);
extern GType cc_wifi_panel_get_type (void);
#endif /* BUILD_NETWORK */
extern GType cc_notifications_panel_get_type (void);
extern GType cc_goa_panel_get_type (void);
extern GType cc_power_panel_get_type (void);
extern GType cc_printers_panel_get_type (void);
extern GType cc_privacy_panel_get_type (void);
extern GType cc_region_panel_get_type (void);
extern GType cc_search_panel_get_type (void);
extern GType cc_sharing_panel_get_type (void);
extern GType cc_sound_panel_get_type (void);
extern GType cc_ua_panel_get_type (void);
extern GType cc_user_panel_get_type (void);
#ifdef BUILD_WACOM
extern GType cc_wacom_panel_get_type (void);
#endif /* BUILD_WACOM */

#define PANEL_TYPE(name, get_type) { name, get_type }

#else /* CC_PANEL_LOADER_NO_GTYPES */

#define PANEL_TYPE(name, get_type) { name }

#endif

static struct {
  const char *name;
#ifndef CC_PANEL_LOADER_NO_GTYPES
  GType (*get_type)(void);
#endif
} all_panels[] = {
  PANEL_TYPE("background",       cc_background_panel_get_type   ),
#ifdef BUILD_BLUETOOTH
  PANEL_TYPE("bluetooth",        cc_bluetooth_panel_get_type    ),
#endif
  PANEL_TYPE("color",            cc_color_panel_get_type        ),
  PANEL_TYPE("datetime",         cc_date_time_panel_get_type    ),
  PANEL_TYPE("display",          cc_display_panel_get_type      ),
  PANEL_TYPE("info-overview",    cc_info_overview_panel_get_type),
  PANEL_TYPE("default-apps",     cc_info_default_apps_panel_get_type),
  PANEL_TYPE("removable-media",  cc_info_removable_media_panel_get_type),
  PANEL_TYPE("keyboard",         cc_keyboard_panel_get_type     ),
  PANEL_TYPE("mouse",            cc_mouse_panel_get_type        ),
#ifdef BUILD_NETWORK
  PANEL_TYPE("network",          cc_network_panel_get_type      ),
  PANEL_TYPE("wifi",             cc_wifi_panel_get_type         ),
#endif
  PANEL_TYPE("notifications",    cc_notifications_panel_get_type),
  PANEL_TYPE("online-accounts",  cc_goa_panel_get_type          ),
  PANEL_TYPE("power",            cc_power_panel_get_type        ),
  PANEL_TYPE("printers",         cc_printers_panel_get_type     ),
  PANEL_TYPE("privacy",          cc_privacy_panel_get_type      ),
  PANEL_TYPE("region",           cc_region_panel_get_type       ),
  PANEL_TYPE("search",           cc_search_panel_get_type       ),
  PANEL_TYPE("sharing",          cc_sharing_panel_get_type      ),
  PANEL_TYPE("sound",            cc_sound_panel_get_type        ),
  PANEL_TYPE("universal-access", cc_ua_panel_get_type           ),
  PANEL_TYPE("user-accounts",    cc_user_panel_get_type         ),
#ifdef BUILD_WACOM
  PANEL_TYPE("wacom",            cc_wacom_panel_get_type        ),
#endif
};

GList *
cc_panel_loader_get_panels (void)
{
  GList *l = NULL;
  guint i;

  for (i = 0; i < G_N_ELEMENTS (all_panels); i++)
    l = g_list_prepend (l, (gpointer) all_panels[i].name);

  return g_list_reverse (l);
}

static int
parse_categories (GDesktopAppInfo *app)
{
  g_auto(GStrv) split = NULL;
  const gchar *categories;
  gint retval;

  categories = g_desktop_app_info_get_categories (app);
  split = g_strsplit (categories, ";", -1);

  retval = -1;

#define const_strv(s) ((const gchar* const*) s)

  if (g_strv_contains (const_strv (split), "X-GNOME-ConnectivitySettings"))
    retval = CC_CATEGORY_CONNECTIVITY;
  else if (g_strv_contains (const_strv (split), "X-GNOME-PersonalizationSettings"))
    retval = CC_CATEGORY_PERSONALIZATION;
  else if (g_strv_contains (const_strv (split), "X-GNOME-AccountSettings"))
    retval = CC_CATEGORY_ACCOUNT;
  else if (g_strv_contains (const_strv (split), "X-GNOME-DevicesSettings"))
    retval = CC_CATEGORY_DEVICES;
  else if (g_strv_contains (const_strv (split), "X-GNOME-DetailsSettings"))
    retval = CC_CATEGORY_DETAILS;
  else if (g_strv_contains (const_strv (split), "HardwareSettings"))
    retval = CC_CATEGORY_HARDWARE;

#undef const_strv

  if (retval < 0)
    {
      g_warning ("Invalid categories %s for panel %s",
                 categories, g_app_info_get_id (G_APP_INFO (app)));
    }

  return retval;
}

void
cc_panel_loader_fill_model (CcShellModel *model)
{
  guint i;

  for (i = 0; i < G_N_ELEMENTS (all_panels); i++)
    {
      g_autoptr (GDesktopAppInfo) app;
      g_autofree gchar *desktop_name = NULL;
      gint category;

      desktop_name = g_strconcat ("gnome-", all_panels[i].name, "-panel.desktop", NULL);
      app = g_desktop_app_info_new (desktop_name);

      if (!app)
        {
          g_warning ("Ignoring broken panel %s (missing desktop file)", all_panels[i].name);
          continue;
        }

      category = parse_categories (app);
      if (G_UNLIKELY (category < 0))
        continue;

      /* Consult OnlyShowIn/NotShowIn for desktop environments */
      if (!g_desktop_app_info_get_show_in (app, NULL))
        continue;

      cc_shell_model_add_item (model, category, G_APP_INFO (app), all_panels[i].name);
    }
}

#ifndef CC_PANEL_LOADER_NO_GTYPES

static GHashTable *panel_types;

static void
ensure_panel_types (void)
{
  int i;

  if (G_LIKELY (panel_types != NULL))
    return;

  panel_types = g_hash_table_new (g_str_hash, g_str_equal);
  for (i = 0; i < G_N_ELEMENTS (all_panels); i++)
    g_hash_table_insert (panel_types, (char*)all_panels[i].name, all_panels[i].get_type);
}

CcPanel *
cc_panel_loader_load_by_name (CcShell     *shell,
                              const char  *name,
                              GVariant    *parameters)
{
  GType (*get_type) (void);

  ensure_panel_types ();

  get_type = g_hash_table_lookup (panel_types, name);
  g_return_val_if_fail (get_type != NULL, NULL);

  return g_object_new (get_type (),
                       "shell", shell,
                       "parameters", parameters,
                       NULL);
}

#endif /* CC_PANEL_LOADER_NO_GTYPES */