kopete/kopete
addaccountwizard.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "addaccountwizard.h"
00020
00021 #include <qcheckbox.h>
00022 #include <qlabel.h>
00023
00024 #include <kcolorbutton.h>
00025 #include <kdebug.h>
00026 #include <kiconloader.h>
00027 #include <k3listview.h>
00028 #include <klocale.h>
00029 #include <kmessagebox.h>
00030 #include <kplugininfo.h>
00031 #include <kvbox.h>
00032
00033 #include "editaccountwidget.h"
00034 #include "kopeteaccount.h"
00035 #include "kopeteaccountmanager.h"
00036 #include "kopeteprotocol.h"
00037 #include "kopetepluginmanager.h"
00038 #include "kopeteidentity.h"
00039 #include "kopeteidentitymanager.h"
00040
00041 class AddAccountWizard::Private
00042 {
00043 public:
00044 Private()
00045 : accountPage(0)
00046 , proto(0)
00047 , identity(0L)
00048 {
00049 }
00050
00051 QTreeWidgetItem* selectedProtocol();
00052
00053 QMap<QTreeWidgetItem *, KPluginInfo> protocolItems;
00054 KopeteEditAccountWidget *accountPage;
00055 KVBox *accountPageWidget;
00056 QWidget *selectService;
00057 QWidget *finish;
00058 Ui::AddAccountWizardPage1 uiSelectService;
00059 Ui::AddAccountWizardPage2 uiFinish;
00060 Kopete::Protocol *proto;
00061 KPageWidgetItem *selectServiceItem;
00062 Kopete::Identity *identity;
00063 };
00064
00065 AddAccountWizard::AddAccountWizard( QWidget *parent, bool firstRun )
00066 : KAssistantDialog(parent)
00067 , d(new Private)
00068 {
00069
00070 d->selectService = new QWidget(this);
00071 d->uiSelectService.setupUi(d->selectService);
00072 d->uiSelectService.protocolListView->setColumnCount( 2 );
00073 QStringList header;
00074 header << i18n("Name") << i18n("Description");
00075 d->uiSelectService.protocolListView->setHeaderLabels( header );
00076 if ( firstRun )
00077 d->uiSelectService.m_header->setText( i18nc( "1st message shown to users on first run of Kopete. Please keep the formatting.", "<h2>Welcome to Kopete</h2><p>Which messaging service do you want to connect to?</p>") );
00078
00079 d->selectServiceItem = addPage(d->selectService,d->selectService->windowTitle());
00080 setValid(d->selectServiceItem, false);
00081
00082 d->accountPageWidget = new KVBox(this);
00083 addPage(d->accountPageWidget,i18n("Step Two: Account Information"));
00084
00085
00086 d->finish = new QWidget(this);
00087 d->uiFinish.setupUi(d->finish);
00088 if ( firstRun )
00089 d->uiFinish.m_header->setText( i18nc( "2nd message shown to users on first run of Kopete. Please keep the formatting.", "<h2>Congratulations</h2><p>You have finished configuring the account. You can add more accounts with <i>Settings->Configure</i>. Please click the \"Finish\" button.</p>") );
00090 addPage(d->finish,d->finish->windowTitle());
00091
00092
00093 QList<KPluginInfo> protocols = Kopete::PluginManager::self()->availablePlugins("Protocols");
00094 qSort(protocols);
00095 for (QList<KPluginInfo>::Iterator it = protocols.begin(); it != protocols.end(); ++it)
00096 {
00097 QTreeWidgetItem *pluginItem = new QTreeWidgetItem(d->uiSelectService.protocolListView);
00098 pluginItem->setIcon(0, QIcon(SmallIcon(it->icon())));
00099 pluginItem->setText(0, it->name());
00100 pluginItem->setText(1, it->comment());
00101
00102 d->protocolItems.insert(pluginItem, *it);
00103 }
00104
00105
00106 QTreeWidget *protocol_list = d->uiSelectService.protocolListView;
00107 protocol_list->setFocus();
00108
00109
00110
00111
00112 connect(d->uiSelectService.protocolListView, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
00113 this, SLOT(slotProtocolListClicked()));
00114 connect(d->uiSelectService.protocolListView, SIGNAL(itemSelectionChanged()),
00115 this, SLOT( slotProtocolListClicked()));
00116 connect(d->uiSelectService.protocolListView, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
00117 this, SLOT(slotProtocolListDoubleClicked()));
00118 setHelp(QString(),"kopete");
00119 }
00120
00121 QTreeWidgetItem* AddAccountWizard::Private::selectedProtocol()
00122 {
00123 QList<QTreeWidgetItem*> selectedItems = uiSelectService.protocolListView->selectedItems();
00124 if(!selectedItems.empty())
00125 return selectedItems.first();
00126 return 0;
00127 }
00128
00129 void AddAccountWizard::slotProtocolListClicked()
00130 {
00131
00132 setValid(d->selectServiceItem, d->selectedProtocol() != 0);
00133 }
00134
00135 void AddAccountWizard::slotProtocolListDoubleClicked()
00136 {
00137
00138 next();
00139 }
00140
00141 void AddAccountWizard::back()
00142 {
00143 if (currentPage()->widget() == d->accountPageWidget)
00144 {
00145
00146
00147 delete d->accountPage;
00148 d->accountPage = 0;
00149 d->proto = 0;
00150
00151
00152 }
00153 KAssistantDialog::back();
00154 }
00155
00156 void AddAccountWizard::next()
00157 {
00158 if (currentPage()->widget() == d->selectService)
00159 {
00160 QTreeWidgetItem *lvi = d->selectedProtocol();
00161 if(!d->protocolItems[lvi].isValid())
00162 {
00163 return;
00164 }
00165 d->proto = qobject_cast<Kopete::Protocol *>(Kopete::PluginManager::self()->loadPlugin(d->protocolItems[lvi].pluginName()));
00166 if (!d->proto)
00167 {
00168 KMessageBox::queuedMessageBox(this, KMessageBox::Error,
00169 i18n("Cannot load the %1 protocol plugin.", d->protocolItems[lvi].name()),
00170 i18n("Error While Adding Account"));
00171 return;
00172 }
00173
00174 d->accountPage = d->proto->createEditAccountWidget(0, d->accountPageWidget);
00175 if (!d->accountPage)
00176 {
00177 KMessageBox::queuedMessageBox(this, KMessageBox::Error,
00178 i18n("This protocol does not currently support adding accounts."),
00179 i18n("Error While Adding Account"));
00180 return;
00181 }
00182
00183 KAssistantDialog::next();
00184 }
00185 else if (currentPage()->widget() == d->accountPageWidget)
00186 {
00187
00188 if (!d->accountPage->validateData())
00189 {
00190 return;
00191 }
00192
00193 QColor col = Kopete::AccountManager::self()->guessColor(d->proto);
00194
00195 d->uiFinish.mColorButton->setColor(col);
00196 d->uiFinish.mUseColor->setChecked(col.isValid());
00197 KAssistantDialog::next();
00198 }
00199 else
00200 {
00201 kDebug(14100) << "Next pressed on misc page";
00202 KAssistantDialog::next();
00203 }
00204
00205 }
00206
00207 void AddAccountWizard::accept()
00208 {
00209
00210
00211 Kopete::AccountManager *manager = Kopete::AccountManager::self();
00212 Kopete::Account *account = d->accountPage->apply();
00213
00214
00215 if (!account)
00216 {
00217 reject();
00218 return;
00219 }
00220
00221
00222 if (!d->identity)
00223 {
00224 account->setIdentity(Kopete::IdentityManager::self()->defaultIdentity());
00225 }
00226 else
00227 {
00228 account->setIdentity(d->identity);
00229 }
00230
00231 account = manager->registerAccount(account);
00232
00233 if (!account)
00234 {
00235 reject();
00236 return;
00237 }
00238
00239
00240 const QString PROTO_NAME = d->proto->pluginId().remove("Protocol").toLower();
00241 Kopete::PluginManager::self()->setPluginEnabled(PROTO_NAME , true);
00242
00243
00244 if (d->uiFinish.mUseColor->isChecked())
00245 {
00246 account->setColor(d->uiFinish.mColorButton->color());
00247 }
00248
00249
00250 if (d->uiFinish.mConnectNow->isChecked())
00251 {
00252 account->connect();
00253 }
00254
00255 KAssistantDialog::accept();
00256 }
00257
00258 void AddAccountWizard::reject()
00259 {
00260
00261 if (d->proto)
00262 {
00263 bool hasAccount=false;
00264 foreach( Kopete::Account *act, Kopete::AccountManager::self()->accounts() )
00265 {
00266 if( act->protocol() == d->proto )
00267 {
00268 hasAccount=true;
00269 break;
00270 }
00271 }
00272 if(hasAccount)
00273 {
00274 const QString PROTO_NAME = d->proto->pluginId().remove("Protocol").toLower();
00275 Kopete::PluginManager::self()->unloadPlugin(PROTO_NAME);
00276 }
00277 }
00278
00279 KAssistantDialog::reject();
00280 }
00281
00282 AddAccountWizard::~AddAccountWizard()
00283 {
00284 delete d;
00285 }
00286
00287 void AddAccountWizard::setIdentity( Kopete::Identity *identity )
00288 {
00289 d->identity = identity;
00290 }
00291
00292
00293 #include "addaccountwizard.moc"
00294
00295
00296