Debian Codegraph @

shortcuts/src/applet-drives.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 <math.h>
22
23 #include <cairo-dock.h>
24
25 #include "applet-struct.h"
26 #include "applet-bookmarks.h"
27 #include "applet-disk-usage.h"
28 #include "applet-load-icons.h"
29 #include "applet-drives.h"
30
31 #define CD_SHORTCUT_DEFAULT_DRIVE_ICON_FILENAME "drive-harddisk"
32
33
34 void cd_shortcuts_add_progress_bar (Icon *pIcon, GldiModuleInstance *myApplet)
35 {
36 // set a progress bar to display the disk space
37 CairoProgressBarAttribute attr;
38 memset (&attr, 0, sizeof (CairoProgressBarAttribute));
39 if (myConfig.iDisplayType == CD_SHOW_USED_SPACE || myConfig.iDisplayType == CD_SHOW_USED_SPACE_PERCENT)
40 {
41 attr.bInverted = TRUE;
42 }
43 CairoDataRendererAttribute *pRenderAttr = CAIRO_DATA_RENDERER_ATTRIBUTE (&attr);
44 pRenderAttr->cModelName = "progressbar";
45 cairo_dock_add_new_data_renderer_on_icon (pIcon, pIcon->pContainer, pRenderAttr);
46 }
47
48
49 void _init_disk_usage (Icon *pIcon, GldiModuleInstance *myApplet)
50 {
51 // ensure the applet has a valid icon, in case the VFS didn't give us one.
52 if (pIcon->cFileName == NULL)
53 pIcon->cFileName = cairo_dock_search_icon_s_path (CD_SHORTCUT_DEFAULT_DRIVE_ICON_FILENAME, CAIRO_DOCK_DEFAULT_ICON_SIZE);
54
55 if (pIcon->cCommand)
56 {
57 // set our private data for disk usage
58 CDDiskUsage *pDiskUsage = g_new0 (CDDiskUsage, 1);
59 pDiskUsage->iPrevAvail = -1; // data not yet retrieved (0 bytes is a valid value for non writable disks).
60 CD_APPLET_SET_MY_ICON_DATA (pIcon, pDiskUsage);
61
62 // get the current disk usage
63 cd_shortcuts_get_fs_stat (pIcon->cCommand, pDiskUsage);
64 }
65 }
66
67 static void _manage_event_on_drive (CairoDockFMEventType iEventType, const gchar *cBaseURI, GList *pIconsList, GldiContainer *pContainer, GldiModuleInstance *myApplet)
68 {
69 gchar *cURI = (g_strdup (cBaseURI));
70 cairo_dock_remove_html_spaces (cURI);
71 cd_debug (" * event %d on '%s'", iEventType, cURI);
72
73 switch (iEventType)
74 {
75 case CAIRO_DOCK_FILE_DELETED : // a mount point has been disconnected
76 {
77 Icon *pConcernedIcon = cairo_dock_get_icon_with_base_uri (pIconsList, cURI);
78 if (pConcernedIcon == NULL) // search by name
79 {
80 pConcernedIcon = cairo_dock_get_icon_with_name (pIconsList, cURI);
81 }
82 if (pConcernedIcon == NULL)
83 {
84 cd_warning (" an unknown mount point was removed");
85 return ;
86 }
87 //g_print (" %s will be removed\n", pConcernedIcon->cName);
88
89 CD_APPLET_REMOVE_ICON_FROM_MY_ICONS_LIST (pConcernedIcon);
90 g_free (myData.cLastDeletedUri);
91 myData.cLastDeletedUri = g_strdup (cURI);
92 }
93 break ;
94
95 case CAIRO_DOCK_FILE_CREATED : // a mount point has been connected
96 {
97 //\_______________________does it already exist?
98 Icon *pSameIcon = cairo_dock_get_icon_with_base_uri (pIconsList, cURI);
99 if (pSameIcon != NULL)
100 {
101 cd_warning ("this mount point (%s) already exists.", pSameIcon->cName);
102 return; // do nothing, certainly an useless/double signal
103 }
104
105 //\_______________________ create a new icon
106 Icon *pNewIcon = cairo_dock_fm_create_icon_from_URI (cURI, pContainer, CAIRO_DOCK_FM_SORT_BY_NAME);
107 if (pNewIcon == NULL)
108 {
109 cd_warning ("couldn't create an icon for this mount point");
110 return ;
111 }
112 pNewIcon->iGroup = CD_DRIVE_GROUP;
113
114 //\_______________________ place it at the right position (by name)
115 cd_shortcuts_set_icon_order_by_name (pNewIcon, pIconsList);
116 cd_debug (" new drive : %s, %s", pNewIcon->cName, pNewIcon->cCommand);
117
118 //\_______________________ added in the list
119 CD_APPLET_ADD_ICON_IN_MY_ICONS_LIST (pNewIcon);
120 _init_disk_usage (pNewIcon, myApplet);
121 if (pNewIcon->cCommand)
122 {
123 cd_shortcuts_add_progress_bar (pNewIcon, myApplet);
124 cd_shortcuts_display_disk_usage (pNewIcon, myApplet);
125 }
126
127 //\_______________________ display a notification
128 gboolean bIsMounted = FALSE;
129 gchar *cUri = cairo_dock_fm_is_mounted (pNewIcon->cBaseURI, &bIsMounted);
130 g_free (cUri);
131 gldi_dialog_show_temporary_with_icon_printf (
132 bIsMounted ? D_("%s is now mounted") : D_("%s has been connected"),
133 pNewIcon, pContainer,
134 4000,
135 NULL, // it's icon is not already loaded
136 pNewIcon->cName);
137 g_free (myData.cLastCreatedUri);
138 myData.cLastCreatedUri = g_strdup (cURI);
139 }
140 break ;
141
142 case CAIRO_DOCK_FILE_MODIFIED : // a mount point has been (un)mounted
143 {
144 //\_______________________ search the right icon
145 Icon *pConcernedIcon = cairo_dock_get_icon_with_base_uri (pIconsList, cURI);
146 if (pConcernedIcon == NULL) // search by using the name
147 {
148 pConcernedIcon = cairo_dock_get_icon_with_name (pIconsList, cURI);
149 }
150 if (pConcernedIcon == NULL)
151 {
152 cd_warning (" an unknown mount point was modified");
153 return ;
154 }
155 cd_debug (" %s is modified (%s)", pConcernedIcon->cName, pConcernedIcon->cCommand);
156
157 //\_______________________ grab current info
158 Icon *pNewIcon = cairo_dock_fm_create_icon_from_URI (cURI, pContainer, CAIRO_DOCK_FM_SORT_BY_NAME);
159 if (pNewIcon == NULL)
160 {
161 cd_warning ("couldn't create an icon for this mount point");
162 return ;
163 }
164 pNewIcon->iGroup = CD_DRIVE_GROUP;
165
166 //\_______________________ replace the icon if smthg has changed
167 if (cairo_dock_strings_differ (pConcernedIcon->cName, pNewIcon->cName)
168 || cairo_dock_strings_differ (pConcernedIcon->cFileName, pNewIcon->cFileName))
169 {
170 //g_print (" '%s' -> '%s'\n'%s' -> '%s'\n", pConcernedIcon->cName, pNewIcon->cName, pConcernedIcon->cFileName, pNewIcon->cFileName);
171
172 CD_APPLET_REMOVE_ICON_FROM_MY_ICONS_LIST (pConcernedIcon);
173 pIconsList = CD_APPLET_MY_ICONS_LIST;
174
175 cd_shortcuts_set_icon_order_by_name (pNewIcon, pIconsList);
176 CD_APPLET_ADD_ICON_IN_MY_ICONS_LIST (pNewIcon);
177 _init_disk_usage (pNewIcon, myApplet);
178 if (pNewIcon->cCommand)
179 {
180 cd_shortcuts_add_progress_bar (pNewIcon, myApplet);
181 }
182
183 // pConcernedIcon has been distroyed, assigned the new one to display a dialogue just after
184 pConcernedIcon = pNewIcon;
185 }
186 else
187 {
188 gldi_object_unref (GLDI_OBJECT (pNewIcon));
189 pNewIcon = NULL;
190 }
191
192 cd_shortcuts_display_disk_usage (pConcernedIcon, myApplet);
193
194 //\_______________________ display a notification
195 gldi_dialogs_remove_on_icon (pConcernedIcon); // avoid multiple dialogues
196 gboolean bIsMounted = FALSE;
197 gchar *cUri = cairo_dock_fm_is_mounted (pConcernedIcon->cBaseURI, &bIsMounted);
198 g_free (cUri);
199 gldi_dialog_show_temporary_with_icon_printf (
200 bIsMounted ? D_("%s is now mounted") : D_("%s is now unmounted"),
201 pConcernedIcon, pContainer,
202 4000,
203 "same icon", // it's possible to not have the right icon, not so important
204 pConcernedIcon->cName);
205 // disk has been unmounted but a icon is needed
206 if (! bIsMounted && pNewIcon == NULL)
207 {
208 CDDiskUsage *pDiskUsage = CD_APPLET_GET_MY_ICON_DATA (pConcernedIcon);
209 if (pDiskUsage != NULL)
210 {
211 if (pDiskUsage->iTotal != 0)
212 {
213 pDiskUsage->iTotal = 0;
214 pDiskUsage->iAvail = 0;
215 // remove the quick-info because it has been unmounted
216 gldi_icon_set_quick_info (pConcernedIcon, NULL);
217 }
218 }
219 }
220 }
221 break ;
222 case CAIRO_DOCK_NB_EVENT_ON_FILES :
223 break ;
224 }
225 g_free (cURI);
226 }
227
228 void cd_shortcuts_on_drive_event (CairoDockFMEventType iEventType, const gchar *cURI, GldiModuleInstance *myApplet)
229 {
230 g_return_if_fail (cURI != NULL);
231 CD_APPLET_ENTER;
232 //\________________ Manage event about this mount point
233 GList *pIconsList = CD_APPLET_MY_ICONS_LIST;
234 GldiContainer *pContainer = CD_APPLET_MY_ICONS_LIST_CONTAINER;
235 CD_APPLET_LEAVE_IF_FAIL (pContainer != NULL);
236
237 _manage_event_on_drive (iEventType, cURI, pIconsList, pContainer, myApplet);
238
239 //\________________ Update bookmarks which are linked to this mount point
240 if (!myConfig.bListBookmarks || pIconsList == NULL)
241 {
242 CD_APPLET_LEAVE();
243 }
244 GList *ic;
245 Icon *icon;
246 gboolean bIsMounted;
247 gchar *cTargetURI = cairo_dock_fm_is_mounted (cURI, &bIsMounted);
248 if (cTargetURI != NULL) // optimized version.
249 {
250 //g_print ("test bookmarks in '%s'...\n", cTargetURI);
251 pIconsList = CD_APPLET_MY_ICONS_LIST;
252 for (ic = pIconsList; ic != NULL; ic = ic->next)
253 {
254 icon = ic->data;
255 if (icon->iGroup == (CairoDockIconGroup) CD_BOOKMARK_GROUP)
256 {
257 if (strncmp (cTargetURI, icon->cBaseURI, strlen (cTargetURI)) == 0)
258 {
259 //g_print ("bookmark '%s' is located in a mount point which has changed (%s)\n", icon->cBaseURI, cTargetURI);
260 gchar *cName = NULL, *cRealURI = NULL, *cIconName = NULL;
261 int iVolumeID = 0;
262 gboolean bIsDirectory = FALSE;
263 double fOrder;
264 if (cairo_dock_fm_get_file_info (icon->cBaseURI, &cName,
265 &cRealURI, &cIconName, &bIsDirectory, &iVolumeID,
266 &fOrder, CAIRO_DOCK_FM_SORT_BY_NAME))
267 {
268 //g_print (" -> %s (%d)\n", cIconName, bIsMounted);
269 if (bIsMounted/** || cIconName == NULL*/)
270 {
271 gchar *str;
272 /* if it was previously an unmounted bookmark, just
273 * remove the 'unmounted' part to avoid changing the
274 * name (when mounted, gvfs returns the path whereas
275 * we want to display the bookmark name). Note that
276 * the icon might also changes (it was NULL when the
277 * bookmark was not mounted), and we can use this
278 * new one which is probably more accurate.
279 */
280 if ((str = strchr (icon->cName, '\n')) != NULL)
281 *str = '\0';
282 else
283 {
284 g_free (icon->cName);
285 icon->cName = cName;
286 }
287 }
288 else
289 {
290 g_free (icon->cName);
291 icon->cName = g_strdup_printf ("%s\n[%s]", cName, D_("Unmounted"));
292 g_free (cName);
293 }
294 g_free (icon->cCommand);
295 icon->cCommand = cRealURI;
296 g_free (icon->cFileName);
297 icon->cFileName = cIconName;
298 icon->iVolumeID = iVolumeID;
299 cairo_dock_load_icon_buffers (icon, pContainer);
300 }
301 }
302 }
303 }
304 g_free (cTargetURI);
305 }
306 CD_APPLET_LEAVE();
307 }
308
309
310 GList * cd_shortcuts_list_drives (CDSharedMemory *pSharedMemory)
311 {
312 GList *pIconList = NULL;
313 gchar *cFullURI = NULL;
314
315 //\_______________________ Get the list of mount points.
316 pIconList = cairo_dock_fm_list_directory (CAIRO_DOCK_FM_VFS_ROOT,
317 CAIRO_DOCK_FM_SORT_BY_NAME, CD_DRIVE_GROUP, FALSE, 100, &cFullURI);
318 cd_message (" cFullURI : %s", cFullURI);
319 if (pIconList == NULL)
320 {
321 cd_warning ("couldn't detect any drives"); // continue: for bookmarks
322 }
323 /// TODO: if ! bListBookmarks, then we should add the Home in the drives list, to have the disk space information...
324
325 pSharedMemory->cDisksURI = cFullURI;
326
327 //\_______________________ Initialize disk usages.
328 Icon *pIcon;
329 GList *ic;
330 for (ic = pIconList; ic != NULL; ic = ic->next)
331 {
332 pIcon = ic->data;
333 _init_disk_usage (pIcon, pSharedMemory->pApplet);
334 }
335
336 return pIconList;
337 }