summaryrefslogtreecommitdiffstats
path: root/src/nautilus-tracker-utilities.c
blob: b2e894a410f170b6cc9abe835cab5963fafd3be4 (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
/* nautilus-tracker-utilities.c
 *
 * Copyright 2019 Carlos Soriano <csoriano@redhat.com>
 * Copyright 2020 Sam Thursfield <sam@afuera.me.uk>
 *
 * 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"

/* 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 void
local_tracker_miner_fs_ready (GObject      *source,
                              GAsyncResult *res,
                              gpointer      user_data)
{
    tracker_miner_fs_connection = tracker_sparql_connection_new_finish (res, &tracker_miner_fs_error);
    if (tracker_miner_fs_error != NULL)
    {
        g_critical ("Could not start local Tracker indexer at %s: %s", tracker_miner_fs_busname, tracker_miner_fs_error->message);
    }
}

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

    g_message ("Starting %s", busname);
    tracker_sparql_connection_bus_new_async (busname, NULL, NULL, NULL, local_tracker_miner_fs_ready, NULL);

    tracker_miner_fs_busname = busname;
}

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

static void
host_tracker_miner_fs_ready (GObject      *source,
                             GAsyncResult *res,
                             gpointer      user_data)
{
    tracker_miner_fs_connection = tracker_sparql_connection_bus_new_finish (res, &tracker_miner_fs_error);
    if (tracker_miner_fs_error)
    {
        g_warning ("Unable to create connection for session-wide Tracker indexer: %s", (tracker_miner_fs_error)->message);
        if (inside_flatpak ())
        {
            g_clear_error (&tracker_miner_fs_error);
            start_local_tracker_miner_fs ();
        }
    }
}

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

    if (tracker_miner_fs_connection != NULL)
    {
        /* The connection was already established */
        return;
    }

    if (g_once_init_enter (&tried_tracker_init))
    {
        const gchar *busname = "org.freedesktop.Tracker3.Miner.Files";

        g_message ("Connecting to %s", busname);
        tracker_sparql_connection_bus_new_async (busname, NULL, NULL, NULL, host_tracker_miner_fs_ready, NULL);

        tracker_miner_fs_busname = busname;

        g_once_init_leave (&tried_tracker_init, TRUE);
    }
}

/**
 * nautilus_tracker_setup_host_miner_fs_connection_sync:
 *
 * This function is only meant to be used within tests.
 * This version of this setup function intentionally blocks to help with tests.
 *
 */
void
nautilus_tracker_setup_host_miner_fs_connection_sync (void)
{
    g_autoptr (GError) error = NULL;
    const gchar *busname = "org.freedesktop.Tracker3.Miner.Files";

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

    tracker_miner_fs_busname = busname;
}

/**
 * nautilus_tracker_get_miner_fs_connection:
 * @error: return location for a #GError
 *
 * This function returns a global singleton #TrackerSparqlConnection, or %NULL
 * if either we couldn't connect to Tracker Miner FS or the connection is still
 * pending.
 *
 * 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)
{
    nautilus_tracker_setup_miner_fs_connection ();

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

    return tracker_miner_fs_connection;
}