23 #include "imeditordialog.h" 
   25 #include "imdelegate.h" 
   26 #include "imitemdialog.h" 
   28 #include <QGridLayout> 
   30 #include <QPushButton> 
   33 #include <klocalizedstring.h> 
   34 #include <kmessagebox.h> 
   35 #include <KSharedConfig> 
   37 IMEditorDialog::IMEditorDialog( QWidget *parent )
 
   40   setCaption( i18nc( 
"@title:window", 
"Edit Instant Messaging Addresses" ) );
 
   41   setButtons( Ok | Cancel );
 
   42   setDefaultButton( Ok );
 
   44   QWidget *widget = 
new QWidget( 
this );
 
   45   setMainWidget( widget );
 
   47   QGridLayout *layout = 
new QGridLayout( widget );
 
   49   mAddButton = 
new QPushButton( i18nc( 
"@action:button", 
"Add..." ) );
 
   50   mEditButton = 
new QPushButton( i18nc( 
"@action:button", 
"Edit..." ) );
 
   51   mRemoveButton = 
new QPushButton( i18nc( 
"@action:button", 
"Remove" ) );
 
   52   mStandardButton = 
new QPushButton( i18nc( 
"@action:button", 
"Set as Standard" ) );
 
   54   mView = 
new QTreeView;
 
   55   mView->setRootIsDecorated( 
false );
 
   57   layout->addWidget( mView, 0, 0, 5, 1 );
 
   58   layout->addWidget( mAddButton, 0, 1 );
 
   59   layout->addWidget( mEditButton, 1, 1 );
 
   60   layout->addWidget( mRemoveButton, 2, 1 );
 
   61   layout->addWidget( mStandardButton, 3, 1 );
 
   62   layout->setRowStretch( 4, 1 );
 
   64   connect( mAddButton, SIGNAL(clicked()), SLOT(slotAdd()) );
 
   65   connect( mEditButton, SIGNAL(clicked()), SLOT(slotEdit()) );
 
   66   connect( mRemoveButton, SIGNAL(clicked()), SLOT(slotRemove()) );
 
   67   connect( mStandardButton, SIGNAL(clicked()), SLOT(slotSetStandard()) );
 
   69   mModel = 
new IMModel( 
this );
 
   71   mView->setModel( mModel );
 
   72   mView->setItemDelegate( 
new IMDelegate( 
this ) );
 
   74   connect( mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
 
   75            this, SLOT(slotUpdateButtons()) );
 
   76   connect( mView, SIGNAL(doubleClicked(QModelIndex)),
 
   77            this, SLOT(slotEdit()) );
 
   82 IMEditorDialog::~IMEditorDialog()
 
   87 void IMEditorDialog::readConfig()
 
   89     KConfigGroup group( KGlobal::config(), 
"IMEditorDialog" );
 
   90     const QSize sizeDialog = group.readEntry( 
"Size", QSize(400,200) );
 
   91     if ( sizeDialog.isValid() ) {
 
   96 void IMEditorDialog::writeConfig()
 
   98     KConfigGroup group( KGlobal::config(), 
"IMEditorDialog" );
 
   99     group.writeEntry( 
"Size", size() );
 
  102 void IMEditorDialog::setAddresses( 
const IMAddress::List &addresses )
 
  104   mModel->setAddresses( addresses );
 
  107 IMAddress::List IMEditorDialog::addresses()
 const 
  109   return mModel->addresses();
 
  112 void IMEditorDialog::slotAdd()
 
  114   QPointer<IMItemDialog> d( 
new IMItemDialog( 
this ) );
 
  115   d->setCaption( i18nc( 
"@title:window", 
"Add IM Address" ) );
 
  116   if ( d->exec() == QDialog::Accepted && d != 0 ) {
 
  117     IMAddress newAddress = d->address();
 
  118     int addedRow = mModel->rowCount();
 
  119     mModel->insertRow( addedRow );
 
  121     mModel->setData( mModel->index( addedRow, 0 ), newAddress.protocol(), IMModel::ProtocolRole );
 
  122     mModel->setData( mModel->index( addedRow, 1 ), newAddress.name(), Qt::EditRole );
 
  127 void IMEditorDialog::slotEdit()
 
  129   const int currentRow = mView->currentIndex().row();
 
  130   if ( currentRow < 0 ) {
 
  134   QPointer<IMItemDialog> d( 
new IMItemDialog( 
this ) );
 
  135   d->setCaption( i18nc( 
"@title:window", 
"Edit IM Address" ) );
 
  136   d->setAddress( mModel->addresses().at( currentRow ) );
 
  138   if ( d->exec() == QDialog::Accepted && d != 0 ) {
 
  139     IMAddress editedAddress = d->address();
 
  140     mModel->setData( mModel->index( currentRow, 0 ), editedAddress.protocol(),
 
  141                      IMModel::ProtocolRole );
 
  142     mModel->setData( mModel->index( currentRow, 1 ), editedAddress.name(),
 
  148 void IMEditorDialog::slotRemove()
 
  150   const int currentRow = mView->currentIndex().row();
 
  151   if ( currentRow < 0 ) {
 
  155   if ( KMessageBox::warningContinueCancel(
 
  157          i18nc( 
"@info Instant messaging",
 
  158                 "Do you really want to delete the selected <resource>%1</resource> address?",
 
  159                 mModel->data( mModel->index( currentRow, 0 ), Qt::DisplayRole ).toString() ),
 
  160          i18nc( 
"@title:window", 
"Confirm Delete Resource" ),
 
  161          KStandardGuiItem::del() ) != KMessageBox::Continue ) {
 
  165   mModel->removeRow( currentRow );
 
  168 void IMEditorDialog::slotSetStandard()
 
  170   const int currentRow = mView->currentIndex().row();
 
  171   if ( currentRow < 0 ) {
 
  176   for ( 
int i = 0; i < mModel->rowCount(); ++i ) {
 
  177     const QModelIndex index = mModel->index( i, 0 );
 
  178     mModel->setData( index, ( index.row() == currentRow ), IMModel::IsPreferredRole );
 
  182 void IMEditorDialog::slotUpdateButtons()
 
  184   const QModelIndex currentIndex = mView->currentIndex();
 
  186   mRemoveButton->setEnabled( currentIndex.isValid() );
 
  187   mEditButton->setEnabled( currentIndex.isValid() );
 
  189   mStandardButton->setEnabled( currentIndex.isValid() &&
 
  190                                !mModel->data( currentIndex, IMModel::IsPreferredRole ).toBool() );