1/*
2 Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "addtransportdialog.h"
21#include "transport.h"
22#include "transportconfigwidget.h"
23#include "transportmanager.h"
24#include "transporttype.h"
25#include "ui_addtransportdialog.h"
26
27#include <KDebug>
28#include <KGlobal>
29
30#include <akonadi/agentinstance.h>
31#include <akonadi/agentinstancecreatejob.h>
32
33using namespace MailTransport;
34
35/**
36 @internal
37*/
38class AddTransportDialog::Private
39{
40 public:
41 Private( AddTransportDialog *qq )
42 : q( qq )
43 {
44 }
45
46 /**
47 Returns the currently selected type in the type selection widget, or
48 an invalid type if none is selected.
49 */
50 TransportType selectedType() const;
51
52 /**
53 Enables the OK button if a type is selected.
54 */
55 void updateOkButton(); // slot
56 void doubleClicked(); //slot
57 void writeConfig();
58 void readConfig();
59
60 AddTransportDialog *const q;
61 ::Ui::AddTransportDialog ui;
62};
63
64
65void AddTransportDialog::Private::writeConfig()
66{
67 KConfigGroup group( KGlobal::config(), "AddTransportDialog" );
68 group.writeEntry( "Size", q->size() );
69}
70
71void AddTransportDialog::Private::readConfig()
72{
73 KConfigGroup group( KGlobal::config(), "AddTransportDialog" );
74 const QSize sizeDialog = group.readEntry( "Size", QSize(300,200) );
75 if ( sizeDialog.isValid() ) {
76 q->resize( sizeDialog );
77 }
78}
79
80TransportType AddTransportDialog::Private::selectedType() const
81{
82 QList<QTreeWidgetItem*> sel = ui.typeListView->selectedItems();
83 if ( !sel.empty() ) {
84 return sel.first()->data( 0, Qt::UserRole ).value<TransportType>();
85 }
86 return TransportType();
87}
88
89void AddTransportDialog::Private::doubleClicked()
90{
91 if (selectedType().isValid() && !ui.name->text().trimmed().isEmpty()) {
92 q->accept();
93 }
94}
95
96void AddTransportDialog::Private::updateOkButton()
97{
98 // Make sure a type is selected before allowing the user to continue.
99 q->enableButtonOk( selectedType().isValid() && !ui.name->text().trimmed().isEmpty() );
100}
101
102AddTransportDialog::AddTransportDialog( QWidget *parent )
103 : KDialog( parent ), d( new Private( this ) )
104{
105 // Setup UI.
106 {
107 QWidget *widget = new QWidget( this );
108 d->ui.setupUi( widget );
109 setMainWidget( widget );
110 setCaption( i18n( "Create Outgoing Account" ) );
111 setButtons( Ok|Cancel );
112 enableButtonOk( false );
113 setButtonText( Ok, i18nc( "create and configure a mail transport", "Create and Configure" ) );
114
115#ifdef KDEPIM_MOBILE_UI
116 d->ui.descLabel->hide();
117 d->ui.setDefault->hide();
118#endif
119 }
120
121 // Populate type list.
122 foreach ( const TransportType &type, TransportManager::self()->types() ) {
123 QTreeWidgetItem *treeItem = new QTreeWidgetItem( d->ui.typeListView );
124 treeItem->setText( 0, type.name() );
125 treeItem->setText( 1, type.description() );
126 treeItem->setData( 0, Qt::UserRole, QVariant::fromValue( type ) ); // the transport type
127 }
128 d->ui.typeListView->resizeColumnToContents( 0 );
129 updateGeometry();
130 d->ui.typeListView->setFocus();
131
132 // Connect user input.
133 connect( d->ui.typeListView, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
134 this, SLOT(updateOkButton()) );
135 connect( d->ui.typeListView, SIGNAL(itemSelectionChanged()),
136 this, SLOT(updateOkButton()) );
137 connect( d->ui.typeListView, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
138 this, SLOT(doubleClicked()) );
139 connect( d->ui.name, SIGNAL(textChanged(QString)),
140 this, SLOT(updateOkButton()) );
141 d->readConfig();
142}
143
144AddTransportDialog::~AddTransportDialog()
145{
146 d->writeConfig();
147 delete d;
148}
149
150void AddTransportDialog::accept()
151{
152 if ( !d->selectedType().isValid() ) {
153 return;
154 }
155
156 // Create a new transport and configure it.
157 Transport *transport = TransportManager::self()->createTransport();
158 transport->setTransportType( d->selectedType() );
159 if ( d->selectedType().type() == Transport::EnumType::Akonadi ) {
160 // Create a resource instance if Akonadi-type transport.
161 using namespace Akonadi;
162 AgentInstanceCreateJob *cjob = new AgentInstanceCreateJob( d->selectedType().agentType() );
163 if ( !cjob->exec() ) {
164 kWarning() << "Failed to create agent instance of type"
165 << d->selectedType().agentType().identifier();
166 return;
167 }
168 transport->setHost( cjob->instance().identifier() );
169 }
170 transport->setName( d->ui.name->text().trimmed() );
171 transport->forceUniqueName();
172 if ( TransportManager::self()->configureTransport( transport, this ) ) {
173 // The user clicked OK and the transport settings were saved.
174 TransportManager::self()->addTransport( transport );
175#ifndef KDEPIM_MOBILE_UI
176 if ( d->ui.setDefault->isChecked() ) {
177 TransportManager::self()->setDefaultTransport( transport->id() );
178 }
179#endif
180 KDialog::accept();
181 }
182}
183
184#include "moc_addtransportdialog.cpp"
185