2007-01-07 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / MimeIcon.cs
index 025660523b31fb1c3d2d95827da766676797f863..395a1caa127da0d8146af5a95634bd652d076677 100644 (file)
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2006 Alexander Olk
 //
 // Authors:
 //
-//  Alexander Olk      xenomorph2@onlinehome.de
+//  Alexander Olk      alex.olk@googlemail.com
 //
 
 // use
@@ -33,7 +33,6 @@ using System;
 using System.Reflection;
 using System.Drawing;
 using System.Collections;
-//using System.Collections.Specialized;
 using System.IO;
 using System.Text;
 using System.Runtime.InteropServices;
@@ -102,9 +101,9 @@ namespace System.Windows.Forms
                                                session = "GNOME";
                                }
                        } else
-                               session = "";
+                               session = String.Empty;
                        
-                       if (session == "GNOME") {
+                       if (Mime.MimeAvailable && session == "GNOME") {
                                SmallIcons.ImageSize = new Size (24, 24);
                                LargeIcons.ImageSize = new Size (48, 48);
                                
@@ -129,25 +128,28 @@ namespace System.Windows.Forms
                public static int GetIconIndexForFile (string full_filename)
                {
                        lock (lock_object) {
-                               string mime_type = Mime.GetMimeTypeForFile (full_filename);
-                               
                                if (platform == EPlatformHandler.Default) {
-                                       if (mime_type == "inode/directory") {
-                                               return (int)MimeIconIndex ["inode/directory"];
-                                       } else {
-                                               return (int)MimeIconIndex ["unknown/unknown"];
-                                       }
+                                       return (int)MimeIconIndex ["unknown/unknown"];
                                }
                                
+                               string mime_type = Mime.GetMimeTypeForFile (full_filename);
+                               
                                object oindex = GetIconIndex (mime_type);
                                
                                // not found, add it
                                if (oindex == null) {
-                                       oindex = platformMimeHandler.AddAndGetIconIndex (full_filename, mime_type);
+                                       int index = full_filename.IndexOf (':');
                                        
-                                       // sanity check
-                                       if (oindex == null)
+                                       if (index > 1) {
                                                oindex = MimeIconIndex ["unknown/unknown"];
+                                               
+                                       } else {
+                                               oindex = platformMimeHandler.AddAndGetIconIndex (full_filename, mime_type);
+                                               
+                                               // sanity check
+                                               if (oindex == null)
+                                                       oindex = MimeIconIndex ["unknown/unknown"];
+                                       }
                                }
                                
                                return (int)oindex;
@@ -274,10 +276,9 @@ namespace System.Windows.Forms
                        MimeIconEngine.AddIconByImage ("desktop/desktop",  ResourceImageLoader.Get ("user-desktop.png"));
                        MimeIconEngine.AddIconByImage ("directory/home",  ResourceImageLoader.Get ("user-home.png"));
                        
-                       // fix
-                       MimeIconEngine.AddIconByImage ("network/network",  ResourceImageLoader.Get ("document-open.png"));
+                       MimeIconEngine.AddIconByImage ("network/network",  ResourceImageLoader.Get ("folder-remote.png"));
                        MimeIconEngine.AddIconByImage ("recently/recently",  ResourceImageLoader.Get ("document-open.png"));
-                       MimeIconEngine.AddIconByImage ("workplace/workplace",  ResourceImageLoader.Get ("document-open.png"));
+                       MimeIconEngine.AddIconByImage ("workplace/workplace",  ResourceImageLoader.Get ("computer.png"));
                        
                        return MimeExtensionHandlerStatus.OK; // return always ok
                }
@@ -320,6 +321,32 @@ namespace System.Windows.Forms
                        
                        Image image = GnomeUtil.GetIcon (name, 48);
                        
+                       if (image == null) {
+                               if (internal_mime_type == "unknown/unknown")
+                                       image = ResourceImageLoader.Get ("text-x-generic.png");
+                               else
+                               if (internal_mime_type == "inode/directory")
+                                       image = ResourceImageLoader.Get ("folder.png");
+                               else
+                               if (internal_mime_type == "directory/home")
+                                       image = ResourceImageLoader.Get ("user-home.png");
+                               else
+                               if (internal_mime_type == "desktop/desktop")
+                                       image = ResourceImageLoader.Get ("user-desktop.png");
+                               else
+                               if (internal_mime_type == "recently/recently")
+                                       image = ResourceImageLoader.Get ("document-open.png");
+                               else
+                               if (internal_mime_type == "workplace/workplace")
+                                       image = ResourceImageLoader.Get ("computer.png");
+                               else
+                               if (internal_mime_type == "network/network" || internal_mime_type == "nfs/nfs" || internal_mime_type == "smb/smb")
+                                       image = ResourceImageLoader.Get ("folder-remote.png");
+                               else
+                               if (internal_mime_type == "harddisk/harddisk" || internal_mime_type == "cdrom/cdrom" || internal_mime_type == "removable/removable")
+                                       image = ResourceImageLoader.Get ("text-x-generic.png");
+                       }
+                       
                        index = MimeIconEngine.SmallIcons.Images.Add (image, Color.Transparent);
                        MimeIconEngine.LargeIcons.Images.Add (image, Color.Transparent);
                        
@@ -357,31 +384,39 @@ namespace System.Windows.Forms
        
        internal class GnomeUtil
        {
-               [DllImport("librsvg-2.so")]
+               const string libgdk = "libgdk-x11-2.0.so.0";
+               const string libgdk_pixbuf = "libgdk_pixbuf-2.0.so.0";
+               const string libgtk = "libgtk-x11-2.0.so.0";
+               const string libglib = "libglib-2.0.so.0";
+               const string libgobject = "libgobject-2.0.so.0";
+               const string libgnomeui = "libgnomeui-2.so.0";
+               const string librsvg = "librsvg-2.so.2";
+               
+               [DllImport(librsvg)]
                static extern IntPtr rsvg_pixbuf_from_file_at_size (string file_name, int  width, int  height, out IntPtr error);
                
-               [DllImport("libgdk-x11-2.0.so.0")]
+               [DllImport(libgdk_pixbuf)]
                static extern bool gdk_pixbuf_save_to_buffer (IntPtr pixbuf, out IntPtr buffer, out UIntPtr buffer_size, string type, out IntPtr error, IntPtr option_dummy);
                
-               [DllImport("libglib-2.0.so")]
+               [DllImport(libglib)]
                static extern void g_free (IntPtr mem);
                
-               [DllImport("libgdk-x11-2.0.so.0")]
-               static extern bool gdk_init_check (out int argc, string argv);
+               [DllImport(libgdk)]
+               static extern bool gdk_init_check (IntPtr argc, IntPtr argv);
                
-               [DllImport("libgobject-2.0.so")]
+               [DllImport(libgobject)]
                static extern void g_object_unref (IntPtr nativeObject);
                
-               [DllImport("libgnomeui-2.so.0")]
+               [DllImport(libgnomeui)]
                static extern string gnome_icon_lookup (IntPtr icon_theme, IntPtr thumbnail_factory, string file_uri, string custom_icon, IntPtr file_info, string mime_type, GnomeIconLookupFlags flags, IntPtr result);
                
-               [DllImport("libgtk-x11-2.0.so")]
+               [DllImport(libgtk)]
                static extern IntPtr gtk_icon_theme_get_default ();
                
-               [DllImport("libgtk-x11-2.0.so")]
+               [DllImport(libgtk)]
                static extern IntPtr gtk_icon_theme_load_icon (IntPtr icon_theme, string icon_name, int size, GtkIconLookupFlags flags, out IntPtr error);
                
-               [DllImport("libgtk-x11-2.0.so")]
+               [DllImport(libgtk)]
                static extern bool gtk_icon_theme_has_icon (IntPtr icon_theme, string icon_name);
                
                enum GnomeIconLookupFlags
@@ -405,10 +440,7 @@ namespace System.Windows.Forms
                
                static void Init ()
                {
-                       int argc = 0;
-                       string argv = "";
-                       
-                       gdk_init_check (out argc, argv);
+                       gdk_init_check (IntPtr.Zero, IntPtr.Zero);
                        
                        inited = true;