~bas-dotbas/do/win32-next

« back to all changes in this revision

Viewing changes to PluginLib/src/Builtin/FileItem.cs

  • Committer: djsiegel at gmail
  • Date: 2007-10-13 21:55:22 UTC
  • Revision ID: djsiegel@gmail.com-20071013215522-n0vjgog0tyjfnpno
Restructured plugins library.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// FileItem.cs created with MonoDevelop
2
 
// User: dave at 2:25 PM 9/13/2007
3
 
//
4
 
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
5
 
//
6
 
 
7
 
using System;
8
 
using System.IO;
9
 
using System.Collections;
10
 
 
11
 
using Do.PluginLib;
12
 
 
13
 
namespace Do.PluginLib.Builtin
14
 
{
15
 
        
16
 
        public class FileItem : IFileItem
17
 
        {
18
 
 
19
 
                static Hashtable extensionTypes;
20
 
                
21
 
                static FileItem ()
22
 
                {
23
 
                        string[] extentions;
24
 
                        
25
 
                        extensionTypes = new Hashtable ();
26
 
                        
27
 
                        // Register extensions for specialized subclasses.
28
 
                        // See note in ImageFileItem.cs
29
 
                        extentions = new string[] { "jpg", "jpeg", "png", "gif" };
30
 
                        foreach (string ext in extentions) {
31
 
                                FileItem.RegisterExtensionForFileItemType (ext, typeof (ImageFileItem));
32
 
                        }
33
 
                }
34
 
                
35
 
                public static bool RegisterExtensionForFileItemType (string ext, Type fi_type)
36
 
                {
37
 
                        if (extensionTypes.ContainsKey (ext)) {
38
 
                                return false;
39
 
                        }
40
 
                        extensionTypes[ext] = fi_type;
41
 
                        return true;
42
 
                }
43
 
                
44
 
                public static FileItem Create (string uri)
45
 
                {
46
 
                        string ext;
47
 
                        Type fi_type;
48
 
                        FileItem result;
49
 
                        
50
 
                        ext = System.IO.Path.GetExtension (uri).ToLower ();
51
 
                        if (ext.StartsWith (".")) {
52
 
                                ext = ext.Substring (1);
53
 
                        }
54
 
                        if (extensionTypes.ContainsKey (ext)) {
55
 
                                fi_type = extensionTypes[ext] as Type;
56
 
                        } else {
57
 
                                fi_type = typeof (FileItem);
58
 
                        }
59
 
                        try {
60
 
                                result = (FileItem) System.Activator.CreateInstance (fi_type, new string[] {uri});
61
 
                        } catch {
62
 
                                result = new FileItem (uri);
63
 
                        }
64
 
                        return result;
65
 
                }
66
 
                
67
 
                public static string ShortUri (string uri) {
68
 
                        string home;
69
 
                        
70
 
                        uri = (uri == null ? "" : uri);
71
 
                        home = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
72
 
                        uri = uri.Replace (home, "~");
73
 
                        return uri;
74
 
                }
75
 
                
76
 
                string uri, name, icon, mime_type;
77
 
                
78
 
                public FileItem (string uri)
79
 
                {       
80
 
                        this.uri = uri;
81
 
                        this.name = Path.GetFileName (uri);
82
 
                        this.mime_type = Gnome.Vfs.Global.GetMimeType (uri);
83
 
 
84
 
                        if (System.IO.Directory.Exists (uri)) {
85
 
                                icon = "folder";
86
 
                        } else {
87
 
                                try {
88
 
                                        icon = mime_type.Replace ('/', '-');
89
 
                                        icon = string.Format ("gnome-mime-{0}", icon);
90
 
                                } catch (NullReferenceException) {
91
 
                                        icon = "file";
92
 
                                }
93
 
                        }
94
 
                }
95
 
                
96
 
                public virtual string Name {
97
 
                        get { return name; }
98
 
                }
99
 
                
100
 
                public virtual string Description {
101
 
                        get { return ShortUri (uri); }
102
 
                }
103
 
                
104
 
                public virtual string Icon {
105
 
                        get { return icon; }
106
 
                }
107
 
                
108
 
                public string URI {
109
 
                        get { return uri; }
110
 
                }
111
 
                
112
 
                public string MimeType {
113
 
                        get { return mime_type; }
114
 
                }
115
 
 
116
 
        }
117
 
}