4 // gnome.cs: Bindings for some miscellaneous GNOME classes.
7 // Miguel de Icaza <miguel@ximian.com>
8 // Nat Friedman <nat@nat.ximian.com>
14 using System
.Runtime
.InteropServices
;
16 namespace Beagle
.Util
{
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
);
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
));
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
));
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
));
52 public enum FileInfoOptions
{
54 GET_MIME_TYPE
= 1 << 0,
55 FORCE_FAST_MIME_TYPE
= 1 << 1,
56 FORCE_SLOW_MIME_TYPE
= 1 << 2,
75 ERROR_INVALID_OPEN_MODE
,
77 ERROR_TOO_MANY_OPEN_FILES
,
79 ERROR_NOT_A_DIRECTORY
,
88 ERROR_INVALID_HOST_NAME
,
89 ERROR_HOST_HAS_NO_ADDRESS
,
93 ERROR_DIRECTORY_NOT_EMPTY
,
95 ERROR_READ_ONLY_FILE_SYSTEM
,
96 ERROR_NOT_SAME_FILE_SYSTEM
,
98 ERROR_SERVICE_NOT_AVAILABLE
,
99 ERROR_SERVICE_OBSOLETE
,
100 ERROR_PROTOCOL_ERROR
,
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
);
125 gnome_vfs_file_info_unref (handle
);
129 public FileInfo (string uri
, FileInfoOptions options
)
131 handle
= gnome_vfs_file_info_new ();
133 Result r
= Get (uri
, options
);
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
);
148 public class MimeApplication
{
150 public enum ArgumentType
{
155 [StructLayout(LayoutKind
.Sequential
)]
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
)
177 info
= gnome_vfs_mime_get_default_application (mime_type
);
180 Console
.WriteLine ("Unable to open " + uri
);
181 // FIXME: Can we please stop hard coding Nautilus!?
182 System
.Diagnostics
.Process
.Start ("nautilus", "\"" + uri
+ "\"");
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);