~ubuntu-branches/ubuntu/precise/gnome-do/precise-proposed

« back to all changes in this revision

Viewing changes to Do.Addins/src/Do.UI/IconCache.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-09-14 10:09:40 UTC
  • mto: (0.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20080914100940-kyghudg7py14bu2z
Tags: upstream-0.6.0.0
ImportĀ upstreamĀ versionĀ 0.6.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* IconCache.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.Collections;
 
22
using System.Collections.Generic;
 
23
 
 
24
namespace Do.UI
 
25
{
 
26
        public class IconCache
 
27
        {
 
28
                const int MaxSize = 50;
 
29
                
 
30
                protected Dictionary<string, Entry> cache;
 
31
                
 
32
                public IconCache ()
 
33
                {                       
 
34
                        cache = new Dictionary<string, Entry> ();
 
35
                }
 
36
                
 
37
                public bool TryGetValue (string key, out Gdk.Pixbuf p)
 
38
                {
 
39
                        Entry e = null;
 
40
                        
 
41
                        p = null;
 
42
                        if (cache.TryGetValue (key, out e))
 
43
                                p = e.Icon;
 
44
                        return null != e;
 
45
                }
 
46
                
 
47
                public void Clear ()
 
48
                {
 
49
                        cache.Clear ();
 
50
                }
 
51
                
 
52
                public Gdk.Pixbuf this [string key] {
 
53
                        get {
 
54
                                Entry e;
 
55
                                
 
56
                                if (cache.TryGetValue (key, out e)) {
 
57
                                        e.Time = DateTime.Now;
 
58
                                        return e.Icon;
 
59
                                } else {
 
60
                                        return null;
 
61
                                }
 
62
                        }
 
63
                        set {
 
64
                                if (cache.ContainsKey (key)) {
 
65
                                        cache [key].Time = DateTime.Now;
 
66
                                } else {        
 
67
                                        if (cache.Count > MaxSize) EvictLRU ();
 
68
                                        cache [key] = new Entry (value);
 
69
                                }
 
70
                        }
 
71
                }
 
72
                
 
73
                protected void EvictLRU ()
 
74
                {
 
75
                        string name = null;
 
76
                        Entry least = null;
 
77
                        
 
78
                        foreach (KeyValuePair<string, Entry> e in cache) {
 
79
                                if (least == null || e.Value.Time < least.Time) {
 
80
                                        name = e.Key;
 
81
                                        least = e.Value;
 
82
                                }
 
83
                        }
 
84
                        if (null != name) {
 
85
                                cache.Remove (name);
 
86
                        }
 
87
                }
 
88
                
 
89
                protected class Entry
 
90
                {
 
91
                        public Gdk.Pixbuf Icon;
 
92
                        public DateTime Time;
 
93
                        
 
94
                        
 
95
                        public Entry (Gdk.Pixbuf p)
 
96
                        {
 
97
                                Icon = p;
 
98
                                Time = DateTime.Now;
 
99
                        }
 
100
                }
 
101
        }
 
102
}