2
// Copyright (C) 2009 GNOME Do
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.
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.
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/>.
19
using System.Collections.Generic;
27
using Docky.Interface;
29
namespace Docky.Core.Default
33
public class DockletService : IDockletService
35
const string ExtensionPath = "/Docky/Docklet";
36
Dictionary<AbstractDockletItem, bool> docklets;
38
#region IDockletService implementation
40
public event EventHandler AppletVisibilityChanged;
42
public bool ToggleDocklet (AbstractDockletItem docklet)
44
if (!docklets.ContainsKey (docklet))
47
if (docklets [docklet])
52
docklets [docklet] = !docklets [docklet];
54
OnAppletVisibilityChanged ();
58
public IEnumerable<AbstractDockletItem> Docklets {
60
return docklets.Keys.OrderBy (d => d.Name);
64
public IEnumerable<AbstractDockletItem> ActiveDocklets {
68
foreach (AbstractDockletItem adi in docklets
69
.Where (kvp => kvp.Value)
70
.Select (kvp => kvp.Key)
71
.OrderBy (adi => adi.Name))
80
public static IEnumerable<AbstractDockletItem> MADocklets {
81
get { return AddinManager.GetExtensionObjects (ExtensionPath).OfType<AbstractDockletItem> (); }
84
public DockletService()
86
prefs = Services.Preferences.Get<DockletService> ();
87
prefs.PreferencesChanged += HandlePreferencesChanged;
89
AddinManager.AddExtensionNodeHandler (ExtensionPath, HandleDockletsChanged);
94
void HandlePreferencesChanged (object sender, PreferencesChangedEventArgs args)
96
if (args.Key != "ActiveApplets")
99
OnAppletVisibilityChanged ();
102
void HandleDockletsChanged (object sender, ExtensionNodeEventArgs args)
105
SaveConfiguration ();
106
OnAppletVisibilityChanged ();
109
void BuildDocklets ()
111
// ToArray this due to lazy evaluation
112
IEnumerable<AbstractDockletItem> previous = ActiveDocklets.ToArray ();
114
IEnumerable<string> visible = VisibleApplets ();
115
docklets = new Dictionary<AbstractDockletItem, bool> ();
117
foreach (AbstractDockletItem adi in MADocklets) {
118
docklets.Add (adi, visible.Contains (adi.GetType ().Name));
121
foreach (AbstractDockletItem adi in ActiveDocklets.Where (d => !previous.Contains (d))) {
126
IEnumerable<string> VisibleApplets ()
128
string configString = prefs.Get ("ActiveApplets", "ClockDockItem;");
129
return configString.Split (';');
132
void SaveConfiguration ()
135
foreach (AbstractDockletItem abi in ActiveDocklets) {
136
s += abi.GetType ().Name + ";";
138
if (s != prefs.Get ("ActiveApplets", "ClockDockItem;"))
139
prefs.Set ("ActiveApplets", s);
142
void OnAppletVisibilityChanged ()
144
if (AppletVisibilityChanged != null)
145
AppletVisibilityChanged (this, EventArgs.Empty);
148
#region IDisposable implementation
150
public void Dispose ()
152
AddinManager.RemoveExtensionNodeHandler (ExtensionPath, HandleDockletsChanged);