1 /* vi:set et ai sw=2 sts=2 ts=2: */
2 /*-
3  * Copyright (c) 2010 Jannis Pohlmann <jannis@xfce.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  * NOTE: THIS FILE WAS COPIED FROM THUNAR. FUNCTION PREFIXES WERE
21  * ALIGNED TO PLACES PLUGIN AND A FEW TRANSLATOR HINTS WERE ADDED.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <glib.h>
29 #include <gio/gio.h>
30 
31 #include <libnotify/notify.h>
32 
33 #include <libxfce4util/libxfce4util.h>
34 
35 #include "model_volumes_notify.h"
36 
37 
38 
39 static gboolean pbvol_notify_initted = FALSE;
40 
41 
42 
43 static gboolean
pbvol_notify_init(void)44 pbvol_notify_init (void)
45 {
46   gchar *spec_version = NULL;
47 
48   if (!pbvol_notify_initted
49       && notify_init (PACKAGE_NAME))
50     {
51       /* we do this to work around bugs in libnotify < 0.6.0. Older
52        * versions crash in notify_uninit() when no notifications are
53        * displayed before. These versions also segfault when the
54        * ret_spec_version parameter of notify_get_server_info is
55        * NULL... */
56       notify_get_server_info (NULL, NULL, NULL, &spec_version);
57       g_free (spec_version);
58 
59       pbvol_notify_initted = TRUE;
60     }
61 
62   return pbvol_notify_initted;
63 }
64 
65 
66 
67 void
pbvol_notify_unmount(GMount * mount)68 pbvol_notify_unmount (GMount *mount)
69 {
70   const gchar * const *icon_names;
71   NotifyNotification  *notification = NULL;
72   const gchar         *summary;
73   GFileInfo           *info;
74   gboolean             read_only = FALSE;
75   GFile               *icon_file;
76   GFile               *mount_point;
77   GIcon               *icon;
78   gchar               *icon_name = NULL;
79   gchar               *message;
80   gchar               *name;
81 
82   g_return_if_fail (G_IS_MOUNT (mount));
83 
84   if (!pbvol_notify_init ())
85     return;
86 
87   mount_point = g_mount_get_root (mount);
88 
89   info = g_file_query_info (mount_point, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
90                             G_FILE_QUERY_INFO_NONE, NULL, NULL);
91 
92   if (info != NULL)
93     {
94       read_only = !g_file_info_get_attribute_boolean (info,
95                                                       G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
96 
97       g_object_unref (info);
98     }
99 
100   g_object_unref (mount_point);
101 
102   name = g_mount_get_name (mount);
103 
104   icon = g_mount_get_icon (mount);
105   if (G_IS_THEMED_ICON (icon))
106     {
107       icon_names = g_themed_icon_get_names (G_THEMED_ICON (icon));
108       if (icon_names != NULL)
109         icon_name = g_strdup (icon_names[0]);
110     }
111   else if (G_IS_FILE_ICON (icon))
112     {
113       icon_file = g_file_icon_get_file (G_FILE_ICON (icon));
114       if (icon_file != NULL)
115         {
116           icon_name = g_file_get_path (icon_file);
117           g_object_unref (icon_file);
118         }
119     }
120   g_object_unref (icon);
121 
122   if (icon_name == NULL)
123     icon_name = g_strdup ("drive-removable-media");
124 
125   if (read_only)
126     {
127       /* TRANSLATORS: Please use the same translation here as in Thunar */
128       summary = _("Unmounting device");
129 
130       /* TRANSLATORS: Please use the same translation here as in Thunar */
131       message = g_strdup_printf (_("The device \"%s\" is being unmounted by the system. "
132                                    "Please do not remove the media or disconnect the "
133                                    "drive"), name);
134     }
135   else
136     {
137       /* TRANSLATORS: Please use the same translation here as in Thunar */
138       summary = _("Writing data to device");
139 
140       /* TRANSLATORS: Please use the same translation here as in Thunar */
141       message = g_strdup_printf (_("There is data that needs to be written to the "
142                                    "device \"%s\" before it can be removed. Please "
143                                    "do not remove the media or disconnect the drive"),
144                                    name);
145     }
146 
147 #ifdef NOTIFY_CHECK_VERSION
148 #if NOTIFY_CHECK_VERSION (0, 7, 0)
149   notification = notify_notification_new (summary, message, icon_name);
150 #else
151   notification = notify_notification_new (summary, message, icon_name, NULL);
152 #endif
153 #else
154   notification = notify_notification_new (summary, message, icon_name, NULL);
155 #endif
156   notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
157   notify_notification_set_timeout (notification, NOTIFY_EXPIRES_NEVER);
158   notify_notification_show (notification, NULL);
159 
160   g_object_set_data_full (G_OBJECT (mount), "pbvol-notification", notification,
161                           g_object_unref);
162 
163   g_free (message);
164   g_free (icon_name);
165   g_free (name);
166 }
167 
168 
169 
170 void
pbvol_notify_unmount_finish(GMount * mount)171 pbvol_notify_unmount_finish (GMount *mount)
172 {
173   NotifyNotification *notification;
174 
175   g_return_if_fail (G_IS_MOUNT (mount));
176 
177   notification = g_object_get_data (G_OBJECT (mount), "pbvol-notification");
178   if (notification != NULL)
179     {
180       notify_notification_close (notification, NULL);
181       g_object_set_data (G_OBJECT (mount), "pbvol-notification", NULL);
182     }
183 }
184 
185 
186 
187 void
pbvol_notify_eject(GVolume * volume)188 pbvol_notify_eject (GVolume *volume)
189 {
190   const gchar * const *icon_names;
191   NotifyNotification  *notification = NULL;
192   const gchar         *summary;
193   GFileInfo           *info;
194   gboolean             read_only = FALSE;
195   GMount              *mount;
196   GFile               *icon_file;
197   GFile               *mount_point;
198   GIcon               *icon;
199   gchar               *icon_name = NULL;
200   gchar               *message;
201   gchar               *name;
202 
203   g_return_if_fail (G_IS_VOLUME (volume));
204 
205   if (!pbvol_notify_init ())
206     return;
207 
208   mount = g_volume_get_mount (volume);
209   if (mount != NULL)
210     {
211       mount_point = g_mount_get_root (mount);
212 
213       info = g_file_query_info (mount_point, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
214                                 G_FILE_QUERY_INFO_NONE, NULL, NULL);
215 
216       if (info != NULL)
217         {
218           read_only =
219             !g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
220 
221           g_object_unref (info);
222         }
223 
224       g_object_unref (mount_point);
225     }
226 
227   name = g_volume_get_name (volume);
228 
229   icon = g_volume_get_icon (volume);
230   if (G_IS_THEMED_ICON (icon))
231     {
232       icon_names = g_themed_icon_get_names (G_THEMED_ICON (icon));
233       if (icon_names != NULL)
234         icon_name = g_strdup (icon_names[0]);
235     }
236   else if (G_IS_FILE_ICON (icon))
237     {
238       icon_file = g_file_icon_get_file (G_FILE_ICON (icon));
239       if (icon_file != NULL)
240         {
241           icon_name = g_file_get_path (icon_file);
242           g_object_unref (icon_file);
243         }
244     }
245   g_object_unref (icon);
246 
247   if (icon_name == NULL)
248     icon_name = g_strdup ("drive-removable-media");
249 
250   if (read_only)
251     {
252       /* TRANSLATORS: Please use the same translation here as in Thunar */
253       summary = _("Ejecting device");
254 
255       /* TRANSLATORS: Please use the same translation here as in Thunar */
256       message = g_strdup_printf (_("The device \"%s\" is being ejected. "
257                                    "This may take some time"), name);
258     }
259   else
260     {
261       /* TRANSLATORS: Please use the same translation here as in Thunar */
262       summary = _("Writing data to device");
263 
264       /* TRANSLATORS: Please use the same translation here as in Thunar */
265       message = g_strdup_printf (_("There is data that needs to be written to the "
266                                    "device \"%s\" before it can be removed. Please "
267                                    "do not remove the media or disconnect the drive"),
268                                    name);
269     }
270 
271 #ifdef NOTIFY_CHECK_VERSION
272 #if NOTIFY_CHECK_VERSION (0, 7, 0)
273   notification = notify_notification_new (summary, message, icon_name);
274 #else
275   notification = notify_notification_new (summary, message, icon_name, NULL);
276 #endif
277 #else
278   notification = notify_notification_new (summary, message, icon_name, NULL);
279 #endif
280   notify_notification_set_urgency (notification, NOTIFY_URGENCY_CRITICAL);
281   notify_notification_set_timeout (notification, NOTIFY_EXPIRES_NEVER);
282   notify_notification_show (notification, NULL);
283 
284   g_object_set_data_full (G_OBJECT (volume), "pbvol-notification", notification,
285                           g_object_unref);
286 
287   g_free (message);
288   g_free (icon_name);
289   g_free (name);
290 }
291 
292 
293 
294 void
pbvol_notify_eject_finish(GVolume * volume)295 pbvol_notify_eject_finish (GVolume *volume)
296 {
297   NotifyNotification *notification;
298 
299   g_return_if_fail (G_IS_VOLUME (volume));
300 
301   notification = g_object_get_data (G_OBJECT (volume), "pbvol-notification");
302   if (notification != NULL)
303     {
304       notify_notification_close (notification, NULL);
305       g_object_set_data (G_OBJECT (volume), "pbvol-notification", NULL);
306     }
307 }
308 
309 
310 
311 void
pbvol_notify_uninit(void)312 pbvol_notify_uninit (void)
313 {
314   if (pbvol_notify_initted
315       && notify_is_initted ())
316     notify_uninit ();
317 }
318