New e-d-s backend which indexes all local addressbooks and calendars.
[beagle.git] / Util / KdeUtils.cs
blobf117237c7c4014493602356cf7d43cbf49f452a2
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 Mono.Posix;
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 string icon_theme_default = Syscall.readlink (Path.Combine (icon_prefix, "default.kde"));
68 if (icon_theme_default != null) {
69 if (! icon_theme_default.StartsWith ("/"))
70 icon_theme_default = Path.Combine (icon_prefix, icon_theme_default);
72 if (! Directory.Exists (icon_theme_default) || icon_theme_default == icon_theme_hicolor)
73 icon_theme_default = null;
76 int i = 0;
77 if (icon_theme_default != null)
78 icon_themes [i++] = icon_theme_default;
79 if (icon_theme_hicolor != null)
80 icon_themes [i++] = icon_theme_hicolor;
81 if (i == 0)
82 continue;
84 // Loop through all detected themes
85 foreach (string theme in icon_themes) {
86 if (theme == null)
87 continue;
89 // Try the preset icon sizes
90 foreach (string size in icon_sizes) {
91 string icon_base = Path.Combine (theme, size);
92 if (! Directory.Exists (icon_base))
93 continue;
95 foreach (string icon_subdir in Directory.GetDirectories (icon_base)) {
96 string icon_dir = Path.Combine (icon_base, icon_subdir);
98 // Check for icon existance
99 string icon_path = Path.Combine (icon_dir, icon_name);
100 if (File.Exists (icon_path))
101 return icon_path;
105 // Only search the first valid path that we find
106 break;
108 return null;