qBittorrent
Private::FileSystemPathValidator Class Referencefinal

#include <fspathedit_p.h>

Inheritance diagram for Private::FileSystemPathValidator:
Collaboration diagram for Private::FileSystemPathValidator:

Public Types

enum class  TestResult {
  OK , DoesNotExist , NotADir , NotAFile ,
  CantRead , CantWrite
}
 

Public Member Functions

 FileSystemPathValidator (QObject *parent=nullptr)
 
bool strictMode () const
 
void setStrictMode (bool v)
 
bool existingOnly () const
 
void setExistingOnly (bool v)
 
bool directoriesOnly () const
 
void setDirectoriesOnly (bool v)
 
bool checkReadPermission () const
 
void setCheckReadPermission (bool v)
 
bool checkWritePermission () const
 
void setCheckWritePermission (bool v)
 
QValidator::State validate (QString &input, int &pos) const override
 
TestResult lastTestResult () const
 
QValidator::State lastValidationState () const
 
QString lastTestedPath () const
 

Private Member Functions

QValidator::State validate (const QList< QStringView > &pathComponents, bool strict, int firstComponentToTest, int lastComponentToTest) const
 
TestResult testPath (QStringView path, bool pathIsComplete) const
 

Private Attributes

bool m_strictMode
 
bool m_existingOnly
 
bool m_directoriesOnly
 
bool m_checkReadPermission
 
bool m_checkWritePermission
 
TestResult m_lastTestResult
 
QValidator::State m_lastValidationState
 
QString m_lastTestedPath
 

Detailed Description

Definition at line 45 of file fspathedit_p.h.

Member Enumeration Documentation

◆ TestResult

Enumerator
OK 
DoesNotExist 
NotADir 
NotAFile 
CantRead 
CantWrite 

Definition at line 69 of file fspathedit_p.h.

70  {
71  OK,
72  DoesNotExist,
73  NotADir,
74  NotAFile,
75  CantRead,
76  CantWrite
77  };

Constructor & Destructor Documentation

◆ FileSystemPathValidator()

Private::FileSystemPathValidator::FileSystemPathValidator ( QObject *  parent = nullptr)

Member Function Documentation

◆ checkReadPermission()

bool Private::FileSystemPathValidator::checkReadPermission ( ) const

Definition at line 83 of file fspathedit_p.cpp.

84 {
85  return m_checkReadPermission;
86 }

◆ checkWritePermission()

bool Private::FileSystemPathValidator::checkWritePermission ( ) const

Definition at line 93 of file fspathedit_p.cpp.

94 {
96 }

◆ directoriesOnly()

bool Private::FileSystemPathValidator::directoriesOnly ( ) const

Definition at line 73 of file fspathedit_p.cpp.

74 {
75  return m_directoriesOnly;
76 }

◆ existingOnly()

bool Private::FileSystemPathValidator::existingOnly ( ) const

Definition at line 63 of file fspathedit_p.cpp.

64 {
65  return m_existingOnly;
66 }

◆ lastTestedPath()

QString Private::FileSystemPathValidator::lastTestedPath ( ) const

Definition at line 197 of file fspathedit_p.cpp.

198 {
199  return m_lastTestedPath;
200 }

◆ lastTestResult()

Private::FileSystemPathValidator::TestResult Private::FileSystemPathValidator::lastTestResult ( ) const

Definition at line 187 of file fspathedit_p.cpp.

188 {
189  return m_lastTestResult;
190 }

◆ lastValidationState()

QValidator::State Private::FileSystemPathValidator::lastValidationState ( ) const

Definition at line 192 of file fspathedit_p.cpp.

193 {
194  return m_lastValidationState;
195 }

◆ setCheckReadPermission()

void Private::FileSystemPathValidator::setCheckReadPermission ( bool  v)

Definition at line 88 of file fspathedit_p.cpp.

89 {
91 }

◆ setCheckWritePermission()

void Private::FileSystemPathValidator::setCheckWritePermission ( bool  v)

Definition at line 98 of file fspathedit_p.cpp.

99 {
101 }

◆ setDirectoriesOnly()

void Private::FileSystemPathValidator::setDirectoriesOnly ( bool  v)

Definition at line 78 of file fspathedit_p.cpp.

79 {
81 }

◆ setExistingOnly()

void Private::FileSystemPathValidator::setExistingOnly ( bool  v)

Definition at line 68 of file fspathedit_p.cpp.

69 {
70  m_existingOnly = v;
71 }

◆ setStrictMode()

void Private::FileSystemPathValidator::setStrictMode ( bool  v)

Definition at line 58 of file fspathedit_p.cpp.

59 {
60  m_strictMode = v;
61 }

Referenced by FileSystemPathEdit::FileSystemPathEditPrivate::FileSystemPathEditPrivate().

Here is the caller graph for this function:

◆ strictMode()

bool Private::FileSystemPathValidator::strictMode ( ) const

Definition at line 53 of file fspathedit_p.cpp.

54 {
55  return m_strictMode;
56 }

◆ testPath()

Private::FileSystemPathValidator::TestResult Private::FileSystemPathValidator::testPath ( QStringView  path,
bool  pathIsComplete 
) const
private

Definition at line 164 of file fspathedit_p.cpp.

165 {
166  QFileInfo fi(path.toString());
167  if (m_existingOnly && !fi.exists())
169 
170  if ((!pathIsComplete || m_directoriesOnly) && !fi.isDir())
171  return TestResult::NotADir;
172 
173  if (pathIsComplete)
174  {
175  if (!m_directoriesOnly && fi.isDir())
176  return TestResult::NotAFile;
177 
178  if (m_checkWritePermission && (fi.exists() && !fi.isWritable()))
179  return TestResult::CantWrite;
180  if (m_checkReadPermission && !fi.isReadable())
181  return TestResult::CantRead;
182  }
183 
184  return TestResult::OK;
185 }

◆ validate() [1/2]

QValidator::State Private::FileSystemPathValidator::validate ( const QList< QStringView > &  pathComponents,
bool  strict,
int  firstComponentToTest,
int  lastComponentToTest 
) const
private

Definition at line 135 of file fspathedit_p.cpp.

137 {
138  Q_ASSERT(firstComponentToTest >= 0);
139  Q_ASSERT(lastComponentToTest >= firstComponentToTest);
140  Q_ASSERT(lastComponentToTest < pathComponents.size());
141 
143  if (pathComponents.empty())
144  return strict ? QValidator::Invalid : QValidator::Intermediate;
145 
146  for (int i = firstComponentToTest; i <= lastComponentToTest; ++i)
147  {
148  const bool isFinalPath = (i == (pathComponents.size() - 1));
149  const QStringView componentPath = pathComponents[i];
150  if (componentPath.isEmpty()) continue;
151 
152  m_lastTestResult = testPath(pathComponents[i], isFinalPath);
154  {
155  m_lastTestedPath = componentPath.toString();
156  return strict ? QValidator::Invalid : QValidator::Intermediate;
157  }
158  }
159 
160  return QValidator::Acceptable;
161 }
TestResult testPath(QStringView path, bool pathIsComplete) const

◆ validate() [2/2]

QValidator::State Private::FileSystemPathValidator::validate ( QString &  input,
int &  pos 
) const
override

Definition at line 103 of file fspathedit_p.cpp.

104 {
105  if (input.isEmpty())
106  return m_strictMode ? QValidator::Invalid : QValidator::Intermediate;
107 
108  // we test path components from beginning to the one with cursor location in strict mode
109  // and the one with cursor and beyond in non-strict mode
110  QList<QStringView> components = QStringView(input).split(QDir::separator(), Qt::KeepEmptyParts);
111  // find index of the component that contains pos
112  int componentWithCursorIndex = 0;
113  int componentWithCursorPosition = 0;
114  int pathLength = 0;
115 
116  // components.size() - 1 because when path ends with QDir::separator(), we will not see the last
117  // character in the components array, yet everything past the one before the last delimiter
118  // belongs to the last component
119  for (; (componentWithCursorIndex < (components.size() - 1)) && (pathLength < pos); ++componentWithCursorIndex)
120  {
121  pathLength = componentWithCursorPosition + components[componentWithCursorIndex].size();
122  componentWithCursorPosition += components[componentWithCursorIndex].size() + 1;
123  }
124 
125  Q_ASSERT(componentWithCursorIndex < components.size());
126 
127  m_lastValidationState = QValidator::Acceptable;
128  if (componentWithCursorIndex > 0)
129  m_lastValidationState = validate(components, m_strictMode, 0, componentWithCursorIndex - 1);
130  if ((m_lastValidationState == QValidator::Acceptable) && (componentWithCursorIndex < components.size()))
131  m_lastValidationState = validate(components, false, componentWithCursorIndex, components.size() - 1);
132  return m_lastValidationState;
133 }
QValidator::State validate(QString &input, int &pos) const override

Member Data Documentation

◆ m_checkReadPermission

bool Private::FileSystemPathValidator::m_checkReadPermission
private

Definition at line 92 of file fspathedit_p.h.

◆ m_checkWritePermission

bool Private::FileSystemPathValidator::m_checkWritePermission
private

Definition at line 93 of file fspathedit_p.h.

◆ m_directoriesOnly

bool Private::FileSystemPathValidator::m_directoriesOnly
private

Definition at line 91 of file fspathedit_p.h.

◆ m_existingOnly

bool Private::FileSystemPathValidator::m_existingOnly
private

Definition at line 90 of file fspathedit_p.h.

◆ m_lastTestedPath

QString Private::FileSystemPathValidator::m_lastTestedPath
mutableprivate

Definition at line 97 of file fspathedit_p.h.

◆ m_lastTestResult

TestResult Private::FileSystemPathValidator::m_lastTestResult
mutableprivate

Definition at line 95 of file fspathedit_p.h.

◆ m_lastValidationState

QValidator::State Private::FileSystemPathValidator::m_lastValidationState
mutableprivate

Definition at line 96 of file fspathedit_p.h.

◆ m_strictMode

bool Private::FileSystemPathValidator::m_strictMode
private

Definition at line 89 of file fspathedit_p.h.


The documentation for this class was generated from the following files: