| 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 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include "applet-struct.h" |
| 24 |
#include "applet-dnd2share.h" |
| 25 |
#include "applet-backend-custom.h" |
| 26 |
|
| 27 |
#define NB_URLS 1 |
| 28 |
static const gchar *s_UrlLabels[NB_URLS] = {N_("Direct Link")}; |
| 29 |
|
| 30 |
|
| 31 |
static void _upload (CDFileType iCurrentFileType, const gchar *cFilePath, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError) |
| 32 |
{ |
| 33 |
/* => already check in applet-config.c |
| 34 |
if (myConfig.cCustomScripts[iCurrentFileType] == NULL) |
| 35 |
{ |
| 36 |
const gchar *cError = D_("No script set for this file type"); |
| 37 |
cd_warning ("%s", cError); |
| 38 |
g_set_error (pError, 1, 1, "%s", cError); |
| 39 |
return; |
| 40 |
}*/ |
| 41 |
|
| 42 |
// Upload the file |
| 43 |
gchar *cCommand = g_strdup_printf ("%s '%s'", myConfig.cCustomScripts[iCurrentFileType], cFilePath); |
| 44 |
gchar *cResult = cairo_dock_launch_command_sync (cCommand); |
| 45 |
g_free (cCommand); |
| 46 |
if (cResult == NULL || *cResult == '\0') |
| 47 |
{ |
| 48 |
g_set_error (pError, 1, 1, DND2SHARE_GENERIC_ERROR_MSG); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
if (cResult[strlen(cResult)-1] == '\r') |
| 53 |
cResult[strlen(cResult)-1] = '\0'; |
| 54 |
if (cResult[strlen(cResult)-1] == '\n') |
| 55 |
cResult[strlen(cResult)-1] = '\0'; |
| 56 |
|
| 57 |
// We take the last line in case of there are some log text before |
| 58 |
gchar *str = strrchr (cResult, '\n'); |
| 59 |
if (str != NULL) |
| 60 |
str ++; |
| 61 |
else |
| 62 |
str = cResult; |
| 63 |
|
| 64 |
if (! cairo_dock_string_is_address (str)) |
| 65 |
cd_warning ("this address (%s) seems not valid !\nThe output was : '%s'", str, cResult); |
| 66 |
|
| 67 |
cResultUrls[0] = g_strdup (str); |
| 68 |
g_free (cResult); |
| 69 |
} |
| 70 |
|
| 71 |
static void upload_text (const gchar *cFilePath, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError) |
| 72 |
{ |
| 73 |
if (cFilePath == NULL || *cFilePath == '\0') |
| 74 |
{ |
| 75 |
g_set_error (pError, 1, 1, D_("Your text is empty and couldn't be uploaded to this server")); |
| 76 |
return; |
| 77 |
} |
| 78 |
_upload (CD_TYPE_TEXT, cFilePath, cLocalDir, bAnonymous, iLimitRate, cResultUrls, pError); |
| 79 |
} |
| 80 |
|
| 81 |
static void upload_image (const gchar *cFilePath, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError) |
| 82 |
{ |
| 83 |
_upload (CD_TYPE_IMAGE, cFilePath, cLocalDir, bAnonymous, iLimitRate, cResultUrls, pError); |
| 84 |
} |
| 85 |
|
| 86 |
static void upload_video (const gchar *cFilePath, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError) |
| 87 |
{ |
| 88 |
_upload (CD_TYPE_VIDEO, cFilePath, cLocalDir, bAnonymous, iLimitRate, cResultUrls, pError); |
| 89 |
} |
| 90 |
|
| 91 |
static void upload_file (const gchar *cFilePath, gchar *cLocalDir, gboolean bAnonymous, gint iLimitRate, gchar **cResultUrls, GError **pError) |
| 92 |
{ |
| 93 |
_upload (CD_TYPE_FILE, cFilePath, cLocalDir, bAnonymous, iLimitRate, cResultUrls, pError); |
| 94 |
} |
| 95 |
|
| 96 |
static const CDUploadFunc upload_funcs[CD_NB_FILE_TYPES] = {upload_text, upload_image, upload_video, upload_file}; |
| 97 |
|
| 98 |
void cd_dnd2share_register_custom_backends (void) |
| 99 |
{ |
| 100 |
CDFileType t; |
| 101 |
for (t = 0; t < CD_NB_FILE_TYPES; t ++) |
| 102 |
{ |
| 103 |
cd_dnd2share_register_new_backend (t, |
| 104 |
"custom", |
| 105 |
NB_URLS, |
| 106 |
s_UrlLabels, |
| 107 |
0, |
| 108 |
upload_funcs[t]); |
| 109 |
} |
| 110 |
} |