3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this source distribution.
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.
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.
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/>.
22
using System.Reflection;
23
using System.Collections.Generic;
30
namespace Do.Interface
32
public static class IconProvider
34
const string MissingIconIcon = "emblem-noread";
35
public const int DefaultIconSize = 80;
37
static IconProvider ()
41
static Pixbuf UnknownPixbuf ()
43
Pixbuf pb = new Pixbuf (Colorspace.Rgb, true, 8, 1, 1);
48
static bool IconIsEmbeddedResource (string name)
50
return 0 < name.IndexOf ("@");
53
static bool IconIsFile (string name)
55
return name.StartsWith ("/") ||
56
name.StartsWith ("~/") ||
57
name.StartsWith ("file://", StringComparison.OrdinalIgnoreCase);
60
static Pixbuf IconFromEmbeddedResource (string name, int size)
63
string resource, assemblyName;
65
resource = name.Substring (0, name.IndexOf ("@"));
66
assemblyName = name.Substring (resource.Length + 1);
68
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies ()) {
69
if (asm.FullName != assemblyName) continue;
70
pixbuf = new Pixbuf (asm, resource, size, size);
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);
82
static Pixbuf IconFromFile (string name, int size)
86
string home = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
87
name = name.Replace ("~", home);
89
pixbuf = new Pixbuf (name, size, size);
96
static Pixbuf IconFromTheme (string name, int size, IconTheme theme)
101
// We may have to remove the extension.
102
if (name.Contains (".")) {
103
name_noext = name.Remove (name.LastIndexOf ("."));
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);
122
static Pixbuf GenericFileIcon (int size)
124
Pixbuf pixbuf = null;
125
if (IconTheme.Default.HasIcon ("gtk-file")) {
127
pixbuf = IconTheme.Default.LoadIcon ("gtk-file", size, 0);
135
public static Pixbuf PixbufFromIconName (string name, int size)
138
PixbufFromIconName (name, size, out pixbuf);
142
public static bool PixbufFromIconName (string name, int size, out Pixbuf pixbuf)
144
if (null == name) throw new ArgumentNullException ("name");
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;
153
if (IconIsFile (name)) {
154
pixbuf = IconFromFile (name, size);
155
if (pixbuf != null) return true;
158
// Try to load icon from defaul theme.
159
pixbuf = IconFromTheme (name, size, IconTheme.Default);
160
if (pixbuf != null) return true;
162
// Try to load a generic file icon.
163
if (name.StartsWith ("gnome-mime")) {
164
pixbuf = GenericFileIcon (size);
165
if (pixbuf != null) return true;
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.
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;
178
// If all else fails, use the UnknownPixbuf.
179
pixbuf = UnknownPixbuf ();