~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Interface.Windows/src/Do.Interface/IconProvider.cs

  • Committer: Hardeep S
  • Date: 2009-06-23 03:53:12 UTC
  • Revision ID: ootz0rz@gmail.com-20090623035312-it8tb5wkha6nf31p
Added Do.Interface.Windows, and a bunch of cleanup with old MonoDevelop files elsewhere

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* IconProvider.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this source distribution.
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
using System;
 
21
using System.IO;
 
22
using System.Reflection;
 
23
using System.Collections.Generic;
 
24
 
 
25
using Gtk;
 
26
using Gdk;
 
27
 
 
28
using Do.Platform;
 
29
 
 
30
namespace Do.Interface
 
31
{
 
32
        public static class IconProvider
 
33
        {
 
34
                const string MissingIconIcon = "emblem-noread";
 
35
                public const int DefaultIconSize = 80;
 
36
                
 
37
                static IconProvider ()
 
38
                {
 
39
                }
 
40
                
 
41
                static Pixbuf UnknownPixbuf () 
 
42
                {
 
43
                        Pixbuf pb = new Pixbuf (Colorspace.Rgb, true, 8, 1, 1);
 
44
                        pb.Fill (0x00000000);
 
45
                        return pb;
 
46
                }
 
47
                
 
48
                static bool IconIsEmbeddedResource (string name)
 
49
                {
 
50
                        return 0 < name.IndexOf ("@");
 
51
                }
 
52
                
 
53
                static bool IconIsFile (string name)
 
54
                {
 
55
                        return name.StartsWith ("/") ||
 
56
                                   name.StartsWith ("~/") || 
 
57
                                   name.StartsWith ("file://", StringComparison.OrdinalIgnoreCase);
 
58
                }
 
59
                
 
60
                static Pixbuf IconFromEmbeddedResource (string name, int size)
 
61
                {
 
62
                        Pixbuf pixbuf = null;
 
63
                        string resource, assemblyName;
 
64
                        
 
65
                        resource = name.Substring (0, name.IndexOf ("@"));
 
66
                        assemblyName = name.Substring (resource.Length + 1);
 
67
                        try {
 
68
                                foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies ()) {
 
69
                                        if (asm.FullName != assemblyName) continue;
 
70
                                        pixbuf = new Pixbuf (asm, resource, size, size);
 
71
                                        break;
 
72
                                }
 
73
                        } catch (Exception e) {
 
74
                                Log.Error ("Failed to load icon resource {0} from assembly {1}: {2}",
 
75
                                     resource, assemblyName, e.Message); 
 
76
                                Log.Debug (e.StackTrace);
 
77
                                pixbuf = null;
 
78
                        }
 
79
                        return pixbuf;
 
80
                }
 
81
                
 
82
                static Pixbuf IconFromFile (string name, int size)
 
83
                {
 
84
                        Pixbuf pixbuf;
 
85
 
 
86
                        string home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
 
87
                        name = name.Replace ("~", home);
 
88
                        try     {
 
89
                                pixbuf = new Pixbuf (name, size, size);
 
90
                        } catch {
 
91
                                pixbuf = null;
 
92
                        }
 
93
                        return pixbuf;
 
94
                }
 
95
                
 
96
                static Pixbuf IconFromTheme (string name, int size, IconTheme theme)
 
97
                {
 
98
                        Pixbuf pixbuf = null;
 
99
                        string name_noext;
 
100
                        
 
101
                        // We may have to remove the extension.
 
102
                        if (name.Contains (".")) {
 
103
                                name_noext = name.Remove (name.LastIndexOf ("."));
 
104
                        } else {
 
105
                                name_noext = name;
 
106
                        }
 
107
                        try     {
 
108
                                if (theme.HasIcon (name)) {  
 
109
                                        pixbuf = theme.LoadIcon (name, size, 0);
 
110
                                } else if (theme.HasIcon (name_noext)) { 
 
111
                                        pixbuf = theme.LoadIcon (name_noext, size, 0);
 
112
                                } else if (name == "gnome-mime-text-plain" && theme.HasIcon ("gnome-mime-text")) { 
 
113
                                        pixbuf = theme.LoadIcon ("gnome-mime-text", size, 0);
 
114
                                }
 
115
                        } catch {
 
116
                                pixbuf = null;
 
117
                        }
 
118
                
 
119
                        return pixbuf;
 
120
                }
 
121
                
 
122
                static Pixbuf GenericFileIcon (int size)
 
123
                {
 
124
                        Pixbuf pixbuf = null;
 
125
                        if (IconTheme.Default.HasIcon ("gtk-file")) {
 
126
                                try {
 
127
                                        pixbuf = IconTheme.Default.LoadIcon ("gtk-file", size, 0);
 
128
                                } catch {
 
129
                                        pixbuf = null;                                  
 
130
                                }
 
131
                        }
 
132
                        return pixbuf;
 
133
                }
 
134
                
 
135
                public static Pixbuf PixbufFromIconName (string name, int size)
 
136
                {
 
137
                        Pixbuf pixbuf;
 
138
                        PixbufFromIconName (name, size, out pixbuf);
 
139
                        return pixbuf;
 
140
                }
 
141
                
 
142
                public static bool PixbufFromIconName (string name, int size, out Pixbuf pixbuf)                
 
143
                {
 
144
                        if (null == name) throw new ArgumentNullException ("name");
 
145
                        
 
146
                        // The icon can be loaded from a loaded assembly if the icon has
 
147
                        // the format: "resource@assemblyname".
 
148
                        if (IconIsEmbeddedResource (name)) {
 
149
                                pixbuf = IconFromEmbeddedResource (name, size);
 
150
                                if (pixbuf != null) return true;
 
151
                        } 
 
152
 
 
153
                        if (IconIsFile (name)) {
 
154
                                pixbuf = IconFromFile (name, size);
 
155
                                if (pixbuf != null) return true;
 
156
                        }
 
157
 
 
158
                        // Try to load icon from defaul theme.
 
159
                        pixbuf = IconFromTheme (name, size, IconTheme.Default);
 
160
                        if (pixbuf != null) return true;
 
161
 
 
162
                        // Try to load a generic file icon.
 
163
                        if (name.StartsWith ("gnome-mime")) {
 
164
                                pixbuf = GenericFileIcon (size);
 
165
                                if (pixbuf != null) return true;
 
166
                        }
 
167
                        
 
168
                        // After this point, we assume that the caller's icon cannot be found, so we attempt
 
169
                        // to provide a suitable alternative. We return false to indicate that an alternative
 
170
                        // icon selection was made.
 
171
                        
 
172
                        // Try to load a pretty "no icon found" icon.
 
173
                        if (name != MissingIconIcon) {
 
174
                                pixbuf = PixbufFromIconName (MissingIconIcon, size);
 
175
                                if (pixbuf != null) return false;
 
176
                        }
 
177
                        
 
178
                        // If all else fails, use the UnknownPixbuf.
 
179
                        pixbuf = UnknownPixbuf ();
 
180
                        return false;
 
181
                }
 
182
        }
 
183
}