[tracker/collation-gconf-locale: 28/28] tracker-miner-applications: Keep locale used between restarts



commit 58b5b8c23453902332e0bb49adfa08141f14c23d
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Tue Nov 16 17:48:07 2010 +0100

    tracker-miner-applications: Keep locale used between restarts

 src/miners/fs/tracker-miner-applications-meego.cpp |   13 ++-
 src/miners/fs/tracker-miner-applications-meego.h   |    6 +-
 src/miners/fs/tracker-miner-applications.c         |  123 ++++++++++++++++++++
 3 files changed, 138 insertions(+), 4 deletions(-)
---
diff --git a/src/miners/fs/tracker-miner-applications-meego.cpp b/src/miners/fs/tracker-miner-applications-meego.cpp
index 20961ce..5ffabd9 100644
--- a/src/miners/fs/tracker-miner-applications-meego.cpp
+++ b/src/miners/fs/tracker-miner-applications-meego.cpp
@@ -36,8 +36,8 @@
  * layers here.
  */
 gchar *
-tracker_miner_applications_meego_translate (const gchar *catalogue,
-					    const gchar *id)
+tracker_miner_applications_meego_translate (const gchar  *catalogue,
+                                            const gchar  *id)
 {
 	char *argv[] = { "dummy", NULL };
 	int argc = 1;
@@ -60,3 +60,12 @@ tracker_miner_applications_meego_translate (const gchar *catalogue,
 
 	return ret;
 }
+
+gchar *
+tracker_miner_applications_meego_get_locale (void)
+{
+	/* Get the system default locale */
+	MLocale locale;
+
+	return g_strdup (locale.name ().data ());
+}
diff --git a/src/miners/fs/tracker-miner-applications-meego.h b/src/miners/fs/tracker-miner-applications-meego.h
index 8f36523..d3688ee 100644
--- a/src/miners/fs/tracker-miner-applications-meego.h
+++ b/src/miners/fs/tracker-miner-applications-meego.h
@@ -22,8 +22,10 @@
 
 G_BEGIN_DECLS
 
-gchar* tracker_miner_applications_meego_translate (const gchar *catalogue,
-                                                   const gchar *id);
+gchar *tracker_miner_applications_meego_translate (const gchar  *catalogue,
+                                                   const gchar  *id);
+
+gchar *tracker_miner_applications_meego_get_locale (void);
 
 G_END_DECLS
 
diff --git a/src/miners/fs/tracker-miner-applications.c b/src/miners/fs/tracker-miner-applications.c
index 6371554..ace8c2c 100644
--- a/src/miners/fs/tracker-miner-applications.c
+++ b/src/miners/fs/tracker-miner-applications.c
@@ -29,6 +29,8 @@
 #include "tracker-miner-applications-meego.h"
 #endif
 
+#define TRACKER_MINER_APPLICATIONS_LOCALE_FILE "miner-applications-locale.txt"
+
 #define GROUP_DESKTOP_ENTRY          "Desktop Entry"
 
 #define APPLICATION_DATASOURCE_URN   "urn:nepomuk:datasource:84f20000-1241-11de-8c30-0800200c9a66"
@@ -727,9 +729,130 @@ miner_applications_process_file_attributes (TrackerMinerFS       *fs,
 	return FALSE;
 }
 
+static gchar *
+miner_applications_get_previous_locale (const gchar *locale_file)
+{
+	gchar *locale = NULL;
+
+	if (G_LIKELY (g_file_test (locale_file, G_FILE_TEST_EXISTS))) {
+		gchar *contents;
+
+		/* Check locale is correct */
+		if (G_LIKELY (g_file_get_contents (locale_file, &contents, NULL, NULL))) {
+			if (contents && strlen (contents) == 0) {
+				g_critical ("  Empty locale file found at '%s'", locale_file);
+				g_free (contents);
+			} else {
+				/* Re-use contents */
+				locale = contents;
+			}
+		} else {
+			g_critical ("  Could not get content of file '%s'", locale_file);
+		}
+	} else {
+		g_critical ("  Could not find locale file:'%s'", locale_file);
+	}
+
+	return locale;
+}
+
+static void
+miner_applications_set_current_locale (const gchar *locale_file,
+                                       const gchar *locale)
+{
+	GError *error = NULL;
+	gchar  *str;
+
+	g_message ("  Creating locale file '%s'", locale_file);
+
+	str = g_strdup_printf ("%s", locale ? locale : "");
+
+	if (!g_file_set_contents (locale_file, str, -1, &error)) {
+		g_message ("  Could not set file contents, %s",
+		           error ? error->message : "no error given");
+		g_clear_error (&error);
+	}
+
+	g_free (str);
+}
+
+static gboolean
+miner_applications_locale_changed (void)
+{
+	gchar *previous_locale;
+	gchar *current_locale;
+	gboolean changed;
+	gchar *data_dir;
+	gchar *filename;
+
+	/* Locate previous locale file */
+	data_dir = g_build_filename (g_get_user_cache_dir (),
+	                             "tracker",
+	                             NULL);
+	filename = g_build_filename (data_dir, TRACKER_MINER_APPLICATIONS_LOCALE_FILE, NULL);
+
+	/* Get current tracker locale */
+	current_locale = tracker_locale_get (TRACKER_LOCALE_COLLATE);
+
+#ifdef HAVE_MEEGOTOUCH
+	/* If we have meegotouch enabled, sanity check to compare with our
+	 * tracker locale */
+	{
+		/* Get also current meegotouch locale, which should be EQUAL to
+		 * the tracker locale */
+		gchar *current_mlocale;
+
+		current_mlocale = tracker_miner_applications_meego_get_locale ();
+
+		if (g_strcmp0 (current_locale, current_mlocale) != 0) {
+			g_critical ("Wrong locale settings (tracker locale '%s' vs MLocale '%s')",
+			            current_locale, current_mlocale);
+		}
+
+		g_free (current_mlocale);
+	}
+#endif
+
+	/* Get previous locale */
+	previous_locale = miner_applications_get_previous_locale (filename);
+
+	/* Note that having both to NULL is actually valid, they would default
+	 * to the unicode collation without locale-specific stuff. */
+	if (g_strcmp0 (previous_locale, current_locale) != 0) {
+		g_message ("Locale change detected from '%s' to '%s'...",
+		           previous_locale, current_locale);
+		/* Store the new one now */
+		miner_applications_set_current_locale (filename, current_locale);
+		changed = TRUE;
+	} else {
+		g_message ("Current and previous locales match: '%s'", previous_locale);
+		changed = FALSE;
+	}
+
+	g_free (previous_locale);
+	g_free (current_locale);
+	g_free (filename);
+	g_free (data_dir);
+	return changed;
+}
+
+static void
+miner_applications_initialize_locale (void)
+{
+	/* If a locale change was detected, we need to remove all our previously
+	 * created resources from the store, so that they are properly recreated.
+	 */
+	if (miner_applications_locale_changed ()) {
+		/* TODO, nice sparql update removing all in our graph */
+	}
+}
+
 TrackerMiner *
 tracker_miner_applications_new (void)
 {
+	/* Initialize locale-related checks */
+	miner_applications_initialize_locale ();
+
 	return g_object_new (TRACKER_TYPE_MINER_APPLICATIONS,
 	                     "name", "Applications",
 	                     NULL);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]