Skip to content
This repository has been archived by the owner on Sep 1, 2019. It is now read-only.

Commit

Permalink
support portable mode, place .ini file next to executable file
Browse files Browse the repository at this point in the history
closes #28
  • Loading branch information
mmozeiko committed Mar 5, 2017
1 parent a7c12ba commit 8e99768
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 69 deletions.
18 changes: 9 additions & 9 deletions src/export_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ ExportDialog::ExportDialog(const QString& remote, const QDir& path, QWidget* par
}
});

QSettings settings;
settings.beginGroup("Export");
ReadSettings(&settings, this);
settings.endGroup();
auto settings = GetSettings();
settings->beginGroup("Export");
ReadSettings(settings.get(), this);
settings->endGroup();
}

ExportDialog::~ExportDialog()
{
if (result() == QDialog::Accepted)
{
QSettings settings;
settings.beginGroup("Export");
WriteSettings(&settings, this);
settings.remove("textFile");
settings.endGroup();
auto settings = GetSettings();
settings->beginGroup("Export");
WriteSettings(settings.get(), this);
settings->remove("textFile");
settings->endGroup();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/item_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ ItemModel::ItemModel(IconCache* icons, const QString& remote, QObject* parent)
mFolderIcon = style->standardIcon(QStyle::SP_DirIcon);
mFileIcon = style->standardIcon(QStyle::SP_FileIcon);

QSettings settings;
mFolderIcons = settings.value("Settings/showFolderIcons", true).toBool();
mFileIcons = settings.value("Settings/showFileIcons", true).toBool();
auto settings = GetSettings();
mFolderIcons = settings->value("Settings/showFolderIcons", true).toBool();
mFileIcons = settings->value("Settings/showFileIcons", true).toBool();

mRoot = new Item();
mRoot->isFolder = true;
Expand Down
54 changes: 27 additions & 27 deletions src/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ MainWindow::MainWindow()

mSystemTray.setIcon(qApp->windowIcon());
{
QSettings settings;
if (settings.contains("MainWindow/geometry"))
auto settings = GetSettings();
if (settings->contains("MainWindow/geometry"))
{
restoreGeometry(settings.value("MainWindow/geometry").toByteArray());
restoreGeometry(settings->value("MainWindow/geometry").toByteArray());
}
SetRclone(settings.value("Settings/rclone").toString());
SetRcloneConf(settings.value("Settings/rcloneConf").toString());
SetRclone(settings->value("Settings/rclone").toString());
SetRcloneConf(settings->value("Settings/rcloneConf").toString());

mAlwaysShowInTray = settings.value("Settings/alwaysShowInTray", false).toBool();
mCloseToTray = settings.value("Settings/closeToTray", false).toBool();
mNotifyFinishedTransfers = settings.value("Settings/notifyFinishedTransfers", true).toBool();
mAlwaysShowInTray = settings->value("Settings/alwaysShowInTray", false).toBool();
mCloseToTray = settings->value("Settings/closeToTray", false).toBool();
mNotifyFinishedTransfers = settings->value("Settings/notifyFinishedTransfers", true).toBool();

mSystemTray.setVisible(mAlwaysShowInTray);
}
Expand All @@ -35,19 +35,19 @@ MainWindow::MainWindow()
PreferencesDialog dialog(this);
if (dialog.exec() == QDialog::Accepted)
{
QSettings settings;
settings.setValue("Settings/rclone", dialog.getRclone().trimmed());
settings.setValue("Settings/rcloneConf", dialog.getRcloneConf().trimmed());
settings.setValue("Settings/stream", dialog.getStream());
auto settings = GetSettings();
settings->setValue("Settings/rclone", dialog.getRclone().trimmed());
settings->setValue("Settings/rcloneConf", dialog.getRcloneConf().trimmed());
settings->setValue("Settings/stream", dialog.getStream());
#ifndef Q_OS_WIN32
settings.setValue("Settings/mount", dialog.getMount());
settings->setValue("Settings/mount", dialog.getMount());
#endif
settings.setValue("Settings/alwaysShowInTray", dialog.getAlwaysShowInTray());
settings.setValue("Settings/closeToTray", dialog.getCloseToTray());
settings.setValue("Settings/notifyFinishedTransfers", dialog.getNotifyFinishedTransfers());
settings.setValue("Settings/showFolderIcons", dialog.getShowFolderIcons());
settings.setValue("Settings/showFileIcons", dialog.getShowFileIcons());
settings.setValue("Settings/rowColors", dialog.getRowColors());
settings->setValue("Settings/alwaysShowInTray", dialog.getAlwaysShowInTray());
settings->setValue("Settings/closeToTray", dialog.getCloseToTray());
settings->setValue("Settings/notifyFinishedTransfers", dialog.getNotifyFinishedTransfers());
settings->setValue("Settings/showFolderIcons", dialog.getShowFolderIcons());
settings->setValue("Settings/showFileIcons", dialog.getShowFileIcons());
settings->setValue("Settings/rowColors", dialog.getRowColors());
SetRclone(dialog.getRclone());
SetRcloneConf(dialog.getRcloneConf());
mFirstTime = true;
Expand Down Expand Up @@ -165,8 +165,8 @@ MainWindow::MainWindow()
if (rclone.isEmpty())
{
rclone = QStandardPaths::findExecutable("rclone");
QSettings settings;
settings.setValue("Settings/rclone", rclone);
auto settings = GetSettings();
settings->setValue("Settings/rclone", rclone);
SetRclone(rclone);
}
if (rclone.isEmpty())
Expand All @@ -182,8 +182,8 @@ MainWindow::MainWindow()

MainWindow::~MainWindow()
{
QSettings settings;
settings.setValue("MainWindow/geometry", saveGeometry());
auto settings = GetSettings();
settings->setValue("MainWindow/geometry", saveGeometry());
}

void MainWindow::rcloneGetVersion()
Expand Down Expand Up @@ -548,8 +548,8 @@ void MainWindow::addMount(const QString& remote, const QString& folder)
ui.jobs->insertWidget(1, line);
ui.tabs->setTabText(1, QString("Jobs (%1)").arg(++mJobCount));

QSettings settings;
QString opt = settings.value("Settings/mount").toString();
auto settings = GetSettings();
QString opt = settings->value("Settings/mount").toString();

QStringList args;
args << "mount";
Expand All @@ -576,8 +576,8 @@ void MainWindow::addStream(const QString& remote, const QString& stream)
if (status != 0 && player->error() == QProcess::FailedToStart)
{
QMessageBox::critical(this, "Error", QString("Failed to start '%1' player process").arg(stream));
QSettings settings;
settings.remove("Settings/streamConfirmed");
auto settings = GetSettings();
settings->remove("Settings/streamConfirmed");
}
});

Expand Down
2 changes: 2 additions & 0 deletions src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#pragma warning(push, 0)
#endif

#include <memory>

#include <QtDebug>
#include <QtCore>
#include <QtGui>
Expand Down
22 changes: 11 additions & 11 deletions src/preferences_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ PreferencesDialog::PreferencesDialog(QWidget* parent)
ui.rcloneConf->setText(rcloneConf);
});

QSettings settings;
ui.rclone->setText(QDir::toNativeSeparators(settings.value("Settings/rclone").toString()));
ui.rcloneConf->setText(QDir::toNativeSeparators(settings.value("Settings/rcloneConf").toString()));
ui.stream->setText(settings.value("Settings/stream").toString());
ui.showFolderIcons->setChecked(settings.value("Settings/showFolderIcons", true).toBool());
auto settings = GetSettings();
ui.rclone->setText(QDir::toNativeSeparators(settings->value("Settings/rclone").toString()));
ui.rcloneConf->setText(QDir::toNativeSeparators(settings->value("Settings/rcloneConf").toString()));
ui.stream->setText(settings->value("Settings/stream").toString());
ui.showFolderIcons->setChecked(settings->value("Settings/showFolderIcons", true).toBool());
if (QSystemTrayIcon::isSystemTrayAvailable())
{
ui.alwaysShowInTray->setChecked(settings.value("Settings/alwaysShowInTray", false).toBool());
ui.closeToTray->setChecked(settings.value("Settings/closeToTray", false).toBool());
ui.notifyFinishedTransfers->setChecked(settings.value("Settings/notifyFinishedTransfers", true).toBool());
ui.alwaysShowInTray->setChecked(settings->value("Settings/alwaysShowInTray", false).toBool());
ui.closeToTray->setChecked(settings->value("Settings/closeToTray", false).toBool());
ui.notifyFinishedTransfers->setChecked(settings->value("Settings/notifyFinishedTransfers", true).toBool());
}
else
{
Expand All @@ -60,14 +60,14 @@ PreferencesDialog::PreferencesDialog(QWidget* parent)
ui.notifyFinishedTransfers->setChecked(false);
ui.notifyFinishedTransfers->setDisabled(true);
}
ui.showFileIcons->setChecked(settings.value("Settings/showFileIcons", true).toBool());
ui.rowColors->setChecked(settings.value("Settings/rowColors", false).toBool());
ui.showFileIcons->setChecked(settings->value("Settings/showFileIcons", true).toBool());
ui.rowColors->setChecked(settings->value("Settings/rowColors", false).toBool());

#ifdef Q_OS_WIN32
ui.mount->hide();
ui.mountLabel->hide();
#else
ui.mount->setText(settings.value("Settings/mount").toString());
ui.mount->setText(settings->value("Settings/mount").toString());
#endif
}

Expand Down
14 changes: 7 additions & 7 deletions src/remote_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ RemoteWidget::RemoteWidget(IconCache* iconCache, const QString& remote, bool isL
#else
isLocal = false;
#endif
QSettings settings;
ui.tree->setAlternatingRowColors(settings.value("Settings/rowColors", false).toBool());
auto settings = GetSettings();
ui.tree->setAlternatingRowColors(settings->value("Settings/rowColors", false).toBool());

QStyle* style = QApplication::style();
ui.refresh->setIcon(style->standardIcon(QStyle::SP_BrowserReload));
Expand Down Expand Up @@ -214,9 +214,9 @@ RemoteWidget::RemoteWidget(IconCache* iconCache, const QString& remote, bool isL
QModelIndex index = ui.tree->selectionModel()->selectedRows().front();
QString path = model->path(index).path();

QSettings settings;
bool streamConfirmed = settings.value("Settings/streamConfirmed", false).toBool();
QString stream = settings.value("Settings/stream", "mpv -").toString();
auto settings = GetSettings();
bool streamConfirmed = settings->value("Settings/streamConfirmed", false).toBool();
QString stream = settings->value("Settings/stream", "mpv -").toString();
if (!streamConfirmed)
{
QString result = QInputDialog::getText(this, "Stream", "Enter stream command (file will be passed in STDIN):", QLineEdit::Normal, stream);
Expand All @@ -227,8 +227,8 @@ RemoteWidget::RemoteWidget(IconCache* iconCache, const QString& remote, bool isL

stream = result;

settings.setValue("Settings/stream", stream);
settings.setValue("Settings/streamConfirmed", true);
settings->setValue("Settings/stream", stream);
settings->setValue("Settings/streamConfirmed", true);
}

emit addStream(remote + ":" + path, stream);
Expand Down
20 changes: 10 additions & 10 deletions src/transfer_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ TransferDialog::TransferDialog(bool isDownload, const QString& remote, const QDi
}
});

QSettings settings;
settings.beginGroup("Transfer");
ReadSettings(&settings, this);
settings.endGroup();
auto settings = GetSettings();
settings->beginGroup("Transfer");
ReadSettings(settings.get(), this);
settings->endGroup();

ui.buttonSourceFile->setVisible(!isDownload);
ui.buttonSourceFolder->setVisible(!isDownload);
Expand All @@ -102,12 +102,12 @@ TransferDialog::~TransferDialog()
{
if (result() == QDialog::Accepted)
{
QSettings settings;
settings.beginGroup("Transfer");
WriteSettings(&settings, this);
settings.remove("textSource");
settings.remove("textDest");
settings.endGroup();
auto settings = GetSettings();
settings->beginGroup("Transfer");
WriteSettings(settings.get(), this);
settings->remove("textSource");
settings->remove("textDest");
settings->endGroup();
}
}

Expand Down
36 changes: 34 additions & 2 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ static QString gRclone;
static QString gRcloneConf;
static QString gRclonePassword;

static QString GetIniFilename()
{
QFileInfo info = qApp->applicationFilePath();
return info.dir().filePath(info.baseName() + ".ini");
}

static bool IsPortableMode()
{
QString ini = GetIniFilename();
return QFileInfo(ini).exists();
}

std::unique_ptr<QSettings> GetSettings()
{
if (IsPortableMode())
{
return std::unique_ptr<QSettings>(new QSettings(GetIniFilename(), QSettings::IniFormat));
}
return std::unique_ptr<QSettings>(new QSettings);
}

void ReadSettings(QSettings* settings, QObject* widget)
{
QString name = widget->objectName();
Expand Down Expand Up @@ -117,7 +138,12 @@ QStringList GetRcloneConf()
return QStringList();
}

return QStringList() << "--config" << gRcloneConf;
QString conf = gRcloneConf;
if (IsPortableMode() && QFileInfo(conf).isRelative())
{
conf = QDir(qApp->applicationDirPath()).filePath(conf);
}
return QStringList() << "--config" << conf;
}

void SetRcloneConf(const QString& rcloneConf)
Expand All @@ -127,7 +153,13 @@ void SetRcloneConf(const QString& rcloneConf)

QString GetRclone()
{
return gRclone;
QString rclone = gRclone;
if (IsPortableMode() && QFileInfo(rclone).isRelative())
{
rclone = QDir(qApp->applicationDirPath()).filePath(rclone);
}

return rclone;
}

void SetRclone(const QString& rclone)
Expand Down
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "pch.h"

std::unique_ptr<QSettings> GetSettings();

void ReadSettings(QSettings* settings, QObject* widget);
void WriteSettings(QSettings* settings, QObject* widget);

Expand Down

0 comments on commit 8e99768

Please sign in to comment.