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
.core
.DefaultPathKindDetector
;
24 import ch
.cyberduck
.core
.Host
;
25 import ch
.cyberduck
.core
.HostParser
;
26 import ch
.cyberduck
.core
.LocalFactory
;
27 import ch
.cyberduck
.core
.LocaleFactory
;
28 import ch
.cyberduck
.core
.Path
;
29 import ch
.cyberduck
.core
.PathKindDetector
;
30 import ch
.cyberduck
.core
.PathNormalizer
;
31 import ch
.cyberduck
.core
.local
.BrowserLauncherFactory
;
32 import ch
.cyberduck
.core
.preferences
.PreferencesFactory
;
33 import ch
.cyberduck
.core
.transfer
.DownloadTransfer
;
34 import ch
.cyberduck
.core
.transfer
.Transfer
;
36 import org
.apache
.commons
.lang3
.StringUtils
;
37 import org
.rococoa
.cocoa
.foundation
.NSRect
;
39 import java
.util
.EnumSet
;
44 public class DownloadController
extends AlertController
{
46 protected NSTextField urlField
47 = NSTextField
.textfieldWithFrame(new NSRect(0, 22));
49 private PathKindDetector detector
= new DefaultPathKindDetector();
52 public void beginSheet() {
53 this.setAccessoryView(urlField
);
54 this.updateField(urlField
, url
);
55 alert
.setShowsHelp(true);
61 public DownloadController(final WindowController parent
) {
62 this(parent
, StringUtils
.EMPTY
);
65 public DownloadController(final WindowController parent
, final String url
) {
66 super(parent
, NSAlert
.alert(
67 LocaleFactory
.localizedString("New Download", "Download"),
68 LocaleFactory
.localizedString("URL", "Download"),
69 LocaleFactory
.localizedString("Download", "Download"),
71 LocaleFactory
.localizedString("Cancel", "Download")
72 ), NSAlert
.NSInformationalAlertStyle
);
77 public void callback(final int returncode
) {
78 if(returncode
== DEFAULT_OPTION
) {
79 final Host host
= HostParser
.parse(urlField
.stringValue());
80 final Path file
= new Path(PathNormalizer
.normalize(host
.getDefaultPath(), true),
81 EnumSet
.of(detector
.detect(host
.getDefaultPath())));
82 host
.setDefaultPath(file
.getParent().getAbsolute());
83 final Transfer transfer
= new DownloadTransfer(host
, file
,
84 LocalFactory
.get(PreferencesFactory
.get().getProperty("queue.download.folder"), file
.getName()));
85 TransferControllerFactory
.get().start(transfer
);
90 protected void focus() {
91 // Focus accessory view.
92 urlField
.selectText(null);
93 this.window().makeFirstResponder(urlField
);
97 protected boolean validateInput() {
98 Host host
= HostParser
.parse(urlField
.stringValue());
99 return StringUtils
.isNotBlank(host
.getDefaultPath());
103 protected void help() {
104 StringBuilder site
= new StringBuilder(PreferencesFactory
.get().getProperty("website.help"));
105 site
.append("/howto/download");
106 BrowserLauncherFactory
.get().open(site
.toString());