Debian Codegraph @

dnd2share/src/applet-notifications.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 <string.h>
23 #include <unistd.h>
24 #include <glib/gstdio.h>
25
26 #include "applet-struct.h"
27 #include "applet-dnd2share.h"
28 #include "applet-notifications.h"
29
30 static void _on_answer_clear_history (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer data, CairoDialog *pDialog)
31 {
32 CD_APPLET_ENTER;
33 if (iClickedButton == 0 || iClickedButton == -1) // ok button or Enter.
34 {
35 cd_dnd2share_clear_working_directory ();
36 cd_dnd2share_clear_history ();
37 }
38 CD_APPLET_LEAVE ();
39 }
40
41 static void _clear_history (GtkMenuItem *menu_item, gpointer data)
42 {
43 gldi_dialog_show_with_question (D_("Clear the list of the recently uploaded files?"),
44 myIcon, myContainer,
45 "same icon",
46 (CairoDockActionOnAnswerFunc) _on_answer_clear_history, NULL, (GFreeFunc)NULL);
47 }
48
49 static void _show_local_file (GtkMenuItem *menu_item, CDUploadedItem *pItem)
50 {
51 CD_APPLET_ENTER;
52 if (pItem->iFileType == CD_TYPE_TEXT)
53 {
54 cd_dnd2share_copy_url_to_clipboard (pItem->cLocalPath);
55 if (myConfig.bEnableDialogs)
56 {
57 gldi_dialogs_remove_on_icon (myIcon);
58 gldi_dialog_show_temporary_with_icon (D_("The text has been pasted in the clipboard.\nYou can retrieve it with CTRL+v."),
59 myIcon,
60 myContainer,
61 myConfig.dTimeDialogs,
62 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
63 }
64 }
65 else
66 {
67 if (g_file_test (pItem->cLocalPath, G_FILE_TEST_EXISTS))
68 cairo_dock_fm_launch_uri (pItem->cLocalPath);
69 else
70 {
71 gchar *cPreviewPath = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, pItem->cItemName);
72 if (g_file_test (cPreviewPath, G_FILE_TEST_EXISTS))
73 {
74 cairo_dock_fm_launch_uri (cPreviewPath);
75 }
76 else
77 {
78 cd_warning ("couldn't find the orignial file nor a preview of it");
79 gldi_dialogs_remove_on_icon (myIcon);
80 gldi_dialog_show_temporary_with_icon (D_("Sorry, couldn't find the original file nor a preview of it."),
81 myIcon,
82 myContainer,
83 myConfig.dTimeDialogs,
84 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
85 }
86 g_free (cPreviewPath);
87 }
88 }
89 CD_APPLET_LEAVE ();
90 }
91 static void _remove_from_history (GtkMenuItem *menu_item, CDUploadedItem *pItem)
92 {
93 CD_APPLET_ENTER;
94 cd_dnd2share_remove_one_item (pItem);
95 CD_APPLET_LEAVE ();
96 }
97
98 static void _copy_url_into_clipboard (GtkMenuItem *menu_item, const gchar *cURL)
99 {
100 CD_APPLET_ENTER;
101 cd_dnd2share_copy_url_to_clipboard (cURL);
102 if (myConfig.bEnableDialogs)
103 {
104 gldi_dialogs_remove_on_icon (myIcon);
105 gldi_dialog_show_temporary_with_icon (D_("The URL has been stored in the clipboard.\nJust use 'CTRL+v' to paste it anywhere."),
106 myIcon,
107 myContainer,
108 myConfig.dTimeDialogs,
109 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
110 }
111 CD_APPLET_LEAVE ();
112 }
113
114 static void _store_last_url (gboolean bIntoClipboard)
115 {
116 if (myData.cLastURL == NULL)
117 {
118 gldi_dialogs_remove_on_icon (myIcon);
119 gldi_dialog_show_temporary_with_icon (myConfig.iNbItems != 0 ?
120 D_("No uploaded file available\n.Just drag'n drop a file on the icon to upload it") :
121 D_("No uploaded file available.\nConsider activating the history if you want the applet to remember previous uploads."),
122 myIcon,
123 myContainer,
124 myConfig.dTimeDialogs,
125 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
126 }
127 else
128 {
129 if (bIntoClipboard)
130 cd_dnd2share_copy_url_to_clipboard (myData.cLastURL);
131 else
132 cd_dnd2share_copy_url_to_primary (myData.cLastURL);
133
134 if (myConfig.bEnableDialogs)
135 {
136 gldi_dialogs_remove_on_icon (myIcon);
137 gldi_dialog_show_temporary_with_icon (bIntoClipboard ?
138 D_("The current URL has been stored in the clipboard.\nJust use 'CTRL+v' to paste it anywhere.") :
139 D_("The current URL has been stored into the selection.\nJust middle-click to paste it anywhere."),
140 myIcon,
141 myContainer,
142 myConfig.dTimeDialogs,
143 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
144 }
145 }
146 }
147
148 static void _on_drop_data (const gchar *cMyData)
149 {
150 CDFileType iFileType = CD_UNKNOWN_TYPE;
151 gchar *cFilePath = NULL;
152 if (strncmp(cMyData, "file://", 7) == 0)
153 {
154 // Uppix.net supports : GIF, JPEG, PNG, Flash (SWF or SWC), BMP, PSD, TIFF, JP2, JPX,
155 // JB2, JPC, WBMP, and XBM.
156 // ... but the applet will support the most common
157
158 cFilePath = g_filename_from_uri (cMyData, NULL, NULL); // switch to UTF-8.
159 g_return_if_fail (cFilePath != NULL);
160
161 if (strchr(cFilePath, ',') != NULL) // if there is a comma, curl doesn't like it
162 {
163 myData.cTmpFilePath = g_strdup ("/tmp/dnd2share-file_with_comma.XXXXXX");
164 int fds = mkstemp (myData.cTmpFilePath);
165 if (fds == -1)
166 {
167 g_free (myData.cTmpFilePath);
168 myData.cTmpFilePath = NULL;
169 return;
170 }
171 close(fds);
172
173 gchar *cCommandCopyFileWithComma = g_strdup_printf ("cp '%s' '%s'", cFilePath, myData.cTmpFilePath); // copy the current file to tmp.
174 int r = system (cCommandCopyFileWithComma);
175 if (r < 0)
176 cd_warning ("Not able to launch this command: %s", cCommandCopyFileWithComma);
177 g_free (cCommandCopyFileWithComma);
178 g_free (cFilePath);
179 cFilePath = g_strdup (myData.cTmpFilePath); // we use a tmp file which will be remove at the end of the update
180 }
181
182 guint64 iSize;
183 time_t iLastModificationTime;
184 gchar *cMimeType = NULL;
185 int iUID, iGID, iPermissionsMask;
186 if (cairo_dock_fm_get_file_properties (cMyData, &iSize, &iLastModificationTime, &cMimeType, &iUID, &iGID, &iPermissionsMask))
187 {
188 if (cMimeType != NULL)
189 {
190 cd_debug ("cMimeType : %s (%s)", cMimeType, cMyData);
191 if (strncmp (cMimeType, "image", 5) == 0)
192 iFileType = CD_TYPE_IMAGE;
193 else if (strncmp (cMimeType, "video", 5) == 0)
194 iFileType = CD_TYPE_VIDEO;
195 else if (strncmp (cMimeType, "text", 4) == 0)
196 iFileType = CD_TYPE_TEXT;
197 }
198 g_free (cMimeType);
199 }
200
201 if (iFileType == CD_UNKNOWN_TYPE)
202 {
203 if (g_str_has_suffix(cMyData,"jpg")
204 || g_str_has_suffix(cMyData,"JPG")
205 || g_str_has_suffix(cMyData,"png")
206 || g_str_has_suffix(cMyData,"PNG")
207 || g_str_has_suffix(cMyData,"jpeg")
208 || g_str_has_suffix(cMyData,"JPEG")
209 || g_str_has_suffix(cMyData,"gif")
210 || g_str_has_suffix(cMyData,"GIF")
211 || g_str_has_suffix(cMyData,"bmp")
212 || g_str_has_suffix(cMyData,"BMP")
213 || g_str_has_suffix(cMyData,"TIFF")
214 || g_str_has_suffix(cMyData,"tiff"))
215 iFileType = CD_TYPE_IMAGE;
216 else if (g_str_has_suffix(cMyData,"avi")
217 || g_str_has_suffix(cMyData,"AVI")
218 || g_str_has_suffix(cMyData,"ogg")
219 || g_str_has_suffix(cMyData,"OGG")
220 || g_str_has_suffix(cMyData,"ogv")
221 || g_str_has_suffix(cMyData,"OGV")
222 || g_str_has_suffix(cMyData,"mp4")
223 || g_str_has_suffix(cMyData,"MP4")
224 || g_str_has_suffix(cMyData,"mov")
225 || g_str_has_suffix(cMyData,"MOV"))
226 iFileType = CD_TYPE_VIDEO;
227 }
228 }
229 else // text.
230 {
231 // cd_debug ("TEXT\n");
232 iFileType = CD_TYPE_TEXT;
233 }
234
235 if (iFileType == CD_UNKNOWN_TYPE)
236 {
237 iFileType = CD_TYPE_FILE;
238 cd_debug ("we'll consider this as an archive.");
239 }
240 cd_dnd2share_launch_upload (cFilePath ? cFilePath : cMyData, iFileType);
241 g_free (cFilePath);
242 }
243
244 static void _get_image (GtkClipboard *clipboard, GdkPixbuf *pixbuf, gpointer data)
245 {
246 g_return_if_fail (pixbuf != NULL);
247 if (myData.cTmpFilePath != NULL)
248 {
249 cd_warning ("Please wait the current upload is finished before starting a new one.");
250 return ;
251 }
252 myData.cTmpFilePath = g_strdup ("/tmp/dnd2share-tmp-file.XXXXXX");
253 int fds = mkstemp (myData.cTmpFilePath);
254 if (fds == -1)
255 {
256 g_free (myData.cTmpFilePath);
257 myData.cTmpFilePath = NULL;
258 return ;
259 }
260 close(fds);
261
262 CD_APPLET_ENTER;
263 gboolean bSaved = gdk_pixbuf_save (pixbuf,
264 myData.cTmpFilePath,
265 "png",
266 NULL,
267 NULL);
268 CD_APPLET_LEAVE_IF_FAIL (bSaved);
269 //g_return_if_fail (bSaved);
270
271 cd_dnd2share_launch_upload (myData.cTmpFilePath, CD_TYPE_IMAGE);
272 CD_APPLET_LEAVE ();
273 }
274 static void _get_text (GtkClipboard *clipboard, const gchar *cText, gpointer data)
275 {
276 g_return_if_fail (cText != NULL);
277 CD_APPLET_ENTER;
278 gchar *cFilePath = NULL;
279 const gchar *cDropData = NULL;
280 if ( *cText == '/' && g_file_test (cText, G_FILE_TEST_EXISTS) )
281 cFilePath = g_strdup_printf ("file://%s", cText);
282 else
283 cDropData = cText;
284
285 _on_drop_data (cFilePath ? cFilePath : cDropData);
286 CD_APPLET_LEAVE ();
287 }
288 static void _send_clipboard (GtkMenuItem *menu_item, gpointer *data)
289 {
290 CD_APPLET_ENTER;
291 GtkClipboard *pClipBoard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
292 gboolean bDataAvailable = gtk_clipboard_wait_is_image_available (pClipBoard);
293 g_return_if_fail (myIcon != NULL); // protection, this function is blocked but the mainloop continue to run
294 if (bDataAvailable)
295 {
296 gtk_clipboard_request_image (pClipBoard, (GtkClipboardImageReceivedFunc) _get_image, data);
297 CD_APPLET_LEAVE ();
298 //return;
299 }
300 bDataAvailable = gtk_clipboard_wait_is_text_available (pClipBoard);
301 g_return_if_fail (myIcon != NULL);
302 if (bDataAvailable)
303 {
304 gtk_clipboard_request_text (pClipBoard, (GtkClipboardTextReceivedFunc) _get_text, data);
305 CD_APPLET_LEAVE ();
306 //return;
307 }
308 CD_APPLET_LEAVE ();
309 }
310
311 // checkbox => Use only a file web hosting
312 static void _set_use_only_file_type (GtkCheckMenuItem *pMenuItem, gpointer *data)
313 {
314 myConfig.bUseOnlyFileType = gtk_check_menu_item_get_active (GTK_CHECK_MENU_ITEM (pMenuItem));
315 cairo_dock_update_conf_file (CD_APPLET_MY_CONF_FILE,
316 G_TYPE_BOOLEAN, "Configuration", "only file type", myConfig.bUseOnlyFileType,
317 G_TYPE_INVALID); // updated the config file => maybe not needed if we want a temporary status
318 }
319
320
321 //\___________ Define here the action to be taken when the user left-clicks on your icon or on its subdock or your desklet. The icon and the container that were clicked are available through the macros CD_APPLET_CLICKED_ICON and CD_APPLET_CLICKED_CONTAINER. CD_APPLET_CLICKED_ICON may be NULL if the user clicked in the container but out of icons.
322 CD_APPLET_ON_CLICK_BEGIN
323 _store_last_url (TRUE);
324 CD_APPLET_ON_CLICK_END
325
326
327 CD_APPLET_ON_DROP_DATA_BEGIN
328 cd_debug ("DND2SHARE : drop de '%s'", CD_APPLET_RECEIVED_DATA);
329 _on_drop_data (CD_APPLET_RECEIVED_DATA);
330 CD_APPLET_ON_DROP_DATA_END
331
332
333 CD_APPLET_ON_SCROLL_BEGIN
334 if (myData.pUpoadedItems == NULL)
335 return GLDI_NOTIFICATION_LET_PASS;
336
337 CDUploadedItem *pItem;
338 if (CD_APPLET_SCROLL_DOWN)
339 {
340 myData.iCurrentItemNum ++; // next item.
341 pItem = g_list_nth_data (myData.pUpoadedItems, myData.iCurrentItemNum);
342 if (pItem == NULL)
343 {
344 pItem = myData.pUpoadedItems->data;
345 myData.iCurrentItemNum = 0;
346 }
347 }
348 else if (CD_APPLET_SCROLL_UP)
349 {
350 myData.iCurrentItemNum --; // prev item.
351 pItem = g_list_nth_data (myData.pUpoadedItems, myData.iCurrentItemNum);
352 if (pItem == NULL)
353 {
354 pItem = g_list_last (myData.pUpoadedItems)->data;
355 cd_debug ("dernier item\n");
356 myData.iCurrentItemNum = g_list_length (myData.pUpoadedItems) - 1;
357 }
358 }
359 else
360 return GLDI_NOTIFICATION_LET_PASS;
361 g_free (myData.cLastURL);
362 myData.cLastURL = NULL;
363
364 g_return_val_if_fail (pItem != NULL, GLDI_NOTIFICATION_LET_PASS); // parano
365
366 myData.cLastURL = g_strdup (cd_dnd2share_get_prefered_url_from_item (pItem));
367 if (myConfig.bDisplayLastImage)
368 {
369 gchar *cPreview = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, pItem->cItemName);
370 if (g_file_test (cPreview, G_FILE_TEST_EXISTS))
371 {
372 CD_APPLET_SET_IMAGE_ON_MY_ICON (cPreview);
373 g_free (cPreview);
374 }
375 else
376 {
377 cPreview = pItem->cLocalPath;
378 if (g_file_test (cPreview, G_FILE_TEST_EXISTS))
379 {
380 CD_APPLET_SET_IMAGE_ON_MY_ICON (cPreview);
381 }
382 else
383 {
384 CD_APPLET_SET_IMAGE_ON_MY_ICON (MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
385 }
386 }
387 CD_APPLET_REDRAW_MY_ICON;
388 }
389
390 if (myConfig.bEnableDialogs)
391 {
392 gldi_dialogs_remove_on_icon (myIcon);
393 gldi_dialog_show_temporary_with_icon_printf ("%s '%s' (n°%d):\n%s",
394 myIcon,
395 myContainer,
396 myConfig.dTimeDialogs,
397 "same icon",
398 (pItem->iFileType == CD_TYPE_TEXT ? D_("Text") : D_("File")),
399 pItem->cFileName,
400 myData.iCurrentItemNum,
401 D_("Click on the icon to copy the URL into the clipboard."));
402 }
403 CD_APPLET_ON_SCROLL_END
404
405
406 CD_APPLET_ON_MIDDLE_CLICK_BEGIN
407 _store_last_url (FALSE);
408 CD_APPLET_ON_MIDDLE_CLICK_END
409
410
411 //\___________ Define here the entries you want to add to the menu when the user right-clicks on your icon or on its subdock or your desklet. The icon and the container that were clicked are available through the macros CD_APPLET_CLICKED_ICON and CD_APPLET_CLICKED_CONTAINER. CD_APPLET_CLICKED_ICON may be NULL if the user clicked in the container but out of icons. The menu where you can add your entries is available throught the macro CD_APPLET_MY_MENU; you can add sub-menu to it if you want.
412 CD_APPLET_ON_BUILD_MENU_BEGIN
413 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Send the clipboard's content"), GLDI_ICON_NAME_PASTE, _send_clipboard, CD_APPLET_MY_MENU);
414
415 GtkWidget *mi;
416 GtkWidget *pHistoryMenu = gldi_menu_add_sub_menu_full (CD_APPLET_MY_MENU, D_("History"), GLDI_ICON_NAME_SORT_DESCENDING, &mi);
417
418 if (myData.pUpoadedItems != NULL)
419 {
420 CDSiteBackend *pBackend;
421 CDUploadedItem *pItem;
422 GtkWidget *pItemSubMenu;
423 gchar *cName = NULL, *cURI = NULL;
424 gboolean bIsDirectory;
425 int iVolumeID;
426 double fOrder;
427 gint iDesiredIconSize = cairo_dock_search_icon_size (GTK_ICON_SIZE_LARGE_TOOLBAR); // 24px by default
428 int i;
429 GList *it;
430 for (it = myData.pUpoadedItems; it != NULL; it = it->next)
431 {
432 pItem = it->data;
433
434 // we're looking for a thumbnail (for the menu)
435 gchar *cPreview = NULL;
436 if (pItem->iFileType == CD_TYPE_IMAGE)
437 {
438 cPreview = g_strdup_printf ("%s/%s", myData.cWorkingDirPath, pItem->cItemName);
439 if (! g_file_test (cPreview, G_FILE_TEST_EXISTS))
440 {
441 g_free (cPreview);
442 cPreview = cairo_dock_search_icon_s_path ("image-x-generic", iDesiredIconSize);
443 }
444 }
445 else if (pItem->iFileType == CD_TYPE_TEXT)
446 {
447 cPreview = cairo_dock_search_icon_s_path ("text-x-generic", iDesiredIconSize);
448 }
449 else if (pItem->iFileType == CD_TYPE_VIDEO)
450 {
451 cPreview = cairo_dock_search_icon_s_path ("video-x-generic", iDesiredIconSize);
452 }
453 if (cPreview == NULL)
454 {
455 gchar *cIconName = NULL;
456 cairo_dock_fm_get_file_info (pItem->cLocalPath, &cName, &cURI, &cIconName, &bIsDirectory, &iVolumeID, &fOrder, 0);
457 cPreview = cairo_dock_search_icon_s_path (cIconName, iDesiredIconSize);
458 g_free (cIconName);
459 g_free (cName);
460 cName = NULL;
461 g_free (cURI);
462 cURI = NULL;
463 }
464
465 // we create a sub menu for this file
466 pItemSubMenu = CD_APPLET_ADD_SUB_MENU_WITH_IMAGE (pItem->cFileName, pHistoryMenu, cPreview);
467 g_free (cPreview);
468
469 // we add links
470 pBackend = &myData.backends[pItem->iFileType][pItem->iSiteID];
471 for (i = 0; i < pBackend->iNbUrls; i ++)
472 {
473 //g_print ("%d) %s : ", i, pBackend->cUrlLabels[i]);
474 //g_print (" + %s\n", pItem->cDistantUrls[i]);
475 if (pItem->cDistantUrls[i] != NULL) // can be null (e.g. tiny url).
476 CD_APPLET_ADD_IN_MENU_WITH_DATA (dgettext (MY_APPLET_GETTEXT_DOMAIN, pBackend->cUrlLabels[i]), _copy_url_into_clipboard, pItemSubMenu, pItem->cDistantUrls[i]);
477 }
478 if (pItem->iFileType != CD_TYPE_TEXT)
479 CD_APPLET_ADD_IN_MENU_WITH_DATA (D_("Open file"), _show_local_file, pItemSubMenu, pItem);
480 else
481 CD_APPLET_ADD_IN_MENU_WITH_DATA (D_("Get text"), _show_local_file, pItemSubMenu, pItem);
482
483 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Remove from history"), GLDI_ICON_NAME_REMOVE, _remove_from_history, pItemSubMenu, pItem);
484 }
485
486 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Clear History"), GLDI_ICON_NAME_CLEAR, _clear_history, pHistoryMenu);
487 }
488 else
489 gtk_widget_set_sensitive (GTK_WIDGET (mi), FALSE);
490
491 pMenuItem = gtk_check_menu_item_new_with_label (D_("Use only a files hosting site"));
492 gtk_menu_shell_append (GTK_MENU_SHELL (CD_APPLET_MY_MENU), pMenuItem);
493 if (myConfig.bUseOnlyFileType)
494 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (pMenuItem), TRUE);
495 g_signal_connect (G_OBJECT (pMenuItem), "toggled", G_CALLBACK (_set_use_only_file_type), NULL);
496 CD_APPLET_ON_BUILD_MENU_END