~ubuntu-branches/ubuntu/saucy/f-spot/saucy

« back to all changes in this revision

Viewing changes to src/Utils/SafeUri.cs

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-05-24 10:35:57 UTC
  • mfrom: (2.4.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20100524103557-1j0i8f66caybci2n
Tags: 0.7.0-1
* New upstream release 0.7.0
 + First release of the unstable 0.7 development series. Massive changes.
 + Reparenting and detaching support (Anton Keks) (Closes: #559745)
 + A new Mallard-based documentation (Harold Schreckengost)
 + No longer embeds flickrnet, uses distribution copy (Iain Lane)
 + Adoption of a large amount of Hyena functionality (Paul Lange, Peter
   Goetz)
 + No longer embeds gnome-keyring-sharp
 + Completely rewritten import, much faster and less memory hungry (Ruben
   Vermeersch) (Closes: #559080, #492658, #341790, #357811, #426017) (LP:
   #412091)
 + No longer use gphoto2-sharp, now uses gvfs which is less crash-pron
   (Ruben Vermeersch)
 + Fix Facebook support (Ruben Vermeersch)
 + Modernized unit tests
 + Revamped build (Mike Gemünde)
 + Much improved duplicate detection (much faster too) (Ruben Vermeersch)
 + Mouse selection in Iconview (Vincent Pomey)
 + Image panning support using middle mouse button (Wojciech Dzierżanowski)
 + Timeline slider now restricted to the size of the window (Iain Churcher)
 + Over 100 bugs closed (http://bit.ly/cyVjnD)
   - No more warnings about schema defaults (Closes: #584215) (LP: #586132)
* debian/control: Clean up build deps to match configure checks
* debian/rules: Don't run dh_makeshilbs as we don't ship any shared
  libraries. There are some private ones though, which get picked up and
  result in a useless postinst/postrm call to ldconfig. Thanks, lintian.
* debian/patches/debian_fix-distclean.patch,
  debian/patches/debian_fix_f-spot.exe.config.patch,
  debian/patches/debian_link-system-flickrnet.patch,
  debian/patches/debian_link-system-gnome-keyring.patch,
  debian/patches/debian_disable-unit-tests,
  debian/patches/git_transition_duration.patch,
  debian/patches/ubuntu_fix_folder_export_hang.patch:
  Clean up obsolete patches which are no longer necessary 
* debian/patches/*: Temporarily disable patches which originated from Ubuntu
  and no longer apply cleanly. We will get these back in a future upstream
  development release.
* debian/patches/*: Refresh to apply cleanly 
* debian/rules: Add new include dir to autoreconf call to pick up f-spot
  macros 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *  SafeUri.cs
 
3
 *
 
4
 *  Copyright (C) 2006 Novell, Inc.
 
5
 *  Written by Aaron Bockover <aaron@abock.org>
 
6
 ****************************************************************************/
 
7
 
 
8
/*  THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
 
9
 *
 
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:
 
16
 *
 
17
 *  The above copyright notice and this permission notice shall be included in
 
18
 *  all copies or substantial portions of the Software.
 
19
 *
 
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.
 
27
 */
 
28
 
 
29
using System;
 
30
using System.Runtime.InteropServices;
 
31
 
 
32
namespace Hyena
 
33
{
 
34
    public class SafeUri
 
35
    {
 
36
        private enum LocalPathCheck {
 
37
            NotPerformed,
 
38
            Yes,
 
39
            No
 
40
        }
 
41
 
 
42
        private static int MAX_SCHEME_LENGTH = 6;
 
43
 
 
44
        private string uri;
 
45
        private string local_path;
 
46
        private string scheme;
 
47
        private LocalPathCheck local_path_check = LocalPathCheck.NotPerformed;
 
48
 
 
49
        public SafeUri (string uri)
 
50
        {
 
51
            int scheme_delimit_index = uri.IndexOf ("://");
 
52
            if (scheme_delimit_index > 0 && scheme_delimit_index <= MAX_SCHEME_LENGTH) {
 
53
                this.uri = uri;
 
54
            } else {
 
55
                this.uri = FilenameToUri (uri);
 
56
            }
 
57
        }
 
58
 
 
59
        public SafeUri (string uri, bool isUri)
 
60
        {
 
61
            if (isUri) {
 
62
                this.uri = uri;
 
63
            } else {
 
64
                this.uri = FilenameToUri (uri);
 
65
            }
 
66
        }
 
67
 
 
68
        public SafeUri (Uri uri)
 
69
        {
 
70
            this.uri = uri.AbsoluteUri;
 
71
        }
 
72
 
 
73
        public static string FilenameToUri (string localPath)
 
74
        {
 
75
            // TODO: replace with managed conversion to avoid marshalling
 
76
            IntPtr path_ptr = GLib.Marshaller.StringToPtrGStrdup (localPath);
 
77
 
 
78
#if WIN32
 
79
            IntPtr uri_ptr = g_filename_to_uri_utf8 (path_ptr, IntPtr.Zero, IntPtr.Zero);
 
80
#else
 
81
            IntPtr uri_ptr = g_filename_to_uri (path_ptr, IntPtr.Zero, IntPtr.Zero);
 
82
#endif
 
83
            GLib.Marshaller.Free (path_ptr);
 
84
 
 
85
            if (uri_ptr == IntPtr.Zero) {
 
86
                throw new ApplicationException ("Filename path must be absolute");
 
87
            }
 
88
 
 
89
            string uri = GLib.Marshaller.Utf8PtrToString (uri_ptr);
 
90
            GLib.Marshaller.Free (uri_ptr);
 
91
 
 
92
            return uri;
 
93
        }
 
94
 
 
95
        public static string UriToFilename (string uri)
 
96
        {
 
97
            // TODO: replace with managed conversion to avoid marshalling
 
98
            IntPtr uri_ptr = GLib.Marshaller.StringToPtrGStrdup (uri);
 
99
#if WIN32
 
100
            IntPtr path_ptr = g_filename_from_uri_utf8 (uri_ptr, IntPtr.Zero, IntPtr.Zero);
 
101
#else
 
102
            IntPtr path_ptr = g_filename_from_uri (uri_ptr, IntPtr.Zero, IntPtr.Zero);
 
103
#endif
 
104
 
 
105
            GLib.Marshaller.Free (uri_ptr);
 
106
 
 
107
            if (path_ptr == IntPtr.Zero) {
 
108
                throw new ApplicationException ("URI could not be converted to local file location");
 
109
            }
 
110
 
 
111
            string path = GLib.Marshaller.Utf8PtrToString (path_ptr);
 
112
            GLib.Marshaller.Free (path_ptr);
 
113
 
 
114
            return path;
 
115
        }
 
116
 
 
117
        public static string UriToFilename (SafeUri uri)
 
118
        {
 
119
            return UriToFilename (uri.AbsoluteUri);
 
120
        }
 
121
 
 
122
        public override string ToString ()
 
123
        {
 
124
            return AbsoluteUri;
 
125
        }
 
126
 
 
127
        public static implicit operator string(SafeUri s)
 
128
        {
 
129
            return s.ToString ();
 
130
        }
 
131
 
 
132
        public override bool Equals (object o)
 
133
        {
 
134
            if (!(o is SafeUri)) {
 
135
                return false;
 
136
            }
 
137
 
 
138
            return (o as SafeUri).AbsoluteUri == AbsoluteUri;
 
139
        }
 
140
 
 
141
        public override int GetHashCode ()
 
142
        {
 
143
            return AbsoluteUri.GetHashCode ();
 
144
        }
 
145
 
 
146
        public string AbsoluteUri {
 
147
            get { return uri; }
 
148
        }
 
149
 
 
150
        public bool IsLocalPath {
 
151
            get {
 
152
                if (local_path_check == LocalPathCheck.NotPerformed) {
 
153
                    if (IsFile) {
 
154
                        local_path_check = LocalPathCheck.Yes;
 
155
                        return true;
 
156
                    } else {
 
157
                        local_path_check = LocalPathCheck.No;
 
158
                        return false;
 
159
                    }
 
160
                }
 
161
 
 
162
                return local_path_check == LocalPathCheck.Yes;
 
163
            }
 
164
        }
 
165
 
 
166
        public string AbsolutePath {
 
167
            get {
 
168
                if (local_path == null && IsLocalPath) {
 
169
                    local_path = UriToFilename (uri);
 
170
                }
 
171
 
 
172
                return local_path;
 
173
            }
 
174
        }
 
175
 
 
176
        public string LocalPath {
 
177
            get { return AbsolutePath; }
 
178
        }
 
179
 
 
180
        public string Scheme {
 
181
            get {
 
182
                if (scheme == null) {
 
183
                    scheme = uri.Substring (0, uri.IndexOf ("://"));
 
184
                }
 
185
 
 
186
                return scheme;
 
187
            }
 
188
        }
 
189
 
 
190
        public bool IsFile {
 
191
            get { return Scheme == System.Uri.UriSchemeFile; }
 
192
        }
 
193
 
 
194
#if WIN32
 
195
        [DllImport ("libglib-2.0-0.dll")]
 
196
        private static extern IntPtr g_filename_to_uri_utf8 (IntPtr filename, IntPtr hostname, IntPtr error);
 
197
 
 
198
        [DllImport ("libglib-2.0-0.dll")]
 
199
        private static extern IntPtr g_filename_from_uri_utf8 (IntPtr uri, IntPtr hostname, IntPtr error);
 
200
#else
 
201
        [DllImport ("libglib-2.0-0.dll")]
 
202
        private static extern IntPtr g_filename_to_uri (IntPtr filename, IntPtr hostname, IntPtr error);
 
203
 
 
204
        [DllImport ("libglib-2.0-0.dll")]
 
205
        private static extern IntPtr g_filename_from_uri (IntPtr uri, IntPtr hostname, IntPtr error);
 
206
#endif
 
207
    }
 
208
}