-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
TransferPromptModel.cs
283 lines (248 loc) · 10 KB
/
TransferPromptModel.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// Copyright (c) 2010-2014 Yves Langisch. All rights reserved.
// http://cyberduck.ch/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// Bug fixes, suggestions and comments should be sent to:
// yves@cyberduck.ch
//
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Ch.Cyberduck.Core;
using Ch.Cyberduck.Ui.Winforms;
using ch.cyberduck.core;
using ch.cyberduck.core.formatter;
using ch.cyberduck.core.transfer;
using ch.cyberduck.ui.action;
using ch.cyberduck.ui.threading;
using java.util;
using org.apache.log4j;
namespace Ch.Cyberduck.Ui.Controller
{
internal abstract class TransferPromptModel
{
protected static Logger log = Logger.getLogger(typeof (TransferPromptModel).FullName);
protected readonly Transfer Transfer;
private readonly string UNKNOWN = LocaleFactory.localizedString("Unknown");
private readonly Cache _cache = new Cache(int.MaxValue);
private readonly TransferPromptController _controller;
private readonly List<TransferItem> _roots = new List<TransferItem>();
/**
* Selection status map in the prompt
*/
private readonly IDictionary<TransferItem, CheckState> _selected = new Dictionary<TransferItem, CheckState>();
private readonly Session _session;
/**
* Transfer status determined by filters
*/
protected Bitmap AlertIcon = IconCache.Instance.IconForName("alert");
private TransferAction _action;
protected IDictionary<TransferItem, TransferStatus> _status = new Dictionary<TransferItem, TransferStatus>();
protected TransferPromptModel(TransferPromptController controller, Session session, Transfer transfer)
{
_controller = controller;
_session = session;
Transfer = transfer;
_action =
TransferAction.forName(
Preferences.instance()
.getProperty(String.Format("queue.prompt.{0}.action.default", transfer.getType().name())));
}
public virtual void Add(TransferItem item)
{
_roots.Add(item);
}
public bool CanExpand(object t)
{
return ((TransferItem) t).remote.isDirectory();
}
public IEnumerable<TransferItem> ChildrenGetter(object p)
{
TransferItem directory = ((TransferItem) p);
if (null == directory)
{
// Root
if (!_cache.isCached(null))
{
_cache.put(null, new AttributedList(Transfer.getRoots()));
Filter();
}
}
else if (!_cache.isCached(directory.getReference()))
{
_controller.Background(new TransferPromptListAction(this, _controller, _session, directory, Transfer,
_cache));
}
// Return list with filtered files included
AttributedList list = _cache.get(null == directory ? null : directory.getReference());
for (int i = 0; i < list.size(); i++)
{
yield return (TransferItem) list.get(i);
}
}
public object GetName(TransferItem path)
{
return path.remote.getName();
}
public object GetModified(TransferItem path)
{
long modificationDate = path.remote.attributes().getModificationDate();
if (modificationDate != -1)
{
return UserDefaultsDateFormatter.ConvertJavaMillisecondsToDateTime(modificationDate);
}
return UNKNOWN;
}
public object GetSize(TransferItem path)
{
TransferStatus status = GetStatus(path);
return status.getLength();
}
public TransferStatus GetStatus(TransferItem path)
{
if (!_status.ContainsKey(path))
{
// Transfer filter background task has not yet finished
return new TransferStatus();
}
TransferStatus status = _status[path];
return status;
}
public string GetSizeAsString(object size)
{
return SizeFormatterFactory.get().format((long) size);
}
public object GetIcon(TransferItem item)
{
return IconCache.Instance.IconForPath(item.remote, IconCache.IconSize.Small);
}
public bool IsSelected(TransferItem item)
{
if (_selected.ContainsKey(item))
{
return _selected[item] == CheckState.Checked;
}
return true;
}
public CheckState GetCheckState(Object i)
{
TransferItem item = (TransferItem) i;
TransferStatus status = GetStatus(item);
if (status.isSkipped())
{
return CheckState.Unchecked;
}
return IsSelected(item) ? CheckState.Checked : CheckState.Unchecked;
}
public CheckState SetCheckState(object i, CheckState newValue)
{
_selected[(TransferItem) i] = newValue;
return newValue;
}
public object GetWarningImage(TransferItem item)
{
TransferStatus status = GetStatus(item);
if (item.remote.isFile())
{
if (status.getLength() == 0)
{
return AlertIcon;
}
}
return null;
}
public virtual object GetCreateImage(TransferItem item)
{
return null;
}
public virtual object GetSyncGetter(TransferItem item)
{
return null;
}
public bool IsActive(TransferItem item)
{
return _status.ContainsKey(item);
}
/// <summary>
/// Change transfer action and reload list of files
/// </summary>
/// <param name="action">Transfer action</param>
public void SetAction(TransferAction action)
{
_action = action;
Filter();
}
private void Filter()
{
_controller.background(new FilterAction(this, _controller, _session, Transfer, _action, _cache));
}
private class FilterAction : WorkerBackgroundAction
{
public FilterAction(TransferPromptModel model, TransferPromptController controller, Session session,
Transfer transfer, TransferAction action, Cache cache)
: base(
controller, session,
new InnerTransferPromptFilterWorker(model, controller, session, transfer, action, cache))
{
}
private class InnerTransferPromptFilterWorker : TransferPromptFilterWorker
{
private readonly TransferPromptController _controller;
private readonly TransferPromptModel _model;
public InnerTransferPromptFilterWorker(TransferPromptModel model, TransferPromptController controller,
Session session, Transfer transfer, TransferAction action,
Cache cache) : base(session, transfer, action, cache)
{
_model = model;
_controller = controller;
}
public override void cleanup(object result)
{
IDictionary<TransferItem, TransferStatus> map =
Utils.ConvertFromJavaMap<TransferItem, TransferStatus>((Map) result);
_model._status = map;
_controller.ReloadData(_model._roots);
}
}
}
private class TransferPromptListAction : WorkerBackgroundAction
{
public TransferPromptListAction(TransferPromptModel model, TransferPromptController controller,
Session session, TransferItem directory, Transfer transfer, Cache cache)
: base(
controller, session, new InnerTransferPromptListWorker(model, session, transfer, directory, cache))
{
}
private class InnerTransferPromptListWorker : TransferPromptListWorker
{
private readonly Cache _cache;
private readonly TransferItem _directory;
private readonly TransferPromptModel _model;
public InnerTransferPromptListWorker(TransferPromptModel model, Session session, Transfer transfer,
TransferItem directory, Cache cache)
: base(session, transfer, directory.remote, directory.local)
{
_model = model;
_directory = directory;
_cache = cache;
}
public override void cleanup(object list)
{
_cache.put(_directory.getReference(), new AttributedList((List) list));
_model.Filter();
}
}
}
}
}