3
// Copyright (C) 2008 GNOME Do
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.
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.
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/>.
20
using System.Collections.Generic;
29
using Do.Interface.CairoUtils;
32
using Docky.Utilities;
33
using Docky.Interface.Menus;
35
namespace Docky.Interface
39
public class TrashDockItem : AbstractDockletItem, IRightClickable
41
const string TrashEmptyIcon = "gnome-stock-trash";
42
const string TrashFullIcon = "gnome-stock-trash-full";
44
FileSystemWatcher fsw;
48
return Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "Trash/files/");
52
public override bool IsAcceptingDrops {
56
public override string Name {
57
get { return "Trash"; }
60
protected override string Icon {
62
if (Directory.Exists (Trash) && (Directory.GetFiles (Trash).Any () || Directory.GetDirectories (Trash).Any ()))
64
return TrashEmptyIcon;
68
public TrashDockItem()
70
if (!Directory.Exists (Trash))
71
Directory.CreateDirectory (Trash);
73
SetText (Catalog.GetString ("Trash"));
75
SetupFileSystemWatch ();
78
void SetupFileSystemWatch ()
80
fsw = new FileSystemWatcher (Trash);
81
fsw.IncludeSubdirectories = false;
82
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName;
84
fsw.Changed += HandleChanged;
85
fsw.Created += HandleChanged;
86
fsw.Deleted += HandleChanged;
87
fsw.EnableRaisingEvents = true;
90
void HandleChanged(object sender, FileSystemEventArgs e)
92
Gtk.Application.Invoke (delegate {
97
public override bool ReceiveItem (string item)
99
if (item.StartsWith ("file://"))
100
item = item.Substring ("file://".Length);
102
// if the file doesn't exist for whatever reason, we bail
103
if (!File.Exists (item) && !Directory.Exists (item))
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);
118
public override void Clicked (uint button, ModifierType state, Cairo.PointD position)
121
Services.Environment.OpenUrl ("trash://");
122
AnimationType = ClickAnimationType.Bounce;
124
AnimationType = ClickAnimationType.None;
127
base.Clicked (button, state, position);
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,
143
md.AddButton ("_Cancel", ResponseType.Cancel);
144
md.AddButton ("Empty _Trash", ResponseType.Ok);
146
ResponseType result = (ResponseType) md.Run ();
149
if (result != ResponseType.Cancel &&
150
Directory.Exists (Trash)) {
152
Directory.Delete (Trash, true);
153
Directory.CreateDirectory (Trash);
154
} catch { /* do nothing */ }
157
SetupFileSystemWatch ();
162
#region IRightClickable implementation
164
public event EventHandler RemoveClicked;
166
public IEnumerable<AbstractMenuArgs> GetMenuItems ()
168
yield return new SeparatorMenuButtonArgs ();
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);