summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-link.c
blob: 70f31b39ea8cf482ae7d9b608b7b5930211f49c4 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   nautilus-link.c: xml-based link files.
 
   Copyright (C) 1999, 2000, 2001 Eazel, 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: Andy Hertzfeld <andy@eazel.com>
*/

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

#include "nautilus-directory-notify.h"
#include "nautilus-directory.h"
#include "nautilus-file-attributes.h"
#include "nautilus-file.h"
#include "nautilus-file-utilities.h"
#include "nautilus-global-preferences.h"
#include "nautilus-metadata.h"
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gnome-extensions.h>
#include <eel/eel-stock-dialogs.h>
#include <eel/eel-string.h>
#include <eel/eel-vfs-extensions.h>
#include <eel/eel-xml-extensions.h>
#include <libxml/parser.h>
#include <glib/gi18n.h>
#include <libgnome/gnome-util.h>
#include <libgnomevfs/gnome-vfs-ops.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <stdlib.h>

/* NOTE: This is pretty ugly.
 * We once supported another type of link, "historical" links, which were xml files.
 * I've now removed that code, but that makes this file sort of unnecessary, and we
 * could clean up the code a lot since we know we're dealing with desktop files.
 */

static gboolean
is_link_mime_type (const char *mime_type)
{
	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;
	}
	
	return FALSE;
}

static gboolean
is_local_file_a_link (const char *uri, GnomeVFSFileInfo *opt_info)
{
	gboolean link;
	GnomeVFSResult result;
	GnomeVFSFileInfo *info;

	if (!(info = opt_info)) {
		info = gnome_vfs_file_info_new ();

		result = gnome_vfs_get_file_info (uri, info,
						  GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
						  GNOME_VFS_FILE_INFO_FOLLOW_LINKS);
		if (result != GNOME_VFS_OK) {
			gnome_vfs_file_info_unref (info);
			info = NULL;
		}
	}

	if (info && info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE) {
		link = is_link_mime_type (info->mime_type);
	} else {
		link = FALSE;
	}

	if (!opt_info && info)
		gnome_vfs_file_info_unref (info);

	return link;
}

static gboolean
is_link_data (const char *file_contents, int file_size)
{
	return is_link_mime_type
		(gnome_vfs_get_mime_type_for_data (file_contents, file_size));
}

gboolean
nautilus_link_local_create (const char *directory_uri,
			    const char *file_name,
			    const char *display_name,
			    const char *image,
			    const char *target_uri,
			    const GdkPoint *point,
			    int screen,
			    gboolean unique_filename)
{
	return nautilus_link_desktop_file_local_create (directory_uri,
							file_name,
							display_name, image,
							target_uri, 
							point, screen,
							unique_filename);
}

/* returns additional text to display under the name, NULL if none */
char *
nautilus_link_local_get_additional_text (const char *uri)
{
	if (!is_local_file_a_link (uri, NULL)) {
		return NULL;
	}
	
	return nautilus_link_desktop_file_local_get_additional_text (uri);
}

/* Returns the link uri associated with a link file. */
char *
nautilus_link_local_get_link_uri (const char *uri)
{
	if (!is_local_file_a_link (uri, NULL)) {
		return NULL;
	}
	return nautilus_link_desktop_file_local_get_link_uri (uri);
}

void
nautilus_link_get_link_info_given_file_contents (const char       *file_contents,
						 int               link_file_size,
						 char            **uri,
						 char            **name,
						 char            **icon,
						 gulong           *drive_id,
						 gulong           *volume_id)
{
	*uri = NULL;
	*name = NULL;
	*icon = NULL;
	*drive_id = 0;
	*volume_id = 0;
	
	if (is_link_data (file_contents, link_file_size)) {
		nautilus_link_desktop_file_get_link_info_given_file_contents (file_contents, link_file_size, uri, name, icon, drive_id, volume_id);
	}
}

void
nautilus_link_local_create_from_gnome_entry (GnomeDesktopItem *item,
					     const char *dest_uri,
					     const GdkPoint *position,
					     int screen)
{
	nautilus_link_desktop_file_local_create_from_gnome_entry (item, dest_uri, position, screen);
}