~didrocks/ubuntuone-client/dont-suffer-zg-crash

« back to all changes in this revision

Viewing changes to nautilus/location-widget.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2010-06-23 23:08:15 UTC
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20100623230815-4m3ugh10u9x9xzw5
Tags: upstream-1.3.2
ImportĀ upstreamĀ versionĀ 1.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * UbuntuOne Nautilus plugin
 
3
 *
 
4
 * Authors: Rodrigo Moya <rodrigo.moya@canonical.com>
 
5
 *
 
6
 * Copyright 2009-2010 Canonical Ltd.
 
7
 *
 
8
 * This program is free software: you can redistribute it and/or modify it
 
9
 * under the terms of the GNU General Public License version 3, as published
 
10
 * by the Free Software Foundation.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
14
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
15
 * PURPOSE.  See the GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License along
 
18
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 */
 
21
 
 
22
#include <glib/gi18n.h>
 
23
#include <libsyncdaemon/syncdaemon-folders-interface.h>
 
24
#include <libsyncdaemon/syncdaemon-shares-interface.h>
 
25
#include "location-widget.h"
 
26
 
 
27
G_DEFINE_TYPE(LocationWidget, location_widget, GTK_TYPE_HBOX)
 
28
 
 
29
static void
 
30
set_label_text (LocationWidget *location, gboolean enabled)
 
31
{
 
32
        gchar *labeltext;
 
33
 
 
34
        labeltext = g_strdup_printf ("<b>Ubuntu One</b> %s",
 
35
                                     enabled ? _("enabled") : _("disabled"));
 
36
        gtk_label_set_markup (GTK_LABEL (location->info_label), labeltext);
 
37
        g_free (labeltext);
 
38
}
 
39
 
 
40
static void
 
41
folder_created_cb (SyncdaemonDaemon *daemon, gboolean success, SyncdaemonFolderInfo *folder_info, gpointer user_data)
 
42
{
 
43
        const gchar *path;
 
44
        LocationWidget *location = LOCATION_WIDGET (user_data);
 
45
 
 
46
        path = syncdaemon_folder_info_get_path (folder_info);
 
47
        g_warning ("in folder_created_cb with path = %s", path);
 
48
        if (g_strcmp0 (path, location->path) == 0) {
 
49
                set_label_text (location, success);
 
50
                if (success) {
 
51
                        gtk_widget_hide (location->enable_button);
 
52
                        gtk_widget_show (location->disable_button);
 
53
                        gtk_widget_set_sensitive (location->disable_button, TRUE);
 
54
                } else {
 
55
                        gtk_widget_show (location->enable_button);
 
56
                        gtk_widget_set_sensitive (location->enable_button, TRUE);
 
57
                        gtk_widget_hide (location->disable_button);
 
58
                        ubuntuone_show_error_dialog (location->uon, _("Error enabling folder"),
 
59
                                                     _("Could not enable folder %s for synchronizing to Ubuntu One"),
 
60
                                                     location->path);
 
61
                }
 
62
        }
 
63
}
 
64
 
 
65
static void
 
66
folder_deleted_cb (SyncdaemonDaemon *daemon, gboolean success, SyncdaemonFolderInfo *folder_info, gpointer user_data)
 
67
{
 
68
        const gchar *path;
 
69
        LocationWidget *location = LOCATION_WIDGET (user_data);
 
70
 
 
71
        path = syncdaemon_folder_info_get_path (folder_info);
 
72
        if (g_strcmp0 (path, location->path) == 0) {
 
73
                if (success) {
 
74
                        set_label_text (location, FALSE);
 
75
                        gtk_widget_show (location->enable_button);
 
76
                        gtk_widget_set_sensitive (location->enable_button, TRUE);
 
77
                        gtk_widget_hide (location->disable_button);
 
78
                } else {
 
79
                        set_label_text (location, TRUE);
 
80
                        gtk_widget_hide (location->enable_button);
 
81
                        gtk_widget_show (location->disable_button);
 
82
                        gtk_widget_set_sensitive (location->disable_button, TRUE);
 
83
                        ubuntuone_show_error_dialog (location->uon, _("Error disabling folder"),
 
84
                                                     _("Could not disable folder %s for synchronizing to Ubuntu One"),
 
85
                                                     location->path);
 
86
                }
 
87
        }
 
88
}
 
89
 
 
90
static void
 
91
location_widget_finalize (GObject *object)
 
92
{
 
93
        LocationWidget *location = LOCATION_WIDGET (object);
 
94
 
 
95
        g_signal_handlers_disconnect_by_func (location->uon->syncdaemon, folder_created_cb, location);
 
96
        g_signal_handlers_disconnect_by_func (location->uon->syncdaemon, folder_deleted_cb, location);
 
97
 
 
98
        if (location->path != NULL)
 
99
                g_free (location->path);
 
100
 
 
101
        G_OBJECT_CLASS (location_widget_parent_class)->finalize (object);
 
102
}
 
103
 
 
104
static void
 
105
location_widget_class_init (LocationWidgetClass *klass)
 
106
{
 
107
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
108
 
 
109
        object_class->finalize = location_widget_finalize;
 
110
}
 
111
 
 
112
static void
 
113
disable_u1_cb (GtkButton *button, gpointer user_data)
 
114
{
 
115
        SyncdaemonInterface *interface;
 
116
        gchar *labeltext;
 
117
        LocationWidget *location = LOCATION_WIDGET (user_data);
 
118
 
 
119
        /* Update label to show progress */
 
120
        labeltext = g_strdup_printf (_("Disabling %s folder for synchronization on Ubuntu One..."), location->path);
 
121
        gtk_label_set_text (GTK_LABEL (location->info_label), labeltext);
 
122
        g_free (labeltext);
 
123
        gtk_widget_set_sensitive (location->disable_button, FALSE);
 
124
 
 
125
        /* Perform the removal of this folder */
 
126
        interface = syncdaemon_daemon_get_folders_interface (location->uon->syncdaemon);
 
127
        if (interface != NULL) {
 
128
                SyncdaemonFolderInfo *folder_info;
 
129
 
 
130
                folder_info = syncdaemon_folders_interface_get_info (
 
131
                        SYNCDAEMON_FOLDERS_INTERFACE (interface),
 
132
                        location->path);
 
133
                if (folder_info != NULL) {
 
134
                        syncdaemon_folders_interface_delete (
 
135
                                SYNCDAEMON_FOLDERS_INTERFACE (interface),
 
136
                                syncdaemon_folder_info_get_volume_id (folder_info));
 
137
 
 
138
                        g_object_unref (G_OBJECT (folder_info));
 
139
                }
 
140
        }
 
141
}
 
142
 
 
143
static void
 
144
enable_u1_cb (GtkButton *button, gpointer user_data)
 
145
{
 
146
        SyncdaemonInterface *interface;
 
147
        gchar *labeltext;
 
148
        LocationWidget *location = LOCATION_WIDGET (user_data);
 
149
 
 
150
        /* Update label to show progress */
 
151
        labeltext = g_strdup_printf (_("Enabling %s folder for synchronization on Ubuntu One..."), location->path);
 
152
        gtk_label_set_text (GTK_LABEL (location->info_label), labeltext);
 
153
        g_free (labeltext);
 
154
        gtk_widget_set_sensitive (location->enable_button, FALSE);
 
155
 
 
156
        /* Perform the addition of this folder */
 
157
        interface = syncdaemon_daemon_get_folders_interface (location->uon->syncdaemon);
 
158
        if (interface != NULL) {
 
159
                syncdaemon_folders_interface_create (SYNCDAEMON_FOLDERS_INTERFACE (interface),
 
160
                                                     location->path);
 
161
        }
 
162
}
 
163
 
 
164
static void
 
165
location_widget_init (LocationWidget *location)
 
166
{
 
167
        location->path = NULL;
 
168
        location->info_label = location->enable_button = NULL;
 
169
 
 
170
        /* Create the widgets in the interface */
 
171
        location->info_label = gtk_label_new ("<b>Ubuntu One</b>");
 
172
        gtk_label_set_use_markup (GTK_LABEL (location->info_label), TRUE);
 
173
        gtk_misc_set_alignment (GTK_MISC (location->info_label), 0.0, 0.5);
 
174
        gtk_widget_show (location->info_label);
 
175
        gtk_box_pack_start (GTK_BOX (location), location->info_label, TRUE, FALSE, 0);
 
176
 
 
177
        location->disable_button = gtk_button_new_with_label (_("Disable"));
 
178
        g_signal_connect (G_OBJECT (location->disable_button), "clicked",
 
179
                          G_CALLBACK (disable_u1_cb), location);
 
180
        gtk_box_pack_end (GTK_BOX (location), location->disable_button, FALSE, FALSE, 0);
 
181
 
 
182
        location->enable_button = gtk_button_new_with_label (_("Enable"));
 
183
        g_signal_connect (G_OBJECT (location->enable_button), "clicked",
 
184
                          G_CALLBACK (enable_u1_cb), location);
 
185
        gtk_box_pack_end (GTK_BOX (location), location->enable_button, FALSE, FALSE, 0);
 
186
}
 
187
 
 
188
GtkWidget *
 
189
location_widget_new (UbuntuOneNautilus *uon, const gchar *path)
 
190
{
 
191
        LocationWidget *location;
 
192
        gboolean is_root;
 
193
 
 
194
        g_return_val_if_fail (uon != NULL, NULL);
 
195
        g_return_val_if_fail (path != NULL, NULL);
 
196
 
 
197
        location = (LocationWidget *) g_object_new (TYPE_LOCATION_WIDGET, NULL);
 
198
        location->uon = uon;
 
199
        location->path = g_strdup (path);
 
200
 
 
201
        /* Connect to Syncdaemon signals */
 
202
        g_signal_connect (G_OBJECT (uon->syncdaemon), "folder_created",
 
203
                          G_CALLBACK (folder_created_cb), location);
 
204
        g_signal_connect (G_OBJECT (uon->syncdaemon), "folder_deleted",
 
205
                          G_CALLBACK (folder_deleted_cb), location);
 
206
 
 
207
        if (ubuntuone_is_storagefs (uon, path, &is_root)) {
 
208
                /* Create label and disable button */
 
209
                set_label_text (location, TRUE);
 
210
                if (is_root && strcmp (path, uon->managed) != 0)
 
211
                        gtk_widget_show (location->disable_button);
 
212
        } else {
 
213
                set_label_text (location, FALSE);
 
214
                gtk_widget_show (location->enable_button);
 
215
        }
 
216
 
 
217
        gtk_widget_show (GTK_WIDGET (location));
 
218
 
 
219
        return GTK_WIDGET (location);
 
220
}