aboutsummaryrefslogtreecommitdiffstats
path: root/keylist.cpp
blob: 3d93433dd195ba51ed8589d706d9105609d57aee (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
/*
 *      keylist.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 "keylist.h"

KeyList::KeyList(GpgME::GpgContext *ctx, QWidget *parent)
        : QWidget(parent)
{
    mCtx = ctx;

    mKeyList = new QTableWidget(this);
    mKeyList->setColumnCount(6);
    mKeyList->verticalHeader()->hide();
    mKeyList->setShowGrid(false);
    mKeyList->setColumnWidth(0, 24);
    mKeyList->setColumnWidth(1, 20);
    mKeyList->sortByColumn(2, Qt::AscendingOrder);
    mKeyList->setSelectionBehavior(QAbstractItemView::SelectRows);
    // hide id and fingerprint of key
    mKeyList->setColumnHidden(4, true);
    mKeyList->setColumnHidden(5, true);

    // tableitems not editable
    mKeyList->setEditTriggers(QAbstractItemView::NoEditTriggers);
    // no focus (rectangle around tableitems)
    // may be it should focus on whole row
    mKeyList->setFocusPolicy(Qt::NoFocus);

    mKeyList->setAlternatingRowColors(true);

    QStringList labels;
    labels << "" << "" << tr("Name") << tr("EMail") << "id" << "fpr";
    mKeyList->setHorizontalHeaderLabels(labels);
    mKeyList->horizontalHeader()->setStretchLastSection(true);

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(mKeyList);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(3);
    setLayout(layout);

    popupMenu = new QMenu(this);
    connect(mCtx, SIGNAL(keyDBChanged()), this, SLOT(refresh()));
    setAcceptDrops(true);
    refresh();
}

void KeyList::refresh()
{
    QStringList *keyList;
    keyList = getChecked();
    // while filling the table, sort enabled causes errors
    mKeyList->setSortingEnabled(false);
    mKeyList->clearContents();

    GpgKeyList keys = mCtx->listKeys();
    mKeyList->setRowCount(keys.size());

    int row = 0;
    GpgKeyList::iterator it = keys.begin();
    while (it != keys.end()) {

        QTableWidgetItem *tmp0 = new QTableWidgetItem();
        tmp0->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        tmp0->setCheckState(Qt::Unchecked);
        mKeyList->setItem(row, 0, tmp0);

        if (it->privkey) {
            QTableWidgetItem *tmp1 = new QTableWidgetItem(QIcon(":kgpg_key2.png"), "");
            mKeyList->setItem(row, 1, tmp1);
        }
        QTableWidgetItem *tmp2 = new QTableWidgetItem(it->name);
        tmp2->setToolTip(it->name);
        mKeyList->setItem(row, 2, tmp2);
        QTableWidgetItem *tmp3 = new QTableWidgetItem(it->email);
        tmp3->setToolTip(it->email);
        // strike out expired keys
        if(it->expired || it->revoked) {
            QFont strike = tmp2->font();
            strike.setStrikeOut(true);
            tmp2->setFont(strike);
            tmp3->setFont(strike);
        }
        mKeyList->setItem(row, 3, tmp3);
        QTableWidgetItem *tmp4 = new QTableWidgetItem(it->id);
        mKeyList->setItem(row, 4, tmp4);
        QTableWidgetItem *tmp5 = new QTableWidgetItem(it->fpr);
        mKeyList->setItem(row, 5, tmp5);


        it++;
        ++row;
    }
    mKeyList->setSortingEnabled(true);
    setChecked(keyList);
}

QStringList *KeyList::getChecked()
{
    QStringList *ret = new QStringList();
    for (int i = 0; i < mKeyList->rowCount(); i++) {
        if (mKeyList->item(i, 0)->checkState() == Qt::Checked) {
            *ret << mKeyList->item(i, 4)->text();
        }
    }
    return ret;
}

QStringList *KeyList::getPrivateChecked()
{
    QStringList *ret = new QStringList();
    for (int i = 0; i < mKeyList->rowCount(); i++) {
        if ((mKeyList->item(i, 0)->checkState() == Qt::Checked) && (mKeyList->item(i, 1))) {
            *ret << mKeyList->item(i, 4)->text();
        }
    }
    return ret;
}

void KeyList::setChecked(QStringList *keyIds)
{
    if (!keyIds->isEmpty()) {
        for (int i = 0; i < mKeyList->rowCount(); i++) {
            if (keyIds->contains(mKeyList->item(i, 4)->text()))  {
                mKeyList->item(i, 0)->setCheckState(Qt::Checked);
            }
        }
    }
}

QStringList *KeyList::getSelected()
{
    QStringList *ret = new QStringList();

    for (int i = 0; i < mKeyList->rowCount(); i++) {
        if (mKeyList->item(i, 0)->isSelected() == 1) {
            *ret << mKeyList->item(i, 4)->text();
        }
    }
    return ret;
}

bool KeyList::containsPrivateKeys()
{
    for (int i = 0; i < mKeyList->rowCount(); i++) {
        if (mKeyList->item(i, 1)) {
            return  true;
        }
    }
    return false;
}

void KeyList::setColumnWidth(int row, int size)
{
    mKeyList->setColumnWidth(row, size);
}

void KeyList::contextMenuEvent(QContextMenuEvent *event)
{
    popupMenu->exec(event->globalPos());
}

void KeyList::addMenuAction(QAction *act)
{
    popupMenu->addAction(act);
}

void KeyList::dropEvent(QDropEvent* event)
{
//    importKeyDialog();
    QSettings settings;

    QDialog *dialog = new QDialog();

    dialog->setWindowTitle(tr("Import Keys"));
    QLabel *label;
    label = new QLabel(tr("You've dropped something on the keylist.\n gpg4usb will now try to import key(s).")+"\n");

    // "always import keys"-CheckBox
    QCheckBox *checkBox = new QCheckBox(tr("Always import without bothering."));
    if (settings.value("general/confirmImportKeys").toBool()) checkBox->setCheckState(Qt::Unchecked);

    // Buttons for ok and cancel
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));

    QVBoxLayout *vbox = new QVBoxLayout();
    vbox->addWidget(label);
    vbox->addWidget(checkBox);
    vbox->addWidget(buttonBox);

    dialog->setLayout(vbox);

    if (settings.value("general/confirmImportKeys",Qt::Checked).toBool())
    {
        dialog->exec();
        if (dialog->result() == QDialog::Rejected) {
            return;
        }
        if (checkBox->isChecked()){
           settings.setValue("general/confirmImportKeys", false);
        } else {
            settings.setValue("general/confirmImportKeys", true);

        }
    }

    if (event->mimeData()->hasUrls())
    {
        foreach (QUrl tmp, event->mimeData()->urls())
        {
                QFile file;
                file.setFileName(tmp.toLocalFile());
                if (!file.open(QIODevice::ReadOnly)) {
                    qDebug() << tr("Couldn't Open File: ") + tmp.toString();
                }
                QByteArray inBuffer = file.readAll();
                this->importKeys(inBuffer);
                file.close();
        }
   } else  {
            QByteArray inBuffer(event->mimeData()->text().toUtf8());
            this->importKeys(inBuffer);
  }
}

void KeyList::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

/** set background color for Keys and put them to top
 *
 */
void KeyList::markKeys(QStringList *keyIds)
{
    foreach(QString id, *keyIds) {
        qDebug() << "marked: " << id;
     }
}

void KeyList::importKeys(QByteArray inBuffer)
{
    GpgImportInformation result = mCtx->importKey(inBuffer);
    new KeyImportDetailDialog(mCtx, result, this);
}