2016-04-20 22:42:35 +02:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2022-07-18 21:30:16 +08:00
|
|
|
* Copyright (C) 2022 Mike Tzou (Chocobo1)
|
2016-04-20 22:42:35 +02:00
|
|
|
* Copyright (C) 2016 Eugene Shalygin
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fspathedit_p.h"
|
|
|
|
|
|
|
|
#include <QCompleter>
|
2019-06-02 12:13:34 +03:00
|
|
|
#include <QContextMenuEvent>
|
2017-05-18 15:13:08 +02:00
|
|
|
#include <QDir>
|
2016-04-20 22:42:35 +02:00
|
|
|
#include <QFileInfo>
|
2019-06-02 12:13:34 +03:00
|
|
|
#include <QFileSystemModel>
|
|
|
|
#include <QMenu>
|
2017-05-18 15:13:08 +02:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QStyle>
|
|
|
|
|
2022-02-08 06:03:48 +03:00
|
|
|
#include "base/path.h"
|
|
|
|
|
2017-05-18 15:13:08 +02:00
|
|
|
// -------------------- FileSystemPathValidator ----------------------------------------
|
|
|
|
Private::FileSystemPathValidator::FileSystemPathValidator(QObject *parent)
|
|
|
|
: QValidator(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Private::FileSystemPathValidator::strictMode() const
|
|
|
|
{
|
|
|
|
return m_strictMode;
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
void Private::FileSystemPathValidator::setStrictMode(const bool value)
|
2017-05-18 15:13:08 +02:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
m_strictMode = value;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Private::FileSystemPathValidator::existingOnly() const
|
|
|
|
{
|
|
|
|
return m_existingOnly;
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
void Private::FileSystemPathValidator::setExistingOnly(const bool value)
|
2017-05-18 15:13:08 +02:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
m_existingOnly = value;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Private::FileSystemPathValidator::directoriesOnly() const
|
|
|
|
{
|
|
|
|
return m_directoriesOnly;
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
void Private::FileSystemPathValidator::setDirectoriesOnly(const bool value)
|
2017-05-18 15:13:08 +02:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
m_directoriesOnly = value;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Private::FileSystemPathValidator::checkReadPermission() const
|
|
|
|
{
|
|
|
|
return m_checkReadPermission;
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
void Private::FileSystemPathValidator::setCheckReadPermission(const bool value)
|
2017-05-18 15:13:08 +02:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
m_checkReadPermission = value;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Private::FileSystemPathValidator::checkWritePermission() const
|
|
|
|
{
|
|
|
|
return m_checkWritePermission;
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
void Private::FileSystemPathValidator::setCheckWritePermission(const bool value)
|
2017-05-18 15:13:08 +02:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
m_checkWritePermission = value;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
Private::FileSystemPathValidator::TestResult
|
|
|
|
Private::FileSystemPathValidator::testPath(const Path &path) const
|
2017-05-18 15:13:08 +02:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
// `QFileInfo` will cache the query results and avoid exessive querying to filesystem
|
|
|
|
const QFileInfo info {path.data()};
|
2017-05-18 15:13:08 +02:00
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
if (existingOnly() && !info.exists())
|
|
|
|
return TestResult::DoesNotExist;
|
2017-05-18 15:13:08 +02:00
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
if (directoriesOnly())
|
2020-11-16 10:02:11 +03:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
if (!info.isDir())
|
|
|
|
return TestResult::NotADir;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
2022-07-18 21:30:16 +08:00
|
|
|
else
|
2020-11-16 10:02:11 +03:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
if (!info.isFile())
|
2017-05-18 15:13:08 +02:00
|
|
|
return TestResult::NotAFile;
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
if (checkReadPermission() && !info.isReadable())
|
|
|
|
return TestResult::CantRead;
|
|
|
|
|
|
|
|
if (checkWritePermission() && !info.isWritable())
|
|
|
|
return TestResult::CantWrite;
|
|
|
|
|
2017-05-18 15:13:08 +02:00
|
|
|
return TestResult::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Private::FileSystemPathValidator::TestResult Private::FileSystemPathValidator::lastTestResult() const
|
|
|
|
{
|
|
|
|
return m_lastTestResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
QValidator::State Private::FileSystemPathValidator::lastValidationState() const
|
|
|
|
{
|
|
|
|
return m_lastValidationState;
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
QValidator::State Private::FileSystemPathValidator::validate(QString &input, int &pos) const
|
2017-05-18 15:13:08 +02:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
// ignore cursor position and validate the full path anyway
|
|
|
|
Q_UNUSED(pos);
|
|
|
|
|
|
|
|
m_lastTestResult = testPath(Path(input));
|
|
|
|
m_lastValidationState = (m_lastTestResult == TestResult::OK)
|
|
|
|
? QValidator::Acceptable
|
|
|
|
: (strictMode() ? QValidator::Invalid : QValidator::Intermediate);
|
|
|
|
|
|
|
|
return m_lastValidationState;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
2016-04-20 22:42:35 +02:00
|
|
|
|
|
|
|
Private::FileLineEdit::FileLineEdit(QWidget *parent)
|
|
|
|
: QLineEdit {parent}
|
|
|
|
, m_completerModel {new QFileSystemModel(this)}
|
|
|
|
, m_completer {new QCompleter(this)}
|
|
|
|
, m_browseAction {nullptr}
|
2017-05-18 15:13:08 +02:00
|
|
|
, m_warningAction {nullptr}
|
2016-04-20 22:42:35 +02:00
|
|
|
{
|
2022-07-18 18:58:40 +08:00
|
|
|
m_iconProvider.setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
|
|
|
|
|
2016-04-20 22:42:35 +02:00
|
|
|
m_completerModel->setIconProvider(&m_iconProvider);
|
2022-07-18 18:58:40 +08:00
|
|
|
m_completerModel->setOptions(QFileSystemModel::DontWatchForChanges);
|
|
|
|
|
2016-04-20 22:42:35 +02:00
|
|
|
m_completer->setModel(m_completerModel);
|
|
|
|
setCompleter(m_completer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Private::FileLineEdit::~FileLineEdit()
|
|
|
|
{
|
|
|
|
delete m_completerModel; // has to be deleted before deleting the m_iconProvider object
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
void Private::FileLineEdit::completeDirectoriesOnly(const bool completeDirsOnly)
|
2016-04-20 22:42:35 +02:00
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
const QDir::Filters filters = QDir::NoDotAndDotDot
|
|
|
|
| (completeDirsOnly ? QDir::Dirs : QDir::AllEntries);
|
2016-04-20 22:42:35 +02:00
|
|
|
m_completerModel->setFilter(filters);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Private::FileLineEdit::setFilenameFilters(const QStringList &filters)
|
|
|
|
{
|
|
|
|
m_completerModel->setNameFilters(filters);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Private::FileLineEdit::setBrowseAction(QAction *action)
|
|
|
|
{
|
|
|
|
m_browseAction = action;
|
|
|
|
}
|
|
|
|
|
2017-05-18 15:13:08 +02:00
|
|
|
void Private::FileLineEdit::setValidator(QValidator *validator)
|
|
|
|
{
|
|
|
|
QLineEdit::setValidator(validator);
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:03:48 +03:00
|
|
|
Path Private::FileLineEdit::placeholder() const
|
2021-12-01 17:35:21 +03:00
|
|
|
{
|
2022-02-08 06:03:48 +03:00
|
|
|
return Path(placeholderText());
|
2021-12-01 17:35:21 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 06:03:48 +03:00
|
|
|
void Private::FileLineEdit::setPlaceholder(const Path &val)
|
2021-12-01 17:35:21 +03:00
|
|
|
{
|
2022-02-08 06:03:48 +03:00
|
|
|
setPlaceholderText(val.toString());
|
2021-12-01 17:35:21 +03:00
|
|
|
}
|
|
|
|
|
2016-04-20 22:42:35 +02:00
|
|
|
QWidget *Private::FileLineEdit::widget()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Private::FileLineEdit::keyPressEvent(QKeyEvent *e)
|
|
|
|
{
|
|
|
|
QLineEdit::keyPressEvent(e);
|
2022-07-18 21:30:16 +08:00
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
if ((e->key() == Qt::Key_Space) && (e->modifiers() == Qt::CTRL))
|
|
|
|
{
|
2016-04-20 22:42:35 +02:00
|
|
|
m_completerModel->setRootPath(QFileInfo(text()).absoluteDir().absolutePath());
|
|
|
|
showCompletionPopup();
|
|
|
|
}
|
2017-05-18 15:13:08 +02:00
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
const auto *validator = qobject_cast<const FileSystemPathValidator *>(this->validator());
|
2020-11-16 10:02:11 +03:00
|
|
|
if (validator)
|
|
|
|
{
|
2022-07-18 21:30:16 +08:00
|
|
|
const FileSystemPathValidator::TestResult lastTestResult = validator->lastTestResult();
|
|
|
|
const QValidator::State lastState = validator->lastValidationState();
|
2020-11-16 10:02:11 +03:00
|
|
|
if (lastTestResult == FileSystemPathValidator::TestResult::OK)
|
|
|
|
{
|
2020-02-03 16:38:46 -05:00
|
|
|
delete m_warningAction;
|
|
|
|
m_warningAction = nullptr;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
2020-11-16 10:02:11 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!m_warningAction)
|
|
|
|
{
|
2017-05-18 15:13:08 +02:00
|
|
|
m_warningAction = new QAction(this);
|
|
|
|
addAction(m_warningAction, QLineEdit::TrailingPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
if (m_warningAction)
|
|
|
|
{
|
2017-05-18 15:13:08 +02:00
|
|
|
if (lastState == QValidator::Invalid)
|
|
|
|
m_warningAction->setIcon(style()->standardIcon(QStyle::SP_MessageBoxCritical));
|
|
|
|
else if (lastState == QValidator::Intermediate)
|
|
|
|
m_warningAction->setIcon(style()->standardIcon(QStyle::SP_MessageBoxWarning));
|
2022-07-18 21:30:16 +08:00
|
|
|
m_warningAction->setToolTip(warningText(lastTestResult));
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-20 22:42:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Private::FileLineEdit::contextMenuEvent(QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
QMenu *menu = createStandardContextMenu();
|
2019-06-03 15:10:19 +08:00
|
|
|
menu->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
2020-11-16 10:02:11 +03:00
|
|
|
if (m_browseAction)
|
|
|
|
{
|
2016-04-20 22:42:35 +02:00
|
|
|
menu->addSeparator();
|
|
|
|
menu->addAction(m_browseAction);
|
|
|
|
}
|
2019-06-03 15:10:19 +08:00
|
|
|
|
|
|
|
menu->popup(event->globalPos());
|
2016-04-20 22:42:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Private::FileLineEdit::showCompletionPopup()
|
|
|
|
{
|
|
|
|
m_completer->setCompletionPrefix(text());
|
|
|
|
m_completer->complete();
|
|
|
|
}
|
|
|
|
|
2022-07-18 21:30:16 +08:00
|
|
|
QString Private::FileLineEdit::warningText(const FileSystemPathValidator::TestResult result)
|
2017-05-18 15:13:08 +02:00
|
|
|
{
|
|
|
|
using TestResult = FileSystemPathValidator::TestResult;
|
2022-07-18 21:30:16 +08:00
|
|
|
switch (result)
|
2020-11-16 10:02:11 +03:00
|
|
|
{
|
2017-05-18 15:13:08 +02:00
|
|
|
case TestResult::DoesNotExist:
|
2022-07-18 21:30:16 +08:00
|
|
|
return tr("Path does not exist");
|
2017-05-18 15:13:08 +02:00
|
|
|
case TestResult::NotADir:
|
2022-07-18 21:30:16 +08:00
|
|
|
return tr("Path does not point to a directory");
|
2017-05-18 15:13:08 +02:00
|
|
|
case TestResult::NotAFile:
|
2022-07-18 21:30:16 +08:00
|
|
|
return tr("Path does not point to a file");
|
2017-05-18 15:13:08 +02:00
|
|
|
case TestResult::CantRead:
|
2022-07-18 21:30:16 +08:00
|
|
|
return tr("Don't have read permission to path");
|
2017-05-18 15:13:08 +02:00
|
|
|
case TestResult::CantWrite:
|
2022-07-18 21:30:16 +08:00
|
|
|
return tr("Don't have write permission to path");
|
2017-05-18 15:13:08 +02:00
|
|
|
default:
|
2022-07-18 21:30:16 +08:00
|
|
|
break;
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
2022-07-18 21:30:16 +08:00
|
|
|
return {};
|
2017-05-18 15:13:08 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 22:42:35 +02:00
|
|
|
Private::FileComboEdit::FileComboEdit(QWidget *parent)
|
|
|
|
: QComboBox {parent}
|
|
|
|
{
|
|
|
|
setEditable(true);
|
|
|
|
setLineEdit(new FileLineEdit(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Private::FileComboEdit::completeDirectoriesOnly(bool completeDirsOnly)
|
|
|
|
{
|
|
|
|
static_cast<FileLineEdit *>(lineEdit())->completeDirectoriesOnly(completeDirsOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Private::FileComboEdit::setBrowseAction(QAction *action)
|
|
|
|
{
|
|
|
|
static_cast<FileLineEdit *>(lineEdit())->setBrowseAction(action);
|
|
|
|
}
|
|
|
|
|
2017-05-18 15:13:08 +02:00
|
|
|
void Private::FileComboEdit::setValidator(QValidator *validator)
|
|
|
|
{
|
|
|
|
lineEdit()->setValidator(validator);
|
|
|
|
}
|
|
|
|
|
2022-02-08 06:03:48 +03:00
|
|
|
Path Private::FileComboEdit::placeholder() const
|
2021-12-01 17:35:21 +03:00
|
|
|
{
|
2022-02-08 06:03:48 +03:00
|
|
|
return Path(lineEdit()->placeholderText());
|
2021-12-01 17:35:21 +03:00
|
|
|
}
|
|
|
|
|
2022-02-08 06:03:48 +03:00
|
|
|
void Private::FileComboEdit::setPlaceholder(const Path &val)
|
2021-12-01 17:35:21 +03:00
|
|
|
{
|
2022-02-08 06:03:48 +03:00
|
|
|
lineEdit()->setPlaceholderText(val.toString());
|
2021-12-01 17:35:21 +03:00
|
|
|
}
|
|
|
|
|
2016-04-20 22:42:35 +02:00
|
|
|
void Private::FileComboEdit::setFilenameFilters(const QStringList &filters)
|
|
|
|
{
|
|
|
|
static_cast<FileLineEdit *>(lineEdit())->setFilenameFilters(filters);
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *Private::FileComboEdit::widget()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Private::FileComboEdit::text() const
|
|
|
|
{
|
|
|
|
return currentText();
|
|
|
|
}
|