~ubuntu-branches/ubuntu/oneiric/ubuntuone-client/oneiric

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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
 * UbuntuOne Nautilus plugin
 *
 * Authors: Rodrigo Moya <rodrigo.moya@canonical.com>
 *
 * Copyright 2009-2010 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n-lib.h>
#include <libsyncdaemon/libsyncdaemon.h>
#include "context-menu.h"
#include "location-widget.h"
#include "share-dialog.h"

typedef struct {
	UbuntuOneNautilus *uon;
	gchar *path;
	GtkWidget *parent;

	/* Whether to make a file public or private */
	gboolean make_public;
} MenuCallbackData;

static void
free_menu_cb_data (gpointer data, GObject *where_the_object_was)
{
	MenuCallbackData *cb_data = (MenuCallbackData *) data;

	g_free (cb_data->path);
	g_free (cb_data);
}

/* Menu callbacks */
static void
got_public_meta (SyncdaemonFilesystemInterface *interface,
		 gboolean success,
		 SyncdaemonMetadata *metadata,
		 gpointer user_data)
{
	MenuCallbackData *data = (MenuCallbackData *) user_data;
	const gchar * share_id, * node_id;
	SyncdaemonInterface *public;

	if (!success) {
		g_warning ("ERROR: getting metadata for public file");
		return;
	}

	share_id = syncdaemon_metadata_get_share_id (metadata);
	node_id = syncdaemon_metadata_get_node_id (metadata);

	public = syncdaemon_daemon_get_publicfiles_interface (data->uon->syncdaemon);
	if (public != NULL) {
		syncdaemon_publicfiles_interface_change_public_access (SYNCDAEMON_PUBLICFILES_INTERFACE (public),
								       share_id, node_id, data->make_public);
	}
}

static void
unsubscribe_folder_cb (NautilusMenuItem *item, gpointer user_data)
{
	SyncdaemonInterface *interface;
	MenuCallbackData * data = (MenuCallbackData *) user_data;

	/* Perform the removal of this folder */
	interface = syncdaemon_daemon_get_folders_interface (data->uon->syncdaemon);
	if (interface != NULL) {
		SyncdaemonFolderInfo *folder_info;

		folder_info = syncdaemon_folders_interface_get_info (SYNCDAEMON_FOLDERS_INTERFACE (interface),
								     data->path);
		if (folder_info != NULL) {
			if (ubuntuone_check_shares_and_public_files (data->uon, folder_info, data->parent)) {
				syncdaemon_folders_interface_delete (SYNCDAEMON_FOLDERS_INTERFACE (interface),
								     syncdaemon_folder_info_get_volume_id (folder_info));
			}
			g_object_unref (G_OBJECT (folder_info));
		}
	}
}

static void
subscribe_folder_cb (NautilusMenuItem *item, gpointer user_data)
{
	SyncdaemonInterface *interface;
	MenuCallbackData * data = (MenuCallbackData *) user_data;

	/* Perform the addition of this folder */
	interface = syncdaemon_daemon_get_folders_interface (data->uon->syncdaemon);
	if (interface != NULL) {
		/* If there is no user authenticated, make Syncdaemon do so */
		if (!syncdaemon_authentication_has_credentials (syncdaemon_daemon_get_authentication (data->uon->syncdaemon)))
			syncdaemon_daemon_connect (data->uon->syncdaemon);
		syncdaemon_folders_interface_create (SYNCDAEMON_FOLDERS_INTERFACE (interface),
						     data->path);
	}
}

static void
copy_public_url_cb (NautilusMenuItem *item, gpointer user_data)
{
	MenuCallbackData * data = (MenuCallbackData *) user_data;
	gchar * url;

	url = g_hash_table_lookup (data->uon->public, data->path);
	gtk_clipboard_set_text (gtk_clipboard_get(GDK_SELECTION_CLIPBOARD),
				url, strlen (url));
	gtk_clipboard_store (gtk_clipboard_get(GDK_SELECTION_CLIPBOARD));
}

static void
toggle_publicity_cb (NautilusMenuItem * item, gpointer user_data)
{
	SyncdaemonFilesystemInterface *interface;
	MenuCallbackData * data = (MenuCallbackData *) user_data;

	interface = (SyncdaemonFilesystemInterface *) syncdaemon_daemon_get_filesystem_interface (data->uon->syncdaemon);
	if (interface != NULL) {
		/* we know this will not be a directory (so no need for _and_quick_tree_synced) */
		syncdaemon_filesystem_interface_get_metadata_async (interface, data->path, FALSE,
								    (SyncdaemonGotMetadataFunc) got_public_meta, data);
	}

	g_hash_table_replace (data->uon->public, g_strdup (data->path), g_strdup (UPDATE_PENDING));
	file_watcher_update_path (data->uon->file_watcher, data->path);
}

static void
share_folder_cb (NautilusMenuItem *item, gpointer user_data)
{
	MenuCallbackData * data = (MenuCallbackData *) user_data;
	GtkWidget * dialog;

	dialog = share_dialog_new (data->parent, data->uon, data->path);
	gtk_widget_show (dialog);
}

static void
unshare_folder_cb (NautilusMenuItem *item, gpointer user_data)
{
	MenuCallbackData * data = (MenuCallbackData *) user_data;
	SyncdaemonSharesInterface *interface;

	interface = (SyncdaemonSharesInterface *) syncdaemon_daemon_get_shares_interface (data->uon->syncdaemon);
	if (interface != NULL)
		syncdaemon_shares_interface_delete (interface, data->path);
}

static void
toggle_location_cb (NautilusMenuItem *item, gpointer user_data)
{
	GConfClient *conf_client;

	conf_client = gconf_client_get_default ();
	gconf_client_set_bool (conf_client, EXPANDER_SHOWN_KEY, !ubuntuone_is_location_bar_enabled (), NULL);
}

gboolean
check_share_offer_pending (UbuntuOneNautilus *uon, const gchar *path)
{
	GSList *shares, *l;
	SyncdaemonInterface *interface;
	gboolean is_share_offer_pending = FALSE;
	const gchar *node_id;

	interface = syncdaemon_daemon_get_shares_interface (uon->syncdaemon);
	if (SYNCDAEMON_IS_SHARES_INTERFACE (interface)) {
		shares = syncdaemon_shares_interface_get_shared (SYNCDAEMON_SHARES_INTERFACE (interface));
		for (l = shares; l != NULL; l = l->next) {
			SyncdaemonShareInfo *share_info = SYNCDAEMON_SHARE_INFO (l->data);

			if (g_strcmp0 (syncdaemon_share_info_get_path (share_info), path) == 0) {
				node_id = syncdaemon_share_info_get_node_id (share_info);
				if (node_id == NULL)
					is_share_offer_pending = TRUE;
				break;
			}
		}

		g_slist_free (shares);
	}

	return is_share_offer_pending;
}

NautilusMenuItem *
context_menu_new (UbuntuOneNautilus *uon,
		  GtkWidget *window,
		  GList *files)
{
	NautilusFileInfo *file;
	NautilusMenu *submenu;
	NautilusMenuItem *root_item, *menu_item, *urlitem;
	gchar *path, *item, *homedir_path, *path_uri;
	gboolean is_managed, is_root, is_udf, is_public, is_shared, is_pending;
	gboolean is_shared_to_me, is_inhome, is_dir, is_regular, is_symlink;
	gboolean is_share_offer_pending;
	MenuCallbackData *cb_data;

	is_managed = is_root = is_udf = is_public = is_shared = is_pending = FALSE;
	is_shared_to_me = is_inhome = is_dir = is_regular = is_symlink = FALSE;
	is_share_offer_pending = FALSE;

	if (g_list_length (files) != 1)
		return NULL;

	file = NAUTILUS_FILE_INFO (g_list_nth_data (files, 0));
	path_uri = nautilus_file_info_get_uri (file);
	path = g_filename_from_uri (path_uri, NULL, NULL);
	g_free (path_uri);

	if (path == NULL)
		return NULL;

	if (syncdaemon_daemon_is_folder_enabled (uon->syncdaemon, path, &is_root))
		is_managed = TRUE;

	homedir_path = g_strdup_printf ("%s/", g_get_home_dir());
	if (strncmp (path, homedir_path, strlen (homedir_path)) == 0)
		is_inhome = TRUE;
	g_free (homedir_path);

	if ((item = g_hash_table_lookup (uon->udfs, path)) != NULL) {
		is_udf = TRUE;
		if (strcmp (item, UPDATE_PENDING) == 0)
			is_pending = TRUE;
	} else if ((item = g_hash_table_lookup (uon->public, path)) != NULL) {
		is_public = TRUE;
		if (strcmp (item, UPDATE_PENDING) == 0)
			is_pending = TRUE;
	}

	if (ubuntuone_is_folder_shared (uon, path)) {
		is_shared = TRUE;
		if (check_share_offer_pending (uon, path))
			is_share_offer_pending = TRUE;
	}

	if (ubuntuone_is_inside_shares (uon, path))
		is_shared_to_me = TRUE;

	is_dir = nautilus_file_info_is_directory (file);
	is_regular = nautilus_file_info_get_file_type (file) == G_FILE_TYPE_REGULAR;

	is_symlink = g_file_test (path, G_FILE_TEST_IS_SYMLINK);

	cb_data = g_new0 (MenuCallbackData, 1);
	cb_data->uon = uon;
	cb_data->parent = window;
	cb_data->path = g_strdup (path);

	/* Create the root item */
	root_item = nautilus_menu_item_new ("ubuntuone",
					    _("_Ubuntu One"),
					    _("Ubuntu One options"),
					    "ubuntuone");
	submenu = nautilus_menu_new ();
	nautilus_menu_item_set_submenu (root_item, submenu);

	g_object_weak_ref (G_OBJECT (root_item), (GWeakNotify) free_menu_cb_data, cb_data);

	/* Share/unshare */
	if ((is_managed || is_udf) && !is_root && is_dir && !is_symlink) {

		menu_item = nautilus_menu_item_new ("ubuntuone-share",
						    _("_Share..."),
						    _("Share this folder on Ubuntu One"),
						    "ubuntuone");
		if (is_pending)
			g_object_set (menu_item, "sensitive", FALSE, NULL);

		g_signal_connect (menu_item, "activate",
				  G_CALLBACK (share_folder_cb), cb_data);
	} else {
		/* the different tooltips will probably do us no good */
		if (is_root) {
			menu_item = nautilus_menu_item_new ("ubuntuone-noshare-root",
							    _("_Share..."),
							    _("Sorry, you can't share the root of a Ubuntu One volume"),
							    "ubuntuone");
		} else if (!(is_managed || is_udf)) {
			menu_item = nautilus_menu_item_new ("ubuntuone-noshare-unmanaged",
							    _("_Share..."),
							    _("Sorry, you can't share folders not managed by Ubuntu One"),
							    "ubuntuone");
		} else {
			menu_item = nautilus_menu_item_new ("ubuntuone-noshare-unmanaged",
							    _("_Share..."),
							    _("Sorry, you can only share folders"),
							    "ubuntuone");
		}
		g_object_set (menu_item, "sensitive", FALSE, NULL);
	}

	nautilus_menu_append_item (submenu, menu_item);

	if ((is_managed && is_shared) && !is_root && is_dir && !is_symlink) {
		menu_item = nautilus_menu_item_new ("ubuntuone-unshare",
						    _("Stop _Sharing"),
						    _("Stop sharing this folder on Ubuntu One"),
						    "ubuntuone");
		if (is_pending || is_share_offer_pending)
			g_object_set (menu_item, "sensitive", FALSE, NULL);

		g_signal_connect (menu_item, "activate",
				  G_CALLBACK (unshare_folder_cb), cb_data);
		nautilus_menu_append_item (submenu, menu_item);
	}

	/* UDF logic
	 *
	 * XXX: clean this up and separate the logic out and reuse this
	 * and locationbar somewhere (libsd?)
	 */
	menu_item = NULL;

	if (is_dir && is_inhome && !is_symlink) {
		/* UDFs could be happening */
		if (is_managed) {
			if (strcmp (path, uon->managed) == 0) {
				/* the Ubuntu One directory, no UDFs */
				menu_item = nautilus_menu_item_new ("ubuntuone-no-disable-u1",
								    _("Stop Synchronizing This _Folder"),
								    _("Sorry, you can't stop synchronizing ~/Ubuntu One"),
								    "ubuntuone");
				g_object_set (menu_item, "sensitive", FALSE, NULL);
			} else if (is_root) {
				/* the root of a UDF: disabling possible */

				menu_item = nautilus_menu_item_new ("ubuntuone-disable-udf",
								    _("Stop Synchronizing This _Folder"),
								    _("Stop synchronizing this folder with Ubuntu One"),
								    "ubuntuone");

				g_signal_connect (menu_item, "activate",
						  G_CALLBACK (unsubscribe_folder_cb), cb_data);
			}
		} else {
			/* unmanaged */
			menu_item = nautilus_menu_item_new ("ubuntuone-enable-udf",
							    _("Synchronize This _Folder"),
							    _("Start synchronizing this folder with Ubuntu One"),
							    "ubuntuone");

			g_signal_connect (menu_item, "activate",
					  G_CALLBACK (subscribe_folder_cb), cb_data);
		}
	} else {
		if (is_dir) {
			menu_item = nautilus_menu_item_new ("ubuntuone-no-disable-u1",
							    _("Synchronize This _Folder"),
							    _("Sorry, you can only synchronize folders within your home folder"),
							    "ubuntuone");
		} else {
			menu_item = nautilus_menu_item_new ("ubuntuone-no-disable-u1",
							    _("Synchronize This _Folder"),
							    _("Sorry, you can only synchronize folders"),
							    "ubuntuone");
		}

		g_object_set (menu_item, "sensitive", FALSE, NULL);
	}

	if (!menu_item) {
		menu_item = nautilus_menu_item_new ("ubuntuone-no-udf-operation-possible",
						    _("Synchronize This _Folder"),
						    _("Synchronization not possible for this folder"),
						    "ubuntuone");
		g_object_set (menu_item, "sensitive", FALSE, NULL);
	}

	nautilus_menu_append_item (submenu, menu_item);

	/* public files */
	menu_item = urlitem = NULL;

	if (!is_shared_to_me && is_managed && is_regular && !is_symlink) {
		if (is_public) {
			urlitem = nautilus_menu_item_new ("ubuntuone-geturl",
							  _("Copy Web _Link"),
							  _("Copy the Ubuntu One public URL for this file to the clipboard."),
							  "ubuntuone");
			if (is_pending)
				g_object_set (urlitem, "sensitive", FALSE, NULL);
			g_signal_connect (urlitem, "activate",
					  G_CALLBACK (copy_public_url_cb), cb_data);

			menu_item = nautilus_menu_item_new ("ubuntuone-unpublish",
							    _("Stop _Publishing"),
							    _("No longer share this file with everyone via Ubuntu One."),
							    "ubuntuone");
			if (is_pending)
				g_object_set (menu_item, "sensitive", FALSE, NULL);

			cb_data->make_public = FALSE;
		} else {
			menu_item = nautilus_menu_item_new ("ubuntuone-publish",
							    _("_Publish"),
							    _("Make this file available to anyone via Ubuntu One."),
							    "ubuntuone");
			cb_data->make_public = TRUE;
		}
		g_signal_connect (menu_item, "activate",
				  G_CALLBACK (toggle_publicity_cb), cb_data);
	}

	if (!urlitem) {
		urlitem = nautilus_menu_item_new ("ubuntuone-geturl",
						  _("Copy Web _Link"),
						  _("Sorry, no public URL for this file via Ubuntu One."),
						  "ubuntuone");
		g_object_set (urlitem, "sensitive", FALSE, NULL);
	}

	if (!menu_item) {
		menu_item = nautilus_menu_item_new ("ubuntuone-publish",
						    _("_Publish"),
						    _("Sorry, unable to publish via Ubuntu One."),
						    "ubuntuone");
		g_object_set (menu_item, "sensitive", FALSE, NULL);
	}

	nautilus_menu_append_item (submenu, menu_item);
	nautilus_menu_append_item (submenu, urlitem);

	/* location bar enable/disable */
	if (ubuntuone_is_location_bar_enabled ()) {
		menu_item = nautilus_menu_item_new ("ubuntuone-location-hide",
						    _("Hide _Ribbon"),
						    _("Do not show the Ubuntu One ribbon"
						      " in selected folders"),
						    "ubuntuone");
	} else {
		menu_item = nautilus_menu_item_new ("ubuntuone-location-show",
						    _("Show _Ribbon in Some Folders"),
						    _("Show the Ubuntu One ribbon"
						      " in selected folders"),
						    "ubuntuone");
	}

	g_signal_connect (menu_item, "activate",
			  G_CALLBACK (toggle_location_cb), cb_data);
	nautilus_menu_append_item (submenu, menu_item);

	g_free (path);
	return root_item;
}