|
| 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 | + |
0 commit comments