23 #include "imeditordialog.h"
25 #include "imdelegate.h"
26 #include "imitemdialog.h"
28 #include <QtCore/QStringList>
29 #include <QGridLayout>
30 #include <QPushButton>
34 #include <kmessagebox.h>
36 IMEditorDialog::IMEditorDialog( QWidget *parent )
39 setCaption( i18nc(
"@title:window",
"Edit Instant Messaging Addresses" ) );
40 setButtons( Ok | Cancel );
41 setDefaultButton( Ok );
43 QWidget *widget =
new QWidget(
this );
44 setMainWidget( widget );
46 QGridLayout *layout =
new QGridLayout( widget );
48 mAddButton =
new QPushButton( i18nc(
"@action:button",
"Add..." ) );
49 mEditButton =
new QPushButton( i18nc(
"@action:button",
"Edit..." ) );
50 mRemoveButton =
new QPushButton( i18nc(
"@action:button",
"Remove" ) );
51 mStandardButton =
new QPushButton( i18nc(
"@action:button",
"Set as Standard" ) );
53 mView =
new QTreeView;
54 mView->setRootIsDecorated(
false );
56 layout->addWidget( mView, 0, 0, 5, 1 );
57 layout->addWidget( mAddButton, 0, 1 );
58 layout->addWidget( mEditButton, 1, 1 );
59 layout->addWidget( mRemoveButton, 2, 1 );
60 layout->addWidget( mStandardButton, 3, 1 );
61 layout->setRowStretch( 4, 1 );
63 connect( mAddButton, SIGNAL(clicked()), SLOT(slotAdd()) );
64 connect( mEditButton, SIGNAL(clicked()), SLOT(slotEdit()) );
65 connect( mRemoveButton, SIGNAL(clicked()), SLOT(slotRemove()) );
66 connect( mStandardButton, SIGNAL(clicked()), SLOT(slotSetStandard()) );
68 mModel =
new IMModel(
this );
70 mView->setModel( mModel );
71 mView->setItemDelegate(
new IMDelegate(
this ) );
73 connect( mView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
74 this, SLOT(slotUpdateButtons()) );
75 connect( mView, SIGNAL(doubleClicked(QModelIndex)),
76 this, SLOT(slotEdit()) );
80 void IMEditorDialog::setAddresses(
const IMAddress::List &addresses )
82 mModel->setAddresses( addresses );
85 IMAddress::List IMEditorDialog::addresses()
const
87 return mModel->addresses();
90 void IMEditorDialog::slotAdd()
92 IMItemDialog d(
this );
93 d.setCaption( i18nc(
"@title:window",
"Add IM Address" ) );
95 IMAddress newAddress = d.address();
96 int addedRow = mModel->rowCount();
97 mModel->insertRow( addedRow );
99 mModel->setData( mModel->index( addedRow, 0 ), newAddress.protocol(), IMModel::ProtocolRole );
100 mModel->setData( mModel->index( addedRow, 1 ), newAddress.name(), Qt::EditRole );
104 void IMEditorDialog::slotEdit()
106 const int currentRow = mView->currentIndex().row();
107 if ( currentRow < 0 ) {
111 IMItemDialog d(
this );
112 d.setCaption( i18nc(
"@title:window",
"Edit IM Address" ) );
113 d.setAddress( mModel->addresses().at( currentRow ) );
116 IMAddress editedAddress = d.address();
117 mModel->setData( mModel->index( currentRow, 0 ), editedAddress.protocol(),
118 IMModel::ProtocolRole );
119 mModel->setData( mModel->index( currentRow, 1 ), editedAddress.name(),
124 void IMEditorDialog::slotRemove()
126 const int currentRow = mView->currentIndex().row();
127 if ( currentRow < 0 ) {
131 if ( KMessageBox::warningContinueCancel(
133 i18nc(
"@info Instant messaging",
134 "Do you really want to delete the selected <resource>%1</resource> address?",
135 mModel->data( mModel->index( currentRow, 0 ), Qt::DisplayRole ).toString() ),
136 i18nc(
"@title:window",
"Confirm Delete Resource" ),
137 KStandardGuiItem::del() ) != KMessageBox::Continue ) {
141 mModel->removeRow( currentRow );
144 void IMEditorDialog::slotSetStandard()
146 const int currentRow = mView->currentIndex().row();
147 if ( currentRow < 0 ) {
152 for (
int i = 0; i < mModel->rowCount(); ++i ) {
153 const QModelIndex index = mModel->index( i, 0 );
154 mModel->setData( index, ( index.row() == currentRow ), IMModel::IsPreferredRole );
158 void IMEditorDialog::slotUpdateButtons()
160 const QModelIndex currentIndex = mView->currentIndex();
162 mRemoveButton->setEnabled( currentIndex.isValid() );
163 mEditButton->setEnabled( currentIndex.isValid() );
165 mStandardButton->setEnabled( currentIndex.isValid() &&
166 !mModel->data( currentIndex, IMModel::IsPreferredRole ).toBool() );