-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
TransferForm.cs
453 lines (407 loc) · 17.5 KB
/
TransferForm.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//
// Copyright (c) 2010-2012 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.Drawing.Drawing2D;
using System.Windows.Forms;
using Ch.Cyberduck.Core;
using Ch.Cyberduck.Ui.Controller;
using Ch.Cyberduck.Ui.Winforms.Controls;
using Windows7.DesktopIntegration;
using Windows7.DesktopIntegration.WindowsForms;
using ch.cyberduck.core;
using ch.cyberduck.core.io;
namespace Ch.Cyberduck.Ui.Winforms
{
public partial class TransferForm : BaseForm, ITransferView
{
private static readonly Font FixedFont = new Font("Courier New", 8, FontStyle.Regular);
private static readonly int MaxHeight = 800;
private static readonly int MaxWidth = 800;
private static readonly int MinHeight = 250;
private static readonly int MinWidth = 450;
private string _currentImage = string.Empty;
private ToolStripMenuItem _lastMenuItemClicked;
public TransferForm()
{
InitializeComponent();
MaximumSize = new Size(MaxWidth, MaxHeight);
MinimumSize = new Size(MinWidth, MinHeight);
ConfigureToolbar();
bandwithSplitButton.Image = IconCache.Instance.IconForName("bandwidth", 16);
showToolStripButton.Image = IconCache.Instance.IconForName("reveal");
transferListView.FullRowSelect = false;
transferListView.HeaderStyle = ColumnHeaderStyle.None;
transferListView.ShowGroups = false;
transferListView.View = View.Details;
transferListView.RowHeight = 90;
transferListView.MultiSelect = true;
transferListView.GridLines = false;
transferListView.UseOverlays = false;
dummyColumn.Width = 0;
transferColumn.FillsFreeSpace = true;
transferListView.ItemSelectionChanged += delegate(object sender, ListViewItemSelectionChangedEventArgs e)
{
TransferControl uc =
((TransferControl)
transferListView.GetEmbeddedControl(1, e.ItemIndex));
if (null != uc)
{
uc.Selected = e.IsSelected;
if (e.IsSelected && uc.FocusRemoveAllowed)
{
transferListView.Select();
}
}
};
transferListView.ItemSelectionChanged += (sender, e) => SelectionChangedEvent();
transferListView.ItemsChanged +=
delegate { transferListView.GridLines = transferListView.GetItemCount() > 0; };
//force handle creation
IntPtr intPtr = Handle;
}
public override string[] BundleNames
{
get { return new[] {"Transfer", "Localizable"}; }
}
public string Url
{
set { urlLabel.Text = value; }
}
public string Local
{
set { localLabel.Text = value; }
}
public Image FileIcon
{
set { fileIcon.Image = value; }
}
public int TranscriptHeight
{
get { return splitContainer.Height - splitContainer.SplitterDistance; }
set { splitContainer.SplitterDistance = splitContainer.Height - value; }
}
public bool TranscriptVisible
{
get { return !splitContainer.Panel2Collapsed; }
set { splitContainer.Panel2Collapsed = !value; }
}
public float Bandwidth
{
get
{
foreach (ToolStripItem item in bandwidthMenuStrip.Items)
{
if (item is ToolStripMenuItem)
{
ToolStripMenuItem m = (ToolStripMenuItem) item;
if (m.Checked)
{
return (float) m.Tag;
}
}
}
return BandwidthThrottle.UNLIMITED;
}
set
{
foreach (ToolStripItem item in bandwidthMenuStrip.Items)
{
if (item is ToolStripMenuItem)
{
ToolStripMenuItem m = (ToolStripMenuItem) item;
m.Checked = value.Equals(m.Tag);
if (m.Checked)
{
if (m.Tag.Equals(BandwidthThrottle.UNLIMITED))
{
if (!_currentImage.Equals("bandwidth"))
{
bandwithSplitButton.Image = IconCache.Instance.IconForName("bandwidth", 16);
_currentImage = "bandwidth";
}
}
else
{
if (!_currentImage.Equals("turtle"))
{
bandwithSplitButton.Image = IconCache.Instance.IconForName("turtle");
_currentImage = "turtle";
}
}
}
}
}
}
}
public bool BandwidthEnabled
{
set { bandwithSplitButton.Enabled = value; }
}
public int QueueSize
{
get { return Convert.ToInt32(queueSizeUpDown.Value); }
set { queueSizeUpDown.Value = value; }
}
public void SetModel(IList<IProgressView> model)
{
transferListView.RemoveAllEmbeddedControls();
transferListView.Objects = model;
for (int i = 0; i < model.Count; i++)
{
Control c = model[i] as Control;
if (null != c)
{
c.DoubleClick += delegate { ReloadEvent(); };
transferListView.AddEmbeddedControl((model[i] as Control), 1, i, DockStyle.Fill);
}
}
}
public void SelectTransfer(IProgressView view)
{
transferListView.DeselectAll();
transferListView.EnsureModelVisible(view);
int index = transferListView.IndexOf(view);
transferListView.FocusedItem = transferListView.Items[index];
transferListView.SelectedObject = view;
}
public void AddTransfer(IProgressView view)
{
Control c = view as Control;
if (null != c)
{
transferListView.AddObject(view);
c.DoubleClick += delegate { ReloadEvent(); };
transferListView.AddEmbeddedControl(c, 1, transferListView.GetItemCount() - 1, DockStyle.Fill);
}
}
public void RemoveTransfer(IProgressView view)
{
transferListView.RemoveEmbeddedControl(view as Control);
transferListView.RemoveObject(view);
}
public void AddTranscriptEntry(bool request, string entry)
{
transcriptBox.SelectionFont = FixedFont;
if (request)
{
transcriptBox.SelectionColor = Color.Black;
}
else
{
transcriptBox.SelectionColor = Color.DarkGray;
}
transcriptBox.SelectedText = entry + Environment.NewLine;
transcriptBox.Select(transcriptBox.TextLength, transcriptBox.TextLength);
ScrollToBottom(transcriptBox);
}
public IList<IProgressView> SelectedTransfers
{
get
{
IList<IProgressView> selected = new List<IProgressView>();
foreach (IProgressView o in transferListView.SelectedObjects)
{
selected.Add(o);
}
return selected;
}
}
public event VoidHandler ResumeEvent;
public event VoidHandler ReloadEvent;
public event VoidHandler StopEvent;
public event VoidHandler RemoveEvent;
public event VoidHandler CleanEvent;
public event VoidHandler OpenEvent;
public event VoidHandler ShowEvent;
public event VoidHandler ToggleTranscriptEvent;
public event VoidHandler SelectionChangedEvent;
public event VoidHandler BandwidthChangedEvent;
public event VoidHandler QueueSizeChangedEvent = delegate { };
public event VoidHandler TranscriptHeightChangedEvent = delegate { };
public event ValidateCommand ValidateResumeEvent;
public event ValidateCommand ValidateReloadEvent;
public event ValidateCommand ValidateStopEvent;
public event ValidateCommand ValidateRemoveEvent;
public event ValidateCommand ValidateCleanEvent;
public event ValidateCommand ValidateOpenEvent;
public event ValidateCommand ValidateShowEvent;
public void PopulateBandwidthList(IList<KeyValuePair<float, string>> throttles)
{
for (int index = 0; index < throttles.Count; index++)
{
KeyValuePair<float, string> throttle = throttles[index];
ToolStripMenuItem item = new ToolStripMenuItem(throttle.Value);
item.Tag = throttle.Key;
item.Click += delegate(object sender, EventArgs args)
{
foreach (ToolStripItem i in bandwidthMenuStrip.Items)
{
if (i is ToolStripMenuItem)
{
((ToolStripMenuItem) i).Checked = false;
}
((ToolStripMenuItem) sender).Checked = true;
}
BandwidthChangedEvent();
};
bandwidthMenuStrip.Items.Add(item);
if (index == 0)
{
bandwidthMenuStrip.Items.Add(new ToolStripSeparator());
}
}
}
public void TaskbarBadge(string text)
{
if (null == text)
{
this.SetTaskbarOverlayIcon(null, String.Empty);
}
else
{
Bitmap bm = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(bm);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillEllipse(Brushes.Navy, new Rectangle(0, 0, 15, 15));
if (text.Length == 1)
{
Font f = new Font("Segoe UI", 8, FontStyle.Bold);
g.DrawString(text, f, new SolidBrush(Color.White), 3, 1);
}
else
{
Font f = new Font("Segoe UI", 7, FontStyle.Bold);
g.DrawString(text, f, new SolidBrush(Color.White), 1, 1);
}
this.SetTaskbarOverlayIcon(Icon.FromHandle(bm.GetHicon()), text);
g.Dispose();
bm.Dispose();
}
}
public void UpdateOverallProgressState(double progress, double maximum)
{
if (Utils.IsWin7OrLater)
{
if (progress == 0 || maximum == 0)
{
Windows7Taskbar.SetProgressState(Handle, Windows7Taskbar.ThumbnailProgressState.NoProgress);
}
else
{
Windows7Taskbar.SetProgressState(Handle, Windows7Taskbar.ThumbnailProgressState.Normal);
Windows7Taskbar.SetProgressValue(Handle, (ulong) progress, (ulong) maximum);
}
}
}
public static void ScrollToBottom(RichTextBox richTextBox)
{
NativeMethods.SendMessage(richTextBox.Handle, NativeConstants.WM_VSCROLL, NativeConstants.SB_BOTTOM, 0);
}
private void ConfigureToolbarMenu(ToolStripMenuItem menuItem, ToolStripButton button, String property)
{
menuItem.CheckOnClick = true;
menuItem.Click += delegate
{
button.Visible =
!button.Visible;
Preferences.instance().setProperty(
property,
button.Visible);
};
button.Visible = menuItem.Checked = Preferences.instance().getBoolean(property);
}
private void ConfigureToolbar()
{
ConfigureToolbarMenu(resumeToolStripMenuItem, resumeToolStripButton, "transfer.toolbar.resume");
ConfigureToolbarMenu(reloadToolStripMenuItem, reloadToolStripButton, "transfer.toolbar.reload");
ConfigureToolbarMenu(stopToolStripMenuItem, stopToolStripButton, "transfer.toolbar.stop");
ConfigureToolbarMenu(removeToolStripMenuItem, removeToolStripButton, "transfer.toolbar.remove");
ConfigureToolbarMenu(cleanUpToolStripMenuItem, cleanUptoolStripButton, "transfer.toolbar.cleanup");
ConfigureToolbarMenu(logToolStripMenuItem, logToolStripButton, "transfer.toolbar.log");
ConfigureToolbarMenu(openToolStripMenuItem, openToolStripButton, "transfer.toolbar.open");
ConfigureToolbarMenu(showToolStripMenuItem, showToolStripButton, "transfer.toolbar.show");
Commands.Add(new ToolStripItem[] {resumeToolStripButton}, null, (sender, args) => ResumeEvent(),
() => ValidateResumeEvent());
Commands.Add(new ToolStripItem[] {reloadToolStripButton}, null, (sender, args) => ReloadEvent(),
() => ValidateReloadEvent());
Commands.Add(new ToolStripItem[] {stopToolStripButton}, null, (sender, args) => StopEvent(),
() => ValidateStopEvent());
Commands.Add(new ToolStripItem[] {removeToolStripButton}, null, (sender, args) => RemoveEvent(),
() => ValidateRemoveEvent());
Commands.Add(new ToolStripItem[] {cleanUptoolStripButton}, null, (sender, args) => CleanEvent(),
() => ValidateCleanEvent());
Commands.Add(new ToolStripItem[] {logToolStripButton}, null, (sender, args) => ToggleTranscriptEvent(),
() => true);
Commands.Add(new ToolStripItem[] {openToolStripButton}, null, (sender, args) => OpenEvent(),
() => ValidateOpenEvent());
Commands.Add(new ToolStripItem[] {showToolStripButton}, null, (sender, args) => ShowEvent(),
() => ValidateShowEvent());
}
private void queueSizeUpDown_ValueChanged(object sender, EventArgs e)
{
QueueSizeChangedEvent();
}
private void toolbarMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem is ToolStripMenuItem)
{
_lastMenuItemClicked = (ToolStripMenuItem) e.ClickedItem;
}
}
private void toolbarMenuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = (
e.CloseReason == ToolStripDropDownCloseReason.ItemClicked &&
_lastMenuItemClicked != null);
}
private void bandwithSplitButton_Click(object sender, EventArgs e)
{
bandwithSplitButton.SplitMenuStrip.Show(bandwithSplitButton, new Point(0, bandwithSplitButton.Height),
ToolStripDropDownDirection.BelowRight);
}
private void splitContainer_SplitterMoved(object sender, SplitterEventArgs e)
{
TranscriptHeightChangedEvent();
}
private void transferListView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ReloadEvent();
}
if (e.KeyCode == Keys.Delete)
{
RemoveEvent();
}
}
private void transferListView_KeyPress(object sender, KeyPressEventArgs e)
{
//do not beep
if (e.KeyChar == (char) Keys.Enter)
{
e.Handled = true;
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
Close();
}
}
}