~larsu/indicator-messages/set-status-return-value

« back to all changes in this revision

Viewing changes to src/im-desktop-menu.c

  • Committer: Tarmac
  • Author(s): Lars Uebernickel
  • Date: 2013-08-27 14:28:43 UTC
  • mfrom: (362.2.3 trunk)
  • Revision ID: tarmac-20130827142843-ygdaown2ztf60hdp
Fix bug #1216843

Remove all sources when an applications quits and don't show sources with a count of 0. Fixes: https://bugs.launchpad.net/bugs/1216843.

Approved by Charles Kerr, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
}
107
107
 
108
108
static void
109
 
im_desktop_menu_source_added (ImApplicationList *applist,
110
 
                              const gchar       *app_id,
111
 
                              const gchar       *source_id,
112
 
                              const gchar       *label,
113
 
                              const gchar       *icon,
114
 
                              gpointer           user_data)
 
109
im_desktop_menu_source_section_insert_source (GMenu       *source_section,
 
110
                                              const gchar *source_id,
 
111
                                              const gchar *label,
 
112
                                              const gchar *icon,
 
113
                                              gint         pos)
115
114
{
116
 
  ImDesktopMenu *menu = user_data;
117
 
  GMenu *source_section;
118
115
  GMenuItem *item;
119
116
  gchar *action;
120
117
 
121
 
  source_section = g_hash_table_lookup (menu->source_sections, app_id);
122
 
  g_return_if_fail (source_section != NULL);
123
 
 
124
118
  action = g_strconcat ("src.", source_id, NULL);
125
119
  item = g_menu_item_new (label, NULL);
126
 
  g_menu_item_set_action_and_target_value(item, action, NULL);
 
120
  g_menu_item_set_action_and_target_value (item, action, NULL);
127
121
  g_menu_item_set_attribute (item, "x-canonical-type", "s", "com.canonical.indicator.messages.source");
128
122
  if (icon && *icon)
129
123
    g_menu_item_set_attribute (item, "icon", "s", icon);
130
124
 
131
 
  g_menu_append_item (source_section, item);
 
125
  if (pos >= 0)
 
126
    g_menu_insert_item (source_section, pos, item);
 
127
  else
 
128
    g_menu_append_item (source_section, item);
132
129
 
133
130
  g_free (action);
134
131
  g_object_unref (item);
135
132
}
136
133
 
137
 
static void
138
 
im_desktop_menu_source_removed (ImApplicationList *applist,
139
 
                                const gchar       *app_id,
140
 
                                const gchar       *source_id,
141
 
                                gpointer           user_data)
 
134
static gint
 
135
im_desktop_menu_source_section_find_source (GMenu       *source_section,
 
136
                                            const gchar *source_id)
142
137
{
143
 
  ImDesktopMenu *menu = user_data;
144
 
  GMenu *source_section;
145
138
  gint n_items;
146
139
  gchar *action;
147
140
  gint i;
148
141
 
149
 
  source_section = g_hash_table_lookup (menu->source_sections, app_id);
150
 
  g_return_if_fail (source_section != NULL);
151
 
 
152
142
  n_items = g_menu_model_get_n_items (G_MENU_MODEL (source_section));
153
143
  action = g_strconcat ("src.", source_id, NULL);
154
144
 
158
148
 
159
149
      if (g_menu_model_get_item_attribute (G_MENU_MODEL (source_section), i, "action", "s", &item_action))
160
150
        {
161
 
          if (g_str_equal (action, item_action))
162
 
            g_menu_remove (source_section, i);
 
151
          gboolean equal;
163
152
 
 
153
          equal = g_str_equal (action, item_action);
164
154
          g_free (item_action);
 
155
 
 
156
          if (equal)
 
157
            break;
165
158
        }
166
159
    }
167
160
 
168
161
  g_free (action);
 
162
 
 
163
  return i < n_items ? i : -1;
 
164
}
 
165
 
 
166
 
 
167
static void
 
168
im_desktop_menu_source_added (ImApplicationList *applist,
 
169
                              const gchar       *app_id,
 
170
                              const gchar       *source_id,
 
171
                              const gchar       *label,
 
172
                              const gchar       *icon,
 
173
                              gpointer           user_data)
 
174
{
 
175
  ImDesktopMenu *menu = user_data;
 
176
  GMenu *source_section;
 
177
 
 
178
  source_section = g_hash_table_lookup (menu->source_sections, app_id);
 
179
  g_return_if_fail (source_section != NULL);
 
180
 
 
181
  im_desktop_menu_source_section_insert_source (source_section, source_id, label, icon, -1);
 
182
}
 
183
 
 
184
static void
 
185
im_desktop_menu_source_removed (ImApplicationList *applist,
 
186
                                const gchar       *app_id,
 
187
                                const gchar       *source_id,
 
188
                                gpointer           user_data)
 
189
{
 
190
  ImDesktopMenu *menu = user_data;
 
191
  GMenu *source_section;
 
192
  gint pos;
 
193
 
 
194
  source_section = g_hash_table_lookup (menu->source_sections, app_id);
 
195
  g_return_if_fail (source_section != NULL);
 
196
 
 
197
  pos = im_desktop_menu_source_section_find_source (source_section, source_id);
 
198
  if (pos >= 0)
 
199
    g_menu_remove (source_section, pos);
 
200
}
 
201
 
 
202
static void
 
203
im_desktop_menu_source_changed (ImApplicationList *applist,
 
204
                                const gchar       *app_id,
 
205
                                const gchar       *source_id,
 
206
                                const gchar       *label,
 
207
                                const gchar       *icon,
 
208
                                gboolean           visible,
 
209
                                gpointer           user_data)
 
210
{
 
211
  ImDesktopMenu *menu = user_data;
 
212
  GMenu *section;
 
213
  gint pos;
 
214
 
 
215
  section = g_hash_table_lookup (menu->source_sections, app_id);
 
216
  g_return_if_fail (section != NULL);
 
217
 
 
218
  pos = im_desktop_menu_source_section_find_source (section, source_id);
 
219
 
 
220
  if (pos >= 0)
 
221
    g_menu_remove (section, pos);
 
222
 
 
223
  if (visible)
 
224
    im_desktop_menu_source_section_insert_source (section, source_id, label, icon, pos);
169
225
}
170
226
 
171
227
static void
184
240
    }
185
241
}
186
242
 
 
243
static void
 
244
im_desktop_menu_app_stopped (ImApplicationList *applist,
 
245
                             const gchar       *app_id,
 
246
                             gpointer           user_data)
 
247
{
 
248
  ImDesktopMenu *menu = user_data;
 
249
  GMenu *section;
 
250
 
 
251
  section = g_hash_table_lookup (menu->source_sections, app_id);
 
252
  g_return_if_fail (section != NULL);
 
253
 
 
254
  while (g_menu_model_get_n_items (G_MENU_MODEL (section)) > 0)
 
255
    g_menu_remove (section, 0);
 
256
}
 
257
 
187
258
static GMenu *
188
259
create_status_section (void)
189
260
{
262
333
  g_signal_connect (applist, "app-added", G_CALLBACK (im_desktop_menu_app_added), menu);
263
334
  g_signal_connect (applist, "source-added", G_CALLBACK (im_desktop_menu_source_added), menu);
264
335
  g_signal_connect (applist, "source-removed", G_CALLBACK (im_desktop_menu_source_removed), menu);
 
336
  g_signal_connect (applist, "source-changed", G_CALLBACK (im_desktop_menu_source_changed), menu);
265
337
  g_signal_connect (applist, "remove-all", G_CALLBACK (im_desktop_menu_remove_all), menu);
 
338
  g_signal_connect (applist, "app-stopped", G_CALLBACK (im_desktop_menu_app_stopped), menu);
266
339
 
267
340
  G_OBJECT_CLASS (im_desktop_menu_parent_class)->constructed (object);
268
341
}