aboutsummaryrefslogtreecommitdiffstats
path: root/wizard.cpp
blob: b9fd9144a6b0457c6477e5ee2996ef68cee15e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *      wizard.cpp
 *
 *      Copyright 2008 gpg4usb-team <[email protected]>
 *
 *      This file is part of gpg4usb.
 *
 *      Gpg4usb 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 3 of the License, or
 *      (at your option) any later version.
 *
 *      Gpg4usb 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 gpg4usb.  If not, see <http://www.gnu.org/licenses/>
 */

#include <QtGui>

 #include "wizard.h"

Wizard::Wizard(GpgME::GpgContext *ctx, KeyMgmt *keyMgmt, QWidget *parent)
    : QWizard(parent)
{
    mCtx=ctx;
    mKeyMgmt=keyMgmt;
    IntroPage *introPage = new IntroPage();
    KeyGenPage *keyGenPage = new KeyGenPage(mCtx);
    ImportPage *importPage = new ImportPage(mCtx,mKeyMgmt);
    ConclusionPage *conclusionPage = new ConclusionPage();
    addPage(introPage);
    addPage(keyGenPage);
    addPage(importPage);
    addPage(conclusionPage);

#ifndef Q_WS_MAC
    setWizardStyle(ModernStyle);
#endif

    setWindowTitle(tr("First Start Wizard"));
}

IntroPage::IntroPage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Introduction"));

     topLabel = new QLabel(tr("This wizard will help you getting started with encrypting and decrypting."));
     topLabel->setWordWrap(true);

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(topLabel);
     setLayout(layout);
 }

int IntroPage::nextId() const
{
    return 1;
}

KeyGenPage::KeyGenPage(GpgME::GpgContext *ctx, QWidget *parent)
     : QWizardPage(parent)
{
    mCtx=ctx;
    setTitle(tr("Key-Generating"));
    topLabel = new QLabel(tr("First you've got to create an own keypair.<br/>"
                             "The pair contains a public and a private key.<br/>"
                             "Other users can use the public key to encrypt texts for you<br/>"
                             "and verify texts signed by you.<br/>"
                             "You can use the private key to decrypt and sign texts.<br/>"
                             "For more information have a look in the online tutorial:"));
    QLabel *linkLabel = new QLabel("<a href=""http://gpg4usb.cpunk.de/docu.html"">"+tr("Online tutorial")+"</a>");
    linkLabel->setOpenExternalLinks(true);

    QWidget *createKeyButtonBox = new QWidget(this);
    QHBoxLayout  *createKeyButtonBoxLayout = new QHBoxLayout(createKeyButtonBox);
    createKeyButton = new QPushButton(tr("Create New Key"));
    createKeyButtonBoxLayout->addWidget(createKeyButton);
    createKeyButtonBoxLayout->addStretch(1);
    layout = new QVBoxLayout();
    layout->addWidget(topLabel);
    layout->addWidget(linkLabel);
    layout->addWidget(createKeyButtonBox);
    connect(createKeyButton, SIGNAL(clicked()), this, SLOT(generateKeyDialog()));

    setLayout(layout);
}

int KeyGenPage::nextId() const
{
    return 2;
}

void KeyGenPage::generateKeyDialog()
{
    KeyGenDialog *keyGenDialog = new KeyGenDialog(mCtx, this);
    connect(mCtx, SIGNAL(keyDBChanged()), this, SLOT(showKeyGeneratedMessage()));
    keyGenDialog->show();
}

void KeyGenPage::showKeyGeneratedMessage()
{
    layout->addWidget(new QLabel(tr("key generated. Now you can crypt and sign texts.")));
}

ImportPage::ImportPage(GpgME::GpgContext *ctx, KeyMgmt *keyMgmt, QWidget *parent)
     : QWizardPage(parent)
{
    mCtx=ctx;
    mKeyMgmt=keyMgmt;
    setTitle(tr("Keyring Import"));
    QGroupBox *gnupgBox = new QGroupBox(tr("Import from GnuPG"), this);

    QGridLayout *gnupgLayout = new QGridLayout();
    gnupgLabel = new QLabel(tr("Should I try to import keys from GnuPG?"));
    gnupgLayout->addWidget(gnupgLabel,1,1,1,2);

    gnupgPrivKeyCheckBox = new QCheckBox();
    gnupgPrivKeyCheckBox->setChecked(true);
    gnupgLayout->addWidget(gnupgPrivKeyCheckBox,2,1,Qt::AlignRight);
    QLabel *privateKeyLabel = new QLabel(tr("Private Keys"));
    gnupgLayout->addWidget(privateKeyLabel,2,2);

    gnupgpPubKeyCheckBox = new QCheckBox();
    gnupgpPubKeyCheckBox->setChecked(true);
    gnupgLayout->addWidget(gnupgpPubKeyCheckBox,3,1,Qt::AlignRight);
    QLabel *gnupgPrivKeyLabel = new QLabel(tr("Public Keys"));
    gnupgLayout->addWidget(gnupgPrivKeyLabel,3,2);


    QWidget *importFromGnupgButtonBox = new QWidget(this);
    QHBoxLayout  *importFromGnupgButtonBoxLayout = new QHBoxLayout(importFromGnupgButtonBox);
    importFromGnupgButton = new QPushButton(tr("Import keys from GnuPG"));
    connect(importFromGnupgButton, SIGNAL(clicked()), this, SLOT(importKeysFromGnupg()));
    importFromGnupgButtonBox->setLayout(importFromGnupgButtonBoxLayout);
    gnupgLayout->addWidget(importFromGnupgButton,2,3);

    gnupgBox->setLayout(gnupgLayout);

    QGroupBox *gpg4usbBox = new QGroupBox(tr("Import from older gpg4usb"), this);

    QGridLayout *gpg4usbLayout = new QGridLayout();
    QLabel *gnupgLabel = new QLabel(tr("Point to the folder of last gpg4usb"));
    gpg4usbLayout->addWidget(gnupgLabel,1,1,1,2);

    gpg4usbPrivKeyCheckBox = new QCheckBox();
    gpg4usbPrivKeyCheckBox->setChecked(true);
    gpg4usbLayout->addWidget(gpg4usbPrivKeyCheckBox,2,1,Qt::AlignRight);
    QLabel *privateKeyLabel2 = new QLabel(tr("Private Keys"));
    gpg4usbLayout->addWidget(privateKeyLabel2,2,2);

    gpg4usbPubKeyCheckBox = new QCheckBox();
    gpg4usbPubKeyCheckBox->setChecked(true);
    gpg4usbLayout->addWidget(gpg4usbPubKeyCheckBox,3,1,Qt::AlignRight);
    QLabel *gpg4usbLabel = new QLabel(tr("Public Keys"));
    gpg4usbLayout->addWidget(gpg4usbLabel,3,2);

    QWidget *importFromGpg4usbButtonBox = new QWidget(this);
    QHBoxLayout  *importFromGpg4usbButtonBoxLayout = new QHBoxLayout(importFromGpg4usbButtonBox);
    importFromGpg4usbButton = new QPushButton(tr("Import keys from gpg4usb"));
    connect(importFromGpg4usbButton, SIGNAL(clicked()), this, SLOT(importKeysFromGpg4usb()));
    importFromGpg4usbButtonBox->setLayout(importFromGpg4usbButtonBoxLayout);
    gpg4usbLayout->addWidget(importFromGpg4usbButton,2,3);

    gpg4usbBox->setLayout(gpg4usbLayout);

    layout = new QVBoxLayout();
    layout->addWidget(gnupgBox);
    layout->addWidget(gpg4usbBox);

    setLayout(layout);
}

bool ImportPage::importKeysFromGpg4usb()
{
    QString dir = QFileDialog::getExistingDirectory(this,tr("Old gpg4usb directory"));

    // Return, if cancel was hit
    if (dir.isEmpty()) {
        return false;
    }

    QFile secRing(dir+"/keydb/secring.gpg");
    QFile pubRing(dir+"/keydb/pubring.gpg");

    qDebug() << pubRing.fileName();
    // Return, if no keyrings are found in subdir of chosen dir
    if (!(pubRing.exists() or secRing.exists())) {
        QMessageBox::critical(0, tr("Import Error"), tr("Couldn't locate any keyring file in choosen directory"));
        return false;
    }

    if (pubRing.exists() and gnupgpPubKeyCheckBox->isChecked()) {
        if (!pubRing.open(QIODevice::ReadOnly)) {
            QMessageBox::critical(0, tr("Import error"), tr("Couldn't open public keyringfile: ") + pubRing.fileName());
            return false;
        }
        QByteArray inBuffer = pubRing.readAll();
        mKeyMgmt->importKeys(inBuffer);
    }

    if (secRing.exists() and gnupgPrivKeyCheckBox->isChecked()) {
        if (!secRing.open(QIODevice::ReadOnly)) {
            QMessageBox::critical(0, tr("Import error"), tr("Couldn't open private keyringfile: ") + secRing.fileName());
            return false;
        }
        QByteArray inBuffer = secRing.readAll();
        mKeyMgmt->importKeys(inBuffer);
    }
    return true;
}

bool ImportPage::importKeysFromGnupg()
{
    // first get gnupghomedir and check, if it exists
    QString gnuPGHome = getGnuPGHome();
    if (gnuPGHome == NULL) {
        QMessageBox::critical(0, tr("Import Error"), tr("Couldn't locate GnuPG home directory"));
        return false;
    }

    // try to import private files, if private key checkbox is checked
    if (gnupgPrivKeyCheckBox->isChecked()) {
        QString privRingFile = gnuPGHome+"/secring.gpg";
        QFile file;
        file.setFileName(privRingFile);
        if (!file.open(QIODevice::ReadOnly)) {
            QMessageBox::critical(0, tr("Import error"), tr("Couldn't open private keyringfile: ") + privRingFile);
            return false;
        }
        QByteArray inBuffer = file.readAll();

        mKeyMgmt->importKeys(inBuffer);
    }

    // try to import public keys, if public checkbox is checked
    if (gnupgpPubKeyCheckBox->isChecked()) {
        QString pubRingFile = gnuPGHome+"/pubring.gpg";
        QFile file;
        file.setFileName(pubRingFile);
        if (!file.open(QIODevice::ReadOnly)) {
            QMessageBox::critical(0, tr("Import error"), tr("Couldn't open public keyringfile: ") + pubRingFile);
            return false;
        }
        QByteArray inBuffer = file.readAll();

        mKeyMgmt->importKeys(inBuffer);
    }

    return true;
}

QString ImportPage::getGnuPGHome()
{
    QString gnuPGHome="";
    #ifdef _WIN32
        QSettings gnuPGsettings("HKEY_CURRENT_USER\\Software\\GNU\\GNUPG", QSettings::NativeFormat);
        gnuPGHome = gnuPGsettings.value("HomeDir").toString();
        if (gnuPGHome.isEmpty()) {
            return NULL;
        }

    #else
        gnuPGHome=QDir::homePath()+"/.gnupg";
        if (! QFile(gnuPGHome).exists()) {
            return NULL;
        }
    #endif

    return gnuPGHome;
}

int ImportPage::nextId() const
{
    return 3;
}

ConclusionPage::ConclusionPage(QWidget *parent)
    : QWizardPage(parent)
{
    setTitle(tr("Finish Start Wizard"));

    bottomLabel = new QLabel(tr("You're ready to encrypt and decrpt now."));
    bottomLabel->setWordWrap(true);

    showWizardCheckBox = new QCheckBox(tr("Dont show the wizard again."));
    showWizardCheckBox->setChecked(Qt::Checked);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(bottomLabel);
    layout->addWidget(showWizardCheckBox);
    setLayout(layout);
    setVisible(true);
}

int ConclusionPage::nextId() const
{
    if (showWizardCheckBox->isChecked())
    {
        QSettings settings;
        settings.setValue("wizard/showWizard", false);
    }
    return -1;
}