renamefiledialog.cpp 8.13 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
 * Copyright (c) 2011-2020 Laurent Montel <montel@kde.org>
3
 *
4
5
6
7
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of this program with any edition of
 *  the Qt library by Trolltech AS, Norway (or with modified versions
 *  of Qt that use the same license as Qt), and distribute linked
 *  combinations including the two.  You must obey the GNU General
 *  Public License in all respects for all of the code used other than
 *  Qt.  If you modify this file, you may extend this exception to
 *  your version of the file, but you are not obligated to do so.  If
 *  you do not wish to do so, delete this exception statement from
 *  your version.
 */
#include "renamefiledialog.h"
Laurent Montel's avatar
Laurent Montel committed
30
#include "kio_version.h"
31
32
33
34
35
36
37
#include <kseparator.h>
#include <QLineEdit>
#include <QPushButton>
#include <KLocalizedString>
#include <KIO/StatJob>
#include <KJobWidgets>
#include <KMessageBox>
38
#include <KStandardGuiItem>
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <QHBoxLayout>
#include <QCheckBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QFileInfo>

using namespace PimCommon;

class Q_DECL_HIDDEN PimCommon::RenameFileDialog::RenameFileDialogPrivate
{
public:
    RenameFileDialogPrivate(const QUrl &_url, RenameFileDialog *qq)
Laurent Montel's avatar
Laurent Montel committed
52
53
        : url(_url)
        , applyAll(nullptr)
54
55
        , renameBtn(nullptr)
        , suggestNewNameBtn(nullptr)
Laurent Montel's avatar
Laurent Montel committed
56
57
        , nameEdit(nullptr)
        , q(qq)
58
59
    {
    }
Laurent Montel's avatar
Laurent Montel committed
60

61
62
63
    QString suggestName(const QUrl &baseURL, const QString &oldName);

    QUrl url;
Laurent Montel's avatar
Laurent Montel committed
64
65
66
67
68
    QCheckBox *applyAll = nullptr;
    QPushButton *renameBtn = nullptr;
    QPushButton *suggestNewNameBtn = nullptr;
    QLineEdit *nameEdit = nullptr;
    RenameFileDialog *q = nullptr;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
};

QString PimCommon::RenameFileDialog::RenameFileDialogPrivate::suggestName(const QUrl &baseURL, const QString &oldName)
{
    QString dotSuffix, suggestedName;
    QString basename = oldName;
    const QChar spacer(QLatin1Char(' '));

    //ignore dots at the beginning, that way "..aFile.tar.gz" will become "..aFile 1.tar.gz" instead of " 1..aFile.tar.gz"
    int index = basename.indexOf(QLatin1Char('.'));
    int continuous = 0;
    while (continuous == index) {
        index = basename.indexOf(QLatin1Char('.'), index + 1);
        ++continuous;
    }

    if (index != -1) {
        dotSuffix = basename.mid(index);
        basename.truncate(index);
    }

    const int pos = basename.lastIndexOf(spacer);

    if (pos != -1) {
        const QString tmp = basename.mid(pos + 1);
        bool ok;
        const int number = tmp.toInt(&ok);

        if (!ok) {  // ok there is no number
            suggestedName = basename + spacer + QLatin1Char('1') + dotSuffix;
        } else {
            // yes there's already a number behind the spacer so increment it by one
            basename.replace(pos + 1, tmp.length(), QString::number(number + 1));
            suggestedName = basename + dotSuffix;
        }
    } else { // no spacer yet
        suggestedName = basename + spacer + QLatin1Char('1') + dotSuffix;
    }

    // Check if suggested name already exists
    bool exists = false;
    // TODO: network transparency. However, using NetAccess from a modal dialog
    // could be a problem, no? (given that it uses a modal widget itself....)
    if (baseURL.isLocalFile()) {
113
        exists = QFileInfo::exists(baseURL.toLocalFile() + QLatin1Char('/') + suggestedName);
114
115
116
117
118
119
120
121
122
123
    }

    if (!exists) {
        return suggestedName;
    } else { // already exists -> recurse
        return suggestName(baseURL, suggestedName);
    }
}

RenameFileDialog::RenameFileDialog(const QUrl &url, bool multiFiles, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
124
125
    : QDialog(parent)
    , d(new RenameFileDialogPrivate(url, this))
126
{
127
    setWindowTitle(i18nc("@title:window", "File Already Exists"));
128
129
130
131
132
133
134
135
136
137
138
139
    QVBoxLayout *pLayout = new QVBoxLayout(this);

    QLabel *label = new QLabel(xi18n("A file named <filename>%1</filename> already exists. Do you want to overwrite it?", url.fileName()), this);
    pLayout->addWidget(label);

    QHBoxLayout *renameLayout = new QHBoxLayout();
    pLayout->addLayout(renameLayout);

    d->nameEdit = new QLineEdit(this);
    renameLayout->addWidget(d->nameEdit);
    d->nameEdit->setClearButtonEnabled(true);
    d->nameEdit->setText(url.fileName());
140
141
142
    d->suggestNewNameBtn = new QPushButton(i18n("Suggest New &Name"), this);
    renameLayout->addWidget(d->suggestNewNameBtn);
    connect(d->suggestNewNameBtn, &QPushButton::clicked, this, &RenameFileDialog::slotSuggestNewNamePressed);
143

144
145
    QPushButton *overWrite = new QPushButton(this);
    KStandardGuiItem::assign(overWrite, KStandardGuiItem::Overwrite);
146
147
148
149
150
    connect(overWrite, &QPushButton::clicked, this, &RenameFileDialog::slotOverwritePressed);

    QPushButton *ignore = new QPushButton(i18n("&Ignore"), this);
    connect(ignore, &QPushButton::clicked, this, &RenameFileDialog::slotIgnorePressed);

151
152
    d->renameBtn = new QPushButton(i18n("&Rename"), this);
    connect(d->renameBtn, &QPushButton::clicked, this, &RenameFileDialog::slotRenamePressed);
153
154
155
156
157
158
159
160
161
162
163
164
165

    KSeparator *separator = new KSeparator(this);
    pLayout->addWidget(separator);

    QHBoxLayout *layout = new QHBoxLayout();
    pLayout->addLayout(layout);

    if (multiFiles) {
        d->applyAll = new QCheckBox(i18n("Appl&y to All"), this);
        connect(d->applyAll, &QCheckBox::clicked, this, &RenameFileDialog::slotApplyAllPressed);
        layout->addWidget(d->applyAll);
        slotApplyAllPressed();
    }
166
    layout->addWidget(d->renameBtn);
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    layout->addWidget(overWrite);
    layout->addWidget(ignore);
}

RenameFileDialog::~RenameFileDialog()
{
    delete d;
}

void RenameFileDialog::slotOverwritePressed()
{
    if (d->applyAll && d->applyAll->isChecked()) {
        done(RENAMEFILE_OVERWRITEALL);
    } else {
        done(RENAMEFILE_OVERWRITE);
    }
}

void RenameFileDialog::slotIgnorePressed()
{
    if (d->applyAll && d->applyAll->isChecked()) {
        done(RENAMEFILE_IGNOREALL);
    } else {
        done(RENAMEFILE_IGNORE);
    }
}

void RenameFileDialog::slotRenamePressed()
{
    if (d->nameEdit->text().isEmpty()) {
        return;
    }

    bool fileExists = false;
    if (newName().isLocalFile()) {
        fileExists = QFile::exists(newName().path());
    } else {
Laurent Montel's avatar
Laurent Montel committed
204
205
//#if KIO_VERSION < QT_VERSION_CHECK(5, 69, 0)
#if KIOCORE_ENABLE_DEPRECATED_SINCE(5, 69)
206
        auto job = KIO::stat(newName(), KIO::StatJob::DestinationSide, 0);
Laurent Montel's avatar
Laurent Montel committed
207
208
209
#else
        auto job = KIO::statDetails(newName(), KIO::StatJob::DestinationSide, KIO::StatDetail::Basic);
#endif
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
        KJobWidgets::setWindow(job, this);
        fileExists = job->exec();
    }

    if (fileExists) {
        KMessageBox::error(this, i18n("This filename \"%1\" already exists.", newName().toDisplayString(QUrl::PreferLocalFile)), i18n("File already exists"));
        return;
    }
    done(RENAMEFILE_RENAME);
}

void RenameFileDialog::slotApplyAllPressed()
{
    const bool enabled(!d->applyAll->isChecked());
    d->nameEdit->setEnabled(enabled);
225
226
    d->suggestNewNameBtn->setEnabled(enabled);
    d->renameBtn->setEnabled(enabled);
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
}

void RenameFileDialog::slotSuggestNewNamePressed()
{
    if (d->nameEdit->text().isEmpty()) {
        return;
    }

    auto destDirectory = d->url.adjusted(QUrl::RemoveFilename);
    d->nameEdit->setText(d->suggestName(destDirectory, d->nameEdit->text()));
}

QUrl RenameFileDialog::newName() const
{
    const QString fileName = d->nameEdit->text();

    auto newDest = d->url.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash);
    newDest.setPath(newDest.path() + QLatin1Char('/') + KIO::encodeFileName(fileName));

    return newDest;
}