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
00039 class AddAccountWizard::Private
00040 {
00041 public:
00042 Private()
00043 : accountPage(0)
00044 , proto(0)
00045 {
00046 }
00047
00048 QTreeWidgetItem* selectedProtocol();
00049
00050 QMap<QTreeWidgetItem *, KPluginInfo> protocolItems;
00051 KopeteEditAccountWidget *accountPage;
00052 KVBox *accountPageWidget;
00053 QWidget *selectService;
00054 QWidget *finish;
00055 Ui::AddAccountWizardPage1 uiSelectService;
00056 Ui::AddAccountWizardPage2 uiFinish;
00057 Kopete::Protocol *proto;
00058 KPageWidgetItem *selectServiceItem;
00059 };
00060
00061 AddAccountWizard::AddAccountWizard( QWidget *parent, bool firstRun )
00062 : KAssistantDialog(parent)
00063 , d(new Private)
00064 {
00065
00066 d->selectService = new QWidget(this);
00067 d->uiSelectService.setupUi(d->selectService);
00068 d->uiSelectService.protocolListView->setColumnCount( 2 );
00069 QStringList header;
00070 header << i18n("Name") << i18n("Description");
00071 d->uiSelectService.protocolListView->setHeaderLabels( header );
00072 if ( firstRun )
00073 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>") );
00074
00075 d->selectServiceItem = addPage(d->selectService,d->selectService->windowTitle());
00076 setValid(d->selectServiceItem, false);
00077
00078 d->accountPageWidget = new KVBox(this);
00079 addPage(d->accountPageWidget,i18n("Step Two: Account Information"));
00080
00081
00082 d->finish = new QWidget(this);
00083 d->uiFinish.setupUi(d->finish);
00084 if ( firstRun )
00085 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>") );
00086 addPage(d->finish,d->finish->windowTitle());
00087
00088
00089 QList<KPluginInfo> protocols = Kopete::PluginManager::self()->availablePlugins("Protocols");
00090 qSort(protocols);
00091 for (QList<KPluginInfo>::Iterator it = protocols.begin(); it != protocols.end(); ++it)
00092 {
00093 QTreeWidgetItem *pluginItem = new QTreeWidgetItem(d->uiSelectService.protocolListView);
00094 pluginItem->setIcon(0, QIcon(SmallIcon(it->icon())));
00095 pluginItem->setText(0, it->name());
00096 pluginItem->setText(1, it->comment());
00097
00098 d->protocolItems.insert(pluginItem, *it);
00099 }
00100
00101
00102 QTreeWidget *protocol_list = d->uiSelectService.protocolListView;
00103 protocol_list->setFocus();
00104
00105
00106
00107
00108 connect(d->uiSelectService.protocolListView, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
00109 this, SLOT(slotProtocolListClicked()));
00110 connect(d->uiSelectService.protocolListView, SIGNAL(itemSelectionChanged()),
00111 this, SLOT( slotProtocolListClicked()));
00112 connect(d->uiSelectService.protocolListView, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
00113 this, SLOT(slotProtocolListDoubleClicked()));
00114 setHelp(QString(),"kopete");
00115 }
00116
00117 QTreeWidgetItem* AddAccountWizard::Private::selectedProtocol()
00118 {
00119 QList<QTreeWidgetItem*> selectedItems = uiSelectService.protocolListView->selectedItems();
00120 if(!selectedItems.empty())
00121 return selectedItems.first();
00122 return 0;
00123 }
00124
00125 void AddAccountWizard::slotProtocolListClicked()
00126 {
00127
00128 setValid(d->selectServiceItem, d->selectedProtocol() != 0);
00129 }
00130
00131 void AddAccountWizard::slotProtocolListDoubleClicked()
00132 {
00133
00134 next();
00135 }
00136
00137 void AddAccountWizard::back()
00138 {
00139 if (currentPage()->widget() == d->accountPageWidget)
00140 {
00141
00142
00143 delete d->accountPage;
00144 d->accountPage = 0;
00145 d->proto = 0;
00146
00147
00148 }
00149 KAssistantDialog::back();
00150 }
00151
00152 void AddAccountWizard::next()
00153 {
00154 if (currentPage()->widget() == d->selectService)
00155 {
00156 QTreeWidgetItem *lvi = d->selectedProtocol();
00157 if(!d->protocolItems[lvi].isValid())
00158 {
00159 return;
00160 }
00161 d->proto = qobject_cast<Kopete::Protocol *>(Kopete::PluginManager::self()->loadPlugin(d->protocolItems[lvi].pluginName()));
00162 if (!d->proto)
00163 {
00164 KMessageBox::queuedMessageBox(this, KMessageBox::Error,
00165 i18n("Cannot load the %1 protocol plugin.", d->protocolItems[lvi].name()),
00166 i18n("Error While Adding Account"));
00167 return;
00168 }
00169
00170 d->accountPage = d->proto->createEditAccountWidget(0, d->accountPageWidget);
00171 if (!d->accountPage)
00172 {
00173 KMessageBox::queuedMessageBox(this, KMessageBox::Error,
00174 i18n("This protocol does not currently support adding accounts."),
00175 i18n("Error While Adding Account"));
00176 return;
00177 }
00178
00179 KAssistantDialog::next();
00180 }
00181 else if (currentPage()->widget() == d->accountPageWidget)
00182 {
00183
00184 if (!d->accountPage->validateData())
00185 {
00186 return;
00187 }
00188
00189 QColor col = Kopete::AccountManager::self()->guessColor(d->proto);
00190
00191 d->uiFinish.mColorButton->setColor(col);
00192 d->uiFinish.mUseColor->setChecked(col.isValid());
00193 KAssistantDialog::next();
00194 }
00195 else
00196 {
00197 kDebug(14100) << "Next pressed on misc page";
00198 KAssistantDialog::next();
00199 }
00200
00201 }
00202
00203 void AddAccountWizard::accept()
00204 {
00205
00206
00207 Kopete::AccountManager *manager = Kopete::AccountManager::self();
00208 Kopete::Account *account = manager->registerAccount(d->accountPage->apply());
00209
00210
00211 if (!account)
00212 {
00213 reject();
00214 return;
00215 }
00216
00217
00218 const QString PROTO_NAME = d->proto->pluginId().remove("Protocol").toLower();
00219 Kopete::PluginManager::self()->setPluginEnabled(PROTO_NAME , true);
00220
00221
00222 if (d->uiFinish.mUseColor->isChecked())
00223 {
00224 account->setColor(d->uiFinish.mColorButton->color());
00225 }
00226
00227
00228 if (d->uiFinish.mConnectNow->isChecked())
00229 {
00230 account->connect();
00231 }
00232
00233 KAssistantDialog::accept();
00234 }
00235
00236 void AddAccountWizard::reject()
00237 {
00238
00239 if (d->proto)
00240 {
00241 bool hasAccount=false;
00242 foreach( Kopete::Account *act, Kopete::AccountManager::self()->accounts() )
00243 {
00244 if( act->protocol() == d->proto )
00245 {
00246 hasAccount=true;
00247 break;
00248 }
00249 }
00250 if(hasAccount)
00251 {
00252 const QString PROTO_NAME = d->proto->pluginId().remove("Protocol").toLower();
00253 Kopete::PluginManager::self()->unloadPlugin(PROTO_NAME);
00254 }
00255 }
00256
00257 KAssistantDialog::reject();
00258 }
00259
00260 AddAccountWizard::~AddAccountWizard()
00261 {
00262 delete d;
00263 }
00264
00265 #include "addaccountwizard.moc"
00266
00267
00268