~ubuntu-branches/debian/sid/docky/sid

« back to all changes in this revision

Viewing changes to StandardPlugins/RecentDocuments/src/RecentFilesProvider.cs

  • Committer: Package Import Robot
  • Author(s): Rico Tzschichholz
  • Date: 2012-01-19 19:03:38 UTC
  • mfrom: (1.1.14) (10.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20120119190338-n44q7tmqsrkudvk7
Tags: 2.1.3-2
* Upload to unstable
* debian/watch:
  + Look for xz tarballs from now on

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2009 Chris Szikszoy, Robert Dyer
 
3
//  Copyright (C) 2010 Robert Dyer
 
4
//  Copyright (C) 2011 Robert Dyer
 
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.Linq;
 
21
using System.Collections.Generic;
 
22
 
 
23
using Mono.Unix;
 
24
 
 
25
using Docky.Items;
 
26
using Docky.Menus;
 
27
using Docky.Services;
 
28
using Docky.Services.Prefs;
 
29
 
 
30
namespace RecentDocuments
 
31
{
 
32
        public class RecentFilesProvider : AbstractDockItemProvider
 
33
        {
 
34
                #region AbstractDockItemProvider implementation
 
35
                
 
36
                public override string Name {
 
37
                        get {
 
38
                                return "RecentFiles";
 
39
                        }
 
40
                }
 
41
                
 
42
                #endregion
 
43
                
 
44
                int NumRecentDocs { get; set; }
 
45
                
 
46
                bool CanClear { get; set; }
 
47
                
 
48
                AbstractDockItem emptyItem = new EmptyRecentItem ();
 
49
                
 
50
                public RecentFilesProvider (IPreferences prefs)
 
51
                {
 
52
                        NumRecentDocs = prefs.Get<int> ("NumRecentDocs", 7);
 
53
                        
 
54
                        RefreshRecentDocs ();
 
55
                        Gtk.RecentManager.Default.Changed += delegate { RefreshRecentDocs (); };
 
56
                }
 
57
                
 
58
                public void RefreshRecentDocs ()
 
59
                {
 
60
                        List<AbstractDockItem> items = new List<AbstractDockItem> ();
 
61
                        
 
62
                        GLib.List recent_items = new GLib.List (Gtk.RecentManager.Default.Items.Handle, typeof(Gtk.RecentInfo), false, false);
 
63
                        IEnumerable<Gtk.RecentInfo> infos = recent_items.OfType<Gtk.RecentInfo> ();
 
64
                        CanClear = recent_items.Count > 0;
 
65
                        
 
66
                        items.Add (emptyItem);
 
67
                        items.AddRange (infos.Where (it => it.Exists ())
 
68
                                                                 .OrderByDescending (f => f.Modified)
 
69
                                                                 .Take (NumRecentDocs)
 
70
                                                                 .Select (f => (AbstractDockItem)FileDockItem.NewFromUri (f.Uri)));
 
71
                        
 
72
                        foreach (Gtk.RecentInfo ri in infos)
 
73
                                ri.Dispose ();
 
74
                        recent_items.Dispose ();
 
75
                        
 
76
                        Items = items;
 
77
                }
 
78
                
 
79
                public override MenuList GetMenuItems (AbstractDockItem item)
 
80
                {
 
81
                        MenuList list = base.GetMenuItems (item);
 
82
                        
 
83
                        list[MenuListContainer.ProxiedItems].RemoveAt (0);
 
84
                        list[MenuListContainer.Footer].Add (new MenuItem (Catalog.GetString ("_Clear Recent Documents..."), "edit-clear", (o, a) => ClearRecent (), !CanClear));
 
85
                        
 
86
                        return list;
 
87
                }
 
88
                
 
89
                void ClearRecent ()
 
90
                {
 
91
                        Gtk.MessageDialog md = new Gtk.MessageDialog (null, 
 
92
                                          0,
 
93
                                          Gtk.MessageType.Warning, 
 
94
                                          Gtk.ButtonsType.None,
 
95
                                          "<b><big>" + Catalog.GetString ("Clear the Recent Documents list?") + "</big></b>");
 
96
                        
 
97
                        md.Title = Catalog.GetString ("Clear Recent Documents");
 
98
                        md.Icon = DockServices.Drawing.LoadIcon ("docky", 22);
 
99
                        md.SecondaryText = Catalog.GetString ("If you clear the Recent Documents list, you clear the following:\n" +
 
100
                                "\u2022 All items from the Places \u2192 Recent Documents menu item.\n" +
 
101
                                "\u2022 All items from the recent documents list in all applications.");
 
102
                        md.Modal = false;
 
103
                        
 
104
                        md.AddButton (Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
 
105
                        md.AddButton (Gtk.Stock.Clear, Gtk.ResponseType.Ok);
 
106
                        md.DefaultResponse = Gtk.ResponseType.Ok;
 
107
 
 
108
                        md.Response += (o, args) => {
 
109
                                if (args.ResponseId != Gtk.ResponseType.Cancel)
 
110
                                        Gtk.RecentManager.Default.PurgeItems ();
 
111
                                md.Destroy ();
 
112
                        };
 
113
                        
 
114
                        md.Show ();
 
115
                }
 
116
        }
 
117
}