/
FDArchiveBrowser.cs
251 lines (214 loc) · 8.32 KB
/
FDArchiveBrowser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Collections.Generic;
using Gtk;
namespace snowpack
{
public class ArchiveSelectedEventArgs : EventArgs
{
public List<string> archiveIDs;
public List<Int64> paths;
public string restoreTo;
}
public partial class FDArchiveBrowser : Gtk.Window
{
private FDDataStore DataStore;
private Gtk.TreeStore TreeModel;
public event ArchiveRestoreHandler ArchivesSelectedForRestore;
public EventArgs e = null;
public delegate void ArchiveRestoreHandler(FDArchiveBrowser b, ArchiveSelectedEventArgs e);
public FDArchiveBrowser (FDDataStore ds) : base(Gtk.WindowType.Toplevel)
{
this.Build ();
DataStore = ds;
TreeModel = new Gtk.TreeStore(
typeof(string), //file or dirname, 0
typeof(string), //time modified, 1
typeof(string), //time uploaded, 2
typeof(int), //versions, 3
typeof(string), //checksum, 4
typeof(string), //archiveid, 5
typeof(Int64), //id 6
typeof(Int64), //parent 7
typeof(bool) //if expanded 8
);
//Create the tree view columns and misc
TreeViewColumn filename = new TreeViewColumn();
TreeViewColumn modified = new TreeViewColumn();
TreeViewColumn uploaded = new TreeViewColumn();
filename.Title = "Filename";
modified.Title = "Modified";
uploaded.Title = "Uploaded";
Gtk.CellRendererText filenameCell = new Gtk.CellRendererText();
Gtk.CellRendererText modifiedCell = new Gtk.CellRendererText();
Gtk.CellRendererText uploadedCell = new Gtk.CellRendererText();
filename.PackStart(filenameCell, true);
modified.PackStart(modifiedCell, false);
uploaded.PackStart(uploadedCell, false);
filename.AddAttribute(filenameCell, "text", 0);
modified.AddAttribute(modifiedCell, "text", 1);
uploaded.AddAttribute(uploadedCell, "text", 2);
treeview1.AppendColumn(filename);
treeview1.AppendColumn(modified);
treeview1.AppendColumn(uploaded);
treeview1.Model = this.TreeModel;
Gtk.TreeSelection selection = treeview1.Selection;
selection.Mode = Gtk.SelectionMode.Multiple;
treeview1.Selection.Changed += OnTreeview1RowSelected;
//insert root node, and populate the first set of directories/files
TreeIter rootIter = TreeModel.AppendValues ("(root)");
TreeModel.AppendValues (rootIter, "(loading…)");
}
private void LoadDirectories (Gtk.TreeIter parentIter, FDArchiveDirectory parent)
{
List<FDArchiveDirectory> directories = DataStore.GetDirectories(parent);
foreach (FDArchiveDirectory directory in directories)
{
Gtk.TreeIter iter = TreeModel.AppendValues(parentIter, directory.dirname, null, null, 0, null, null, directory.id, directory.parent);
TreeModel.AppendValues(iter, "(loading…)"); //append a "loading" message to force expansion caret
}
}
private void LoadFiles (Gtk.TreeIter parentIter, FDArchiveDirectory parent)
{
List<FDArchiveItem> items = DataStore.GetItems(parent);
foreach(FDArchiveItem item in items)
{
TreeModel.AppendValues (parentIter,
item.filename,
item.modified.ToLocalTime().ToShortDateString() + " " + item.modified.ToLocalTime().ToShortTimeString(),
item.stored.ToLocalTime().ToShortDateString() + " " + item.stored.ToLocalTime().ToShortTimeString(),
0,
item.checksum,
item.archiveID,
item.id,
item.parent);
}
RemoveLoadingItem(parentIter);
}
//clear the "loading" item we use to force expansion caret
private void RemoveLoadingItem(Gtk.TreeIter parentIter)
{
TreeIter loadingNode;
TreeModel.IterChildren(out loadingNode, parentIter);
if((string)TreeModel.GetValue (loadingNode, 0) == "(loading…)")
TreeModel.Remove (ref loadingNode);
return;
}
//the endpoint for clicking the "restore" button in the interface
private void RestoreItems(object o, System.EventArgs e)
{
//ask where we're restoring to
Gtk.FileChooserDialog restoreTo = new Gtk.FileChooserDialog("Where should files be saved?",
this,
FileChooserAction.SelectFolder,
"Cancel",ResponseType.Cancel,
"Restore",ResponseType.Accept);
string restorePath;
if (restoreTo.Run () == (int)ResponseType.Accept)
{
restorePath = restoreTo.Filename;
restoreTo.Destroy();
}
else
{
restoreTo.Destroy();
return;
}
//get the selected items
Gtk.TreeSelection selection = treeview1.Selection;
Gtk.TreePath[] selectionPath = selection.GetSelectedRows();
if(selectionPath.Length < 1) return;
//build our arguments to send to the listener
ArchiveSelectedEventArgs args = new ArchiveSelectedEventArgs();
args.archiveIDs = new List<string>();
args.paths = new List<Int64>();
args.restoreTo = restorePath;
//iterate over the selections
foreach (Gtk.TreePath path in selectionPath)
{
TreeIter iter;
if(!selection.TreeView.Model.GetIter(out iter, path)) continue;
string archiveID = (string)selection.TreeView.Model.GetValue(iter, 5); //if we have an archive id, add it to the list
if(!String.IsNullOrWhiteSpace(archiveID)) args.archiveIDs.Add (archiveID);
else { //otherwise, we're on a directory
Int64 directoryID = (Int64)selection.TreeView.Model.GetValue(iter, 6);
args.paths.Add(directoryID);
}
}
//fire the event
ArchivesSelectedForRestore(this, args);
}
protected void OnTreeview1RowExpanded (object o, Gtk.RowExpandedArgs args)
{
FDArchiveDirectory parent = new FDArchiveDirectory(
(Int64)treeview1.Model.GetValue (args.Iter, 7),
(string)treeview1.Model.GetValue (args.Iter, 0),
(Int64)treeview1.Model.GetValue (args.Iter, 6)
);
if((bool)TreeModel.GetValue(args.Iter, 8)) return;
TreeModel.SetValue(args.Iter, 8, true);
this.LoadDirectories(args.Iter, parent);
this.LoadFiles(args.Iter, parent);
}
//when we select a vault item, retrieve its id (if it has one)
private void OnTreeview1RowSelected (object sender, System.EventArgs e)
{
Gtk.TreeSelection selection = sender as Gtk.TreeSelection;
Gtk.TreePath[] selectionPath = selection.GetSelectedRows();
if(selectionPath.Length < 1) {
buttonRestore.Sensitive = false;
return;
}
buttonRestore.Sensitive = true;
}
protected void OnCloseActionActivated (object sender, System.EventArgs e)
{
this.Destroy ();
}
protected void OnButtonRestoreActivated (object sender, System.EventArgs e)
{
RestoreItems (sender, e);
}
protected void OnButtonRestoreClicked (object sender, System.EventArgs e)
{
RestoreItems (sender, e);
}
//captures the right and left key presses to expand/collapse the tree node
//this exists to make nautilus-style treeview browsing a crazy reality
[GLib.ConnectBefore] //need to do this so i get called first
protected void OnTreeview1KeyPressEvent (object tree, Gtk.KeyPressEventArgs args)
{
TreeSelection selection = treeview1.Selection;
TreePath[] selectionPath = selection.GetSelectedRows();
if(selectionPath.Length > 1) return; //do nothing if there's more than one item selected
foreach (TreePath path in selectionPath)
{
if(args.Event.Key == Gdk.Key.Right)
{
treeview1.ExpandRow(path, false);
}
else if (args.Event.Key == Gdk.Key.Left)
{
if(treeview1.GetRowExpanded(path))
treeview1.CollapseRow(path);
else {
path.Up();
treeview1.SetCursor(path, null, false);
selection.UnselectAll();
selection.SelectPath(path);
}
}
}
//selects the middle column so we don't have to hear a 'bonk' while navigating
//since the treeview selects columns with left/right keys (although invisible and useless for us)
//it will still complain loudly when it 'hits' the end or start, visible or not
TreeSelection innerSelection = treeview1.Selection;
TreePath[] innerTreePath = innerSelection.GetSelectedRows();
if(innerTreePath.Length < 1) return;
innerSelection.UnselectAll();
foreach (TreePath path in innerTreePath)
{
treeview1.SetCursor(innerTreePath[0], treeview1.Columns[1], false);
}
}
}
}