Skip to content

Commit

Permalink
Ensure config directory exists
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Sep 20, 2023
1 parent 50c9910 commit 1a288ba
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions plugins/itemencrypted/itemencrypted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class GpgExecutable {
const bool needsSecring = version.major == 2 && version.minor == 0;

const QString path = getConfigurationFilePath("");
ensureSettingsDirectoryExists();
m_pubring = QDir::toNativeSeparators(path + ".pub");
if (needsSecring)
m_secring = QDir::toNativeSeparators(path + ".sec");
Expand Down
2 changes: 2 additions & 0 deletions src/app/clipboardserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ ClipboardServer::ClipboardServer(QApplication *app, const QString &sessionName)

QApplication::setQuitOnLastWindowClosed(false);

ensureSettingsDirectoryExists();

m_sharedData = std::make_shared<ClipboardBrowserShared>();
m_sharedData->itemFactory = new ItemFactory(this);
m_sharedData->notifications = new NotificationDaemon(this);
Expand Down
14 changes: 14 additions & 0 deletions src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ QString getConfigurationFilePathHelper()

} // namespace

bool ensureSettingsDirectoryExists()
{
QDir settingsDir( settingsDirectoryPath() );
if ( !settingsDir.mkpath(".") ) {
log( QStringLiteral("Failed to create the directory for settings: %1")
.arg(settingsDir.path()),
LogError );

return false;
}

return true;
}

const QString &getConfigurationFilePath()
{
static const QString path = getConfigurationFilePathHelper();
Expand Down
2 changes: 2 additions & 0 deletions src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class QString;
class QVariant;
class QWidget;

bool ensureSettingsDirectoryExists();

const QString &getConfigurationFilePath();

QString getConfigurationFilePath(const char *suffix);
Expand Down
19 changes: 1 addition & 18 deletions src/item/itemstore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,6 @@ QString itemFileName(const QString &id)
return getConfigurationFilePath("_tab_") + part + QLatin1String(".dat");
}

bool createItemDirectory()
{
QDir settingsDir( settingsDirectoryPath() );
if ( !settingsDir.mkpath(".") ) {
log( QString("Cannot create directory for settings %1!")
.arg(quoteString(settingsDir.path()) ),
LogError );

return false;
}

return true;
}

void printItemFileError(
const QString &action, const QString &id, const QFileDevice &file)
{
Expand Down Expand Up @@ -83,9 +69,6 @@ ItemSaverPtr createTab(

ItemSaverPtr loadItems(const QString &tabName, QAbstractItemModel &model, ItemFactory *itemFactory, int maxItems)
{
if ( !createItemDirectory() )
return nullptr;

const QString tabFileName = itemFileName(tabName);
if ( !QFile::exists(tabFileName) )
return createTab(tabName, model, itemFactory, maxItems);
Expand All @@ -107,7 +90,7 @@ bool saveItems(const QString &tabName, const QAbstractItemModel &model, const It
{
const QString tabFileName = itemFileName(tabName);

if ( !createItemDirectory() )
if ( !ensureSettingsDirectoryExists() )
return false;

// Save tab data to a new temporary file.
Expand Down

0 comments on commit 1a288ba

Please sign in to comment.