* TreeView.cs: Don't draw the selected node when we lose
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / MimeIcon.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //
24 //  Alexander Olk       xenomorph2@onlinehome.de
25 //
26
27 // short "how to" if you want to add an other platform handler, etc:
28 // - first add mime type names and icon names (best is without extension) to MimeIconEngine, for example:
29 //   MimeIconEngine.AddMimeTypeAndIconName( "inode/directory", "gnome-fs-directory" );
30 // - next add the icon name (the same as used in AddMimeTypeAndIconName) and the full filename, for example:
31 //   MimeIconEngine.AddIcon( "gnome-fs-directory", "/opt/gnome/share/icons/gnome/48x48/filesystems/gnome-fs-directory.png" );
32 //   AddIcon adds the icon to the image lists SmallIconList and LargeIconList
33 // - provide always a "unknown/unknown" 'mime type' with a default icon for unkown mime types,
34 //   "desktop/desktop" 'mime type' for the desktop icon, "directory/home" 'mime type for the home dir of the user and so on
35 //   (look at the default platform handler)
36 //
37 // use
38 // public static int GetIconIndexForFile( string full_filename )
39 // public static int GetIconIndexForMimeType( string mime_type )
40 // to get the image index in MimeIconEngine.SmallIcons and MimeIconEngine.LargeIcons
41 // use
42 // public static Image GetIconForMimeTypeAndSize( string mime_type, Size size )
43 // to get the image itself for a mime type with a specific size
44
45 using System;
46 using System.Drawing;
47 using System.Collections;
48 using System.Collections.Specialized;
49 using System.IO;
50 using System.Text;
51
52 namespace System.Windows.Forms
53 {
54         internal enum MimeExtensionHandlerStatus
55         {
56                 OK,
57                 NO_KDEGLOBALS,
58                 NO_GNOMECONFIG,
59                 NO_ICONS,
60                 NO_MIMELNK
61         }
62         
63         internal enum EPlatformHandler
64         {
65                 Default,
66                 KDE,
67                 GNOME
68                 // Win, Mac OSX...
69         }
70         
71         internal class MimeIconEngine
72         {
73                 public static ImageList SmallIcons = new ImageList();
74                 public static ImageList LargeIcons = new ImageList();
75                 
76                 private static PlatformMimeIconHandler platformMimeHandler = null;
77                 
78                 private static EPlatformHandler platform = EPlatformHandler.Default;
79                 
80                 private static Hashtable MimeTypeIconIndexHash = new Hashtable();
81                 
82                 private static NameValueCollection IconNameMimeTypeNameValueCollection = new NameValueCollection();
83                 
84                 private static StringCollection added_icons = new StringCollection();
85                 
86                 private static object lock_object = new Object();
87                 
88                 static MimeIconEngine( )
89                 {
90                         // add some more aliases, kde for example uses other mime type names for some mime types...
91                         MimeGenerated.Aliases.Add( "application/x-compressed-tar", "application/x-tgz" );
92                         MimeGenerated.Aliases.Add( "application/x-bzip-compressed-tar", "application/x-tbz" );
93                         MimeGenerated.Aliases.Add( "application/zip", "application/x-zip" );
94                         MimeGenerated.Aliases.Add( "text/x-patch", "text/x-diff" );
95                         
96                         SmallIcons.ColorDepth = ColorDepth.Depth32Bit;
97                         SmallIcons.TransparentColor = Color.Transparent;
98                         LargeIcons.ColorDepth = ColorDepth.Depth32Bit;
99                         LargeIcons.TransparentColor = Color.Transparent;
100                         
101                         string session =  Environment.GetEnvironmentVariable( "DESKTOP_SESSION" );
102                         
103                         if ( session != null )
104                         {
105                                 session = session.ToUpper( );
106                                 
107                                 if ( session == "DEFAULT" )
108                                 {
109                                         string helper = Environment.GetEnvironmentVariable( "KDE_FULL_SESSION" );
110                                         
111                                         if ( helper != null )
112                                                 session = "KDE";
113                                         else
114                                         {
115                                                 helper = Environment.GetEnvironmentVariable( "GNOME_DESKTOP_SESSION_ID" );
116                                                 
117                                                 if ( helper != null )
118                                                         session = "GNOME";
119                                         }
120                                 }
121                         }
122                         else
123                                 session = "";
124                         
125                         Console.WriteLine( "Desktop session is: " + session );
126                         
127                         if ( session == "KDE" )
128                         {
129                                 SmallIcons.ImageSize = new Size( 24, 24 );
130                                 LargeIcons.ImageSize = new Size( 48, 48 );
131                                 
132                                 platformMimeHandler = new KdeHandler( );
133                                 if ( platformMimeHandler.Start( ) == MimeExtensionHandlerStatus.OK )
134                                 {
135                                         Console.WriteLine( "Kde icons ready..." );
136                                         platform = EPlatformHandler.KDE;
137                                 }
138                                 else // fallback to default
139                                 {
140                                         MimeIconEngine.LargeIcons.Images.Clear( );
141                                         MimeIconEngine.SmallIcons.Images.Clear( );
142                                         platformMimeHandler = new PlatformDefaultHandler( );
143                                         platformMimeHandler.Start( );
144                                 }
145                         }
146                         else
147                         if ( session == "GNOME" )
148                         {
149                                 SmallIcons.ImageSize = new Size( 24, 24 );
150                                 LargeIcons.ImageSize = new Size( 48, 48 );
151                                 
152                                 platformMimeHandler = new GnomeHandler( );
153                                 if ( platformMimeHandler.Start( ) == MimeExtensionHandlerStatus.OK )
154                                 {
155                                         Console.WriteLine( "Gnome icons ready..." );
156                                         platform = EPlatformHandler.GNOME;
157                                 }
158                                 else // fallback to default
159                                 {
160                                         MimeIconEngine.LargeIcons.Images.Clear( );
161                                         MimeIconEngine.SmallIcons.Images.Clear( );
162                                         platformMimeHandler = new PlatformDefaultHandler( );
163                                         platformMimeHandler.Start( );
164                                 }
165                         }
166                         else
167                         {
168                                 SmallIcons.ImageSize = new Size( 16, 16 );
169                                 LargeIcons.ImageSize = new Size( 48, 48 );
170                                 
171                                 platformMimeHandler = new PlatformDefaultHandler( );
172                                 platformMimeHandler.Start( );
173                         }
174                         
175                         IconNameMimeTypeNameValueCollection = null;
176                         added_icons = null;
177                 }
178                 
179                 public static int GetIconIndexForFile( string full_filename )
180                 {
181                         lock ( lock_object )
182                         {
183                                 string mime_type = Mime.GetMimeTypeForFile( full_filename );
184                                 
185                                 if ( platform == EPlatformHandler.Default )
186                                 {
187                                         if ( mime_type == "inode/directory" )
188                                         {
189                                                 return (int)MimeTypeIconIndexHash[ "inode/directory" ];
190                                         }
191                                         else
192                                         {
193                                                 return (int)MimeTypeIconIndexHash[ "unknown/unknown" ];
194                                         }
195                                 }
196                                 
197                                 object oindex = GetIconIndex( mime_type );
198                                 
199                                 if ( oindex == null )
200                                         oindex = MimeTypeIconIndexHash[ "unknown/unknown" ];
201                                 
202                                 return (int)oindex;
203                         }
204                 }
205                 
206                 public static int GetIconIndexForMimeType( string mime_type )
207                 {
208                         lock ( lock_object )
209                         {
210                                 if ( platform == EPlatformHandler.Default )
211                                 {
212                                         if ( mime_type == "inode/directory" )
213                                         {
214                                                 return (int)MimeTypeIconIndexHash[ "inode/directory" ];
215                                         }
216                                         else
217                                         {
218                                                 return (int)MimeTypeIconIndexHash[ "unknown/unknown" ];
219                                         }
220                                 }
221                                 
222                                 object oindex = GetIconIndex( mime_type );
223                                 
224                                 if ( oindex == null )
225                                         oindex = MimeTypeIconIndexHash[ "unknown/unknown" ];
226                                 
227                                 return (int)oindex;
228                         }
229                 }
230                 
231                 public static Image GetIconForMimeTypeAndSize( string mime_type, Size size )
232                 {
233                         lock ( lock_object )
234                         {
235                                 object oindex = GetIconIndex( mime_type );
236                                 
237                                 if ( oindex == null )
238                                         oindex = MimeTypeIconIndexHash[ "unknown/unknown" ];
239                                 
240                                 Bitmap bmp = new Bitmap( LargeIcons.Images[ (int)oindex ], size );
241                                 
242                                 return bmp;
243                         }
244                 }
245                 
246                 internal static void AddIcon( string name, string fullname )
247                 {
248                         if ( !CheckIfIconIsNeeded( name ) )
249                                 return;
250                         
251                         if ( added_icons.Contains( name ) )
252                                 return;
253                         
254                         added_icons.Add( name );
255                         
256                         Bitmap bmp = new Bitmap( fullname );
257                         
258                         int index = SmallIcons.Images.Add( bmp, Color.Transparent );
259                         LargeIcons.Images.Add( bmp, Color.Transparent );
260                         
261                         AddMimeTypeIconIndexHash( name, index );
262                 }
263                 
264                 private static bool CheckIfIconIsNeeded( string name )
265                 {
266                         string mime_types = IconNameMimeTypeNameValueCollection[ name ];
267                         
268                         if ( mime_types != null )
269                                 return true;
270                         
271                         return false;
272                 }
273                 
274                 internal static void AddMimeTypeIconIndexHash( string name, int index )
275                 {
276                         string mime_type = IconNameMimeTypeNameValueCollection[ name ];
277                         
278                         if ( mime_type == null )
279                                 return;
280                         
281                         string[] split = mime_type.Split( new char[] { ',' } );
282                         
283                         foreach ( string s in split )
284                         {
285                                 if ( MimeTypeIconIndexHash.ContainsKey( s ) )
286                                         continue;
287                                 
288                                 MimeTypeIconIndexHash.Add( s, index );
289                         }
290                 }
291                 
292                 internal static void AddIconByImage( string name, Image image )
293                 {
294                         int index = SmallIcons.Images.Add( image, Color.Transparent );
295                         LargeIcons.Images.Add( image, Color.Transparent );
296                         
297                         AddMimeTypeIconIndexHash( name, index );
298                 }
299                 
300                 internal static void AddMimeTypeAndIconName( string mimetype, string iconname )
301                 {
302                         if ( iconname.Equals( String.Empty ) )
303                                 return;
304                         
305                         IconNameMimeTypeNameValueCollection.Add( iconname, mimetype );
306                 }
307                 
308                 private static object GetIconIndex( string mime_type )
309                 {
310                         object oindex = null;
311                         
312                         if ( mime_type != null )
313                         {
314                                 // first check if mime_type is available in the mimetype/icon hashtable
315                                 oindex = MimeTypeIconIndexHash[ mime_type ];
316                                 
317                                 if ( oindex == null )
318                                 {
319                                         // it is not available, check if an alias exist for mime_type
320                                         string alias = Mime.GetMimeAlias( mime_type );
321                                         
322                                         if ( alias != null )
323                                         {
324                                                 string[] split = alias.Split( new char[] { ',' } );
325                                                 
326                                                 foreach ( string s in split )
327                                                 {
328                                                         oindex = MimeTypeIconIndexHash[ s ];
329                                                         
330                                                         if ( oindex != null )
331                                                                 break;
332                                                 }
333                                         }
334                                         
335                                         // if oindex is still null check if mime_type is a sub class of an other mime type
336                                         if ( oindex == null )
337                                         {
338                                                 string sub_class = MimeGenerated.SubClasses[ mime_type ];
339                                                 
340                                                 if ( sub_class != null )
341                                                         oindex = MimeTypeIconIndexHash[ sub_class ];
342                                         }
343                                 }
344                         }
345                         
346                         return oindex;
347                 }
348         }
349         
350         internal abstract class PlatformMimeIconHandler
351         {
352                 protected StringCollection mime_paths = new StringCollection();
353                 
354                 protected StringCollection icon_paths = new StringCollection();
355                 
356                 protected string icon_theme = "";
357                 
358                 protected MimeExtensionHandlerStatus mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.OK;
359                 
360                 public MimeExtensionHandlerStatus MimeExtensionHandlerStatus
361                 {
362                         get {
363                                 return mimeExtensionHandlerStatus;
364                         }
365                 }
366                 
367                 public abstract MimeExtensionHandlerStatus Start( );
368                 
369                 // check, if icon, mime, etc., directories exist
370                 protected virtual bool CheckPlatformDirectories( )
371                 {
372                         return true;
373                 }
374         }
375         
376         internal class PlatformDefaultHandler : PlatformMimeIconHandler
377         {
378                 public override MimeExtensionHandlerStatus Start( )
379                 {
380                         MimeIconEngine.AddMimeTypeAndIconName( "unknown/unknown", "paper" );
381                         MimeIconEngine.AddMimeTypeAndIconName( "inode/directory", "folder" );
382                         MimeIconEngine.AddMimeTypeAndIconName( "desktop/desktop", "desktop" );
383                         MimeIconEngine.AddMimeTypeAndIconName( "directory/home", "folder_with_paper" );
384                         MimeIconEngine.AddMimeTypeAndIconName( "network/network", "monitor-planet" );
385                         
386                         MimeIconEngine.AddIconByImage( "folder",  (Image)Locale.GetResource( "folder" ) );
387                         MimeIconEngine.AddIconByImage( "paper",  (Image)Locale.GetResource( "paper" ) );
388                         MimeIconEngine.AddIconByImage( "desktop",  (Image)Locale.GetResource( "desktop" ) );
389                         MimeIconEngine.AddIconByImage( "folder_with_paper",  (Image)Locale.GetResource( "folder_with_paper" ) );
390                         MimeIconEngine.AddIconByImage( "monitor-planet",  (Image)Locale.GetResource( "monitor-planet" ) );
391                         
392                         return MimeExtensionHandlerStatus.OK; // return always ok
393                 }
394         }
395         
396         internal class KdeHandler : PlatformMimeIconHandler
397         {
398                 string full_kdegloabals_filename = Environment.GetFolderPath( Environment.SpecialFolder.Personal )
399                 + "/"
400                 + ".kde/share/config/kdeglobals";
401                 
402                 public override MimeExtensionHandlerStatus Start( )
403                 {
404                         if ( !ReadKdeglobals( ) )
405                                 return mimeExtensionHandlerStatus;
406                         
407                         if ( !CheckPlatformDirectories( ) )
408                                 return mimeExtensionHandlerStatus;
409                         
410                         // check if the theme is svg only
411                         // if true, use theme "default.kde"
412                         // don't know if that is available in every linux distribution
413                         // MWF has no svg support yet (cairo's libsvg!?!)
414                         if ( SVGOnly( ) )
415                                 icon_theme = "default.kde";
416                         
417                         ReadMimetypes( );
418                         
419                         ReadIcons( );
420                         
421                         return mimeExtensionHandlerStatus;
422                 }
423                 
424                 private bool SVGOnly( )
425                 {
426                         // check only the first path in icon_paths
427                         if ( icon_paths.Count > 0 )
428                         {
429                                 string icon_path = icon_paths[ 0 ] + icon_theme;
430                                 string[] dirs = Directory.GetDirectories( icon_path );
431                                 
432                                 if ( dirs.Length == 1 && dirs[ 0 ] == "scalable" )
433                                         return true;
434                         }
435                         
436                         return false;
437                 }
438                 
439                 protected override bool CheckPlatformDirectories( )
440                 {
441                         bool icons_found = false;
442                         
443                         // default icon dirs
444                         if ( Directory.Exists( "/opt/kde3/share/icons/default.kde" ) )
445                         {
446                                 icon_paths.Add( "/opt/kde3/share/icons" + "/" );
447                                 icons_found = true;
448                         }
449                         else
450                         if ( Directory.Exists( "/usr/share/icons/default.kde" ) )
451                         {
452                                 icon_paths.Add( "/usr/share/icons" + "/" );
453                                 icons_found = true;
454                         }
455                         else
456                         if ( Directory.Exists( "/usr/local/share/icons/default.kde" ) )
457                         {
458                                 icon_paths.Add( "/usr/local/share/icons"  + "/" );
459                                 icons_found = true;
460                         }
461                         else
462                         if ( !icons_found )
463                         {
464                                 mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.NO_ICONS;
465                                 return false;
466                         }
467                         
468                         bool mimelnk_found = false;
469                         
470                         if ( Directory.Exists( "/usr/share/mimelnk" ) )
471                         {
472                                 mime_paths.Add( "/usr/share/mimelnk" + "/" );
473                                 mimelnk_found = true;
474                         }
475                         
476                         if ( Directory.Exists( "/usr/local/share/mimelnk" ) )
477                         {
478                                 mime_paths.Add( "/usr/local/share/mimelnk" + "/" );
479                                 mimelnk_found = true;
480                         }
481                         
482                         if ( Directory.Exists( "/opt/kde3/share/mimelnk" ) )
483                         {
484                                 mime_paths.Add( "/opt/kde3/share/mimelnk" + "/" );
485                                 mimelnk_found = true;
486                         }
487                         
488                         if ( !mimelnk_found )
489                         {
490                                 mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.NO_MIMELNK;
491                                 return false;
492                         }
493                         
494                         return true;
495                 }
496                 
497                 private void ReadIcons( )
498                 {
499                         foreach ( string icon_path_in in icon_paths )
500                         {
501                                 string icon_path = icon_path_in + icon_theme + "/48x48";
502                                 
503                                 string[] directories = Directory.GetDirectories( icon_path );
504                                 
505                                 foreach ( string d in directories )
506                                 {
507                                         DirectoryInfo di = new DirectoryInfo( d );
508                                         
509                                         FileInfo[] fileinfo = di.GetFiles( );
510                                         
511                                         foreach ( FileInfo fi in fileinfo )
512                                         {
513                                                 string name = Path.GetFileNameWithoutExtension( fi.Name );
514                                                 
515                                                 MimeIconEngine.AddIcon( name, fi.FullName );
516                                         }
517                                 }
518                         }
519                 }
520                 
521                 private void ReadMimetypes( )
522                 {
523                         MimeIconEngine.AddMimeTypeAndIconName( "unknown/unknown", "unknown" );
524                         MimeIconEngine.AddMimeTypeAndIconName( "desktop/desktop", "desktop" );
525                         MimeIconEngine.AddMimeTypeAndIconName( "directory/home", "folder_home" );
526                         MimeIconEngine.AddMimeTypeAndIconName( "network/network", "network" );
527                         
528                         foreach ( string mime_path in mime_paths )
529                         {
530                                 string[] directories = Directory.GetDirectories( mime_path );
531                                 
532                                 foreach ( string d in directories )
533                                 {
534                                         string[] files = Directory.GetFiles( d );
535                                         
536                                         foreach ( string f in files )
537                                         {
538                                             try {
539                                                 ReadDotDesktop( f );
540                                             } catch {
541                                                 // Ignore errors if the file can not be read.
542                                             }
543                                         }
544                                 }
545                         }
546                 }
547                 
548                 private void ReadDotDesktop( string filename )
549                 {
550                         StreamReader sr = new StreamReader( filename );
551                         
552                         string line = sr.ReadLine( );
553                         
554                         string icon_name = "";
555                         
556                         string mime_type = "";
557                         
558                         bool have_icon = false;
559                         bool have_mimetype = false;
560                         
561                         while ( line != null )
562                         {
563                                 line = line.Trim( );
564                                 
565                                 if ( line.StartsWith( "Icon" ) )
566                                 {
567                                         icon_name = line.Substring( line.IndexOf( '=' ) + 1 );
568                                         icon_name = icon_name.Trim( );
569                                         if ( have_mimetype )
570                                                 break;
571                                         have_icon = true;
572                                 }
573                                 else
574                                 if ( line.StartsWith( "MimeType" ) )
575                                 {
576                                         mime_type = line.Substring( line.IndexOf( '=' ) + 1 );
577                                         mime_type = mime_type.Trim( );
578                                         if ( have_icon )
579                                                 break;
580                                         have_mimetype = true;
581                                 }
582                                 
583                                 line = sr.ReadLine( );
584                         }
585                         
586                         sr.Close( );
587                         
588                         MimeIconEngine.AddMimeTypeAndIconName( mime_type, icon_name );
589                 }
590                 
591                 private bool ReadKdeglobals( )
592                 {
593                         if ( !File.Exists( full_kdegloabals_filename ) )
594                         {
595                                 mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.NO_KDEGLOBALS;
596                                 
597                                 return false;
598                         }
599                         
600                         StreamReader sr = new StreamReader( full_kdegloabals_filename );
601                         
602                         string line = sr.ReadLine( );
603                         
604                         while ( line != null )
605                         {
606                                 if ( line.IndexOf( "[Icons]" ) != -1 )
607                                 {
608                                         line = sr.ReadLine( );
609                                         
610                                         if ( line != null && line.IndexOf( "Theme" ) != -1 )
611                                         {
612                                                 line = line.Trim( );
613                                                 
614                                                 icon_theme = line.Substring( line.IndexOf( '=' ) + 1 );
615                                                 
616                                                 icon_theme = icon_theme.Trim( );
617                                                 
618                                                 break;
619                                         }
620                                 }
621                                 
622                                 line = sr.ReadLine( );
623                         }
624                         
625                         sr.Close( );
626                         
627                         return true;
628                 }
629         }
630         
631         // GnomeHandler uses the default gnome icon theme (many others are svg only)
632         internal class GnomeHandler : PlatformMimeIconHandler
633         {
634                 public override MimeExtensionHandlerStatus Start( )
635                 {
636                         if ( !CheckPlatformDirectories( ) )
637                                 return mimeExtensionHandlerStatus;
638                         
639                         CreateMimeTypeFromIconName( );
640                         
641                         ReadIcons( );
642                         
643                         return MimeExtensionHandlerStatus.OK;
644                 }
645                 
646                 protected override bool CheckPlatformDirectories( )
647                 {
648                         // add more directories ???
649                         if ( Directory.Exists( "/opt/gnome/share/icons/gnome/48x48" ) )
650                         {
651                                 icon_paths.Add( "/opt/gnome/share/icons/gnome/48x48/" );
652                         }
653                         else
654                         if ( Directory.Exists( "/usr/share/icons/gnome/48x48" ) )
655                         {
656                                 icon_paths.Add( "/usr/share/icons/gnome/48x48/" );
657                         }
658                         else
659                         if ( Directory.Exists( "/usr/local/share/icons/gnome/48x48" ) )
660                         {
661                                 icon_paths.Add( "/usr/local/share/icons/gnome/48x48/" );
662                         }
663                         else
664                         {
665                                 mimeExtensionHandlerStatus = MimeExtensionHandlerStatus.NO_ICONS;
666                                 return false;
667                         }
668                         
669                         return true;
670                 }
671                 
672                 private void CreateMimeTypeFromIconName( )
673                 {
674                         MimeIconEngine.AddMimeTypeAndIconName( "inode/directory", "gnome-fs-directory" );
675                         MimeIconEngine.AddMimeTypeAndIconName( "unknown/unknown", "gnome-fs-regular" );
676                         MimeIconEngine.AddMimeTypeAndIconName( "desktop/desktop", "gnome-fs-desktop" );
677                         MimeIconEngine.AddMimeTypeAndIconName( "directory/home", "gnome-fs-home" );
678                         MimeIconEngine.AddMimeTypeAndIconName( "network/network", "gnome-fs-network" );
679                         
680                         foreach ( string ip in icon_paths )
681                         {
682                                 string[] files = Directory.GetFiles( ip + "mimetypes" );
683                                 
684                                 foreach ( string file in files )
685                                 {
686                                         string extension = Path.GetExtension( file );
687                                         
688                                         if ( extension != ".png" )
689                                                 continue;
690                                         
691                                         string file_name = Path.GetFileNameWithoutExtension( file );
692                                         
693                                         if ( !file_name.StartsWith( "gnome-mime-" ) )
694                                                 continue;
695                                         
696                                         StringBuilder mime_type = new StringBuilder( file_name.Replace( "gnome-mime-", "" ) );
697                                         
698                                         for ( int i = 0; i < mime_type.Length; i++ )
699                                                 if ( mime_type[ i ] == '-' )
700                                                 {
701                                                         mime_type[ i ] = '/';
702                                                         break;
703                                                 }
704                                         
705                                         MimeIconEngine.AddMimeTypeAndIconName( mime_type.ToString( ), file_name );
706                                 }
707                         }
708                 }
709                 
710                 private void ReadIcons( )
711                 {
712                         foreach ( string icon_path in icon_paths )
713                         {
714                                 string[] directories = Directory.GetDirectories( icon_path );
715                                 
716                                 foreach ( string directory in directories )
717                                 {
718                                         DirectoryInfo di = new DirectoryInfo( directory );
719                                         
720                                         FileInfo[] fileinfo = di.GetFiles( );
721                                         
722                                         foreach ( FileInfo fi in fileinfo )
723                                         {
724                                                 if ( fi.Extension != ".png" )
725                                                         continue;
726                                                 
727                                                 string name = Path.GetFileNameWithoutExtension( fi.Name );
728                                                 
729                                                 MimeIconEngine.AddIcon( name, fi.FullName );
730                                         }
731                                 }
732                         }
733                 }
734         }
735 }
736