// 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
//
-// short "how to" if you want to add an other platform handler, etc:
-// - first add mime type names and icon names (best is without extension) to MimeIconEngine, for example:
-// MimeIconEngine.AddMimeTypeAndIconName( "inode/directory", "gnome-fs-directory" );
-// - next add the icon name (the same as used in AddMimeTypeAndIconName) and the full filename, for example:
-// MimeIconEngine.AddIcon( "gnome-fs-directory", "/opt/gnome/share/icons/gnome/48x48/filesystems/gnome-fs-directory.png" );
-// AddIcon adds the icon to the image lists SmallIconList and LargeIconList
-// - provide always a "unknown/unknown" 'mime type' with a default icon for unkown mime types,
-// "desktop/desktop" 'mime type' for the desktop icon, "directory/home" 'mime type for the home dir of the user and so on
-// (look at the default platform handler)
-//
// use
// public static int GetIconIndexForFile( string full_filename )
// public static int GetIconIndexForMimeType( string mime_type )
// to get the image index in MimeIconEngine.SmallIcons and MimeIconEngine.LargeIcons
-// use
-// public static Image GetIconForMimeTypeAndSize( string mime_type, Size size )
-// to get the image itself for a mime type with a specific size
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;
+using System.Xml;
namespace System.Windows.Forms
{
internal enum MimeExtensionHandlerStatus
{
OK,
- NO_KDEGLOBALS,
- NO_GNOMECONFIG,
- NO_ICONS,
- NO_MIMELNK
+ ERROR
}
internal enum EPlatformHandler
{
Default,
- KDE,
GNOME
- // Win, Mac OSX...
+ }
+
+ internal class ResourceImageLoader
+ {
+ static Assembly assembly = typeof (ResourceImageLoader).Assembly;
+
+ static internal Bitmap Get (string name)
+ {
+ using (Stream stream = assembly.GetManifestResourceStream (name)) {
+ if (stream == null) {
+ Console.WriteLine ("Failed to read {0}", name);
+ return null;
+ }
+
+ return new Bitmap (stream);
+ }
+ }
}
internal class MimeIconEngine
public static ImageList SmallIcons = new ImageList();
public static ImageList LargeIcons = new ImageList();
- private static PlatformMimeIconHandler platformMimeHandler = null;
-
private static EPlatformHandler platform = EPlatformHandler.Default;
- private static Hashtable MimeTypeIconIndexHash = new Hashtable();
+ internal static Hashtable MimeIconIndex = new Hashtable ();
- private static NameValueCollection IconNameMimeTypeNameValueCollection = new NameValueCollection();
-
- private static StringCollection added_icons = new StringCollection();
+ private static PlatformMimeIconHandler platformMimeHandler = null;
private static object lock_object = new Object();
- static MimeIconEngine( )
+ static MimeIconEngine ()
{
- // add some more aliases, kde for example uses other mime type names for some mime types...
- MimeGenerated.Aliases.Add( "application/x-compressed-tar", "application/x-tgz" );
- MimeGenerated.Aliases.Add( "application/x-bzip-compressed-tar", "application/x-tbz" );
- MimeGenerated.Aliases.Add( "application/zip", "application/x-zip" );
- MimeGenerated.Aliases.Add( "text/x-patch", "text/x-diff" );
-
SmallIcons.ColorDepth = ColorDepth.Depth32Bit;
SmallIcons.TransparentColor = Color.Transparent;
LargeIcons.ColorDepth = ColorDepth.Depth32Bit;
LargeIcons.TransparentColor = Color.Transparent;
- string session = Environment.GetEnvironmentVariable( "DESKTOP_SESSION" );
+ string session = Environment.GetEnvironmentVariable ("DESKTOP_SESSION");
- if ( session != null )
- {
- session = session.ToUpper( );
+ if (session != null) {
+ session = session.ToUpper ();
- if ( session == "DEFAULT" )
- {
- string helper = Environment.GetEnvironmentVariable( "KDE_FULL_SESSION" );
+ if (session == "DEFAULT") {
+ string helper = Environment.GetEnvironmentVariable ("GNOME_DESKTOP_SESSION_ID");
- if ( helper != null )
- session = "KDE";
- else
- {
- helper = Environment.GetEnvironmentVariable( "GNOME_DESKTOP_SESSION_ID" );
-
- if ( helper != null )
- session = "GNOME";
- }
+ if (helper != null)
+ session = "GNOME";
}
- }
- else
- session = "";
-
- Console.WriteLine( "Desktop session is: " + session );
+ } else
+ session = String.Empty;
- if ( session == "KDE" )
- {
- SmallIcons.ImageSize = new Size( 24, 24 );
- LargeIcons.ImageSize = new Size( 48, 48 );
+ if (Mime.MimeAvailable && session == "GNOME") {
+ SmallIcons.ImageSize = new Size (24, 24);
+ LargeIcons.ImageSize = new Size (48, 48);
- platformMimeHandler = new KdeHandler( );
- if ( platformMimeHandler.Start( ) == MimeExtensionHandlerStatus.OK )
- {
- Console.WriteLine( "Kde icons ready..." );
- platform = EPlatformHandler.KDE;
- }
- else // fallback to default
- {
- MimeIconEngine.LargeIcons.Images.Clear( );
- MimeIconEngine.SmallIcons.Images.Clear( );
- platformMimeHandler = new PlatformDefaultHandler( );
- platformMimeHandler.Start( );
- }
- }
- else
- if ( session == "GNOME" )
- {
- SmallIcons.ImageSize = new Size( 24, 24 );
- LargeIcons.ImageSize = new Size( 48, 48 );
-
- platformMimeHandler = new GnomeHandler( );
- if ( platformMimeHandler.Start( ) == MimeExtensionHandlerStatus.OK )
- {
- Console.WriteLine( "Gnome icons ready..." );
+ platformMimeHandler = new GnomeHandler ();
+ if (platformMimeHandler.Start () == MimeExtensionHandlerStatus.OK) {
platform = EPlatformHandler.GNOME;
+ } else {
+ MimeIconEngine.LargeIcons.Images.Clear ();
+ MimeIconEngine.SmallIcons.Images.Clear ();
+ platformMimeHandler = new PlatformDefaultHandler ();
+ platformMimeHandler.Start ();
}
- else // fallback to default
- {
- MimeIconEngine.LargeIcons.Images.Clear( );
- MimeIconEngine.SmallIcons.Images.Clear( );
- platformMimeHandler = new PlatformDefaultHandler( );
- platformMimeHandler.Start( );
- }
- }
- else
- {
- SmallIcons.ImageSize = new Size( 16, 16 );
- LargeIcons.ImageSize = new Size( 48, 48 );
+ } else {
+ SmallIcons.ImageSize = new Size (16, 16);
+ LargeIcons.ImageSize = new Size (48, 48);
- platformMimeHandler = new PlatformDefaultHandler( );
- platformMimeHandler.Start( );
+ platformMimeHandler = new PlatformDefaultHandler ();
+ platformMimeHandler.Start ();
}
-
- IconNameMimeTypeNameValueCollection = null;
- added_icons = null;
}
- public static int GetIconIndexForFile( string full_filename )
+ 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)MimeTypeIconIndexHash[ "inode/directory" ];
- }
- else
- {
- return (int)MimeTypeIconIndexHash[ "unknown/unknown" ];
- }
+ lock (lock_object) {
+ if (platform == EPlatformHandler.Default) {
+ return (int)MimeIconIndex ["unknown/unknown"];
}
- object oindex = GetIconIndex( mime_type );
+ string mime_type = Mime.GetMimeTypeForFile (full_filename);
- if ( oindex == null )
- oindex = MimeTypeIconIndexHash[ "unknown/unknown" ];
+ object oindex = GetIconIndex (mime_type);
+
+ // not found, add it
+ if (oindex == null) {
+ int index = full_filename.IndexOf (':');
+
+ 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;
}
}
- public static int GetIconIndexForMimeType( string mime_type )
+ public static int GetIconIndexForMimeType (string mime_type)
{
- lock ( lock_object )
- {
- if ( platform == EPlatformHandler.Default )
- {
- if ( mime_type == "inode/directory" )
- {
- return (int)MimeTypeIconIndexHash[ "inode/directory" ];
- }
- else
- {
- return (int)MimeTypeIconIndexHash[ "unknown/unknown" ];
+ lock (lock_object) {
+ if (platform == EPlatformHandler.Default) {
+ if (mime_type == "inode/directory") {
+ return (int)MimeIconIndex ["inode/directory"];
+ } else {
+ return (int)MimeIconIndex ["unknown/unknown"];
}
}
- object oindex = GetIconIndex( mime_type );
+ object oindex = GetIconIndex (mime_type);
- if ( oindex == null )
- oindex = MimeTypeIconIndexHash[ "unknown/unknown" ];
+ // not found, add it
+ if (oindex == null) {
+ oindex = platformMimeHandler.AddAndGetIconIndex (mime_type);
+
+ // sanity check
+ if (oindex == null)
+ oindex = MimeIconIndex ["unknown/unknown"];
+ }
return (int)oindex;
}
}
- public static Image GetIconForMimeTypeAndSize( string mime_type, Size size )
+ public static Image GetIconForMimeTypeAndSize (string mime_type, Size size)
{
- lock ( lock_object )
- {
- object oindex = GetIconIndex( mime_type );
+ lock (lock_object) {
+ object oindex = GetIconIndex (mime_type);
- if ( oindex == null )
- oindex = MimeTypeIconIndexHash[ "unknown/unknown" ];
-
- Bitmap bmp = new Bitmap( LargeIcons.Images[ (int)oindex ], size );
+ Bitmap bmp = new Bitmap (LargeIcons.Images [(int)oindex], size);
return bmp;
}
}
- internal static void AddIcon( string name, string fullname )
- {
- if ( !CheckIfIconIsNeeded( name ) )
- return;
-
- if ( added_icons.Contains( name ) )
- return;
-
- added_icons.Add( name );
-
- Bitmap bmp = new Bitmap( fullname );
-
- int index = SmallIcons.Images.Add( bmp, Color.Transparent );
- LargeIcons.Images.Add( bmp, Color.Transparent );
-
- AddMimeTypeIconIndexHash( name, index );
- }
-
- private static bool CheckIfIconIsNeeded( string name )
+ internal static void AddIconByImage (string mime_type, Image image)
{
- string mime_types = IconNameMimeTypeNameValueCollection[ name ];
-
- if ( mime_types != null )
- return true;
+ int index = SmallIcons.Images.Add (image, Color.Transparent);
+ LargeIcons.Images.Add (image, Color.Transparent);
- return false;
+ MimeIconIndex.Add (mime_type, index);
}
- internal static void AddMimeTypeIconIndexHash( string name, int index )
- {
- string mime_type = IconNameMimeTypeNameValueCollection[ name ];
-
- if ( mime_type == null )
- return;
-
- string[] split = mime_type.Split( new char[] { ',' } );
-
- foreach ( string s in split )
- {
- if ( MimeTypeIconIndexHash.ContainsKey( s ) )
- continue;
-
- MimeTypeIconIndexHash.Add( s, index );
- }
- }
-
- internal static void AddIconByImage( string name, Image image )
- {
- int index = SmallIcons.Images.Add( image, Color.Transparent );
- LargeIcons.Images.Add( image, Color.Transparent );
-
- AddMimeTypeIconIndexHash( name, index );
- }
-
- internal static void AddMimeTypeAndIconName( string mimetype, string iconname )
- {
- if ( iconname.Equals( String.Empty ) )
- return;
-
- IconNameMimeTypeNameValueCollection.Add( iconname, mimetype );
- }
-
- private static object GetIconIndex( string mime_type )
+ private static object GetIconIndex (string mime_type)
{
object oindex = null;
- if ( mime_type != null )
- {
+ if (mime_type != null) {
// first check if mime_type is available in the mimetype/icon hashtable
- oindex = MimeTypeIconIndexHash[ mime_type ];
+ oindex = MimeIconIndex [mime_type];
- if ( oindex == null )
- {
+ if (oindex == null) {
// it is not available, check if an alias exist for mime_type
- string alias = Mime.GetMimeAlias( mime_type );
+ string alias = Mime.GetMimeAlias (mime_type);
- if ( alias != null )
- {
- string[] split = alias.Split( new char[] { ',' } );
+ if (alias != null) {
+ string[] split = alias.Split (new char [] { ',' });
- foreach ( string s in split )
- {
- oindex = MimeTypeIconIndexHash[ s ];
+ for (int i = 0; i < split.Length; i++) {
+ oindex = MimeIconIndex [split [i]];
- if ( oindex != null )
- break;
+ if (oindex != null)
+ return oindex;
}
}
// if oindex is still null check if mime_type is a sub class of an other mime type
- if ( oindex == null )
- {
- string sub_class = MimeGenerated.SubClasses[ mime_type ];
+ string sub_class = Mime.SubClasses [mime_type];
+
+ if (sub_class != null) {
+ oindex = MimeIconIndex [sub_class];
- if ( sub_class != null )
- oindex = MimeTypeIconIndexHash[ sub_class ];
+ if (oindex != null)
+ return oindex;
}
+
+ // last check, see if we find an entry for the main mime type class
+ string mime_class_main = mime_type.Substring (0, mime_type.IndexOf ('/'));
+ return MimeIconIndex [mime_class_main];
}
}
internal abstract class PlatformMimeIconHandler
{
- protected StringCollection mime_paths = new StringCollection();
-
- protected StringCollection icon_paths = new StringCollection();
-
- protected string icon_theme = "";
-
protected MimeExtensionHandlerStatus mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.OK;
- public MimeExtensionHandlerStatus MimeExtensionHandlerStatus
- {
+ public MimeExtensionHandlerStatus MimeExtensionHandlerStatus {
get {
return mimeExtensionHandlerStatus;
}
}
- public abstract MimeExtensionHandlerStatus Start( );
+ public abstract MimeExtensionHandlerStatus Start ();
+
+ public virtual object AddAndGetIconIndex (string filename, string mime_type)
+ {
+ return null;
+ }
- // check, if icon, mime, etc., directories exist
- protected virtual bool CheckPlatformDirectories( )
+ public virtual object AddAndGetIconIndex (string mime_type)
{
- return true;
+ return null;
}
}
internal class PlatformDefaultHandler : PlatformMimeIconHandler
{
- public override MimeExtensionHandlerStatus Start( )
+ public override MimeExtensionHandlerStatus Start ()
{
- MimeIconEngine.AddMimeTypeAndIconName( "unknown/unknown", "paper" );
- MimeIconEngine.AddMimeTypeAndIconName( "inode/directory", "folder" );
- MimeIconEngine.AddMimeTypeAndIconName( "desktop/desktop", "desktop" );
- MimeIconEngine.AddMimeTypeAndIconName( "directory/home", "folder_with_paper" );
- MimeIconEngine.AddMimeTypeAndIconName( "network/network", "monitor-planet" );
-
- MimeIconEngine.AddIconByImage( "folder", (Image)Locale.GetResource( "folder" ) );
- MimeIconEngine.AddIconByImage( "paper", (Image)Locale.GetResource( "paper" ) );
- MimeIconEngine.AddIconByImage( "desktop", (Image)Locale.GetResource( "desktop" ) );
- MimeIconEngine.AddIconByImage( "folder_with_paper", (Image)Locale.GetResource( "folder_with_paper" ) );
- MimeIconEngine.AddIconByImage( "monitor-planet", (Image)Locale.GetResource( "monitor-planet" ) );
+ MimeIconEngine.AddIconByImage ("inode/directory", ResourceImageLoader.Get ("folder.png"));
+ MimeIconEngine.AddIconByImage ("unknown/unknown", ResourceImageLoader.Get ("text-x-generic.png"));
+ MimeIconEngine.AddIconByImage ("desktop/desktop", ResourceImageLoader.Get ("user-desktop.png"));
+ MimeIconEngine.AddIconByImage ("directory/home", ResourceImageLoader.Get ("user-home.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 ("computer.png"));
return MimeExtensionHandlerStatus.OK; // return always ok
}
}
- internal class KdeHandler : PlatformMimeIconHandler
+ internal class GnomeHandler : PlatformMimeIconHandler
{
- string full_kdegloabals_filename = Environment.GetFolderPath( Environment.SpecialFolder.Personal )
- + "/"
- + ".kde/share/config/kdeglobals";
-
- public override MimeExtensionHandlerStatus Start( )
+ public override MimeExtensionHandlerStatus Start ()
{
- if ( !ReadKdeglobals( ) )
- return mimeExtensionHandlerStatus;
+ CreateUIIcons ();
- if ( !CheckPlatformDirectories( ) )
- return mimeExtensionHandlerStatus;
-
- // check if the theme is svg only
- // if true, use theme "default.kde"
- // don't know if that is available in every linux distribution
- // MWF has no svg support yet (cairo's libsvg!?!)
- if ( SVGOnly( ) )
- icon_theme = "default.kde";
-
- ReadMimetypes( );
-
- ReadIcons( );
-
- return mimeExtensionHandlerStatus;
+ return MimeExtensionHandlerStatus.OK;
}
- private bool SVGOnly( )
+ private void CreateUIIcons ()
{
- // check only the first path in icon_paths
- if ( icon_paths.Count > 0 )
- {
- string icon_path = icon_paths[ 0 ] + icon_theme;
- string[] dirs = Directory.GetDirectories( icon_path );
-
- if ( dirs.Length == 1 && dirs[ 0 ] == "scalable" )
- return true;
- }
-
- return false;
+ AddGnomeIcon ("unknown/unknown", "gnome-fs-regular");
+ AddGnomeIcon ("inode/directory", "gnome-fs-directory");
+ AddGnomeIcon ("directory/home", "gnome-fs-home");
+ AddGnomeIcon ("desktop/desktop", "gnome-fs-desktop");
+ AddGnomeIcon ("recently/recently", "gnome-fs-directory-accept");
+ AddGnomeIcon ("workplace/workplace", "gnome-fs-client");
+
+ AddGnomeIcon ("network/network", "gnome-fs-network");
+ AddGnomeIcon ("nfs/nfs", "gnome-fs-nfs");
+ AddGnomeIcon ("smb/smb", "gnome-fs-smb");
+
+ AddGnomeIcon ("harddisk/harddisk", "gnome-dev-harddisk");
+ AddGnomeIcon ("cdrom/cdrom", "gnome-dev-cdrom");
+ AddGnomeIcon ("removable/removable", "gnome-dev-removable");
}
- protected override bool CheckPlatformDirectories( )
+ private void AddGnomeIcon (string internal_mime_type, string name)
{
- bool icons_found = false;
-
- // default icon dirs
- if ( Directory.Exists( "/opt/kde3/share/icons/default.kde" ) )
- {
- icon_paths.Add( "/opt/kde3/share/icons" + "/" );
- icons_found = true;
- }
- else
- if ( Directory.Exists( "/usr/share/icons/default.kde" ) )
- {
- icon_paths.Add( "/usr/share/icons" + "/" );
- icons_found = true;
- }
- else
- if ( Directory.Exists( "/usr/local/share/icons/default.kde" ) )
- {
- icon_paths.Add( "/usr/local/share/icons" + "/" );
- icons_found = true;
- }
- else
- if ( !icons_found )
- {
- mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.NO_ICONS;
- return false;
- }
-
- bool mimelnk_found = false;
+ int index = -1;
- if ( Directory.Exists( "/usr/share/mimelnk" ) )
- {
- mime_paths.Add( "/usr/share/mimelnk" + "/" );
- mimelnk_found = true;
+ if (MimeIconEngine.MimeIconIndex.ContainsKey (internal_mime_type)) {
+ return;
}
- if ( Directory.Exists( "/usr/local/share/mimelnk" ) )
- {
- mime_paths.Add( "/usr/local/share/mimelnk" + "/" );
- mimelnk_found = true;
- }
+ Image image = GnomeUtil.GetIcon (name, 48);
- if ( Directory.Exists( "/opt/kde3/share/mimelnk" ) )
- {
- mime_paths.Add( "/opt/kde3/share/mimelnk" + "/" );
- mimelnk_found = true;
+ 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");
}
- if ( !mimelnk_found )
- {
- mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.NO_MIMELNK;
- return false;
- }
+ index = MimeIconEngine.SmallIcons.Images.Add (image, Color.Transparent);
+ MimeIconEngine.LargeIcons.Images.Add (image, Color.Transparent);
- return true;
- }
-
- private void ReadIcons( )
- {
- foreach ( string icon_path_in in icon_paths )
- {
- string icon_path = icon_path_in + icon_theme + "/48x48";
-
- string[] directories = Directory.GetDirectories( icon_path );
-
- foreach ( string d in directories )
- {
- DirectoryInfo di = new DirectoryInfo( d );
-
- FileInfo[] fileinfo = di.GetFiles( );
-
- foreach ( FileInfo fi in fileinfo )
- {
- string name = Path.GetFileNameWithoutExtension( fi.Name );
-
- MimeIconEngine.AddIcon( name, fi.FullName );
- }
- }
- }
+ MimeIconEngine.MimeIconIndex.Add (internal_mime_type, index);
}
- private void ReadMimetypes( )
+ public override object AddAndGetIconIndex (string filename, string mime_type)
{
- MimeIconEngine.AddMimeTypeAndIconName( "unknown/unknown", "unknown" );
- MimeIconEngine.AddMimeTypeAndIconName( "desktop/desktop", "desktop" );
- MimeIconEngine.AddMimeTypeAndIconName( "directory/home", "folder_home" );
- MimeIconEngine.AddMimeTypeAndIconName( "network/network", "network" );
-
- foreach ( string mime_path in mime_paths )
- {
- string[] directories = Directory.GetDirectories( mime_path );
-
- foreach ( string d in directories )
- {
- string[] files = Directory.GetFiles( d );
-
- foreach ( string f in files )
- {
- ReadDotDesktop( f );
- }
- }
- }
+ int index = -1;
+
+ Image image = GnomeUtil.GetIcon (filename, mime_type, 48);
+
+ index = MimeIconEngine.SmallIcons.Images.Add (image, Color.Transparent);
+ MimeIconEngine.LargeIcons.Images.Add (image, Color.Transparent);
+
+ MimeIconEngine.MimeIconIndex.Add (mime_type, index);
+
+ return index;
}
- private void ReadDotDesktop( string filename )
+ public override object AddAndGetIconIndex (string mime_type)
{
- StreamReader sr = new StreamReader( filename );
+ int index = -1;
- string line = sr.ReadLine( );
+ Image image = GnomeUtil.GetIcon (mime_type, 48);
- string icon_name = "";
+ index = MimeIconEngine.SmallIcons.Images.Add (image, Color.Transparent);
+ MimeIconEngine.LargeIcons.Images.Add (image, Color.Transparent);
- string mime_type = "";
+ MimeIconEngine.MimeIconIndex.Add (mime_type, index);
- bool have_icon = false;
- bool have_mimetype = false;
-
- while ( line != null )
- {
- line = line.Trim( );
-
- if ( line.StartsWith( "Icon" ) )
- {
- icon_name = line.Substring( line.IndexOf( '=' ) + 1 );
- icon_name = icon_name.Trim( );
- if ( have_mimetype )
- break;
- have_icon = true;
- }
- else
- if ( line.StartsWith( "MimeType" ) )
- {
- mime_type = line.Substring( line.IndexOf( '=' ) + 1 );
- mime_type = mime_type.Trim( );
- if ( have_icon )
- break;
- have_mimetype = true;
- }
-
- line = sr.ReadLine( );
- }
+ return index;
+ }
+ }
+
+ internal class GnomeUtil
+ {
+ 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_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)]
+ static extern void g_free (IntPtr mem);
+
+ [DllImport(libgdk)]
+ static extern bool gdk_init_check (IntPtr argc, IntPtr argv);
+
+ [DllImport(libgobject)]
+ static extern void g_object_unref (IntPtr nativeObject);
+
+ [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)]
+ static extern IntPtr gtk_icon_theme_get_default ();
+
+ [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)]
+ static extern bool gtk_icon_theme_has_icon (IntPtr icon_theme, string icon_name);
+
+ enum GnomeIconLookupFlags
+ {
+ GNOME_ICON_LOOKUP_FLAGS_NONE = 0,
+ GNOME_ICON_LOOKUP_FLAGS_EMBEDDING_TEXT = 1<<0,
+ GNOME_ICON_LOOKUP_FLAGS_SHOW_SMALL_IMAGES_AS_THEMSELVES = 1<<1,
+ GNOME_ICON_LOOKUP_FLAGS_ALLOW_SVG_AS_THEMSELVES = 1<<2
+ };
+
+ enum GtkIconLookupFlags
+ {
+ GTK_ICON_LOOKUP_NO_SVG = 1 << 0,
+ GTK_ICON_LOOKUP_FORCE_SVG = 1 << 1,
+ GTK_ICON_LOOKUP_USE_BUILTIN = 1 << 2
+ };
+
+ static bool inited = false;
+
+ static IntPtr default_icon_theme = IntPtr.Zero;
+
+ static void Init ()
+ {
+ gdk_init_check (IntPtr.Zero, IntPtr.Zero);
- sr.Close( );
+ inited = true;
- MimeIconEngine.AddMimeTypeAndIconName( mime_type, icon_name );
+ default_icon_theme = gtk_icon_theme_get_default ();
}
- private bool ReadKdeglobals( )
+ public static Image GetIcon (string file_name, string mime_type, int size)
{
- if ( !File.Exists( full_kdegloabals_filename ) )
- {
- mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.NO_KDEGLOBALS;
-
- return false;
- }
+ if (!inited)
+ Init ();
- StreamReader sr = new StreamReader( full_kdegloabals_filename );
+ Uri uri = new Uri (file_name);
- string line = sr.ReadLine( );
+ string icon = gnome_icon_lookup (default_icon_theme, IntPtr.Zero, uri.AbsoluteUri,
+ null, IntPtr.Zero, mime_type,
+ GnomeIconLookupFlags.GNOME_ICON_LOOKUP_FLAGS_NONE, IntPtr.Zero);
- while ( line != null )
- {
- if ( line.IndexOf( "[Icons]" ) != -1 )
- {
- line = sr.ReadLine( );
-
- if ( line != null && line.IndexOf( "Theme" ) != -1 )
- {
- line = line.Trim( );
-
- icon_theme = line.Substring( line.IndexOf( '=' ) + 1 );
-
- icon_theme = icon_theme.Trim( );
-
- break;
- }
- }
-
- line = sr.ReadLine( );
- }
+ IntPtr error = IntPtr.Zero;
+ IntPtr pixbuf = gtk_icon_theme_load_icon (default_icon_theme, icon, size,
+ GtkIconLookupFlags.GTK_ICON_LOOKUP_USE_BUILTIN, out error);
- sr.Close( );
+ if (error != IntPtr.Zero)
+ return null;
- return true;
+ return GdkPixbufToImage (pixbuf);
}
- }
-
- // GnomeHandler uses the default gnome icon theme (many others are svg only)
- internal class GnomeHandler : PlatformMimeIconHandler
- {
- public override MimeExtensionHandlerStatus Start( )
+
+ public static Image GetIcon (string icon, int size)
{
- if ( !CheckPlatformDirectories( ) )
- return mimeExtensionHandlerStatus;
+ if (!inited)
+ Init ();
- CreateMimeTypeFromIconName( );
+ IntPtr error = IntPtr.Zero;
+ IntPtr pixbuf = gtk_icon_theme_load_icon (default_icon_theme, icon, size,
+ GtkIconLookupFlags.GTK_ICON_LOOKUP_USE_BUILTIN, out error);
- ReadIcons( );
+ if (error != IntPtr.Zero)
+ return null;
- return MimeExtensionHandlerStatus.OK;
+ return GdkPixbufToImage (pixbuf);
}
- protected override bool CheckPlatformDirectories( )
+ public static Image GdkPixbufToImage (IntPtr pixbuf)
{
- // add more directories ???
- if ( Directory.Exists( "/opt/gnome/share/icons/gnome/48x48" ) )
- {
- icon_paths.Add( "/opt/gnome/share/icons/gnome/48x48/" );
- }
- else
- if ( Directory.Exists( "/usr/share/icons/gnome/48x48" ) )
- {
- icon_paths.Add( "/usr/share/icons/gnome/48x48/" );
- }
- else
- if ( Directory.Exists( "/usr/local/share/icons/gnome/48x48" ) )
- {
- icon_paths.Add( "/usr/local/share/icons/gnome/48x48/" );
- }
- else
- {
- mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.NO_ICONS;
- return false;
- }
+ IntPtr error = IntPtr.Zero;
+ IntPtr buffer;
+ UIntPtr buffer_size_as_ptr;
+ string type = "png";
+
+ bool saved = gdk_pixbuf_save_to_buffer (pixbuf, out buffer, out buffer_size_as_ptr, type, out error, IntPtr.Zero);
- return true;
+ if (!saved)
+ return null;
+
+ int buffer_size = (int) (uint) buffer_size_as_ptr;
+ byte[] result = new byte [buffer_size];
+ Marshal.Copy (buffer, result, 0, (int) buffer_size);
+ g_free (buffer);
+ g_object_unref (pixbuf);
+
+ Image image = null;
+ using (MemoryStream s = new MemoryStream (result))
+ image = Image.FromStream (s);
+
+ return image;
}
- private void CreateMimeTypeFromIconName( )
+ public static Image GetSVGasImage (string filename, int width, int height)
{
- MimeIconEngine.AddMimeTypeAndIconName( "inode/directory", "gnome-fs-directory" );
- MimeIconEngine.AddMimeTypeAndIconName( "unknown/unknown", "gnome-fs-regular" );
- MimeIconEngine.AddMimeTypeAndIconName( "desktop/desktop", "gnome-fs-desktop" );
- MimeIconEngine.AddMimeTypeAndIconName( "directory/home", "gnome-fs-home" );
- MimeIconEngine.AddMimeTypeAndIconName( "network/network", "gnome-fs-network" );
-
- foreach ( string ip in icon_paths )
- {
- string[] files = Directory.GetFiles( ip + "mimetypes" );
-
- foreach ( string file in files )
- {
- string extension = Path.GetExtension( file );
-
- if ( extension != ".png" )
- continue;
-
- string file_name = Path.GetFileNameWithoutExtension( file );
-
- if ( !file_name.StartsWith( "gnome-mime-" ) )
- continue;
-
- StringBuilder mime_type = new StringBuilder( file_name.Replace( "gnome-mime-", "" ) );
-
- for ( int i = 0; i < mime_type.Length; i++ )
- if ( mime_type[ i ] == '-' )
- {
- mime_type[ i ] = '/';
- break;
- }
-
- MimeIconEngine.AddMimeTypeAndIconName( mime_type.ToString( ), file_name );
- }
- }
+ if (!inited)
+ Init ();
+
+ if (!File.Exists (filename))
+ return null;
+ IntPtr error = IntPtr.Zero;
+ IntPtr pixbuf = rsvg_pixbuf_from_file_at_size (filename, width, height, out error);
+
+ if (error != IntPtr.Zero)
+ return null;
+
+ return GdkPixbufToImage (pixbuf);
}
- private void ReadIcons( )
+ public static bool HasImage (string name)
{
- foreach ( string icon_path in icon_paths )
- {
- string[] directories = Directory.GetDirectories( icon_path );
-
- foreach ( string directory in directories )
- {
- DirectoryInfo di = new DirectoryInfo( directory );
-
- FileInfo[] fileinfo = di.GetFiles( );
-
- foreach ( FileInfo fi in fileinfo )
- {
- if ( fi.Extension != ".png" )
- continue;
-
- string name = Path.GetFileNameWithoutExtension( fi.Name );
-
- MimeIconEngine.AddIcon( name, fi.FullName );
- }
- }
- }
+ if (!inited)
+ Init ();
+
+ return gtk_icon_theme_has_icon (default_icon_theme, name);
}
}
}