cvsimport
[beagle.git] / Util / KdeUtils.cs
blobcea24113adfaeea636309aee1e82ccf588566a63
1 //
2 // KdeUtils.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
28 using System.Diagnostics;
29 using System.IO;
30 using System.Text;
32 namespace Beagle.Util {
33 public class KdeUtils {
35 // No instantiation
36 private KdeUtils () { }
38 private static string [] icon_sizes = { "128x128", "64x64", "48x48", "32x32", "22x22", "16x16" };
39 private static string [] kde_locations = { ExternalStringsHack.KdePrefix, Environment.GetEnvironmentVariable ("KDEDIR"), "/opt/kde3", "/usr" };
40 public static string [] KdeLocations {
41 get { return kde_locations; }
44 // Finds an icon by its name and returns its absolute path, or null if not found.
45 public static string LookupIcon (string icon_name) {
46 foreach (string kde_dir in KdeLocations) {
47 if (kde_dir == null || kde_dir == String.Empty || !Directory.Exists (kde_dir))
48 continue;
50 string kde_share = Path.Combine (kde_dir, "share");
51 string icon_prefix = Path.Combine (kde_share, "icons");
52 string icon_theme_hicolor = Path.Combine (icon_prefix, "hicolor");
53 string [] icon_themes = { null, null };
55 if (! Directory.Exists (icon_theme_hicolor))
56 icon_theme_hicolor = null;
58 // FIXME: We should probably support svg icons at some point
59 if (! icon_name.EndsWith(".png"))
60 icon_name = icon_name + ".png";
62 // We try up to 2 icon themes: we first try the theme pointed at by the
63 // "default.kde" link, and then we try the trusted default "hicolor" theme.
64 // We handle the situations if either (or both) of these aren't present, or
65 // if default.kde == hicolor.
67 StringBuilder icon_theme_default_sb = new StringBuilder ();
68 Mono.Unix.Native.Syscall.readlink (Path.Combine (icon_prefix, "default.kde"), icon_theme_default_sb);
69 string icon_theme_default = icon_theme_default_sb.ToString ();
70 if (icon_theme_default != null) {
71 if (! icon_theme_default.StartsWith ("/"))
72 icon_theme_default = Path.Combine (icon_prefix, icon_theme_default);
74 if (! Directory.Exists (icon_theme_default) || icon_theme_default == icon_theme_hicolor)
75 icon_theme_default = null;
78 int i = 0;
79 if (icon_theme_default != null)
80 icon_themes [i++] = icon_theme_default;
81 if (icon_theme_hicolor != null)
82 icon_themes [i++] = icon_theme_hicolor;
83 if (i == 0)
84 continue;
86 // Loop through all detected themes
87 foreach (string theme in icon_themes) {
88 if (theme == null)
89 continue;
91 // Try the preset icon sizes
92 foreach (string size in icon_sizes) {
93 string icon_base = Path.Combine (theme, size);
94 if (! Directory.Exists (icon_base))
95 continue;
97 foreach (string icon_subdir in Directory.GetDirectories (icon_base)) {
98 string icon_dir = Path.Combine (icon_base, icon_subdir);
100 // Check for icon existance
101 string icon_path = Path.Combine (icon_dir, icon_name);
102 if (File.Exists (icon_path))
103 return icon_path;
107 // Only search the first valid path that we find
108 break;
110 return null;