| 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 <glib/gstdio.h> |
| 22 |
|
| 23 |
#include "applet-config.h" |
| 24 |
#include "applet-notifications.h" |
| 25 |
#include "applet-struct.h" |
| 26 |
#include "applet-dnd2share.h" |
| 27 |
#include "applet-backend-codepad.h" |
| 28 |
#include "applet-backend-custom.h" |
| 29 |
#include "applet-backend-dropbox.h" |
| 30 |
#include "applet-backend-free.h" |
| 31 |
#include "applet-backend-imagebin.h" |
| 32 |
#include "applet-backend-imgur.h" |
| 33 |
#include "applet-backend-pastebin.h" |
| 34 |
#include "applet-backend-pastebin-mozilla.h" |
| 35 |
#include "applet-backend-paste-ubuntu.h" |
| 36 |
#include "applet-backend-uppix.h" |
| 37 |
#include "applet-backend-videobin.h" |
| 38 |
#include "applet-init.h" |
| 39 |
|
| 40 |
|
| 41 |
CD_APPLET_DEFINE_BEGIN ("dnd2share", |
| 42 |
2, 0, 7, |
| 43 |
CAIRO_DOCK_CATEGORY_APPLET_INTERNET, |
| 44 |
N_("This applet lets you share files easily:\n" |
| 45 |
"Drag-and-drop a file on the icon to upload it to one of the available hosting sites.\n" |
| 46 |
"You can upload text, image, video, files, and even the current clipboard\n" |
| 47 |
" and you can choose amongst many sites, like DropBox, Imageshack, pastebin, etc\n" |
| 48 |
"The resulting URL is automatically stored in the clipboard to be directly copied by CTRL+v.\n" |
| 49 |
"It can keep an history of your last uploads to retrieve them without registering on the sites.\n" |
| 50 |
"Press left-click to copy the latest URL into the clipboard, middle-click to copy it into the mouse selection.\n" |
| 51 |
"You'll need to install 'curl' and 'wget' to upload the data."), |
| 52 |
"Nochka85 (Yann Dulieu) & Fabounet (Fabrice Rey)") |
| 53 |
CD_APPLET_DEFINE_COMMON_APPLET_INTERFACE |
| 54 |
CD_APPLET_REDEFINE_TITLE (N_("Drop to share")) |
| 55 |
CD_APPLET_DEFINE_END |
| 56 |
|
| 57 |
|
| 58 |
//\___________ Here is where you initiate your applet. myConfig is already set at this point, and also myIcon, myContainer, myDock, myDesklet (and myDrawContext if you're in dock mode). The macro CD_APPLET_MY_CONF_FILE and CD_APPLET_MY_KEY_FILE can give you access to the applet's conf-file and its corresponding key-file (also available during reload). If you're in desklet mode, myDrawContext is still NULL, and myIcon's buffers has not been filled, because you may not need them then (idem when reloading). |
| 59 |
CD_APPLET_INIT_BEGIN |
| 60 |
if (myDesklet) |
| 61 |
{ |
| 62 |
CD_APPLET_SET_DESKLET_RENDERER ("Simple"); // set a desklet renderer. |
| 63 |
} |
| 64 |
|
| 65 |
//\____________ we create the history dir |
| 66 |
myData.cWorkingDirPath = g_strdup_printf ("%s/dnd2share", g_cCairoDockDataDir); |
| 67 |
if (! g_file_test (myData.cWorkingDirPath, G_FILE_TEST_EXISTS)) |
| 68 |
{ |
| 69 |
cd_debug ("DND2SHARE : le dossier '%s' n'existe pas encore -> On le crée", myData.cWorkingDirPath); |
| 70 |
if (g_mkdir (myData.cWorkingDirPath, 7*8*8+7*8+5) != 0) |
| 71 |
{ |
| 72 |
cd_warning ("couldn't create directory '%s' !\nNo history will be available.", myData.cWorkingDirPath); |
| 73 |
myConfig.iNbItems = 0; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
//\____________ We clean the history dir if needed |
| 78 |
cd_dnd2share_clean_working_directory (); |
| 79 |
|
| 80 |
//\____________ We save all backend (warning: we have to respect the same order as set in the .conf file) |
| 81 |
// custom backends, number 0. |
| 82 |
cd_dnd2share_register_custom_backends (); |
| 83 |
// text backends |
| 84 |
cd_dnd2share_register_pastebin_backend (); |
| 85 |
cd_dnd2share_register_paste_ubuntu_backend (); |
| 86 |
cd_dnd2share_register_pastebin_mozilla_backend (); |
| 87 |
cd_dnd2share_register_codepad_backend (); |
| 88 |
// image backends |
| 89 |
cd_dnd2share_register_uppix_backend (); |
| 90 |
cd_dnd2share_register_imagebin_backend (); |
| 91 |
cd_dnd2share_register_imgur_backend (); |
| 92 |
// video backends |
| 93 |
cd_dnd2share_register_videobin_backend (); |
| 94 |
// file backends |
| 95 |
cd_dnd2share_register_free_backend (); |
| 96 |
cd_dnd2share_register_dropbox_backend (); |
| 97 |
|
| 98 |
/// NOTE: when adding/removing backends: change CD_NB_SITES* in struct.h !! |
| 99 |
|
| 100 |
int t; |
| 101 |
for (t = 0; t < CD_NB_FILE_TYPES; t ++) |
| 102 |
myData.pCurrentBackend[t] = &myData.backends[t][myConfig.iPreferedSite[t]]; |
| 103 |
|
| 104 |
//\____________ We build the history. |
| 105 |
if (myConfig.iNbItems != 0) |
| 106 |
cd_dnd2share_build_history (); |
| 107 |
|
| 108 |
//\____________ We add the last URL in the memory (for the left click) |
| 109 |
if (myData.pUpoadedItems != NULL) |
| 110 |
{ |
| 111 |
CDUploadedItem *pItem = g_list_last (myData.pUpoadedItems)->data; |
| 112 |
cd_dnd2share_set_current_url_from_item (pItem); |
| 113 |
} |
| 114 |
|
| 115 |
//\____________ We display the last uploaded image. |
| 116 |
if (myConfig.bDisplayLastImage && myData.pUpoadedItems != NULL) |
| 117 |
{ |
| 118 |
CDUploadedItem *pItem = myData.pUpoadedItems->data; |
| 119 |
gchar *cPreview = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, pItem->cItemName); |
| 120 |
if (g_file_test (cPreview, G_FILE_TEST_EXISTS)) |
| 121 |
CD_APPLET_SET_IMAGE_ON_MY_ICON (cPreview); |
| 122 |
g_free (cPreview); |
| 123 |
} |
| 124 |
CD_APPLET_SET_DEFAULT_IMAGE_ON_MY_ICON_IF_NONE; |
| 125 |
|
| 126 |
//\____________ register to notifications. |
| 127 |
CD_APPLET_REGISTER_FOR_CLICK_EVENT; |
| 128 |
CD_APPLET_REGISTER_FOR_DROP_DATA_EVENT; |
| 129 |
CD_APPLET_REGISTER_FOR_SCROLL_EVENT; |
| 130 |
CD_APPLET_REGISTER_FOR_BUILD_MENU_EVENT; |
| 131 |
CD_APPLET_REGISTER_FOR_MIDDLE_CLICK_EVENT; |
| 132 |
|
| 133 |
CD_APPLET_INIT_END |
| 134 |
|
| 135 |
|
| 136 |
//\___________ Here is where you stop your applet. myConfig and myData are still valid, but will be reseted to 0 at the end of the function. In the end, your applet will go back to its original state, as if it had never been activated. |
| 137 |
CD_APPLET_STOP_BEGIN |
| 138 |
CD_APPLET_UNREGISTER_FOR_CLICK_EVENT; |
| 139 |
CD_APPLET_UNREGISTER_FOR_DROP_DATA_EVENT; |
| 140 |
CD_APPLET_UNREGISTER_FOR_SCROLL_EVENT; |
| 141 |
CD_APPLET_UNREGISTER_FOR_BUILD_MENU_EVENT; |
| 142 |
CD_APPLET_UNREGISTER_FOR_MIDDLE_CLICK_EVENT; |
| 143 |
|
| 144 |
CD_APPLET_STOP_END |
| 145 |
|
| 146 |
|
| 147 |
//\___________ The reload occurs in 2 occasions : when the user changes the applet's config, and when the user reload the cairo-dock's config or modify the desklet's size. The macro CD_APPLET_MY_CONFIG_CHANGED can tell you this. myConfig has already been reloaded at this point if you're in the first case, myData is untouched. You also have the macro CD_APPLET_MY_CONTAINER_TYPE_CHANGED that can tell you if you switched from dock/desklet to desklet/dock mode. |
| 148 |
CD_APPLET_RELOAD_BEGIN |
| 149 |
if (CD_APPLET_MY_CONFIG_CHANGED) |
| 150 |
{ |
| 151 |
if (myDesklet && CD_APPLET_MY_CONTAINER_TYPE_CHANGED) // we are now in a desklet, set a renderer. |
| 152 |
{ |
| 153 |
CD_APPLET_SET_DESKLET_RENDERER ("Simple"); // set a desklet renderer. |
| 154 |
} |
| 155 |
|
| 156 |
//\____________ We clean the working directory if needed |
| 157 |
cd_dnd2share_clean_working_directory (); |
| 158 |
|
| 159 |
//\____________ We rebuild the history. |
| 160 |
cd_dnd2share_clear_history (); |
| 161 |
if (myConfig.iNbItems != 0) |
| 162 |
cd_dnd2share_build_history (); |
| 163 |
|
| 164 |
int t; |
| 165 |
for (t = 0; t < CD_NB_FILE_TYPES; t ++) |
| 166 |
myData.pCurrentBackend[t] = &myData.backends[t][myConfig.iPreferedSite[t]]; |
| 167 |
|
| 168 |
//\____________ We update the last URL |
| 169 |
if (myData.cLastURL != NULL && myData.pUpoadedItems != NULL) |
| 170 |
{ |
| 171 |
CDUploadedItem *pItem = myData.pUpoadedItems->data; |
| 172 |
g_free (myData.cLastURL); |
| 173 |
myData.cLastURL = g_strdup (cd_dnd2share_get_prefered_url_from_item (pItem)); |
| 174 |
} |
| 175 |
|
| 176 |
//\____________ We display the last uploaded image. |
| 177 |
if (myConfig.bDisplayLastImage && myData.pUpoadedItems != NULL) |
| 178 |
{ |
| 179 |
CDUploadedItem *pItem = g_list_nth_data (myData.pUpoadedItems, myData.iCurrentItemNum); |
| 180 |
if (pItem == NULL) |
| 181 |
pItem = myData.pUpoadedItems->data; |
| 182 |
gchar *cPreview = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, pItem->cItemName); |
| 183 |
if (g_file_test (cPreview, G_FILE_TEST_EXISTS)) |
| 184 |
CD_APPLET_SET_IMAGE_ON_MY_ICON (cPreview); |
| 185 |
g_free (cPreview); |
| 186 |
} |
| 187 |
CD_APPLET_SET_DEFAULT_IMAGE_ON_MY_ICON_IF_NONE; |
| 188 |
} |
| 189 |
CD_APPLET_RELOAD_END |