1 package ch
.cyberduck
.ui
.cocoa
;
4 * Copyright (c) 2005 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * dkocher@cyberduck.ch
21 import ch
.cyberduck
.binding
.application
.NSAlert
;
22 import ch
.cyberduck
.binding
.application
.NSTextField
;
23 import ch
.cyberduck
.binding
.application
.NSWindow
;
24 import ch
.cyberduck
.core
.DefaultPathKindDetector
;
25 import ch
.cyberduck
.core
.Host
;
26 import ch
.cyberduck
.core
.HostParser
;
27 import ch
.cyberduck
.core
.LocalFactory
;
28 import ch
.cyberduck
.core
.LocaleFactory
;
29 import ch
.cyberduck
.core
.Path
;
30 import ch
.cyberduck
.core
.PathKindDetector
;
31 import ch
.cyberduck
.core
.PathNormalizer
;
32 import ch
.cyberduck
.core
.local
.BrowserLauncherFactory
;
33 import ch
.cyberduck
.core
.preferences
.PreferencesFactory
;
34 import ch
.cyberduck
.core
.transfer
.DownloadTransfer
;
35 import ch
.cyberduck
.core
.transfer
.Transfer
;
37 import org
.apache
.commons
.lang3
.StringUtils
;
38 import org
.rococoa
.cocoa
.foundation
.NSRect
;
40 import java
.util
.EnumSet
;
45 public class DownloadController
extends AlertController
{
47 protected NSTextField urlField
48 = NSTextField
.textfieldWithFrame(new NSRect(0, 22));
50 private PathKindDetector detector
= new DefaultPathKindDetector();
53 protected void beginSheet(final NSWindow window
) {
54 this.setAccessoryView(urlField
);
55 this.updateField(urlField
, url
);
56 alert
.setShowsHelp(true);
57 super.beginSheet(window
);
62 public DownloadController(final WindowController parent
) {
63 this(parent
, StringUtils
.EMPTY
);
66 public DownloadController(final WindowController parent
, final String url
) {
67 super(parent
, NSAlert
.alert(
68 LocaleFactory
.localizedString("New Download", "Download"),
69 LocaleFactory
.localizedString("URL", "Download"),
70 LocaleFactory
.localizedString("Download", "Download"),
72 LocaleFactory
.localizedString("Cancel", "Download")
73 ), NSAlert
.NSInformationalAlertStyle
);
78 public void callback(final int returncode
) {
79 if(returncode
== DEFAULT_OPTION
) {
80 final Host host
= HostParser
.parse(urlField
.stringValue());
81 final Path file
= new Path(PathNormalizer
.normalize(host
.getDefaultPath(), true),
82 EnumSet
.of(detector
.detect(host
.getDefaultPath())));
83 host
.setDefaultPath(file
.getParent().getAbsolute());
84 final Transfer transfer
= new DownloadTransfer(host
, file
,
85 LocalFactory
.get(PreferencesFactory
.get().getProperty("queue.download.folder"), file
.getName()));
86 TransferControllerFactory
.get().start(transfer
);
91 protected void focus() {
92 // Focus accessory view.
93 urlField
.selectText(null);
94 window
.makeFirstResponder(urlField
);
98 protected boolean validateInput() {
99 Host host
= HostParser
.parse(urlField
.stringValue());
100 return StringUtils
.isNotBlank(host
.getDefaultPath());
104 protected void help() {
105 StringBuilder site
= new StringBuilder(PreferencesFactory
.get().getProperty("website.help"));
106 site
.append("/howto/download");
107 BrowserLauncherFactory
.get().open(site
.toString());