Debian Codegraph @

dnd2share/src/applet-backend-pastebin.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-pastebin.h"
30
31 #define URL "http://pastebin.com/api/api_post.php"
32 #define FORMAT "text" // pastebin is only used for text.
33 #define EXPIRE "1M" // 1 month
34 #define DEV_KEY "4dacb211338b25bfad20bc6d4358e555" // if you re-use this code, please make your own key !
35 #define OPTION "paste"
36
37 #define NB_URLS 1
38 static const gchar *s_UrlLabels[NB_URLS] = {N_("Direct Link")};
39
40
41 static void upload (const gchar *cText, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError)
42 {
43 if (cText == NULL || *cText == '\0')
44 {
45 g_set_error (pError, 1, 1, D_("Your text is empty and couldn't be uploaded to this server"));
46 return;
47 }
48
49 GError *erreur = NULL;
50 // http://pastebin.com/api
51 gchar *cResult = cairo_dock_get_url_data_with_post (URL, FALSE, &erreur,
52 "api_option", OPTION,
53 "api_user_key", "", // guest
54 "api_paste_private", bAnonymous ? "1" : "0", // unlisted or public
55 "api_paste_name", bAnonymous ? "" : g_getenv ("USER"),
56 "api_paste_expire_date", EXPIRE,
57 "api_paste_format", FORMAT,
58 "api_dev_key", DEV_KEY,
59 "api_paste_code", cText,
60 NULL);
61 if (erreur)
62 {
63 cd_warning (erreur->message);
64 g_error_free (erreur);
65 DND2SHARE_SET_GENERIC_ERROR_WEBSITE ("Pastebin.com");
66 }
67 else
68 {
69 cd_debug (" --> got '%s'", cResult);
70 if (cResult == NULL || ! g_str_has_prefix (cResult, "http"))
71 DND2SHARE_SET_GENERIC_ERROR_WEBSITE ("Pastebin.com");
72 else
73 cResultUrls[0] = cResult;
74 }
75 }
76
77
78 void cd_dnd2share_register_pastebin_backend (void)
79 {
80 cd_dnd2share_register_new_backend (CD_TYPE_TEXT,
81 "pastebin.com",
82 NB_URLS,
83 s_UrlLabels,
84 0,
85 upload);
86 }