Debian Codegraph @

dnd2share/src/applet-backend-uppix.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
26 #include "applet-struct.h"
27 #include "applet-dnd2share.h"
28 #include "applet-backend-uppix.h"
29
30 #define NB_URLS 3
31 static const gchar *s_UrlLabels[NB_URLS] = {N_("Direct Link"), N_("Thumbnail"), "BBCode"};
32
33
34 static void upload (const gchar *cFilePath, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError)
35 {
36 // Upload the file
37 gchar *cCommand = g_strdup_printf ("curl -L --connect-timeout 5 --retry 2 --limit-rate %dk uppix.com/upload -H Expect: -F u_file=@\"%s\" -F u_submit=Upload -F u_terms=yes", iLimitRate, cFilePath);
38 cd_debug ("%s", cCommand);
39 gchar *cResult = cairo_dock_launch_command_sync (cCommand);
40 g_free (cCommand);
41
42 if (! cResult)
43 {
44 DND2SHARE_SET_GENERIC_ERROR_WEBSITE ("Uppix.com");
45 return;
46 }
47
48 gchar *cDirectLink = NULL, *cThumbnail = NULL, *cBBCode = NULL;
49 gchar *cDirectLinkStart = strstr (cResult, "http://uppix.com/"); // find the direct link
50 if (cDirectLinkStart)
51 {
52 gchar *cDirectLinkEnd = strstr (cDirectLinkStart, "&quot;");
53 if (cDirectLinkEnd)
54 {
55 *cDirectLinkEnd = '\0';
56 gint iLength = cDirectLinkEnd - cDirectLinkStart;
57 cDirectLink = g_strdup (cDirectLinkStart); // http://uppix.com/f-(...)
58 cThumbnail = g_strdup (cDirectLink); // the same url but with 't' instead of 'f' => http://uppix.com/t-(...)
59 if (iLength > 17 && cThumbnail[17] == 'f')
60 cThumbnail[17] = 't';
61 cBBCode = g_strdup_printf ("[url=%s][img]%s[/img][/url]", cDirectLink, cThumbnail);
62 }
63 }
64 else
65 DND2SHARE_SET_GENERIC_ERROR_WEBSITE ("Uppix.com");
66 g_free (cResult);
67
68 cResultUrls[0] = cDirectLink;
69 cResultUrls[1] = cThumbnail;
70 cResultUrls[2] = cBBCode;
71 }
72
73
74 void cd_dnd2share_register_uppix_backend (void)
75 {
76 cd_dnd2share_register_new_backend (CD_TYPE_IMAGE,
77 "Uppix.com",
78 NB_URLS,
79 s_UrlLabels,
80 0,
81 upload);
82 }