Skip to content

Commit

Permalink
Introduce basic argument handling
Browse files Browse the repository at this point in the history
Ability to specify the database and mappings on the command line,
optionally replacing/extending the store configuration.
  • Loading branch information
Osse committed Mar 19, 2018
1 parent 32ed391 commit 6d195e4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 13 deletions.
28 changes: 28 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "git_version.h"
#include "mainwindow.h"
#include "shotwin.h"

#include <QApplication>
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QSettings>

#include "initstaticplugins.h"
Expand All @@ -13,11 +16,36 @@ int main(int argc, char* argv[])
QCoreApplication::setOrganizationDomain("http://github.com/Osse/shotwin");
QCoreApplication::setOrganizationName("Øystein Walle");
QCoreApplication::setApplicationName("Shotwin");
QCoreApplication::setApplicationVersion(GIT_VERSION);
QSettings::setDefaultFormat(QSettings::IniFormat);

QCommandLineParser parser;
parser.setApplicationDescription(QObject::tr("Simple Qt-based view onto a Shotwell database."));
parser.addHelpOption();
parser.addVersionOption();

parser.addOptions({{{"d", "database"}, QObject::tr("Location of Shotwell database."), "database"},
{{"m", "add-map"}, QObject::tr("Add a picture path mapping."), "mapping"},
{{"n", "no-config"}, QObject::tr("Don't update config file.")},
{{"N", "no-use-config"}, QObject::tr("Don't read existing config file.")}});

parser.process(a);

Args args;

if (parser.isSet("database"))
args.database = parser.value("database");

if (parser.isSet("add-map"))
args.mapList = parser.values("add-map");

args.readConfig = !parser.isSet("no-config");
args.updateConfig = !parser.isSet("no-use-config");

Shotwin s;

MainWindow w(&s);
w.handleArgs(args);
w.show();

return a.exec();
Expand Down
49 changes: 38 additions & 11 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <QShortcut>
#include <QSqlDatabase>
#include <QStandardPaths>
#include <QString>
#include <QVariant>

MainWindow::MainWindow(Shotwin* shotwin, QWidget* parent)
: QMainWindow(parent), ui(new Ui::MainWindow), shotwin(shotwin)
Expand All @@ -46,37 +48,62 @@ MainWindow::MainWindow(Shotwin* shotwin, QWidget* parent)

ui->splitter->setSizes({33000, 67000});
ui->mainToolBar->hide();

auto argv = QApplication::arguments();
if (argv.length() > 1)
openDataBaseConnection(argv[1]);
else {
QSettings settings;
if (settings.contains("databasepath"))
openDataBaseConnection(settings.value("databasepath").toString());
}
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::handleArgs(const Args& args)
{
QSettings settings;
QMap<QString, QVariant> map;

if (!args.mapList.isEmpty()) {
if (args.readConfig)
map = settings.value("map").toMap();

for (const auto& string : args.mapList) {
auto parts = string.split(";");
map[parts[0]] = parts[1];
}

if (args.updateConfig)
settings.setValue("map", map);

shotwin->setMap(map);
}
else
map = settings.value("map").toMap();

shotwin->setMap(map);

if (!args.database.isEmpty()) {
openDataBaseConnection(args.database);
if (args.updateConfig)
settings.setValue("databasepath", args.database);
}
else if (settings.contains("databasepath"))
openDataBaseConnection(settings.value("databasepath").toString());
}

void MainWindow::openDatabase()
{
auto home = QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first();
auto dbName = QFileDialog::getOpenFileName(this, "Choose Shotwell database", home);

if (!dbName.isEmpty())
if (!dbName.isEmpty()) {
openDataBaseConnection(dbName);
QSettings().setValue("databasepath", dbName);
}
}

void MainWindow::openDataBaseConnection(const QString& dbName)
{
auto db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbName);
if (db.open()) {
QSettings().setValue("databasepath", dbName);
initModelsAndViews();
}
else
Expand Down
9 changes: 9 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class MainWindow;
class Shotwin;
class QTreeView;

struct Args {
QString database;
QStringList mapList;
bool updateConfig = true;
bool readConfig = false;
};

class MainWindow : public QMainWindow
{
Q_OBJECT
Expand All @@ -19,6 +26,8 @@ class MainWindow : public QMainWindow
explicit MainWindow(Shotwin* shotwin, QWidget* parent = 0);
~MainWindow();

void handleArgs(const Args& args);

private:
void openDatabase();
void openDataBaseConnection(const QString& dbName);
Expand Down
4 changes: 2 additions & 2 deletions shotwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

Shotwin::Shotwin(QObject* parent) : QObject(parent)
{
map = QSettings().value("map").toMap();
}

bool Shotwin::initModels()
Expand Down Expand Up @@ -203,7 +202,8 @@ QMap<QString, QVariant> Shotwin::getMap() const
void Shotwin::setMap(const QMap<QString, QVariant>& value)
{
map = value;
photoModel->setMap(map);
if (photoModel)
photoModel->setMap(map);
}

QStringList Shotwin::getTagsForPhoto(int id) const
Expand Down

0 comments on commit 6d195e4

Please sign in to comment.