root / modules / encryption / keys_manager.cpp @ 18636b4a
History | View | Annotate | Download (8.1 kB)
| 1 | /***************************************************************************
|
|---|---|
| 2 | * * |
| 3 | * This program is free software; you can redistribute it and/or modify * |
| 4 | * it under the terms of the GNU General Public License as published by * |
| 5 | * the Free Software Foundation; either version 2 of the License, or * |
| 6 | * (at your option) any later version. * |
| 7 | * * |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | #include <QtGui/QGroupBox> |
| 11 | #include <QtGui/QHBoxLayout> |
| 12 | #include <QtGui/QLabel> |
| 13 | #include <QtGui/QTextEdit> |
| 14 | #include <QTreeWidget> |
| 15 | #include <QtGui/QVBoxLayout> |
| 16 | |
| 17 | #include "config_file.h" |
| 18 | #include "debug.h" |
| 19 | #include "../modules/gadu_protocol/gadu.h" |
| 20 | #include "userlist.h" |
| 21 | #include "icons_manager.h" |
| 22 | #include "encryption.h" |
| 23 | #include "misc/misc.h" |
| 24 | #include "message_box.h" |
| 25 | |
| 26 | #include "keys_manager.h" |
| 27 | |
| 28 | KeysManager::KeysManager(QDialog *parent) |
| 29 | : QWidget(parent), |
| 30 | lv_keys(0), e_key(0), pb_del(0), pb_on(0) |
| 31 | {
|
| 32 | kdebugf(); |
| 33 | |
| 34 | setWindowTitle(tr("Manage keys"));
|
| 35 | setAttribute(Qt::WA_DeleteOnClose); |
| 36 | |
| 37 | // create main QLabel widgets (icon and app info)
|
| 38 | |
| 39 | QWidget *left = new QWidget();
|
| 40 | |
| 41 | QLabel *l_icon = new QLabel;
|
| 42 | l_icon->setPixmap(icons_manager->loadPixmap("ManageKeysWindowIcon"));
|
| 43 | |
| 44 | QWidget *blank = new QWidget;
|
| 45 | blank->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding)); |
| 46 | |
| 47 | QVBoxLayout *left_layout = new QVBoxLayout;
|
| 48 | left_layout->addWidget(l_icon); |
| 49 | left_layout->addWidget(blank); |
| 50 | left->setLayout(left_layout); |
| 51 | |
| 52 | QWidget *center = new QWidget;
|
| 53 | |
| 54 | QLabel *l_info = new QLabel();
|
| 55 | |
| 56 | l_info->setText(tr("This dialog box allows you to manage your keys."));
|
| 57 | l_info->setWordWrap(true);
|
| 58 | l_info->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum)); |
| 59 | |
| 60 | // end create main QLabel widgets (icon and app info)
|
| 61 | |
| 62 | |
| 63 | // our QTreeWidget
|
| 64 | lv_keys = new QTreeWidget(center);
|
| 65 | |
| 66 | QStringList headers; |
| 67 | headers << tr("Contact") << tr("Uin") << tr("Encryption is on"); |
| 68 | |
| 69 | lv_keys->setHeaderLabels(headers); |
| 70 | lv_keys->setAllColumnsShowFocus(true);
|
| 71 | lv_keys->setIndentation(false);
|
| 72 | // end our QTreeWidget
|
| 73 | |
| 74 | // our QGroupBox
|
| 75 | QGroupBox *vgb_key = new QGroupBox(tr("Key"), center); |
| 76 | QVBoxLayout *keyLayout = new QVBoxLayout(vgb_key);
|
| 77 | // end our QVroupBox
|
| 78 | |
| 79 | // our QTextEdit
|
| 80 | e_key = new QTextEdit(vgb_key);
|
| 81 | e_key->setReadOnly(true);
|
| 82 | keyLayout->addWidget(e_key); |
| 83 | // e_key->setTextFormat(Qt::LogText);
|
| 84 | // end our QTextEdit
|
| 85 | |
| 86 | // buttons
|
| 87 | QWidget *bottom = new QWidget;
|
| 88 | |
| 89 | QWidget *blank2 = new QWidget;
|
| 90 | blank2->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum)); |
| 91 | |
| 92 | pb_del = new QPushButton(icons_manager->loadIcon("DeleteKeyButton"), tr("Delete"), bottom); |
| 93 | pb_del->setEnabled(false);
|
| 94 | pb_on = new QPushButton(icons_manager->loadIcon("DecryptedChat"), tr("On"), bottom); |
| 95 | pb_on->setEnabled(false);
|
| 96 | QPushButton *pb_close = new QPushButton(icons_manager->loadIcon("CloseWindow"), tr("&Close"), bottom, "close"); |
| 97 | |
| 98 | QHBoxLayout *bottom_layout = new QHBoxLayout(bottom);
|
| 99 | bottom_layout->addWidget(blank2); |
| 100 | bottom_layout->addWidget(pb_del); |
| 101 | bottom_layout->addWidget(pb_on); |
| 102 | bottom_layout->addWidget(pb_close); |
| 103 | // end buttons
|
| 104 | |
| 105 | QVBoxLayout *center_layout = new QVBoxLayout(center);
|
| 106 | center_layout->addWidget(l_info); |
| 107 | center_layout->addWidget(lv_keys); |
| 108 | center_layout->addWidget(vgb_key); |
| 109 | center_layout->addWidget(bottom); |
| 110 | |
| 111 | QHBoxLayout *layout = new QHBoxLayout(this); |
| 112 | layout->addWidget(left); |
| 113 | layout->addWidget(center); |
| 114 | |
| 115 | connect(pb_del, SIGNAL(clicked()), this, SLOT(removeKey()));
|
| 116 | connect(pb_on, SIGNAL(clicked()), this, SLOT(turnEncryption()));
|
| 117 | connect(pb_close, SIGNAL(clicked()), this, SLOT(close()));
|
| 118 | |
| 119 | connect(lv_keys, SIGNAL(itemSelectionChanged()), this, SLOT(selectionChanged()));
|
| 120 | connect(lv_keys, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)), this, SLOT(turnEncryption(QTreeWidgetItem *))); |
| 121 | |
| 122 | // refreshing (loading) QTreeWidget
|
| 123 | refreshKeysList(); |
| 124 | |
| 125 | loadWindowGeometry(this, "General", "KeysManagerDialogGeometry", 0, 0, 680, 460); |
| 126 | kdebugf2(); |
| 127 | } |
| 128 | |
| 129 | KeysManager::~KeysManager() |
| 130 | {
|
| 131 | kdebugf(); |
| 132 | saveWindowGeometry(this, "General", "KeysManagerDialogGeometry"); |
| 133 | delete lv_keys;
|
| 134 | delete e_key;
|
| 135 | delete pb_del;
|
| 136 | kdebugf2(); |
| 137 | } |
| 138 | |
| 139 | void KeysManager::keyPressEvent(QKeyEvent *ke_event)
|
| 140 | {
|
| 141 | if (ke_event->key() == Qt::Key_Escape)
|
| 142 | close(); |
| 143 | } |
| 144 | |
| 145 | QTreeWidgetItem * KeysManager::getSelected() |
| 146 | {
|
| 147 | if (lv_keys->selectedItems().count())
|
| 148 | return lv_keys->selectedItems()[0]; |
| 149 | else
|
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | void KeysManager::getKeysList(QStringList &uins)
|
| 154 | {
|
| 155 | kdebugf(); |
| 156 | |
| 157 | QDir dir(ggPath("keys/"), "*.pem", QDir::Name, QDir::Files); |
| 158 | QStringList list = dir.entryList(); |
| 159 | QFile file; |
| 160 | |
| 161 | QString temp; |
| 162 | QString GGUIN = QString::number(config_file.readNumEntry("General", "UIN")); |
| 163 | |
| 164 | foreach(const QString &fileName, list)
|
| 165 | {
|
| 166 | file.setName(ggPath("keys/").append(fileName));
|
| 167 | if (fileName != QString("private.pem") && fileName != GGUIN + QString(".pem") && file.open(QIODevice::ReadOnly)) |
| 168 | {
|
| 169 | temp = fileName; |
| 170 | uins << (temp.remove(QRegExp(".pem$")));
|
| 171 | file.close(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void KeysManager::turnContactEncryptionText(QString id, bool on) |
| 177 | {
|
| 178 | QList <QTreeWidgetItem *> items = lv_keys->findItems(id, Qt::MatchExactly, 1);
|
| 179 | if (items.count())
|
| 180 | {
|
| 181 | items[0]->setText(2, bool2text(on)); |
| 182 | if (getSelected() == items[0]) |
| 183 | turnEncryptionBtn(on); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void KeysManager::turnEncryption()
|
| 188 | {
|
| 189 | turnEncryption(getSelected()); |
| 190 | } |
| 191 | |
| 192 | void KeysManager::turnEncryption(QTreeWidgetItem *item)
|
| 193 | {
|
| 194 | bool on = (item->text(2) == tr("Yes") ? false : true); |
| 195 | |
| 196 | if (getSelected() == item)
|
| 197 | turnEncryptionBtn(on); |
| 198 | |
| 199 | UserListElements ules(userlist->byID("Gadu", item->text(1))); |
| 200 | UserGroup group(ules); |
| 201 | emit turnEncryption(&group, on); |
| 202 | } |
| 203 | |
| 204 | void KeysManager::turnEncryptionBtn(bool on) |
| 205 | {
|
| 206 | if (on)
|
| 207 | {
|
| 208 | pb_on->setText(tr("Off"));
|
| 209 | pb_on->setIcon(icons_manager->loadIcon("DecryptedChat_off"));
|
| 210 | } |
| 211 | else
|
| 212 | {
|
| 213 | pb_on->setText(tr("On"));
|
| 214 | pb_on->setIcon(icons_manager->loadIcon("DecryptedChat"));
|
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void KeysManager::refreshKeysList()
|
| 219 | {
|
| 220 | // begin filling QListView
|
| 221 | QStringList uins; |
| 222 | getKeysList(uins); |
| 223 | |
| 224 | UserListElement ule; |
| 225 | bool ok;
|
| 226 | bool encrypt;
|
| 227 | |
| 228 | // clearing list
|
| 229 | lv_keys->clear(); |
| 230 | |
| 231 | foreach(const QString &strUin, uins)
|
| 232 | {
|
| 233 | UinType uin = strUin.toUInt(&ok); |
| 234 | if (ok)
|
| 235 | {
|
| 236 | ule = userlist->byID("Gadu", QString::number(uin));
|
| 237 | if (ule.data("EncryptionEnabled").isValid()) |
| 238 | encrypt = ule.data("EncryptionEnabled").toString() == "true"; |
| 239 | else
|
| 240 | encrypt = config_file.readBoolEntry("Chat", "Encryption"); |
| 241 | |
| 242 | QStringList strings; |
| 243 | strings << (userlist->contains(ule) ? ule.altNick() : QString::null) << ule.ID("Gadu") << bool2text(encrypt);
|
| 244 | |
| 245 | new QTreeWidgetItem(lv_keys, strings);
|
| 246 | } |
| 247 | } |
| 248 | // end filling QListView
|
| 249 | |
| 250 | // forcing selectionChanged() becouse QListView doesn't emit signal
|
| 251 | selectionChanged(); |
| 252 | } |
| 253 | |
| 254 | void KeysManager::removeKey()
|
| 255 | {
|
| 256 | kdebugf(); |
| 257 | if (MessageBox::ask(tr("Are you sure you want to delete the selected key?"))) |
| 258 | {
|
| 259 | QString id = getSelected()->text(1);
|
| 260 | QString path = ggPath("keys/")+id+QString(".pem"); |
| 261 | QFile *file = new QFile(path);
|
| 262 | if (file->remove())
|
| 263 | {
|
| 264 | refreshKeysList(); |
| 265 | emit keyRemoved(userlist->byID("Gadu", id));
|
| 266 | } |
| 267 | else
|
| 268 | MessageBox::msg(tr("Cannot remove key\nCheck if you have access to file \"%1\"").arg(path));
|
| 269 | |
| 270 | delete file;
|
| 271 | } |
| 272 | kdebugf2(); |
| 273 | } |
| 274 | |
| 275 | void KeysManager::selectionChanged()
|
| 276 | {
|
| 277 | e_key->clear(); |
| 278 | if (lv_keys->selectedItems().count())
|
| 279 | {
|
| 280 | pb_del->setEnabled(true);
|
| 281 | pb_on->setEnabled(true);
|
| 282 | turnEncryptionBtn(lv_keys->selectedItems()[0]->text(2) == tr("Yes")); |
| 283 | getKeyInfo(); |
| 284 | } |
| 285 | // else if (lv_keys->childCount() > 0)
|
| 286 | // lv_keys->setSelected(lv_keys->firstChild(), true);
|
| 287 | else
|
| 288 | {
|
| 289 | pb_del->setEnabled(false);
|
| 290 | pb_on->setEnabled(false);
|
| 291 | } |
| 292 | } |
| 293 | |
| 294 | void KeysManager::getKeyInfo()
|
| 295 | {
|
| 296 | QFile *file = new QFile(ggPath("keys/") + (lv_keys->selectedItems()[0]->text(1)) + QString(".pem")); |
| 297 | if (file->open(QIODevice::ReadOnly))
|
| 298 | {
|
| 299 | e_key->append(file->readAll()); |
| 300 | file->close(); |
| 301 | } |
| 302 | delete file;
|
| 303 | } |
| 304 | |
| 305 | QString KeysManager::bool2text(bool on)
|
| 306 | {
|
| 307 | return (on ? tr("Yes") : tr("No")); |
| 308 | } |