fix(ui): fix docset storage writable check

Checks if the directly is writable by attempting to write.

Fixes #1036 and #1059.
This commit is contained in:
Oleg Shparber 2020-05-26 01:37:32 -04:00
parent bc390d3570
commit de5ba7bd0c
2 changed files with 14 additions and 11 deletions

View File

@ -78,16 +78,7 @@ DocsetsDialog::DocsetsDialog(Core::Application *app, QWidget *parent)
loadDocsetList();
#ifdef Q_OS_WIN32
qt_ntfs_permission_lookup++;
#endif
const QFileInfo fi(m_application->settings()->docsetPath);
m_isStorageReadOnly = !fi.isWritable();
#ifdef Q_OS_WIN32
qt_ntfs_permission_lookup--;
#endif
m_isStorageReadOnly = !isDirWritable(m_application->settings()->docsetPath);
#ifdef Q_OS_MACOS
ui->availableDocsetList->setAttribute(Qt::WA_MacShowFocusRect, false);
@ -96,6 +87,8 @@ DocsetsDialog::DocsetsDialog(Core::Application *app, QWidget *parent)
ui->statusLabel->clear(); // Clear text shown in the designer mode.
ui->storageStatusLabel->setVisible(m_isStorageReadOnly);
const QFileInfo fi(m_application->settings()->docsetPath);
ui->storageStatusLabel->setText(fi.exists() ? tr("<b>Docset storage is read only.</b>")
: tr("<b>Docset storage does not exist.</b>"));
@ -636,7 +629,10 @@ void DocsetsDialog::enableControls()
void DocsetsDialog::disableControls()
{
// Dialog buttons.
ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
if (!m_isStorageReadOnly) {
// Always show the close button if storage is read only.
ui->buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
}
// Installed docsets
ui->addFeedButton->setEnabled(false);
@ -844,3 +840,9 @@ QString DocsetsDialog::cacheLocation(const QString &fileName)
{
return QDir(Core::Application::cacheLocation()).filePath(fileName);
}
bool DocsetsDialog::isDirWritable(const QString &path)
{
auto file = std::make_unique<QTemporaryFile>(path + QLatin1String("/.zeal_writable_check_XXXXXX.tmp"));
return file->open();
}

View File

@ -125,6 +125,7 @@ private:
static inline int percent(qint64 fraction, qint64 total);
static QString cacheLocation(const QString &fileName);
static bool isDirWritable(const QString &path);
};
} // namespace WidgetUi