diff --git a/src/gui/DatabaseOpenDialog.cpp b/src/gui/DatabaseOpenDialog.cpp index 5ab0f479c..ba7f879cb 100644 --- a/src/gui/DatabaseOpenDialog.cpp +++ b/src/gui/DatabaseOpenDialog.cpp @@ -68,6 +68,18 @@ Database* DatabaseOpenDialog::database() return m_db; } +void DatabaseOpenDialog::enterKey(const QString& pw, const QString& keyFile) +{ + if (!pw.isNull()) { + m_ui->editPassword->setText(pw); + } + if (!keyFile.isEmpty()) { + m_ui->checkKeyFile->setText(keyFile); + } + + openDatabase(); +} + void DatabaseOpenDialog::openDatabase() { KeePass2Reader reader; diff --git a/src/gui/DatabaseOpenDialog.h b/src/gui/DatabaseOpenDialog.h index 60c6cb6b6..7c04b6684 100644 --- a/src/gui/DatabaseOpenDialog.h +++ b/src/gui/DatabaseOpenDialog.h @@ -38,6 +38,7 @@ public: explicit DatabaseOpenDialog(QFile* file, QString filename, QWidget* parent = 0); ~DatabaseOpenDialog(); Database* database(); + void enterKey(const QString& pw, const QString& keyFile); private Q_SLOTS: void openDatabase(); diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index b9a71a2a2..b8eae0a0d 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -78,7 +78,8 @@ void DatabaseTabWidget::openDatabase() } } -void DatabaseTabWidget::openDatabase(const QString& fileName) +void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, + const QString& keyFile) { QScopedPointer file(new QFile(fileName)); // TODO error handling @@ -96,16 +97,20 @@ void DatabaseTabWidget::openDatabase(const QString& fileName) m_curDbStruct.file = file.take(); m_curDbStruct.fileName = QFileInfo(fileName).absoluteFilePath(); - openDatabaseDialog(); + openDatabaseDialog(pw, keyFile); } -void DatabaseTabWidget::openDatabaseDialog() +void DatabaseTabWidget::openDatabaseDialog(const QString& pw, const QString& keyFile) { m_curKeyDialog = new DatabaseOpenDialog(m_curDbStruct.file, m_curDbStruct.fileName, m_window); connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead())); connect(m_curKeyDialog, SIGNAL(rejected()), SLOT(openDatabaseCleanup())); m_curKeyDialog->setModal(true); m_curKeyDialog->show(); + + if (!pw.isNull() || !keyFile.isEmpty()) { + m_curKeyDialog->enterKey(pw, keyFile); + } } void DatabaseTabWidget::openDatabaseRead() diff --git a/src/gui/DatabaseTabWidget.h b/src/gui/DatabaseTabWidget.h index 10a7eb873..788bc6b6e 100644 --- a/src/gui/DatabaseTabWidget.h +++ b/src/gui/DatabaseTabWidget.h @@ -48,7 +48,8 @@ class DatabaseTabWidget : public QTabWidget public: explicit DatabaseTabWidget(QWidget* parent); - void openDatabase(const QString& fileName); + void openDatabase(const QString& fileName, const QString& pw = QString(), + const QString& keyFile = QString()); DatabaseWidget* currentDatabaseWidget(); public Q_SLOTS: @@ -75,7 +76,7 @@ Q_SIGNALS: private Q_SLOTS: void updateTabName(Database* db); - void openDatabaseDialog(); + void openDatabaseDialog(const QString& pw = QString(), const QString& keyFile = QString()); void openDatabaseRead(); void openDatabaseCleanup(); void emitEntrySelectionChanged(); diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index df4861856..9417eff8e 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -58,6 +58,11 @@ MainWindow::~MainWindow() { } +void MainWindow::openDatabase(const QString& fileName, const QString& pw, const QString& keyFile) +{ + m_ui->tabWidget->openDatabase(fileName, pw, keyFile); +} + const QString MainWindow::m_baseWindowTitle = "KeePassX"; void MainWindow::setMenuActionState(int index) diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index d870d01c5..000a95fd7 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -31,6 +31,7 @@ class MainWindow : public QMainWindow public: MainWindow(); ~MainWindow(); + void openDatabase(const QString& fileName, const QString& pw, const QString& keyFile); protected: void closeEvent(QCloseEvent *event); diff --git a/src/main.cpp b/src/main.cpp index 21bec90c3..0040a86fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,28 +15,46 @@ * along with this program. If not, see . */ +#include #include #include -#include "core/Database.h" #include "crypto/Crypto.h" -#include "format/KeePass2Reader.h" -#include "format/KeePass2XmlReader.h" #include "gui/MainWindow.h" #include "keys/CompositeKey.h" #include "keys/PasswordKey.h" -#include "../tests/config-keepassx-tests.h" - int main(int argc, char **argv) { QApplication app(argc, argv); - // don't set applicationName or organizationName as that changes QDesktopServices::storageLocation + // don't set applicationName or organizationName as that changes + // QDesktopServices::storageLocation() Crypto::init(); + QString filename; + QString password; + + const QStringList args = app.arguments(); + for (int i = 0; i < args.size(); i++) { + if (args[i] == "--password" && args.size() > (i + 1)) { + password = args[i + 1]; + i++; + } + else if (!args[i].startsWith("-") && QFile::exists(args[i])) { + filename = args[i]; + } + else { + qWarning("Unkown argument \"%s\"", qPrintable(args[i])); + } + } + MainWindow mainWindow; mainWindow.show(); + if (!filename.isEmpty()) { + mainWindow.openDatabase(filename, password, QString()); + } + return app.exec(); }