• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdenetwork
  • Sitemap
  • Contact Us
 

kopete/kopete

addaccountwizard.cpp

Go to the documentation of this file.
00001 /*
00002     addaccountwizard.cpp - Kopete Add Account Wizard
00003 
00004     Copyright (c) 2003-2006 by Olivier Goffart       <ogoffart@kde.org>
00005     Copyright (c) 2003      by Martijn Klingens      <klingens@kde.org>
00006 
00007     Kopete    (c) 2003-2006 by the Kopete developers <kopete-devel@kde.org>
00008 
00009     *************************************************************************
00010     *                                                                       *
00011     * This program is free software; you can redistribute it and/or modify  *
00012     * it under the terms of the GNU General Public License as published by  *
00013     * the Free Software Foundation; either version 2 of the License, or     *
00014     * (at your option) any later version.                                   *
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     // setup the select service page
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     // setup the final page
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     // add the available messenger services to the dialogs list
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     // focus the ListView
00106     QTreeWidget *protocol_list = d->uiSelectService.protocolListView;
00107     protocol_list->setFocus();
00108     
00109     
00110  
00111     // hook up the user input
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     // Make sure a protocol is selected before allowing the user to continue
00132     setValid(d->selectServiceItem, d->selectedProtocol() != 0);
00133 }
00134 
00135 void AddAccountWizard::slotProtocolListDoubleClicked()
00136 {
00137     // proceed to the next wizard page if we double click a protocol
00138     next();
00139 }
00140 
00141 void AddAccountWizard::back()
00142 {
00143     if (currentPage()->widget() == d->accountPageWidget)
00144     {
00145         // Deletes the accountPage, K3Wizard does not like deleting pages
00146         // using different pointers, it only seems to watch its own pointer
00147         delete d->accountPage;
00148         d->accountPage = 0;
00149         d->proto       = 0;
00150 
00151         // removePage() already goes back to previous page, no back() needed
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         { //no item selected
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         // check the data of the page is valid
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     // registeredAccount shouldn't probably be called here. Anyway, if the account is already registered, 
00210     // it won't be registered twice
00211     Kopete::AccountManager *manager = Kopete::AccountManager::self();
00212     Kopete::Account        *account = d->accountPage->apply();
00213 
00214     // if the account wasn't created correctly then leave
00215     if (!account)
00216     {
00217         reject();
00218         return;
00219     }
00220 
00221     // Set a valid identity before registering the account
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     // if the account wasn't created correctly then leave
00233     if (!account)
00234     {
00235         reject();
00236         return;
00237     }
00238 
00239     // Make sure the protocol is correctly enabled.  This is not really needed, but still good
00240     const QString PROTO_NAME = d->proto->pluginId().remove("Protocol").toLower();
00241     Kopete::PluginManager::self()->setPluginEnabled(PROTO_NAME , true);
00242 
00243     // setup the custom colour
00244     if (d->uiFinish.mUseColor->isChecked())
00245     {
00246         account->setColor(d->uiFinish.mColorButton->color());
00247     }
00248 
00249     // connect if necessary
00250     if (d->uiFinish.mConnectNow->isChecked())
00251     {
00252         account->connect();
00253     }
00254 
00255     KAssistantDialog::accept();
00256 }
00257 
00258 void AddAccountWizard::reject()
00259 {
00260     // if we have a protocol plugin loaded and its not being used, unload it
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 // vim: set noet ts=4 sts=4 sw=4:
00296 

kopete/kopete

Skip menu "kopete/kopete"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdenetwork

Skip menu "kdenetwork"
  • kget
  • kopete
  •   kopete
  •   libkopete
  •       libpapillon
  • krfb
Generated for kdenetwork by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal