New e-d-s backend which indexes all local addressbooks and calendars.
[beagle.git] / Util / Vfs.cs
blob0c50f446baddc19345ed724070330d340d55224a
1 /*
2 * Vfs.cs (from MonoTagEditor)
4 * Copyright (C) 2003, Mariano Cano PĂ©rez <mariano.cano@hispalinux.es>
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
9 * 1.Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * 2.Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 * 3.The name of the author may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
24 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 using System;
28 using Gtk;
29 using GLib;
30 using System.Runtime.InteropServices;
32 namespace Beagle.Util {
34 public enum GnomeVFSMimeApplicationArgumentType
36 Uris,
37 Path,
38 UrisForNonFiles
41 [StructLayout(LayoutKind.Sequential)]
42 public struct GnomeVFSMimeApplication
44 public string id;
45 public string name;
46 public string command;
47 public bool can_open_multiple_files;
48 public GnomeVFSMimeApplicationArgumentType expects_uris;
49 //public List supported_uri_schemes;
50 private IntPtr supported_uri_schemes;
51 public bool requires_terminal;
53 public IntPtr reserved1;
54 public IntPtr reserved2;
56 public static GnomeVFSMimeApplication Zero = new GnomeVFSMimeApplication ();
58 public static GnomeVFSMimeApplication New (IntPtr raw)
60 if(raw == IntPtr.Zero)
62 return GnomeVFSMimeApplication.Zero;
64 GnomeVFSMimeApplication self = new GnomeVFSMimeApplication();
65 self = (GnomeVFSMimeApplication) Marshal.PtrToStructure (raw, self.GetType ());
66 return self;
69 //Fixme: Create the supported uri schemes struct
70 public List SupportedUriSchemes {
71 get {
72 List list = new List (supported_uri_schemes);
73 return list;
77 public static bool operator == (GnomeVFSMimeApplication a, GnomeVFSMimeApplication b)
79 return a.Equals (b);
82 public static bool operator != (GnomeVFSMimeApplication a, GnomeVFSMimeApplication b)
84 return ! a.Equals (b);
87 public override bool Equals (object o)
89 //if (!(o is GnomeVFSMimeApplication))
90 // return false;
91 return base.Equals(o) ;
94 public override int GetHashCode ()
96 return base.GetHashCode ();
100 public enum GnomeIconLookupFlags
102 None,
103 EmbeddingText,
104 SmallImages
107 public enum GnomeIconLookupResultsFlags
109 None,
110 Thumbnail
113 public class GnomeIconLookup{
115 [DllImport("libgnomeui-2")]
116 static extern IntPtr gnome_icon_lookup (IntPtr icon_theme,
117 IntPtr thumbnail_factory,
118 string uri,
119 string custim_icon,
120 IntPtr file_info,
121 string mime_type,
122 GnomeIconLookupFlags flags,
123 ref GnomeIconLookupResultsFlags result);
124 [DllImport("libgnomeui-2")]
125 static extern IntPtr gnome_icon_theme_lookup_icon (IntPtr icon_theme,
126 string icon_name,
127 IconSize icon_size,
128 ref IntPtr icon_data,
129 ref int base_size);
131 [DllImport("libgnomeui-2")]
132 static extern void gnome_icon_data_free (IntPtr icon_data);
134 [DllImport("libgnomeui-2")]
135 static extern IntPtr gnome_icon_theme_new ();
137 [DllImport("libgnomevfs-2")]
138 static extern IntPtr gnome_vfs_mime_type_from_name (string mime_type);
140 [DllImport("libgnomevfs-2")]
141 static extern IntPtr gnome_vfs_get_file_mime_type (string filename,
142 IntPtr optional_stat_info,
143 bool suffix_only);
145 [DllImport("libgnomevfs-2")]
146 static extern IntPtr gnome_vfs_application_registry_get_applications(string mime_type);
148 [DllImport("libgnomevfs-2")]
149 static extern IntPtr gnome_vfs_mime_get_default_application(string mime_type);
151 static IntPtr icon_theme = IntPtr.Zero;
153 public static string LookupMimeIcon(string mime,IconSize size)
155 string icon_name=String.Empty;
156 string icon_path=String.Empty;
157 GnomeIconLookupResultsFlags result=0;
158 IntPtr icon_data=IntPtr.Zero;
159 int base_size=0;
161 if (icon_theme == IntPtr.Zero) {
162 icon_theme = gnome_icon_theme_new ();
165 if(icon_theme==IntPtr.Zero)
167 return String.Empty;
170 icon_name=GLib.Marshaller.PtrToStringGFree (gnome_icon_lookup(
171 icon_theme,
172 IntPtr.Zero,
173 String.Empty,
174 String.Empty,
175 IntPtr.Zero,
176 mime,
177 GnomeIconLookupFlags.None,
178 ref result));
180 if(icon_name.Length>0)
182 icon_path=GLib.Marshaller.PtrToStringGFree (gnome_icon_theme_lookup_icon (
183 icon_theme,
184 icon_name,
185 size,
186 ref icon_data,
187 ref base_size));
189 return icon_path;
192 public static string LookupFileIcon(string file,IconSize size)
194 string mime = Marshal.PtrToStringAnsi (gnome_vfs_mime_type_from_name(file));
196 if(mime.Length>0)
197 return LookupMimeIcon(mime,size);
199 return String.Empty;
202 public static string GetMimeType(string file)
204 return Marshal.PtrToStringAnsi (gnome_vfs_mime_type_from_name(file));
207 public static string GetFileMimeType (string file)
209 return Marshal.PtrToStringAnsi (gnome_vfs_get_file_mime_type (file, (IntPtr)null, false));
212 public static GLib.List GetApplications(string mime_type)
214 IntPtr raw_ret = gnome_vfs_application_registry_get_applications(mime_type);
215 GLib.List app = new GLib.List(raw_ret);
216 return app;
219 public static GnomeVFSMimeApplication GetDefaultAction(string mime_type)
221 IntPtr ptr = gnome_vfs_mime_get_default_application(mime_type);
222 GnomeVFSMimeApplication ret = GnomeVFSMimeApplication.New(ptr);
223 return ret;