1
/***************************************************************************
4
* Copyright (C) 2006 Novell, Inc.
5
* Written by Aaron Bockover <aaron@abock.org>
6
****************************************************************************/
8
/* THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
10
* Permission is hereby granted, free of charge, to any person obtaining a
11
* copy of this software and associated documentation files (the "Software"),
12
* to deal in the Software without restriction, including without limitation
13
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
* and/or sell copies of the Software, and to permit persons to whom the
15
* Software is furnished to do so, subject to the following conditions:
17
* The above copyright notice and this permission notice shall be included in
18
* all copies or substantial portions of the Software.
20
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26
* DEALINGS IN THE SOFTWARE.
30
using System.Runtime.InteropServices;
36
private enum LocalPathCheck {
42
private static int MAX_SCHEME_LENGTH = 6;
45
private string local_path;
46
private string scheme;
47
private LocalPathCheck local_path_check = LocalPathCheck.NotPerformed;
49
public SafeUri (string uri)
51
int scheme_delimit_index = uri.IndexOf ("://");
52
if (scheme_delimit_index > 0 && scheme_delimit_index <= MAX_SCHEME_LENGTH) {
55
this.uri = FilenameToUri (uri);
59
public SafeUri (string uri, bool isUri)
64
this.uri = FilenameToUri (uri);
68
public SafeUri (Uri uri)
70
this.uri = uri.AbsoluteUri;
73
public static string FilenameToUri (string localPath)
75
// TODO: replace with managed conversion to avoid marshalling
76
IntPtr path_ptr = GLib.Marshaller.StringToPtrGStrdup (localPath);
79
IntPtr uri_ptr = g_filename_to_uri_utf8 (path_ptr, IntPtr.Zero, IntPtr.Zero);
81
IntPtr uri_ptr = g_filename_to_uri (path_ptr, IntPtr.Zero, IntPtr.Zero);
83
GLib.Marshaller.Free (path_ptr);
85
if (uri_ptr == IntPtr.Zero) {
86
throw new ApplicationException ("Filename path must be absolute");
89
string uri = GLib.Marshaller.Utf8PtrToString (uri_ptr);
90
GLib.Marshaller.Free (uri_ptr);
95
public static string UriToFilename (string uri)
97
// TODO: replace with managed conversion to avoid marshalling
98
IntPtr uri_ptr = GLib.Marshaller.StringToPtrGStrdup (uri);
100
IntPtr path_ptr = g_filename_from_uri_utf8 (uri_ptr, IntPtr.Zero, IntPtr.Zero);
102
IntPtr path_ptr = g_filename_from_uri (uri_ptr, IntPtr.Zero, IntPtr.Zero);
105
GLib.Marshaller.Free (uri_ptr);
107
if (path_ptr == IntPtr.Zero) {
108
throw new ApplicationException ("URI could not be converted to local file location");
111
string path = GLib.Marshaller.Utf8PtrToString (path_ptr);
112
GLib.Marshaller.Free (path_ptr);
117
public static string UriToFilename (SafeUri uri)
119
return UriToFilename (uri.AbsoluteUri);
122
public override string ToString ()
127
public static implicit operator string(SafeUri s)
129
return s.ToString ();
132
public override bool Equals (object o)
134
if (!(o is SafeUri)) {
138
return (o as SafeUri).AbsoluteUri == AbsoluteUri;
141
public override int GetHashCode ()
143
return AbsoluteUri.GetHashCode ();
146
public string AbsoluteUri {
150
public bool IsLocalPath {
152
if (local_path_check == LocalPathCheck.NotPerformed) {
154
local_path_check = LocalPathCheck.Yes;
157
local_path_check = LocalPathCheck.No;
162
return local_path_check == LocalPathCheck.Yes;
166
public string AbsolutePath {
168
if (local_path == null && IsLocalPath) {
169
local_path = UriToFilename (uri);
176
public string LocalPath {
177
get { return AbsolutePath; }
180
public string Scheme {
182
if (scheme == null) {
183
scheme = uri.Substring (0, uri.IndexOf ("://"));
191
get { return Scheme == System.Uri.UriSchemeFile; }
195
[DllImport ("libglib-2.0-0.dll")]
196
private static extern IntPtr g_filename_to_uri_utf8 (IntPtr filename, IntPtr hostname, IntPtr error);
198
[DllImport ("libglib-2.0-0.dll")]
199
private static extern IntPtr g_filename_from_uri_utf8 (IntPtr uri, IntPtr hostname, IntPtr error);
201
[DllImport ("libglib-2.0-0.dll")]
202
private static extern IntPtr g_filename_to_uri (IntPtr filename, IntPtr hostname, IntPtr error);
204
[DllImport ("libglib-2.0-0.dll")]
205
private static extern IntPtr g_filename_from_uri (IntPtr uri, IntPtr hostname, IntPtr error);