~and471/+junk/do-with-docky

« back to all changes in this revision

Viewing changes to Do.Interface.Linux.Docky/src/Docky.Core/Docky.Core.Default/DockletService.cs

  • Committer: rugby471 at gmail
  • Date: 2010-10-15 16:08:38 UTC
  • Revision ID: rugby471@gmail.com-20101015160838-z9m3utbf7bxzb5ty
reverted to before docky removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2009 GNOME Do
 
3
// 
 
4
//  This program is free software: you can redistribute it and/or modify
 
5
//  it under the terms of the GNU General Public License as published by
 
6
//  the Free Software Foundation, either version 3 of the License, or
 
7
//  (at your option) any later version.
 
8
// 
 
9
//  This program is distributed in the hope that it will be useful,
 
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
//  GNU General Public License for more details.
 
13
// 
 
14
//  You should have received a copy of the GNU General Public License
 
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
// 
 
17
 
 
18
using System;
 
19
using System.Collections.Generic;
 
20
using System.Linq;
 
21
 
 
22
using Mono.Addins;
 
23
 
 
24
using Do.Platform;
 
25
 
 
26
using Docky.Core;
 
27
using Docky.Interface;
 
28
 
 
29
namespace Docky.Core.Default
 
30
{
 
31
        
 
32
        
 
33
        public class DockletService : IDockletService
 
34
        {
 
35
                const string ExtensionPath = "/Docky/Docklet";
 
36
                Dictionary<AbstractDockletItem, bool> docklets;
 
37
                
 
38
                #region IDockletService implementation 
 
39
                
 
40
                public event EventHandler AppletVisibilityChanged;
 
41
                
 
42
                public bool ToggleDocklet (AbstractDockletItem docklet)
 
43
                {
 
44
                        if (!docklets.ContainsKey (docklet))
 
45
                                return false;
 
46
                        
 
47
                        if (docklets [docklet])
 
48
                                docklet.Disable ();
 
49
                        else
 
50
                                docklet.Enable ();
 
51
                        
 
52
                        docklets [docklet] = !docklets [docklet];
 
53
                        SaveConfiguration ();
 
54
                        OnAppletVisibilityChanged ();
 
55
                        return true;
 
56
                }
 
57
                
 
58
                public IEnumerable<AbstractDockletItem> Docklets {
 
59
                        get {
 
60
                                return docklets.Keys.OrderBy (d => d.Name);
 
61
                        }
 
62
                }
 
63
                
 
64
                public IEnumerable<AbstractDockletItem> ActiveDocklets {
 
65
                        get {
 
66
                                if (docklets == null)
 
67
                                        yield break;
 
68
                                foreach (AbstractDockletItem adi in docklets
 
69
                                         .Where (kvp => kvp.Value)
 
70
                                         .Select (kvp => kvp.Key)
 
71
                                         .OrderBy (adi => adi.Name))
 
72
                                        yield return adi;
 
73
                        }
 
74
                }
 
75
                
 
76
                #endregion 
 
77
 
 
78
                IPreferences prefs;
 
79
                
 
80
                public static IEnumerable<AbstractDockletItem> MADocklets {
 
81
                        get { return AddinManager.GetExtensionObjects (ExtensionPath).OfType<AbstractDockletItem> (); }
 
82
                }
 
83
                
 
84
                public DockletService()
 
85
                {
 
86
                        prefs = Services.Preferences.Get<DockletService> ();
 
87
                        prefs.PreferencesChanged += HandlePreferencesChanged;
 
88
                        
 
89
                        AddinManager.AddExtensionNodeHandler (ExtensionPath, HandleDockletsChanged);
 
90
                        
 
91
                        BuildDocklets ();
 
92
                }
 
93
                
 
94
                void HandlePreferencesChanged (object sender, PreferencesChangedEventArgs args)
 
95
                {
 
96
                        if (args.Key != "ActiveApplets")
 
97
                                return;
 
98
                        BuildDocklets ();
 
99
                        OnAppletVisibilityChanged ();
 
100
                }
 
101
                
 
102
                void HandleDockletsChanged (object sender, ExtensionNodeEventArgs args)
 
103
                {
 
104
                        BuildDocklets ();
 
105
                        SaveConfiguration ();
 
106
                        OnAppletVisibilityChanged ();
 
107
                }
 
108
                
 
109
                void BuildDocklets ()
 
110
                {
 
111
                        // ToArray this due to lazy evaluation
 
112
                        IEnumerable<AbstractDockletItem> previous = ActiveDocklets.ToArray ();
 
113
                        
 
114
                        IEnumerable<string> visible = VisibleApplets ();
 
115
                        docklets = new Dictionary<AbstractDockletItem, bool> ();
 
116
                        
 
117
                        foreach (AbstractDockletItem adi in MADocklets) {
 
118
                                docklets.Add (adi, visible.Contains (adi.GetType ().Name));
 
119
                        }
 
120
                        
 
121
                        foreach (AbstractDockletItem adi in ActiveDocklets.Where (d => !previous.Contains (d))) {
 
122
                                adi.Enable ();
 
123
                        }
 
124
                }
 
125
                
 
126
                IEnumerable<string> VisibleApplets ()
 
127
                {
 
128
                        string configString = prefs.Get ("ActiveApplets", "ClockDockItem;");
 
129
                        return configString.Split (';');
 
130
                }
 
131
                
 
132
                void SaveConfiguration ()
 
133
                {
 
134
                        string s = "";
 
135
                        foreach (AbstractDockletItem abi in ActiveDocklets) {
 
136
                                s += abi.GetType ().Name + ";";
 
137
                        }
 
138
                        if (s != prefs.Get ("ActiveApplets", "ClockDockItem;"))
 
139
                                prefs.Set ("ActiveApplets", s);
 
140
                }
 
141
                
 
142
                void OnAppletVisibilityChanged ()
 
143
                {
 
144
                        if (AppletVisibilityChanged != null)
 
145
                                AppletVisibilityChanged (this, EventArgs.Empty);
 
146
                }
 
147
 
 
148
                #region IDisposable implementation 
 
149
                
 
150
                public void Dispose ()
 
151
                {
 
152
                        AddinManager.RemoveExtensionNodeHandler (ExtensionPath, HandleDockletsChanged);
 
153
                }
 
154
                
 
155
                #endregion 
 
156
                
 
157
        }
 
158
}