mailtransport
addtransportdialog.cpp
00001 /* 00002 Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "addtransportdialog.h" 00021 #include "transport.h" 00022 #include "transportconfigwidget.h" 00023 #include "transportmanager.h" 00024 #include "transporttype.h" 00025 #include "ui_addtransportdialog.h" 00026 00027 #include <KDebug> 00028 00029 #include <akonadi/agentinstance.h> 00030 #include <akonadi/agentinstancecreatejob.h> 00031 00032 using namespace MailTransport; 00033 00037 class AddTransportDialog::Private 00038 { 00039 public: 00040 Private( AddTransportDialog *qq ) 00041 : q( qq ) 00042 { 00043 } 00044 00049 TransportType selectedType() const; 00050 00054 void typeListClicked(); // slot 00055 00056 AddTransportDialog *const q; 00057 ::Ui::AddTransportDialog ui; 00058 }; 00059 00060 TransportType AddTransportDialog::Private::selectedType() const 00061 { 00062 QList<QTreeWidgetItem*> sel = ui.typeListView->selectedItems(); 00063 if ( !sel.empty() ) { 00064 return sel.first()->data( 0, Qt::UserRole ).value<TransportType>(); 00065 } 00066 return TransportType(); 00067 } 00068 00069 void AddTransportDialog::Private::typeListClicked() 00070 { 00071 // Make sure a type is selected before allowing the user to continue. 00072 q->enableButtonOk( selectedType().isValid() ); 00073 } 00074 00075 AddTransportDialog::AddTransportDialog( QWidget *parent ) 00076 : KDialog( parent ), d( new Private( this ) ) 00077 { 00078 // Setup UI. 00079 { 00080 QWidget *widget = new QWidget( this ); 00081 d->ui.setupUi( widget ); 00082 setMainWidget( widget ); 00083 setCaption( i18n( "Create Outgoing Account" ) ); 00084 setButtons( Ok|Cancel ); 00085 enableButtonOk( false ); 00086 setButtonText( Ok, i18nc( "create and configure a mail transport", "Create and Configure" ) ); 00087 00088 #ifdef KDEPIM_MOBILE_UI 00089 d->ui.descLabel->hide(); 00090 d->ui.setDefault->hide(); 00091 #endif 00092 } 00093 00094 // Populate type list. 00095 foreach ( const TransportType &type, TransportManager::self()->types() ) { 00096 QTreeWidgetItem *treeItem = new QTreeWidgetItem( d->ui.typeListView ); 00097 treeItem->setText( 0, type.name() ); 00098 treeItem->setText( 1, type.description() ); 00099 treeItem->setData( 0, Qt::UserRole, QVariant::fromValue( type ) ); // the transport type 00100 } 00101 d->ui.typeListView->resizeColumnToContents( 0 ); 00102 updateGeometry(); 00103 d->ui.typeListView->setFocus(); 00104 00105 // Connect user input. 00106 connect( d->ui.typeListView, SIGNAL(itemClicked(QTreeWidgetItem*,int)), 00107 this, SLOT(typeListClicked()) ); 00108 connect( d->ui.typeListView, SIGNAL(itemSelectionChanged()), 00109 this, SLOT(typeListClicked()) ); 00110 connect( d->ui.typeListView, SIGNAL(doubleClicked(const QModelIndex &) ), 00111 this, SLOT(accept() ) ); 00112 } 00113 00114 AddTransportDialog::~AddTransportDialog() 00115 { 00116 delete d; 00117 } 00118 00119 void AddTransportDialog::accept() 00120 { 00121 if( !d->selectedType().isValid() ) { 00122 return; 00123 } 00124 00125 // Create a new transport and configure it. 00126 Transport *transport = TransportManager::self()->createTransport(); 00127 transport->setTransportType( d->selectedType() ); 00128 if( d->selectedType().type() == Transport::EnumType::Akonadi ) { 00129 // Create a resource instance if Akonadi-type transport. 00130 using namespace Akonadi; 00131 AgentInstanceCreateJob *cjob = new AgentInstanceCreateJob( d->selectedType().agentType() ); 00132 if( !cjob->exec() ) { 00133 kWarning() << "Failed to create agent instance of type" 00134 << d->selectedType().agentType().identifier(); 00135 return; 00136 } 00137 transport->setHost( cjob->instance().identifier() ); 00138 } 00139 transport->setName( d->ui.name->text().trimmed() ); 00140 transport->forceUniqueName(); 00141 if( TransportManager::self()->configureTransport( transport, this ) ) { 00142 // The user clicked OK and the transport settings were saved. 00143 TransportManager::self()->addTransport( transport ); 00144 if( d->ui.setDefault->isChecked() ) { 00145 TransportManager::self()->setDefaultTransport( transport->id() ); 00146 } 00147 KDialog::accept(); 00148 } 00149 } 00150 00151 #include "addtransportdialog.moc"