No issues found
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2
3 nautilus-desktop-link.c: Class that handles the links on the desktop
4
5 Copyright (C) 2003 Red Hat, Inc.
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 as
9 published by the Free Software Foundation; either version 2 of the
10 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 GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public
18 License along with this program; if not, write to the
19 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21
22 Author: Alexander Larsson <alexl@redhat.com>
23 */
24
25 #include <config.h>
26 #include "nautilus-desktop-link.h"
27 #include "nautilus-desktop-link-monitor.h"
28 #include "nautilus-desktop-icon-file.h"
29 #include "nautilus-directory-private.h"
30 #include "nautilus-desktop-directory.h"
31 #include "nautilus-icon-names.h"
32 #include <glib/gi18n.h>
33 #include <gio/gio.h>
34 #include <libnautilus-private/nautilus-file-utilities.h>
35 #include <libnautilus-private/nautilus-trash-monitor.h>
36 #include <libnautilus-private/nautilus-global-preferences.h>
37 #include <string.h>
38
39 struct NautilusDesktopLinkDetails {
40 NautilusDesktopLinkType type;
41 char *filename;
42 char *display_name;
43 GFile *activation_location;
44 GIcon *icon;
45
46 NautilusDesktopIconFile *icon_file;
47
48 GObject *signal_handler_obj;
49 gulong signal_handler;
50
51 /* Just for mount icons: */
52 GMount *mount;
53 };
54
55 G_DEFINE_TYPE(NautilusDesktopLink, nautilus_desktop_link, G_TYPE_OBJECT)
56
57 static void
58 create_icon_file (NautilusDesktopLink *link)
59 {
60 link->details->icon_file = nautilus_desktop_icon_file_new (link);
61 }
62
63 static void
64 nautilus_desktop_link_changed (NautilusDesktopLink *link)
65 {
66 if (link->details->icon_file != NULL) {
67 nautilus_desktop_icon_file_update (link->details->icon_file);
68 }
69 }
70
71 static void
72 mount_changed_callback (GMount *mount, NautilusDesktopLink *link)
73 {
74 g_free (link->details->display_name);
75 if (link->details->activation_location) {
76 g_object_unref (link->details->activation_location);
77 }
78 if (link->details->icon) {
79 g_object_unref (link->details->icon);
80 }
81
82 link->details->display_name = g_mount_get_name (mount);
83 link->details->activation_location = g_mount_get_default_location (mount);
84 link->details->icon = g_mount_get_icon (mount);
85
86 nautilus_desktop_link_changed (link);
87 }
88
89 static GIcon *
90 get_desktop_trash_icon (void)
91 {
92 const gchar *icon_name;
93
94 if (nautilus_trash_monitor_is_empty ()) {
95 icon_name = NAUTILUS_DESKTOP_ICON_TRASH;
96 } else {
97 icon_name = NAUTILUS_DESKTOP_ICON_TRASH_FULL;
98 }
99
100 return g_themed_icon_new (icon_name);
101 }
102
103 static void
104 trash_state_changed_callback (NautilusTrashMonitor *trash_monitor,
105 gboolean state,
106 gpointer callback_data)
107 {
108 NautilusDesktopLink *link;
109
110 link = NAUTILUS_DESKTOP_LINK (callback_data);
111 g_assert (link->details->type == NAUTILUS_DESKTOP_LINK_TRASH);
112
113 if (link->details->icon) {
114 g_object_unref (link->details->icon);
115 }
116 link->details->icon = get_desktop_trash_icon ();
117
118 nautilus_desktop_link_changed (link);
119 }
120
121 static void
122 home_name_changed (gpointer callback_data)
123 {
124 NautilusDesktopLink *link;
125
126 link = NAUTILUS_DESKTOP_LINK (callback_data);
127 g_assert (link->details->type == NAUTILUS_DESKTOP_LINK_HOME);
128
129 g_free (link->details->display_name);
130 link->details->display_name = g_settings_get_string (nautilus_desktop_preferences,
131 NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME);
132 if (link->details->display_name[0] == 0) {
133 g_free (link->details->display_name);
134 link->details->display_name = g_strdup (_("Home"));
135 }
136
137 nautilus_desktop_link_changed (link);
138 }
139
140 static void
141 trash_name_changed (gpointer callback_data)
142 {
143 NautilusDesktopLink *link;
144
145 link = NAUTILUS_DESKTOP_LINK (callback_data);
146 g_assert (link->details->type == NAUTILUS_DESKTOP_LINK_TRASH);
147
148 g_free (link->details->display_name);
149 link->details->display_name = g_settings_get_string (nautilus_desktop_preferences,
150 NAUTILUS_PREFERENCES_DESKTOP_TRASH_NAME);
151 nautilus_desktop_link_changed (link);
152 }
153
154 static void
155 network_name_changed (gpointer callback_data)
156 {
157 NautilusDesktopLink *link;
158
159 link = NAUTILUS_DESKTOP_LINK (callback_data);
160 g_assert (link->details->type == NAUTILUS_DESKTOP_LINK_NETWORK);
161
162 g_free (link->details->display_name);
163 link->details->display_name = g_settings_get_string (nautilus_desktop_preferences,
164 NAUTILUS_PREFERENCES_DESKTOP_NETWORK_NAME);
165 nautilus_desktop_link_changed (link);
166 }
167
168 NautilusDesktopLink *
169 nautilus_desktop_link_new (NautilusDesktopLinkType type)
170 {
171 NautilusDesktopLink *link;
172
173 link = NAUTILUS_DESKTOP_LINK (g_object_new (NAUTILUS_TYPE_DESKTOP_LINK, NULL));
174
175 link->details->type = type;
176 switch (type) {
177 case NAUTILUS_DESKTOP_LINK_HOME:
178 link->details->filename = g_strdup ("home");
179 link->details->display_name = g_settings_get_string (nautilus_desktop_preferences,
180 NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME);
181 link->details->activation_location = g_file_new_for_path (g_get_home_dir ());
182 link->details->icon = g_themed_icon_new (NAUTILUS_DESKTOP_ICON_HOME);
183
184 g_signal_connect_swapped (nautilus_desktop_preferences,
185 "changed::" NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME,
186 G_CALLBACK (home_name_changed),
187 link);
188 break;
189
190 case NAUTILUS_DESKTOP_LINK_TRASH:
191 link->details->filename = g_strdup ("trash");
192 link->details->display_name = g_settings_get_string (nautilus_desktop_preferences,
193 NAUTILUS_PREFERENCES_DESKTOP_TRASH_NAME);
194 link->details->activation_location = g_file_new_for_uri (EEL_TRASH_URI);
195 link->details->icon = get_desktop_trash_icon ();
196
197 g_signal_connect_swapped (nautilus_desktop_preferences,
198 "changed::" NAUTILUS_PREFERENCES_DESKTOP_TRASH_NAME,
199 G_CALLBACK (trash_name_changed),
200 link);
201 link->details->signal_handler_obj = G_OBJECT (nautilus_trash_monitor_get ());
202 link->details->signal_handler =
203 g_signal_connect_object (nautilus_trash_monitor_get (), "trash_state_changed",
204 G_CALLBACK (trash_state_changed_callback), link, 0);
205 break;
206
207 case NAUTILUS_DESKTOP_LINK_NETWORK:
208 link->details->filename = g_strdup ("network");
209 link->details->display_name = g_settings_get_string (nautilus_desktop_preferences,
210 NAUTILUS_PREFERENCES_DESKTOP_NETWORK_NAME);
211 link->details->activation_location = g_file_new_for_uri ("network:///");
212 link->details->icon = g_themed_icon_new (NAUTILUS_DESKTOP_ICON_NETWORK);
213
214 g_signal_connect_swapped (nautilus_desktop_preferences,
215 "changed::" NAUTILUS_PREFERENCES_DESKTOP_NETWORK_NAME,
216 G_CALLBACK (network_name_changed),
217 link);
218 break;
219
220 default:
221 case NAUTILUS_DESKTOP_LINK_MOUNT:
222 g_assert_not_reached();
223 }
224
225 create_icon_file (link);
226
227 return link;
228 }
229
230 NautilusDesktopLink *
231 nautilus_desktop_link_new_from_mount (GMount *mount)
232 {
233 NautilusDesktopLink *link;
234 GVolume *volume;
235 char *name, *filename;
236
237 link = NAUTILUS_DESKTOP_LINK (g_object_new (NAUTILUS_TYPE_DESKTOP_LINK, NULL));
238
239 link->details->type = NAUTILUS_DESKTOP_LINK_MOUNT;
240
241 link->details->mount = g_object_ref (mount);
242
243 /* We try to use the drive name to get somewhat stable filenames
244 for metadata */
245 volume = g_mount_get_volume (mount);
246 if (volume != NULL) {
247 name = g_volume_get_name (volume);
248 g_object_unref (volume);
249 } else {
250 name = g_mount_get_name (mount);
251 }
252
253 /* Replace slashes in name */
254 filename = g_strconcat (g_strdelimit (name, "/", '-'), ".volume", NULL);
255 link->details->filename =
256 nautilus_desktop_link_monitor_make_filename_unique (nautilus_desktop_link_monitor_get (),
257 filename);
258 g_free (filename);
259 g_free (name);
260
261 link->details->display_name = g_mount_get_name (mount);
262
263 link->details->activation_location = g_mount_get_default_location (mount);
264 link->details->icon = g_mount_get_icon (mount);
265
266 link->details->signal_handler_obj = G_OBJECT (mount);
267 link->details->signal_handler =
268 g_signal_connect (mount, "changed",
269 G_CALLBACK (mount_changed_callback), link);
270
271 create_icon_file (link);
272
273 return link;
274 }
275
276 GMount *
277 nautilus_desktop_link_get_mount (NautilusDesktopLink *link)
278 {
279 if (link->details->mount) {
280 return g_object_ref (link->details->mount);
281 }
282 return NULL;
283 }
284
285 NautilusDesktopLinkType
286 nautilus_desktop_link_get_link_type (NautilusDesktopLink *link)
287 {
288 return link->details->type;
289 }
290
291 char *
292 nautilus_desktop_link_get_file_name (NautilusDesktopLink *link)
293 {
294 return g_strdup (link->details->filename);
295 }
296
297 char *
298 nautilus_desktop_link_get_display_name (NautilusDesktopLink *link)
299 {
300 return g_strdup (link->details->display_name);
301 }
302
303 GIcon *
304 nautilus_desktop_link_get_icon (NautilusDesktopLink *link)
305 {
306 if (link->details->icon != NULL) {
307 return g_object_ref (link->details->icon);
308 }
309 return NULL;
310 }
311
312 GFile *
313 nautilus_desktop_link_get_activation_location (NautilusDesktopLink *link)
314 {
315 if (link->details->activation_location) {
316 return g_object_ref (link->details->activation_location);
317 }
318 return NULL;
319 }
320
321 char *
322 nautilus_desktop_link_get_activation_uri (NautilusDesktopLink *link)
323 {
324 if (link->details->activation_location) {
325 return g_file_get_uri (link->details->activation_location);
326 }
327 return NULL;
328 }
329
330
331 gboolean
332 nautilus_desktop_link_get_date (NautilusDesktopLink *link,
333 NautilusDateType date_type,
334 time_t *date)
335 {
336 return FALSE;
337 }
338
339 gboolean
340 nautilus_desktop_link_can_rename (NautilusDesktopLink *link)
341 {
342 return (link->details->type == NAUTILUS_DESKTOP_LINK_HOME ||
343 link->details->type == NAUTILUS_DESKTOP_LINK_TRASH ||
344 link->details->type == NAUTILUS_DESKTOP_LINK_NETWORK);
345 }
346
347 gboolean
348 nautilus_desktop_link_rename (NautilusDesktopLink *link,
349 const char *name)
350 {
351 switch (link->details->type) {
352 case NAUTILUS_DESKTOP_LINK_HOME:
353 g_settings_set_string (nautilus_desktop_preferences,
354 NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME,
355 name);
356 break;
357 case NAUTILUS_DESKTOP_LINK_TRASH:
358 g_settings_set_string (nautilus_desktop_preferences,
359 NAUTILUS_PREFERENCES_DESKTOP_TRASH_NAME,
360 name);
361 break;
362 case NAUTILUS_DESKTOP_LINK_NETWORK:
363 g_settings_set_string (nautilus_desktop_preferences,
364 NAUTILUS_PREFERENCES_DESKTOP_NETWORK_NAME,
365 name);
366 break;
367 default:
368 g_assert_not_reached ();
369 /* FIXME: Do we want volume renaming?
370 * We didn't support that before. */
371 break;
372 }
373
374 return TRUE;
375 }
376
377 static void
378 nautilus_desktop_link_init (NautilusDesktopLink *link)
379 {
380 link->details = G_TYPE_INSTANCE_GET_PRIVATE (link,
381 NAUTILUS_TYPE_DESKTOP_LINK,
382 NautilusDesktopLinkDetails);
383 }
384
385 static void
386 desktop_link_finalize (GObject *object)
387 {
388 NautilusDesktopLink *link;
389
390 link = NAUTILUS_DESKTOP_LINK (object);
391
392 if (link->details->signal_handler != 0) {
393 g_signal_handler_disconnect (link->details->signal_handler_obj,
394 link->details->signal_handler);
395 }
396
397 if (link->details->icon_file != NULL) {
398 nautilus_desktop_icon_file_remove (link->details->icon_file);
399 nautilus_file_unref (NAUTILUS_FILE (link->details->icon_file));
400 link->details->icon_file = NULL;
401 }
402
403 if (link->details->type == NAUTILUS_DESKTOP_LINK_HOME) {
404 g_signal_handlers_disconnect_by_func (nautilus_desktop_preferences,
405 home_name_changed,
406 link);
407 }
408
409 if (link->details->type == NAUTILUS_DESKTOP_LINK_TRASH) {
410 g_signal_handlers_disconnect_by_func (nautilus_desktop_preferences,
411 trash_name_changed,
412 link);
413 }
414
415 if (link->details->type == NAUTILUS_DESKTOP_LINK_NETWORK) {
416 g_signal_handlers_disconnect_by_func (nautilus_desktop_preferences,
417 network_name_changed,
418 link);
419 }
420
421 if (link->details->type == NAUTILUS_DESKTOP_LINK_MOUNT) {
422 g_object_unref (link->details->mount);
423 }
424
425 g_free (link->details->filename);
426 g_free (link->details->display_name);
427 if (link->details->activation_location) {
428 g_object_unref (link->details->activation_location);
429 }
430 if (link->details->icon) {
431 g_object_unref (link->details->icon);
432 }
433
434 G_OBJECT_CLASS (nautilus_desktop_link_parent_class)->finalize (object);
435 }
436
437 static void
438 nautilus_desktop_link_class_init (NautilusDesktopLinkClass *klass)
439 {
440 GObjectClass *object_class;
441
442 object_class = G_OBJECT_CLASS (klass);
443
444 object_class->finalize = desktop_link_finalize;
445
446 g_type_class_add_private (object_class, sizeof(NautilusDesktopLinkDetails));
447 }