summaryrefslogtreecommitdiff
path: root/src/nautilus-tracker-utilities.c
blob: 4c408942efd305f74598947cab1d0d70924e8510 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* nautilus-tracker-utilities.c
 *
 * Copyright 2019 Carlos Soriano <csoriano@redhat.com>
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "config.h"
#include "nautilus-tracker-utilities.h"
#include "nautilus-global-preferences.h"

#define TRACKER_KEY_RECURSIVE_DIRECTORIES "index-recursive-directories"
#define TRACKER_KEY_SINGLE_DIRECTORIES "index-single-directories"

/* Shared global connection to Tracker Miner FS */
static const gchar *tracker_miner_fs_busname = NULL;
static TrackerSparqlConnection *tracker_miner_fs_connection = NULL;
static GError *tracker_miner_fs_error = NULL;

static gboolean
get_host_tracker_miner_fs (GError **error)
{
    const gchar *busname = "org.freedesktop.Tracker3.Miner.Files";

    g_message ("Connecting to %s", busname);
    tracker_miner_fs_connection = tracker_sparql_connection_bus_new (busname, NULL, NULL, error);
    if (*error)
    {
        g_warning ("Unable to create connection for session-wide Tracker indexer: %s", (*error)->message);
        return FALSE;
    }

    tracker_miner_fs_busname = busname;
    return TRUE;
}

static gboolean
start_local_tracker_miner_fs (GError **error)
{
    const gchar *busname = APPLICATION_ID ".Tracker3.Miner.Files";

    g_message ("Starting %s", busname);
    tracker_miner_fs_connection = tracker_sparql_connection_bus_new (busname, NULL, NULL, error);
    if (*error)
    {
        g_critical ("Could not start local Tracker indexer at %s: %s", busname, (*error)->message);
        return FALSE;
    }

    tracker_miner_fs_busname = busname;
    return TRUE;
}

static gboolean
inside_flatpak (void)
{
    return g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS);
}

static void
setup_tracker_miner_fs_connection (void)
{
    static gsize tried_tracker_init = FALSE;

    if (g_once_init_enter (&tried_tracker_init))
    {
        gboolean success;

        success = get_host_tracker_miner_fs (&tracker_miner_fs_error);

        if (!success && inside_flatpak ())
        {
            g_clear_error (&tracker_miner_fs_error);
            success = start_local_tracker_miner_fs (&tracker_miner_fs_error);
        }

        g_once_init_leave (&tried_tracker_init, TRUE);
    }
}

/**
 * nautilus_tracker_get_miner_fs_connection:
 * @error: return location for a #GError
 *
 * This function returns a global singleton #TrackerSparqlConnection, or %NULL
 * if we couldn't connect to Tracker Miner FS.
 *
 * The first time you call it, this function will block while trying to connect.
 * This may take some time if starting Tracker Miners from a Flatpak bundle.
 *
 * The returned object is a globally shared singleton which should NOT be
 * unreffed.
 *
 * Returns: a #TrackerSparqlConnection, or %NULL
 */
TrackerSparqlConnection *
nautilus_tracker_get_miner_fs_connection (GError **error)
{
    setup_tracker_miner_fs_connection ();

    if (tracker_miner_fs_error && error)
    {
        *error = g_error_copy (tracker_miner_fs_error);
    }

    return tracker_miner_fs_connection;
}

/**
 * nautilus_tracker_get_miner_fs_busname:
 * @error: return location for a #GError
 *
 * This function returns a DBus name that can be used to talk to
 * tracker-miner-fs, or %NULL if there is no Tracker Miner FS available.
 *
 * The first time you call it, this function will block while trying to connect.
 * This may take some time if starting Tracker Miners from a Flatpak bundle.
 *
 * Returns: a string
 */
const gchar *
nautilus_tracker_get_miner_fs_busname (GError **error)
{
    setup_tracker_miner_fs_connection ();

    if (tracker_miner_fs_error && error)
    {
        *error = g_error_copy (tracker_miner_fs_error);
    }

    return tracker_miner_fs_busname;
}

static GFile *
location_from_tracker_dir (const gchar *value)
{
    const gchar *special_dir;
    g_autoptr (GFile) home = NULL;
    GFile *location;

    home = g_file_new_for_path (g_get_home_dir ());

    if (g_strcmp0 (value, "$HOME") == 0)
    {
        return g_steal_pointer (&home);
    }

    special_dir = NULL;
    if (g_strcmp0 (value, "&DESKTOP") == 0)
    {
        special_dir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
    }
    else if (g_strcmp0 (value, "&DOCUMENTS") == 0)
    {
        special_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
    }
    else if (g_strcmp0 (value, "&DOWNLOAD") == 0)
    {
        special_dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD);
    }
    else if (g_strcmp0 (value, "&MUSIC") == 0)
    {
        special_dir = g_get_user_special_dir (G_USER_DIRECTORY_MUSIC);
    }
    else if (g_strcmp0 (value, "&PICTURES") == 0)
    {
        special_dir = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
    }
    else if (g_strcmp0 (value, "&PUBLIC_SHARE") == 0)
    {
        special_dir = g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE);
    }
    else if (g_strcmp0 (value, "&TEMPLATES") == 0)
    {
        special_dir = g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES);
    }
    else if (g_strcmp0 (value, "&VIDEOS") == 0)
    {
        special_dir = g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS);
    }

    if (special_dir != NULL)
    {
        location = g_file_new_for_commandline_arg (special_dir);

        /* Ignore XDG directories set to $HOME, like the miner does */
        if (g_file_equal (location, home))
        {
            g_clear_object (&location);
        }
    }
    else
    {
        location = g_file_new_for_commandline_arg (value);
    }

    return location;
}

static GList *
get_tracker_locations (const gchar *key)
{
    g_auto (GStrv) locations = NULL;
    GList *list = NULL;
    gint idx;
    GFile *location;

    locations = g_settings_get_strv (tracker_preferences, key);

    for (idx = 0; locations[idx] != NULL; idx++)
    {
        location = location_from_tracker_dir (locations[idx]);
        if (location != NULL)
        {
            list = g_list_prepend (list, location);
        }
    }

    return list;
}

/**
 * nautilus_tracker_directory_is_tracked:
 * @directory: a #GFile representing a directory
 *
 * This function reads the "index-recursive-directories" and
 * "index-single-directories" keys from the org.freedesktop.tracker.miner.files
 * schema, and assumes the listed directories (and their descendants for the
 * former key) are tracked.
 *
 * Exception: XDG user dirs set to $HOME are ignored.
 *
 * FIXME: Tracker's files miner's logic is actually a lot more complex,
 * including configurable ignore patterns, but we are overlooking that.
 *
 * Returns: $TRUE if the @directory is, in principle, tracked. $FALSE otherwise.
 */
gboolean
nautilus_tracker_directory_is_tracked (GFile *directory)
{
    g_autolist (GFile) recursive_locations = NULL;
    g_autolist (GFile) single_locations = NULL;
    GList *l;

    recursive_locations = get_tracker_locations (TRACKER_KEY_RECURSIVE_DIRECTORIES);
    for (l = recursive_locations; l != NULL; l = l->next)
    {
        if (g_file_equal (directory, G_FILE (l->data)) ||
            g_file_has_prefix (directory, G_FILE (l->data)))
        {
            return TRUE;
        }
    }

    single_locations = get_tracker_locations (TRACKER_KEY_SINGLE_DIRECTORIES);
    for (l = single_locations; l != NULL; l = l->next)
    {
        if (g_file_equal (directory, G_FILE (l->data)))
        {
            return TRUE;
        }
    }

    return FALSE;
}