Add account name to distinguish multiple instances of service.
[sharing-cli] / src / send.c
1 /*
2  * This file is part of Command-Line Sharing Plugin
3  *
4  * Copyright (C) 2010 Tuomas Kulve.
5  * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
6  *
7  * This maemo code example is licensed under a MIT-style license,
8  * that can be found in the file called "COPYING" in the root
9  * directory.
10  *
11  */
12
13 #include <stdio.h>
14 #include <glib.h>
15 #include <osso-log.h>
16 #include <sharing-http.h>
17 #include "send.h"
18
19
20 #ifdef ULOG_DEBUG_L
21 #undef ULOG_DEBUG_L
22 #endif
23
24
25 #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);}
26
27
28 /**
29  * send:
30  * @account: #SharingTransfer to be send
31  * @con: Connection used
32  * @dead_mans_switch: Turn to %FALSE at least every 30 seconds.
33  *
34  * Sends #SharingTransfer to service.
35  *
36  * Returns: #SharingPluginInterfaceSendResult
37  */
38 SharingPluginInterfaceSendResult cli_send (SharingTransfer* transfer,
39     ConIcConnection* con, gboolean* dead_mans_switch)
40 {
41     SharingEntry *entry;
42         SharingAccount *account;
43
44         entry = sharing_transfer_get_entry(transfer);
45         account = sharing_entry_get_account(entry);
46
47         ULOG_DEBUG_L("Sending");
48
49     for (GSList* p = sharing_entry_get_media (entry);
50                  p != NULL;
51                  p = g_slist_next(p)) {
52
53       SharingEntryMedia* media = p->data;
54
55       /* Process media */
56       if (!sharing_entry_media_get_sent (media)) {
57                 gchar cmd[4096];
58                 gint exit_status;
59                 GError *error = NULL;
60                 gboolean retval;
61                 const gchar *local_file;
62                 gchar* command_line;
63
64                 local_file = sharing_entry_media_get_localpath(media);
65
66                 ULOG_DEBUG_L("Local file: %s", local_file);
67
68                 if (local_file == NULL) {
69                   ULOG_DEBUG_L("No local file: %s", error->message);
70                   return SHARING_SEND_ERROR_UNKNOWN;
71                 }
72
73
74                 sharing_transfer_set_progress(transfer, 0.3);
75
76         command_line = sharing_account_get_param (account, "command_line");
77
78                 ULOG_DEBUG_L("service command line: %s", command_line);
79
80                 g_snprintf(cmd, 4096,
81                                    command_line,
82                                    local_file, 
83                                    sharing_entry_media_get_filename(media));
84
85                 ULOG_DEBUG_L("Executing: %s", cmd);
86
87                 retval = g_spawn_command_line_sync(cmd,
88                                                                                 NULL, NULL,
89                                                                                 &exit_status,
90                                                                                 &error);
91                 if (!retval) {
92                   ULOG_DEBUG_L("Failed to scp: %s", error->message);
93                   g_error_free(error);
94                   return SHARING_SEND_ERROR_UNKNOWN;
95                 }
96
97                 sharing_transfer_set_progress(transfer, 1.0);
98                 sharing_entry_media_set_sent (media, TRUE);
99
100       }
101     }
102
103     return SHARING_SEND_SUCCESS;
104 }
105