~and471/+junk/do-with-docky

« back to all changes in this revision

Viewing changes to Do.Interface.Linux.Docky/src/Docky.Interface/Docky.Interface.Items/TrashDockItem.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
// TrashDockItem.cs
 
2
// 
 
3
// Copyright (C) 2008 GNOME Do
 
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.IO;
 
22
using System.Linq;
 
23
 
 
24
using Gdk;
 
25
using Gtk;
 
26
using Mono.Unix;
 
27
 
 
28
using Do.Interface;
 
29
using Do.Interface.CairoUtils;
 
30
using Do.Platform;
 
31
 
 
32
using Docky.Utilities;
 
33
using Docky.Interface.Menus;
 
34
 
 
35
namespace Docky.Interface
 
36
{
 
37
        
 
38
        
 
39
        public class TrashDockItem :  AbstractDockletItem, IRightClickable
 
40
        {
 
41
                const string TrashEmptyIcon = "gnome-stock-trash";
 
42
                const string TrashFullIcon = "gnome-stock-trash-full";
 
43
                
 
44
                FileSystemWatcher fsw;
 
45
                
 
46
                string Trash {
 
47
                        get { 
 
48
                                return Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "Trash/files/");
 
49
                        }
 
50
                }
 
51
                
 
52
                public override bool IsAcceptingDrops {
 
53
                        get { return true; }
 
54
                }
 
55
                
 
56
                public override string Name {
 
57
                        get { return "Trash"; }
 
58
                }
 
59
 
 
60
                protected override string Icon {
 
61
                        get {
 
62
                                if (Directory.Exists (Trash) && (Directory.GetFiles (Trash).Any () || Directory.GetDirectories (Trash).Any ()))
 
63
                                        return TrashFullIcon;
 
64
                                return TrashEmptyIcon;
 
65
                        }
 
66
                }
 
67
                
 
68
                public TrashDockItem()
 
69
                {
 
70
                        if (!Directory.Exists (Trash))
 
71
                                Directory.CreateDirectory (Trash);
 
72
 
 
73
                        SetText (Catalog.GetString ("Trash"));
 
74
                        
 
75
                        SetupFileSystemWatch ();
 
76
                }
 
77
                
 
78
                void SetupFileSystemWatch ()
 
79
                {
 
80
                        fsw = new FileSystemWatcher (Trash);
 
81
                        fsw.IncludeSubdirectories = false;
 
82
                        fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName;
 
83
 
 
84
                        fsw.Changed += HandleChanged;
 
85
                        fsw.Created += HandleChanged;
 
86
                        fsw.Deleted += HandleChanged;
 
87
                        fsw.EnableRaisingEvents = true;
 
88
                }
 
89
 
 
90
                void HandleChanged(object sender, FileSystemEventArgs e)
 
91
                {
 
92
                        Gtk.Application.Invoke (delegate {
 
93
                                RedrawIcon ();
 
94
                        });
 
95
                }
 
96
 
 
97
                public override bool ReceiveItem (string item)
 
98
                {
 
99
                        if (item.StartsWith ("file://"))
 
100
                                item = item.Substring ("file://".Length);
 
101
                        
 
102
                        // if the file doesn't exist for whatever reason, we bail
 
103
                        if (!File.Exists (item) && !Directory.Exists (item))
 
104
                                return false;
 
105
                        
 
106
                        try {
 
107
                                Services.Environment.Execute (string.Format ("gvfs-trash \"{0}\"", item));
 
108
                        } catch (Exception e) { 
 
109
                                Log.Error (e.Message);
 
110
                                Log.Error ("Could not move {0} to trash", item); 
 
111
                                return false;
 
112
                        }
 
113
                        
 
114
                        RedrawIcon ();
 
115
                        return true;
 
116
                }
 
117
                
 
118
                public override void Clicked (uint button, ModifierType state, Cairo.PointD position)
 
119
                {
 
120
                        if (button == 1) {
 
121
                                Services.Environment.OpenUrl ("trash://");
 
122
                                AnimationType = ClickAnimationType.Bounce;
 
123
                        } else {
 
124
                                AnimationType = ClickAnimationType.None;
 
125
                        }
 
126
                        
 
127
                        base.Clicked (button, state, position);
 
128
                }
 
129
 
 
130
                void EmptyTrash ()
 
131
                {
 
132
                        fsw.Dispose ();
 
133
                        fsw = null;
 
134
                        
 
135
                        string message = Catalog.GetString ("<big><b>Empty all of the items from the trash?</b></big>\n\n" + 
 
136
                                                            "If you choose to empty the trash, all items in it\nwill be permanently lost. " + 
 
137
                                                            "Please note that you\ncan also delete them separately.");
 
138
                        MessageDialog md = new MessageDialog (null, 
 
139
                                                                                                  DialogFlags.Modal,
 
140
                                                                                                  MessageType.Warning, 
 
141
                                                                                                  ButtonsType.None,
 
142
                                                                                                  message);
 
143
                        md.AddButton ("_Cancel", ResponseType.Cancel);
 
144
                        md.AddButton ("Empty _Trash", ResponseType.Ok);
 
145
 
 
146
                        ResponseType result = (ResponseType) md.Run ();
 
147
                        md.Destroy ();
 
148
 
 
149
                        if (result != ResponseType.Cancel && 
 
150
                                Directory.Exists (Trash)) {
 
151
                                try {
 
152
                                        Directory.Delete (Trash, true);
 
153
                                        Directory.CreateDirectory (Trash);
 
154
                                } catch { /* do nothing */ }
 
155
                        }
 
156
                                
 
157
                        SetupFileSystemWatch ();
 
158
                        
 
159
                        RedrawIcon ();
 
160
                }
 
161
 
 
162
                #region IRightClickable implementation 
 
163
                
 
164
                public event EventHandler RemoveClicked;
 
165
                
 
166
                public IEnumerable<AbstractMenuArgs> GetMenuItems ()
 
167
                {
 
168
                        yield return new SeparatorMenuButtonArgs ();
 
169
                        
 
170
                        yield return new SimpleMenuButtonArgs (
 
171
                                        () => Services.Environment.OpenUrl ("trash://"),
 
172
                                        Catalog.GetString ("Open Trash"), TrashFullIcon);
 
173
                        yield return new SimpleMenuButtonArgs (EmptyTrash,
 
174
                                        Catalog.GetString ("Empty Trash"), Gtk.Stock.Delete);
 
175
                }
 
176
                
 
177
                #endregion 
 
178
                
 
179
        }
 
180
}