summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-desktop-link.c
blob: b5270d6167d8b310f6b09879c77513d5fc289e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-desktop-link.c: Class that handles the links on the desktop
    
   Copyright (C) 2003 Red Hat, Inc.
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
  
   You should have received a copy of the GNU General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
   Author: Alexander Larsson <alexl@redhat.com>
*/

#include <config.h>
#include "nautilus-desktop-link.h"
#include "nautilus-desktop-icon-file.h"
#include "nautilus-directory-private.h"
#include "nautilus-desktop-directory.h"

#include <eel/eel-gtk-macros.h>
#include <eel/eel-vfs-extensions.h>
#include <gtk/gtksignal.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libnautilus-private/nautilus-trash-monitor.h>
#include <libnautilus-private/nautilus-global-preferences.h>
#include <string.h>

#define TRASH_EMPTY_ICON "gnome-fs-trash-empty"
#define TRASH_FULL_ICON "gnome-fs-trash-full"

struct NautilusDesktopLinkDetails {
	NautilusDesktopLinkType type;
        char *filename;
	char *display_name;
	char *activation_uri;
	char *icon;

	NautilusDesktopIconFile *icon_file;
	
	/* Just for trash icons: */
	gulong trash_state_handler;

	/* Just for volume icons: */
	char *mount_path;
};

static void nautilus_desktop_link_init       (gpointer              object,
					      gpointer              klass);
static void nautilus_desktop_link_class_init (gpointer              klass);
static void trash_state_changed_callback     (NautilusTrashMonitor *trash_monitor,
					      gboolean              state,
					      gpointer              callback_data);
static void nautilus_desktop_link_changed    (NautilusDesktopLink  *link);
static void home_uri_changed                 (gpointer              callback_data);

EEL_CLASS_BOILERPLATE (NautilusDesktopLink,
		       nautilus_desktop_link,
		       G_TYPE_OBJECT)

static void
create_icon_file (NautilusDesktopLink *link)
{
	link->details->icon_file = nautilus_desktop_icon_file_new (link);
}

static void
home_name_changed (gpointer callback_data)
{
	NautilusDesktopLink *link;

	link = NAUTILUS_DESKTOP_LINK (callback_data);
	g_assert (link->details->type == NAUTILUS_DESKTOP_LINK_HOME);

	g_free (link->details->display_name);
	link->details->display_name = eel_preferences_get (NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME);
	
	nautilus_desktop_link_changed (link);
}

static void
trash_name_changed (gpointer callback_data)
{
	NautilusDesktopLink *link;

	link = NAUTILUS_DESKTOP_LINK (callback_data);
	g_assert (link->details->type == NAUTILUS_DESKTOP_LINK_TRASH);

	
	g_free (link->details->display_name);
	link->details->display_name = eel_preferences_get (NAUTILUS_PREFERENCES_DESKTOP_TRASH_NAME);
	nautilus_desktop_link_changed (link);
}



NautilusDesktopLink *
nautilus_desktop_link_new (NautilusDesktopLinkType type)
{
	NautilusDesktopLink *link;

	link = NAUTILUS_DESKTOP_LINK (g_object_new (NAUTILUS_TYPE_DESKTOP_LINK, NULL));

	link->details->type = type;
	switch (type) {
	case NAUTILUS_DESKTOP_LINK_HOME:
		link->details->filename = g_strdup ("home");

		link->details->display_name = eel_preferences_get (NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME);
		
#ifdef WEB_NAVIGATION_ENABLED
		link->details->activation_uri = eel_preferences_get (NAUTILUS_PREFERENCES_HOME_URI);
#else
		link->details->activation_uri = gnome_vfs_get_uri_from_local_path (g_get_home_dir ());
#endif
		link->details->icon = g_strdup ("gnome-fs-home");

		eel_preferences_add_callback (NAUTILUS_PREFERENCES_HOME_URI,
					      home_uri_changed,
					      link);
		eel_preferences_add_callback (NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME,
					      home_name_changed,
					      link);
		
		break;
	case NAUTILUS_DESKTOP_LINK_TRASH:
		link->details->filename = g_strdup ("trash");
		link->details->display_name = g_strdup (_("Trash"));
		link->details->activation_uri = g_strdup (EEL_TRASH_URI);
		if (nautilus_trash_monitor_is_empty ()) {
			link->details->icon = g_strdup (TRASH_EMPTY_ICON);
		} else {
			link->details->icon = g_strdup (TRASH_FULL_ICON);
		}
		
		eel_preferences_add_callback (NAUTILUS_PREFERENCES_DESKTOP_TRASH_NAME,
					      trash_name_changed,
					      link);
		link->details->trash_state_handler =
			g_signal_connect_object (nautilus_trash_monitor_get (), "trash_state_changed",
						 G_CALLBACK (trash_state_changed_callback), link, 0);	
		break;
	default:
	case NAUTILUS_DESKTOP_LINK_VOLUME:
		g_assert_not_reached();
	}

	create_icon_file (link);

	return link;
}

static char *
get_icon_for_volume (const NautilusVolume *volume)
{
	char *icon_name;
	
	icon_name = "gnome-dev-harddisk";
	switch (nautilus_volume_get_device_type (volume)) {
	case NAUTILUS_DEVICE_AUDIO_CD:
	case NAUTILUS_DEVICE_CDROM_DRIVE:
		icon_name = "gnome-dev-cdrom";
		break;

	case NAUTILUS_DEVICE_FLOPPY_DRIVE:
		icon_name = "gnome-dev-floppy";
		break;

	case NAUTILUS_DEVICE_JAZ_DRIVE:
		icon_name = "gnome-dev-jazdisk";
		break;

	case NAUTILUS_DEVICE_MEMORY_STICK:
		icon_name = "gnome-dev-memory";
		break;
	
	case NAUTILUS_DEVICE_NFS:
		icon_name = "gnome-fs-nfs";
		break;

	case NAUTILUS_DEVICE_SMB:
		icon_name = "gnome-fs-smb";
		break;
	
	case NAUTILUS_DEVICE_ZIP_DRIVE:
		icon_name = "gnome-dev-zipdisk";
		break;

	case NAUTILUS_DEVICE_APPLE:
	case NAUTILUS_DEVICE_WINDOWS:
	case NAUTILUS_DEVICE_CAMERA:
	case NAUTILUS_DEVICE_UNKNOWN:
		break;
	}

	return g_strdup (icon_name);
}

NautilusDesktopLink *
nautilus_desktop_link_new_from_volume (const NautilusVolume *volume)
{
	NautilusDesktopLink *link;
	const char *mount_path;
	char *underscore_mount_path, *p;

	link = NAUTILUS_DESKTOP_LINK (g_object_new (NAUTILUS_TYPE_DESKTOP_LINK, NULL));
	
	link->details->type = NAUTILUS_DESKTOP_LINK_VOLUME;

	mount_path = nautilus_volume_get_mount_path (volume);
	link->details->mount_path = g_strdup (mount_path);
	
	/* Convert slashes in the mount path to underscores and skip
	   first slash */
	while (*mount_path == '/') {
		mount_path ++;
	}
	underscore_mount_path = g_strdup (mount_path);
	for (p = underscore_mount_path; *p != 0; p++) {
		if (*p == '/') {
			*p = '_';
		}
	}

	link->details->filename = g_strconcat ("mount_", underscore_mount_path, NULL);
	g_free (underscore_mount_path);
	
	link->details->display_name = nautilus_volume_get_name (volume);
	
	link->details->activation_uri = nautilus_volume_get_target_uri (volume);
	link->details->icon = get_icon_for_volume (volume);
	
	create_icon_file (link);

	return link;
}

char *
nautilus_desktop_link_get_mount_path (NautilusDesktopLink *link)
{
	g_assert (link->details->type == NAUTILUS_DESKTOP_LINK_VOLUME);
	return g_strdup (link->details->mount_path);
}


NautilusDesktopLinkType
nautilus_desktop_link_get_link_type  (NautilusDesktopLink *link)
{
  return link->details->type;
}

char *
nautilus_desktop_link_get_file_name (NautilusDesktopLink *link)
{
	return g_strdup (link->details->filename);
}

char *
nautilus_desktop_link_get_display_name (NautilusDesktopLink *link)
{
	return g_strdup (link->details->display_name);
}

char *
nautilus_desktop_link_get_icon (NautilusDesktopLink *link)
{
	return g_strdup (link->details->icon);
}

char *
nautilus_desktop_link_get_activation_uri (NautilusDesktopLink *link)
{
	return g_strdup (link->details->activation_uri);
}

gboolean
nautilus_desktop_link_get_date (NautilusDesktopLink *link,
				NautilusDateType     date_type,
				time_t               *date)
{
	return FALSE;
}

static void
nautilus_desktop_link_changed (NautilusDesktopLink *link)
{
	if (link->details->icon_file != NULL) {
		nautilus_desktop_icon_file_update (link->details->icon_file);
	}
}

static void
trash_state_changed_callback (NautilusTrashMonitor *trash_monitor,
			      gboolean state,
			      gpointer callback_data)
{
	NautilusDesktopLink *link;

	link = NAUTILUS_DESKTOP_LINK (callback_data);
	g_assert (link->details->type == NAUTILUS_DESKTOP_LINK_TRASH);

	g_free (link->details->icon);
	
	if (state) {
		link->details->icon = g_strdup (TRASH_EMPTY_ICON);
	} else {
		link->details->icon = g_strdup (TRASH_FULL_ICON);
	}

	nautilus_desktop_link_changed (link);
}

static void
home_uri_changed (gpointer callback_data)
{
	NautilusDesktopLink *link;

	link = NAUTILUS_DESKTOP_LINK (callback_data);

	g_free (link->details->activation_uri);
#ifdef WEB_NAVIGATION_ENABLED
	link->details->activation_uri = eel_preferences_get (NAUTILUS_PREFERENCES_HOME_URI);
#else
	link->details->activation_uri = gnome_vfs_get_uri_from_local_path (g_get_home_dir ());
#endif
	
	nautilus_desktop_link_changed (link);
}


gboolean
nautilus_desktop_link_can_rename (NautilusDesktopLink     *link)
{
	return (link->details->type == NAUTILUS_DESKTOP_LINK_HOME ||
		link->details->type == NAUTILUS_DESKTOP_LINK_TRASH);
}

gboolean
nautilus_desktop_link_rename (NautilusDesktopLink     *link,
			      const char              *name)
{
	switch (link->details->type) {
	case NAUTILUS_DESKTOP_LINK_HOME:
		eel_preferences_set (NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME,
				     name);
		break;
	case NAUTILUS_DESKTOP_LINK_TRASH:
		eel_preferences_set (NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME,
				     name);
		break;
	default:
		g_assert_not_reached ();
		/* FIXME: Do we want volume renaming?
		 * We didn't support that before. */
		break;
	}
	
	return TRUE;
}


static void
nautilus_desktop_link_init (gpointer object, gpointer klass)
{
	NautilusDesktopLink *link;

	link = NAUTILUS_DESKTOP_LINK (object);

	link->details = g_new0 (NautilusDesktopLinkDetails, 1);
}	

static void
desktop_link_finalize (GObject *object)
{
	NautilusDesktopLink *link;

	link = NAUTILUS_DESKTOP_LINK (object);

	if (link->details->trash_state_handler != 0) {
		g_signal_handler_disconnect (nautilus_trash_monitor_get (),
					     link->details->trash_state_handler);
	}

	if (link->details->icon_file != NULL) {
		nautilus_desktop_icon_file_remove (link->details->icon_file);
		nautilus_file_unref (NAUTILUS_FILE (link->details->icon_file));
		link->details->icon_file = NULL;
	}

	if (link->details->type == NAUTILUS_DESKTOP_LINK_HOME) {
		eel_preferences_remove_callback (NAUTILUS_PREFERENCES_HOME_URI,
						 home_uri_changed,
						 link);
		eel_preferences_remove_callback (NAUTILUS_PREFERENCES_DESKTOP_HOME_NAME,
						 home_name_changed,
						 link);
	}
	
	if (link->details->type == NAUTILUS_DESKTOP_LINK_TRASH) {
		eel_preferences_remove_callback (NAUTILUS_PREFERENCES_DESKTOP_TRASH_NAME,
						 trash_name_changed,
						 link);
	}
	
	g_free (link->details);

	EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}

static void
nautilus_desktop_link_class_init (gpointer klass)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);
	
	object_class->finalize = desktop_link_finalize;

}