Remove 1 g_hash_table_unref.
[sharing-cli] / src / plugin.c
1 /*
2  * This file is part of Command-Line Sharing Plugin.
3  *
4  * Copyright (C) 2010 Tuomas Kulve.
5  * Copyright (C) 2009 Heikki Kallasjoki. All rights reserved.
6  * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
7  *
8  * This maemo code example is licensed under a MIT-style license,
9  * that can be found in the file called "COPYING" in the root
10  * directory.
11  *
12  */
13
14 #include <gtk/gtk.h>
15 #include <glib.h>
16 #include <sharing-plugin-interface.h>
17 #include <sharing-transfer.h>
18 #include <conicconnection.h>
19 #include <osso-log.h>
20 #include <hildon/hildon.h>
21 #include <string.h>
22
23 #include "send.h"
24
25
26 /*
27 #define ULOG_DEBUG_L(FMT, ARG...) syslog(LOG_DEBUG | LOG_USER, \
28         "%s:%d: " FMT, __FILE__, __LINE__, ## ARG)
29 */
30
31 #ifdef ULOG_DEBUG_L
32 #undef ULOG_DEBUG_L
33 #endif
34
35
36 #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);}
37
38 static void
39 gui_add_item(GtkWidget* table,
40                          guint row,
41                          gchar* id,
42                          const gchar* label, 
43                          const gchar* placeholder,
44                          gboolean invis,
45                          SharingAccount* a,
46                          GHashTable* h);
47
48 static gboolean
49 gui_read(GHashTable* h, SharingAccount* a);
50
51 static SharingPluginInterfaceEditAccountResult
52 edit_account(SharingAccount* account, 
53                          GtkWindow* parent,
54                          gboolean setup);
55
56 static gboolean
57 gui_read_item(GHashTable* h,
58                           const gchar* id,
59                           SharingAccount* a);
60 guint
61 sharing_plugin_interface_init (gboolean* dead_mans_switch);
62
63 guint
64 sharing_plugin_interface_uninit (gboolean* dead_mans_switch);
65
66 SharingPluginInterfaceSendResult
67 sharing_plugin_interface_send(SharingTransfer* transfer,
68                                                           ConIcConnection* con,
69                                                           gboolean* dead_mans_switch);
70
71 SharingPluginInterfaceAccountSetupResult
72 sharing_plugin_interface_account_setup(GtkWindow* parent,
73                                                                            SharingService* service,
74                                                                            SharingAccount** worked_on,
75                                                                            osso_context_t* osso);
76
77 SharingPluginInterfaceAccountValidateResult
78 sharing_plugin_interface_account_validate(SharingAccount* account, 
79                                                                                   ConIcConnection* con,
80                                                                                   gboolean *cont,
81                                                                                   gboolean* dead_mans_switch);
82
83 SharingPluginInterfaceEditAccountResult
84 sharing_plugin_interface_edit_account(GtkWindow* parent,
85                                                                           SharingAccount* account,
86                                                                           ConIcConnection* con,
87                                                                           gboolean* dead_mans_switch);
88 /**
89  * sharing_plugin_interface_init:
90  * @dead_mans_switch: What?
91  *
92  * Initialize interface
93  *
94  * Returns: 0
95  */
96 guint
97 sharing_plugin_interface_init(gboolean* dead_mans_switch)
98 {
99     ULOG_DEBUG_L("sharing_manager_plugin_interface_init");
100     return 0;
101 }
102
103 /**
104  * sharing_plugin_interface_uninit:
105  * @dead_mans_switch: What?
106  *
107  * Uninitialize interface
108  *
109  * Returns: 0
110  */
111 guint
112 sharing_plugin_interface_uninit(gboolean* dead_mans_switch)
113 {
114     ULOG_DEBUG_L("sharing_manager_plugin_interface_uninit");
115     return 0;
116 }
117
118 /**
119  * sharing_plugin_interface_send:
120  * @transfer: Transfer to be send
121  * @con: Connection used
122  * @dead_mans_switch: 
123  *
124  * Send interface.
125  *
126  * Returns: Result of send
127  */
128 SharingPluginInterfaceSendResult
129 sharing_plugin_interface_send(SharingTransfer* transfer,
130                                                           ConIcConnection* con,
131                                                           gboolean* dead_mans_switch)
132 {
133     SharingPluginInterfaceSendResult ret_val;
134     ULOG_DEBUG_L ("sharing_plugin_interface_send");
135
136     ret_val = cli_send (transfer, con, dead_mans_switch);
137
138     return ret_val;
139 }
140
141 /**
142  * sharing_plugin_interface_account_setup:
143  * @transfer: #SharingTransfer send
144  * @service: #SharingService
145  * @worked_on: Connection used
146  * @osso_context_t: Osso context
147  *
148  * Send interface
149  *
150  * Returns: Result of account setup
151  */
152 SharingPluginInterfaceAccountSetupResult
153 sharing_plugin_interface_account_setup(GtkWindow* parent,
154                                                                            SharingService* service,
155                                                                            SharingAccount** worked_on,
156                                                                            osso_context_t* osso)
157 {
158     ULOG_DEBUG_L ("sharing_plugin_interface_account_setup");
159         SharingPluginInterfaceEditAccountResult res;
160
161         if (!worked_on || !*worked_on)
162           return SHARING_ACCOUNT_SETUP_ERROR_UNKNOWN;
163
164         res = edit_account (*worked_on, parent, TRUE);
165     
166     if (res == SHARING_EDIT_ACCOUNT_SUCCESS)
167           return SHARING_ACCOUNT_SETUP_SUCCESS;
168         else
169           return SHARING_ACCOUNT_SETUP_ERROR_UNKNOWN;
170 }
171
172 /**
173  * sharing_manager_plugin_interface_account_validate:
174  * @account: Account tested
175  * @con: Connection used to test account
176  * @dead_mans_switch: 
177  *
178  * Validates account information.
179  *
180  * Returns: Result of account validation
181  */
182 SharingPluginInterfaceAccountValidateResult
183 sharing_plugin_interface_account_validate(SharingAccount* account, 
184                                                                                   ConIcConnection* con,
185                                                                                   gboolean *cont,
186                                                                                   gboolean* dead_mans_switch)
187 {
188     ULOG_DEBUG_L ("sharing_plugin_interface_account_validate");
189     SharingPluginInterfaceAccountValidateResult ret_val;
190
191         ret_val = SHARING_ACCOUNT_VALIDATE_SUCCESS;
192
193     return ret_val;
194 }
195
196 /**
197  * sharing_plugin_interface_edit_account:
198  * @account: Account tested
199  * @con: Connection used to test account
200  * @dead_mans_switch: 
201  *
202  * Edit account plugin implementation.
203  *
204  * Returns: Result of account edit
205  */
206 SharingPluginInterfaceEditAccountResult
207 sharing_plugin_interface_edit_account(GtkWindow* parent,
208                                                                           SharingAccount* account,
209                                                                           ConIcConnection* con,
210                                                                           gboolean* dead_mans_switch)
211 {
212     ULOG_DEBUG_L ("sharing_plugin_interface_edit_account");
213
214         if (!account)
215           return SHARING_EDIT_ACCOUNT_NOT_STARTED;
216         
217         return edit_account (account, parent, FALSE);
218 }
219
220
221
222 static void
223 gui_add_item(GtkWidget* table,
224                          guint row,
225                          gchar* id,
226                          const gchar* label,
227                          const gchar* placeholder,
228                          gboolean invis,
229                          SharingAccount* a,
230                          GHashTable* h)
231 {
232   GtkWidget* wlabel = gtk_label_new (label);
233
234   gtk_table_attach (GTK_TABLE (table), wlabel, 0, 1, row, row+1,
235                                         GTK_FILL, GTK_FILL|GTK_EXPAND, HILDON_MARGIN_DOUBLE, 0);
236   
237   GtkWidget* wentry = hildon_entry_new (HILDON_SIZE_AUTO);
238   hildon_entry_set_placeholder (HILDON_ENTRY (wentry), placeholder);
239
240   if (invis) {
241         
242         hildon_gtk_entry_set_input_mode(GTK_ENTRY (wentry),
243                                                                         HILDON_GTK_INPUT_MODE_FULL |
244                                                                         HILDON_GTK_INPUT_MODE_INVISIBLE);
245   }
246
247   gtk_table_attach_defaults (GTK_TABLE (table), wentry, 1, 2, row, row+1);
248   
249   g_hash_table_insert (h, id, wentry);
250   
251   gchar* old = sharing_account_get_param (a, id);
252
253   if (old) {
254         gtk_entry_set_text (GTK_ENTRY (wentry), old);
255         g_free (old);
256   }
257
258 }
259
260
261
262 static gboolean
263 gui_read_item (GHashTable* h, const gchar* id, SharingAccount* a)
264 {
265   GtkWidget* wentry = g_hash_table_lookup (h, id);
266   if (!wentry) return FALSE;
267   
268   gchar* old = sharing_account_get_param (a, id);
269   const gchar* new = gtk_entry_get_text (GTK_ENTRY (wentry));
270   
271   gboolean changed = FALSE;
272   
273   if (!old || strcmp (old, new) != 0)
274         {
275           sharing_account_set_param (a, id, new);
276           changed = TRUE;
277         }
278   /* Make the account name shown under service name */
279   if (changed && id == "name") {
280           sharing_account_set_username(a, new);
281   }
282   g_free (old);
283   return changed;
284 }
285
286 static gboolean
287 gui_read(GHashTable* h, SharingAccount* a)
288 {
289   gboolean command_updated = FALSE;
290   gboolean name_updated = FALSE;
291   command_updated = gui_read_item (h, "command_line", a);
292   name_updated = gui_read_item (h, "name", a);
293   /* Must separate the evaluation of two gui_read_item, or 1 of 2 params
294    * won't be saved */
295   return command_updated || name_updated;
296 }
297
298
299 static SharingPluginInterfaceEditAccountResult
300 edit_account(SharingAccount* account, GtkWindow* parent, gboolean setup)
301 {
302   GHashTable* h = g_hash_table_new (g_str_hash, g_str_equal);  
303   GtkWidget* dlg = 0;
304   GtkWidget* dlg_content;
305   GtkWidget* table;
306   GtkWidget* vbox;
307   GtkWidget* label;
308
309   ULOG_DEBUG_L ("edit_account");
310
311
312   if (setup) {
313         dlg = gtk_dialog_new_with_buttons ("Account setup - CLI", parent,
314                                                                            GTK_DIALOG_MODAL |
315                                                                            GTK_DIALOG_DESTROY_WITH_PARENT,
316                                                                            GTK_STOCK_OK, GTK_RESPONSE_YES,
317                                                                            NULL);
318   } else {
319         dlg = gtk_dialog_new_with_buttons ("Edit account - CLI", parent,
320                                                                            GTK_DIALOG_MODAL |
321                                                                            GTK_DIALOG_DESTROY_WITH_PARENT,
322                                                                            GTK_STOCK_SAVE, GTK_RESPONSE_YES,
323                                                                            GTK_STOCK_DELETE, GTK_RESPONSE_NO,
324                                                                            NULL);
325   }
326
327   dlg_content = gtk_dialog_get_content_area (GTK_DIALOG (dlg));
328   vbox = gtk_vbox_new(FALSE, 0);
329   gtk_container_add (GTK_CONTAINER (dlg_content), vbox);
330
331
332   /* Add info label */
333   
334   label = gtk_label_new("You must use 2x \"%s\", one for src filename, one for dst filename.");
335   gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
336   gtk_label_set_single_line_mode(GTK_LABEL(label), FALSE);
337
338   gtk_box_pack_end(GTK_BOX(vbox), label, FALSE, FALSE, 0);
339
340
341   /* Add account info */
342   table = gtk_table_new (4, 2, FALSE);
343
344   gtk_box_pack_end(GTK_BOX(vbox), table, TRUE, TRUE, 0);
345   
346   gui_add_item(table, 1,
347                            "command_line", "CLI command",
348                            "Command to execute",
349                            FALSE, account, h);
350   gui_add_item(table, 0,
351                            "name", "Name",
352                            "Account name",
353                            FALSE, account, h);
354   
355   gtk_widget_show_all (GTK_WIDGET (dlg));
356   gint result = gtk_dialog_run (GTK_DIALOG (dlg));
357   
358   gboolean changed = FALSE;
359   if (result == GTK_RESPONSE_YES)
360         changed = gui_read (h, account);
361   
362   gtk_widget_destroy (dlg);
363   g_hash_table_unref (h);
364   
365   if (result == GTK_RESPONSE_YES && (changed || setup))
366         return SHARING_EDIT_ACCOUNT_SUCCESS;
367   else if (result == GTK_RESPONSE_YES) /* !changed in edit */
368         return SHARING_EDIT_ACCOUNT_NOT_STARTED;
369   else if (result == GTK_RESPONSE_NO)
370         return SHARING_EDIT_ACCOUNT_DELETE;
371   else
372         return SHARING_EDIT_ACCOUNT_CANCELLED;
373 }