Debian Codegraph @

dnd2share/src/applet-dnd2share.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 #include <string.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <glib/gstdio.h>
24
25 #include "applet-struct.h"
26 #include "applet-dnd2share.h"
27
28
29 void cd_dnd2share_free_uploaded_item (CDUploadedItem *pItem)
30 {
31 if (pItem == NULL)
32 return ;
33 g_strfreev (pItem->cDistantUrls);
34 g_free (pItem->cItemName);
35 g_free (pItem->cLocalPath);
36 g_free (pItem);
37 }
38
39
40 static gchar * _get_short_text_for_menu (const gchar *cInitText)
41 {
42 if (cInitText == NULL) // the backend should not allow that!
43 return g_strdup (D_("No text"));
44
45 // remove extras withespaces first
46 gchar *cLongText = g_strstrip (g_strdup (cInitText));
47 // then print only the first line => no, like Clipper
48 /*gchar *str = strchr (cLongText, '\n');
49 if (str)
50 *str = '\0';*/
51 gchar *cShortText = cairo_dock_cut_string (cLongText, 40);
52
53 // With 'Text' label, it's different than filename
54 gchar *cResult = g_strdup_printf ("%s %s", D_("Text:"), cShortText);
55
56 g_free (cLongText);
57 g_free (cShortText);
58
59 return cResult;
60 }
61
62 void cd_dnd2share_build_history (void)
63 {
64 gchar *cConfFilePath = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, "history.conf");
65 GKeyFile *pKeyFile = cairo_dock_open_key_file (cConfFilePath);
66 g_free (cConfFilePath);
67 if (pKeyFile == NULL) // no history yet.
68 return ;
69
70 gsize length = 0;
71 gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
72 if (pGroupList == NULL)
73 return ;
74
75 CDUploadedItem *pItem;
76 int iSiteID, iFileType;
77 gchar *cItemName;
78 GString *sUrlKey = g_string_new ("");
79 GError *erreur = NULL;
80 int i,j;
81 for (i = 0; pGroupList[i] != NULL; i ++)
82 {
83 cItemName = pGroupList[i];
84 iSiteID = g_key_file_get_integer (pKeyFile, cItemName, "site", &erreur);
85 if (erreur != NULL)
86 {
87 cd_warning (erreur->message);
88 g_error_free (erreur);
89 erreur = NULL;
90 g_free (cItemName);
91 continue;
92 }
93 if (iSiteID >= CD_NB_SITES_MAX)
94 {
95 cd_warning ("dnd2share : this backend doesn't exist !");
96 g_free (cItemName);
97 continue;
98 }
99 iFileType = g_key_file_get_integer (pKeyFile, cItemName, "type", &erreur);
100 if (erreur != NULL)
101 {
102 cd_warning (erreur->message);
103 g_error_free (erreur);
104 erreur = NULL;
105 g_free (cItemName);
106 continue;
107 }
108 if (iFileType >= CD_NB_FILE_TYPES)
109 {
110 cd_warning ("dnd2share : this type of file doesn't exist !");
111 g_free (cItemName);
112 continue;
113 }
114
115 pItem = g_new0 (CDUploadedItem, 1);
116 pItem->cItemName = cItemName;
117 pItem->iSiteID = iSiteID;
118 pItem->iFileType = iFileType;
119 pItem->cDistantUrls = g_new0 (gchar*, myData.backends[iFileType][iSiteID].iNbUrls+1);
120 for (j = 0; j < myData.backends[iFileType][iSiteID].iNbUrls; j ++)
121 {
122 g_string_printf (sUrlKey, "url%d", j);
123 pItem->cDistantUrls[j] = g_key_file_get_string (pKeyFile, cItemName, sUrlKey->str, NULL); // NULL if this URL has not been saved before.
124 }
125 pItem->iDate = g_key_file_get_integer (pKeyFile, cItemName, "date", NULL);
126
127 pItem->cLocalPath = g_key_file_get_string (pKeyFile, cItemName, "local path", NULL);
128 if (pItem->iFileType == CD_TYPE_TEXT)
129 pItem->cFileName = _get_short_text_for_menu (pItem->cLocalPath);
130 else
131 pItem->cFileName = g_path_get_basename (pItem->cLocalPath);
132
133 myData.pUpoadedItems = g_list_prepend (myData.pUpoadedItems, pItem);
134 }
135 g_string_free (sUrlKey, TRUE);
136 g_free (pGroupList); // the content has been added in the list.
137 g_key_file_free (pKeyFile);
138 }
139
140 void cd_dnd2share_clear_history (void)
141 {
142 g_list_foreach (myData.pUpoadedItems, (GFunc) cd_dnd2share_free_uploaded_item, NULL);
143 g_list_free (myData.pUpoadedItems);
144 myData.pUpoadedItems = NULL;
145 }
146
147
148
149 static void _cd_dnd2share_threaded_upload (CDSharedMemory *pSharedMemory)
150 {
151 gchar *cFilePath = pSharedMemory->cCurrentFilePath;
152
153 pSharedMemory->cResultUrls = g_new0 (gchar *, pSharedMemory->iNbUrls+1); // NULL-terminated
154 pSharedMemory->upload (cFilePath, pSharedMemory->cLocalDir, pSharedMemory->bAnonymous, pSharedMemory->iLimitRate, pSharedMemory->cResultUrls, &pSharedMemory->pError);
155
156 if (pSharedMemory->cResultUrls[0] && pSharedMemory->iTinyURLService != 0) // tiny-url.
157 {
158 gchar *Command = NULL;
159 switch (pSharedMemory->iTinyURLService)
160 {
161 case 1:
162 default:
163 Command = g_strdup_printf ("http://tinyurl.com/api-create.php?url=%s", pSharedMemory->cResultUrls[0]);
164 break;
165 case 2:
166 Command = g_strdup_printf ("http://shorterlink.org/createlink.php?url=%s", pSharedMemory->cResultUrls[0]);
167 break;
168 /*http://soso.bz/
169 http://notlong.com/links/
170 http://www.minu.me/
171 http://cuturl.biz/
172 http://tiny.cc/
173 http://o-x.fr/create.php
174 http://petitlien.fr/create.php
175 http://bit.ly
176 http://is.gd/create.php*/
177 }
178 pSharedMemory->cResultUrls[pSharedMemory->iNbUrls-1] = cairo_dock_get_url_data (Command, NULL);
179 g_free (Command);
180 }
181 }
182
183 static void _cd_dnd2share_show_error_dialog (const gchar *cError)
184 {
185 gldi_dialogs_remove_on_icon (myIcon);
186
187 gldi_dialog_show_temporary_with_icon (cError,
188 myIcon,
189 myContainer,
190 myConfig.dTimeDialogs * 2,
191 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
192 }
193
194 static gboolean _cd_dnd2share_update_from_result (CDSharedMemory *pSharedMemory)
195 {
196 CD_APPLET_ENTER;
197 gchar *cFilePath = pSharedMemory->cCurrentFilePath;
198 if (pSharedMemory->pError != NULL)
199 _cd_dnd2share_show_error_dialog (pSharedMemory->pError->message);
200 else if (pSharedMemory->cResultUrls == NULL || pSharedMemory->cResultUrls[0] == NULL) // just to be sure
201 _cd_dnd2share_show_error_dialog (DND2SHARE_GENERIC_ERROR_MSG);
202 else
203 {
204 CDSiteBackend *pCurrentBackend = myData.pCurrentBackend[pSharedMemory->iCurrentFileType];
205 // we add it in the history.
206 if (myConfig.iNbItems != 0)
207 {
208 // open the file which contains the history
209 gchar *cConfFilePath = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, "history.conf");
210 GKeyFile *pKeyFile;
211 if (! g_file_test (cConfFilePath, G_FILE_TEST_EXISTS)) // no history yet.
212 pKeyFile = g_key_file_new ();
213 else
214 pKeyFile = cairo_dock_open_key_file (cConfFilePath);
215 if (pKeyFile == NULL) // probleme de droit ?
216 {
217 cd_warning ("Couldn't add this item to history.");
218 }
219 else
220 {
221 // we check the size limit
222 gsize length = 0;
223 gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
224 if (length == myConfig.iNbItems) // if yes, we remove the first entry
225 {
226 g_key_file_remove_group (pKeyFile, pGroupList[0], NULL);
227 if (myData.pUpoadedItems != NULL) // which is the last one in the list
228 {
229 GList *it = g_list_last (myData.pUpoadedItems);
230 if (it->prev != NULL)
231 it->prev->next = NULL;
232 it->prev = NULL;
233 cd_dnd2share_free_uploaded_item (it->data);
234 g_list_free1 (it);
235 }
236 }
237 g_strfreev (pGroupList);
238
239 // we add the new item at the end of the file
240 time_t iDate = time (NULL);
241 gchar *cItemName = g_strdup_printf ("item_%ld", iDate);
242
243 g_key_file_set_integer (pKeyFile, cItemName, "site", myConfig.iPreferedSite[pSharedMemory->iCurrentFileType]);
244 g_key_file_set_integer (pKeyFile, cItemName, "date", iDate);
245 g_key_file_set_integer (pKeyFile, cItemName, "type", pSharedMemory->iCurrentFileType);
246 GString *sUrlKey = g_string_new ("");
247 int j;
248 for (j = 0; j < pCurrentBackend->iNbUrls; j ++)
249 {
250 g_string_printf (sUrlKey, "url%d", j);
251 g_key_file_set_string (pKeyFile, cItemName, sUrlKey->str, pSharedMemory->cResultUrls[j]);
252 }
253 g_key_file_set_string (pKeyFile, cItemName, "local path", cFilePath);
254
255 // and at the beginning of the list
256 CDUploadedItem *pItem = g_new0 (CDUploadedItem, 1);
257 pItem->cItemName = cItemName;
258 pItem->iSiteID = myConfig.iPreferedSite[pSharedMemory->iCurrentFileType];
259 pItem->iFileType = pSharedMemory->iCurrentFileType;
260 pItem->cDistantUrls = g_new0 (gchar*, pCurrentBackend->iNbUrls + 1);
261 for (j = 0; j < pCurrentBackend->iNbUrls; j ++)
262 {
263 pItem->cDistantUrls[j] = g_strdup (pSharedMemory->cResultUrls[j]);
264 }
265 pItem->iDate = iDate;
266 pItem->cLocalPath = g_strdup (cFilePath);
267 if (pItem->iFileType == CD_TYPE_TEXT)
268 pItem->cFileName = _get_short_text_for_menu (cFilePath);
269 else
270 pItem->cFileName = g_path_get_basename (cFilePath);
271 myData.pUpoadedItems = g_list_prepend (myData.pUpoadedItems, pItem);
272
273 // We flush the file.
274 cairo_dock_write_keys_to_file (pKeyFile, cConfFilePath);
275 g_key_file_free (pKeyFile);
276 g_string_free (sUrlKey, TRUE);
277
278 // we keep a copy if it's an image
279 if (myConfig.bkeepCopy && pSharedMemory->iCurrentFileType == CD_TYPE_IMAGE)
280 {
281 gchar *cCommand = g_strdup_printf ("cp '%s' '%s/%s'", cFilePath, myData.cWorkingDirPath, cItemName);
282 int r = system (cCommand);
283 if (r < 0)
284 cd_warning ("Not able to launch this command: %s", cCommand);
285 g_free (cCommand);
286 }
287 }
288 g_free (cConfFilePath);
289 }
290
291 // We copy the url to the clipboard
292 gchar *cURL = NULL;
293 if (myConfig.bUseTinyAsDefault)
294 cURL = pSharedMemory->cResultUrls[pCurrentBackend->iNbUrls-1];
295 if (cURL == NULL)
296 cURL = pSharedMemory->cResultUrls[pCurrentBackend->iPreferedUrlType];
297 if (cURL == NULL)
298 {
299 int i;
300 for (i = 0; i < pCurrentBackend->iNbUrls && cURL == NULL; i ++)
301 {
302 cURL = pSharedMemory->cResultUrls[i];
303 }
304 }
305 cd_dnd2share_copy_url_to_clipboard (cURL);
306
307 // we keep the last URL if we don't want the history.
308 g_free (myData.cLastURL);
309 myData.cLastURL = g_strdup (cURL);
310 myData.iCurrentItemNum = 0;
311
312 // we can now display a dialogue.
313 if (myConfig.bEnableDialogs || myDesklet)
314 {
315 gldi_dialogs_remove_on_icon (myIcon);
316 gldi_dialog_show_temporary_with_icon (D_("File has been uploaded.\nJust press CTRL+v to paste its URL anywhere."),
317 myIcon,
318 myContainer,
319 myConfig.dTimeDialogs,
320 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
321 }
322
323 // and set the image on the icon.
324 if (myConfig.bDisplayLastImage)
325 {
326 if (pSharedMemory->iCurrentFileType == CD_TYPE_IMAGE)
327 CD_APPLET_SET_IMAGE_ON_MY_ICON (cFilePath);
328 else
329 CD_APPLET_SET_IMAGE_ON_MY_ICON (MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
330 }
331 }
332
333 // stop the animation.
334 CD_APPLET_STOP_DEMANDING_ATTENTION;
335
336 // delete the file if it was a temporary one.
337 if (pSharedMemory->bTempFile)
338 {
339 g_remove (pSharedMemory->cCurrentFilePath);
340 }
341
342 if (myData.cTmpFilePath != NULL)
343 {
344 g_remove (myData.cTmpFilePath);
345 g_free (myData.cTmpFilePath);
346 myData.cTmpFilePath = NULL;
347 }
348
349 gldi_task_discard (myData.pTask);
350 myData.pTask = NULL;
351 CD_APPLET_LEAVE (FALSE);
352 }
353 static void _free_shared_memory (CDSharedMemory *pSharedMemory)
354 {
355 g_free (pSharedMemory->cLocalDir);
356 g_free (pSharedMemory->cCurrentFilePath);
357 g_strfreev (pSharedMemory->cResultUrls);
358 if (pSharedMemory->pError != NULL)
359 g_error_free (pSharedMemory->pError);
360 g_free (pSharedMemory);
361 }
362 #define CD_BUFFER_LENGTH 50
363 void cd_dnd2share_launch_upload (const gchar *cFilePath, CDFileType iFileType)
364 {
365 if (myData.pTask != NULL)
366 {
367 cd_warning ("Please wait the current upload is finished before starting a new one.");
368 gldi_dialogs_remove_on_icon (myIcon);
369 gldi_dialog_show_temporary_with_icon (D_("Please wait for the current upload to finish before starting a new one."),
370 myIcon,
371 myContainer,
372 myConfig.dTimeDialogs,
373 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
374 return ;
375 }
376
377 if (myData.pCurrentBackend[iFileType]->upload == NULL)
378 {
379 cd_warning ("sorry, it's still not possible to upload this type of file");
380 return ;
381 }
382
383 // launch the task.
384 CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1);
385 gboolean bIsPath = FALSE; // we can receive text or a text file
386 if (strncmp (cFilePath, "file://", 7) == 0)
387 {
388 cd_debug ("FilePath: %s", cFilePath);
389 cFilePath += 7;
390 bIsPath = TRUE;
391 }
392 else if (iFileType == CD_TYPE_TEXT && *cFilePath == '/' && g_file_test (cFilePath, G_FILE_TEST_EXISTS))
393 bIsPath = TRUE;
394
395 gchar *cTmpFile = NULL;
396 if (myConfig.bUseOnlyFileType)
397 {
398 // for a piece of text, write it in a temporary file and upload this one.
399 if (iFileType == CD_TYPE_TEXT && ! bIsPath)
400 {
401 // make a filename based on the upload date.
402 cTmpFile = g_new0 (gchar, CD_BUFFER_LENGTH+1);
403 time_t epoch = time (NULL);
404 struct tm currentTime;
405 localtime_r (&epoch, &currentTime);
406 strftime (cTmpFile, CD_BUFFER_LENGTH, "/tmp/cd-%F__%H-%M-%S.txt", &currentTime);
407
408 // write the text inside.
409 g_file_set_contents (cTmpFile,
410 cFilePath,
411 -1,
412 NULL);
413
414 // upload this file
415 cFilePath = cTmpFile;
416 pSharedMemory->bTempFile = TRUE;
417 }
418 // force the 'file' type to be used.
419 pSharedMemory->iCurrentFileType = CD_TYPE_FILE;
420 }
421 else
422 {
423 pSharedMemory->iCurrentFileType = iFileType;
424 }
425
426 // If we drop a text file, we have an URI but we want to post the content to a website
427 if (pSharedMemory->iCurrentFileType == CD_TYPE_TEXT && bIsPath)
428 {
429 cd_debug ("Type is text and it's a file: %s", cFilePath);
430 gchar *cContents = NULL;
431 gsize iLength;
432 g_file_get_contents (cFilePath, &cContents, &iLength, NULL);
433 if (cContents == NULL) // file was not readable, abort.
434 {
435 cd_warning ("file not readable !");
436 gldi_dialogs_remove_on_icon (myIcon);
437 gldi_dialog_show_temporary_with_icon (D_("This file is not readable."),
438 myIcon,
439 myContainer,
440 myConfig.dTimeDialogs,
441 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
442 return ;
443 }
444 pSharedMemory->cCurrentFilePath = cContents;
445 }
446 else
447 pSharedMemory->cCurrentFilePath = g_strdup (cFilePath);
448
449 g_free (cTmpFile);
450
451 pSharedMemory->iTinyURLService = myConfig.iTinyURLService;
452 pSharedMemory->cLocalDir = g_strdup (myConfig.cLocalDir);
453 pSharedMemory->bAnonymous = myConfig.bAnonymous;
454 pSharedMemory->iLimitRate = myConfig.iLimitRate;
455
456 CDSiteBackend *pCurrentBackend = myData.pCurrentBackend[pSharedMemory->iCurrentFileType];
457 g_return_if_fail (pCurrentBackend != NULL);
458 pSharedMemory->upload = pCurrentBackend->upload;
459 pSharedMemory->iNbUrls = pCurrentBackend->iNbUrls;
460
461 myData.pTask = gldi_task_new_full (0, // 1 shot task.
462 (GldiGetDataAsyncFunc) _cd_dnd2share_threaded_upload,
463 (GldiUpdateSyncFunc) _cd_dnd2share_update_from_result,
464 (GFreeFunc) _free_shared_memory,
465 pSharedMemory);
466
467 gldi_task_launch (myData.pTask);
468
469 CD_APPLET_DEMANDS_ATTENTION (myConfig.cIconAnimation, 1e6); // we'll stop it later, at the end of the upload.
470 }
471
472
473
474 void cd_dnd2share_clear_working_directory (void)
475 {
476 g_return_if_fail (myData.cWorkingDirPath != NULL && *myData.cWorkingDirPath == '/');
477 gchar *cCommand = g_strdup_printf ("rm -rf '%s'/*", myData.cWorkingDirPath);
478 int r = system (cCommand);
479 if (r < 0)
480 cd_warning ("Not able to launch this command: %s", cCommand);
481 g_free (cCommand);
482
483 gchar *cConfFilePath = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, "history.conf");
484 g_file_set_contents (cConfFilePath, "#dnd2share's history\n\n", -1, NULL);
485 g_free (cConfFilePath);
486
487 if (myConfig.bDisplayLastImage)
488 {
489 CD_APPLET_SET_IMAGE_ON_MY_ICON (MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
490 }
491 }
492
493
494 void cd_dnd2share_clear_copies_in_working_directory (void)
495 {
496 g_return_if_fail (myData.cWorkingDirPath != NULL && *myData.cWorkingDirPath == '/');
497 gchar *cCommand = g_strdup_printf ("find '%s' -mindepth 1 ! -name *.conf -exec rm -f '{}' \\;", myData.cWorkingDirPath);
498 int r = system (cCommand);
499 if (r < 0)
500 cd_warning ("Not able to launch this command: %s", cCommand);
501 g_free (cCommand);
502 }
503
504 void cd_dnd2share_set_working_directory_size (guint iNbItems)
505 {
506 gchar *cConfFilePath = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, "history.conf");
507 GKeyFile *pKeyFile = cairo_dock_open_key_file (cConfFilePath);
508 if (pKeyFile == NULL) // no history yet
509 {
510 g_free (cConfFilePath);
511 return ;
512 }
513
514 gsize length = 0;
515 gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
516 if (length > iNbItems)
517 {
518 gchar *cItemName;
519 GString *sPreviewPath = g_string_new ("");
520 guint i;
521 for (i = 0; pGroupList[i] != NULL && i < length - iNbItems; i ++) // we remove all extras groups and the preview
522 {
523 cItemName = pGroupList[i];
524 g_string_printf (sPreviewPath, "%s/%s", myData.cWorkingDirPath, cItemName);
525 g_remove (sPreviewPath->str);
526 g_key_file_remove_group (pKeyFile, cItemName, NULL);
527 }
528 cairo_dock_write_keys_to_file (pKeyFile, cConfFilePath);
529 g_string_free (sPreviewPath, TRUE);
530 }
531
532 g_strfreev (pGroupList);
533 g_key_file_free (pKeyFile);
534 g_free (cConfFilePath);
535 }
536
537 void cd_dnd2share_clean_working_directory (void)
538 {
539 if (myConfig.iNbItems == 0) // no more history => clean the working directory.
540 {
541 cd_debug ("DND2SHARE : Pas d'historique -> On efface le contenu de '%s'", myData.cWorkingDirPath);
542 cd_dnd2share_clear_working_directory ();
543 }
544 else
545 {
546 cd_dnd2share_set_working_directory_size (myConfig.iNbItems); // we remove extras items.
547 if (! myConfig.bkeepCopy) // we don't want a copy of the images
548 {
549 cd_debug ("DND2SHARE : Pas de copies locales -> On efface les images de '%s'", myData.cWorkingDirPath);
550 cd_dnd2share_clear_copies_in_working_directory ();
551 }
552 }
553 }
554
555
556 void cd_dnd2share_copy_url_to_clipboard (const gchar *cURL)
557 {
558 GtkClipboard *pClipBoard;
559 pClipBoard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
560 gtk_clipboard_set_text (pClipBoard, cURL, -1);
561 }
562 void cd_dnd2share_copy_url_to_primary (const gchar *cURL)
563 {
564 GtkClipboard *pClipBoard;
565 pClipBoard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
566 gtk_clipboard_set_text (pClipBoard, cURL, -1);
567 }
568
569 gchar *cd_dnd2share_get_prefered_url_from_item (CDUploadedItem *pItem)
570 {
571 CDSiteBackend *pBackend = &myData.backends[pItem->iFileType][pItem->iSiteID];
572 //g_print ("%s (type:%d; site:%d)\n", __func__, pItem->iFileType, pItem->iSiteID);
573 gchar *cURL = NULL;
574 if (myConfig.bUseTinyAsDefault)
575 cURL = pItem->cDistantUrls[pBackend->iNbUrls-1];
576 if (cURL == NULL)
577 cURL = pItem->cDistantUrls[pBackend->iPreferedUrlType];
578 if (cURL == NULL)
579 {
580 int i;
581 for (i = 0; i < pBackend->iNbUrls && cURL == NULL; i ++)
582 {
583 cURL = pItem->cDistantUrls[i];
584 }
585 }
586 return cURL;
587 }
588
589 void cd_dnd2share_set_current_url_from_item (CDUploadedItem *pItem)
590 {
591 gchar *cURL = cd_dnd2share_get_prefered_url_from_item (pItem);
592 g_free (myData.cLastURL);
593 myData.cLastURL = g_strdup (cURL);
594
595 int i = 0;
596 GList *it;
597 for (it = myData.pUpoadedItems; it != NULL; it = it->next)
598 {
599 if (it->data == pItem)
600 break ;
601 i ++;
602 }
603 myData.iCurrentItemNum = i;
604 }
605
606
607 void cd_dnd2share_remove_one_item (CDUploadedItem *pItem)
608 {
609 g_return_if_fail (pItem != NULL);
610
611 // we remove the corresponding group in the history file
612 gchar *cConfFilePath = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, "history.conf");
613 if (! g_file_test (cConfFilePath, G_FILE_TEST_EXISTS)) // no history yet.
614 return;
615
616 GKeyFile *pKeyFile = cairo_dock_open_key_file (cConfFilePath);
617 if (pKeyFile == NULL) // right problem?
618 {
619 cd_warning ("Couldn't remove this item from history.");
620 return ;
621 }
622
623 g_key_file_remove_group (pKeyFile, pItem->cItemName, NULL);
624 cairo_dock_write_keys_to_file (pKeyFile, cConfFilePath);
625 g_key_file_free (pKeyFile);
626 g_free (cConfFilePath);
627
628 // we remove the local copy.
629 gchar *cPreviewPath = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, pItem->cItemName);
630 g_remove (cPreviewPath);
631 g_free (cPreviewPath);
632
633 // If it's the current item, switch to the next one.
634 if (myData.pUpoadedItems && myData.pUpoadedItems->data == pItem)
635 {
636 g_free (myData.cLastURL);
637 myData.cLastURL = NULL;
638 myData.iCurrentItemNum = 0;
639 if (myData.pUpoadedItems->next != NULL)
640 {
641 CDUploadedItem *pNextItem = myData.pUpoadedItems->next->data;
642 gchar *cURL = cd_dnd2share_get_prefered_url_from_item (pNextItem);
643 myData.cLastURL = g_strdup (cURL);
644 }
645 }
646
647 // We remove the item from the list
648 myData.pUpoadedItems = g_list_remove (myData.pUpoadedItems, pItem);
649 cd_dnd2share_free_uploaded_item (pItem);
650 }
651
652
653 void cd_dnd2share_register_new_backend (CDFileType iFileType, const gchar *cSiteName, int iNbUrls, const gchar **cUrlLabels, int iPreferedUrlType, CDUploadFunc pUploadFunc)
654 {
655 int iNumSite = myData.iNbSitesForType[iFileType];
656 CDSiteBackend *pNewBackend = &myData.backends[iFileType][iNumSite];
657 myData.iNbSitesForType[iFileType] ++;
658
659 pNewBackend->cSiteName = cSiteName;
660 pNewBackend->iNbUrls = iNbUrls + 1; // +1 for tiny-url.
661 pNewBackend->cUrlLabels = g_new0 (gchar *, pNewBackend->iNbUrls+1); // +1 for end NULL.
662 memcpy (pNewBackend->cUrlLabels, cUrlLabels, iNbUrls * sizeof (gchar*)); // we take N first labels given by the backend.
663 pNewBackend->cUrlLabels[iNbUrls] = D_("Tiny URL"); // + tiny-url.
664 pNewBackend->iPreferedUrlType = iPreferedUrlType;
665 pNewBackend->upload = pUploadFunc;
666 }