~ubuntu-branches/ubuntu/lucid/docky/lucid-proposed

« back to all changes in this revision

Viewing changes to Docky/Docky/ConfigurationWindow.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-02-17 15:10:07 UTC
  • Revision ID: james.westby@ubuntu.com-20100217151007-msxpd0lsj300ndde
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2009 Jason Smith, Robert Dyer
 
3
//  Copyright (C) 2010 Chris Szikszoy
 
4
// 
 
5
//  This program is free software: you can redistribute it and/or modify
 
6
//  it under the terms of the GNU General Public License as published by
 
7
//  the Free Software Foundation, either version 3 of the License, or
 
8
//  (at your option) any later version.
 
9
// 
 
10
//  This program is distributed in the hope that it will be useful,
 
11
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
//  GNU General Public License for more details.
 
14
// 
 
15
//  You should have received a copy of the GNU General Public License
 
16
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
// 
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Collections.ObjectModel;
 
22
using System.IO;
 
23
using System.Linq;
 
24
using System.Text;
 
25
 
 
26
using Cairo;
 
27
using Gdk;
 
28
using GLib;
 
29
using Gnome;
 
30
using Gtk;
 
31
using Mono.Unix;
 
32
 
 
33
using Docky.Interface;
 
34
using Docky.Services;
 
35
using Docky.Widgets;
 
36
using Docky.Items;
 
37
 
 
38
namespace Docky
 
39
{
 
40
        enum Pages : uint {
 
41
                Docks = 0,
 
42
                Docklets,
 
43
                Helpers,
 
44
                NPages
 
45
        }
 
46
        
 
47
        enum HelperShowStates : uint {
 
48
                All = 0,
 
49
                Enabled,
 
50
                Disabled,
 
51
                NStates
 
52
        }
 
53
        
 
54
        enum DockletShowStates : uint {
 
55
                All = 0,
 
56
                Active,
 
57
                Disabled,
 
58
                NStates
 
59
        }
 
60
        
 
61
        public partial class ConfigurationWindow : Gtk.Window
 
62
        {
 
63
                string AutoStartKey = "Hidden";
 
64
                DesktopItem auto_start_item;
 
65
                TileView HelpersTileview, DockletsTileview;
 
66
                Widgets.SearchEntry HelperSearch, DockletSearch;
 
67
                
 
68
                internal static ConfigurationWindow Instance { get; private set; }
 
69
                
 
70
                static ConfigurationWindow () {
 
71
                        Instance = new ConfigurationWindow ();
 
72
                }
 
73
                
 
74
                static Dock activeDock;
 
75
                public Dock ActiveDock {
 
76
                        get { return activeDock; }
 
77
                        private set {
 
78
                                if (activeDock == value)
 
79
                                        return;
 
80
                                
 
81
                                if (activeDock != null)
 
82
                                        activeDock.UnsetActiveGlow ();
 
83
                                
 
84
                                if (value != null)
 
85
                                        value.SetActiveGlow ();
 
86
                                
 
87
                                activeDock = value;
 
88
                                
 
89
                                RefreshDocklets ();
 
90
                                SetupConfigAlignment ();
 
91
                                CheckButtons ();
 
92
                        }
 
93
                }
 
94
                
 
95
                private ConfigurationWindow () : base(Gtk.WindowType.Toplevel)
 
96
                {
 
97
                        this.Build ();
 
98
                        
 
99
                        SkipTaskbarHint = true;
 
100
                        
 
101
                        int i = 0;
 
102
                        foreach (string theme in Docky.Controller.DockThemes) {
 
103
                                theme_combo.AppendText (theme);
 
104
                                if (Docky.Controller.DockTheme == theme) {
 
105
                                        theme_combo.Active = i;
 
106
                                }
 
107
                                i++;
 
108
                        }
 
109
                        
 
110
                        if (Docky.Controller.Docks.Count () == 1)
 
111
                                ActiveDock = Docky.Controller.Docks.First ();
 
112
                        
 
113
                        start_with_computer_checkbutton.Active = IsAutoStartEnabled ();
 
114
                        
 
115
                        // setup docklets {
 
116
                        DockletSearch = new SearchEntry ();
 
117
                        DockletSearch.EmptyMessage = Catalog.GetString ("Search Docklets...");
 
118
                        DockletSearch.InnerEntry.Changed += delegate {
 
119
                                RefreshDocklets ();
 
120
                        };
 
121
                        DockletSearch.Ready = true;
 
122
                        DockletSearch.Show ();
 
123
                        hbox1.PackStart (DockletSearch, true, true, 2);
 
124
                        
 
125
                        DockletsTileview = new TileView ();
 
126
                        DockletsTileview.IconSize = 48;
 
127
                        docklet_scroll.AddWithViewport (DockletsTileview);
 
128
                        // }
 
129
                        
 
130
                        // setup helpers {
 
131
                        HelperSearch = new SearchEntry ();
 
132
                        HelperSearch.EmptyMessage = Catalog.GetString ("Search Helpers...");
 
133
                        HelperSearch.InnerEntry.Changed += delegate {
 
134
                                RefreshHelpers ();
 
135
                        };
 
136
                        HelperSearch.Ready = true;
 
137
                        HelperSearch.Show ();
 
138
                        hbox5.PackStart (HelperSearch, true, true, 2);
 
139
                        
 
140
                        HelpersTileview = new TileView ();
 
141
                        HelpersTileview.IconSize = 48;
 
142
                        helper_scroll.AddWithViewport (HelpersTileview);
 
143
                        
 
144
                        DockServices.Helpers.HelperUninstalled += delegate {
 
145
                                RefreshHelpers ();
 
146
                        };
 
147
                        // }
 
148
                        
 
149
                        ShowAll ();
 
150
                }
 
151
                
 
152
                protected override bool OnDeleteEvent (Event evnt)
 
153
                {
 
154
                        Hide ();
 
155
                        ActiveDock = null;
 
156
                        return true;
 
157
                }
 
158
 
 
159
                protected virtual void OnCloseButtonClicked (object sender, System.EventArgs e)
 
160
                {
 
161
                        Hide ();
 
162
                        ActiveDock = null;
 
163
                }
 
164
                
 
165
                void SetupConfigAlignment ()
 
166
                {
 
167
                        if (config_alignment.Child != null) {
 
168
                                config_alignment.Remove (config_alignment.Child);
 
169
                        }
 
170
                        
 
171
                        if (ActiveDock == null) {
 
172
                                VBox vbox = new VBox ();
 
173
                                
 
174
                                HBox hboxTop = new HBox ();
 
175
                                HBox hboxBottom = new HBox ();
 
176
                                Label label1 = new Gtk.Label (Mono.Unix.Catalog.GetString ("Click on any dock to configure."));
 
177
                                Label label2 = new Gtk.Label (Mono.Unix.Catalog.GetString ("Drag any dock to reposition."));
 
178
                                
 
179
                                vbox.Add (hboxTop);
 
180
                                vbox.Add (label1);
 
181
                                vbox.Add (label2);
 
182
                                vbox.Add (hboxBottom);
 
183
                                
 
184
                                vbox.SetChildPacking (hboxTop, true, true, 0, PackType.Start);
 
185
                                vbox.SetChildPacking (label1, false, false, 0, PackType.Start);
 
186
                                vbox.SetChildPacking (label2, false, false, 0, PackType.Start);
 
187
                                vbox.SetChildPacking (hboxBottom, true, true, 0, PackType.Start);
 
188
                                
 
189
                                config_alignment.Add (vbox);
 
190
                        } else {
 
191
                                config_alignment.Add (ActiveDock.PreferencesWidget);
 
192
                        }
 
193
                        config_alignment.ShowAll ();
 
194
                }
 
195
 
 
196
                protected override void OnShown ()
 
197
                {
 
198
                        foreach (Dock dock in Docky.Controller.Docks) {
 
199
                                dock.EnterConfigurationMode ();
 
200
                                dock.ConfigurationClick += HandleDockConfigurationClick;
 
201
                        }
 
202
                        
 
203
                        if (Docky.Controller.Docks.Count () == 1)
 
204
                                ActiveDock = Docky.Controller.Docks.First ();
 
205
                        
 
206
                        config_notebook.CurrentPage = (int) Pages.Docks;
 
207
                        
 
208
                        KeepAbove = true;
 
209
                        Stick ();
 
210
 
 
211
                        base.OnShown ();
 
212
                }
 
213
 
 
214
                void HandleDockConfigurationClick (object sender, EventArgs e)
 
215
                {
 
216
                        ActiveDock = sender as Dock;
 
217
                }
 
218
 
 
219
                protected override void OnHidden ()
 
220
                {
 
221
                        foreach (Dock dock in Docky.Controller.Docks) {
 
222
                                dock.ConfigurationClick -= HandleDockConfigurationClick;
 
223
                                dock.LeaveConfigurationMode ();
 
224
                                dock.UnsetActiveGlow ();
 
225
                        }
 
226
                        base.OnHidden ();
 
227
                }
 
228
 
 
229
                protected virtual void OnThemeComboChanged (object sender, System.EventArgs e)
 
230
                {
 
231
                        Docky.Controller.DockTheme = theme_combo.ActiveText;
 
232
                        if (Docky.Controller.NumDocks == 1)
 
233
                                ActiveDock = null;
 
234
                }
 
235
        
 
236
                protected virtual void OnDeleteDockButtonClicked (object sender, System.EventArgs e)
 
237
                {
 
238
                        if (!(Docky.Controller.Docks.Count () > 1))
 
239
                                return;
 
240
                        
 
241
                        if (ActiveDock != null) {
 
242
                                Gtk.MessageDialog md = new Gtk.MessageDialog (null, 
 
243
                                                  0,
 
244
                                                  Gtk.MessageType.Warning, 
 
245
                                                  Gtk.ButtonsType.None,
 
246
                                                  "<b><big>" + Catalog.GetString ("Delete the currently selected dock?") + "</big></b>");
 
247
                                md.Icon = DockServices.Drawing.LoadIcon ("docky", 22);
 
248
                                md.SecondaryText = Catalog.GetString ("If you choose to delete the dock, all settings\n" +
 
249
                                        "for the deleted dock will be permanently lost.");
 
250
                                md.Modal = true;
 
251
                                md.KeepAbove = true;
 
252
                                md.Stick ();
 
253
                                
 
254
                                Gtk.Button cancel_button = new Gtk.Button();
 
255
                                cancel_button.CanFocus = true;
 
256
                                cancel_button.CanDefault = true;
 
257
                                cancel_button.Name = "cancel_button";
 
258
                                cancel_button.UseStock = true;
 
259
                                cancel_button.UseUnderline = true;
 
260
                                cancel_button.Label = "gtk-cancel";
 
261
                                cancel_button.Show ();
 
262
                                md.AddActionWidget (cancel_button, Gtk.ResponseType.Cancel);
 
263
                                md.AddButton (Catalog.GetString ("_Delete Dock"), Gtk.ResponseType.Ok);
 
264
                                md.DefaultResponse = Gtk.ResponseType.Cancel;
 
265
                        
 
266
                                if ((ResponseType)md.Run () == Gtk.ResponseType.Ok) {
 
267
                                        Docky.Controller.DeleteDock (ActiveDock);
 
268
                                        if (Docky.Controller.Docks.Count () == 1)
 
269
                                                ActiveDock = Docky.Controller.Docks.First ();
 
270
                                        else
 
271
                                                ActiveDock = null;
 
272
                                }
 
273
                                
 
274
                                md.Destroy ();
 
275
                        }
 
276
                }
 
277
                
 
278
                protected virtual void OnNewDockButtonClicked (object sender, System.EventArgs e)
 
279
                {
 
280
                        Dock newDock = Docky.Controller.CreateDock ();
 
281
                        
 
282
                        if (newDock != null) {
 
283
                                newDock.ConfigurationClick += HandleDockConfigurationClick;
 
284
                                newDock.EnterConfigurationMode ();
 
285
                                ActiveDock = newDock;
 
286
                        }
 
287
                }
 
288
                
 
289
                void CheckButtons ()
 
290
                {
 
291
                        int spotsAvailable = 0;
 
292
                        for (int i = 0; i < Screen.Default.NMonitors; i++)
 
293
                                spotsAvailable += Docky.Controller.PositionsAvailableForDock (i).Count ();
 
294
                        
 
295
                        delete_dock_button.Sensitive = (Docky.Controller.Docks.Count () == 1 || ActiveDock == null) ? false : true;
 
296
                        new_dock_button.Sensitive = (spotsAvailable == 0) ? false : true;
 
297
                }
 
298
 
 
299
                string AutoStartDir {
 
300
                        get { return System.IO.Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "autostart"); }
 
301
                }
 
302
 
 
303
                string AutoStartFileName {
 
304
                        get { return System.IO.Path.Combine (AutoStartDir, "docky.desktop"); }
 
305
                }
 
306
                
 
307
                DesktopItem AutoStartItem {
 
308
                        get {
 
309
                                if (auto_start_item != null)
 
310
                                        return auto_start_item;
 
311
                                
 
312
                                GLib.File file = DockServices.Paths.AutoStartFile;
 
313
                                
 
314
                                try {
 
315
                                        auto_start_item = DesktopItem.NewFromFile (file.Path, DesktopItemLoadFlags.NoTranslations);
 
316
                                } catch (GLib.GException loadException) {
 
317
                                        Log<ConfigurationWindow>.Info ("Unable to load existing autostart file: {0}", loadException.Message);
 
318
                                        Log<ConfigurationWindow>.Info ("Writing new autostart file to {0}", file.Path);
 
319
                                        auto_start_item = DesktopItem.NewFromFile (System.IO.Path.Combine (AssemblyInfo.InstallData, "applications/docky.desktop"), DesktopItemLoadFlags.NoTranslations);
 
320
                                        try {
 
321
                                                if (!file.Parent.Exists)
 
322
                                                        file.Parent.MakeDirectoryWithParents (null);                                            
 
323
                                                
 
324
                                                auto_start_item.Save (file.StringUri (), true);
 
325
                                                auto_start_item.Location = file.StringUri ();
 
326
                                        } catch (Exception e) {
 
327
                                                Log<ConfigurationWindow>.Error ("Failed to write initial autostart file: {0}", e.Message);
 
328
                                        }
 
329
                                }
 
330
                                return auto_start_item;
 
331
                        }
 
332
                }
 
333
 
 
334
                bool IsAutoStartEnabled ()
 
335
                {
 
336
                        if (!AutoStartItem.Exists ()) {
 
337
                                Log<SystemService>.Error ("Could not open autostart file {0}", DockServices.Paths.AutoStartFile.Path);
 
338
                        }
 
339
                        
 
340
                        if (AutoStartItem.AttrExists (AutoStartKey)) {
 
341
                                return !String.Equals (AutoStartItem.GetString (AutoStartKey), "true", StringComparison.OrdinalIgnoreCase);
 
342
                        }
 
343
                        return false;
 
344
                }
 
345
 
 
346
                void SetAutoStartEnabled (bool enabled)
 
347
                {
 
348
                        AutoStartItem.SetBoolean (AutoStartKey, !enabled);
 
349
                        try {
 
350
                                AutoStartItem.Save (null, true);
 
351
                        } catch (Exception e) {
 
352
                                Log<SystemService>.Error ("Failed to update autostart file: {0}", e.Message);
 
353
                        }
 
354
                }
 
355
                
 
356
                protected virtual void OnStartWithComputerCheckbuttonToggled (object sender, System.EventArgs e)
 
357
                {
 
358
                        SetAutoStartEnabled (start_with_computer_checkbutton.Active);
 
359
                }
 
360
 
 
361
                [GLib.ConnectBefore]
 
362
                protected virtual void OnPageSwitch (object o, Gtk.SwitchPageArgs args)
 
363
                {
 
364
                        if (args.PageNum == (int)Pages.Helpers)
 
365
                                RefreshHelpers ();
 
366
                        if (args.PageNum == (int)Pages.Docklets)
 
367
                                RefreshDocklets ();
 
368
                }
 
369
 
 
370
                protected virtual void OnInstallClicked (object sender, System.EventArgs e)
 
371
                {
 
372
                        GLib.File file = null;
 
373
                        Gtk.FileChooserDialog script_chooser = new Gtk.FileChooserDialog ("Helpers", this, FileChooserAction.Open, Gtk.Stock.Cancel, ResponseType.Cancel, Catalog.GetString ("_Select"), ResponseType.Ok);
 
374
                        FileFilter filter = new FileFilter ();
 
375
                        filter.AddPattern ("*.tar");
 
376
                        filter.Name = Catalog.GetString (".tar Archives");
 
377
                        script_chooser.AddFilter (filter);
 
378
                        
 
379
                        if ((ResponseType) script_chooser.Run () == ResponseType.Ok)
 
380
                                file = GLib.FileFactory.NewForPath (script_chooser.Filename);
 
381
 
 
382
                        script_chooser.Destroy ();
 
383
                        
 
384
                        if (file == null)
 
385
                                return;
 
386
                        
 
387
                        Helper installedHelper;
 
388
                        if (DockServices.Helpers.InstallHelper (file.Path, out installedHelper)) {
 
389
                                installedHelper.Data.DataReady += delegate {
 
390
                                        RefreshHelpers ();
 
391
                                };
 
392
                        }
 
393
                }
 
394
 
 
395
                protected virtual void OnShowHelperChanged (object sender, System.EventArgs e)
 
396
                {
 
397
                        RefreshHelpers ();
 
398
                }
 
399
                
 
400
                protected virtual void OnShowDockletChanged (object sender, System.EventArgs e)
 
401
                {
 
402
                        RefreshDocklets ();
 
403
                }
 
404
                
 
405
                void RefreshHelpers ()
 
406
                {
 
407
                        string query = HelperSearch.InnerEntry.Text.ToLower ();
 
408
                        IEnumerable<HelperTile> tiles = DockServices.Helpers.Helpers.Select (h => new HelperTile (h))
 
409
                                .Where (h => h.Name.ToLower ().Contains (query) || h.Description.ToLower ().Contains (query))
 
410
                                .OrderBy (t => t.Name);
 
411
                        
 
412
                        if (helper_show_cmb.Active == (uint) HelperShowStates.Enabled)
 
413
                                tiles = tiles.Where (h => h.Enabled);
 
414
                        else if (helper_show_cmb.Active == (uint) HelperShowStates.Disabled)
 
415
                                tiles = tiles.Where (h => !h.Enabled);
 
416
                        
 
417
                        HelpersTileview.Clear ();
 
418
                        foreach (HelperTile helper in tiles) {
 
419
                                HelpersTileview.AppendTile (helper);
 
420
                        }
 
421
                }
 
422
                
 
423
                void RefreshDocklets ()
 
424
                {
 
425
                        if (DockletsTileview == null)
 
426
                                return;
 
427
                        DockletsTileview.Clear ();
 
428
                        
 
429
                        if (ActiveDock == null)
 
430
                                return;
 
431
                        
 
432
                        string query = DockletSearch.InnerEntry.Text.ToLower ();
 
433
                        // build a list of DockletTiles, starting with the currently active tiles for the active dock,
 
434
                        // and the available addins
 
435
                        List<DockletTile> tiles = new List<DockletTile> ();
 
436
                        
 
437
                        foreach (AbstractDockItemProvider provider in ActiveDock.Preferences.ItemProviders) {
 
438
                                string providerID = PluginManager.AddinIDFromProvider (provider);
 
439
                                if (string.IsNullOrEmpty (providerID))
 
440
                                    continue;
 
441
 
 
442
                                tiles.Add (new DockletTile (providerID, provider));
 
443
                        }
 
444
                        
 
445
                        tiles = tiles.Concat (PluginManager.AvailableProviderIDs.Select (id => new DockletTile (id))).ToList ();
 
446
                        
 
447
                        if (docklet_show_cmb.Active == (int) DockletShowStates.Active)
 
448
                                tiles = tiles.Where (t => t.Enabled).ToList ();
 
449
                        else if (docklet_show_cmb.Active == (int) DockletShowStates.Disabled)
 
450
                                tiles = tiles.Where (t => !t.Enabled).ToList ();
 
451
                        
 
452
                        tiles = tiles.Where (t => t.Description.ToLower ().Contains (query) || t.Name.ToLower ().Contains (query)).ToList ();
 
453
                        
 
454
                        foreach (DockletTile docklet in tiles) {
 
455
                                DockletsTileview.AppendTile (docklet);
 
456
                        }
 
457
                }
 
458
        }
 
459
}