Debian Codegraph @

Folders/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 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include "applet-struct.h"
25 #include "applet-load-icons.h"
26 #include "applet-notifications.h"
27
28
29 //\___________ 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.
30 CD_APPLET_ON_CLICK_BEGIN
31 if (CD_APPLET_CLICKED_ICON == myIcon)
32 {
33 if (! myConfig.bShowFiles)
34 {
35 cairo_dock_fm_launch_uri (myConfig.cDirPath);
36 }
37 else if (CD_APPLET_MY_ICONS_LIST == NULL) // repertoire vide, ou illisible, ou non defini
38 {
39 gldi_dialogs_remove_on_icon (myIcon);
40 if (myConfig.cDirPath == NULL)
41 gldi_dialog_show_temporary_with_icon (D_("Open the configuration of the applet to choose a folder to import."),
42 myIcon, myContainer,
43 8000.,
44 myConfig.iSubdockViewType == 0 ? "same icon" : MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE);
45 else
46 {
47 gchar *cPath = g_filename_from_uri (myConfig.cDirPath, NULL, NULL);
48 gldi_dialog_show_temporary_with_icon_printf ("%s :\n%s",
49 myIcon, myContainer,
50 4000.,
51 myConfig.iSubdockViewType == 0 ? "same icon" : MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE,
52 cPath ? cPath : myConfig.cDirPath,
53 D_("Empty or unreadable folder."));
54 g_free (cPath);
55 }
56 }
57 else
58 CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS); // on laisse passer la notification (pour ouvrir le sous-dock au clic).
59 }
60 else if (CD_APPLET_CLICKED_ICON != NULL)
61 {
62 //g_print ("clic on %s\n", CD_APPLET_CLICKED_ICON->cBaseURI);
63 if (CD_APPLET_CLICKED_ICON->iVolumeID == -1)
64 {
65 //g_print ("folder\n");
66
67 /// lister le repertoire dans un thread.
68
69 /// lancer une animation de transition.
70
71
72 /// en attendant, on ouvre le repertoire...
73 cairo_dock_fm_launch_uri (CD_APPLET_CLICKED_ICON->cBaseURI);
74 }
75 else
76 {
77 cairo_dock_fm_launch_uri (CD_APPLET_CLICKED_ICON->cBaseURI);
78 }
79 }
80 CD_APPLET_ON_CLICK_END
81
82
83 //\___________ Same as ON_CLICK, but with middle-click.
84 CD_APPLET_ON_MIDDLE_CLICK_BEGIN
85 if (CD_APPLET_CLICKED_ICON == myIcon)
86 {
87 cairo_dock_fm_launch_uri (myConfig.cDirPath);
88 }
89 else if (CD_APPLET_CLICKED_ICON != NULL && CD_APPLET_CLICKED_ICON->iVolumeID != 0) // clic sur un des repertoires.
90 {
91 cairo_dock_fm_launch_uri (CD_APPLET_CLICKED_ICON->cBaseURI);
92 }
93 CD_APPLET_ON_MIDDLE_CLICK_END
94
95
96 //\___________ 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.
97
98 #define MARGIN 3
99 static void _cd_folders_show_file_properties (GtkMenuItem *pMenuItem, gpointer *data)
100 {
101 Icon *icon = data[0];
102 CairoDock *pDock = data[1];
103 //g_print ("%s (%s)\n", __func__, icon->cName);
104
105 guint64 iSize = 0;
106 time_t iLastModificationTime = 0;
107 gchar *cMimeType = NULL;
108 int iUID=0, iGID=0, iPermissionsMask=0;
109 if (cairo_dock_fm_get_file_properties (icon->cBaseURI, &iSize, &iLastModificationTime, &cMimeType, &iUID, &iGID, &iPermissionsMask))
110 {
111 GtkWidget *pDialog = gtk_message_dialog_new (GTK_WINDOW (pDock->container.pWidget),
112 GTK_DIALOG_DESTROY_WITH_PARENT,
113 GTK_MESSAGE_INFO,
114 GTK_BUTTONS_OK,
115 D_("Properties:"));
116
117 GString *sInfo = g_string_new ("");
118 g_string_printf (sInfo, "<b>%s</b>", icon->cName);
119
120 GtkWidget *pLabel= gtk_label_new (NULL);
121 gtk_label_set_use_markup (GTK_LABEL (pLabel), TRUE);
122 gtk_label_set_markup (GTK_LABEL (pLabel), sInfo->str);
123
124 GtkWidget *pFrame = gtk_frame_new (NULL);
125 gtk_container_set_border_width (GTK_CONTAINER (pFrame), MARGIN);
126 gtk_frame_set_label_widget (GTK_FRAME (pFrame), pLabel);
127 gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG(pDialog))), pFrame);
128 gtk_frame_set_shadow_type (GTK_FRAME (pFrame), GTK_SHADOW_OUT);
129
130 GtkWidget *pVBox = gtk_box_new (GTK_ORIENTATION_VERTICAL, MARGIN);
131 gtk_container_add (GTK_CONTAINER (pFrame), pVBox);
132
133 pLabel = gtk_label_new (NULL);
134 gtk_label_set_use_markup (GTK_LABEL (pLabel), TRUE);
135 g_string_printf (sInfo, "<u>%s</u>: %"G_GUINT64_FORMAT" %s", D_("Size"), iSize, D_("bytes"));
136 if (iSize > 1024*1024)
137 g_string_append_printf (sInfo, " (%.1f %s)", 1. * iSize / 1024 / 1024, D_("MB"));
138 else if (iSize > 1024)
139 g_string_append_printf (sInfo, " (%.1f %s)", 1. * iSize / 1024, D_("KB"));
140 gtk_label_set_markup (GTK_LABEL (pLabel), sInfo->str);
141 gtk_container_add (GTK_CONTAINER (pVBox), pLabel);
142
143 pLabel = gtk_label_new (NULL);
144 gtk_label_set_use_markup (GTK_LABEL (pLabel), TRUE);
145 struct tm epoch_tm;
146 localtime_r (&iLastModificationTime, &epoch_tm); // et non pas gmtime_r.
147 gchar *cTimeChain = g_new0 (gchar, 100);
148 strftime (cTimeChain, 100, "%F, %T", &epoch_tm);
149 g_string_printf (sInfo, "<u>%s</u>: %s", D_("Last Modification"), cTimeChain);
150 g_free (cTimeChain);
151 gtk_label_set_markup (GTK_LABEL (pLabel), sInfo->str);
152 gtk_container_add (GTK_CONTAINER (pVBox), pLabel);
153
154 if (cMimeType != NULL)
155 {
156 pLabel = gtk_label_new (NULL);
157 gtk_label_set_use_markup (GTK_LABEL (pLabel), TRUE);
158 g_string_printf (sInfo, "<u>%s</u>: %s", D_("Mime Type"), cMimeType);
159 gtk_label_set_markup (GTK_LABEL (pLabel), sInfo->str);
160 gtk_container_add (GTK_CONTAINER (pVBox), pLabel);
161 }
162
163 GtkWidget *pSeparator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
164 gtk_container_add (GTK_CONTAINER (pVBox), pSeparator);
165
166 pLabel = gtk_label_new (NULL);
167 gtk_label_set_use_markup (GTK_LABEL (pLabel), TRUE);
168 g_string_printf (sInfo, "<u>%s</u>: %d \t <u>%s</u>: %d", D_("User ID"), iUID, D_("Group ID"), iGID);
169 gtk_label_set_markup (GTK_LABEL (pLabel), sInfo->str);
170 gtk_container_add (GTK_CONTAINER (pVBox), pLabel);
171
172 pLabel = gtk_label_new (NULL);
173 gtk_label_set_use_markup (GTK_LABEL (pLabel), TRUE);
174 int iOwnerPermissions = iPermissionsMask >> 6; // 8*8.
175 int iGroupPermissions = (iPermissionsMask - (iOwnerPermissions << 6)) >> 3;
176 int iOthersPermissions = (iPermissionsMask % 8);
177 g_string_printf (sInfo, "<u>%s</u>: %s: %s / %s: %s / %s: %s",
178 D_("Permissions"), D_("Read"),
179 iOwnerPermissions ? D_("Yes") : D_("No"), D_("Write"),
180 iGroupPermissions ? D_("Yes") : D_("No"), D_("Execute"),
181 iOthersPermissions ? D_("Yes") : D_("No"));
182 gtk_label_set_markup (GTK_LABEL (pLabel), sInfo->str);
183 gtk_container_add (GTK_CONTAINER (pVBox), pLabel);
184
185 gtk_window_set_type_hint (GTK_WINDOW (pDialog), GDK_WINDOW_TYPE_HINT_DOCK); // sinon le dialogue sera toujours derriere le sous-dock, meme avec un 'set_keep_above'. en contrepartie, le dialgue n'aura pas de bordure (du moins avec Compiz).
186 gtk_window_set_keep_above (GTK_WINDOW (pDialog), TRUE);
187
188 gtk_widget_show_all (gtk_dialog_get_content_area (GTK_DIALOG(pDialog)));
189 gtk_window_set_position (GTK_WINDOW (pDialog), GTK_WIN_POS_CENTER_ALWAYS);
190 gtk_dialog_run (GTK_DIALOG (pDialog));
191 gtk_widget_destroy (pDialog);
192
193 g_string_free (sInfo, TRUE);
194 g_free (cMimeType);
195 }
196 }
197
198 static void _on_answer_delete_file (int iClickedButton, GtkWidget *pInteractiveWidget, Icon *icon, CairoDialog *pDialog)
199 {
200 if (iClickedButton == 0 || iClickedButton == -1) // ok button or Enter.
201 {
202 gboolean bSuccess = cairo_dock_fm_delete_file (icon->cBaseURI, FALSE);
203 if (! bSuccess)
204 {
205 cd_warning ("couldn't delete this file.\nCheck that you have writing rights on this file.\n");
206 gchar *cMessage = g_strdup_printf (D_("Warning: could not delete this file.\nPlease check file permissions."));
207 gldi_dialog_show_temporary_with_default_icon (cMessage, icon, icon->pContainer, 4000);
208 g_free (cMessage);
209 }
210 }
211 }
212 static void _cd_folders_delete_file (GtkMenuItem *pMenuItem, gpointer *data)
213 {
214 Icon *icon = data[0];
215 GldiContainer *pContainer = data[1];
216 cd_message ("%s (%s)", __func__, icon->cName);
217
218 gchar *cPath = g_filename_from_uri (icon->cBaseURI, NULL, NULL);
219 g_return_if_fail (cPath != NULL);
220 gchar *question = g_strdup_printf (D_("You're about deleting this file\n (%s)\nfrom your hard-disk. Sure ?"), cPath);
221 g_free (cPath);
222 gldi_dialog_show_with_question (question,
223 icon, pContainer,
224 "same icon",
225 (CairoDockActionOnAnswerFunc) _on_answer_delete_file, icon, (GFreeFunc)NULL); // if the icon is deleted during the question, the dialog will disappear with it, and we won't be called back.
226 }
227
228 static void _on_answer_rename_file (int iClickedButton, GtkWidget *pInteractiveWidget, Icon *icon, CairoDialog *pDialog)
229 {
230 if (iClickedButton == 0 || iClickedButton == -1) // ok button or Enter.
231 {
232 const gchar *cNewName = gtk_entry_get_text (GTK_ENTRY (pInteractiveWidget));
233 if (cNewName != NULL && *cNewName != '\0')
234 {
235 gboolean bSuccess = cairo_dock_fm_rename_file (icon->cBaseURI, cNewName);
236 if (! bSuccess)
237 {
238 cd_warning ("couldn't rename this file.\nCheck that you have writing rights, and that the new name does not already exist.");
239 gldi_dialog_show_temporary_with_icon_printf (D_("Warning: could not rename %s.\nCheck file permissions \nand that the new name does not already exist."), icon, icon->pContainer, 5000, NULL, icon->cBaseURI);
240 }
241 }
242 }
243 }
244 static void _cd_folders_rename_file (GtkMenuItem *pMenuItem, gpointer *data)
245 {
246 Icon *icon = data[0];
247 cd_message ("%s (%s)", __func__, icon->cName);
248 gldi_dialog_show_with_entry (D_("Rename to:"),
249 icon, icon->pContainer,
250 "same icon",
251 icon->cName,
252 (CairoDockActionOnAnswerFunc)_on_answer_rename_file, icon, (GFreeFunc)NULL);
253 }
254
255 static void _cd_folders_move_file (GtkMenuItem *pMenuItem, gpointer *data)
256 {
257 Icon *icon = data[0];
258 GldiContainer *pContainer = data[1];
259 GldiModuleInstance *myApplet = data[2];
260 cd_message ("%s (%s)", __func__, icon->cName);
261
262 GtkWidget* pFileChooserDialog = gtk_file_chooser_dialog_new (
263 _("Pick up a directory"),
264 GTK_WINDOW (pContainer->pWidget),
265 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
266 _("Ok"),
267 GTK_RESPONSE_OK,
268 _("Cancel"),
269 GTK_RESPONSE_CANCEL,
270 NULL);
271
272 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (pFileChooserDialog), myConfig.cDirPath);
273 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (pFileChooserDialog), FALSE);
274
275 gtk_widget_show (pFileChooserDialog);
276 int answer = gtk_dialog_run (GTK_DIALOG (pFileChooserDialog));
277 if (answer == GTK_RESPONSE_OK)
278 {
279 gchar *cFilePath = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (pFileChooserDialog));
280
281 cairo_dock_fm_move_file (icon->cBaseURI, cFilePath);
282 }
283 gtk_widget_destroy (pFileChooserDialog);
284 }
285
286 static void _on_answer_create_file (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer *data, CairoDialog *pDialog)
287 {
288 if (iClickedButton == 0 || iClickedButton == -1) // ok button or Enter.
289 {
290 gboolean bFolder = GPOINTER_TO_INT (data[0]);
291 GldiModuleInstance *myApplet = data[1];
292 const gchar *cNewName = gtk_entry_get_text (GTK_ENTRY (pInteractiveWidget));
293 if (cNewName != NULL && *cNewName != '\0')
294 {
295 gchar *cURI = g_strdup_printf ("%s/%s", myConfig.cDirPath, cNewName);
296 gboolean bSuccess = cairo_dock_fm_create_file (cURI, bFolder);
297 if (! bSuccess)
298 {
299 cd_warning ("couldn't create this file.\nCheck that you have writing rights, and that the new name does not already exist.");
300 gldi_dialog_show_temporary_with_icon_printf (D_("Warning: could not create %s.\nCheck file permissions \nand that the new name does not already exist."), myIcon, myContainer, 5000, NULL, cNewName);
301 }
302 }
303 }
304 }
305 static inline void _create_new_file (GldiModuleInstance *myApplet, gboolean bFolder)
306 {
307 gpointer *data = g_new (gpointer, 2);
308 data[0] = GINT_TO_POINTER (bFolder);
309 data[1] = myApplet;
310 gldi_dialog_show_with_entry (D_("Enter a file name:"),
311 myIcon, myContainer,
312 "same icon",
313 NULL,
314 (CairoDockActionOnAnswerFunc)_on_answer_create_file, data, (GFreeFunc)g_free);
315 }
316 static void _cd_folders_new_file (GtkMenuItem *pMenuItem, GldiModuleInstance *myApplet)
317 {
318 _create_new_file (myApplet, FALSE);
319 }
320 static void _cd_folders_new_folder (GtkMenuItem *pMenuItem, GldiModuleInstance *myApplet)
321 {
322 _create_new_file (myApplet, TRUE);
323 }
324
325 static void _cd_folders_open_folder (GtkMenuItem *pMenuItem, GldiModuleInstance *myApplet)
326 {
327 cairo_dock_fm_launch_uri (myConfig.cDirPath);
328 }
329
330 static void _cd_folders_launch_with (GtkMenuItem *pMenuItem, gpointer *app)
331 {
332 Icon *icon = app[0];
333 gchar *cExec = app[3];
334 gchar *cUri = NULL;
335 if (icon->cBaseURI && *icon->cBaseURI == '/')
336 cUri = g_filename_from_uri (icon->cBaseURI, NULL, NULL);
337 cairo_dock_launch_command_printf ("%s \"%s\"", NULL, cExec, cUri?cUri:icon->cBaseURI); // in case the program doesn't handle URI (geeqie, etc).
338 g_free (cUri);
339 }
340
341 static void _cd_folders_sort_by_name (GtkMenuItem *pMenuItem, GldiModuleInstance *myApplet)
342 {
343 cd_folders_sort_icons (myApplet, CAIRO_DOCK_FM_SORT_BY_NAME);
344 }
345
346 static void _cd_folders_sort_by_date (GtkMenuItem *pMenuItem, GldiModuleInstance *myApplet)
347 {
348 cd_folders_sort_icons (myApplet, CAIRO_DOCK_FM_SORT_BY_DATE);
349 }
350
351 static void _cd_folders_sort_by_size (GtkMenuItem *pMenuItem, GldiModuleInstance *myApplet)
352 {
353 cd_folders_sort_icons (myApplet, CAIRO_DOCK_FM_SORT_BY_SIZE);
354 }
355
356 static void _cd_folders_sort_by_type (GtkMenuItem *pMenuItem, GldiModuleInstance *myApplet)
357 {
358 cd_folders_sort_icons (myApplet, CAIRO_DOCK_FM_SORT_BY_TYPE);
359 }
360
361 static void _free_app (gpointer *app)
362 {
363 g_free (app[3]);
364 g_free (app);
365 }
366 void cd_folders_free_apps_list (GldiModuleInstance *myApplet)
367 {
368 if (myData.pAppList != NULL)
369 {
370 g_list_foreach (myData.pAppList, (GFunc) _free_app, NULL);
371 g_list_free (myData.pAppList);
372 myData.pAppList = NULL;
373 }
374 }
375
376 CD_APPLET_ON_BUILD_MENU_BEGIN
377 static gpointer *data = NULL;
378 //g_print ("%x;%x;%x\n", icon, pContainer, menu);
379 if (data == NULL)
380 data = g_new0 (gpointer, 4);
381 data[0] = CD_APPLET_CLICKED_ICON;
382 data[1] = CD_APPLET_CLICKED_CONTAINER;
383 data[2] = myApplet;
384
385 if (CD_APPLET_CLICKED_ICON == myIcon || CD_APPLET_CLICKED_ICON == NULL) // click on main icon or on the container.
386 {
387 if (myConfig.bShowFiles)
388 {
389 gchar *cLabel = g_strdup_printf ("%s (%s)", D_("Open the folder"), D_("middle-click"));
390 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (cLabel, GLDI_ICON_NAME_OPEN, _cd_folders_open_folder, CD_APPLET_MY_MENU, myApplet);
391 g_free (cLabel);
392 }
393 }
394 else // clic on a file.
395 {
396 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Rename this file"), GLDI_ICON_NAME_SAVE_AS, _cd_folders_rename_file, CD_APPLET_MY_MENU, data);
397 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Delete this file"), GLDI_ICON_NAME_REMOVE, _cd_folders_delete_file, CD_APPLET_MY_MENU, data);
398 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Move this file"), GLDI_ICON_NAME_JUMP_TO, _cd_folders_move_file, CD_APPLET_MY_MENU, data);
399
400 GList *pApps = cairo_dock_fm_list_apps_for_file (CD_APPLET_CLICKED_ICON->cBaseURI);
401 if (pApps != NULL)
402 {
403 CD_APPLET_ADD_SEPARATOR_IN_MENU (CD_APPLET_MY_MENU);
404 GtkWidget *pSubMenu = CD_APPLET_ADD_SUB_MENU_WITH_IMAGE (D_("Open with"), CD_APPLET_MY_MENU, GLDI_ICON_NAME_OPEN);
405
406 cd_folders_free_apps_list (myApplet);
407
408 GList *a;
409 gchar **pAppInfo;
410 gchar *cIconPath;
411 gpointer *app;
412 gint iDesiredIconSize = cairo_dock_search_icon_size (GTK_ICON_SIZE_LARGE_TOOLBAR); // 24px
413 for (a = pApps; a != NULL; a = a->next)
414 {
415 pAppInfo = a->data;
416
417 app = g_new0 (gpointer, 4);
418 app[0] = CD_APPLET_CLICKED_ICON;
419 app[1] = CD_APPLET_CLICKED_CONTAINER;
420 app[2] = myApplet;
421 app[3] = g_strdup (pAppInfo[1]);
422 //g_print (" + %s (%s ; %s)\n", pAppInfo[0], pAppInfo[1], pAppInfo[2]);
423 myData.pAppList = g_list_prepend (myData.pAppList, app);
424
425 if (pAppInfo[2] != NULL)
426 cIconPath = cairo_dock_search_icon_s_path (pAppInfo[2], iDesiredIconSize);
427 else
428 cIconPath = NULL;
429 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (pAppInfo[0], cIconPath, _cd_folders_launch_with, pSubMenu, app);
430 g_free (cIconPath);
431 g_strfreev (pAppInfo);
432 }
433 g_list_free (pApps);
434 }
435
436 CD_APPLET_ADD_SEPARATOR_IN_MENU (CD_APPLET_MY_MENU);
437 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Properties"), GLDI_ICON_NAME_PROPERTIES, _cd_folders_show_file_properties, CD_APPLET_MY_MENU, data);
438
439 CD_APPLET_ADD_SEPARATOR_IN_MENU (CD_APPLET_MY_MENU);
440 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Create a new file"), GLDI_ICON_NAME_NEW, _cd_folders_new_file, CD_APPLET_MY_MENU, myApplet);
441 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Create a new folder"), GLDI_ICON_NAME_NEW, _cd_folders_new_folder, CD_APPLET_MY_MENU, myApplet);
442 }
443
444 if (myConfig.bShowFiles)
445 {
446 GtkWidget *pSubMenu = CD_APPLET_ADD_SUB_MENU_WITH_IMAGE (D_("Sort by"), CD_APPLET_MY_MENU, GLDI_ICON_NAME_SORT_DESCENDING);
447 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_DATA (D_("By name"), _cd_folders_sort_by_name, pSubMenu, myApplet);
448 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_DATA (D_("By date"), _cd_folders_sort_by_date, pSubMenu, myApplet);
449 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_DATA (D_("By size"), _cd_folders_sort_by_size, pSubMenu, myApplet);
450 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_DATA (D_("By type"), _cd_folders_sort_by_type, pSubMenu, myApplet);
451 }
452
453 if (CD_APPLET_CLICKED_ICON != NULL && CD_APPLET_CLICKED_ICON != myIcon)
454 CD_APPLET_LEAVE (GLDI_NOTIFICATION_INTERCEPT);
455 CD_APPLET_ON_BUILD_MENU_END
456
457
458 typedef struct {
459 gchar *cReceivedData;
460 double fOrder;
461 gchar *cDockName;
462 } CDDropData;
463
464 static void _on_answer_import (int iClickedButton, GtkWidget *pInteractiveWidget, CDDropData *data, CairoDialog *pDialog)
465 {
466 cd_debug ("%s (%d)", __func__, iClickedButton);
467 const gchar *cReceivedData = data->cReceivedData;
468 double fOrder = data->fOrder;
469 gboolean bImportFiles = (iClickedButton == 0 || iClickedButton == -1); // ok or Enter.
470
471 // add a new conf file for the "Folders" module, with proper values.
472 GldiModule *pModule = gldi_module_get ("Folders");
473 g_return_if_fail (pModule != NULL);
474
475 gchar *cConfFilePath = gldi_module_add_conf_file (pModule); // we want to update the conf file before we instanciate the applet, so don't use high-level functions.
476 cairo_dock_update_conf_file (cConfFilePath,
477 G_TYPE_STRING, "Configuration", "dir path", cReceivedData,
478 G_TYPE_BOOLEAN, "Configuration", "show files", bImportFiles,
479 G_TYPE_DOUBLE, "Icon", "order", fOrder,
480 G_TYPE_STRING, "Icon", "dock name", data->cDockName,
481 G_TYPE_INVALID);
482
483 // instanciate the module from this conf file.
484 GldiModuleInstance *pNewInstance = gldi_module_instance_new (pModule, cConfFilePath); // prend le 'cConfFilePath'.
485
486 // show a success message on the new icon.
487 if (pNewInstance != NULL)
488 gldi_dialog_show_temporary_with_icon (D_("The folder has been imported."),
489 pNewInstance->pIcon, pNewInstance->pContainer,
490 5000,
491 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE); // not "same icon" because the icon may not be loaded yet (eg. stack or emblem icon).
492 }
493 static void _free_dialog_data (CDDropData *data)
494 {
495 g_free (data->cReceivedData);
496 g_free (data->cDockName);
497 g_free (data);
498 }
499 gboolean cd_folders_on_drop_data (gpointer data, const gchar *cReceivedData, Icon *icon, double fOrder, GldiContainer *pContainer)
500 {
501 //g_print ("Folders received '%s'\n", cReceivedData);
502 if (icon != NULL || fOrder == CAIRO_DOCK_LAST_ORDER) // drop on an icon or outside of icons.
503 return GLDI_NOTIFICATION_LET_PASS;
504
505 gchar *cPath = NULL;
506 if (strncmp (cReceivedData, "file://", 7) == 0)
507 cPath = g_filename_from_uri (cReceivedData, NULL, NULL);
508 else
509 cPath = g_strdup (cReceivedData);
510
511 if (g_file_test (cPath, G_FILE_TEST_IS_DIR)) // it's a folder, let's add a new instance of the applet that will handle it.
512 {
513 // search the closest icon to the drop point (we want to place the dialog on it).
514 GList *pIconsList = NULL, *ic;
515 if (CAIRO_DOCK_IS_DOCK (pContainer))
516 pIconsList = CAIRO_DOCK (pContainer)->icons;
517 else if (CAIRO_DOCK_IS_DESKLET (pContainer))
518 pIconsList = CAIRO_DESKLET (pContainer)->icons;
519 Icon *pIcon = NULL;
520 for (ic = pIconsList; ic != NULL; ic = ic->next)
521 {
522 icon = ic->data;
523 if (icon->fOrder > fOrder)
524 {
525 pIcon = icon;
526 break;
527 }
528 }
529 if (pIcon == NULL)
530 {
531 if (CAIRO_DOCK_IS_DOCK (pContainer))
532 pIcon = gldi_icons_get_without_dialog (CAIRO_DOCK (pContainer)->icons);
533 else
534 pIcon = gldi_icons_get_any_without_dialog ();
535 }
536
537 // ask the user whether (s)he wants to import the folder's content.
538 CDDropData *data = g_new0 (CDDropData, 1);
539 data->cReceivedData = g_strdup (cReceivedData);
540 data->fOrder = fOrder;
541 if (CAIRO_DOCK_IS_DOCK (pContainer))
542 data->cDockName = g_strdup (gldi_dock_get_name (CAIRO_DOCK (pContainer)));
543 gldi_dialog_show (D_("Do you want to import the content of the folder too?"),
544 pIcon, pContainer,
545 0,
546 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE,
547 NULL,
548 (CairoDockActionOnAnswerFunc) _on_answer_import,
549 data,
550 (GFreeFunc)_free_dialog_data);
551
552 return GLDI_NOTIFICATION_INTERCEPT;
553 }
554 return GLDI_NOTIFICATION_LET_PASS;
555 }