~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to Launchpad/src/LaunchpadIcons.cs

  • Committer: djsiegel at gmail
  • Date: 2007-11-07 17:13:21 UTC
  • Revision ID: djsiegel@gmail.com-20071107171321-zgu46ukqlpdx1ypa
Initial files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* LaunchpadIcons.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
 
using System;
20
 
using System.Text.RegularExpressions;
21
 
using System.Collections.Generic;
22
 
using System.IO;
23
 
using System.Reflection;
24
 
 
25
 
using Do.Addins;
26
 
using Do.Universe;
27
 
 
28
 
namespace Do.Launchpad
29
 
{
30
 
        /// <summary>
31
 
        /// Singleton class for saving and storing Launchpad icons.
32
 
        /// When an icon is requested, it is extracted from the resources file
33
 
        /// and saved to a temp directory, and the path to that icon is returned.
34
 
        /// Upon destruction, the class will clear out all its icons.
35
 
        /// </summary>
36
 
        class LaunchpadIcons
37
 
        {
38
 
                public static LaunchpadIcons instance = null;
39
 
                private Dictionary<string, string> iconcache;
40
 
                Assembly asm;
41
 
 
42
 
                private LaunchpadIcons()
43
 
                {
44
 
                        iconcache = new Dictionary<string, string>();
45
 
                        asm = Assembly.GetExecutingAssembly();
46
 
                }
47
 
 
48
 
                public static LaunchpadIcons Instance
49
 
                {
50
 
                        get
51
 
                        {
52
 
                                if (instance == null)
53
 
                                {
54
 
                                        instance = new LaunchpadIcons();
55
 
                                }
56
 
                                return instance;
57
 
                        }
58
 
                }
59
 
 
60
 
                ~LaunchpadIcons()
61
 
                {
62
 
                        foreach (string s in iconcache.Keys)
63
 
                        {
64
 
                                try
65
 
                                {
66
 
                                        if (File.Exists(iconcache[s]))
67
 
                                        {
68
 
                                                File.Delete(iconcache[s]); //TODO: be smarter about this.
69
 
                                        }
70
 
                                }
71
 
                                catch (Exception)
72
 
                                {
73
 
                                }
74
 
                        }
75
 
                }
76
 
 
77
 
                public string GetIconPath(string iconName)
78
 
                {
79
 
                        if (false == iconcache.ContainsKey(iconName))
80
 
                        {
81
 
                                //Extract the icon from the resource file.
82
 
                                Stream icon = asm.GetManifestResourceStream(iconName);
83
 
                                //TODO: Use a facility within gnome-do's core for creating
84
 
                                //temporary icons
85
 
                                if (false == Directory.Exists("/tmp/gnome-do"))
86
 
                                        Directory.CreateDirectory("/tmp/gnome-do");
87
 
                                if (false == Directory.Exists("/tmp/gnome-do/icons"))
88
 
                                        Directory.CreateDirectory("/tmp/gnome-do/icons");
89
 
                                string tmp_filename = Path.Combine("/tmp/gnome-do/icons", iconName);
90
 
                                BinaryReader input = new BinaryReader(icon);
91
 
                                BinaryWriter output = new BinaryWriter(File.OpenWrite(tmp_filename));
92
 
                                
93
 
                                int try_read;
94
 
                                while (true) {
95
 
                                        try {
96
 
                                                try_read = input.ReadInt32();
97
 
                                                output.Write(try_read);
98
 
                                        } catch (Exception) {
99
 
                                                break;
100
 
                                        }
101
 
                                }
102
 
 
103
 
                                input.Close();
104
 
                                output.Close();
105
 
 
106
 
                                iconcache[iconName] = tmp_filename;
107
 
                        }
108
 
                        return iconcache[iconName];
109
 
                }
110
 
        }
111
 
}