2010-09-19 10:59:32 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "MainWindow.h"
|
2011-12-26 12:55:50 -05:00
|
|
|
#include "ui_MainWindow.h"
|
2010-09-19 10:59:32 -04:00
|
|
|
|
2012-04-11 14:35:52 -04:00
|
|
|
#include <QtGui/QCloseEvent>
|
|
|
|
|
2010-09-19 10:59:32 -04:00
|
|
|
#include "core/Database.h"
|
2012-01-05 16:27:08 -05:00
|
|
|
#include "core/DataPath.h"
|
2010-09-19 10:59:32 -04:00
|
|
|
#include "core/Metadata.h"
|
2012-05-02 09:37:21 -04:00
|
|
|
#include "gui/AboutDialog.h"
|
2011-07-08 08:51:14 -04:00
|
|
|
#include "gui/DatabaseWidget.h"
|
2012-05-16 04:16:32 -04:00
|
|
|
#include "gui/entry/EntryView.h"
|
2010-09-19 10:59:32 -04:00
|
|
|
|
|
|
|
MainWindow::MainWindow()
|
2011-12-26 12:55:50 -05:00
|
|
|
: m_ui(new Ui::MainWindow())
|
2010-09-19 10:59:32 -04:00
|
|
|
{
|
2011-12-26 12:55:50 -05:00
|
|
|
m_ui->setupUi(this);
|
2010-09-19 10:59:32 -04:00
|
|
|
|
2012-01-05 16:36:06 -05:00
|
|
|
setWindowIcon(dataPath()->applicationIcon());
|
2012-05-06 14:17:26 -04:00
|
|
|
m_ui->toolBar->toggleViewAction()->setText(tr("Show toolbar"));
|
2012-01-05 16:27:08 -05:00
|
|
|
|
2012-05-15 12:16:04 -04:00
|
|
|
setShortcut(m_ui->actionDatabaseOpen, QKeySequence::Open, Qt::CTRL + Qt::Key_O);
|
|
|
|
setShortcut(m_ui->actionDatabaseSave, QKeySequence::Save, Qt::CTRL + Qt::Key_S);
|
|
|
|
setShortcut(m_ui->actionDatabaseSaveAs, QKeySequence::SaveAs);
|
|
|
|
setShortcut(m_ui->actionDatabaseClose, QKeySequence::Close, Qt::CTRL + Qt::Key_W);
|
|
|
|
setShortcut(m_ui->actionQuit, QKeySequence::Quit, Qt::CTRL + Qt::Key_Q);
|
2012-05-19 05:53:32 -04:00
|
|
|
setShortcut(m_ui->actionSearch, QKeySequence::Find, Qt::CTRL + Qt::Key_F);
|
2012-05-26 10:20:32 -04:00
|
|
|
m_ui->actionEntryCopyUsername->setShortcut(Qt::CTRL + Qt::Key_B);
|
|
|
|
m_ui->actionEntryCopyPassword->setShortcut(Qt::CTRL + Qt::Key_C);
|
2012-05-15 12:16:04 -04:00
|
|
|
|
2012-05-27 09:39:32 -04:00
|
|
|
m_ui->actionDatabaseNew->setIcon(dataPath()->icon("actions", "document-new"));
|
|
|
|
m_ui->actionDatabaseOpen->setIcon(dataPath()->icon("actions", "document-open"));
|
|
|
|
m_ui->actionDatabaseSave->setIcon(dataPath()->icon("actions", "document-save"));
|
|
|
|
m_ui->actionDatabaseSaveAs->setIcon(dataPath()->icon("actions", "document-save-as"));
|
|
|
|
m_ui->actionDatabaseClose->setIcon(dataPath()->icon("actions", "document-close"));
|
|
|
|
m_ui->actionChangeDatabaseSettings->setIcon(dataPath()->icon("actions", "document-edit"));
|
|
|
|
m_ui->actionChangeMasterKey->setIcon(dataPath()->icon("actions", "database-change-key", false));
|
|
|
|
m_ui->actionQuit->setIcon(dataPath()->icon("actions", "application-exit"));
|
|
|
|
|
2012-05-27 06:46:22 -04:00
|
|
|
m_ui->actionEntryNew->setIcon(dataPath()->icon("actions", "entry-new", false));
|
|
|
|
m_ui->actionEntryClone->setIcon(dataPath()->icon("actions", "entry-clone", false));
|
|
|
|
m_ui->actionEntryEdit->setIcon(dataPath()->icon("actions", "entry-edit", false));
|
|
|
|
m_ui->actionEntryDelete->setIcon(dataPath()->icon("actions", "entry-delete", false));
|
|
|
|
|
2012-05-27 09:39:32 -04:00
|
|
|
m_ui->actionGroupNew->setIcon(dataPath()->icon("actions", "group-new", false));
|
|
|
|
m_ui->actionGroupEdit->setIcon(dataPath()->icon("actions", "group-edit", false));
|
|
|
|
m_ui->actionGroupDelete->setIcon(dataPath()->icon("actions", "group-delete", false));
|
|
|
|
|
|
|
|
m_ui->actionSettings->setIcon(dataPath()->icon("actions", "configure"));
|
|
|
|
|
2012-05-27 13:54:18 -04:00
|
|
|
m_ui->actionAbout->setIcon(dataPath()->icon("actions", "help-about"));
|
|
|
|
|
2012-05-27 09:39:32 -04:00
|
|
|
m_ui->actionSearch->setIcon(dataPath()->icon("actions", "system-search"));
|
2012-05-27 06:46:22 -04:00
|
|
|
|
2012-04-25 14:22:55 -04:00
|
|
|
connect(m_ui->tabWidget, SIGNAL(entrySelectionChanged(bool)),
|
|
|
|
SLOT(setMenuActionState()));
|
|
|
|
connect(m_ui->tabWidget, SIGNAL(currentWidgetModeChanged(DatabaseWidget::Mode)),
|
|
|
|
SLOT(setMenuActionState(DatabaseWidget::Mode)));
|
|
|
|
connect(m_ui->tabWidget, SIGNAL(tabNameChanged()),
|
|
|
|
SLOT(updateWindowTitle()));
|
|
|
|
connect(m_ui->tabWidget, SIGNAL(currentChanged(int)),
|
|
|
|
SLOT(updateWindowTitle()));
|
2012-05-28 12:49:16 -04:00
|
|
|
connect(m_ui->tabWidget, SIGNAL(currentChanged(int)),
|
|
|
|
SLOT(databaseTabChanged(int)));
|
2012-05-27 05:09:52 -04:00
|
|
|
connect(m_ui->stackedWidget, SIGNAL(currentChanged(int)), SLOT(setMenuActionState()));
|
2012-05-27 14:29:15 -04:00
|
|
|
connect(m_ui->settingsWidget, SIGNAL(editFinished(bool)), SLOT(switchToDatabases()));
|
2011-12-30 12:43:24 -05:00
|
|
|
|
2012-04-25 14:22:55 -04:00
|
|
|
connect(m_ui->actionDatabaseNew, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(newDatabase()));
|
|
|
|
connect(m_ui->actionDatabaseOpen, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(openDatabase()));
|
|
|
|
connect(m_ui->actionDatabaseSave, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(saveDatabase()));
|
|
|
|
connect(m_ui->actionDatabaseSaveAs, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(saveDatabaseAs()));
|
|
|
|
connect(m_ui->actionDatabaseClose, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(closeDatabase()));
|
|
|
|
connect(m_ui->actionChangeMasterKey, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(changeMasterKey()));
|
|
|
|
connect(m_ui->actionChangeDatabaseSettings, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(changeDatabaseSettings()));
|
2012-05-12 04:08:41 -04:00
|
|
|
connect(m_ui->actionImportKeePass1, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(importKeePass1Database()));
|
|
|
|
connect(m_ui->actionQuit, SIGNAL(triggered()), SLOT(close()));
|
|
|
|
|
2012-04-25 14:22:55 -04:00
|
|
|
connect(m_ui->actionEntryNew, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(createEntry()));
|
2012-05-15 15:10:39 -04:00
|
|
|
connect(m_ui->actionEntryClone, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(cloneEntry()));
|
2012-04-25 14:22:55 -04:00
|
|
|
connect(m_ui->actionEntryEdit, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(editEntry()));
|
|
|
|
connect(m_ui->actionEntryDelete, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(deleteEntry()));
|
2012-05-26 10:20:32 -04:00
|
|
|
connect(m_ui->actionEntryCopyUsername, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(copyUsername()));
|
|
|
|
connect(m_ui->actionEntryCopyPassword, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(copyPassword()));
|
2012-05-12 04:08:41 -04:00
|
|
|
|
2012-04-25 14:22:55 -04:00
|
|
|
connect(m_ui->actionGroupNew, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(createGroup()));
|
|
|
|
connect(m_ui->actionGroupEdit, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(editGroup()));
|
|
|
|
connect(m_ui->actionGroupDelete, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(deleteGroup()));
|
2012-05-19 05:53:32 -04:00
|
|
|
|
2012-05-27 05:09:52 -04:00
|
|
|
connect(m_ui->actionSettings, SIGNAL(triggered()), SLOT(switchToSettings()));
|
|
|
|
|
|
|
|
connect(m_ui->actionAbout, SIGNAL(triggered()), SLOT(showAboutDialog()));
|
|
|
|
|
2012-05-19 05:53:32 -04:00
|
|
|
connect(m_ui->actionSearch, SIGNAL(triggered()), m_ui->tabWidget,
|
|
|
|
SLOT(toggleSearch()));
|
2011-12-26 12:55:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
2010-09-19 10:59:32 -04:00
|
|
|
}
|
2011-11-16 12:47:17 -05:00
|
|
|
|
2012-04-24 05:47:16 -04:00
|
|
|
void MainWindow::openDatabase(const QString& fileName, const QString& pw, const QString& keyFile)
|
|
|
|
{
|
|
|
|
m_ui->tabWidget->openDatabase(fileName, pw, keyFile);
|
|
|
|
}
|
|
|
|
|
2012-05-14 13:10:42 -04:00
|
|
|
const QString MainWindow::BaseWindowTitle = "KeePassX";
|
2012-04-21 16:02:12 -04:00
|
|
|
|
2012-04-24 19:32:05 -04:00
|
|
|
void MainWindow::setMenuActionState(DatabaseWidget::Mode mode)
|
2011-11-16 12:47:17 -05:00
|
|
|
{
|
2012-05-27 05:09:52 -04:00
|
|
|
bool inDatabaseTabWidget = (m_ui->stackedWidget->currentIndex() == 0);
|
2012-05-27 14:06:03 -04:00
|
|
|
bool inWelcomeWidget = (m_ui->stackedWidget->currentIndex() == 2);
|
2012-05-27 05:09:52 -04:00
|
|
|
|
|
|
|
if (inDatabaseTabWidget && m_ui->tabWidget->currentIndex() != -1) {
|
2012-04-16 15:28:49 -04:00
|
|
|
DatabaseWidget* dbWidget = m_ui->tabWidget->currentDatabaseWidget();
|
|
|
|
Q_ASSERT(dbWidget);
|
|
|
|
|
2012-04-24 19:32:05 -04:00
|
|
|
if (mode == DatabaseWidget::None) {
|
|
|
|
mode = dbWidget->currentMode();
|
2012-04-16 15:28:49 -04:00
|
|
|
}
|
|
|
|
|
2012-04-24 19:32:05 -04:00
|
|
|
switch (mode) {
|
2012-05-25 07:32:37 -04:00
|
|
|
case DatabaseWidget::ViewMode: {
|
2012-05-26 13:40:02 -04:00
|
|
|
m_ui->actionEntryNew->setEnabled(dbWidget->actionEnabled(DatabaseWidget::EntryNew));
|
|
|
|
m_ui->actionEntryClone->setEnabled(dbWidget->actionEnabled(DatabaseWidget::EntryClone));
|
|
|
|
m_ui->actionEntryEdit->setEnabled(dbWidget->actionEnabled(DatabaseWidget::EntryEditView));
|
|
|
|
m_ui->actionEntryDelete->setEnabled(dbWidget->actionEnabled(DatabaseWidget::EntryDelete));
|
|
|
|
m_ui->actionEntryCopyUsername->setEnabled(dbWidget->actionEnabled(DatabaseWidget::EntryCopyUsername));
|
|
|
|
m_ui->actionEntryCopyPassword->setEnabled(dbWidget->actionEnabled(DatabaseWidget::EntryCopyPassword));
|
|
|
|
m_ui->actionGroupNew->setEnabled(dbWidget->actionEnabled(DatabaseWidget::GroupNew));
|
|
|
|
m_ui->actionGroupEdit->setEnabled(dbWidget->actionEnabled(DatabaseWidget::GroupEdit));
|
|
|
|
m_ui->actionGroupDelete->setEnabled(dbWidget->actionEnabled(DatabaseWidget::GroupDelete));
|
2012-05-19 05:53:32 -04:00
|
|
|
m_ui->actionSearch->setEnabled(true);
|
2012-05-26 13:40:02 -04:00
|
|
|
// TODO: get checked state from db widget
|
|
|
|
m_ui->actionSearch->setChecked(dbWidget->entryView()->inSearch());
|
2012-04-25 14:22:55 -04:00
|
|
|
m_ui->actionChangeMasterKey->setEnabled(true);
|
|
|
|
m_ui->actionChangeDatabaseSettings->setEnabled(true);
|
|
|
|
m_ui->actionDatabaseSave->setEnabled(true);
|
|
|
|
m_ui->actionDatabaseSaveAs->setEnabled(true);
|
|
|
|
break;
|
2012-05-25 07:32:37 -04:00
|
|
|
}
|
2012-04-25 14:22:55 -04:00
|
|
|
case DatabaseWidget::EditMode:
|
|
|
|
m_ui->actionEntryNew->setEnabled(false);
|
2012-05-15 15:10:39 -04:00
|
|
|
m_ui->actionEntryClone->setEnabled(false);
|
2012-04-25 14:22:55 -04:00
|
|
|
m_ui->actionEntryEdit->setEnabled(false);
|
|
|
|
m_ui->actionEntryDelete->setEnabled(false);
|
2012-05-26 10:20:32 -04:00
|
|
|
m_ui->actionEntryCopyUsername->setEnabled(false);
|
|
|
|
m_ui->actionEntryCopyPassword->setEnabled(false);
|
|
|
|
m_ui->actionGroupNew->setEnabled(false);
|
|
|
|
m_ui->actionGroupEdit->setEnabled(false);
|
2012-04-25 14:22:55 -04:00
|
|
|
m_ui->actionGroupDelete->setEnabled(false);
|
2012-05-19 05:53:32 -04:00
|
|
|
m_ui->actionSearch->setEnabled(false);
|
|
|
|
m_ui->actionSearch->setChecked(false);
|
2012-04-25 14:22:55 -04:00
|
|
|
m_ui->actionChangeMasterKey->setEnabled(false);
|
|
|
|
m_ui->actionChangeDatabaseSettings->setEnabled(false);
|
|
|
|
m_ui->actionDatabaseSave->setEnabled(false);
|
|
|
|
m_ui->actionDatabaseSaveAs->setEnabled(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
2012-04-16 15:28:49 -04:00
|
|
|
}
|
|
|
|
m_ui->actionDatabaseClose->setEnabled(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_ui->actionEntryNew->setEnabled(false);
|
2012-05-15 15:10:39 -04:00
|
|
|
m_ui->actionEntryClone->setEnabled(false);
|
2012-04-16 15:28:49 -04:00
|
|
|
m_ui->actionEntryEdit->setEnabled(false);
|
|
|
|
m_ui->actionEntryDelete->setEnabled(false);
|
2012-05-26 10:20:32 -04:00
|
|
|
m_ui->actionEntryCopyUsername->setEnabled(false);
|
|
|
|
m_ui->actionEntryCopyPassword->setEnabled(false);
|
|
|
|
m_ui->actionGroupNew->setEnabled(false);
|
|
|
|
m_ui->actionGroupEdit->setEnabled(false);
|
2012-04-21 13:06:28 -04:00
|
|
|
m_ui->actionGroupDelete->setEnabled(false);
|
2012-05-19 05:53:32 -04:00
|
|
|
m_ui->actionSearch->setEnabled(false);
|
|
|
|
m_ui->actionSearch->setChecked(false);
|
2012-04-16 15:28:49 -04:00
|
|
|
m_ui->actionChangeMasterKey->setEnabled(false);
|
2012-04-19 10:20:20 -04:00
|
|
|
m_ui->actionChangeDatabaseSettings->setEnabled(false);
|
2012-04-16 15:28:49 -04:00
|
|
|
m_ui->actionDatabaseSave->setEnabled(false);
|
|
|
|
m_ui->actionDatabaseSaveAs->setEnabled(false);
|
|
|
|
|
|
|
|
m_ui->actionDatabaseClose->setEnabled(false);
|
|
|
|
}
|
2012-05-27 05:09:52 -04:00
|
|
|
|
2012-05-27 14:06:03 -04:00
|
|
|
m_ui->actionDatabaseNew->setEnabled(inDatabaseTabWidget || inWelcomeWidget);
|
|
|
|
m_ui->actionDatabaseOpen->setEnabled(inDatabaseTabWidget || inWelcomeWidget);
|
|
|
|
m_ui->actionImportKeePass1->setEnabled(inDatabaseTabWidget || inWelcomeWidget);
|
2011-11-16 12:47:17 -05:00
|
|
|
}
|
2012-04-11 14:35:52 -04:00
|
|
|
|
2012-04-21 16:02:12 -04:00
|
|
|
void MainWindow::updateWindowTitle()
|
|
|
|
{
|
|
|
|
int index = m_ui->tabWidget->currentIndex();
|
|
|
|
if (index == -1) {
|
2012-05-14 13:10:42 -04:00
|
|
|
setWindowTitle(BaseWindowTitle);
|
2012-04-21 16:02:12 -04:00
|
|
|
}
|
|
|
|
else {
|
2012-05-28 12:38:33 -04:00
|
|
|
QString windowTitle = m_ui->tabWidget->tabText(index);
|
|
|
|
if (m_ui->tabWidget->readOnly(index)) {
|
|
|
|
windowTitle.append(" [").append(tr("read-only")).append("]");
|
|
|
|
}
|
|
|
|
setWindowTitle(windowTitle.append(" - ").append(BaseWindowTitle));
|
2012-04-21 16:02:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 09:37:21 -04:00
|
|
|
void MainWindow::showAboutDialog()
|
|
|
|
{
|
|
|
|
AboutDialog* aboutDialog = new AboutDialog(this);
|
|
|
|
aboutDialog->show();
|
|
|
|
}
|
|
|
|
|
2012-05-27 05:09:52 -04:00
|
|
|
void MainWindow::switchToDatabases()
|
|
|
|
{
|
2012-05-30 09:17:51 -04:00
|
|
|
if (m_ui->tabWidget->currentIndex() == -1) {
|
|
|
|
m_ui->stackedWidget->setCurrentIndex(2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_ui->stackedWidget->setCurrentIndex(0);
|
|
|
|
}
|
2012-05-27 05:09:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::switchToSettings()
|
|
|
|
{
|
|
|
|
m_ui->settingsWidget->loadSettings();
|
|
|
|
m_ui->stackedWidget->setCurrentIndex(1);
|
|
|
|
}
|
|
|
|
|
2012-05-28 12:49:16 -04:00
|
|
|
void MainWindow::databaseTabChanged(int tabIndex)
|
|
|
|
{
|
|
|
|
if (tabIndex != -1 && m_ui->stackedWidget->currentIndex() == 2) {
|
2012-05-30 09:17:51 -04:00
|
|
|
m_ui->stackedWidget->setCurrentIndex(0);
|
2012-05-28 12:49:16 -04:00
|
|
|
}
|
2012-05-28 12:53:39 -04:00
|
|
|
else if (tabIndex == -1 && m_ui->stackedWidget->currentIndex() == 0) {
|
|
|
|
m_ui->stackedWidget->setCurrentIndex(2);
|
|
|
|
}
|
2012-05-28 12:49:16 -04:00
|
|
|
}
|
|
|
|
|
2012-05-02 11:04:03 -04:00
|
|
|
void MainWindow::closeEvent(QCloseEvent* event) {
|
2012-04-11 14:35:52 -04:00
|
|
|
if (!m_ui->tabWidget->closeAllDatabases()) {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
}
|
2012-05-15 12:16:04 -04:00
|
|
|
|
|
|
|
void MainWindow::setShortcut(QAction* action, QKeySequence::StandardKey standard, int fallback)
|
|
|
|
{
|
|
|
|
if (!QKeySequence::keyBindings(standard).isEmpty()) {
|
|
|
|
action->setShortcuts(standard);
|
|
|
|
}
|
|
|
|
else if (fallback != 0) {
|
|
|
|
action->setShortcut(QKeySequence(fallback));
|
|
|
|
}
|
|
|
|
}
|