Debian Codegraph @

dnd2share/src/applet-backend-dropbox.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #define _BSD_SOURCE
21 #include <stdlib.h>
22 #include <math.h>
23 #include <unistd.h>
24 #include <glib/gstdio.h>
25 #include <string.h>
26 #include <time.h>
27
28 #include "applet-struct.h"
29 #include "applet-dnd2share.h"
30 #include "applet-backend-dropbox.h"
31
32 #define NB_URLS 1
33 static const gchar *s_UrlLabels[NB_URLS] = {N_("Direct Link")};
34
35 static void upload (const gchar *cFilePath, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError)
36 {
37 // Upload the file
38 gchar *cCommand;
39 if (cLocalDir)
40 cCommand = g_strdup_printf ("cp \"%s\" \"%s\"", cFilePath, cLocalDir);
41 else
42 cCommand= g_strdup_printf ("cp \"%s\" ~/Dropbox/Public", cFilePath);
43 cd_debug ("commande dropbox1 : %s", cCommand);
44 int r = system (cCommand);
45 if (r < 0)
46 cd_warning ("Not able to launch this command: %s", cCommand);
47 g_free (cCommand);
48
49 // get the result URL (available immediately, no need to loop on 'dropbox status' until having 'Idle').
50 gchar *cFileName = g_path_get_basename (cFilePath);
51 if (cLocalDir)
52 {
53 gchar *str = g_strstr_len (cLocalDir, -1, "Dropbox");
54 if (!str)
55 {
56 str = strrchr (cLocalDir, '/');
57 if (str)
58 str ++;
59 }
60 if (!str)
61 {
62 cd_warning ("Wrong dropbox dir");
63 g_set_error (pError, 1, 1, "%s %s", D_("This directory seems not valid:"), cLocalDir);
64 return;
65 }
66
67 cCommand = g_strdup_printf ("dropbox puburl \"%s/%s\"", cLocalDir, cFileName);
68 }
69 else
70 cCommand = g_strdup_printf ("dropbox puburl \"%s/Dropbox/Public/%s\"", getenv("HOME"), cFileName);
71
72 cd_debug ("command dropbox2 : %s", cCommand);
73 g_free (cFileName);
74 gchar *cResult = cairo_dock_launch_command_sync (cCommand);
75 g_free (cCommand);
76 if (cResult == NULL || *cResult == '\0')
77 {
78 cd_warning ("Dropbox did not give use an address :-(");
79 DND2SHARE_SET_GENERIC_ERROR_SERVICE ("Dropbox", "dropbox");
80 return ;
81 }
82
83 if (cResult[strlen(cResult)-1] == '\r')
84 cResult[strlen(cResult)-1] = '\0';
85 if (cResult[strlen(cResult)-1] == '\n')
86 cResult[strlen(cResult)-1] = '\0';
87
88 // fill the shared memory with our URLs.
89 cResultUrls[0] = cResult;
90 }
91
92
93 void cd_dnd2share_register_dropbox_backend (void)
94 {
95 cd_dnd2share_register_new_backend (CD_TYPE_FILE,
96 "DropBox",
97 NB_URLS,
98 s_UrlLabels,
99 0,
100 upload);
101 }