| 1 | /* |
| 2 | * Copyright (C) 2020 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "RenameOperation.h" |
| 21 | |
| 22 | RenameOperation::RenameOperation( |
| 23 | const QString &pathToImageSource, |
| 24 | const QString &imageFilename, |
| 25 | const QSharedPointer<INotificationService> ¬ificationService, |
| 26 | const QSharedPointer<IConfig> &config, |
| 27 | QWidget *parent) : |
| 28 | mParent(parent), |
| 29 | mPathToImageSource(pathToImageSource), |
| 30 | mImageFilename(imageFilename), |
| 31 | mNotificationService(notificationService), |
| 32 | mConfig(config) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | RenameResultDto RenameOperation::execute() |
| 37 | { |
| 38 | auto newFilename = getNewFilename(); |
| 39 | if (newFilename.isNull() || newFilename.isEmpty()) { |
| 40 | return RenameResultDto(false, mPathToImageSource); |
| 41 | } |
| 42 | |
| 43 | auto renameSuccessful = rename(newFilename); |
| 44 | |
| 45 | if (renameSuccessful) { |
| 46 | NotifyOperation operation( |
| 47 | tr("Image Renamed"), |
| 48 | tr("Successfully renamed image to %1").arg(newFilename), |
| 49 | NotificationTypes::Information, |
| 50 | mNotificationService, |
| 51 | mConfig); |
| 52 | operation.execute(); |
| 53 | } else { |
| 54 | NotifyOperation operation( |
| 55 | tr("Image Rename Failed"), |
| 56 | tr("Failed to rename image to %1").arg(newFilename), |
| 57 | NotificationTypes::Warning, |
| 58 | mNotificationService, |
| 59 | mConfig); |
| 60 | operation.execute(); |
| 61 | } |
| 62 | |
| 63 | return RenameResultDto(renameSuccessful, mPathToImageSource); |
| 64 | } |
| 65 | |
| 66 | QString RenameOperation::getNewFilename() const |
| 67 | { |
| 68 | QInputDialog dialog; |
| 69 | |
| 70 | dialog.setInputMode(QInputDialog::TextInput); |
| 71 | dialog.setWindowTitle(tr("Rename image")); |
| 72 | dialog.setLabelText(tr("New filename:")); |
| 73 | dialog.setTextEchoMode(QLineEdit::Normal); |
| 74 | dialog.setTextValue(mImageFilename); |
| 75 | dialog.resize(270, 0); |
| 76 | |
| 77 | if (QDialog::Accepted == dialog.exec()) { |
| 78 | return dialog.textValue(); |
| 79 | } |
| 80 | return {}; |
| 81 | } |
| 82 | |
| 83 | bool RenameOperation::rename(const QString &newFilename) |
| 84 | { |
| 85 | auto oldFilename = PathHelper::extractFilename(mPathToImageSource); |
| 86 | auto newPathToImageSource = mPathToImageSource; |
| 87 | newPathToImageSource.replace(oldFilename, newFilename); |
| 88 | |
| 89 | QFile file(mPathToImageSource); |
| 90 | auto renameSuccessful = file.rename(newPathToImageSource); |
| 91 | if (renameSuccessful) { |
| 92 | mPathToImageSource = newPathToImageSource; |
| 93 | } |
| 94 | return renameSuccessful; |
| 95 | } |