[tracker-miners/wip/carlosg/apps-in-miner-fs: 16/17] tracker-miner-fs: Reset indexed applications on locale change



commit 2ba8162ddc5b8218ba32af03792d4fa905b2eaf8
Author: Carlos Garnacho <carlosg gnome org>
Date:   Sun Nov 11 11:13:55 2018 +0100

    tracker-miner-fs: Reset indexed applications on locale change
    
    This is basically the same than tracker-miner-apps does, uses the
    same file and everything. This will make tracker-extract go again
    through them, thus using the localized strings as per the current
    locale.

 src/miners/fs/tracker-main.c | 111 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 111 insertions(+)
---
diff --git a/src/miners/fs/tracker-main.c b/src/miners/fs/tracker-main.c
index 9bdbb8f0a..9832d50a8 100644
--- a/src/miners/fs/tracker-main.c
+++ b/src/miners/fs/tracker-main.c
@@ -54,6 +54,7 @@
 
 #define DBUS_NAME_SUFFIX "Tracker1.Miner.Files"
 #define DBUS_PATH "/org/freedesktop/Tracker1/Miner/Files"
+#define LOCALE_FILENAME "locale-for-miner-apps.txt"
 
 static GMainLoop *main_loop;
 
@@ -122,6 +123,112 @@ sanity_check_option_values (TrackerConfig *config)
        }
 }
 
+static void
+miner_reset_applications (TrackerMiner *miner)
+{
+       const gchar *sparql;
+       GError *error = NULL;
+
+       sparql =
+               "DELETE { GRAPH <" TRACKER_OWN_GRAPH_URN "> { ?icon a rdfs:Resource } } "
+               "WHERE { GRAPH <" TRACKER_OWN_GRAPH_URN "> { ?app a nfo:SoftwareApplication; nfo:softwareIcon 
?icon } } "
+               "DELETE { GRAPH <" TRACKER_OWN_GRAPH_URN "> { ?app nie:dataSource <" TRACKER_PREFIX_TRACKER 
"extractor-data-source> } "
+               "WHERE { GRAPH <" TRACKER_OWN_GRAPH_URN "> { ?app a nfo:SoftwareApplication } ";
+
+       /* Execute a sync update, we don't want the apps miner to start before
+        * we finish this. */
+       tracker_sparql_connection_update (tracker_miner_get_connection (miner),
+                                         sparql, G_PRIORITY_HIGH,
+                                         NULL, &error);
+
+       if (error) {
+               /* Some error happened performing the query, not good */
+               g_critical ("Couldn't reset indexed applications: %s",
+                           error ? error->message : "unknown error");
+               g_error_free (error);
+       }
+}
+
+static void
+save_current_locale (void)
+{
+       GError *error = NULL;
+       gchar *locale = tracker_locale_get (TRACKER_LOCALE_LANGUAGE);
+       gchar *locale_file = g_build_filename (g_get_user_cache_dir (), "tracker", LOCALE_FILENAME, NULL);
+
+       g_message ("Saving locale used to index applications");
+       g_message ("  Creating locale file '%s'", locale_file);
+
+       if (locale == NULL) {
+               locale = g_strdup ("");
+       }
+
+       if (!g_file_set_contents (locale_file, locale, -1, &error)) {
+               g_message ("  Could not set file contents, %s",
+                          error ? error->message : "no error given");
+               g_clear_error (&error);
+       }
+
+       g_free (locale);
+       g_free (locale_file);
+}
+
+static gboolean
+detect_locale_changed (TrackerMiner *miner)
+{
+       gchar *locale_file;
+       gchar *previous_locale = NULL;
+       gchar *current_locale;
+       gboolean changed;
+
+       locale_file = g_build_filename (g_get_user_cache_dir (), "tracker", LOCALE_FILENAME, 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 &&
+                           contents[0] == '\0') {
+                               g_critical ("  Empty locale file found at '%s'", locale_file);
+                               g_free (contents);
+                       } else {
+                               /* Re-use contents */
+                               previous_locale = contents;
+                       }
+               } else {
+                       g_critical ("  Could not get content of file '%s'", locale_file);
+               }
+       } else {
+               g_message ("  Could not find locale file:'%s'", locale_file);
+       }
+
+       g_free (locale_file);
+
+       current_locale = tracker_locale_get (TRACKER_LOCALE_LANGUAGE);
+
+       /* 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);
+               changed = TRUE;
+       } else {
+               g_message ("Current and previous locales match: '%s'", previous_locale);
+               changed = FALSE;
+       }
+
+       g_free (current_locale);
+       g_free (previous_locale);
+
+       if (changed) {
+               g_message ("Resetting nfo:Software due to locale change...");
+               miner_reset_applications (miner);
+       }
+
+       return changed;
+}
+
 static gboolean
 signal_handler (gpointer user_data)
 {
@@ -778,6 +885,9 @@ main (gint argc, gchar *argv[])
                return EXIT_FAILURE;
        }
 
+       /* If the locales changed, we need to reset some things first */
+       detect_locale_changed (TRACKER_MINER (miner_files));
+
        proxy = tracker_miner_proxy_new (miner_files, connection, DBUS_PATH, NULL, &error);
        if (error) {
                g_critical ("Couldn't create miner proxy: %s", error->message);
@@ -875,6 +985,7 @@ main (gint argc, gchar *argv[])
        if (miners_timeout_id == 0 &&
            !miner_needs_check (miner_files, store_available)) {
                tracker_miner_files_set_need_mtime_check (FALSE);
+               save_current_locale ();
        }
 
        g_main_loop_unref (main_loop);


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