Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / bittorrent / abstractfilestorage.cpp
blob61f0f75d6a386b5d855362b557ab68b01325869f
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2020 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "abstractfilestorage.h"
31 #include <QDir>
32 #include <QHash>
33 #include <QVector>
35 #include "base/bittorrent/common.h"
36 #include "base/exceptions.h"
37 #include "base/utils/fs.h"
39 #if defined(Q_OS_WIN)
40 const Qt::CaseSensitivity CASE_SENSITIVITY {Qt::CaseInsensitive};
41 #else
42 const Qt::CaseSensitivity CASE_SENSITIVITY {Qt::CaseSensitive};
43 #endif
45 namespace
47 bool areSameFileNames(QString first, QString second)
49 if (first.endsWith(QB_EXT, Qt::CaseInsensitive))
50 first.chop(QB_EXT.size());
51 if (second.endsWith(QB_EXT, Qt::CaseInsensitive))
52 second.chop(QB_EXT.size());
53 return QString::compare(first, second, CASE_SENSITIVITY) == 0;
57 void BitTorrent::AbstractFileStorage::renameFile(const QString &oldPath, const QString &newPath)
59 if (!Utils::Fs::isValidFileSystemName(oldPath, true))
60 throw RuntimeError {tr("The old path is invalid: '%1'.").arg(oldPath)};
61 if (!Utils::Fs::isValidFileSystemName(newPath, true))
62 throw RuntimeError {tr("The new path is invalid: '%1'.").arg(newPath)};
64 const QString oldFilePath = Utils::Fs::toUniformPath(oldPath);
65 if (oldFilePath.endsWith(QLatin1Char {'/'}))
66 throw RuntimeError {tr("Invalid file path: '%1'.").arg(oldFilePath)};
68 const QString newFilePath = Utils::Fs::toUniformPath(newPath);
69 if (newFilePath.endsWith(QLatin1Char {'/'}))
70 throw RuntimeError {tr("Invalid file path: '%1'.").arg(newFilePath)};
71 if (QDir().isAbsolutePath(newFilePath))
72 throw RuntimeError {tr("Absolute path isn't allowed: '%1'.").arg(newFilePath)};
74 int renamingFileIndex = -1;
75 for (int i = 0; i < filesCount(); ++i)
77 const QString path = filePath(i);
79 if ((renamingFileIndex < 0) && areSameFileNames(path, oldFilePath))
80 renamingFileIndex = i;
81 else if (areSameFileNames(path, newFilePath))
82 throw RuntimeError {tr("The file already exists: '%1'.").arg(newFilePath)};
85 if (renamingFileIndex < 0)
86 throw RuntimeError {tr("No such file: '%1'.").arg(oldFilePath)};
88 const auto extAdjusted = [](const QString &path, const bool needExt) -> QString
90 if (path.endsWith(QB_EXT, Qt::CaseInsensitive) == needExt)
91 return path;
93 return (needExt ? (path + QB_EXT) : (path.left(path.size() - QB_EXT.size())));
96 renameFile(renamingFileIndex, extAdjusted(newFilePath, filePath(renamingFileIndex).endsWith(QB_EXT, Qt::CaseInsensitive)));
99 void BitTorrent::AbstractFileStorage::renameFolder(const QString &oldPath, const QString &newPath)
101 if (!Utils::Fs::isValidFileSystemName(oldPath, true))
102 throw RuntimeError {tr("The old path is invalid: '%1'.").arg(oldPath)};
103 if (!Utils::Fs::isValidFileSystemName(newPath, true))
104 throw RuntimeError {tr("The new path is invalid: '%1'.").arg(newPath)};
106 const auto cleanFolderPath = [](const QString &path) -> QString
108 const QString uniformPath = Utils::Fs::toUniformPath(path);
109 return (uniformPath.endsWith(QLatin1Char {'/'}) ? uniformPath : uniformPath + QLatin1Char {'/'});
112 const QString oldFolderPath = cleanFolderPath(oldPath);
113 const QString newFolderPath = cleanFolderPath(newPath);
114 if (QDir().isAbsolutePath(newFolderPath))
115 throw RuntimeError {tr("Absolute path isn't allowed: '%1'.").arg(newFolderPath)};
117 QVector<int> renamingFileIndexes;
118 renamingFileIndexes.reserve(filesCount());
120 for (int i = 0; i < filesCount(); ++i)
122 const QString path = filePath(i);
124 if (path.startsWith(oldFolderPath, CASE_SENSITIVITY))
125 renamingFileIndexes.append(i);
126 else if (path.startsWith(newFolderPath, CASE_SENSITIVITY))
127 throw RuntimeError {tr("The folder already exists: '%1'.").arg(newFolderPath)};
130 if (renamingFileIndexes.isEmpty())
131 throw RuntimeError {tr("No such folder: '%1'.").arg(oldFolderPath)};
133 for (const int index : renamingFileIndexes)
135 const QString newFilePath = newFolderPath + filePath(index).mid(oldFolderPath.size());
136 renameFile(index, newFilePath);