Debian Codegraph @

dnd2share/src/applet-backend-codepad.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
27 #include "applet-struct.h"
28 #include "applet-dnd2share.h"
29 #include "applet-backend-codepad.h"
30
31 #define URL "http://codepad.org"
32 #define FORMAT "Plain Text"
33
34 #define NB_URLS 1
35 static const gchar *s_UrlLabels[NB_URLS] = {N_("Direct Link")};
36
37 /*<html>
38 <head>
39 <title>302 Found</title>
40 </head>
41 <body>
42 <h1>302 Found</h1>
43 The resource was found at <a href="http://codepad.org/INmbIYX2">http://codepad.org/INmbIYX2</a>;
44 you should be redirected automatically.
45 </body>
46 </html>'*/
47 static void upload (const gchar *cText, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError)
48 {
49 if (cText == NULL || *cText == '\0')
50 {
51 g_set_error (pError, 1, 1, D_("Your text is empty and couldn't be uploaded to this server"));
52 return;
53 }
54
55 GError *erreur = NULL;
56 gchar *cResult = cairo_dock_get_url_data_with_post (URL, FALSE, &erreur,
57 "code", cText,
58 "lang", FORMAT,
59 "submit", "Submit",
60 NULL);
61 if (erreur)
62 {
63 cd_warning (erreur->message);
64 g_error_free (erreur);
65 DND2SHARE_SET_GENERIC_ERROR_WEBSITE("Codepad.org");
66 }
67 else if (cResult)
68 {
69 // Get the next '"http://(...)"' link after 'Link:' text
70 gchar *str, *end;
71 if ((str = strstr (cResult, "Link:")) != NULL
72 && (str = strstr (str, "\"http://") + 1) != NULL
73 && (end = strchr (str, '"')) != NULL)
74 {
75 *end = '\0';
76 cResultUrls[0] = g_strdup (str);
77 }
78 else
79 DND2SHARE_SET_GENERIC_ERROR_WEBSITE("Codepad.org");
80 g_free (cResult);
81 }
82 // else -> generic error
83 }
84
85
86 void cd_dnd2share_register_codepad_backend (void)
87 {
88 cd_dnd2share_register_new_backend (CD_TYPE_TEXT,
89 "codepad.org",
90 NB_URLS,
91 s_UrlLabels,
92 0,
93 upload);
94 }