2 * telepathyeditaccountwidget.h - UI to edit Telepathy account settings
4 * Copyright (c) 2006 by Michaƫl Larouche <larouche@kde.org>
6 * Kopete (c) 2002-2006 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 *************************************************************************
17 #include "telepathyeditaccountwidget.h"
18 #include "ui_telepathyeditaccountwidget.h"
21 #include <QtCore/QTimer>
22 #include <QtCore/QLatin1String>
23 #include <QtCore/QStringList>
24 #include <QtCore/QPointer>
25 #include <QtCore/QMap>
26 #include <QtGui/QTreeWidgetItem>
29 #include <kmessagebox.h>
32 #include <kconfiggroup.h>
37 #include <QtTapioca/ConnectionManagerFactory>
38 #include <QtTapioca/ConnectionManager>
41 #include <kopeteaccount.h>
44 #include "telepathyeditparameterwidget.h"
45 #include "telepathyprotocol.h"
46 #include "telepathyaccount.h"
48 using namespace QtTapioca
;
50 class ConnectionManagerItem
: public QTreeWidgetItem
53 ConnectionManagerItem(ConnectionManager
*connectionManager
, QTreeWidget
*parent
)
54 : QTreeWidgetItem(parent
)
56 m_connectionManager
= connectionManager
;
58 setText(0, connectionManager
->name());
59 setText(1, QString::number(connectionManager
->supportedProtocols().size()));
62 ConnectionManager
*connectionManager()
64 return m_connectionManager
;
68 QPointer
<ConnectionManager
> m_connectionManager
;
71 class TelepathyEditAccountWidget::Private
78 Ui::TelepathyEditAccountWidget ui
;
79 TelepathyEditParameterWidget
*paramWidget
;
80 QList
<ConnectionManager::Parameter
> savedParameterList
;
83 // TODO: Required flags for parameters.
84 TelepathyEditAccountWidget::TelepathyEditAccountWidget(Kopete::Account
*account
, QWidget
*parent
)
85 : QWidget(parent
), KopeteEditAccountWidget(account
), d(new Private
)
89 // Setup signal/slot connection
90 connect(d
->ui
.treeConnectionManager
, SIGNAL(itemSelectionChanged()), this, SLOT(connectionManagerSelectionChanged()));
91 connect(d
->ui
.treeProtocol
, SIGNAL(itemSelectionChanged()), this, SLOT(protocolSelectionChanged()));
93 // List connection manager after the constructor.
94 QTimer::singleShot(0, this, SLOT(listConnectionManager()));
97 TelepathyEditAccountWidget::~TelepathyEditAccountWidget()
99 kDebug(TELEPATHY_DEBUG_AREA
) ;
103 TelepathyAccount
*TelepathyEditAccountWidget::account()
105 return static_cast<TelepathyAccount
*>( KopeteEditAccountWidget::account() );
108 bool TelepathyEditAccountWidget::validateData()
110 kDebug(TELEPATHY_DEBUG_AREA
) ;
111 // You must fill the form to move to the next step
112 if( !d
->ui
.treeConnectionManager
->selectedItems().isEmpty() &&
113 !d
->ui
.treeProtocol
->selectedItems().isEmpty() )
117 KMessageBox::error(this, i18n("Please fill in the fields in the dialog. First select a connection manager, then select a protocol."));
122 Kopete::Account
*TelepathyEditAccountWidget::apply()
124 // Get parameter list
127 kDebug(TELEPATHY_DEBUG_AREA
) ;
128 d
->savedParameterList
= d
->paramWidget
->parameterList();
132 QString newAccountId
;
133 // Look for a parameter that begin with "account"
134 foreach(ConnectionManager::Parameter parameter
, d
->savedParameterList
)
136 if( parameter
.name().startsWith( QLatin1String("account") ) )
138 newAccountId
= parameter
.value().toString();
139 kDebug(TELEPATHY_DEBUG_AREA
) << "Found account id: " << newAccountId
;
144 setAccount( TelepathyProtocol::protocol()->createNewAccount(newAccountId
) );
153 void TelepathyEditAccountWidget::readConfig()
155 kDebug(TELEPATHY_DEBUG_AREA
) ;
156 // Restore config not related to ConnectionManager parameters first
157 // so that the UI for the protocol parameters will be generated
158 if( account()->readConfig() )
160 QString readConnectionManager
= account()->connectionManager();
161 QString readProtocol
= account()->connectionProtocol();
163 // Do nothing if the config return empty values
164 if( !readConnectionManager
.isEmpty() && !readProtocol
.isEmpty() )
166 // Look for the connection manager in the tree widget
167 QList
<QTreeWidgetItem
*> availableConnectionManager
= d
->ui
.treeConnectionManager
->findItems( readConnectionManager
, Qt::MatchStartsWith
);
168 if( !availableConnectionManager
.isEmpty() )
170 // Select the connection manager to generate the protocol list
171 d
->ui
.treeConnectionManager
->setCurrentItem( availableConnectionManager
.first() );
173 // At this point, the protocol tree widget is filled
175 // Look for the protocol in the tree widget
176 QList
<QTreeWidgetItem
*> availableProtocol
= d
->ui
.treeProtocol
->findItems( readProtocol
, Qt::MatchStartsWith
);
177 if( !availableProtocol
.isEmpty() )
179 d
->ui
.treeProtocol
->setCurrentItem( availableProtocol
.first() );
181 // At this point, the protocol preferences tab is created
183 // Update the parameters in the UI
184 d
->paramWidget
->setParameterList( account()->allConnectionParameters() );
191 void TelepathyEditAccountWidget::writeConfig()
193 kDebug(TELEPATHY_DEBUG_AREA
) ;
194 QString selectedConnectionManager
= d
->ui
.treeConnectionManager
->selectedItems().first()->text(0);
195 QString selectedProtocol
= d
->ui
.treeProtocol
->selectedItems().first()->text(0);
196 QString accountId
= account()->accountId();
198 KMessageBox::information(this, i18n("ConnectionManager: %1\nProtocol: %2\nAccount: %3",selectedConnectionManager
,selectedProtocol
,accountId
) );
200 // Write config not related to ConnectionManager Parameters
201 KConfigGroup
*accountConfig
= account()->configGroup();
202 accountConfig
->writeEntry( QLatin1String("ConnectionManager"), selectedConnectionManager
);
203 accountConfig
->writeEntry( QLatin1String("SelectedProtocol"), selectedProtocol
);
205 // Write config related to ConnectionManager Parameter
206 QString
telepathyGroup(TelepathyProtocol::protocol()->formatTelepathyConfigGroup(selectedConnectionManager
, selectedProtocol
, accountId
));
207 KConfigGroup telepathyConfig
= KGlobal::config()->group(telepathyGroup
);
209 foreach(ConnectionManager::Parameter parameter
, d
->savedParameterList
)
211 telepathyConfig
.writeEntry( parameter
.name(), parameter
.value() );
215 void TelepathyEditAccountWidget::listConnectionManager()
217 // List all available connection managers in the tree widget
218 QList
<ConnectionManager
*> connectionManagers
= ConnectionManagerFactory::self()->getAllConnectionManagers();
219 foreach(ConnectionManager
*connectionManager
, connectionManagers
)
221 new ConnectionManagerItem(connectionManager
, d
->ui
.treeConnectionManager
);
224 // Read config if account() return true
225 // FIXME: Shouldn't be called here
230 void TelepathyEditAccountWidget::connectionManagerSelectionChanged()
232 QTreeWidgetItem
*item
= d
->ui
.treeConnectionManager
->selectedItems().first();
233 ConnectionManagerItem
*itemActivated
= static_cast<ConnectionManagerItem
*>(item
);
236 // Clear protocol list
237 d
->ui
.treeProtocol
->clear();
238 // List supported protocols by this connetion manager.
239 QStringList supportedProtocols
= itemActivated
->connectionManager()->supportedProtocols();
240 foreach(QString protocol
, supportedProtocols
)
242 new QTreeWidgetItem(d
->ui
.treeProtocol
, QStringList(protocol
));
247 void TelepathyEditAccountWidget::protocolSelectionChanged()
249 QTreeWidgetItem
*connectionItem
= d
->ui
.treeConnectionManager
->selectedItems().first();
250 ConnectionManagerItem
*cmItem
= static_cast<ConnectionManagerItem
*>(connectionItem
);
252 QTreeWidgetItem
*protocolItem
= d
->ui
.treeProtocol
->selectedItems().first();
253 if( protocolItem
&& cmItem
)
255 // Check if existing tab exists and remove it (and delete the widget)
258 d
->ui
.tabWidget
->removeTab(1);
259 d
->paramWidget
->deleteLater();
263 QString protocol
= protocolItem
->text(0);
265 // Use saved parameters if available
266 QList
<QtTapioca::ConnectionManager::Parameter
> tabParameter
;
267 if( account() && protocol
== account()->connectionProtocol() )
269 tabParameter
= account()->allConnectionParameters();
273 tabParameter
= cmItem
->connectionManager()->protocolParameters(protocol
);
276 d
->paramWidget
= new TelepathyEditParameterWidget(tabParameter
, this);
277 d
->ui
.tabWidget
->addTab(d
->paramWidget
, i18n("Protocol Parameters"));
281 #include "telepathyeditaccountwidget.moc"