|
| 1 | +/* DING: Desktop Icons New Generation for GNOME Shell |
| 2 | + * |
| 3 | + * Copyright (C) 2019 Sergio Costas (rastersoft@gmail.com) |
| 4 | + * Based on code original (C) Carlos Soriano |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, version 3 of the License. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +/* exported AskRenamePopup */ |
| 19 | +'use strict'; |
| 20 | +const Gtk = imports.gi.Gtk; |
| 21 | +const Gio = imports.gi.Gio; |
| 22 | +const GLib = imports.gi.GLib; |
| 23 | +const Adw = imports.gi.Adw; |
| 24 | +const DBusUtils = imports.dbusUtils; |
| 25 | +const DesktopIconsUtil = imports.desktopIconsUtil; |
| 26 | +const Gettext = imports.gettext.domain('ding'); |
| 27 | +const SignalManager = imports.signalManager; |
| 28 | + |
| 29 | +const _ = Gettext.gettext; |
| 30 | + |
| 31 | +const RENAME_ENTRY_MIN_CHARS=30; |
| 32 | +const RENAME_ENTRY_MAX_CHARS=50; |
| 33 | + |
| 34 | +var AskRenamePopup = class extends SignalManager.SignalManager { |
| 35 | + constructor(extensionManager, fileItem, allowReturnOnSameName, closeCB) { |
| 36 | + super(); |
| 37 | + this._extensionManager = extensionManager |
| 38 | + this._closeCB = closeCB; |
| 39 | + this._allowReturnOnSameName = allowReturnOnSameName; |
| 40 | + this._desktopPath = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DESKTOP); |
| 41 | + this._fileItem = fileItem; |
| 42 | + this._popover = new Gtk.Popover(); |
| 43 | + this._popover.set_parent(fileItem.container); |
| 44 | + this._popover.set_position((fileItem.relativeX > 0.5) ? Gtk.PositionType.LEFT : Gtk.PositionType.RIGHT); |
| 45 | + let contentBox = new Gtk.Box({ |
| 46 | + margin_start: 18, |
| 47 | + margin_end: 18, |
| 48 | + margin_top: 18, |
| 49 | + margin_bottom: 18, |
| 50 | + orientation: Gtk.Orientation.VERTICAL, |
| 51 | + }); |
| 52 | + this._popover.set_child(contentBox); |
| 53 | + const label = new Gtk.Label({ |
| 54 | + label: fileItem.isDirectory ? _('Rename folder') : _('Rename file'), |
| 55 | + justify: Gtk.Justification.CENTER, |
| 56 | + halign: Gtk.Align.CENTER, |
| 57 | + margin_bottom: 12, |
| 58 | + }); |
| 59 | + label.add_css_class("title-2"); |
| 60 | + contentBox.append(label); |
| 61 | + this._textArea = new Gtk.Entry({ |
| 62 | + margin_bottom: 12, |
| 63 | + }); |
| 64 | + this._textArea.update_property([Gtk.AccessibleProperty.LABEL], [_("New filename")]); |
| 65 | + this._textArea.text = fileItem.fileName; |
| 66 | + this._textArea.set_width_chars(DesktopIconsUtil.clamp(fileItem.displayName, RENAME_ENTRY_MIN_CHARS, RENAME_ENTRY_MAX_CHARS)) |
| 67 | + contentBox.append(this._textArea); |
| 68 | + this._button = new Gtk.Button({ |
| 69 | + label: allowReturnOnSameName ? _('OK') : _('Rename'), |
| 70 | + halign: Gtk.Align.END, |
| 71 | + }); |
| 72 | + this._button.add_css_class("suggested-action"); |
| 73 | + contentBox.append(this._button); |
| 74 | + this.connectSignal(this._button, 'clicked', this._do_rename.bind(this)); |
| 75 | + this.connectSignal(this._textArea, 'changed', this._validate.bind(this)); |
| 76 | + this.connectSignal(this._textArea, 'activate', this._do_rename.bind(this)); |
| 77 | + this.connectSignal(this._popover, 'closed', this._cleanAll.bind(this)); |
| 78 | + this._extensionManager.showPopup(); |
| 79 | + //this._textArea.set_can_default(true); |
| 80 | + this._popover.set_default_widget(this._textArea); |
| 81 | + this._button.add_css_class('suggested-action'); |
| 82 | + contentBox.show(); |
| 83 | + this._popover.popup(); |
| 84 | + this._validate(); |
| 85 | + this._textArea.grab_focus_without_selecting(); |
| 86 | + this._textArea.select_region(0, DesktopIconsUtil.getFileExtensionOffset(fileItem.fileName, { 'isDirectory': fileItem.isDirectory }).offset); |
| 87 | + } |
| 88 | + |
| 89 | + _cleanAll() { |
| 90 | + this.disconnectAllSignals(); |
| 91 | + this._extensionManager.hidePopup(); |
| 92 | + this._closeCB(); |
| 93 | + } |
| 94 | + |
| 95 | + updateFileItem(fileItem) { |
| 96 | + this._fileItem = fileItem; |
| 97 | + if (fileItem) { |
| 98 | + this._popover.set_relative_to(this._fileItem._iconContainer); |
| 99 | + this._popover.modal = true; |
| 100 | + this._textArea.set_position(this._cursorPosition); |
| 101 | + } else { |
| 102 | + this._cursorPosition = this._textArea.get_position(); |
| 103 | + this._popover.modal = false; |
| 104 | + this._popover.set_relative_to(null); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + _validate() { |
| 109 | + let text = this._textArea.text; |
| 110 | + let finalPath = `${this._desktopPath}/${text}`; |
| 111 | + let finalFile = Gio.File.new_for_commandline_arg(finalPath); |
| 112 | + if ((text == '') || (text.indexOf('/') !== -1) || |
| 113 | + ((text == this._fileItem.fileName) && !this._allowReturnOnSameName) || |
| 114 | + (finalFile.query_exists(null) && (text !== this._fileItem.fileName))) { |
| 115 | + this._button.sensitive = false; |
| 116 | + } else { |
| 117 | + this._button.sensitive = true; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + _do_rename() { |
| 122 | + if (!this._button.sensitive) { |
| 123 | + return; |
| 124 | + } |
| 125 | + this._popover.popdown(); |
| 126 | + if (this._fileItem.fileName == this._textArea.text) { |
| 127 | + return; |
| 128 | + } |
| 129 | + DBusUtils.RemoteFileOperations.RenameURIRemote( |
| 130 | + this._fileItem.file.get_uri(), this._textArea.text |
| 131 | + ).catch(e => { |
| 132 | + print(e); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + closeWindow() { |
| 137 | + this._popover.popdown(); |
| 138 | + } |
| 139 | +}; |
0 commit comments