Allow passing a filename and password on the command line.

Passing passwords on the command line is not exactly best practice but will
greatly improve development productivity.
This commit is contained in:
Felix Geyer 2012-04-24 11:47:16 +02:00
parent 3aac16f03e
commit 974d4f5807
7 changed files with 54 additions and 11 deletions

View File

@ -68,6 +68,18 @@ Database* DatabaseOpenDialog::database()
return m_db; 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() void DatabaseOpenDialog::openDatabase()
{ {
KeePass2Reader reader; KeePass2Reader reader;

View File

@ -38,6 +38,7 @@ public:
explicit DatabaseOpenDialog(QFile* file, QString filename, QWidget* parent = 0); explicit DatabaseOpenDialog(QFile* file, QString filename, QWidget* parent = 0);
~DatabaseOpenDialog(); ~DatabaseOpenDialog();
Database* database(); Database* database();
void enterKey(const QString& pw, const QString& keyFile);
private Q_SLOTS: private Q_SLOTS:
void openDatabase(); void openDatabase();

View File

@ -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<QFile> file(new QFile(fileName)); QScopedPointer<QFile> file(new QFile(fileName));
// TODO error handling // TODO error handling
@ -96,16 +97,20 @@ void DatabaseTabWidget::openDatabase(const QString& fileName)
m_curDbStruct.file = file.take(); m_curDbStruct.file = file.take();
m_curDbStruct.fileName = QFileInfo(fileName).absoluteFilePath(); 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); m_curKeyDialog = new DatabaseOpenDialog(m_curDbStruct.file, m_curDbStruct.fileName, m_window);
connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead())); connect(m_curKeyDialog, SIGNAL(accepted()), SLOT(openDatabaseRead()));
connect(m_curKeyDialog, SIGNAL(rejected()), SLOT(openDatabaseCleanup())); connect(m_curKeyDialog, SIGNAL(rejected()), SLOT(openDatabaseCleanup()));
m_curKeyDialog->setModal(true); m_curKeyDialog->setModal(true);
m_curKeyDialog->show(); m_curKeyDialog->show();
if (!pw.isNull() || !keyFile.isEmpty()) {
m_curKeyDialog->enterKey(pw, keyFile);
}
} }
void DatabaseTabWidget::openDatabaseRead() void DatabaseTabWidget::openDatabaseRead()

View File

@ -48,7 +48,8 @@ class DatabaseTabWidget : public QTabWidget
public: public:
explicit DatabaseTabWidget(QWidget* parent); explicit DatabaseTabWidget(QWidget* parent);
void openDatabase(const QString& fileName); void openDatabase(const QString& fileName, const QString& pw = QString(),
const QString& keyFile = QString());
DatabaseWidget* currentDatabaseWidget(); DatabaseWidget* currentDatabaseWidget();
public Q_SLOTS: public Q_SLOTS:
@ -75,7 +76,7 @@ Q_SIGNALS:
private Q_SLOTS: private Q_SLOTS:
void updateTabName(Database* db); void updateTabName(Database* db);
void openDatabaseDialog(); void openDatabaseDialog(const QString& pw = QString(), const QString& keyFile = QString());
void openDatabaseRead(); void openDatabaseRead();
void openDatabaseCleanup(); void openDatabaseCleanup();
void emitEntrySelectionChanged(); void emitEntrySelectionChanged();

View File

@ -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"; const QString MainWindow::m_baseWindowTitle = "KeePassX";
void MainWindow::setMenuActionState(int index) void MainWindow::setMenuActionState(int index)

View File

@ -31,6 +31,7 @@ class MainWindow : public QMainWindow
public: public:
MainWindow(); MainWindow();
~MainWindow(); ~MainWindow();
void openDatabase(const QString& fileName, const QString& pw, const QString& keyFile);
protected: protected:
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);

View File

@ -15,28 +15,46 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <QtCore/QFile>
#include <QtGui/QApplication> #include <QtGui/QApplication>
#include <QtGui/QTreeView> #include <QtGui/QTreeView>
#include "core/Database.h"
#include "crypto/Crypto.h" #include "crypto/Crypto.h"
#include "format/KeePass2Reader.h"
#include "format/KeePass2XmlReader.h"
#include "gui/MainWindow.h" #include "gui/MainWindow.h"
#include "keys/CompositeKey.h" #include "keys/CompositeKey.h"
#include "keys/PasswordKey.h" #include "keys/PasswordKey.h"
#include "../tests/config-keepassx-tests.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
QApplication app(argc, 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(); 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 mainWindow;
mainWindow.show(); mainWindow.show();
if (!filename.isEmpty()) {
mainWindow.openDatabase(filename, password, QString());
}
return app.exec(); return app.exec();
} }