|
| 1 | +const Cinnamon = imports.gi.Cinnamon; |
| 2 | +const Clutter = imports.gi.Clutter; |
| 3 | +const St = imports.gi.St; |
| 4 | +const Pango = imports.gi.Pango; |
| 5 | +const Gio = imports.gi.Gio; |
| 6 | +const GObject = imports.gi.GObject; |
| 7 | +const Gcr = imports.gi.Gcr; |
| 8 | + |
| 9 | +const Dialog = imports.ui.dialog; |
| 10 | +const ModalDialog = imports.ui.modalDialog; |
| 11 | +const CinnamonEntry = imports.ui.cinnamonEntry; |
| 12 | +const CheckBox = imports.ui.checkBox; |
| 13 | +const Util = imports.misc.util; |
| 14 | + |
| 15 | +var KeyringDialog = GObject.registerClass( |
| 16 | +class KeyringDialog extends ModalDialog.ModalDialog { |
| 17 | + _init() { |
| 18 | + super._init({ styleClass: 'prompt-dialog' }); |
| 19 | + |
| 20 | + this.prompt = new Cinnamon.KeyringPrompt(); |
| 21 | + this.prompt.connect('show-password', this._onShowPassword.bind(this)); |
| 22 | + this.prompt.connect('show-confirm', this._onShowConfirm.bind(this)); |
| 23 | + this.prompt.connect('prompt-close', this._onHidePrompt.bind(this)); |
| 24 | + |
| 25 | + let content = new Dialog.MessageDialogContent(); |
| 26 | + |
| 27 | + this.prompt.bind_property('message', |
| 28 | + content, 'title', GObject.BindingFlags.SYNC_CREATE); |
| 29 | + this.prompt.bind_property('description', |
| 30 | + content, 'description', GObject.BindingFlags.SYNC_CREATE); |
| 31 | + |
| 32 | + let passwordBox = new St.BoxLayout({ |
| 33 | + style_class: 'prompt-dialog-password-layout', |
| 34 | + vertical: true, |
| 35 | + }); |
| 36 | + |
| 37 | + this._passwordEntry = new St.Entry({ |
| 38 | + style_class: 'prompt-dialog-password-entry', |
| 39 | + can_focus: true, |
| 40 | + x_align: Clutter.ActorAlign.CENTER, |
| 41 | + }); |
| 42 | + this._passwordEntry.clutter_text.set_password_char('\u25cf'); // ● U+25CF BLACK CIRCLE |
| 43 | + CinnamonEntry.addContextMenu(this._passwordEntry, { isPassword: true }); |
| 44 | + this._passwordEntry.clutter_text.connect('activate', this._onPasswordActivate.bind(this)); |
| 45 | + this.prompt.bind_property('password-visible', |
| 46 | + this._passwordEntry, 'visible', GObject.BindingFlags.SYNC_CREATE); |
| 47 | + passwordBox.add_child(this._passwordEntry); |
| 48 | + |
| 49 | + this._confirmEntry = new St.Entry({ |
| 50 | + style_class: 'prompt-dialog-password-entry', |
| 51 | + can_focus: true, |
| 52 | + x_align: Clutter.ActorAlign.CENTER, |
| 53 | + }); |
| 54 | + this._confirmEntry.clutter_text.set_password_char('\u25cf'); // ● U+25CF BLACK CIRCLE |
| 55 | + CinnamonEntry.addContextMenu(this._confirmEntry, { isPassword: true }); |
| 56 | + this._confirmEntry.clutter_text.connect('activate', this._onConfirmActivate.bind(this)); |
| 57 | + this.prompt.bind_property('confirm-visible', |
| 58 | + this._confirmEntry, 'visible', GObject.BindingFlags.SYNC_CREATE); |
| 59 | + passwordBox.add_child(this._confirmEntry); |
| 60 | + |
| 61 | + this.prompt.set_password_actor(this._passwordEntry.clutter_text); |
| 62 | + this.prompt.set_confirm_actor(this._confirmEntry.clutter_text); |
| 63 | + |
| 64 | + let warningBox = new St.BoxLayout({ vertical: true }); |
| 65 | + |
| 66 | + let capsLockWarning = new CinnamonEntry.CapsLockWarning(); |
| 67 | + let syncCapsLockWarningVisibility = () => { |
| 68 | + capsLockWarning.visible = |
| 69 | + this.prompt.password_visible || this.prompt.confirm_visible; |
| 70 | + }; |
| 71 | + this.prompt.connect('notify::password-visible', syncCapsLockWarningVisibility); |
| 72 | + this.prompt.connect('notify::confirm-visible', syncCapsLockWarningVisibility); |
| 73 | + warningBox.add_child(capsLockWarning); |
| 74 | + |
| 75 | + let warning = new St.Label({ style_class: 'prompt-dialog-error-label' }); |
| 76 | + warning.clutter_text.ellipsize = Pango.EllipsizeMode.NONE; |
| 77 | + warning.clutter_text.line_wrap = true; |
| 78 | + this.prompt.bind_property('warning', |
| 79 | + warning, 'text', GObject.BindingFlags.SYNC_CREATE); |
| 80 | + this.prompt.connect('notify::warning-visible', () => { |
| 81 | + warning.opacity = this.prompt.warning_visible ? 255 : 0; |
| 82 | + }); |
| 83 | + this.prompt.connect('notify::warning', () => { |
| 84 | + if (this._passwordEntry && this.prompt.warning !== '') |
| 85 | + Util.wiggle(this._passwordEntry); |
| 86 | + }); |
| 87 | + warningBox.add_child(warning); |
| 88 | + |
| 89 | + passwordBox.add_child(warningBox); |
| 90 | + content.add_child(passwordBox); |
| 91 | + |
| 92 | + this._choice = new CheckBox.CheckBox2(); |
| 93 | + this.prompt.bind_property('choice-label', this._choice.getLabelActor(), |
| 94 | + 'text', GObject.BindingFlags.SYNC_CREATE); |
| 95 | + this.prompt.bind_property('choice-chosen', this._choice, |
| 96 | + 'checked', GObject.BindingFlags.SYNC_CREATE | GObject.BindingFlags.BIDIRECTIONAL); |
| 97 | + this.prompt.bind_property('choice-visible', this._choice, |
| 98 | + 'visible', GObject.BindingFlags.SYNC_CREATE); |
| 99 | + content.add_child(this._choice); |
| 100 | + |
| 101 | + this.contentLayout.add_child(content); |
| 102 | + |
| 103 | + this._cancelButton = this.addButton({ |
| 104 | + label: '', |
| 105 | + action: this._onCancelButton.bind(this), |
| 106 | + key: Clutter.KEY_Escape, |
| 107 | + }); |
| 108 | + this._continueButton = this.addButton({ |
| 109 | + label: '', |
| 110 | + action: this._onContinueButton.bind(this), |
| 111 | + default: true, |
| 112 | + }); |
| 113 | + |
| 114 | + this.prompt.bind_property('cancel-label', this._cancelButton, |
| 115 | + 'label', GObject.BindingFlags.SYNC_CREATE); |
| 116 | + this.prompt.bind_property('continue-label', this._continueButton, |
| 117 | + 'label', GObject.BindingFlags.SYNC_CREATE); |
| 118 | + } |
| 119 | + |
| 120 | + _updateSensitivity(sensitive) { |
| 121 | + if (this._passwordEntry) |
| 122 | + this._passwordEntry.reactive = sensitive; |
| 123 | + |
| 124 | + if (this._confirmEntry) |
| 125 | + this._confirmEntry.reactive = sensitive; |
| 126 | + |
| 127 | + this._continueButton.can_focus = sensitive; |
| 128 | + this._continueButton.reactive = sensitive; |
| 129 | + } |
| 130 | + |
| 131 | + _ensureOpen() { |
| 132 | + // NOTE: ModalDialog.open() is safe to call if the dialog is |
| 133 | + // already open - it just returns true without side-effects |
| 134 | + if (this.open()) |
| 135 | + return true; |
| 136 | + |
| 137 | + // The above fail if e.g. unable to get input grab |
| 138 | + // |
| 139 | + // In an ideal world this wouldn't happen (because |
| 140 | + // Cinnamon is in complete control of the session) but that's |
| 141 | + // just not how things work right now. |
| 142 | + |
| 143 | + log('keyringPrompt: Failed to show modal dialog.' + |
| 144 | + ' Dismissing prompt request'); |
| 145 | + this.prompt.cancel(); |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + _onShowPassword() { |
| 150 | + this._ensureOpen(); |
| 151 | + this._updateSensitivity(true); |
| 152 | + this._passwordEntry.text = ''; |
| 153 | + this._passwordEntry.grab_key_focus(); |
| 154 | + } |
| 155 | + |
| 156 | + _onShowConfirm() { |
| 157 | + this._ensureOpen(); |
| 158 | + this._updateSensitivity(true); |
| 159 | + this._confirmEntry.text = ''; |
| 160 | + this._continueButton.grab_key_focus(); |
| 161 | + } |
| 162 | + |
| 163 | + _onHidePrompt() { |
| 164 | + this.close(); |
| 165 | + } |
| 166 | + |
| 167 | + _onPasswordActivate() { |
| 168 | + if (this.prompt.confirm_visible) |
| 169 | + this._confirmEntry.grab_key_focus(); |
| 170 | + else |
| 171 | + this._onContinueButton(); |
| 172 | + } |
| 173 | + |
| 174 | + _onConfirmActivate() { |
| 175 | + this._onContinueButton(); |
| 176 | + } |
| 177 | + |
| 178 | + _onContinueButton() { |
| 179 | + this._updateSensitivity(false); |
| 180 | + this.prompt.complete(); |
| 181 | + } |
| 182 | + |
| 183 | + _onCancelButton() { |
| 184 | + this.prompt.cancel(); |
| 185 | + } |
| 186 | +}); |
| 187 | + |
| 188 | +function init() { |
| 189 | + prompter = new Gcr.SystemPrompter(); |
| 190 | + prompter.connect('new-prompt', () => { |
| 191 | + let dialog = new KeyringDialog(); |
| 192 | + return dialog.prompt; |
| 193 | + }); |
| 194 | + |
| 195 | + let connection = Gio.DBus.session; |
| 196 | + prompter.register(connection); |
| 197 | + Gio.bus_own_name_on_connection (connection, 'org.gnome.keyring.SystemPrompter', |
| 198 | + Gio.BusNameOwnerFlags.ALLOW_REPLACEMENT, null, null); |
| 199 | +} |
0 commit comments