Compute lucene-style scores for our hits.
[beagle.git] / Util / gnome.cs
blobb53b1cd75cc41fdce4e947cd0d4cb14cc21ff882
1 //
2 // GNOME Dashboard
3 //
4 // gnome.cs: Bindings for some miscellaneous GNOME classes.
5 //
6 // Authors:
7 // Miguel de Icaza <miguel@ximian.com>
8 // Nat Friedman <nat@nat.ximian.com>
9 //
11 using Gtk;
12 using GLib;
13 using System;
14 using System.Runtime.InteropServices;
16 namespace Beagle.Util {
18 namespace VFS {
20 public class Mime {
22 [DllImport ("libgnomevfs-2")] extern static bool gnome_vfs_init ();
23 [DllImport ("libgnomevfs-2")] extern static IntPtr gnome_vfs_get_mime_type (string text_uri);
24 [DllImport ("libgnomevfs-2")] extern static IntPtr gnome_vfs_get_mime_type_for_data (byte[] data, int length);
25 [DllImport ("libgnomevfs-2")] extern static IntPtr gnome_vfs_mime_type_from_name_or_default (string filename, string defaultv);
27 static Mime ()
29 gnome_vfs_init ();
32 public static string GetMimeType (string text_path)
34 string full_uri = StringFu.PathToQuotedFileUri (text_path);
35 string mimeType = GLib.Marshaller.PtrToStringGFree (gnome_vfs_get_mime_type (full_uri));
36 return mimeType;
39 public static string GetMimeTypeFromData (byte[] buffer, int buffSize, string text_uri)
41 string guessedType = Marshal.PtrToStringAnsi (gnome_vfs_get_mime_type_for_data (buffer, buffSize));
42 if (text_uri != null
43 && (guessedType == "text/plain"
44 || guessedType == "application/octet-stream"
45 || guessedType == "application/zip"))
46 guessedType = Marshal.PtrToStringAnsi (gnome_vfs_mime_type_from_name_or_default (text_uri, guessedType));
47 return guessedType;
51 [Flags]
52 public enum FileInfoOptions {
53 DEFAULT = 0,
54 GET_MIME_TYPE = 1 << 0,
55 FORCE_FAST_MIME_TYPE = 1 << 1,
56 FORCE_SLOW_MIME_TYPE = 1 << 2,
57 FOLLOW_LINKS = 1 << 3
59 public enum Result {
60 OK,
61 ERROR_NOT_FOUND,
62 ERROR_GENERIC,
63 ERROR_INTERNAL,
64 ERROR_BAD_PARAMETERS,
65 ERROR_NOT_SUPPORTED,
66 ERROR_IO,
67 ERROR_CORRUPTED_DATA,
68 ERROR_WRONG_FORMAT,
69 ERROR_BAD_FILE,
70 ERROR_TOO_BIG,
71 ERROR_NO_SPACE,
72 ERROR_READ_ONLY,
73 ERROR_INVALID_URI,
74 ERROR_NOT_OPEN,
75 ERROR_INVALID_OPEN_MODE,
76 ERROR_ACCESS_DENIED,
77 ERROR_TOO_MANY_OPEN_FILES,
78 ERROR_EOF,
79 ERROR_NOT_A_DIRECTORY,
80 ERROR_IN_PROGRESS,
81 ERROR_INTERRUPTED,
82 ERROR_FILE_EXISTS,
83 ERROR_LOOP,
84 ERROR_NOT_PERMITTED,
85 ERROR_IS_DIRECTORY,
86 ERROR_NO_MEMORY,
87 ERROR_HOST_NOT_FOUND,
88 ERROR_INVALID_HOST_NAME,
89 ERROR_HOST_HAS_NO_ADDRESS,
90 ERROR_LOGIN_FAILED,
91 ERROR_CANCELLED,
92 ERROR_DIRECTORY_BUSY,
93 ERROR_DIRECTORY_NOT_EMPTY,
94 ERROR_TOO_MANY_LINKS,
95 ERROR_READ_ONLY_FILE_SYSTEM,
96 ERROR_NOT_SAME_FILE_SYSTEM,
97 ERROR_NAME_TOO_LONG,
98 ERROR_SERVICE_NOT_AVAILABLE,
99 ERROR_SERVICE_OBSOLETE,
100 ERROR_PROTOCOL_ERROR,
101 NUM_ERRORS
104 public class FileInfo {
105 internal IntPtr handle;
107 [DllImport ("libgnomevfs-2")]
108 extern static IntPtr gnome_vfs_file_info_new ();
110 [DllImport ("libgnomevfs-2")]
111 extern static void gnome_vfs_file_info_unref (IntPtr handle);
113 [DllImport ("libgnomevfs-2")]
114 extern static Result gnome_vfs_get_file_info_uri (IntPtr uri, IntPtr file_info, FileInfoOptions options);
116 [DllImport ("libgnomevfs-2")] extern static IntPtr gnome_vfs_uri_new (string text_uri);
117 [DllImport ("libgnomevfs-2")] extern static void gnome_vfs_uri_unref (IntPtr gnomeuri);
119 public FileInfo ()
123 ~FileInfo ()
125 gnome_vfs_file_info_unref (handle);
126 handle = (IntPtr) 0;
129 public FileInfo (string uri, FileInfoOptions options)
131 handle = gnome_vfs_file_info_new ();
133 Result r = Get (uri, options);
134 if (r != Result.OK)
135 throw new Exception ("VFS Error: " + r + ", URI: " + uri);
138 public Result Get (string uri, FileInfoOptions options)
140 IntPtr gnomeuri = gnome_vfs_uri_new (uri);
141 Result r = gnome_vfs_get_file_info_uri (gnomeuri, handle, options);
142 gnome_vfs_uri_unref (gnomeuri);
143 return r;
148 public class MimeApplication {
149 [Flags]
150 public enum ArgumentType {
151 URIS,
152 PATHS,
153 URIS_FOR_NON_FILES,
155 [StructLayout(LayoutKind.Sequential)]
156 public class Info {
157 public string id;
158 public string name;
159 public string command;
160 public bool can_open_multiple_files;
161 public ArgumentType expects_uris;
162 public IntPtr supported_uri_schemes;
163 public bool requires_terminal;
165 public IntPtr reserved1;
166 public IntPtr reserved2;
169 [DllImport ("libgnomevfs-2")]
170 extern static Info gnome_vfs_mime_get_default_application ( string mime_type );
171 [DllImport ("libgnomevfs-2")]
172 extern static void gnome_vfs_mime_application_free ( Info info );
174 public static void Exec (string mime_type, string uri)
176 Info info;
177 info = gnome_vfs_mime_get_default_application (mime_type);
178 if (info == null)
180 Console.WriteLine ("Unable to open " + uri);
181 // FIXME: Can we please stop hard coding Nautilus!?
182 System.Diagnostics.Process.Start ("nautilus", "\"" + uri + "\"");
183 } else {
184 System.Diagnostics.Process.Start (info.command, "\"" + uri + "\"");
186 //FIXME: Memory leak, causes crashes, dunno why...needs fixed
187 // gnome_vfs_mime_application_free (info);