Merge lp:~ted/indicator-messages/seen-applications-list into lp:indicator-messages/0.5

Proposed by Ted Gould
Status: Merged
Merged at revision: not available
Proposed branch: lp:~ted/indicator-messages/seen-applications-list
Merge into: lp:indicator-messages/0.5
Diff against target: 356 lines (+256/-12)
6 files modified
src/Makefile.am (+2/-0)
src/app-menu-item.c (+3/-0)
src/launcher-menu-item.c (+40/-12)
src/messages-service.c (+3/-0)
src/seen-db.c (+177/-0)
src/seen-db.h (+31/-0)
To merge this branch: bzr merge lp:~ted/indicator-messages/seen-applications-list
Reviewer Review Type Date Requested Status
Cody Russell (community) Approve
Review via email: mp+22100@code.launchpad.net

Description of the change

Watches for when applications start to know whether they've been seen by
the messaging menu before. If they haven't, and they're a default
application. We replace the default name with "Set Up <blah>..."
instead of just the name of it.

To post a comment you must log in.
Revision history for this message
Cody Russell (bratsche) wrote :

180 + if (seendb != NULL) {
181 + return;
182 + }

I kind of wouldn't mind seeing that change to a g_return_if_fail(), because it seems like if you are hitting this multiple times there's probably something screwy going on right?

Approve.

review: Approve
195. By Ted Gould

Fail if we have init called more than once. Merge comment.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Makefile.am'
2--- src/Makefile.am 2010-03-23 21:22:57 +0000
3+++ src/Makefile.am 2010-03-25 15:28:31 +0000
4@@ -32,6 +32,8 @@
5 app-menu-item.h \
6 launcher-menu-item.c \
7 launcher-menu-item.h \
8+ seen-db.c \
9+ seen-db.h \
10 dirs.h \
11 dbus-data.h
12 indicator_messages_service_CFLAGS = $(APPLET_CFLAGS) -Wall -Wl,-Bsymbolic-functions -Wl,-z,defs -Wl,--as-needed -Werror
13
14=== modified file 'src/app-menu-item.c'
15--- src/app-menu-item.c 2010-03-23 21:08:48 +0000
16+++ src/app-menu-item.c 2010-03-25 15:28:31 +0000
17@@ -31,6 +31,7 @@
18 #include "app-menu-item.h"
19 #include "dbus-data.h"
20 #include "default-applications.h"
21+#include "seen-db.h"
22
23 enum {
24 COUNT_CHANGED,
25@@ -309,6 +310,8 @@
26 return;
27 }
28
29+ seen_db_add(value);
30+
31 priv->appinfo = G_APP_INFO(g_desktop_app_info_new_from_filename(value));
32 g_return_if_fail(priv->appinfo != NULL);
33
34
35=== modified file 'src/launcher-menu-item.c'
36--- src/launcher-menu-item.c 2010-03-17 18:34:27 +0000
37+++ src/launcher-menu-item.c 2010-03-25 15:28:31 +0000
38@@ -31,6 +31,7 @@
39 #include "launcher-menu-item.h"
40 #include "dbus-data.h"
41 #include "default-applications.h"
42+#include "seen-db.h"
43
44 enum {
45 NAME_CHANGED,
46@@ -163,7 +164,14 @@
47 app info that we've parsed */
48 g_debug("\tName: %s", launcher_menu_item_get_name(self));
49
50- const gchar * default_name = get_default_name(desktop_file);
51+ const gchar * default_name = NULL;
52+
53+ if (seen_db_seen(desktop_file)) {
54+ default_name = get_default_name(desktop_file);
55+ } else {
56+ default_name = get_default_setup(desktop_file);
57+ }
58+
59 if (default_name == NULL) {
60 dbusmenu_menuitem_property_set(DBUSMENU_MENUITEM(self), DBUSMENU_MENUITEM_PROP_LABEL, launcher_menu_item_get_name(self));
61 } else {
62@@ -183,17 +191,19 @@
63 g_signal_connect(G_OBJECT(self), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(activate_cb), NULL);
64
65 /* Start to build static shortcuts */
66- priv->ids = indicator_desktop_shortcuts_new(priv->desktop, "Messaging Menu");
67- const gchar ** nicks = indicator_desktop_shortcuts_get_nicks(priv->ids);
68- gint i;
69- for (i = 0; nicks[i] != NULL; i++) {
70- DbusmenuMenuitem * mi = dbusmenu_menuitem_new();
71- g_object_set_data(G_OBJECT(mi), NICK_DATA, (gpointer)nicks[i]);
72-
73- dbusmenu_menuitem_property_set(mi, DBUSMENU_MENUITEM_PROP_LABEL, indicator_desktop_shortcuts_nick_get_name(priv->ids, nicks[i]));
74- g_signal_connect(G_OBJECT(mi), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(nick_activate_cb), self);
75-
76- priv->shortcuts = g_list_append(priv->shortcuts, mi);
77+ if (seen_db_seen(desktop_file)) {
78+ priv->ids = indicator_desktop_shortcuts_new(priv->desktop, "Messaging Menu");
79+ const gchar ** nicks = indicator_desktop_shortcuts_get_nicks(priv->ids);
80+ gint i;
81+ for (i = 0; nicks[i] != NULL; i++) {
82+ DbusmenuMenuitem * mi = dbusmenu_menuitem_new();
83+ g_object_set_data(G_OBJECT(mi), NICK_DATA, (gpointer)nicks[i]);
84+
85+ dbusmenu_menuitem_property_set(mi, DBUSMENU_MENUITEM_PROP_LABEL, indicator_desktop_shortcuts_nick_get_name(priv->ids, nicks[i]));
86+ g_signal_connect(G_OBJECT(mi), DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED, G_CALLBACK(nick_activate_cb), self);
87+
88+ priv->shortcuts = g_list_append(priv->shortcuts, mi);
89+ }
90 }
91
92 /* Check to see if we should be eclipsed */
93@@ -311,6 +321,24 @@
94 dbusmenu_menuitem_property_set_bool(DBUSMENU_MENUITEM(li), DBUSMENU_MENUITEM_PROP_VISIBLE, !eclipsed);
95
96 g_list_foreach(priv->shortcuts, eclipse_shortcuts_cb, GINT_TO_POINTER(eclipsed));
97+
98+ /* If we're being reshown let's re-evaluate how we should be
99+ showing the label */
100+ if (!eclipsed) {
101+ const gchar * default_name = NULL;
102+
103+ if (seen_db_seen(priv->desktop)) {
104+ default_name = get_default_name(priv->desktop);
105+ } else {
106+ default_name = get_default_setup(priv->desktop);
107+ }
108+
109+ if (default_name == NULL) {
110+ dbusmenu_menuitem_property_set(DBUSMENU_MENUITEM(li), DBUSMENU_MENUITEM_PROP_LABEL, launcher_menu_item_get_name(li));
111+ } else {
112+ dbusmenu_menuitem_property_set(DBUSMENU_MENUITEM(li), DBUSMENU_MENUITEM_PROP_LABEL, _(default_name));
113+ }
114+ }
115
116 return;
117 }
118
119=== modified file 'src/messages-service.c'
120--- src/messages-service.c 2010-02-18 17:19:27 +0000
121+++ src/messages-service.c 2010-03-25 15:28:31 +0000
122@@ -39,6 +39,7 @@
123 #include "dbus-data.h"
124 #include "dirs.h"
125 #include "messages-service-dbus.h"
126+#include "seen-db.h"
127
128 static IndicateListener * listener;
129 static GList * serverList = NULL;
130@@ -1422,6 +1423,8 @@
131 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
132 textdomain (GETTEXT_PACKAGE);
133
134+ seen_db_init();
135+
136 dbus_interface = message_service_dbus_new();
137
138 listener = indicate_listener_ref_default();
139
140=== added file 'src/seen-db.c'
141--- src/seen-db.c 1970-01-01 00:00:00 +0000
142+++ src/seen-db.c 2010-03-25 15:28:31 +0000
143@@ -0,0 +1,177 @@
144+/*
145+A small database of which desktop files we've seen.
146+
147+Copyright 2010 Canonical Ltd.
148+
149+Authors:
150+ Ted Gould <ted@canonical.com>
151+
152+This program is free software: you can redistribute it and/or modify it
153+under the terms of the GNU General Public License version 3, as published
154+by the Free Software Foundation.
155+
156+This program is distributed in the hope that it will be useful, but
157+WITHOUT ANY WARRANTY; without even the implied warranties of
158+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
159+PURPOSE. See the GNU General Public License for more details.
160+
161+You should have received a copy of the GNU General Public License along
162+with this program. If not, see <http://www.gnu.org/licenses/>.
163+*/
164+
165+#include "seen-db.h"
166+
167+#define GROUP_NAME "Seen Database"
168+#define KEY_NAME "DesktopFiles"
169+
170+GHashTable * seendb = NULL;
171+gchar * filename = NULL;
172+gchar * dirname = NULL;
173+guint write_process = 0;
174+
175+/* Build the hashtable and then see if we have a keyfile that
176+ we can get the history of desktop files we've seen. */
177+void
178+seen_db_init(void)
179+{
180+ g_return_if_fail(seendb == NULL);
181+
182+ seendb = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
183+
184+ /* Build the filename for the seen database. We're putting
185+ it in the cache directory because it could get deleted and
186+ it really wouldn't be a big deal. */
187+ if (dirname == NULL) {
188+ dirname = g_build_filename(g_get_user_cache_dir(), "indicators", "messages", NULL);
189+ }
190+ if (filename == NULL) {
191+ filename = g_build_filename(dirname, "seen-db.keyfile", NULL);
192+ }
193+
194+ if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
195+ GKeyFile * keyfile = g_key_file_new();
196+
197+ /* Load from file */
198+ if (!g_key_file_load_from_file(keyfile, filename, G_KEY_FILE_NONE, NULL)) {
199+ g_key_file_free(keyfile);
200+ keyfile = NULL;
201+ }
202+
203+ /* Check for keys */
204+ if (keyfile != NULL && !g_key_file_has_key(keyfile, GROUP_NAME, KEY_NAME, NULL)) {
205+ g_warning("Seen DB '%s' does not have key '%s' in group '%s'", filename, KEY_NAME, GROUP_NAME);
206+ g_key_file_free(keyfile);
207+ keyfile = NULL;
208+ }
209+
210+ /* Grab them and put in DB */
211+ if (keyfile != NULL) {
212+ gchar ** desktops = g_key_file_get_string_list(keyfile, GROUP_NAME, KEY_NAME, NULL, NULL);
213+ gint i = 0;
214+
215+ while (desktops[i] != NULL) {
216+ g_hash_table_insert(seendb,
217+ g_strdup(desktops[i]),
218+ GINT_TO_POINTER(TRUE));
219+ i++;
220+ }
221+
222+ g_strfreev(desktops);
223+ }
224+
225+ /* Clean up our file */
226+ if (keyfile != NULL) {
227+ g_key_file_free(keyfile);
228+ }
229+ }
230+
231+ return;
232+}
233+
234+/* A function to write out the seen database after it's been
235+ modified for a while. */
236+static gboolean
237+write_seen_db (gpointer user_data)
238+{
239+ write_process = 0;
240+
241+ /* Build up the key file */
242+ GKeyFile * keyfile = g_key_file_new();
243+ GArray * desktops = g_array_new(FALSE, FALSE, sizeof(gchar *));
244+
245+ /* Get the keys from the hashtable and make them
246+ into an array */
247+ if (keyfile != NULL) {
248+ GList * desktop_keys = g_hash_table_get_keys(seendb);
249+ GList * head = NULL;
250+
251+ for (head = desktop_keys; head != NULL; head = g_list_next(head)) {
252+ g_array_append_val(desktops, head->data);
253+ }
254+
255+ g_list_free(desktop_keys);
256+ }
257+
258+ /* Use the array to dump the strings into the keyfile */
259+ g_key_file_set_string_list(keyfile,
260+ GROUP_NAME,
261+ KEY_NAME,
262+ (const gchar * const *)desktops->data,
263+ desktops->len);
264+ g_array_free(desktops, TRUE);
265+
266+ /* Dump the key file to string */
267+ gchar * keydump = NULL;
268+ gsize keydumplen = 0;
269+ keydump = g_key_file_to_data(keyfile, &keydumplen, NULL);
270+ g_key_file_free(keyfile);
271+
272+ /* Ensure the directory exists */
273+ if (g_mkdir_with_parents(dirname, 0700) != 0) {
274+ g_warning("Unable to make directory: %s", dirname);
275+ g_free(keydump);
276+ return FALSE;
277+ }
278+
279+ /* Dump out the file */
280+ GError * error = NULL;
281+ if (!g_file_set_contents(filename, keydump, keydumplen, &error)) {
282+ g_warning("Unable to write out file '%s': %s", filename, error->message);
283+ g_error_free(error);
284+ }
285+
286+ /* Clean up */
287+ g_free(keydump);
288+
289+ return FALSE;
290+}
291+
292+/* Add a new desktop file to the seen database. Also sets up a timer
293+ to do the write out. */
294+void
295+seen_db_add (const gchar * desktop)
296+{
297+ /* If this is a new one, let's set up the timer. If
298+ there's already one clear it. */
299+ if (!seen_db_seen(desktop)) {
300+ if (write_process != 0) {
301+ g_source_remove(write_process);
302+ write_process = 0;
303+ }
304+
305+ write_process = g_timeout_add_seconds(60, write_seen_db, NULL);
306+ }
307+
308+ g_hash_table_insert(seendb,
309+ g_strdup(desktop),
310+ GINT_TO_POINTER(TRUE));
311+
312+ return;
313+}
314+
315+/* Checks to see if a desktop file has been seen. */
316+gboolean
317+seen_db_seen (const gchar * desktop)
318+{
319+ return GPOINTER_TO_INT(g_hash_table_lookup(seendb, desktop));
320+}
321
322=== added file 'src/seen-db.h'
323--- src/seen-db.h 1970-01-01 00:00:00 +0000
324+++ src/seen-db.h 2010-03-25 15:28:31 +0000
325@@ -0,0 +1,31 @@
326+/*
327+A small database of which desktop files we've seen.
328+
329+Copyright 2010 Canonical Ltd.
330+
331+Authors:
332+ Ted Gould <ted@canonical.com>
333+
334+This program is free software: you can redistribute it and/or modify it
335+under the terms of the GNU General Public License version 3, as published
336+by the Free Software Foundation.
337+
338+This program is distributed in the hope that it will be useful, but
339+WITHOUT ANY WARRANTY; without even the implied warranties of
340+MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
341+PURPOSE. See the GNU General Public License for more details.
342+
343+You should have received a copy of the GNU General Public License along
344+with this program. If not, see <http://www.gnu.org/licenses/>.
345+*/
346+
347+#ifndef SEEN_DB_H__
348+#define SEEN_DB_H__ 1
349+
350+#include <glib.h>
351+
352+void seen_db_init(void);
353+void seen_db_add (const gchar * desktop);
354+gboolean seen_db_seen (const gchar * desktop);
355+
356+#endif /* SEEN_DB_H__ */

Subscribers

People subscribed via source and target branches

to all changes: