~alecu/ubuntuone-client/stop-proxy-tunnel

« back to all changes in this revision

Viewing changes to gsd-plugin/gsd-ubuntuone.c

  • Committer: Rodney Dawes
  • Date: 2011-07-21 15:24:02 UTC
  • mto: This revision was merged to the branch mainline in revision 1062.
  • Revision ID: rodney.dawes@canonical.com-20110721152402-xp5r1tg5yr02cz94
Moved the GNOME-specific pieces of code to ubuntuone-client-gnome

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2
 
 *
3
 
 * Authors: Alejandro J. Cura <alecu@canonical.com>
4
 
 *
5
 
 * Copyright (C) 2010 Canonical Ltd.
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License version 3,
9
 
 * as published by the Free Software Foundation.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 
 *
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <glib/gi18n-lib.h>
25
 
#include <gmodule.h>
26
 
#include <gio/gio.h>
27
 
#include <gconf/gconf-client.h>
28
 
 
29
 
#include "gnome-settings-daemon/gnome-settings-plugin.h"
30
 
#include "gsd-ubuntuone.h"
31
 
 
32
 
GNOME_SETTINGS_PLUGIN_REGISTER (GsdUbuntuOne, gsd_ubuntuone)
33
 
 
34
 
#define CHECKED_BOOKMARK_FILE_KEY "/apps/gnome_settings_daemon/plugins/ubuntuone/checked_bookmark_file"
35
 
 
36
 
 
37
 
static void
38
 
bookmark_file_loaded (GObject *source, GAsyncResult *res, gpointer user_data)
39
 
{
40
 
        gchar *contents = NULL;
41
 
        GError *error = NULL;
42
 
 
43
 
        g_file_load_contents_finish (G_FILE (source), res, &contents, NULL, NULL, &error);
44
 
        if (error == NULL) {
45
 
                gchar **lines, *u1_location;
46
 
                gint i;
47
 
                gboolean add_it = TRUE;
48
 
 
49
 
                u1_location = g_build_filename ("file://", g_get_home_dir (), "Ubuntu%20One", NULL);
50
 
 
51
 
                lines = g_strsplit (contents, "\n", -1);
52
 
                for (i = 0; lines[i] != NULL; i++) {
53
 
                        /* Ignore empty lines */
54
 
                        if (lines[i][0] != '\0' && lines[i][0] != ' ') {
55
 
                                if (g_str_has_prefix (lines[i], u1_location)) {
56
 
                                        add_it = FALSE;
57
 
                                        break;
58
 
                                }
59
 
                        }
60
 
                }
61
 
 
62
 
                if (add_it) {
63
 
                        gchar *new_contents = g_strdup_printf ("%s\n%s Ubuntu One\n", contents, u1_location);
64
 
 
65
 
                        if (g_file_replace_contents (G_FILE (source),
66
 
                                                     (const gchar *) new_contents,
67
 
                                                     strlen (new_contents),
68
 
                                                     NULL,
69
 
                                                     FALSE,
70
 
                                                     0, NULL, NULL, &error)) {
71
 
                                GConfClient *conf_client;
72
 
 
73
 
                                conf_client = gconf_client_get_default ();
74
 
                                gconf_client_set_bool (conf_client, CHECKED_BOOKMARK_FILE_KEY, TRUE, NULL);
75
 
                        } else {
76
 
                                g_warning ("Could not save bookmarks file: %s\n", error->message);
77
 
                                g_error_free (error);
78
 
                        }
79
 
                }
80
 
 
81
 
                g_free (contents);
82
 
                g_free (u1_location);
83
 
                g_strfreev (lines);
84
 
        } else {
85
 
                g_warning ("Could not load bookmark file: %s\n", error->message);
86
 
                g_error_free (error);
87
 
        }
88
 
}
89
 
 
90
 
static void
91
 
check_bookmark_file (void)
92
 
{
93
 
        gchar *filename;
94
 
        GConfClient *conf_client;
95
 
        GError *error = NULL;
96
 
 
97
 
        /* We only check the bookmark file if we haven't already done so */
98
 
        conf_client = gconf_client_get_default ();
99
 
        if (!gconf_client_get_bool (conf_client, CHECKED_BOOKMARK_FILE_KEY, &error)) {
100
 
                if (error == NULL) {
101
 
                        gchar *u1_folder;
102
 
 
103
 
                        u1_folder = g_build_filename (g_get_home_dir (), "Ubuntu One", NULL);
104
 
                        if (g_file_test ((const gchar *) u1_folder, G_FILE_TEST_IS_DIR)) {
105
 
                                /* Load the bookmark file */
106
 
                                filename = g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
107
 
                                if (filename != NULL) {
108
 
                                        GFile *file;
109
 
 
110
 
                                        file = g_file_new_for_path (filename);
111
 
                                        g_file_load_contents_async (file, NULL, bookmark_file_loaded, NULL);
112
 
 
113
 
                                        g_object_unref (G_OBJECT (file));
114
 
                                        g_free (filename);
115
 
                                }
116
 
                        }
117
 
 
118
 
                        g_free (u1_folder);
119
 
                } else {
120
 
                        g_warning ("Error getting settings: %s\n", error->message);
121
 
                        g_error_free (error);
122
 
                }
123
 
        }
124
 
 
125
 
        g_object_unref (conf_client);
126
 
}
127
 
 
128
 
static void
129
 
credentials_found_cb (SyncdaemonAuthentication *auth, SyncdaemonCredentials *credentials, gpointer user_data)
130
 
{
131
 
        check_bookmark_file ();
132
 
}
133
 
 
134
 
static gboolean
135
 
delayed_syncdaemon_start (gpointer data)
136
 
{
137
 
        GsdUbuntuOne *plugin;
138
 
        SyncdaemonAuthentication *auth;
139
 
 
140
 
        plugin = GSD_UBUNTUONE (data);
141
 
 
142
 
        plugin->syncdaemon = syncdaemon_daemon_new ();
143
 
 
144
 
        /* Check for authentication */
145
 
        auth = syncdaemon_daemon_get_authentication (plugin->syncdaemon);
146
 
        g_signal_connect (auth, "credentials_found",
147
 
                          G_CALLBACK (credentials_found_cb), NULL);
148
 
 
149
 
        return FALSE;
150
 
}
151
 
 
152
 
static void
153
 
gsd_ubuntuone_init (GsdUbuntuOne *plugin)
154
 
{
155
 
}
156
 
 
157
 
void
158
 
gsd_ubuntuone_activate (GnomeSettingsPlugin *gsp_object)
159
 
{
160
 
        GsdUbuntuOne *plugin = GSD_UBUNTUONE (gsp_object);
161
 
 
162
 
        g_timeout_add (DELAYED_START_TIMEOUT, delayed_syncdaemon_start, plugin);
163
 
}
164
 
 
165
 
static void
166
 
gsd_ubuntuone_dispose (GObject *object)
167
 
{
168
 
        GsdUbuntuOne *plugin = GSD_UBUNTUONE (object);
169
 
 
170
 
        if (plugin->syncdaemon != NULL)
171
 
                g_object_unref (plugin->syncdaemon);
172
 
 
173
 
        G_OBJECT_CLASS (gsd_ubuntuone_parent_class)->dispose (object);
174
 
}
175
 
 
176
 
#ifdef HAVE_GSD_30
177
 
static void
178
 
gsd_ubuntuone_class_finalize (GsdUbuntuOneClass *klass)
179
 
{
180
 
}
181
 
#endif
182
 
 
183
 
static void
184
 
gsd_ubuntuone_class_init (GsdUbuntuOneClass *klass)
185
 
{
186
 
        GObjectClass *g_class = G_OBJECT_CLASS (klass);
187
 
        GnomeSettingsPluginClass *gsp_class = (GnomeSettingsPluginClass *) klass;
188
 
 
189
 
        g_class->dispose = gsd_ubuntuone_dispose;
190
 
        gsp_class->activate = gsd_ubuntuone_activate;
191
 
}