summaryrefslogtreecommitdiff
path: root/src/libtracker-common/tracker-miner-locale.c
blob: a247383b75c2536d3433e69f4cd18dd6f515c1e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * 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 "tracker-locale.h"
#include "tracker-miner-locale.h"

/* NOTE: This applies to more miners than just the application miner,
 * it's kept this way to avoid breaking things.
 */
#define TRACKER_MINER_LOCALE_FILE "miner-applications-locale.txt"

static gchar *
miner_locale_get_filename (void)
{
	/* Locate locale file */
	return g_build_filename (g_get_user_cache_dir (), "tracker", TRACKER_MINER_LOCALE_FILE, NULL);
}

static gchar *
miner_locale_get_previous (void)
{
	gchar *locale_file, *locale = NULL;

	locale_file = miner_locale_get_filename ();

	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 */
				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);

	return locale;
}

static gchar *
miner_locale_get_current (void)
{
	gchar *current_locale;

	/* Get current tracker LANG locale */
	current_locale = tracker_locale_get (TRACKER_LOCALE_LANGUAGE);

	return current_locale;
}

void
tracker_miner_locale_set_current (void)
{
	GError *error = NULL;
	gchar *locale_file, *locale = NULL;

	locale_file = miner_locale_get_filename ();

	g_message ("  Creating locale file '%s'", locale_file);

	locale = miner_locale_get_current ();

	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);
}

gboolean
tracker_miner_locale_changed (void)
{
	gchar *previous_locale;
	gchar *current_locale;
	gboolean changed;

	current_locale = miner_locale_get_current ();

	/* Get previous locale */
	previous_locale = miner_locale_get_previous ();

	/* 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 (previous_locale);
	g_free (current_locale);
	return changed;
}