[tracker/collation-gconf-locale: 29/31] tracker-miner-applications: move locale handling to separate files



commit 2c46140ca4bc2612711d01f4875b20f51e9f16e4
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Wed Nov 17 11:14:41 2010 +0100

    tracker-miner-applications: move locale handling to separate files

 src/miners/fs/Makefile.am                         |    2 +
 src/miners/fs/tracker-miner-applications-locale.c |  137 +++++++++++++++++++++
 src/miners/fs/tracker-miner-applications-locale.h |   29 +++++
 3 files changed, 168 insertions(+), 0 deletions(-)
---
diff --git a/src/miners/fs/Makefile.am b/src/miners/fs/Makefile.am
index 5cc14ba..9e2644d 100644
--- a/src/miners/fs/Makefile.am
+++ b/src/miners/fs/Makefile.am
@@ -19,6 +19,8 @@ tracker_miner_fs_SOURCES =                             \
 	tracker-main.c                                 \
 	tracker-miner-applications.c                   \
 	tracker-miner-applications.h                   \
+	tracker-miner-applications-locale.c            \
+	tracker-miner-applications-locale.h            \
 	tracker-miner-files.c                          \
 	tracker-miner-files.h                          \
 	tracker-miner-files-index.c                    \
diff --git a/src/miners/fs/tracker-miner-applications-locale.c b/src/miners/fs/tracker-miner-applications-locale.c
new file mode 100644
index 0000000..39c537c
--- /dev/null
+++ b/src/miners/fs/tracker-miner-applications-locale.c
@@ -0,0 +1,137 @@
+/*
+ * Copyright (C) 2010 Nokia <ivan frade nokia com>
+ *
+ * This library 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.
+ *
+ * This library 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 this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include "config.h"
+#include <string.h>
+
+#include <libtracker-common/tracker-locale.h>
+
+#include "tracker-miner-applications-locale.h"
+#ifdef HAVE_MEEGOTOUCH
+#include "tracker-miner-applications-meego.h"
+#endif
+
+#define TRACKER_MINER_APPLICATIONS_LOCALE_FILE "miner-applications-locale.txt"
+
+static gchar *
+miner_applications_locale_get_previous (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_locale_set_current (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);
+}
+
+gboolean
+tracker_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 LANG locale */
+	current_locale = tracker_locale_get (TRACKER_LOCALE_LANGUAGE);
+
+#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_locale_get_previous (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_locale_set_current (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;
+}
diff --git a/src/miners/fs/tracker-miner-applications-locale.h b/src/miners/fs/tracker-miner-applications-locale.h
new file mode 100644
index 0000000..1f514e2
--- /dev/null
+++ b/src/miners/fs/tracker-miner-applications-locale.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2010 Nokia <ivan frade nokia com>
+ *
+ * This library 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.
+ *
+ * This library 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 this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __TRACKER_MINER_FS_APPLICATIONS_LOCALE_H__
+#define __TRACKER_MINER_FS_APPLICATIONS_LOCALE_H__
+
+G_BEGIN_DECLS
+
+gboolean tracker_miner_applications_locale_changed (void);
+
+G_END_DECLS
+
+#endif /* __TRACKER_MINER_FS_APPLICATIONS_LOCALE_H__ */



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