Move all code to a subdir for the garage repo
[g2-sharing] / src / plugin.c
diff --git a/src/plugin.c b/src/plugin.c
deleted file mode 100644 (file)
index 53cc78f..0000000
+++ /dev/null
@@ -1,472 +0,0 @@
-/*
- * This file is part of sharing-plugin-gallery2
- *
- * Copyright (C) 2009 Heikki Kallasjoki. All rights reserved.
- * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
- *
- * This code is licensed under a MIT-style license, that can be
- * found in the file called "COPYING" in the root directory.
- *
- */
-
-#include <gtk/gtk.h>
-#include <glib.h>
-#include <string.h>
-#include <stdio.h>
-#include <sharing-plugin-interface.h>
-#include <sharing-transfer.h>
-#include <conicconnection.h>
-#include <osso-log.h>
-#include <hildon/hildon.h>
-
-#include "gallery2.h"
-
-/* Plugin interface definition: */
-
-guint sharing_plugin_interface_init (gboolean* dead_mans_switch);
-guint sharing_plugin_interface_uninit (gboolean* dead_mans_switch);
-SharingPluginInterfaceSendResult sharing_plugin_interface_send (
-               SharingTransfer* transfer, ConIcConnection* con,
-               gboolean* dead_mans_switch);
-SharingPluginInterfaceAccountSetupResult
-sharing_plugin_interface_account_setup (
-               GtkWindow* parent,
-               SharingService* service, SharingAccount** worked_on,
-               osso_context_t* osso);
-SharingPluginInterfaceEditAccountResult
-sharing_plugin_interface_edit_account (
-               GtkWindow* parent,
-               SharingAccount* account,
-               ConIcConnection* con,
-               gboolean* dead_mans_switch);
-SharingPluginInterfaceAccountValidateResult
-sharing_plugin_interface_account_validate (
-               SharingAccount* account, ConIcConnection* con,
-               gboolean *cont, gboolean* dead_mans_switch);
-
-/* UI flow helper definition: */
-
-static SharingPluginInterfaceEditAccountResult edit_account (SharingAccount* account, GtkWindow* parent, gboolean setup);
-
-/**
- * sharing_plugin_interface_init:
- * @dead_mans_switch: What?
- *
- * Initialize interface
- *
- * Returns: 0
- */
-guint sharing_plugin_interface_init (gboolean* dead_mans_switch)
-{
-    ULOG_DEBUG_L("sharing_plugin_interface_init, gallery2");
-       *dead_mans_switch = FALSE;
-    return 0;
-}
-
-/**
- * sharing_plugin_interface_uninit:
- * @dead_mans_switch: What?
- *
- * Uninitialize interface
- *
- * Returns: 0
- */
-guint sharing_plugin_interface_uninit (gboolean* dead_mans_switch)
-{
-    ULOG_DEBUG_L("sharing_plugin_interface_uninit, gallery2");
-       *dead_mans_switch = FALSE;
-    return 0;
-}
-
-/**
- * sharing_plugin_interface_send:
- * @transfer: Transfer to be send
- * @con: Connection used
- * @dead_mans_switch: 
- *
- * Send interface.
- *
- * Returns: Result of send
- */
-SharingPluginInterfaceSendResult sharing_plugin_interface_send (
-               SharingTransfer* transfer, ConIcConnection* con,
-               gboolean* dead_mans_switch)
-{
-    ULOG_DEBUG_L ("sharing_plugin_interface_send, gallery2");
-
-       SharingPluginInterfaceSendResult ret = SHARING_SEND_SUCCESS;
-
-       SharingEntry *entry = sharing_transfer_get_entry (transfer);
-       SharingAccount *account = sharing_entry_get_account (entry);
-
-       gchar* urlbase = sharing_account_get_param (account, "urlbase");
-       gchar* albumname = sharing_account_get_param (account, "albumname");
-
-       GHashTable* cookies = 0;
-       gchar* auth = 0;
-
-       gchar* album = 0;
-
-       /* Login to the target service */
-
-       sharing_transfer_set_progress (transfer, 0.0);
-       *dead_mans_switch = FALSE;
-
-       SharingPluginInterfaceAccountValidateResult login_ret = SHARING_ACCOUNT_VALIDATE_FAILED;
-
-       if (urlbase)
-       {
-               gchar* user = sharing_account_get_username (account);
-               gchar* pass = sharing_account_get_password (account);
-               if (user && pass)
-                       login_ret = gallery2_login (con,
-                                       urlbase, user, pass,
-                                       &cookies, &auth);
-               g_free (user);
-               g_free (pass);
-       }
-
-       if (login_ret != SHARING_ACCOUNT_VALIDATE_SUCCESS || !sharing_transfer_continue (transfer))
-       {
-               if (cookies) g_hash_table_unref (cookies);
-               g_free (auth);
-               g_free (urlbase);
-               g_free (albumname);
-               if (!sharing_transfer_continue (transfer))
-                       return SHARING_SEND_CANCELLED;
-               else if (login_ret == SHARING_ACCOUNT_VALIDATE_ERROR_CONNECTION)
-                       return SHARING_SEND_ERROR_CONNECTION;
-               else
-                       return SHARING_SEND_ERROR_AUTH;
-       }
-
-       /* Look up the album */
-
-       sharing_transfer_set_progress (transfer, 0.05);
-       *dead_mans_switch = FALSE;
-
-       login_ret = gallery2_lookup_album (con, urlbase, albumname, &album, cookies, auth);
-       g_free (albumname);
-       albumname = 0;
-
-       if (login_ret != SHARING_ACCOUNT_VALIDATE_SUCCESS || !album || !sharing_transfer_continue (transfer))
-       {
-               if (cookies) g_hash_table_unref (cookies);
-               g_free (auth);
-               g_free (urlbase);
-               g_free (album); /* just in case... */
-               if (!sharing_transfer_continue (transfer))
-                       return SHARING_SEND_CANCELLED;
-               else if (login_ret == SHARING_ACCOUNT_VALIDATE_ERROR_CONNECTION)
-                       return SHARING_SEND_ERROR_CONNECTION;
-               else
-                       return SHARING_SEND_ERROR_UNKNOWN;
-       }
-
-       /* Start sending items */
-
-       sharing_transfer_set_progress (transfer, 0.1);
-       *dead_mans_switch = FALSE;
-
-       guint64 total_bytes = 0;
-
-       for (GSList* p = sharing_entry_get_media (entry); p; p = g_slist_next (p))
-       {
-               SharingEntryMedia* media = p->data;
-               if (!sharing_entry_media_get_sent (media))
-                       total_bytes += sharing_entry_media_get_size (media);
-       }
-
-       if (!total_bytes) total_bytes = 1; /* just in case... */
-
-       guint64 sent_bytes = 0;
-
-       for (GSList* p = sharing_entry_get_media (entry); p; p = g_slist_next (p))
-       {
-               SharingEntryMedia* media = p->data;
-               if (sharing_entry_media_get_sent (media))
-                       continue;
-
-               if (!sharing_transfer_continue (transfer))
-               {
-                       ret = SHARING_SEND_CANCELLED;
-                       break;
-               }
-
-               guint64 media_size = sharing_entry_media_get_size (media);
-
-               gdouble progress_start = 1.0 - 0.9*((total_bytes - sent_bytes) / (gdouble)total_bytes);
-               gdouble progress_end = 1.0 - 0.9*((total_bytes - (sent_bytes + media_size)) / (gdouble)total_bytes);
-
-               sharing_transfer_set_progress (transfer, progress_start);
-               *dead_mans_switch = FALSE;
-
-               SharingPluginInterfaceSendResult mret = gallery2_send (con,
-                               transfer, progress_start, progress_end, dead_mans_switch,
-                               media,
-                               urlbase, album, cookies, auth);
-
-               if (mret == SHARING_SEND_SUCCESS)
-                       sharing_entry_media_set_sent (media, TRUE);
-               else
-                       ret = mret; /* propagate last error */
-       }
-
-       sharing_transfer_set_progress (transfer, 1.0);
-       *dead_mans_switch = FALSE;
-
-       if (cookies) g_hash_table_unref (cookies);
-       g_free (auth);
-       g_free (urlbase);
-       g_free (album);
-
-       return ret;
-}
-
-/**
- * sharing_plugin_interface_account_setup:
- * @parent: Parent window
- * @service: Service information
- * @worked_on: Who knows?
- * @osso: osso_context_t for the dialog.
- *
- * Custom account setup UI flow.
- *
- * Returns: Something?
- */
-SharingPluginInterfaceAccountSetupResult
-sharing_plugin_interface_account_setup (
-               GtkWindow* parent,
-               SharingService* service, SharingAccount** worked_on,
-               osso_context_t* osso)
-{
-       if (!worked_on || !*worked_on)
-               return SHARING_ACCOUNT_SETUP_ERROR_UNKNOWN;
-
-       SharingPluginInterfaceEditAccountResult res = edit_account (*worked_on, parent, TRUE);
-       if (res == SHARING_EDIT_ACCOUNT_SUCCESS)
-               return SHARING_ACCOUNT_SETUP_SUCCESS;
-       else
-               return SHARING_ACCOUNT_SETUP_ERROR_UNKNOWN;
-}
-
-/**
- * sharing_plugin_interface_edit_account:
- * @parent: Parent window
- * @account: Account to edit
- * @con: Connection to use (for what?)
- * @dead_mans_switch: Switch to flip (really?!)
- */
-SharingPluginInterfaceEditAccountResult
-sharing_plugin_interface_edit_account (
-               GtkWindow* parent,
-               SharingAccount* account,
-               ConIcConnection* con,
-               gboolean* dead_mans_switch)
-{
-       if (!account)
-               return SHARING_EDIT_ACCOUNT_NOT_STARTED;
-
-       return edit_account (account, parent, FALSE);
-}
-
-/**
- * sharing_plugin_interface_account_validate:
- * @account: Account tested
- * @con: Connection used to test account
- * @cont: ...
- * @dead_mans_switch: ...
- *
- * Validates account information.
- *
- * Returns: Result of account validation
- */
-SharingPluginInterfaceAccountValidateResult
-sharing_plugin_interface_account_validate (
-               SharingAccount* account, ConIcConnection* con,
-               gboolean *cont, gboolean* dead_mans_switch)
-{
-       ULOG_DEBUG_L ("sharing_plugin_interface_account_validate, gallery2");
-
-       gchar* user = sharing_account_get_username (account);
-       gchar* pass = sharing_account_get_password (account);
-       gchar* urlbase = sharing_account_get_param (account, "urlbase");
-       gchar* albumname = sharing_account_get_param (account, "albumname");
-
-       SharingPluginInterfaceAccountValidateResult ret = SHARING_ACCOUNT_VALIDATE_FAILED;
-
-       if (user && pass && urlbase && albumname)
-       {
-               GHashTable* cookies = 0;
-               gchar* auth = 0;
-
-               /* URL sanity checks and fixups */
-
-               gboolean update_url = FALSE;
-
-               if (!(g_str_has_prefix (urlbase, "http://")
-                               || g_str_has_prefix (urlbase, "https://")))
-               {
-                       gchar* prev = urlbase;
-                       urlbase = g_strdup_printf ("http://%s", prev);
-                       update_url = TRUE;
-                       g_free (prev);
-               }
-
-               if (g_str_has_suffix (urlbase, "/main.php"))
-               {
-                       gchar* prev = urlbase;
-                       urlbase = g_strndup (prev, strlen (prev) - 9);
-                       update_url = TRUE;
-                       g_free (prev);
-               }
-               else if (g_str_has_suffix (urlbase, "/"))
-               {
-                       gchar* prev = urlbase;
-                       urlbase = g_strndup (prev, strlen (prev) - 1);
-                       update_url = TRUE;
-                       g_free (prev);
-               }
-
-               if (update_url)
-                       sharing_account_set_param (account, "urlbase", urlbase);
-
-               /* Validate by trying to login */
-
-               ret = gallery2_login (con,
-                               urlbase, user, pass,
-                               &cookies, &auth);
-               *dead_mans_switch = FALSE;
-
-               /* Also validate the album name here, if login ok */
-
-               if (ret == SHARING_ACCOUNT_VALIDATE_SUCCESS)
-               {
-                       gchar* album = 0;
-                       ret = gallery2_lookup_album (con, urlbase, albumname, &album, cookies, auth);
-                       *dead_mans_switch = FALSE;
-                       g_free (album);
-               }
-
-               if (cookies) g_hash_table_unref (cookies);
-               if (auth) g_free (auth);
-       }
-
-       g_free (user);
-       g_free (pass);
-       g_free (urlbase);
-       g_free (albumname);
-
-       return ret;
-}
-
-/* UI flow helper implementations. */
-
-static void gui_add_item (GtkWidget* table, guint row,
-               gchar* id, const gchar* label, const gchar* placeholder,
-               gboolean invis, SharingAccount* a, GHashTable* h);
-static gboolean gui_read_item (GHashTable* h, const gchar* id, SharingAccount* a);
-static gboolean gui_read (GHashTable* h, SharingAccount* a);
-
-static void gui_add_item (GtkWidget* table, guint row,
-               gchar* id, const gchar* label, const gchar* placeholder,
-               gboolean invis, SharingAccount* a, GHashTable* h)
-{
-       GtkWidget* wlabel = gtk_label_new (label);
-       gtk_table_attach (GTK_TABLE (table), wlabel, 0, 1, row, row+1, GTK_FILL, GTK_FILL|GTK_EXPAND, HILDON_MARGIN_DOUBLE, 0);
-
-       GtkWidget* wentry = hildon_entry_new (HILDON_SIZE_AUTO);
-       hildon_entry_set_placeholder (HILDON_ENTRY (wentry), placeholder);
-       if (invis)
-               hildon_gtk_entry_set_input_mode (GTK_ENTRY (wentry), HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
-       gtk_table_attach_defaults (GTK_TABLE (table), wentry, 1, 2, row, row+1);
-
-       g_hash_table_insert (h, id, wentry);
-
-       gchar* old = sharing_account_get_param (a, id);
-       if (old)
-       {
-               gtk_entry_set_text (GTK_ENTRY (wentry), old);
-               g_free (old);
-       }
-}
-
-static gboolean gui_read_item (GHashTable* h, const gchar* id, SharingAccount* a)
-{
-       GtkWidget* wentry = g_hash_table_lookup (h, id);
-       if (!wentry) return FALSE;
-
-       gchar* old = sharing_account_get_param (a, id);
-       const gchar* new = gtk_entry_get_text (GTK_ENTRY (wentry));
-
-       gboolean changed = FALSE;
-
-       if (!old || strcmp (old, new) != 0)
-       {
-               sharing_account_set_param (a, id, new);
-               changed = TRUE;
-       }
-
-       g_free (old);
-       return changed;
-}
-
-static gboolean gui_read (GHashTable* h, SharingAccount* a)
-{
-       gboolean changed = FALSE;
-       if (gui_read_item (h, "username", a)) changed = TRUE;
-       if (gui_read_item (h, "password", a)) changed = TRUE;
-       if (gui_read_item (h, "urlbase", a)) changed = TRUE;
-       if (gui_read_item (h, "albumname", a)) changed = TRUE;
-       return changed;
-}
-
-static SharingPluginInterfaceEditAccountResult edit_account (SharingAccount* account, GtkWindow* parent, gboolean setup)
-{
-       GHashTable* h = g_hash_table_new (g_str_hash, g_str_equal);
-
-       GtkWidget* dlg = 0;
-
-       if (setup)
-               dlg = gtk_dialog_new_with_buttons (
-                               "Account setup - Gallery 2", parent,
-                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
-                               GTK_STOCK_OK, GTK_RESPONSE_YES,
-                               NULL);
-       else
-               dlg = gtk_dialog_new_with_buttons (
-                               "Edit account - Gallery 2", parent,
-                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
-                               GTK_STOCK_SAVE, GTK_RESPONSE_YES,
-                               GTK_STOCK_DELETE, GTK_RESPONSE_NO,
-                               NULL);
-
-       GtkWidget* dlg_content = gtk_dialog_get_content_area (GTK_DIALOG (dlg));
-
-       GtkWidget* table = gtk_table_new (4, 2, FALSE);
-       gtk_container_add (GTK_CONTAINER (dlg_content), table);
-
-       gui_add_item (table, 0, "username", "User name", "Gallery 2 account name", FALSE, account, h);
-       gui_add_item (table, 1, "password", "Password", "Gallery 2 account password", TRUE, account, h);
-       gui_add_item (table, 2, "urlbase", "URL", "Gallery 2 site address", FALSE, account, h);
-       gui_add_item (table, 3, "albumname", "Album", "Album name to upload to", FALSE, account, h);
-
-       gtk_widget_show_all (GTK_WIDGET (dlg));
-       gint result = gtk_dialog_run (GTK_DIALOG (dlg));
-
-       gboolean changed = FALSE;
-       if (result == GTK_RESPONSE_YES)
-               changed = gui_read (h, account);
-
-       gtk_widget_destroy (dlg);
-       g_hash_table_unref (h);
-
-       if (result == GTK_RESPONSE_YES && (changed || setup))
-               return SHARING_EDIT_ACCOUNT_SUCCESS;
-       else if (result == GTK_RESPONSE_YES) /* !changed in edit */
-               return SHARING_EDIT_ACCOUNT_NOT_STARTED;
-       else if (result == GTK_RESPONSE_NO)
-               return SHARING_EDIT_ACCOUNT_DELETE;
-       else
-               return SHARING_EDIT_ACCOUNT_CANCELLED;
-}