Debian Codegraph @

shortcuts/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 <glib/gi18n.h>
23
24 #include "applet-bookmarks.h"
25 #include "applet-struct.h"
26 #include "applet-disk-usage.h"
27 #include "applet-notifications.h"
28
29 static void _on_volume_mounted (gboolean bMounting, gboolean bSuccess, const gchar *cName, const gchar *cURI, GldiModuleInstance *myApplet)
30 {
31 CD_APPLET_ENTER;
32 GldiContainer *pContainer = CD_APPLET_MY_ICONS_LIST_CONTAINER;
33 CD_APPLET_LEAVE_IF_FAIL (pContainer != NULL);
34
35 //g_print ("%s (%s , %d)\n", __func__, cURI, bSuccess);
36 // success: a dialog as notification
37 if (! bSuccess)
38 {
39 Icon *pIcon = cairo_dock_get_icon_with_base_uri (CD_APPLET_MY_ICONS_LIST, cURI);
40 CD_APPLET_LEAVE_IF_FAIL (pIcon != NULL);
41
42 gldi_dialogs_remove_on_icon (pIcon);
43 gldi_dialog_show_temporary_with_icon_printf (
44 bMounting ? D_("Failed to mount %s") : D_("Failed to unmount %s"),
45 pIcon, pContainer,
46 4000,
47 "same icon", // it's possible to not have the right icon, not so important
48 pIcon->cName);
49 }
50 CD_APPLET_LEAVE ();
51 }
52
53 static void _open_on_mount (gboolean bMounting, gboolean bSuccess, const gchar *cName, const gchar *cURI, GldiModuleInstance *myApplet)
54 {
55 CD_APPLET_ENTER;
56 GldiContainer *pContainer = CD_APPLET_MY_ICONS_LIST_CONTAINER;
57 CD_APPLET_LEAVE_IF_FAIL (pContainer != NULL);
58
59 Icon *pIcon = cairo_dock_get_icon_with_base_uri (CD_APPLET_MY_ICONS_LIST, cURI);
60 /* when mouting a mount point (for instance a file mounted as a loop device),
61 * the associated .volume (its actual URI) might be deleted, and a new one is
62 * created for the mounted volume (therefore the icon is deleted and a new
63 * one is created, with another URI). So we lose the track of the initial
64 * icon. That's why we use a trick: we remember the last created volume;
65 * it's very likely this one.
66 */
67 if (pIcon == NULL && g_strcmp0 (myData.cLastDeletedUri, cURI) == 0 && myData.cLastCreatedUri != NULL)
68 {
69 cd_debug ("no icon for '%s', trying with '%s'", cURI, myData.cLastCreatedUri);
70 pIcon = cairo_dock_get_icon_with_base_uri (CD_APPLET_MY_ICONS_LIST, myData.cLastCreatedUri);
71 }
72 CD_APPLET_LEAVE_IF_FAIL (pIcon != NULL);
73
74 if (bSuccess)
75 {
76 cairo_dock_fm_launch_uri (pIcon->cCommand);
77 }
78 else
79 {
80 gldi_dialogs_remove_on_icon (pIcon);
81 gldi_dialog_show_temporary_with_icon_printf (
82 bMounting ? D_("Failed to mount %s") : D_("Failed to unmount %s"),
83 pIcon, pContainer,
84 4000,
85 "same icon", // it's possible to not have the right icon, not so important
86 pIcon->cName);
87 }
88 CD_APPLET_LEAVE ();
89 }
90
91 CD_APPLET_ON_CLICK_BEGIN
92 // Click on the main icon: display a message if there is a problem
93 if (CD_APPLET_CLICKED_ICON == myIcon)
94 {
95 if (CD_APPLET_MY_ICONS_LIST == NULL)
96 {
97 gldi_dialogs_remove_on_icon (myIcon);
98 if (myData.pTask != NULL) // if it's loading
99 myData.bShowMenuPending = TRUE;
100 else if (g_iDesktopEnv == CAIRO_DOCK_KDE)
101 gldi_dialog_show_temporary_with_icon (D_("Sorry, this applet is not yet available for KDE."),
102 myIcon, myContainer, 6000., "same icon");
103 else
104 gldi_dialog_show_temporary_with_icon (D_("No disks or bookmarks were found."),
105 myIcon, myContainer, 6000., "same icon");
106 }
107 // Let pass the notification: to open the subdock when clicking
108 CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS);
109 }
110 else if (CD_APPLET_CLICKED_ICON != NULL) // click on one icon of the subdock
111 {
112 // click on a mount point
113 if (CD_APPLET_CLICKED_ICON->iGroup == (CairoDockIconGroup) CD_DRIVE_GROUP
114 && CD_APPLET_CLICKED_ICON->iVolumeID)
115 {
116 gboolean bIsMounted = FALSE;
117 gchar *cActivationURI = cairo_dock_fm_is_mounted (CD_APPLET_CLICKED_ICON->cBaseURI, &bIsMounted);
118 g_free (cActivationURI);
119 if (bIsMounted)
120 {
121 cairo_dock_fm_launch_uri (CD_APPLET_CLICKED_ICON->cCommand);
122 }
123 else
124 {
125 cairo_dock_fm_mount_full (CD_APPLET_CLICKED_ICON->cBaseURI,
126 CD_APPLET_CLICKED_ICON->iVolumeID,
127 (CairoDockFMMountCallback) _open_on_mount, myApplet);
128 }
129 }
130 // click on a bookmark (not with a custom cmd): can be on an unmount devise
131 else if (CD_APPLET_CLICKED_ICON->iGroup == (CairoDockIconGroup) CD_BOOKMARK_GROUP)
132 {
133 // Click on a bookmark which contains a custom cmd, e.g.: "nautilus x-nautilus-(...)
134 if (CD_APPLET_CLICKED_ICON->iVolumeID == CD_VOLUME_ID_BOOKMARK_CMD)
135 cairo_dock_launch_command (CD_APPLET_CLICKED_ICON->cCommand);
136 else
137 {
138 /* check if it's a mounted URI
139 * note: if it's a folder on an unmouned volume, it can't be
140 * launched nor mounted; we would need to get its volume first
141 * and it's not obvious
142 * (Nautilus just hide them until the volume is mounted)
143 */
144 gboolean bIsMounted = TRUE;
145 gchar *cTarget = cairo_dock_fm_is_mounted (CD_APPLET_CLICKED_ICON->cCommand, &bIsMounted);
146 cd_debug ("%s is mounted: %d (%s)", CD_APPLET_CLICKED_ICON->cCommand, bIsMounted, cTarget);
147 g_free (cTarget);
148
149 if (bIsMounted) // if mounted, just open it
150 cairo_dock_fm_launch_uri (CD_APPLET_CLICKED_ICON->cCommand);
151 else // else, mount it, and it will be opened in the callback.
152 cairo_dock_fm_mount_full (CD_APPLET_CLICKED_ICON->cCommand,
153 1, (CairoDockFMMountCallback) _open_on_mount, myApplet);
154 }
155 }
156 else // Network
157 cairo_dock_fm_launch_uri (CD_APPLET_CLICKED_ICON->cCommand);
158 }
159 CD_APPLET_ON_CLICK_END
160
161
162
163 static void _mount_unmount (Icon *pIcon, GldiContainer *pContainer, GldiModuleInstance *myApplet)
164 {
165 gboolean bIsMounted = FALSE;
166 gchar *cActivationURI = cairo_dock_fm_is_mounted (pIcon->cBaseURI, &bIsMounted);
167 g_free (cActivationURI);
168
169 if (! bIsMounted)
170 {
171 cairo_dock_fm_mount_full (pIcon->cBaseURI, pIcon->iVolumeID,
172 (CairoDockFMMountCallback) _on_volume_mounted, myApplet);
173 }
174 else
175 {
176 cairo_dock_fm_unmount_full (pIcon->cBaseURI, pIcon->iVolumeID,
177 (CairoDockFMMountCallback) _on_volume_mounted, myApplet);
178
179 // this dialog will be removed when the volume will be unmounted.
180 gldi_dialog_show_temporary_with_icon (D_("Unmouting this volume ..."),
181 pIcon, pContainer, 15000., "same icon");
182 }
183 }
184
185 CD_APPLET_ON_MIDDLE_CLICK_BEGIN
186 if (CD_APPLET_CLICKED_ICON == myIcon) // click on the main icon.
187 {
188 cairo_dock_fm_launch_uri (g_getenv ("HOME"));
189 }
190 // click on an icon of the subdock or desklet and it's a mount point.
191 else if (CD_APPLET_CLICKED_ICON != NULL
192 && (CD_APPLET_CLICKED_ICON->iGroup == (CairoDockIconGroup) CD_DRIVE_GROUP
193 || CD_APPLET_CLICKED_ICON->iVolumeID > 0))
194 {
195 _mount_unmount (CD_APPLET_CLICKED_ICON, CD_APPLET_CLICKED_CONTAINER, myApplet);
196 }
197 CD_APPLET_ON_MIDDLE_CLICK_END
198
199
200 static void _cd_shortcuts_remove_bookmark (GtkMenuItem *menu_item, gpointer *data)
201 {
202 GldiModuleInstance *myApplet = data[0];
203 Icon *pIcon = data[1];
204 cd_shortcuts_remove_one_bookmark (pIcon->cBaseURI, myApplet);
205 }
206
207 static void _on_got_bookmark_name (int iClickedButton, GtkWidget *pInteractiveWidget, gpointer *data, CairoDialog *pDialog)
208 {
209 GldiModuleInstance *myApplet = data[0];
210 Icon *pIcon = data[1];
211 CD_APPLET_ENTER;
212
213 if (iClickedButton == 0 || iClickedButton == -1) // ok button or Enter.
214 {
215 const gchar *cNewName = gtk_entry_get_text (GTK_ENTRY (pInteractiveWidget));
216 if (cNewName != NULL)
217 {
218 cd_shortcuts_rename_one_bookmark (pIcon->cCommand, cNewName, myApplet);
219 }
220 }
221 CD_APPLET_LEAVE ();
222 }
223 static void _cd_shortcuts_rename_bookmark (GtkMenuItem *menu_item, gpointer *data)
224 {
225 GldiModuleInstance *myApplet = data[0];
226 Icon *pIcon = data[1];
227 GldiContainer *pContainer = data[2];
228 CD_APPLET_ENTER;
229
230 gpointer *ddata = g_new (gpointer, 2);
231 ddata[0] = myApplet;
232 ddata[1] = pIcon;
233 // if the icon gets deleted, the dialog will disappear with it.
234 gldi_dialog_show_with_entry (D_("Enter a name for this bookmark:"),
235 pIcon, pContainer, "same icon",
236 pIcon->cName,
237 (CairoDockActionOnAnswerFunc)_on_got_bookmark_name, ddata, (GFreeFunc)g_free);
238 CD_APPLET_LEAVE ();
239 }
240 static void _cd_shortcuts_eject (GtkMenuItem *menu_item, gpointer *data)
241 {
242 GldiModuleInstance *myApplet = data[0];
243 CD_APPLET_ENTER;
244 Icon *pIcon = data[1];
245 // GldiContainer *pContainer = data[2];
246
247 cairo_dock_fm_eject_drive (pIcon->cBaseURI);
248 CD_APPLET_LEAVE ();
249 }
250 static void _cd_shortcuts_unmount (GtkMenuItem *menu_item, gpointer *data)
251 {
252 GldiModuleInstance *myApplet = data[0];
253 CD_APPLET_ENTER;
254 Icon *pIcon = data[1];
255 GldiContainer *pContainer = data[2];
256
257 _mount_unmount (pIcon, pContainer, myApplet);
258 CD_APPLET_LEAVE ();
259 }
260 static void _cd_shortcuts_show_disk_info (GtkMenuItem *menu_item, gpointer *data)
261 {
262 GldiModuleInstance *myApplet = data[0];
263 CD_APPLET_ENTER;
264 Icon *pIcon = data[1];
265 GldiContainer *pContainer = data[2];
266
267 //g_print ("pIcon->cCommand:%s\n", pIcon->cCommand);
268 gchar *cInfo = cd_shortcuts_get_disk_info (pIcon->cCommand, pIcon->cName);
269 gldi_dialog_show_temporary_with_icon (cInfo, pIcon, pContainer, 15000, "same icon");
270 g_free (cInfo);
271 CD_APPLET_LEAVE ();
272 }
273
274 static void _open_home_dir (GtkMenuItem *menu_item, gpointer data)
275 {
276 cairo_dock_fm_launch_uri (g_getenv ("HOME"));
277 }
278
279 static gboolean s_bNCSChecked = FALSE;
280 static gboolean s_bNCSAvailable = FALSE;
281
282 static void _check_ncs (void)
283 {
284 gchar *cResult = cairo_dock_launch_command_sync ("which nautilus-connect-server");
285 if (cResult != NULL && *cResult == '/')
286 s_bNCSAvailable = TRUE;
287 g_free (cResult);
288 s_bNCSChecked = TRUE;
289 }
290
291 static void _open_ncs (G_GNUC_UNUSED GtkMenuItem *menu_item, G_GNUC_UNUSED gpointer data)
292 {
293 cairo_dock_launch_command ("nautilus-connect-server");
294 }
295
296 static void _open_network (G_GNUC_UNUSED GtkMenuItem *menu_item, G_GNUC_UNUSED gpointer data)
297 {
298 cairo_dock_fm_launch_uri ("network:///");
299 }
300
301 static void _open_recent (G_GNUC_UNUSED GtkMenuItem *menu_item, G_GNUC_UNUSED gpointer data)
302 {
303 cairo_dock_fm_launch_uri ("recent:///");
304 }
305
306 static void _open_trash (G_GNUC_UNUSED GtkMenuItem *menu_item, G_GNUC_UNUSED gpointer data)
307 {
308 cairo_dock_fm_launch_uri ("trash:///");
309 }
310
311 CD_APPLET_ON_BUILD_MENU_BEGIN
312 static gpointer *data = NULL;
313 if (data == NULL)
314 data = g_new (gpointer, 3);
315 data[0] = myApplet;
316 data[1] = CD_APPLET_CLICKED_ICON;
317 data[2] = CD_APPLET_CLICKED_CONTAINER;
318
319 if (CD_APPLET_CLICKED_ICON == myIcon) // clic sur l'icone principale (mode dock donc).
320 {
321 gchar *cLabel = g_strdup_printf ("%s (%s)", D_("Open Home directory"), D_("middle-click"));
322 CD_APPLET_ADD_IN_MENU_WITH_STOCK (cLabel, GLDI_ICON_NAME_OPEN, _open_home_dir, CD_APPLET_MY_MENU);
323 g_free (cLabel);
324
325 // Connect to servers (ftp, ssh, samba, etc.)
326 if (! s_bNCSChecked)
327 _check_ncs ();
328 if (s_bNCSAvailable)
329 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Connect to Server..."),
330 GLDI_ICON_NAME_OPEN, _open_ncs, CD_APPLET_MY_MENU);
331
332 // browse network (e.g.: samba)
333 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Browse Network"), GLDI_ICON_NAME_OPEN,
334 _open_network, CD_APPLET_MY_MENU); // or GLDI_ICON_NAME_NETWORK
335 // browse recent files
336 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Browse recent files"),
337 GLDI_ICON_NAME_OPEN, _open_recent, CD_APPLET_MY_MENU); // or "folder-recent"
338 // trash
339 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Open Trash"), GLDI_ICON_NAME_OPEN,
340 _open_trash, CD_APPLET_MY_MENU); // or "user-trash"
341 }
342 else if (CD_APPLET_CLICKED_ICON != NULL) // clic sur un item.
343 {
344 // Click on a bookmark
345 if (CD_APPLET_CLICKED_ICON->iGroup == (CairoDockIconGroup) CD_BOOKMARK_GROUP)
346 {
347 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Rename this bookmark"),
348 GLDI_ICON_NAME_EDIT, _cd_shortcuts_rename_bookmark, CD_APPLET_MY_MENU, data);
349 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Remove this bookmark"),
350 GLDI_ICON_NAME_REMOVE, _cd_shortcuts_remove_bookmark,
351 CD_APPLET_MY_MENU, data);
352 CD_APPLET_LEAVE (GLDI_NOTIFICATION_INTERCEPT);
353 }
354 else if (CD_APPLET_CLICKED_ICON->iGroup == (CairoDockIconGroup) CD_DRIVE_GROUP
355 && CD_APPLET_CLICKED_ICON->cBaseURI != NULL) // click on a drive
356 {
357 if (cairo_dock_fm_can_eject (CD_APPLET_CLICKED_ICON->cBaseURI))
358 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Eject"),
359 GLDI_ICON_NAME_MEDIA_EJECT, _cd_shortcuts_eject,
360 CD_APPLET_MY_MENU, data);
361
362 gboolean bIsMounted = FALSE;
363 gchar *cURI = cairo_dock_fm_is_mounted (CD_APPLET_CLICKED_ICON->cBaseURI, &bIsMounted);
364 g_free (cURI);
365 gchar *cLabel = g_strdup_printf ("%s (%s)",
366 bIsMounted ? D_("Unmount") : D_("Mount"), D_("middle-click"));
367 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (cLabel, GLDI_ICON_NAME_MEDIA_EJECT,
368 _cd_shortcuts_unmount, CD_APPLET_MY_MENU, data);
369 g_free (cLabel);
370
371 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Get disk info"),
372 GLDI_ICON_NAME_INFO, _cd_shortcuts_show_disk_info,
373 CD_APPLET_MY_MENU, data);
374 }
375 }
376 CD_APPLET_ON_BUILD_MENU_END
377
378
379 CD_APPLET_ON_DROP_DATA_BEGIN
380 if (myDock && myIcon->pSubDock == NULL)
381 CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS);
382 cd_message (" new bookmark : %s", CD_APPLET_RECEIVED_DATA);
383 gchar *cName=NULL, *cURI=NULL, *cIconName=NULL;
384 gboolean bIsDirectory;
385 int iVolumeID = 0;
386 double fOrder;
387 if (cairo_dock_fm_get_file_info (CD_APPLET_RECEIVED_DATA,
388 &cName,
389 &cURI,
390 &cIconName,
391 &bIsDirectory,
392 &iVolumeID,
393 &fOrder,
394 0))
395 {
396 if (! iVolumeID && ! bIsDirectory)
397 {
398 cd_warning ("this can't be a bookmark");
399 gldi_dialog_show_temporary_with_icon (D_("Only folders can be bookmarked."),
400 myIcon, myContainer, 4000, "same icon");
401 }
402 else
403 {
404 cd_shortcuts_add_one_bookmark (cURI, myApplet);
405 }
406 }
407 else
408 {
409 cd_warning ("couldn't get info about '%s', we won't add it", CD_APPLET_RECEIVED_DATA);
410 }
411 g_free (cName);
412 g_free (cURI);
413 g_free (cIconName);
414 CD_APPLET_ON_DROP_DATA_END
415
416
417 gboolean cd_shortcuts_free_data (GldiModuleInstance *myApplet, Icon *pIcon)
418 {
419 CDDiskUsage *pDiskUsage = CD_APPLET_GET_MY_ICON_DATA (pIcon);
420 if (pDiskUsage == NULL)
421 return GLDI_NOTIFICATION_LET_PASS;
422
423 g_free (pDiskUsage);
424
425 CD_APPLET_SET_MY_ICON_DATA (pIcon, NULL);
426 return GLDI_NOTIFICATION_LET_PASS;
427 }