mailtransport
transportmanagementwidget.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "transportmanagementwidget.h"
00024 #include "ui_transportmanagementwidget.h"
00025 #include "transportmanager.h"
00026 #include "transport.h"
00027 #include "transportconfigdialog.h"
00028
00029 using namespace MailTransport;
00030
00031 class TransportManagementWidget::Private
00032 {
00033 public:
00034 Ui::TransportManagementWidget ui;
00035 };
00036
00037 TransportManagementWidget::TransportManagementWidget( QWidget *parent )
00038 : QWidget( parent ), d( new Private )
00039 {
00040 KGlobal::locale()->insertCatalog(QString::fromLatin1("libmailtransport"));
00041 d->ui.setupUi( this );
00042
00043 d->ui.transportList->setHeaderLabels( QStringList()
00044 << i18nc( "@title:column email transport name", "Name" )
00045 << i18nc( "@title:column email transport type", "Type" ) );
00046 d->ui.transportList->sortItems( 0, Qt::AscendingOrder );
00047 connect( d->ui.transportList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
00048 SLOT(updateButtonState()) );
00049 connect( d->ui.transportList, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
00050 SLOT(editClicked()) );
00051 connect( d->ui.addButton, SIGNAL(clicked()), SLOT(addClicked()) );
00052 connect( d->ui.editButton, SIGNAL(clicked()), SLOT(editClicked()) );
00053 connect( d->ui.removeButton, SIGNAL(clicked()), SLOT(removeClicked()) );
00054 connect( d->ui.defaultButton, SIGNAL(clicked()), SLOT(defaultClicked()) );
00055
00056 fillTransportList();
00057 connect( TransportManager::self(), SIGNAL(transportsChanged()),
00058 SLOT(fillTransportList()) );
00059 }
00060
00061 TransportManagementWidget::~TransportManagementWidget()
00062 {
00063 delete d;
00064 }
00065
00066 void TransportManagementWidget::fillTransportList()
00067 {
00068
00069 int selected = -1;
00070 if ( d->ui.transportList->currentItem() ) {
00071 selected = d->ui.transportList->currentItem()->data( 0, Qt::UserRole ).toInt();
00072 }
00073
00074 d->ui.transportList->clear();
00075 foreach ( Transport *t, TransportManager::self()->transports() ) {
00076 QTreeWidgetItem *item = new QTreeWidgetItem( d->ui.transportList );
00077 item->setData( 0, Qt::UserRole, t->id() );
00078 item->setText( 0, t->name() );
00079 QString type;
00080 switch ( t->type() ) {
00081 case Transport::EnumType::SMTP:
00082 type = i18nc( "@option SMTP transport", "SMTP" );
00083 break;
00084 case Transport::EnumType::Sendmail:
00085 type = i18nc( "@option sendmail transport", "Sendmail" );
00086 break;
00087 }
00088 if ( TransportManager::self()->defaultTransportId() == t->id() ) {
00089 type += i18nc( "@label the default mail transport", " (Default)" );
00090 }
00091 item->setText( 1, type );
00092 if ( t->id() == selected ) {
00093 d->ui.transportList->setCurrentItem( item );
00094 }
00095 }
00096
00097 updateButtonState();
00098 }
00099
00100 void TransportManagementWidget::updateButtonState()
00101 {
00102 if ( !d->ui.transportList->currentItem() ) {
00103 d->ui.editButton->setEnabled( false );
00104 d->ui.removeButton->setEnabled( false );
00105 d->ui.defaultButton->setEnabled( false );
00106 } else {
00107 d->ui.editButton->setEnabled( true );
00108 d->ui.removeButton->setEnabled( true );
00109 if ( d->ui.transportList->currentItem()->data( 0, Qt::UserRole ) ==
00110 TransportManager::self()->defaultTransportId() ) {
00111 d->ui.defaultButton->setEnabled( false );
00112 } else {
00113 d->ui.defaultButton->setEnabled( true );
00114 }
00115 }
00116 }
00117
00118 void TransportManagementWidget::addClicked()
00119 {
00120
00121 Transport *t = TransportManager::self()->createTransport();
00122 t->setType( Transport::EnumType::SMTP );
00123
00124
00125 TransportConfigDialog *tcd = new TransportConfigDialog( t, this );
00126 connect( tcd, SIGNAL(sendmailClicked()), SLOT(slotSendmail()) );
00127 tcd->setCaption( i18nc( "@title:window", "Add Transport" ) );
00128 if ( tcd->exec() == KDialog::Accepted ) {
00129 TransportManager::self()->addTransport( t );
00130 } else {
00131 delete t;
00132 }
00133 }
00134
00135 void TransportManagementWidget::slotSendmail()
00136 {
00137
00138 Transport *t = TransportManager::self()->createTransport();
00139 t->setType( Transport::EnumType::Sendmail );
00140 t->setHost( QLatin1String( "/usr/sbin/sendmail" ) );
00141
00142 TransportConfigDialog tcd( t, this );
00143 tcd.setCaption( i18nc( "@title:window", "Add Transport" ) );
00144 if ( tcd.exec() == KDialog::Accepted ) {
00145 TransportManager::self()->addTransport( t );
00146 } else {
00147 delete t;
00148 }
00149 }
00150
00151 void TransportManagementWidget::editClicked()
00152 {
00153 Q_ASSERT( d->ui.transportList->currentItem() );
00154
00155 int currentId = d->ui.transportList->currentItem()->data( 0, Qt::UserRole ).toInt();
00156 Transport *transport = TransportManager::self()->transportById( currentId );
00157 if ( !transport ) {
00158 return;
00159 }
00160 transport = transport->clone();
00161 TransportConfigDialog t( transport, this );
00162 t.setCaption( i18nc( "@title:window", "Modify Transport" ) );
00163 t.exec();
00164 delete transport;
00165 }
00166
00167 void TransportManagementWidget::removeClicked()
00168 {
00169 Q_ASSERT( d->ui.transportList->currentItem() );
00170
00171 TransportManager::self()->removeTransport(
00172 d->ui.transportList->currentItem()->data( 0, Qt::UserRole ).toInt() );
00173 }
00174
00175 void TransportManagementWidget::defaultClicked()
00176 {
00177 Q_ASSERT( d->ui.transportList->currentItem() );
00178
00179 TransportManager::self()->setDefaultTransport(
00180 d->ui.transportList->currentItem()->data( 0, Qt::UserRole ).toInt() );
00181 }
00182
00183 #include "transportmanagementwidget.moc"