2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2022 Mike Tzou (Chocobo1)
5 * Copyright (C) 2016 Eugene Shalygin
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * In addition, as a special exception, the copyright holders give permission to
22 * link this program with the OpenSSL project's "OpenSSL" library (or with
23 * modified versions of it that use the same license as the "OpenSSL" library),
24 * and distribute the linked executables. You must obey the GNU General Public
25 * License in all respects for all of the code used other than "OpenSSL". If you
26 * modify file(s), you may extend this exception to your version of the file(s),
27 * but you are not obligated to do so. If you do not wish to do so, delete this
28 * exception statement from your version.
31 #include "fspathedit_p.h"
34 #include <QContextMenuEvent>
36 #include <QFileIconProvider>
38 #include <QFileSystemModel>
40 #include <QStringList>
43 #include "base/path.h"
45 // -------------------- FileSystemPathValidator ----------------------------------------
46 Private::FileSystemPathValidator::FileSystemPathValidator(QObject
*parent
)
51 bool Private::FileSystemPathValidator::strictMode() const
56 void Private::FileSystemPathValidator::setStrictMode(const bool value
)
61 bool Private::FileSystemPathValidator::existingOnly() const
63 return m_existingOnly
;
66 void Private::FileSystemPathValidator::setExistingOnly(const bool value
)
68 m_existingOnly
= value
;
71 bool Private::FileSystemPathValidator::filesOnly() const
76 void Private::FileSystemPathValidator::setFilesOnly(const bool value
)
81 bool Private::FileSystemPathValidator::directoriesOnly() const
83 return m_directoriesOnly
;
86 void Private::FileSystemPathValidator::setDirectoriesOnly(const bool value
)
88 m_directoriesOnly
= value
;
91 bool Private::FileSystemPathValidator::checkReadPermission() const
93 return m_checkReadPermission
;
96 void Private::FileSystemPathValidator::setCheckReadPermission(const bool value
)
98 m_checkReadPermission
= value
;
101 bool Private::FileSystemPathValidator::checkWritePermission() const
103 return m_checkWritePermission
;
106 void Private::FileSystemPathValidator::setCheckWritePermission(const bool value
)
108 m_checkWritePermission
= value
;
111 Private::FileSystemPathValidator::TestResult
112 Private::FileSystemPathValidator::testPath(const Path
&path
) const
114 // `QFileInfo` will cache the query results and avoid excessive querying to filesystem
115 const QFileInfo info
{path
.data()};
118 return existingOnly() ? TestResult::DoesNotExist
: TestResult::OK
;
123 return TestResult::NotAFile
;
126 if (directoriesOnly())
129 return TestResult::NotADir
;
132 if (checkReadPermission() && !info
.isReadable())
133 return TestResult::CantRead
;
135 if (checkWritePermission() && !info
.isWritable())
136 return TestResult::CantWrite
;
138 return TestResult::OK
;
141 Private::FileSystemPathValidator::TestResult
Private::FileSystemPathValidator::lastTestResult() const
143 return m_lastTestResult
;
146 QValidator::State
Private::FileSystemPathValidator::lastValidationState() const
148 return m_lastValidationState
;
151 QValidator::State
Private::FileSystemPathValidator::validate(QString
&input
, [[maybe_unused
]] int &pos
) const
153 // we ignore cursor position and validate the full path anyway
155 m_lastTestResult
= testPath(Path(input
));
156 m_lastValidationState
= (m_lastTestResult
== TestResult::OK
)
157 ? QValidator::Acceptable
158 : (strictMode() ? QValidator::Invalid
: QValidator::Intermediate
);
160 return m_lastValidationState
;
163 Private::FileLineEdit::FileLineEdit(QWidget
*parent
)
166 setCompleter(new QCompleter(this));
167 connect(this, &QLineEdit::textChanged
, this, &FileLineEdit::validateText
);
170 Private::FileLineEdit::~FileLineEdit()
172 delete m_completerModel
; // has to be deleted before deleting the m_iconProvider object
173 delete m_iconProvider
;
176 void Private::FileLineEdit::completeDirectoriesOnly(const bool completeDirsOnly
)
178 m_completeDirectoriesOnly
= completeDirsOnly
;
179 if (m_completerModel
)
181 const QDir::Filters filters
= QDir::NoDotAndDotDot
182 | (completeDirsOnly
? QDir::Dirs
: QDir::AllEntries
);
183 m_completerModel
->setFilter(filters
);
187 void Private::FileLineEdit::setFilenameFilters(const QStringList
&filters
)
189 m_filenameFilters
= filters
;
190 if (m_completerModel
)
191 m_completerModel
->setNameFilters(m_filenameFilters
);
194 void Private::FileLineEdit::setBrowseAction(QAction
*action
)
196 m_browseAction
= action
;
199 void Private::FileLineEdit::setValidator(QValidator
*validator
)
201 QLineEdit::setValidator(validator
);
204 Path
Private::FileLineEdit::placeholder() const
206 return Path(placeholderText());
209 void Private::FileLineEdit::setPlaceholder(const Path
&val
)
211 setPlaceholderText(val
.toString());
214 QWidget
*Private::FileLineEdit::widget()
219 void Private::FileLineEdit::keyPressEvent(QKeyEvent
*e
)
221 QLineEdit::keyPressEvent(e
);
223 if ((e
->key() == Qt::Key_Space
) && (e
->modifiers() == Qt::CTRL
))
225 if (!m_completerModel
)
227 m_iconProvider
= new QFileIconProvider
;
228 m_iconProvider
->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons
);
230 m_completerModel
= new QFileSystemModel(this);
231 m_completerModel
->setIconProvider(m_iconProvider
);
232 m_completerModel
->setOptions(QFileSystemModel::DontWatchForChanges
);
233 m_completerModel
->setNameFilters(m_filenameFilters
);
234 const QDir::Filters filters
= QDir::NoDotAndDotDot
235 | (m_completeDirectoriesOnly
? QDir::Dirs
: QDir::AllEntries
);
236 m_completerModel
->setFilter(filters
);
238 completer()->setModel(m_completerModel
);
241 m_completerModel
->setRootPath(Path(text()).data());
242 showCompletionPopup();
246 void Private::FileLineEdit::contextMenuEvent(QContextMenuEvent
*event
)
248 QMenu
*menu
= createStandardContextMenu();
249 menu
->setAttribute(Qt::WA_DeleteOnClose
);
253 menu
->addSeparator();
254 menu
->addAction(m_browseAction
);
257 menu
->popup(event
->globalPos());
260 void Private::FileLineEdit::showCompletionPopup()
262 completer()->setCompletionPrefix(text());
263 completer()->complete();
266 void Private::FileLineEdit::validateText()
268 const auto *validator
= qobject_cast
<const FileSystemPathValidator
*>(this->validator());
272 const FileSystemPathValidator::TestResult lastTestResult
= validator
->lastTestResult();
273 const QValidator::State lastState
= validator
->lastValidationState();
275 if (lastTestResult
== FileSystemPathValidator::TestResult::OK
)
277 delete m_warningAction
;
278 m_warningAction
= nullptr;
282 if (!m_warningAction
)
284 m_warningAction
= new QAction(this);
285 addAction(m_warningAction
, QLineEdit::TrailingPosition
);
291 if (lastState
== QValidator::Invalid
)
292 m_warningAction
->setIcon(style()->standardIcon(QStyle::SP_MessageBoxCritical
));
293 else if (lastState
== QValidator::Intermediate
)
294 m_warningAction
->setIcon(style()->standardIcon(QStyle::SP_MessageBoxInformation
));
295 m_warningAction
->setToolTip(warningText(lastTestResult
));
299 QString
Private::FileLineEdit::warningText(const FileSystemPathValidator::TestResult result
)
301 using TestResult
= FileSystemPathValidator::TestResult
;
304 case TestResult::DoesNotExist
:
305 return tr("Path does not exist");
306 case TestResult::NotADir
:
307 return tr("Path does not point to a directory");
308 case TestResult::NotAFile
:
309 return tr("Path does not point to a file");
310 case TestResult::CantRead
:
311 return tr("Don't have read permission to path");
312 case TestResult::CantWrite
:
313 return tr("Don't have write permission to path");
320 Private::FileComboEdit::FileComboEdit(QWidget
*parent
)
324 setLineEdit(new FileLineEdit(this));
327 void Private::FileComboEdit::completeDirectoriesOnly(bool completeDirsOnly
)
329 static_cast<FileLineEdit
*>(lineEdit())->completeDirectoriesOnly(completeDirsOnly
);
332 void Private::FileComboEdit::setBrowseAction(QAction
*action
)
334 static_cast<FileLineEdit
*>(lineEdit())->setBrowseAction(action
);
337 void Private::FileComboEdit::setValidator(QValidator
*validator
)
339 lineEdit()->setValidator(validator
);
342 Path
Private::FileComboEdit::placeholder() const
344 return Path(lineEdit()->placeholderText());
347 void Private::FileComboEdit::setPlaceholder(const Path
&val
)
349 lineEdit()->setPlaceholderText(val
.toString());
352 void Private::FileComboEdit::setFilenameFilters(const QStringList
&filters
)
354 static_cast<FileLineEdit
*>(lineEdit())->setFilenameFilters(filters
);
357 QWidget
*Private::FileComboEdit::widget()
362 QString
Private::FileComboEdit::text() const
364 return currentText();