nautilus-link.c 11.5 KB
Newer Older
1 2
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

Alexander Larsson's avatar
Alexander Larsson committed
3
   nautilus-link.c: .desktop link files.
4
 
Alexander Larsson's avatar
Alexander Larsson committed
5
   Copyright (C) 2001 Red Hat, Inc.
6 7 8 9 10 11 12
  
   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,
Alexander Larsson's avatar
Alexander Larsson committed
13
   but WITHOUT ANY WARRANTY; without even the historicalied warranty of
14 15 16 17 18 19 20 21
   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.
  
Alexander Larsson's avatar
Alexander Larsson committed
22 23
   Authors: Jonathan Blandford <jrb@redhat.com>
            Alexander Larsson <alexl@redhat.com>
24 25
*/

26
#include <config.h>
27
#include "nautilus-link.h"
28

29
#include "nautilus-directory-notify.h"
30
#include "nautilus-directory.h"
Ramiro Estrugo's avatar
Ramiro Estrugo committed
31
#include "nautilus-file-utilities.h"
Alexander Larsson's avatar
Alexander Larsson committed
32 33
#include "nautilus-file.h"
#include "nautilus-program-choosing.h"
Ramiro Estrugo's avatar
Ramiro Estrugo committed
34
#include <eel/eel-vfs-extensions.h>
35
#include <glib/gi18n.h>
36
#include <gio/gio.h>
37
#include <stdlib.h>
Alexander Larsson's avatar
Alexander Larsson committed
38
#include <string.h>
39

Alexander Larsson's avatar
Alexander Larsson committed
40 41 42 43
#define NAUTILUS_LINK_GENERIC_TAG	"Link"
#define NAUTILUS_LINK_TRASH_TAG 	"X-nautilus-trash"
#define NAUTILUS_LINK_MOUNT_TAG 	"FSDevice"
#define NAUTILUS_LINK_HOME_TAG 		"X-nautilus-home"
44

45 46
static gboolean
is_link_mime_type (const char *mime_type)
47
{
48 49 50 51
	if (mime_type != NULL &&
	    (g_ascii_strcasecmp (mime_type, "application/x-gnome-app-info") == 0 ||
	     g_ascii_strcasecmp (mime_type, "application/x-desktop") == 0)) {
		return TRUE;
52
	}
Alexander Larsson's avatar
Alexander Larsson committed
53

54
	return FALSE;
55 56
}

57
static gboolean
Alexander Larsson's avatar
Alexander Larsson committed
58
is_local_file_a_link (const char *uri)
59
{
60
	gboolean link;
Alexander Larsson's avatar
Alexander Larsson committed
61 62 63
	GFile *file;
	GFileInfo *info;
	GError *error;
64

Alexander Larsson's avatar
Alexander Larsson committed
65 66 67 68 69 70
	error = NULL;
	link = FALSE;

	file = g_file_new_for_uri (uri);

	info = g_file_query_info (file,
71
				  G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
Alexander Larsson's avatar
Alexander Larsson committed
72 73 74 75 76 77 78 79
				  0, NULL, &error);
	if (info) {
		link = is_link_mime_type (g_file_info_get_content_type (info));
		g_object_unref (info);
	}
	else {
		g_warning ("Error getting info: %s\n", error->message);
		g_error_free (error);
80 81
	}

Alexander Larsson's avatar
Alexander Larsson committed
82
	g_object_unref (file);
83

84
	return link;
85
}
Alexander Larsson's avatar
Alexander Larsson committed
86

87 88
static gboolean
is_link_data (const char *file_contents, int file_size)
89
{
Alexander Larsson's avatar
Alexander Larsson committed
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
	char *mimetype;
	gboolean res;

	mimetype = g_content_type_guess (NULL, file_contents, file_size, NULL);
	res =  is_link_mime_type (mimetype);
	g_free (mimetype);
	return res;
}

static char *
slurp_key_string (const char *uri,
		  const char *keyname,
                  gboolean    localize)
{
	GnomeDesktopItem *desktop_file;
	const char *text;
	char *result;

	desktop_file = gnome_desktop_item_new_from_uri (uri, 0, NULL);
	if (desktop_file == NULL) {
		return NULL;
	}

	if (localize) {
		text = gnome_desktop_item_get_localestring (desktop_file, keyname);
	} else {
		text = gnome_desktop_item_get_string (desktop_file, keyname);
	}

	result = g_strdup (text);
	gnome_desktop_item_unref (desktop_file);

	return result;
123 124
}

125
gboolean
Alexander Larsson's avatar
Alexander Larsson committed
126 127 128 129 130
nautilus_link_local_create (const char     *directory_uri,
			    const char     *base_name,
			    const char     *display_name,
			    const char     *image,
			    const char     *target_uri,
131
			    const GdkPoint *point,
Alexander Larsson's avatar
Alexander Larsson committed
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
			    int             screen,
			    gboolean        unique_filename)
{
	char *real_directory_uri;
	char *uri, *contents;
	GnomeDesktopItem *desktop_item;
	GList dummy_list;
	NautilusFileChangesQueuePosition item;

	g_return_val_if_fail (directory_uri != NULL, FALSE);
	g_return_val_if_fail (base_name != NULL, FALSE);
	g_return_val_if_fail (display_name != NULL, FALSE);
	g_return_val_if_fail (target_uri != NULL, FALSE);

	if (eel_uri_is_trash (directory_uri) ||
	    eel_uri_is_search (directory_uri)) {
		return FALSE;
	}

	if (eel_uri_is_desktop (directory_uri)) {
		real_directory_uri = nautilus_get_desktop_directory_uri ();
	} else {
		real_directory_uri = g_strdup (directory_uri);
	}

	if (unique_filename) {
		uri = nautilus_ensure_unique_file_name (real_directory_uri,
							base_name, ".desktop");
		if (uri == NULL) {
			g_free (real_directory_uri);
			return FALSE;
		}
	} else {
		char *link_name;
		GFile *dir, *link;

		link_name = g_strdup_printf ("%s.desktop", base_name);

		/* replace '/' with '-', just in case */
		g_strdelimit (link_name, "/", '-');

		dir = g_file_new_for_uri (directory_uri);
		link = g_file_get_child (dir, link_name);

		uri = g_file_get_uri (link);

		g_free (link_name);
		g_object_unref (dir);
		g_object_unref (link);
	}

	g_free (real_directory_uri);

	contents = g_strdup_printf ("[Desktop Entry]\n"
				    "Encoding=UTF-8\n"
				    "Name=%s\n"
				    "Type=Link\n"
				    "URL=%s\n"
				    "%s%s\n",
				    display_name,
				    target_uri,
				    image != NULL ? "Icon=" : "",
				    image != NULL ? image : "");

	desktop_item = gnome_desktop_item_new_from_string (uri,
							   contents,
							   strlen (contents),
							   0,
							   NULL);
	if (!desktop_item) {
		g_free (contents);
		g_free (uri);
		return FALSE;
	}

	if (!gnome_desktop_item_save (desktop_item, uri, TRUE, NULL)) {
		gnome_desktop_item_unref (desktop_item);
		g_free (contents);
		g_free (uri);
		return FALSE;
	}

	dummy_list.data = uri;
	dummy_list.next = NULL;
	dummy_list.prev = NULL;
	nautilus_directory_notify_files_added_by_uri (&dummy_list);
	nautilus_directory_schedule_metadata_remove_by_uri (&dummy_list);

	if (point != NULL) {
		item.location = g_file_new_for_uri (uri);
		item.set = TRUE;
		item.point.x = point->x;
		item.point.y = point->y;
		item.screen = screen;
		dummy_list.data = &item;
		dummy_list.next = NULL;
		dummy_list.prev = NULL;
	
		nautilus_directory_schedule_position_set (&dummy_list);
		g_object_unref (item.location);
	}

	gnome_desktop_item_unref (desktop_item);
	g_free (contents);
	g_free (uri);
	return TRUE;
}

gboolean
nautilus_link_local_set_text (const char *uri,
			      const char *text)
243
{
Alexander Larsson's avatar
Alexander Larsson committed
244 245 246 247 248 249 250 251 252 253 254 255 256
	GnomeDesktopItem *desktop_file;
	gboolean success;

	desktop_file = gnome_desktop_item_new_from_uri (uri, 0, NULL);
	if (desktop_file == NULL) {
		return FALSE;
	}

	gnome_desktop_item_set_localestring (desktop_file, "Name", text);
	success = gnome_desktop_item_save (desktop_file, NULL, FALSE, NULL);
	gnome_desktop_item_unref (desktop_file);
	
	return success;
257 258 259
}

char *
Alexander Larsson's avatar
Alexander Larsson committed
260
nautilus_link_local_get_text (const char *path)
261
{
Alexander Larsson's avatar
Alexander Larsson committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	return slurp_key_string (path, "Name", TRUE);
}

char *
nautilus_link_local_get_additional_text (const char *path)
{
	/* The comment field of current .desktop files is often bad.
	 * It just contains a copy of the name. This is probably because the
	 * panel shows the comment field as a tooltip.
	 */
	return NULL;
#ifdef THIS_IS_NOT_USED_RIGHT_NOW
	char *type;
	char *retval;

	if (!is_local_file_a_link (uri)) {
		return NULL;
	}

	type = slurp_key_string (path, "Type", FALSE);
	retval = NULL;
	if (type == NULL) {
284
		return NULL;
Alexander Larsson's avatar
Alexander Larsson committed
285
	}
Alexander Larsson's avatar
Alexander Larsson committed
286 287 288 289

	if (strcmp (type, "Application") == 0) {
		retval = slurp_key_string (path, "Comment", TRUE);
	}
290
	
Alexander Larsson's avatar
Alexander Larsson committed
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
	g_free (type);

	return retval;
#endif
}

static char *
nautilus_link_get_link_uri_from_desktop (GnomeDesktopItem *desktop_file)
{
	const char *launch_string;
	const char *type;
	char *retval;

	retval = NULL;

	type = gnome_desktop_item_get_string (desktop_file, "Type");
	if (type == NULL) {
		return NULL;
	}

	if (strcmp (type, "Application") == 0) {
		launch_string = gnome_desktop_item_get_string (desktop_file, "Exec");
		if (launch_string == NULL) {
			return NULL;
		}

		launch_string = gnome_desktop_item_get_location (desktop_file);
		retval = g_strconcat (NAUTILUS_DESKTOP_COMMAND_SPECIFIER, launch_string, NULL);
	} else if (strcmp (type, "URL") == 0) {
		/* Some old broken desktop files use this nonstandard feature, we need handle it though */
		retval = g_strdup (gnome_desktop_item_get_string (desktop_file, "Exec"));
	} else if ((strcmp (type, NAUTILUS_LINK_GENERIC_TAG) == 0) ||
		   (strcmp (type, NAUTILUS_LINK_MOUNT_TAG) == 0) ||
		   (strcmp (type, NAUTILUS_LINK_TRASH_TAG) == 0) ||
		   (strcmp (type, NAUTILUS_LINK_HOME_TAG) == 0)) {
		retval = g_strdup (gnome_desktop_item_get_string (desktop_file, "URL"));
	}

	return retval;
}

static char *
nautilus_link_get_link_name_from_desktop (GnomeDesktopItem *desktop_file)
{
	return g_strdup (gnome_desktop_item_get_localestring (desktop_file, "Name"));
}

static char *
nautilus_link_get_link_icon_from_desktop (GnomeDesktopItem *desktop_file)
{
	char *icon_uri;
	const char *icon;
	GnomeDesktopItemType desktop_type;

	icon_uri = g_strdup (gnome_desktop_item_get_string (desktop_file, "X-Nautilus-Icon"));
	if (icon_uri != NULL) {
		return icon_uri;
	}

	icon = gnome_desktop_item_get_string (desktop_file, GNOME_DESKTOP_ITEM_ICON);
	if (icon != NULL) {
		return g_strdup (icon);
	}

	desktop_type = gnome_desktop_item_get_entry_type (desktop_file);
	switch (desktop_type) {
	case GNOME_DESKTOP_ITEM_TYPE_APPLICATION:
		return g_strdup ("gnome-fs-executable");

	case GNOME_DESKTOP_ITEM_TYPE_LINK:
		return g_strdup ("gnome-dev-symlink");

	case GNOME_DESKTOP_ITEM_TYPE_FSDEVICE:
		return g_strdup ("gnome-dev-harddisk");

	case GNOME_DESKTOP_ITEM_TYPE_DIRECTORY:
		return g_strdup ("gnome-fs-directory");

	case GNOME_DESKTOP_ITEM_TYPE_SERVICE:
	case GNOME_DESKTOP_ITEM_TYPE_SERVICE_TYPE:
		return g_strdup ("gnome-fs-web");

	default:
		return g_strdup ("gnome-fs-regular");
	}

	g_assert_not_reached ();
	return NULL;
379 380
}

Alexander Larsson's avatar
Alexander Larsson committed
381
char *
382
nautilus_link_local_get_link_uri (const char *uri)
383
{
Alexander Larsson's avatar
Alexander Larsson committed
384 385 386 387 388 389 390 391 392
	GnomeDesktopItem *desktop_file;
	char *retval;

	if (!is_local_file_a_link (uri)) {
		return NULL;
	}

	desktop_file = gnome_desktop_item_new_from_uri (uri, 0, NULL);
	if (desktop_file == NULL) {
393
		return NULL;
394
	}
Alexander Larsson's avatar
Alexander Larsson committed
395 396 397 398 399

	retval = nautilus_link_get_link_uri_from_desktop (desktop_file);
	gnome_desktop_item_unref (desktop_file);

	return retval;
400 401
}

Alexander Larsson's avatar
Alexander Larsson committed
402
void
Alexander Larsson's avatar
Alexander Larsson committed
403 404 405 406 407
nautilus_link_get_link_info_given_file_contents (const char  *file_contents,
						 int          link_file_size,
						 char       **uri,
						 char       **name,
						 char       **icon)
408
{
Alexander Larsson's avatar
Alexander Larsson committed
409 410 411 412
	GnomeDesktopItem *desktop_file;

	if (!is_link_data (file_contents, link_file_size)) {
		return;
Alexander Larsson's avatar
Alexander Larsson committed
413
	}
Alexander Larsson's avatar
Alexander Larsson committed
414 415 416 417 418 419 420 421 422 423 424

	desktop_file = gnome_desktop_item_new_from_string (NULL, file_contents, link_file_size, 0, NULL);
	if (desktop_file == NULL) {
		return; 
	}

	*uri = nautilus_link_get_link_uri_from_desktop (desktop_file);
	*name = nautilus_link_get_link_name_from_desktop (desktop_file);
	*icon = nautilus_link_get_link_icon_from_desktop (desktop_file);

	gnome_desktop_item_unref (desktop_file);
425
}
426 427

void
Alexander Larsson's avatar
Alexander Larsson committed
428 429 430 431
nautilus_link_local_create_from_gnome_entry (GnomeDesktopItem  *entry,
					     const char        *dest_uri,
					     const GdkPoint    *position,
					     int                screen)
432
{
Alexander Larsson's avatar
Alexander Larsson committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	GList dummy_list;
	NautilusFileChangesQueuePosition item;
	GnomeDesktopItem *new_entry;
	char *file_uri;
	const char *name;

	name = gnome_desktop_item_get_string (entry, GNOME_DESKTOP_ITEM_NAME);
	file_uri = g_strdup_printf ("%s/%s.desktop", dest_uri, name);

	new_entry = gnome_desktop_item_copy (entry);
	gnome_desktop_item_save (new_entry, file_uri, TRUE, NULL);

	dummy_list.data = file_uri;
	dummy_list.next = NULL;
	dummy_list.prev = NULL;
	nautilus_directory_notify_files_added_by_uri (&dummy_list);
	nautilus_directory_schedule_metadata_remove_by_uri (&dummy_list);

	if (position != NULL) {
		item.location = g_file_new_for_uri (file_uri);
		item.set = TRUE;
		item.point.x = position->x;
		item.point.y = position->y;
		item.screen = screen;
		
		dummy_list.data = &item;
		dummy_list.next = NULL;
		dummy_list.prev = NULL;
	
		nautilus_directory_schedule_position_set (&dummy_list);
		g_object_unref (item.location);
	}
	gnome_desktop_item_unref (new_entry);
466
}