~ubuntu-branches/ubuntu/saucy/indicator-appmenu/saucy-updates

« back to all changes in this revision

Viewing changes to src/hudindicatorsource.c

  • Committer: Package Import Robot
  • Author(s): Automatic PS uploader, Mathieu Trudel-Lapierre, Automatic PS uploader
  • Date: 2013-02-20 09:41:54 UTC
  • mfrom: (1.1.40)
  • Revision ID: package-import@ubuntu.com-20130220094154-zrxsx0vgay436wyt
Tags: 13.01.0daily13.02.20-0ubuntu1
[ Mathieu Trudel-Lapierre ]
* Artificially bump upstream major version to please hud.

[ Automatic PS uploader ]
* Automatic snapshot from revision 234

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3, as
6
 
 * published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
 
 * PURPOSE.  See the GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License along
14
 
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authors: Ryan Lortie <desrt@desrt.ca>
17
 
 *          Ted Gould <ted@canonical.com>
18
 
 */
19
 
 
20
 
#include "hudindicatorsource.h"
21
 
 
22
 
#include <glib/gi18n.h>
23
 
#include <gio/gio.h>
24
 
 
25
 
#include "hudsettings.h"
26
 
#include "huddbusmenucollector.h"
27
 
#include "hudmenumodelcollector.h"
28
 
#include "hudsource.h"
29
 
 
30
 
/**
31
 
 * SECTION:hudindicatorsource
32
 
 * @title:HudIndicatorSource
33
 
 * @short_description: a #HudSource to search through the menus of
34
 
 *   indicators
35
 
 *
36
 
 * #HudIndicatorSource searches through the menu items of the
37
 
 * indicators.
38
 
 **/
39
 
 
40
 
/**
41
 
 * HudIndicatorSource:
42
 
 *
43
 
 * This is an opaque structure type.
44
 
 **/
45
 
 
46
 
typedef struct
47
 
{
48
 
  const gchar *dbus_name;
49
 
  const gchar *dbus_menu_path;
50
 
  const gchar *indicator_name;
51
 
  const gchar *user_visible_name;
52
 
  const gchar *icon;
53
 
  gboolean     uses_gmenumodel;
54
 
} IndicatorInfo;
55
 
 
56
 
static const IndicatorInfo indicator_info[] = {
57
 
  {
58
 
    .dbus_name         = "com.canonical.indicator.datetime",
59
 
    .dbus_menu_path    = "/com/canonical/indicator/datetime/menu",
60
 
    .indicator_name    = "indicator-datetime",
61
 
    .user_visible_name = N_("Date"),
62
 
    .icon              = "office-calendar"
63
 
  },
64
 
  {
65
 
    .dbus_name         = "com.canonical.indicator.session",
66
 
    .dbus_menu_path    = "/com/canonical/indicator/session/menu",
67
 
    .indicator_name    = "indicator-session-device",
68
 
    .user_visible_name = N_("Device"),
69
 
    .icon              = "system-devices-panel"
70
 
  },
71
 
  {
72
 
    .dbus_name         = "com.canonical.indicator.session",
73
 
    .dbus_menu_path    = "/com/canonical/indicator/users/menu",
74
 
    .indicator_name    = "indicator-session-user",
75
 
    .user_visible_name = N_("Users"),
76
 
    .icon              = "avatar-default"
77
 
  },
78
 
  {
79
 
    .dbus_name         = "com.canonical.indicator.sound",
80
 
    .dbus_menu_path    = "/com/canonical/indicator/sound/menu",
81
 
    .indicator_name    = "indicator-sound",
82
 
    .user_visible_name = N_("Sound"),
83
 
    .icon              = "audio-volume-high-panel"
84
 
  },
85
 
  {
86
 
    .dbus_name         = "com.canonical.indicator.messages",
87
 
    .dbus_menu_path    = "/com/canonical/indicator/messages/menu",
88
 
    .indicator_name    = "indicator-messages",
89
 
    .user_visible_name = N_("Messages"),
90
 
    .icon              = "indicator-messages",
91
 
    .uses_gmenumodel   = TRUE
92
 
  }
93
 
};
94
 
 
95
 
typedef struct
96
 
{
97
 
  const IndicatorInfo *info;
98
 
  HudIndicatorSource *source;
99
 
  HudSource *collector;
100
 
} HudIndicatorSourceIndicator;
101
 
 
102
 
struct _HudIndicatorSource
103
 
{
104
 
  GObject parent_instance;
105
 
 
106
 
  HudIndicatorSourceIndicator *indicators;
107
 
  gint n_indicators;
108
 
  gint use_count;
109
 
};
110
 
 
111
 
typedef GObjectClass HudIndicatorSourceClass;
112
 
 
113
 
static void hud_indicator_source_iface_init (HudSourceInterface *iface);
114
 
G_DEFINE_TYPE_WITH_CODE (HudIndicatorSource, hud_indicator_source, G_TYPE_OBJECT,
115
 
                         G_IMPLEMENT_INTERFACE (HUD_TYPE_SOURCE, hud_indicator_source_iface_init))
116
 
 
117
 
static void
118
 
hud_indicator_source_use (HudSource *hud_source)
119
 
{
120
 
  HudIndicatorSource *source = HUD_INDICATOR_SOURCE (hud_source);
121
 
  gint i;
122
 
 
123
 
  if (source->use_count == 0)
124
 
    {
125
 
      for (i = 0; i < source->n_indicators; i++)
126
 
        if (source->indicators[i].collector)
127
 
          hud_source_use (source->indicators[i].collector);
128
 
    }
129
 
 
130
 
  source->use_count++;
131
 
}
132
 
 
133
 
static void
134
 
hud_indicator_source_unuse (HudSource *hud_source)
135
 
{
136
 
  HudIndicatorSource *source = HUD_INDICATOR_SOURCE (hud_source);
137
 
  gint i;
138
 
 
139
 
  g_return_if_fail (source->use_count > 0);
140
 
 
141
 
  source->use_count--;
142
 
 
143
 
  if (source->use_count == 0)
144
 
    {
145
 
      for (i = 0; i < source->n_indicators; i++)
146
 
        if (source->indicators[i].collector)
147
 
          hud_source_unuse (source->indicators[i].collector);
148
 
    }
149
 
}
150
 
 
151
 
static void
152
 
hud_indicator_source_search (HudSource    *hud_source,
153
 
                             GPtrArray    *results_array,
154
 
                             HudTokenList *search_string)
155
 
{
156
 
  HudIndicatorSource *source = HUD_INDICATOR_SOURCE (hud_source);
157
 
  gint i;
158
 
 
159
 
  for (i = 0; i < source->n_indicators; i++)
160
 
    if (source->indicators[i].collector)
161
 
      hud_source_search (source->indicators[i].collector, results_array, search_string);
162
 
}
163
 
 
164
 
static void
165
 
hud_indicator_source_finalize (GObject *object)
166
 
{
167
 
  g_assert_not_reached ();
168
 
}
169
 
 
170
 
static void
171
 
hud_indicator_source_collector_changed (HudSource *source,
172
 
                                        gpointer   user_data)
173
 
{
174
 
  HudIndicatorSourceIndicator *indicator = user_data;
175
 
 
176
 
  hud_source_changed (HUD_SOURCE (indicator->source));
177
 
}
178
 
 
179
 
static void
180
 
hud_indicator_source_name_appeared (GDBusConnection *connection,
181
 
                                    const gchar     *name,
182
 
                                    const gchar     *name_owner,
183
 
                                    gpointer         user_data)
184
 
{
185
 
  HudIndicatorSourceIndicator *indicator = user_data;
186
 
 
187
 
  if (indicator->info->uses_gmenumodel)
188
 
    {
189
 
      HudMenuModelCollector *collector;
190
 
 
191
 
      collector = hud_menu_model_collector_new_for_endpoint (indicator->info->indicator_name,
192
 
                                                             _(indicator->info->user_visible_name),
193
 
                                                             indicator->info->icon,
194
 
                                                             hud_settings.indicator_penalty,
195
 
                                                             name_owner,
196
 
                                                             indicator->info->dbus_menu_path);
197
 
 
198
 
      indicator->collector = HUD_SOURCE (collector);
199
 
    }
200
 
  else
201
 
    {
202
 
      HudDbusmenuCollector *collector;
203
 
 
204
 
      collector = hud_dbusmenu_collector_new_for_endpoint (indicator->info->indicator_name,
205
 
                                                           _(indicator->info->user_visible_name),
206
 
                                                           indicator->info->icon,
207
 
                                                           hud_settings.indicator_penalty,
208
 
                                                           name_owner, indicator->info->dbus_menu_path);
209
 
      indicator->collector = HUD_SOURCE (collector);
210
 
    }
211
 
 
212
 
  g_signal_connect (indicator->collector, "changed",
213
 
                    G_CALLBACK (hud_indicator_source_collector_changed), indicator);
214
 
 
215
 
  /* Set initial use count on new indicator if query is active. */
216
 
  if (indicator->source->use_count)
217
 
    hud_source_use (indicator->collector);
218
 
 
219
 
  hud_source_changed (HUD_SOURCE (indicator->source));
220
 
}
221
 
 
222
 
static void
223
 
hud_indicator_source_name_vanished (GDBusConnection *connection,
224
 
                                    const gchar     *name,
225
 
                                    gpointer         user_data)
226
 
{
227
 
  HudIndicatorSourceIndicator *indicator = user_data;
228
 
 
229
 
  if (indicator->collector)
230
 
    {
231
 
      g_signal_handlers_disconnect_by_func (indicator->collector, hud_indicator_source_collector_changed, indicator);
232
 
      /* Drop our use count on dying indicator (if any) */
233
 
      if (indicator->source->use_count)
234
 
        hud_source_unuse (indicator->collector);
235
 
      g_clear_object (&indicator->collector);
236
 
    }
237
 
 
238
 
  hud_source_changed (HUD_SOURCE (indicator->source));
239
 
}
240
 
 
241
 
static void
242
 
hud_indicator_source_init (HudIndicatorSource *source)
243
 
{
244
 
  gint i;
245
 
 
246
 
  source->n_indicators = G_N_ELEMENTS (indicator_info);
247
 
  source->indicators = g_new0 (HudIndicatorSourceIndicator, source->n_indicators);
248
 
 
249
 
  for (i = 0; i < source->n_indicators; i++)
250
 
    {
251
 
      HudIndicatorSourceIndicator *indicator = &source->indicators[i];
252
 
 
253
 
      indicator->info = &indicator_info[i];
254
 
      indicator->source = source;
255
 
 
256
 
      g_bus_watch_name (G_BUS_TYPE_SESSION, indicator->info->dbus_name, G_BUS_NAME_WATCHER_FLAGS_NONE,
257
 
                        hud_indicator_source_name_appeared, hud_indicator_source_name_vanished, indicator, NULL);
258
 
    }
259
 
}
260
 
 
261
 
static void
262
 
hud_indicator_source_iface_init (HudSourceInterface *iface)
263
 
{
264
 
  iface->use = hud_indicator_source_use;
265
 
  iface->unuse = hud_indicator_source_unuse;
266
 
  iface->search = hud_indicator_source_search;
267
 
}
268
 
 
269
 
static void
270
 
hud_indicator_source_class_init (HudIndicatorSourceClass *class)
271
 
{
272
 
  class->finalize = hud_indicator_source_finalize;
273
 
}
274
 
 
275
 
/**
276
 
 * hud_indicator_source_new:
277
 
 *
278
 
 * Creates a #HudIndicatorSource.
279
 
 *
280
 
 * Returns: a new #HudIndicatorSource
281
 
 **/
282
 
HudIndicatorSource *
283
 
hud_indicator_source_new (void)
284
 
{
285
 
  return g_object_new (HUD_TYPE_INDICATOR_SOURCE, NULL);
286
 
}