Skip to content

Commit b127c33

Browse files
committed
Add implementation using alerts.
1 parent 531bee1 commit b127c33

File tree

5 files changed

+259
-302
lines changed

5 files changed

+259
-302
lines changed

hub/src/main/java/cloud/katta/model/AccountKeyAndDeviceName.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public class AccountKeyAndDeviceName {
88
private String accountKey;
99
private String deviceName;
10+
private boolean addToKeychain;
1011

1112
public String accountKey() {
1213
return accountKey;
@@ -16,6 +17,10 @@ public String deviceName() {
1617
return deviceName;
1718
}
1819

20+
public boolean addToKeychain() {
21+
return addToKeychain;
22+
}
23+
1924
public AccountKeyAndDeviceName withAccountKey(final String accountKey) {
2025
this.accountKey = accountKey;
2126
return this;
@@ -25,4 +30,9 @@ public AccountKeyAndDeviceName withDeviceName(final String deviceName) {
2530
this.deviceName = deviceName;
2631
return this;
2732
}
33+
34+
public AccountKeyAndDeviceName withAddToKeychain(final boolean addToKeychain) {
35+
this.addToKeychain = addToKeychain;
36+
return this;
37+
}
2838
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 2025 shift7 GmbH. All rights reserved.
3+
*/
4+
5+
package cloud.katta.controller;
6+
7+
import ch.cyberduck.binding.Action;
8+
import ch.cyberduck.binding.AlertController;
9+
import ch.cyberduck.binding.Outlet;
10+
import ch.cyberduck.binding.application.NSAlert;
11+
import ch.cyberduck.binding.application.NSCell;
12+
import ch.cyberduck.binding.application.NSControl;
13+
import ch.cyberduck.binding.application.NSImage;
14+
import ch.cyberduck.binding.application.NSSecureTextField;
15+
import ch.cyberduck.binding.application.NSTextField;
16+
import ch.cyberduck.binding.application.NSView;
17+
import ch.cyberduck.binding.application.SheetCallback;
18+
import ch.cyberduck.binding.foundation.NSNotification;
19+
import ch.cyberduck.binding.foundation.NSNotificationCenter;
20+
import ch.cyberduck.core.LocaleFactory;
21+
import ch.cyberduck.core.StringAppender;
22+
import ch.cyberduck.core.preferences.PreferencesFactory;
23+
import ch.cyberduck.core.resources.IconCacheFactory;
24+
25+
import org.apache.commons.lang3.StringUtils;
26+
import org.rococoa.Foundation;
27+
28+
import cloud.katta.model.AccountKeyAndDeviceName;
29+
30+
public class DeviceSetupController extends AlertController {
31+
32+
private final AccountKeyAndDeviceName accountKeyAndDeviceName;
33+
34+
@Outlet
35+
private final NSTextField accountKeyField = NSSecureTextField.textFieldWithString(StringUtils.EMPTY);
36+
37+
@Outlet
38+
private final NSTextField deviceNameField = NSTextField.textFieldWithString(StringUtils.EMPTY);
39+
40+
public DeviceSetupController(final AccountKeyAndDeviceName accountKeyAndDeviceName) {
41+
this.accountKeyAndDeviceName = accountKeyAndDeviceName;
42+
}
43+
44+
@Override
45+
public NSAlert loadAlert() {
46+
final NSAlert alert = NSAlert.alert();
47+
alert.setIcon(IconCacheFactory.<NSImage>get().iconNamed("cryptomator.tiff", 64));
48+
alert.setAlertStyle(NSAlert.NSInformationalAlertStyle);
49+
alert.setMessageText(LocaleFactory.localizedString("Authorization Required", "Hub"));
50+
alert.setInformativeText(new StringAppender()
51+
.append(LocaleFactory.localizedString("This is your first login on this device.", "Hub"))
52+
.append(LocaleFactory.localizedString("Your Account Key is required to link this browser to your account.", "Hub")).toString());
53+
alert.setShowsSuppressionButton(true);
54+
alert.suppressionButton().setTitle(LocaleFactory.localizedString("Add to Keychain", "Login"));
55+
alert.suppressionButton().setState(PreferencesFactory.get().getBoolean("cryptomator.vault.keychain") ? NSCell.NSOnState : NSCell.NSOffState);
56+
return alert;
57+
}
58+
59+
@Override
60+
public NSView getAccessoryView(final NSAlert alert) {
61+
final NSView accessoryView = NSView.create();
62+
{
63+
accountKeyField.cell().setPlaceholderString(LocaleFactory.localizedString("Account Key", "Hub"));
64+
accountKeyField.setToolTip(LocaleFactory.localizedString("Your Account Key is required to authorize this device.", "Hub"));
65+
NSNotificationCenter.defaultCenter().addObserver(this.id(),
66+
Foundation.selector("accountKeyFieldTextDidChange:"),
67+
NSControl.NSControlTextDidChangeNotification,
68+
accountKeyField.id());
69+
this.addAccessorySubview(accessoryView, accountKeyField);
70+
}
71+
{
72+
updateField(deviceNameField, accountKeyAndDeviceName.deviceName(), TRUNCATE_MIDDLE_ATTRIBUTES);
73+
deviceNameField.cell().setPlaceholderString(LocaleFactory.localizedString("Device Name", "Hub"));
74+
deviceNameField.setToolTip(LocaleFactory.localizedString("Name this device for easy identification in your authorized devices list.", "Hub"));
75+
NSNotificationCenter.defaultCenter().addObserver(this.id(),
76+
Foundation.selector("deviceNameFieldTextDidChange:"),
77+
NSControl.NSControlTextDidChangeNotification,
78+
deviceNameField.id());
79+
this.addAccessorySubview(accessoryView, deviceNameField);
80+
}
81+
return accessoryView;
82+
}
83+
84+
@Override
85+
protected void focus(final NSAlert alert) {
86+
super.focus(alert);
87+
window.makeFirstResponder(accountKeyField);
88+
}
89+
90+
@Override
91+
public boolean validate(final int option) {
92+
if(SheetCallback.DEFAULT_OPTION == option) {
93+
return StringUtils.isNotBlank(accountKeyField.stringValue());
94+
}
95+
return true;
96+
}
97+
98+
@Override
99+
public void callback(final int returncode) {
100+
if(SheetCallback.DEFAULT_OPTION == returncode) {
101+
accountKeyAndDeviceName.withAddToKeychain(this.isSuppressed());
102+
}
103+
}
104+
105+
@Action
106+
public void accountKeyFieldTextDidChange(final NSNotification sender) {
107+
accountKeyAndDeviceName.withAccountKey(StringUtils.trim(accountKeyField.stringValue()));
108+
}
109+
110+
@Action
111+
public void deviceNameFieldTextDidChange(final NSNotification sender) {
112+
accountKeyAndDeviceName.withDeviceName(StringUtils.trim(deviceNameField.stringValue()));
113+
}
114+
}
115+

osx/src/main/java/cloud/katta/controller/DeviceSetupWithAccountKeyController.java

Lines changed: 0 additions & 161 deletions
This file was deleted.

0 commit comments

Comments
 (0)