eog-postasa-plugin.c 33.7 KB
Newer Older
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* Postasa -- PicasaWeb Uploader plugin
 *
 * Copyright (C) 2009 The Free Software Foundation
 *
 * Author: Richard Schwarting <aquarichy@gmail.com>
 * Initially based on Postr code by: Lucas Rocha
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **/

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

#include "eog-postasa-plugin.h"

#include <gmodule.h>
#include <glib/gi18n-lib.h>

#include <eog/eog-debug.h>
#include <eog/eog-thumb-view.h>
#include <eog/eog-image.h>
37
#include <eog/eog-window-activatable.h>
38 39 40

#include <gdata/gdata.h>

41
#define EOG_POSTASA_RESOURCE_PREFIX "/org/gnome/eog/plugins/postasa"
42 43
#define GTKBUILDER_CONFIG_FILE EOG_POSTASA_RESOURCE_PREFIX"/postasa-config.ui"
#define GTKBUILDER_UPLOAD_FILE EOG_POSTASA_RESOURCE_PREFIX"/postasa-uploads.ui"
44

Felix Riemann's avatar
Felix Riemann committed
45 46 47
#define EOG_POSTASA_PLUGIN_MENU_ID "EogPluginPostasa"
#define EOG_POSTASA_PLUGIN_ACTION "postasa-upload"

48 49 50 51 52
enum {
	PROP_O,
	PROP_WINDOW
};

53 54 55 56 57 58 59 60
/**
 * _EogPostasaPluginPrivate:
 *
 * Private data for the Postasa EoG plugin
 **/
struct _EogPostasaPluginPrivate
{
	EogWindow    *eog_window;
61 62 63 64
	GtkActionGroup *ui_action_group;
	guint ui_id;


65
#ifdef HAVE_LIBGDATA_0_9
66 67
	GDataClientLoginAuthorizer *authorizer;
#endif
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	GDataPicasaWebService *service;
	GCancellable *login_cancellable;

	/* TODO: consider using GConf to store the username in; perhaps not the password */
	GtkDialog    *login_dialog;
	GtkEntry     *username_entry;
	GtkEntry     *password_entry;
	GtkLabel     *login_message;
	GtkButton    *login_button;
	GtkButton    *cancel_button;
	gboolean      uploads_pending;

	GtkWindow    *uploads_window;
	GtkTreeView  *uploads_view;
	GtkListStore *uploads_store;
};

85 86 87 88 89 90 91 92 93
static void
eog_window_activatable_iface_init (EogWindowActivatableInterface *iface);

G_DEFINE_DYNAMIC_TYPE_EXTENDED (EogPostasaPlugin, eog_postasa_plugin,
		PEAS_TYPE_EXTENSION_BASE, 0,
		G_ADD_PRIVATE_DYNAMIC(EogPostasaPlugin)
		G_IMPLEMENT_INTERFACE_DYNAMIC(EOG_TYPE_WINDOW_ACTIVATABLE,
					eog_window_activatable_iface_init))

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * PicasaWebUploadFileAsyncData:
 *
 * Small chunk of data for use by our asynchronous PicasaWeb upload
 * API.  It describes the position in the upload window's tree view, for
 * cancellation purposes, and the the image file that we want to
 * upload.
 *
 * TODO: remove this and the async API when we get a proper upload
 * async method into libgdata.
 **/
typedef struct
{
	GtkTreeIter *iter;
	GFile *imgfile;
} PicasaWebUploadFileAsyncData;

Felix Riemann's avatar
Felix Riemann committed
111
static void picasaweb_upload_cb (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
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
static GtkWidget *login_get_dialog (EogPostasaPlugin *plugin);
static gboolean login_dialog_close (EogPostasaPlugin *plugin);

/*** Uploads Dialog ***/

/**
 * uploads_cancel_row:
 *
 * Function on the upload list store to cancel a specified upload.
 * This is intended to be called by foreach functions.
 *
 * There's a small chance that the #GCancellable cancellation might not
 * occur before the upload is completed, in which case we do not
 * change the row's status to "Cancelled".
 **/
static gboolean
uploads_cancel_row (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, EogPostasaPlugin *plugin)
{
	GCancellable *cancellable;

	gtk_tree_model_get (model, iter, 4, &cancellable, -1);
	g_cancellable_cancel (cancellable);
	/* we let picasaweb_upload_async_cb() handle setting a Cancelled message */

	return FALSE;
}

/**
 * uploads_cancel_cb:
 *
 * Obtains the current selection and calls an API to cancel each one.
 **/
static void
uploads_cancel_cb (GtkWidget *cancel_button, EogPostasaPlugin *plugin)
{
	GtkTreeSelection *selection;
	selection = gtk_tree_view_get_selection (plugin->priv->uploads_view); /* don't unref */

	gtk_tree_selection_selected_foreach (selection, (GtkTreeSelectionForeachFunc)uploads_cancel_row, plugin);
}

/**
 * uploads_cancel_all_cb:
 *
 * Calls an API on every row to cancel its upload.
 **/
static void
uploads_cancel_all_cb (GtkWidget *cancel_all_button, EogPostasaPlugin *plugin)
{
	gtk_tree_model_foreach (GTK_TREE_MODEL (plugin->priv->uploads_store), (GtkTreeModelForeachFunc)uploads_cancel_row, plugin);
}

/**
 * uploads_get_dialog:
 *
 * Returns the a #GtkWindow representing the Uploads window.  If it
 * has not already been created, it creates it.  The Uploads window is
 * set to be hidden instead of destroyed when closed, to avoid having
 * to recreate it and re-parse the UI file, etc.
 **/
static GtkWindow *
uploads_get_dialog (EogPostasaPlugin *plugin)
{
	GtkBuilder *builder;
	GError *error = NULL;
	GtkButton *cancel_button;
	GtkButton *cancel_all_button;

	if (plugin->priv->uploads_window == NULL) {
		builder = gtk_builder_new ();
182
		gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
183 184
		gtk_builder_add_from_resource (builder, GTKBUILDER_UPLOAD_FILE,
		                               &error);
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
		if (error != NULL) {
			g_warning ("Couldn't load Postasa uploads UI file:%d:%s", error->code, error->message);
			g_error_free (error);
			return NULL;
		}

		/* note: do not unref gtk_builder_get_object() returns */
		plugin->priv->uploads_window = GTK_WINDOW     (gtk_builder_get_object (builder, "uploads_window"));
		plugin->priv->uploads_view   = GTK_TREE_VIEW  (gtk_builder_get_object (builder, "uploads_view"));
		plugin->priv->uploads_store  = GTK_LIST_STORE (gtk_builder_get_object (builder, "uploads_store"));

		cancel_button     = GTK_BUTTON (gtk_builder_get_object (builder, "cancel_button"));
		cancel_all_button = GTK_BUTTON (gtk_builder_get_object (builder, "cancel_all_button"));

		/* TODO: can't set expand = TRUE when packing cells into columns via glade-3/GtkBuilder apparently?
		   bgo #602152  So for now, we take them, clear them out, and remap them.  Ugh.  Better solutions welcome.  */
		GtkTreeViewColumn *file_col       = GTK_TREE_VIEW_COLUMN (gtk_builder_get_object (builder, "file_col"));
		GtkCellRenderer   *thumbnail_cell = GTK_CELL_RENDERER    (gtk_builder_get_object (builder, "thumbnail_cell"));
		GtkCellRenderer   *filepath_cell  = GTK_CELL_RENDERER    (gtk_builder_get_object (builder, "filepath_cell"));
		gtk_tree_view_column_clear (file_col);
		gtk_tree_view_column_pack_start (file_col, thumbnail_cell, FALSE);
		gtk_tree_view_column_pack_end (file_col, filepath_cell, TRUE);
		gtk_tree_view_column_add_attribute (file_col, thumbnail_cell, "pixbuf", 0);
		gtk_tree_view_column_add_attribute (file_col, filepath_cell, "text", 1);
		GtkTreeViewColumn *progress_col   = GTK_TREE_VIEW_COLUMN (gtk_builder_get_object (builder, "progress_col"));
		GtkCellRenderer   *progress_cell  = GTK_CELL_RENDERER    (gtk_builder_get_object (builder, "progress_cell"));
		gtk_tree_view_column_clear (progress_col);
		gtk_tree_view_column_pack_end (progress_col, progress_cell, TRUE);
		gtk_tree_view_column_add_attribute (progress_col, progress_cell, "pulse", 3);
		gtk_tree_view_column_add_attribute (progress_col, progress_cell, "text", 5);

		g_object_unref (builder);

		g_signal_connect (G_OBJECT (cancel_button),     "clicked", G_CALLBACK (uploads_cancel_cb), plugin);
		g_signal_connect (G_OBJECT (cancel_all_button), "clicked", G_CALLBACK (uploads_cancel_all_cb), plugin);
		g_signal_connect (G_OBJECT (plugin->priv->uploads_window), "delete-event", G_CALLBACK (gtk_widget_hide_on_delete), plugin);
	}

	return plugin->priv->uploads_window;
}

typedef struct {
	EogPostasaPlugin *plugin;
	GtkTreeIter iter;
} PulseData;

static gboolean
pulse (PulseData *data)
{
	gint status;
	GCancellable *cancellable;

	gtk_tree_model_get (GTK_TREE_MODEL (data->plugin->priv->uploads_store), &(data->iter), 3, &status, 4, &cancellable, -1);

	if (0 <= status && status < G_MAXINT && g_cancellable_is_cancelled (cancellable) == FALSE) {
		/* TODO: consider potential for races and how g_timeout_add works wrt threading; none seen in testing, though */
		gtk_list_store_set (data->plugin->priv->uploads_store, &(data->iter), 3, status+1, -1);
		return TRUE;
	} else {
		/* either we've failed, <0, or we're done, G_MAX_INT */
		g_slice_free (PulseData, data);
		return FALSE;
	}
}

/**
 * uploads_add_entry:
 *
 * Adds a new row to the Uploads tree view for an #EogImage to upload.
 * The row stores the upload's #GCancellable and returns.
 *
 * Returns: a #GtkTreeIter that should be freed with g_slice_free()
 **/
static GtkTreeIter *
uploads_add_entry (EogPostasaPlugin *plugin, EogImage *image, GCancellable *cancellable)
{
	GtkWindow *uploads_window;
	GdkPixbuf *thumbnail_pixbuf;
	GdkPixbuf *scaled_pixbuf;
	gchar *size, *uri;
	GtkTreeIter *iter;

	/* display the Uploads window got from the plugin */
	uploads_window = uploads_get_dialog (plugin);
	gtk_widget_show_all (GTK_WIDGET (uploads_window));

	/* obtain the data describing the upload */
	/* TODO: submit patch with documentaiton for eog_image_get_*,
	   particularly what needs unrefing */
	uri = eog_image_get_uri_for_display (image);
	thumbnail_pixbuf = eog_image_get_thumbnail (image);
276
	if (thumbnail_pixbuf && GDK_IS_PIXBUF (thumbnail_pixbuf)) {
277 278 279
		scaled_pixbuf = gdk_pixbuf_scale_simple (thumbnail_pixbuf, 32, 32, GDK_INTERP_BILINEAR);
		g_object_unref (thumbnail_pixbuf);
	} else {
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
		/* This is currently a workaround due to limitations in eog's
		 * eog's thumbnailing mechanism */
		GError *error = NULL;
		GtkIconTheme *icon_theme;

		icon_theme = gtk_icon_theme_get_default ();

		scaled_pixbuf = gtk_icon_theme_load_icon (icon_theme,
							  "image-x-generic",
							  32, 0, &error);

		if (!scaled_pixbuf) {
			g_warning ("Couldn't load icon: %s", error->message);
			g_error_free (error);
		}
295
	}
296 297
	size = g_strdup_printf ("%" G_GOFFSET_FORMAT "KB",
				eog_image_get_bytes (image) / 1024);
298 299 300 301 302 303 304 305 306
	iter = g_slice_new0 (GtkTreeIter);

	/* insert the data into the upload's list store */
	gtk_list_store_insert_with_values (plugin->priv->uploads_store, iter, 0,
					   0, scaled_pixbuf,
					   1, uri,
					   2, size,
					   3, 50, /* upload status: set to G_MAXINT when done, to 0 to not start */
					   4, cancellable,
307
					   5, _("Uploading…"),
308
					   -1); /* TODO: where should cancellabe, scaled_pixbuf be unref'd? don't worry about it since
309
						   they'll exist until EoG exits anyway? or in eog_postasa_plugin_dispose()? */
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
	g_free (uri);
	g_free (size);
	g_object_unref (scaled_pixbuf);

	/* Set the progress bar to pulse every 50ms; freed in pulse() when upload is no longer in progress */
	PulseData *data; /* just needs to be freed with g_slice_free() */
	data = g_slice_new0 (PulseData);
	data->plugin = plugin;
	data->iter = *iter;
	g_timeout_add (50, (GSourceFunc)pulse, data);

	return iter;
}

static void
free_picasaweb_upload_file_async_data (PicasaWebUploadFileAsyncData *data)
{
	g_object_unref (data->imgfile);
	g_slice_free (GtkTreeIter, data->iter);
	g_slice_free (PicasaWebUploadFileAsyncData, data);
}

/**
 * picasaweb_upload_async_cb:
 *
 * Handles completion of the image's asynchronous upload to PicasaWeb.
 *
 * If the #GAsyncResults indicates success, we'll update the
 * treeview's row for the given upload indicating this.  Elsewise, if
 * it wasn't cancelled, then we report an error.
 *
 * NOTE: we also clean up the #PicasaWebUploadFileAsyncData here.
 *
 * TODO: we don't yet make the progress bar throb, how do we do that?
 *
 **/
static void
picasaweb_upload_async_cb (EogPostasaPlugin *plugin, GAsyncResult *res, PicasaWebUploadFileAsyncData *data)
{
	GCancellable* cancellable;
	GError *error = NULL; /* TODO: make sure to clear all set errors */

	if (g_simple_async_result_get_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res)) == TRUE) {
353
		gtk_list_store_set (plugin->priv->uploads_store, data->iter, 3, G_MAXINT, 5, _("Uploaded"), -1);
354 355 356
	} else {
		gtk_tree_model_get (GTK_TREE_MODEL (plugin->priv->uploads_store), data->iter, 4, &cancellable, -1);
		if (g_cancellable_is_cancelled (cancellable) == TRUE) {
357
			gtk_list_store_set (plugin->priv->uploads_store, data->iter, 3, -1, 5, _("Cancelled"), -1);
358 359
		} else {
			g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), &error);
360
			gtk_list_store_set (plugin->priv->uploads_store, data->iter, 3, -1, 5, error ? error->message : _("Failed"), -1);
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
			g_clear_error (&error);
		}
	}

	free_picasaweb_upload_file_async_data (data);
}

/*** PicasaWeb ***/

/**
 * tmp_picasaweb_upload_async:
 *
 * Temporary solution to provide asynchronous uploading and stop
 * blocking the UI until gdata_picasaweb_service_upload_file_async()
 * becomes available (bgo #600262).  This method does the synchronous
 * file upload, but is run asynchronously via
 * g_simple_async_result_run_in_thread().
 *
 * This sets up a minimal #GDataPicasaWebFile entry, using the
 * basename of the filepath for the file's title (which is not the
 * caption, but might be something we would consider doing).  The
 * image file and the minimal entry are then uploaded to PicasaWeb's
 * default album of "Drop Box".  In the future, we might consider
 * adding an Album Chooser to the Preferences/login window, but only
 * if there's demand.
 **/
static void
tmp_picasaweb_upload_async (GSimpleAsyncResult *result, GObject *source_object, GCancellable *cancellable)
{
	GDataPicasaWebFile *new_file = NULL;
	EogPostasaPlugin *plugin = EOG_POSTASA_PLUGIN (source_object);
392
	GDataPicasaWebService *service = plugin->priv->service;
393 394
	GDataPicasaWebFile *file_entry;
	PicasaWebUploadFileAsyncData *data;
395
#ifdef HAVE_LIBGDATA_0_8
396 397 398 399
	GDataUploadStream *upload_stream;
	GFileInputStream *in_stream;
	GFileInfo *file_info;
#endif
400 401 402 403 404 405 406 407 408 409 410
	gchar *filename;
	GError *error = NULL;

	data = (PicasaWebUploadFileAsyncData*)g_async_result_get_user_data (G_ASYNC_RESULT (result));

	/* get filename to set image title */
	file_entry = gdata_picasaweb_file_new (NULL);
	filename = g_file_get_basename (data->imgfile);
	gdata_entry_set_title (GDATA_ENTRY (file_entry), filename);
	g_free (filename);

411
#ifdef HAVE_LIBGDATA_0_8
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 465 466
	file_info = g_file_query_info (data->imgfile,
				      G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
				      G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
				      G_FILE_QUERY_INFO_NONE, cancellable,
				      &error);

	if (file_info == NULL)
		goto got_err;

	upload_stream = gdata_picasaweb_service_upload_file (service,
				      NULL /* Upload to Dropbox */, file_entry,
				      g_file_info_get_display_name (file_info),
				      g_file_info_get_content_type (file_info),
				      cancellable, &error);
	g_object_unref (file_info);

	if (upload_stream == NULL)
		goto got_err;

	in_stream = g_file_read (data->imgfile, cancellable, &error);

	if (in_stream == NULL) {
		g_object_unref (upload_stream);
		goto got_err;
	}

	if (g_output_stream_splice (G_OUTPUT_STREAM (upload_stream),
				    G_INPUT_STREAM (in_stream),
				    G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE |
				    G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
				    cancellable, &error) == -1)
	{
		g_object_unref (upload_stream);
		g_object_unref (in_stream);
		goto got_err;
	}


	new_file = gdata_picasaweb_service_finish_file_upload (service,
							       upload_stream,
							       &error);

	g_object_unref (upload_stream);
	g_object_unref (in_stream);
got_err:
	/* Jump here if any GIO/GData call doesn't return successfully.
	 * Error handling happens below. */

#else
	/* libgdata-0.6 */
	new_file = gdata_picasaweb_service_upload_file (service,
					       NULL /* Uploading to Drop Box */,
					       file_entry, data->imgfile,
					       cancellable, &error);
#endif
467 468 469 470 471 472
	g_object_unref (file_entry);

	if (new_file == NULL || error) {
		if (g_cancellable_is_cancelled (cancellable) == FALSE) {
			g_simple_async_result_set_from_error (result, error);
		}
473 474
		/* Clear errors always as cancelling creates errors too */
		g_clear_error (&error);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	} else {
		g_simple_async_result_set_op_res_gboolean (result, TRUE);
	}

	if (new_file != NULL)
		g_object_unref (new_file);
}


/**
 * picasaweb_upload_files:
 *
 * This obtains the list of selected images in EoG (selected in the
 * thumbview), sets up asynchronous uploads through
 * tmp_picasaweb_upload_async() in their own thread and instigates
 * them.

 * This attempts to upload the selected files.  It provides a message
 * near the end indicating the number successfully uploaded, and any
 * error messages encountered along the way.
 *
 * TODO: once gdata_picasaweb_service_upload_file_async() is available
 * from libgdata, simplify this as possible.
 **/
static void
picasaweb_upload_files (EogPostasaPlugin *plugin)
{
	EogWindow *window;
	GtkWidget *thumbview;
	GList *images, *node;
	EogImage *image;
	GFile *imgfile;
	GCancellable *cancellable;
	GSimpleAsyncResult *result;
	PicasaWebUploadFileAsyncData *data;

511
#ifdef HAVE_LIBGDATA_0_9
512 513
	if (gdata_service_is_authorized (GDATA_SERVICE (plugin->priv->service)) == FALSE) {
#else
514
	if (gdata_service_is_authenticated (GDATA_SERVICE (plugin->priv->service)) == FALSE) {
515
#endif
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
		g_warning ("PicasaWeb could not be authenticated.  Aborting upload.");
		return;
	}

	window = plugin->priv->eog_window;
	thumbview = eog_window_get_thumb_view (window); /* do not unref */
	images = eog_thumb_view_get_selected_images (EOG_THUMB_VIEW (thumbview)); /* need to use g_list_free() */

	for (node = g_list_first (images); node != NULL; node = node->next) {
		image = (EogImage *) node->data;
		cancellable = g_cancellable_new (); /* TODO: this gets attached to the image's list row; free with row */

		imgfile = eog_image_get_file (image); /* unref this */

		data = g_slice_new0(PicasaWebUploadFileAsyncData); /* freed by picasaweb_upload_async_cb() or below */
		data->imgfile = g_file_dup (imgfile); /* unref'd in free_picasaweb_upload_file_async_data() */
		data->iter = uploads_add_entry (plugin, image, cancellable); /* freed with data */

		if (g_file_query_exists (imgfile, cancellable)) {
			/* TODO: want to replace much of this with gdata_picasaweb_service_upload_file_async when that's avail */
			result = g_simple_async_result_new (G_OBJECT (plugin), (GAsyncReadyCallback)picasaweb_upload_async_cb,
							    data, tmp_picasaweb_upload_async); /* TODO: should this be freed? where? */
			g_simple_async_result_run_in_thread (result, tmp_picasaweb_upload_async, 0, cancellable);
		} else {
			/* TODO: consider setting a proper error and passing it in the data through GSimpleAsyncResult's thread */
			gtk_list_store_set (plugin->priv->uploads_store, data->iter, 3, -1, 5, "File not found", -1);
			free_picasaweb_upload_file_async_data (data);
		}
		g_object_unref (imgfile);
	}
	g_list_free (images);
}

/**
 * picasaweb_login_async_cb:
 *
 * Handles the result of the asynchronous
 * gdata_service_authenticate_async() operation, called by our
 * picasaweb_login_cb().  Upon success, it switches "Cancel" to
 * "Close".  Regardless of the response, it re-enables the Login
 * button (which is desensitised during the login attempt).
 **/
558
#ifdef HAVE_LIBGDATA_0_9
559 560 561
static void
picasaweb_login_async_cb (GDataClientLoginAuthorizer *authorizer, GAsyncResult *result, EogPostasaPlugin *plugin)
#else
562 563
static void
picasaweb_login_async_cb (GDataPicasaWebService *service, GAsyncResult *result, EogPostasaPlugin *plugin)
564
#endif
565 566 567 568 569
{
	GError *error = NULL;
	gchar *message;
	gboolean success = FALSE;

570
#ifdef HAVE_LIBGDATA_0_9
571 572 573 574
	success = gdata_client_login_authorizer_authenticate_finish (authorizer,
								     result,
								     &error);
#else
575
	success = gdata_service_authenticate_finish (GDATA_SERVICE (service), result, &error);
576
#endif
577 578 579 580 581 582

	gtk_widget_set_sensitive (GTK_WIDGET (plugin->priv->login_button), TRUE);
	gtk_widget_set_sensitive (GTK_WIDGET (plugin->priv->username_entry), TRUE);
	gtk_widget_set_sensitive (GTK_WIDGET (plugin->priv->password_entry), TRUE);

	if (success == FALSE || error != NULL) {
583
		message = g_strdup_printf (_("Login failed. %s"), error->message);
584 585 586
		gtk_label_set_text (plugin->priv->login_message, message);
		g_free (message);
	} else {
Anders Jonsson's avatar
Anders Jonsson committed
587
		gtk_label_set_text (plugin->priv->login_message, _("Logged in successfully."));
588
		gtk_button_set_label (plugin->priv->cancel_button, _("Close"));
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
		login_dialog_close (plugin);
	}
}

/**
 * picasaweb_login_cb:
 *
 * Handles "clicked" for the Login button.  Attempts to use input from
 * the username and password entries to authenticate.  It does this
 * asynchronously, leaving it to @picasaweb_login_async_cb to handle
 * the result.  It disables the Login button while authenticating
 * (re-enabled when done) and ensures the Close/Cancel button says
 * Cancel (which, incidentally, will call g_cancellable_cancel on the
 * provided #GCancellable.
 **/
static void
picasaweb_login_cb (GtkWidget *login_button, gpointer _plugin)
{
	EogPostasaPlugin *plugin = EOG_POSTASA_PLUGIN (_plugin);

609
	gtk_button_set_label (plugin->priv->cancel_button, _("Cancel"));
610 611 612 613 614
	gtk_widget_set_sensitive (login_button, FALSE);
	gtk_widget_set_sensitive (GTK_WIDGET (plugin->priv->username_entry), FALSE);
	gtk_widget_set_sensitive (GTK_WIDGET (plugin->priv->password_entry), FALSE);

	/* TODO: want to handle passwords more securely */
615
	gtk_label_set_text (plugin->priv->login_message, _("Logging in…"));
616
	g_cancellable_reset (plugin->priv->login_cancellable);
617

618
#ifdef HAVE_LIBGDATA_0_9
619 620 621 622 623 624
	gdata_client_login_authorizer_authenticate_async (
					  plugin->priv->authorizer,
					  gtk_entry_get_text (plugin->priv->username_entry),
					  gtk_entry_get_text (plugin->priv->password_entry),
					  plugin->priv->login_cancellable, (GAsyncReadyCallback)picasaweb_login_async_cb, plugin);
#else
625 626 627 628
	gdata_service_authenticate_async (GDATA_SERVICE (plugin->priv->service),
					  gtk_entry_get_text (plugin->priv->username_entry),
					  gtk_entry_get_text (plugin->priv->password_entry),
					  plugin->priv->login_cancellable, (GAsyncReadyCallback)picasaweb_login_async_cb, plugin);
629
#endif
630 631 632 633 634 635 636 637 638
}

/**
 * picasaweb_upload_cb:
 *
 * This checks that we are authenticated (popping up the login window
 * if we're not) and, if we are, moves on to upload the files.
 **/
static void
Felix Riemann's avatar
Felix Riemann committed
639 640 641
picasaweb_upload_cb (GSimpleAction *simple,
		     GVariant      *parameter,
		     gpointer       user_data)
642
{
Felix Riemann's avatar
Felix Riemann committed
643
	EogPostasaPlugin *plugin;
644
	EogPostasaPluginPrivate *priv;
645

Felix Riemann's avatar
Felix Riemann committed
646 647 648
	g_return_if_fail (EOG_IS_POSTASA_PLUGIN (user_data));

	plugin = EOG_POSTASA_PLUGIN (user_data);
649 650

	priv = plugin->priv;
651

652
#ifdef HAVE_LIBGDATA_0_9
653 654 655 656 657
	if (gdata_service_is_authorized (GDATA_SERVICE (priv->service)) == TRUE)
#else
	if (gdata_service_is_authenticated (GDATA_SERVICE (priv->service)) == TRUE)
#endif
	{
658 659 660
		picasaweb_upload_files (plugin);
	} else {
		/* when the dialog closes, it checks if this is set to see if it should upload anything */
661
		priv->uploads_pending = TRUE;
662 663

		login_get_dialog (plugin);
664 665
		gtk_label_set_text (priv->login_message, _("Please log in to continue upload."));
		gtk_window_present (GTK_WINDOW (priv->login_dialog));
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	}
}


/*** Login Dialog ***/

/**
 * login_dialog_close:
 *
 * Closes the login dialog, used for closing the window or cancelling
 * login.  This will also cancel any authentication in progress.  If
 * the login dialog was prompted by an upload attempt, it will resume
 * the upload attempt.
 **/
static gboolean
login_dialog_close (EogPostasaPlugin *plugin)
{
	/* abort the authentication attempt if in progress and we're cancelling */
	g_cancellable_cancel (plugin->priv->login_cancellable);
	gtk_widget_hide (GTK_WIDGET (plugin->priv->login_dialog));

	if (plugin->priv->uploads_pending == TRUE) {
		plugin->priv->uploads_pending = FALSE;
		picasaweb_upload_files (plugin);
	}

	return TRUE;
}

/**
 * login_dialog_cancel_button_cb:
 *
 * Handles clicks on the Cancel/Close button.
 **/
static gboolean
login_dialog_cancel_button_cb (GtkWidget *cancel_button, EogPostasaPlugin *plugin)
{
703 704 705
	/* Make sure we don't resume uploads */
	plugin->priv->uploads_pending = FALSE;

706 707 708 709 710 711 712 713 714 715 716
	return login_dialog_close (plugin);
}

/**
 * login_dialog_delete_event_cb:
 *
 * Handles other attempts to close the dialog. (e.g. window manager)
 **/
static gboolean
login_dialog_delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer *_plugin)
{
717 718 719
	/* Make sure we don't resume uploads */
	EOG_POSTASA_PLUGIN (_plugin)->priv->uploads_pending = FALSE;

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	return login_dialog_close (EOG_POSTASA_PLUGIN (_plugin));
}

/**
 * login_get_dialog:
 *
 * Retrieves the login dialog.  If it has not yet been constructed, it
 * does so.  If the user is already authenticated, it populates the
 * username and password boxes with the relevant values.
 **/
static GtkWidget *
login_get_dialog (EogPostasaPlugin *plugin)
{
	GtkBuilder *builder;
	GError *error = NULL;

	if (plugin->priv->login_dialog == NULL) {
		builder = gtk_builder_new ();
738
		gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
739 740
		gtk_builder_add_from_resource (builder, GTKBUILDER_CONFIG_FILE,
		                               &error);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
		if (error != NULL) {
			g_warning ("Couldn't load Postasa configuration UI file:%d:%s", error->code, error->message);
			g_error_free (error);
		}

		/* do not unref gtk_builder_get_object() returns */
		plugin->priv->username_entry = GTK_ENTRY  (gtk_builder_get_object (builder, "username_entry"));
		plugin->priv->password_entry = GTK_ENTRY  (gtk_builder_get_object (builder, "password_entry"));
		plugin->priv->login_dialog   = GTK_DIALOG (gtk_builder_get_object (builder, "postasa_login_dialog"));
		plugin->priv->cancel_button  = GTK_BUTTON (gtk_builder_get_object (builder, "cancel_button"));
		plugin->priv->login_button   = GTK_BUTTON (gtk_builder_get_object (builder, "login_button"));
		plugin->priv->login_message  = GTK_LABEL  (gtk_builder_get_object (builder, "login_message"));

		g_object_unref (builder);

		g_signal_connect (G_OBJECT (plugin->priv->login_button),  "clicked", G_CALLBACK (picasaweb_login_cb),     plugin);
		g_signal_connect (G_OBJECT (plugin->priv->cancel_button), "clicked", G_CALLBACK (login_dialog_cancel_button_cb), plugin);
		g_signal_connect (G_OBJECT (plugin->priv->login_dialog), "delete-event", G_CALLBACK (login_dialog_delete_event_cb), plugin);

760
#ifdef HAVE_LIBGDATA_0_9
761 762 763 764
		if (gdata_service_is_authorized (GDATA_SERVICE (plugin->priv->service))) {
			gtk_entry_set_text (plugin->priv->username_entry, gdata_client_login_authorizer_get_username (plugin->priv->authorizer));
			gtk_entry_set_text (plugin->priv->password_entry, gdata_client_login_authorizer_get_password (plugin->priv->authorizer));
#else
765 766 767
		if (gdata_service_is_authenticated (GDATA_SERVICE (plugin->priv->service))) {
			gtk_entry_set_text (plugin->priv->username_entry, gdata_service_get_username (GDATA_SERVICE (plugin->priv->service)));
			gtk_entry_set_text (plugin->priv->password_entry, gdata_service_get_password (GDATA_SERVICE (plugin->priv->service)));
768
#endif
769 770 771 772 773 774 775 776 777
		}
	}

	return GTK_WIDGET (plugin->priv->login_dialog);
}


/*** EogPlugin Functions ***/

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
static void
eog_postasa_plugin_update_action_state (EogPostasaPlugin *plugin)
{
	EogPostasaPluginPrivate *priv = plugin->priv;
	GAction *action;
	EogThumbView *thumbview;
	gboolean enable = FALSE;

	thumbview = EOG_THUMB_VIEW (eog_window_get_thumb_view (priv->eog_window));

	if (G_LIKELY (thumbview))
	{
		enable = (eog_thumb_view_get_n_selected (thumbview) != 0);
	}

	action = g_action_map_lookup_action (G_ACTION_MAP (priv->eog_window),
					     EOG_POSTASA_PLUGIN_ACTION);
	g_simple_action_set_enabled (G_SIMPLE_ACTION (action), enable);
}

static void
_selection_changed_cb (EogThumbView *thumbview, gpointer user_data)
{
	EogPostasaPlugin *plugin = EOG_POSTASA_PLUGIN (user_data);

	if (G_LIKELY (plugin))
		eog_postasa_plugin_update_action_state (plugin);
}

807 808 809 810 811 812 813 814
/**
 * impl_activate:
 *
 * Plugin hook for plugin activation.  Creates #WindowData for the
 * #EogPostasaPlugin that gets associated with the window and defines
 * some UI.
 **/
static void
815
impl_activate (EogWindowActivatable *activatable)
816
{
817
	EogPostasaPlugin *plugin = EOG_POSTASA_PLUGIN (activatable);
Felix Riemann's avatar
Felix Riemann committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
	GMenu *model, *menu;
	GMenuItem *item;
	GSimpleAction *action;

	model= eog_window_get_gear_menu_section (plugin->priv->eog_window,
						 "plugins-section");

	g_return_if_fail (G_IS_MENU (model));

	/* Setup and inject action */
	action = g_simple_action_new (EOG_POSTASA_PLUGIN_ACTION, NULL);
	g_signal_connect(action, "activate",
			 G_CALLBACK (picasaweb_upload_cb), plugin);
	g_action_map_add_action (G_ACTION_MAP (plugin->priv->eog_window),
				 G_ACTION (action));
	g_object_unref (action);

835 836 837 838 839 840
	g_signal_connect (G_OBJECT (eog_window_get_thumb_view (plugin->priv->eog_window)),
			  "selection-changed",
			  G_CALLBACK (_selection_changed_cb),
			  plugin);
	eog_postasa_plugin_update_action_state (plugin);

Felix Riemann's avatar
Felix Riemann committed
841 842 843 844 845 846 847 848 849 850 851 852
	/* Append entry to the window's gear menu */
	menu = g_menu_new ();
	g_menu_append (menu, _("Upload to PicasaWeb"),
		       "win." EOG_POSTASA_PLUGIN_ACTION);

	item = g_menu_item_new_section (NULL, G_MENU_MODEL (menu));
	g_menu_item_set_attribute (item, "id",
				   "s", EOG_POSTASA_PLUGIN_MENU_ID);
	g_menu_append_item (model, item);
	g_object_unref (item);

	g_object_unref (menu);
853 854 855 856 857 858 859 860
}

/**
 * impl_deactivate:
 *
 * Plugin hook for plugin deactivation. Removes UI and #WindowData
 **/
static void
861
impl_deactivate	(EogWindowActivatable *activatable)
862
{
863
	EogPostasaPlugin *plugin = EOG_POSTASA_PLUGIN (activatable);
Felix Riemann's avatar
Felix Riemann committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
	GMenu *menu;
	GMenuModel *model;
	gint i;

	menu = eog_window_get_gear_menu_section (plugin->priv->eog_window,
						 "plugins-section");

	g_return_if_fail (G_IS_MENU (menu));

	/* Remove menu entry */
	model = G_MENU_MODEL (menu);
	for (i = 0; i < g_menu_model_get_n_items (model); i++) {
		gchar *id;
		if (g_menu_model_get_item_attribute (model, i, "id", "s", &id)) {
			const gboolean found =
				(g_strcmp0 (id, EOG_POSTASA_PLUGIN_MENU_ID) == 0);
			g_free (id);

			if (found) {
				g_menu_remove (menu, i);
				break;
			}
		}
	}
888

889 890 891
	g_signal_handlers_disconnect_by_func (eog_window_get_thumb_view (plugin->priv->eog_window),
					      _selection_changed_cb, plugin);

Felix Riemann's avatar
Felix Riemann committed
892 893 894
	/* Finally remove action */
	g_action_map_remove_action (G_ACTION_MAP (plugin->priv->eog_window),
				    EOG_POSTASA_PLUGIN_ACTION);
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
}

/*** GObject Functions ***/

/**
 * eog_postasa_plugin_init:
 *
 * Object initialisation method.  Sets up the (unauthenticated)
 * PicasaWeb service, a #GCancellable for login, and sets the
 * uploads_pending flag to %FALSE.
 **/
static void
eog_postasa_plugin_init (EogPostasaPlugin *plugin)
{
	eog_debug_message (DEBUG_PLUGINS, "EogPostasaPlugin initializing");

911
	plugin->priv = eog_postasa_plugin_get_instance_private (plugin);
912

913
#ifdef HAVE_LIBGDATA_0_9
914 915 916
	plugin->priv->authorizer = gdata_client_login_authorizer_new ("EogPostasa", GDATA_TYPE_PICASAWEB_SERVICE);
	plugin->priv->service = gdata_picasaweb_service_new (GDATA_AUTHORIZER (plugin->priv->authorizer)); /* unref'd in eog_postasa_plugin_dispose() */
#else
917
	plugin->priv->service = gdata_picasaweb_service_new ("EogPostasa"); /* unref'd in eog_postasa_plugin_dispose() */
918
#endif
919
	plugin->priv->login_cancellable = g_cancellable_new (); /* unref'd in eog_postasa_plugin_dispose() */
920 921 922 923
	plugin->priv->uploads_pending = FALSE;
}

/**
924
 * eog_postasa_plugin_dispose:
925 926 927 928
 *
 * Cleans up the #EogPostasaPlugin object, unref'ing its #GDataPicasaWebService and #GCancellable.
 **/
static void
929
eog_postasa_plugin_dispose (GObject *_plugin)
930 931 932
{
	EogPostasaPlugin *plugin = EOG_POSTASA_PLUGIN (_plugin);

933
	eog_debug_message (DEBUG_PLUGINS, "EogPostasaPlugin disposing");
934

935
#ifdef HAVE_LIBGDATA_0_9
936 937 938 939 940 941
	if (plugin->priv->authorizer) {
		g_object_unref (plugin->priv->authorizer);
		plugin->priv->authorizer = NULL;
	}
#endif

942 943 944 945 946 947 948 949 950
	if (plugin->priv->service) {
		g_object_unref (plugin->priv->service);
		plugin->priv->service = NULL;
	}
	if (plugin->priv->login_cancellable) {
		g_object_unref (plugin->priv->login_cancellable);
		plugin->priv->login_cancellable = NULL;
	}
	if (G_IS_OBJECT (plugin->priv->uploads_store)) {
951 952
		/* we check in case the upload window was never created */
		g_object_unref (plugin->priv->uploads_store);
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
		plugin->priv->uploads_store = NULL;
	}

	if (plugin->priv->eog_window) {
		g_object_unref (plugin->priv->eog_window);
		plugin->priv->eog_window = NULL;
	}

	G_OBJECT_CLASS (eog_postasa_plugin_parent_class)->dispose (_plugin);
}

static void
eog_postasa_plugin_get_property (GObject    *object,
				 guint       prop_id,
				 GValue     *value,
				 GParamSpec *pspec)
{
	EogPostasaPlugin *plugin = EOG_POSTASA_PLUGIN (object);

	switch (prop_id)
	{
	case PROP_WINDOW:
		g_value_set_object (value, plugin->priv->eog_window);
		break;
977

978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
eog_postasa_plugin_set_property (GObject      *object,
				 guint         prop_id,
				 const GValue *value,
				 GParamSpec   *pspec)
{
	EogPostasaPlugin *plugin = EOG_POSTASA_PLUGIN (object);

	switch (prop_id)
	{
	case PROP_WINDOW:
		plugin->priv->eog_window = EOG_WINDOW (g_value_dup_object (value));
		break;

	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
}

/**
 * eog_postasa_plugin_class_init:
 *
 * Plugin class initialisation.  Binds class hooks to actual implementations.
 **/
static void
eog_postasa_plugin_class_init (EogPostasaPluginClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
	object_class->dispose = eog_postasa_plugin_dispose;
	object_class->set_property = eog_postasa_plugin_set_property;
	object_class->get_property = eog_postasa_plugin_get_property;

	g_object_class_override_property (object_class, PROP_WINDOW, "window");
}

static void
eog_postasa_plugin_class_finalize (EogPostasaPluginClass *klass)
{
	/* Dummy needed for G_DEFINE_DYNAMIC_TYPE_EXTENDED */
}
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
static void
eog_window_activatable_iface_init (EogWindowActivatableInterface *iface)
{
	iface->activate = impl_activate;
	iface->deactivate = impl_deactivate;
}

G_MODULE_EXPORT void
peas_register_types (PeasObjectModule *module)
{
	eog_postasa_plugin_register_type (G_TYPE_MODULE (module));
	peas_object_module_register_extension_type (module,
						    EOG_TYPE_WINDOW_ACTIVATABLE,
						    EOG_TYPE_POSTASA_PLUGIN);
1041
}