Show
Ignore:
Timestamp:
05/26/08 05:28:02 (8 months ago)
Author:
eugene
Message:

Cleaned up and revised GPG key management classes.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/qt4-gui/src/dialogs/gpgkeymanager.cpp

    r6216 r6219  
    2323 
    2424#include <QDialogButtonBox> 
    25 #include <QGroupBox> 
    26 #include <QHBoxLayout> 
    27 #include <QLabel> 
    2825#include <QMenu> 
    29 #include <QMessageBox> 
    3026#include <QPushButton> 
    3127#include <QVBoxLayout> 
     
    3733 
    3834#include "core/mainwin.h" 
     35#include "core/messagebox.h" 
    3936 
    4037#include "helpers/support.h" 
     
    5552bool compare_luser(const struct luser& left, const struct luser& right) 
    5653{ 
    57   return left.alias < right.alias; 
     54  return QString::compare(left.alias, right.alias, Qt::CaseInsensitive) <= 0; 
    5855} 
    5956 
     
    6764  QVBoxLayout* lay_main = new QVBoxLayout(this); 
    6865 
    69   // GPG Passphrase box 
    70   QGroupBox* box_passphrase = new QGroupBox(tr("GPG Passphrase")); 
    71   lay_main->addWidget( box_passphrase ); 
    72  
    73   QHBoxLayout* lay_passphrase = new QHBoxLayout(box_passphrase); 
    74  
    75   lbl_passphrase = new QLabel(tr("No passphrase set")); 
    76   lay_passphrase->addWidget(lbl_passphrase); 
    77   QPushButton* btn_setPass = new QPushButton(tr("&Set GPG Passphrase")); 
    78   connect(btn_setPass, SIGNAL(clicked()), SLOT(slot_setPassphrase())); 
    79   lay_passphrase->addWidget(btn_setPass); 
    80  
    81   // GPG User/Key list 
    82   QHBoxLayout* lay_hbox = new QHBoxLayout(); 
    83   lay_main->addLayout(lay_hbox); 
    84   QGroupBox* box_keys = new QGroupBox(tr("User Keys")); 
    85   lay_hbox->addWidget(box_keys, 1); 
    86   QVBoxLayout* lay_keys = new QVBoxLayout(box_keys); 
    87  
    88   QVBoxLayout* lay_buttonBox = new QVBoxLayout(); 
    89   lay_hbox->addLayout(lay_buttonBox, 0); 
    90   QPushButton* btn_add = new QPushButton(tr("&Add")); 
    91   QPushButton* btn_edit = new QPushButton(tr("&Edit")); 
    92   QPushButton* btn_remove = new QPushButton(tr("&Remove")); 
    93   connect(btn_add, SIGNAL(clicked()), SLOT(slot_add())); 
    94   connect(btn_edit, SIGNAL(clicked()), SLOT(slot_edit())); 
    95   connect(btn_remove, SIGNAL(clicked()), SLOT(slot_remove())); 
    96   lbl_dragndrop = new QLabel(tr("Drag&Drop user to add to list.")); 
    97   lbl_dragndrop->setWordWrap(true); 
    98  
    99   // create the keylist 
    100   lst_keyList = new KeyList(this); 
     66  lst_keyList = new KeyList(); 
    10167  lst_keyList->setAllColumnsShowFocus(true); 
    10268  QStringList headers; 
     
    10571  connect(lst_keyList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), 
    10672      SLOT(slot_doubleClicked(QTreeWidgetItem*))); 
    107   lay_keys->addWidget(lst_keyList); 
    108  
    109   lay_buttonBox->addWidget(btn_add); 
    110   lay_buttonBox->addWidget(btn_edit); 
    111   lay_buttonBox->addWidget(btn_remove); 
    112   lay_buttonBox->addWidget(lbl_dragndrop); 
    113   lay_buttonBox->addStretch(); 
    114  
    115   // Close 
     73  lay_main->addWidget(lst_keyList); 
     74 
    11675  QDialogButtonBox* buttons = new QDialogButtonBox(); 
    11776  lay_main->addWidget(buttons); 
    11877 
    119   QPushButton* btn_close = buttons->addButton(QDialogButtonBox::Close); 
    120   btn_close->setText(tr("&Close")); 
    121   connect(btn_close, SIGNAL(clicked()), SLOT(slot_close())); 
     78  QPushButton* btn; 
     79#define BUTTON(role, name, slot) \ 
     80  btn = buttons->addButton(name, QDialogButtonBox::role); \ 
     81  connect(btn, SIGNAL(clicked()), SLOT(slot())) 
     82 
     83  BUTTON(ActionRole, tr("&Add"), slot_add); 
     84  BUTTON(ActionRole, tr("&Edit"), slot_edit); 
     85  BUTTON(ActionRole, tr("&Remove"), slot_remove); 
     86 
     87#undef BUTTON 
     88  buttons->addButton(QDialogButtonBox::Close); 
     89  connect(buttons, SIGNAL(rejected()), SLOT(close())); 
    12290 
    12391  initKeyList(); 
     
    12694} 
    12795 
    128 GPGKeyManager::~GPGKeyManager() 
    129 {} 
    130  
    131 void GPGKeyManager::slot_setPassphrase() 
    132 { 
    133   QMessageBox::information( this, tr("Set Passphrase"), tr("Not yet implemented. Use licq_gpg.conf.") ); 
    134 } 
    135  
    136 void GPGKeyManager::slot_close() 
    137 { 
    138   close(); 
    139 } 
    140  
    14196void GPGKeyManager::slot_edit() 
    14297{ 
    143   QTreeWidgetItem* item = lst_keyList->currentItem(); 
    144   if (item ) 
    145     ( (KeyListItem*)item )->edit(); 
     98  slot_doubleClicked(lst_keyList->currentItem()); 
    14699} 
    147100 
    148101void GPGKeyManager::slot_doubleClicked(QTreeWidgetItem* item) 
    149102{ 
    150   if (item ) 
    151     ( (KeyListItem*)item )->edit(); 
     103  if (item != NULL) 
     104    dynamic_cast<KeyListItem*>(item)->edit(); 
    152105} 
    153106 
     
    159112  FOR_EACH_USER_START(LOCK_R) 
    160113  { 
    161     if (strcmp( pUser->GPGKey(), "") == 0) 
     114    if (strcmp(pUser->GPGKey(), "") == 0) 
    162115    { 
    163116      luser tmp; 
     
    182135    return; 
    183136 
    184   editUser(tmp->szId, tmp->nPPID); 
    185 } 
    186  
    187 void GPGKeyManager::editUser(QString id, unsigned long ppid) 
     137  lst_keyList->editUser(tmp->szId, tmp->nPPID); 
     138} 
     139 
     140void GPGKeyManager::slot_remove() 
     141{ 
     142  KeyListItem* item = (KeyListItem*)lst_keyList->currentItem(); 
     143  if (item != NULL) 
     144  { 
     145    if (QueryYesNo(this, tr("Do you want to remove the GPG key binding for the user %1?\n" 
     146            "The key isn't deleted from your keyring.") 
     147          .arg(item->text(0)))) 
     148    { 
     149      item->unsetKey(); 
     150      delete item; 
     151      lst_keyList->resizeColumnsToContents(); 
     152    } 
     153  } 
     154} 
     155 
     156void GPGKeyManager::initKeyList() 
     157{ 
     158  FOR_EACH_USER_START(LOCK_R) 
     159  { 
     160    if (strcmp(pUser->GPGKey(), "") != 0) 
     161    { 
     162      new KeyListItem(lst_keyList, pUser); 
     163    } 
     164  } 
     165  FOR_EACH_USER_END 
     166 
     167  lst_keyList->resizeColumnsToContents(); 
     168} 
     169 
     170// THE KEYLIST 
     171KeyList::KeyList(QWidget* parent) 
     172  : QTreeWidget(parent) 
     173{ 
     174  setAcceptDrops(true); 
     175  setRootIsDecorated(false); 
     176} 
     177 
     178void KeyList::editUser(QString id, unsigned long ppid) 
    188179{ 
    189180  KeyListItem* item = NULL; 
    190181  bool found = false; 
    191182 
    192   for (int i = 0; i < lst_keyList->topLevelItemCount(); ++i) 
    193   { 
    194     item = dynamic_cast<KeyListItem*>(lst_keyList->topLevelItem(i)); 
     183  for (int i = 0; i < topLevelItemCount(); ++i) 
     184  { 
     185    item = dynamic_cast<KeyListItem*>(topLevelItem(i)); 
    195186 
    196187    if (item->getszId() == id && item->getnPPID() == ppid) 
     
    206197    if (u == NULL) 
    207198      return; 
    208     item = new KeyListItem(lst_keyList, u); 
     199    item = new KeyListItem(this, u); 
    209200    gUserManager.DropUser(u); 
     201    resizeColumnsToContents(); 
    210202  } 
    211203 
     
    213205}; 
    214206 
    215 void GPGKeyManager::slot_remove() 
    216 { 
    217   KeyListItem* item = (KeyListItem*)lst_keyList->currentItem(); 
    218   if ( item ) 
    219   { 
    220     if ( QMessageBox::question( this, tr("Remove GPG key"), tr("Do you want to remove the GPG key? The key isn't deleted from your keyring."), QMessageBox::Yes, QMessageBox::No )==QMessageBox::Yes ) 
    221     { 
    222       item->unsetKey(); 
    223       delete item; 
    224     }; 
    225   } 
    226 } 
    227  
    228 void GPGKeyManager::initKeyList() 
    229 { 
    230   FOR_EACH_USER_START(LOCK_R) 
    231   { 
    232     if ( strcmp( pUser->GPGKey(), "" )!= 0 ) 
    233     { 
    234       new KeyListItem( lst_keyList, pUser ); 
    235     } 
    236   } 
    237   FOR_EACH_USER_END 
    238 } 
    239  
    240  
    241  
    242 // THE KEYLIST 
    243 KeyList::KeyList(QWidget* parent) 
    244   : QTreeWidget(parent) 
    245 { 
    246   setAcceptDrops(true); 
    247   setRootIsDecorated(false); 
    248 } 
    249  
    250 KeyList::~KeyList() 
    251 { 
    252 } 
    253  
    254207void KeyList::dragEnterEvent(QDragEnterEvent* event) 
    255208{ 
     
    258211} 
    259212 
    260 void KeyList::dragMoveEvent(QDragMoveEvent* /* event */) 
    261 { 
    262   // Do nothing, just overload function so base class won't interfere 
    263 } 
    264  
    265213void KeyList::dropEvent(QDropEvent* event) 
    266214{ 
    267215  if (!event->mimeData()->hasText()) 
    268216    return; 
     217 
    269218  QString text = event->mimeData()->text(); 
     219 
     220  if (text.length() <= 4) 
     221    return; 
    270222 
    271223  unsigned long nPPID = 0; 
     
    280232  FOR_EACH_PROTO_PLUGIN_END; 
    281233 
    282   if (nPPID == 0 || text.length() <= 4) 
    283     return; 
    284  
    285   QString szId = text.mid(4); 
    286  
    287   GPGKeyManager* km = dynamic_cast<GPGKeyManager*>(nativeParentWidget()); 
    288   if (km != NULL) 
    289     km->editUser(szId, nPPID); 
     234  if (nPPID == 0) 
     235    return; 
     236 
     237  editUser(text.mid(4), nPPID); 
    290238} 
    291239 
     
    310258    setColumnWidth(0, newWidth); 
    311259  } 
     260} 
     261 
     262void KeyList::resizeColumnsToContents() 
     263{ 
     264  for (int i = 0; i < columnCount(); i++) 
     265    resizeColumnToContents(i); 
    312266} 
    313267 
     
    319273    keySelect(NULL) 
    320274{ 
    321   updateText( u ); 
    322 } 
    323  
    324 KeyListItem::~KeyListItem() 
    325 { 
     275  updateText(u); 
    326276} 
    327277 
    328278void KeyListItem::updateText(ICQUser* u) 
    329279{ 
    330   setText( 0, QString::fromUtf8(u->GetAlias()) ); 
    331   setText( 1, u->UseGPG() ? tr("Yes") : tr("No") ); 
    332   setText( 2, u->GPGKey() ); 
     280  setText(0, QString::fromUtf8(u->GetAlias())); 
     281  setText(1, u->UseGPG() ? tr("Yes") : tr("No")); 
     282  setText(2, u->GPGKey()); 
    333283} 
    334284 
    335285void KeyListItem::edit() 
    336286{ 
    337   if ( !keySelect ) 
     287  if (keySelect == NULL) 
    338288  { 
    339289    keySelect = new GPGKeySelect(szId, nPPID); 
     
    347297  keySelect = NULL; 
    348298 
    349   if ( u ) 
    350   { 
    351     if ( strcmp(u->GPGKey(),"")==0 ) 
     299  if (u != NULL) 
     300  { 
     301    if (strcmp(u->GPGKey(), "") == 0) 
    352302      delete this; 
    353303    else 
    354       updateText( u ); 
     304      updateText(u); 
    355305    gUserManager.DropUser(u); 
     306    dynamic_cast<KeyList*>(treeWidget())->resizeColumnsToContents(); 
    356307  } 
    357308}