18
#include "ImportWizardPageSelect.h"
19
#include "ui_ImportWizardPageSelect.h"
21
#include "ImportWizard.h"
23
#include "gui/DatabaseWidget.h"
24
#include "gui/FileDialog.h"
26
#include "gui/MainWindow.h"
28
ImportWizardPageSelect::ImportWizardPageSelect(QWidget* parent)
30
, m_ui(new Ui::ImportWizardPageSelect())
34
new QListWidgetItem(icons()->icon("csv"), tr("Comma Separated Values (.csv)"), m_ui->importTypeList);
35
new QListWidgetItem(icons()->icon("onepassword"), tr("1Password Export (.1pux)"), m_ui->importTypeList);
36
new QListWidgetItem(icons()->icon("onepassword"), tr("1Password Vault (.opvault)"), m_ui->importTypeList);
37
new QListWidgetItem(icons()->icon("bitwarden"), tr("Bitwarden (.json)"), m_ui->importTypeList);
38
new QListWidgetItem(icons()->icon("object-locked"), tr("KeePass 1 Database (.kdb)"), m_ui->importTypeList);
40
m_ui->importTypeList->item(0)->setData(Qt::UserRole, ImportWizard::IMPORT_CSV);
41
m_ui->importTypeList->item(1)->setData(Qt::UserRole, ImportWizard::IMPORT_OPUX);
42
m_ui->importTypeList->item(2)->setData(Qt::UserRole, ImportWizard::IMPORT_OPVAULT);
43
m_ui->importTypeList->item(3)->setData(Qt::UserRole, ImportWizard::IMPORT_BITWARDEN);
44
m_ui->importTypeList->item(4)->setData(Qt::UserRole, ImportWizard::IMPORT_KEEPASS1);
46
connect(m_ui->importTypeList, &QListWidget::currentItemChanged, this, &ImportWizardPageSelect::itemSelected);
47
m_ui->importTypeList->setCurrentRow(0);
49
connect(m_ui->importFileButton, &QAbstractButton::clicked, this, &ImportWizardPageSelect::chooseImportFile);
50
connect(m_ui->keyFileButton, &QAbstractButton::clicked, this, &ImportWizardPageSelect::chooseKeyFile);
51
connect(m_ui->existingDatabaseRadio, &QRadioButton::toggled, this, [this](bool state) {
52
m_ui->existingDatabaseChoice->setEnabled(state);
55
updateDatabaseChoices();
57
registerField("ImportType", this);
58
registerField("ImportFile*", m_ui->importFileEdit);
59
registerField("ImportInto", m_ui->importIntoLabel);
60
registerField("ImportPassword", m_ui->passwordEdit, "text", "textChanged");
61
registerField("ImportKeyFile", m_ui->keyFileEdit);
64
ImportWizardPageSelect::~ImportWizardPageSelect()
68
void ImportWizardPageSelect::initializePage()
70
setField("ImportType", m_ui->importTypeList->currentItem()->data(Qt::UserRole).toInt());
74
bool ImportWizardPageSelect::validatePage()
76
if (m_ui->existingDatabaseRadio->isChecked()) {
77
if (m_ui->existingDatabaseChoice->currentIndex() == -1) {
80
setField("ImportInto", m_ui->existingDatabaseChoice->currentData());
82
setField("ImportInto", {});
88
void ImportWizardPageSelect::itemSelected(QListWidgetItem* current, QListWidgetItem* previous)
93
setCredentialState(false);
97
m_ui->importFileEdit->clear();
98
m_ui->passwordEdit->clear();
99
m_ui->keyFileEdit->clear();
101
auto type = current->data(Qt::UserRole).toInt();
102
setField("ImportType", type);
105
case ImportWizard::IMPORT_CSV:
106
case ImportWizard::IMPORT_OPUX:
107
setCredentialState(false);
110
case ImportWizard::IMPORT_BITWARDEN:
111
case ImportWizard::IMPORT_OPVAULT:
112
setCredentialState(true);
115
case ImportWizard::IMPORT_KEEPASS1:
116
setCredentialState(true, true);
123
void ImportWizardPageSelect::updateDatabaseChoices() const
125
m_ui->existingDatabaseChoice->clear();
126
auto mainWindow = getMainWindow();
128
for (auto dbWidget : mainWindow->getOpenDatabases()) {
130
disconnect(dbWidget, nullptr, nullptr, nullptr);
133
if (dbWidget->isLocked()) {
137
connect(dbWidget, &DatabaseWidget::databaseLocked, this, &ImportWizardPageSelect::updateDatabaseChoices);
138
connect(dbWidget, &DatabaseWidget::databaseModified, this, &ImportWizardPageSelect::updateDatabaseChoices);
141
m_ui->existingDatabaseRadio->setEnabled(true);
142
m_ui->existingDatabaseRadio->setToolTip("");
145
if (m_ui->existingDatabaseChoice->count() > 0) {
146
m_ui->existingDatabaseChoice->insertSeparator(m_ui->existingDatabaseChoice->count());
150
auto db = dbWidget->database();
151
m_ui->existingDatabaseChoice->addItem(
152
QString("%1 (%2)").arg(dbWidget->displayName(), db->rootGroup()->name()),
153
QList<QVariant>() << db->uuid() << db->rootGroup()->uuid());
155
if (dbWidget->isVisible()) {
156
m_ui->existingDatabaseChoice->setCurrentIndex(m_ui->existingDatabaseChoice->count() - 1);
160
for (const auto& group : db->rootGroup()->groupsRecursive(false)) {
161
if (!group->isRecycled()) {
162
auto path = group->hierarchy();
164
m_ui->existingDatabaseChoice->addItem(QString(" / %1").arg(path.join(" / ")),
165
QList<QVariant>() << db->uuid() << group->uuid());
171
if (m_ui->existingDatabaseChoice->count() == 0) {
172
m_ui->existingDatabaseRadio->setEnabled(false);
173
m_ui->newDatabaseRadio->setChecked(true);
177
void ImportWizardPageSelect::chooseImportFile()
182
if (field("ImportType").toInt() == ImportWizard::IMPORT_OPVAULT) {
183
file = fileDialog()->getExistingDirectory(this, tr("Open OPVault"), QDir::homePath());
186
file = fileDialog()->getOpenFileName(this, tr("Select import file"), QDir::homePath(), importFileFilter());
191
if (!file.isEmpty()) {
192
m_ui->importFileEdit->setText(file);
196
void ImportWizardPageSelect::chooseKeyFile()
198
auto filter = QString("%1 (*);;%2 (*.keyx; *.key)").arg(tr("All files"), tr("Key files"));
199
auto file = fileDialog()->getOpenFileName(this, tr("Select key file"), QDir::homePath(), filter);
200
if (!file.isEmpty()) {
201
m_ui->keyFileEdit->setText(file);
205
void ImportWizardPageSelect::setCredentialState(bool passwordEnabled, bool keyFileEnable)
207
bool passwordStateChanged = m_ui->passwordLabel->isVisible() != passwordEnabled;
208
m_ui->passwordLabel->setVisible(passwordEnabled);
209
m_ui->passwordEdit->setVisible(passwordEnabled);
211
bool keyFileStateChanged = m_ui->keyFileLabel->isVisible() != keyFileEnable;
212
m_ui->keyFileLabel->setVisible(keyFileEnable);
213
m_ui->keyFileEdit->setVisible(keyFileEnable);
214
m_ui->keyFileButton->setVisible(keyFileEnable);
218
int height = window()->height();
219
if (passwordStateChanged) {
220
auto diff = m_ui->passwordEdit->height() + m_ui->inputFields->layout()->spacing();
221
height += passwordEnabled ? diff : -diff;
223
if (keyFileStateChanged) {
224
auto diff = m_ui->keyFileEdit->height() + m_ui->inputFields->layout()->spacing();
225
height += keyFileEnable ? diff : -diff;
227
window()->resize(window()->width(), height);
231
QString ImportWizardPageSelect::importFileFilter()
233
switch (field("ImportType").toInt()) {
234
case ImportWizard::IMPORT_CSV:
235
return QString("%1 (*.csv);;%2 (*)").arg(tr("Comma Separated Values"), tr("All files"));
236
case ImportWizard::IMPORT_OPUX:
237
return QString("%1 (*.1pux)").arg(tr("1Password Export"));
238
case ImportWizard::IMPORT_BITWARDEN:
239
return QString("%1 (*.json)").arg(tr("Bitwarden JSON Export"));
240
case ImportWizard::IMPORT_OPVAULT:
241
return QString("%1 (*.opvault)").arg(tr("1Password Vault"));
242
case ImportWizard::IMPORT_KEEPASS1:
243
return QString("%1 (*.kdb)").arg(tr("KeePass1 Database"));