Add account name to distinguish multiple instances of service.
[sharing-cli] / src / plugin.c
index a4fac37..fdd2047 100644 (file)
@@ -1,6 +1,8 @@
 /*
 /*
- * This file is part of sharing-plugin-template
+ * This file is part of Command-Line Sharing Plugin.
  *
  *
+ * Copyright (C) 2010 Tuomas Kulve.
+ * Copyright (C) 2009 Heikki Kallasjoki. All rights reserved.
  * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
  *
  * This maemo code example is licensed under a MIT-style license,
  * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
  *
  * This maemo code example is licensed under a MIT-style license,
 #include <sharing-transfer.h>
 #include <conicconnection.h>
 #include <osso-log.h>
 #include <sharing-transfer.h>
 #include <conicconnection.h>
 #include <osso-log.h>
+#include <hildon/hildon.h>
+#include <string.h>
 
 #include "send.h"
 
 #include "send.h"
-#include "validate.h"
 
 
+
+/*
+#define ULOG_DEBUG_L(FMT, ARG...) syslog(LOG_DEBUG | LOG_USER, \
+        "%s:%d: " FMT, __FILE__, __LINE__, ## ARG)
+*/
+
+#ifdef ULOG_DEBUG_L
+#undef ULOG_DEBUG_L
+#endif
+
+
+#define ULOG_DEBUG_L(FMT, ARG...) {FILE *f = fopen("/tmp/cli.log", "a"); fprintf(f, "%s:%d: " FMT, __FILE__, __LINE__, ## ARG); fprintf(f, "\n"); fclose(f);}
+
+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(GHashTable* h, SharingAccount* a);
+
+static SharingPluginInterfaceEditAccountResult
+edit_account(SharingAccount* account, 
+                        GtkWindow* parent,
+                        gboolean setup);
+
+static gboolean
+gui_read_item(GHashTable* h,
+                         const gchar* id,
+                         SharingAccount* a);
+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);
+
+SharingPluginInterfaceAccountValidateResult
+sharing_plugin_interface_account_validate(SharingAccount* account, 
+                                                                                 ConIcConnection* con,
+                                                                                 gboolean *cont,
+                                                                                 gboolean* dead_mans_switch);
+
+SharingPluginInterfaceEditAccountResult
+sharing_plugin_interface_edit_account(GtkWindow* parent,
+                                                                         SharingAccount* account,
+                                                                         ConIcConnection* con,
+                                                                         gboolean* dead_mans_switch);
 /**
  * sharing_plugin_interface_init:
  * @dead_mans_switch: What?
 /**
  * sharing_plugin_interface_init:
  * @dead_mans_switch: What?
@@ -27,7 +93,8 @@
  *
  * Returns: 0
  */
  *
  * Returns: 0
  */
-guint sharing_plugin_interface_init (gboolean* dead_mans_switch)
+guint
+sharing_plugin_interface_init(gboolean* dead_mans_switch)
 {
     ULOG_DEBUG_L("sharing_manager_plugin_interface_init");
     return 0;
 {
     ULOG_DEBUG_L("sharing_manager_plugin_interface_init");
     return 0;
@@ -41,7 +108,8 @@ guint sharing_plugin_interface_init (gboolean* dead_mans_switch)
  *
  * Returns: 0
  */
  *
  * Returns: 0
  */
-guint sharing_plugin_interface_uninit (gboolean* dead_mans_switch)
+guint
+sharing_plugin_interface_uninit(gboolean* dead_mans_switch)
 {
     ULOG_DEBUG_L("sharing_manager_plugin_interface_uninit");
     return 0;
 {
     ULOG_DEBUG_L("sharing_manager_plugin_interface_uninit");
     return 0;
@@ -57,13 +125,16 @@ guint sharing_plugin_interface_uninit (gboolean* dead_mans_switch)
  *
  * Returns: Result of send
  */
  *
  * Returns: Result of send
  */
-SharingPluginInterfaceSendResult sharing_plugin_interface_send
-    (SharingTransfer* transfer, ConIcConnection* con,
-    gboolean* dead_mans_switch)
+SharingPluginInterfaceSendResult
+sharing_plugin_interface_send(SharingTransfer* transfer,
+                                                         ConIcConnection* con,
+                                                         gboolean* dead_mans_switch)
 {
 {
+    SharingPluginInterfaceSendResult ret_val;
     ULOG_DEBUG_L ("sharing_plugin_interface_send");
     ULOG_DEBUG_L ("sharing_plugin_interface_send");
-    SharingPluginInterfaceSendResult ret_val = SHARING_SEND_ERROR_UNKNOWN;
-    ret_val = send (transfer, con, dead_mans_switch);
+
+    ret_val = cli_send (transfer, con, dead_mans_switch);
+
     return ret_val;
 }
 
     return ret_val;
 }
 
@@ -78,13 +149,24 @@ SharingPluginInterfaceSendResult sharing_plugin_interface_send
  *
  * Returns: Result of account setup
  */
  *
  * Returns: Result of account setup
  */
-SharingPluginInterfaceAccountSetupResult sharing_plugin_interface_account_setup
-    (GtkWindow* parent, SharingService* service, SharingAccount** worked_on,
-    osso_context_t* osso)
+SharingPluginInterfaceAccountSetupResult
+sharing_plugin_interface_account_setup(GtkWindow* parent,
+                                                                          SharingService* service,
+                                                                          SharingAccount** worked_on,
+                                                                          osso_context_t* osso)
 {
     ULOG_DEBUG_L ("sharing_plugin_interface_account_setup");
 {
     ULOG_DEBUG_L ("sharing_plugin_interface_account_setup");
-    SharingPluginInterfaceAccountSetupResult ret = 0;
-    return ret;
+       SharingPluginInterfaceEditAccountResult res;
+
+       if (!worked_on || !*worked_on)
+         return SHARING_ACCOUNT_SETUP_ERROR_UNKNOWN;
+
+       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;
 }
 
 /**
 }
 
 /**
@@ -98,12 +180,16 @@ SharingPluginInterfaceAccountSetupResult sharing_plugin_interface_account_setup
  * Returns: Result of account validation
  */
 SharingPluginInterfaceAccountValidateResult
  * Returns: Result of account validation
  */
 SharingPluginInterfaceAccountValidateResult
-sharing_plugin_interface_account_validate (SharingAccount* account, 
-    ConIcConnection* con, gboolean *cont, gboolean* dead_mans_switch)
+sharing_plugin_interface_account_validate(SharingAccount* account, 
+                                                                                 ConIcConnection* con,
+                                                                                 gboolean *cont,
+                                                                                 gboolean* dead_mans_switch)
 {
     ULOG_DEBUG_L ("sharing_plugin_interface_account_validate");
 {
     ULOG_DEBUG_L ("sharing_plugin_interface_account_validate");
-    SharingPluginInterfaceAccountValidateResult ret_val = 0;
-    ret_val = validate (account, con, cont, dead_mans_switch);
+    SharingPluginInterfaceAccountValidateResult ret_val;
+
+       ret_val = SHARING_ACCOUNT_VALIDATE_SUCCESS;
+
     return ret_val;
 }
 
     return ret_val;
 }
 
@@ -118,11 +204,171 @@ sharing_plugin_interface_account_validate (SharingAccount* account,
  * Returns: Result of account edit
  */
 SharingPluginInterfaceEditAccountResult
  * Returns: Result of account edit
  */
 SharingPluginInterfaceEditAccountResult
-    sharing_plugin_interface_edit_account (GtkWindow* parent,
-    SharingAccount* account, ConIcConnection* con, gboolean* dead_mans_switch)
+sharing_plugin_interface_edit_account(GtkWindow* parent,
+                                                                         SharingAccount* account,
+                                                                         ConIcConnection* con,
+                                                                         gboolean* dead_mans_switch)
 {
 {
-    SharingPluginInterfaceEditAccountResult ret = 0;
     ULOG_DEBUG_L ("sharing_plugin_interface_edit_account");
     ULOG_DEBUG_L ("sharing_plugin_interface_edit_account");
-    return ret;
+
+       if (!account)
+         return SHARING_EDIT_ACCOUNT_NOT_STARTED;
+       
+       return edit_account (account, parent, FALSE);
+}
+
+
+
+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;
+       }
+  /* Make the account name shown under service name */
+  if (changed && id == "name") {
+         sharing_account_set_username(a, new);
+  }
+  g_free (old);
+  return changed;
+}
+
+static gboolean
+gui_read(GHashTable* h, SharingAccount* a)
+{
+  gboolean command_updated = FALSE;
+  gboolean name_updated = FALSE;
+  command_updated = gui_read_item (h, "command_line", a);
+  name_updated = gui_read_item (h, "name", a);
+  /* Must separate the evaluation of two gui_read_item, or 1 of 2 params
+   * won't be saved */
+  return command_updated || name_updated;
 }
 
 }
 
+
+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;
+  GtkWidget* dlg_content;
+  GtkWidget* table;
+  GtkWidget* vbox;
+  GtkWidget* label;
+
+  ULOG_DEBUG_L ("edit_account");
+
+
+  if (setup) {
+       dlg = gtk_dialog_new_with_buttons ("Account setup - CLI", 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 - CLI", parent,
+                                                                          GTK_DIALOG_MODAL |
+                                                                          GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                          GTK_STOCK_SAVE, GTK_RESPONSE_YES,
+                                                                          GTK_STOCK_DELETE, GTK_RESPONSE_NO,
+                                                                          NULL);
+  }
+
+  dlg_content = gtk_dialog_get_content_area (GTK_DIALOG (dlg));
+  vbox = gtk_vbox_new(FALSE, 0);
+  gtk_container_add (GTK_CONTAINER (dlg_content), vbox);
+
+
+  /* Add info label */
+  
+  label = gtk_label_new("You must use 2x \"%s\", one for src filename, one for dst filename.");
+  gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
+  gtk_label_set_single_line_mode(GTK_LABEL(label), FALSE);
+
+  gtk_box_pack_end(GTK_BOX(vbox), label, FALSE, FALSE, 0);
+
+
+  /* Add account info */
+  table = gtk_table_new (4, 2, FALSE);
+
+  gtk_box_pack_end(GTK_BOX(vbox), table, TRUE, TRUE, 0);
+  
+  gui_add_item(table, 1,
+                          "command_line", "CLI command",
+                          "Command to execute",
+                          FALSE, account, h);
+  gui_add_item(table, 0,
+                          "name", "Name",
+                          "Account name",
+                          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);
+  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;
+}