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;
}
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;

View File

@ -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();

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));
// 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()

View File

@ -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();

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";
void MainWindow::setMenuActionState(int index)

View File

@ -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);

View File

@ -15,28 +15,46 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QtCore/QFile>
#include <QtGui/QApplication>
#include <QtGui/QTreeView>
#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();
}