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

kmobiletools

addressbook.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002    Copyright (C) 2007 by Matthias Lechner <matthias@lmme.de>
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2 of the License, or
00007    (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the
00016    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "addressbook.h"
00021 #include "addressbookentryitem.h"
00022 
00023 #include <QtGui/QLabel>
00024 #include <QtGui/QPushButton>
00025 #include <QtGui/QGridLayout>
00026 #include <QtGui/QAction>
00027 #include <QtCore/QPoint>
00028 #include <QtGui/QMenu>
00029 
00030 #include <KDE/KLocale>
00031 #include <KDE/KIcon>
00032 #include <KDE/KMessageBox>
00033 #include <KDE/KIconLoader>
00034 
00035 #include <libkmobiletools/ifaces/addressbook.h>
00036 #include <libkmobiletools/ifaces/status.h>
00037 #include <libkmobiletools/deviceloader.h>
00038 #include <libkmobiletools/enginexp.h>
00039 
00040 Addressbook::Addressbook( QObject* parent, const QString& deviceName )
00041 : CoreService( parent )
00042 {
00043     m_deviceName = deviceName;
00044 
00045     m_widget = 0;
00046     m_addAddresseeDialog = 0;
00047 
00048     m_engine = KMobileTools::DeviceLoader::instance()->engine( m_deviceName );
00049     if( m_engine ) {
00050         m_addressbook = qobject_cast<KMobileTools::Ifaces::Addressbook*> ( m_engine );
00051 
00052         if( m_addressbook ) {
00053             setupWidget();
00054             setupActions();
00055         }
00056     }
00057 }
00058 
00059 
00060 Addressbook::~Addressbook()
00061 {
00062 }
00063 
00064 QString Addressbook::name() const {
00065     return i18n( "Address book" );
00066 }
00067 
00068 KIcon Addressbook::icon() const {
00069     return KIcon( "x-office-address-book" );
00070 }
00071 
00072 QWidget* Addressbook::widget() const {
00073     Q_ASSERT( m_widget );
00074     return m_widget;
00075 }
00076 
00077 QStringList Addressbook::requires() const {
00078     return QStringList( "Addressbook" );
00079 }
00080 
00081 void Addressbook::setupWidget() {
00082     m_widget = new QWidget();
00083 
00084     m_addresseeList = new QListWidget( m_widget );
00085     m_addresseeList->setContextMenuPolicy( Qt::CustomContextMenu );
00086     connect( m_addresseeList, SIGNAL(customContextMenuRequested(const QPoint&)),
00087              this, SLOT(addresseeListContextMenu(const QPoint&)) );
00088     connect( m_addresseeList, SIGNAL(itemActivated(QListWidgetItem*)),
00089              this, SLOT(addresseeActivated(QListWidgetItem*)) );
00090     connect( m_addresseeList, SIGNAL(itemClicked(QListWidgetItem*)),
00091              this, SLOT(addresseeActivated(QListWidgetItem*)) );
00092 
00093     m_addresseeSearch = new KListWidgetSearchLine( m_widget, m_addresseeList );
00094     m_addresseeDetails = new KTextBrowser( m_widget );
00095 
00096     QGridLayout* layout = new QGridLayout( m_widget );
00097     layout->addWidget( m_addresseeSearch, 0, 0 );
00098     layout->addWidget( m_addresseeList, 1, 0 );
00099     layout->addWidget( m_addresseeDetails, 0, 1, 2, 1 );
00100     layout->setColumnStretch( 1, 1 );
00101 }
00102 
00103 void Addressbook::setupActions() {
00104     m_newContact = new QAction( KIcon( "list-add-user" ), i18n( "New contact" ), this );
00105     m_newContact->setShortcut( QKeySequence( Qt::CTRL + Qt::Key_N ) );
00106 
00107     m_editContact = new QAction( KIcon( "user-properties" ), i18n( "Edit contact" ), this );
00108     m_editContact->setEnabled( false );
00109 
00110     m_deleteContact = new QAction( KIcon( "list-remove-user" ), i18n( "Delete contact" ), this );
00111     m_deleteContact->setShortcut( Qt::Key_Delete );
00112     m_deleteContact->setEnabled( false );
00113 
00114     QAction *separator = new QAction( this );
00115     separator->setSeparator( true );
00116 
00117     QAction *reloadContacts = new QAction( KIcon( "view-refresh" ), i18n( "Reload contacts" ), this );
00118     reloadContacts->setShortcut( Qt::Key_F5 );
00119 
00120     QAction *separator2 = new QAction( this );
00121     separator2->setSeparator( true );
00122 
00123     QAction *importContacts = new QAction( KIcon( "document-import" ), i18n( "Import contacts" ), this );
00124 
00125     QAction *exportContacts = new QAction( KIcon( "document-export" ), i18n( "Export contacts" ), this );
00126 
00127 
00128     // connect actions
00129     connect( reloadContacts, SIGNAL(triggered()), m_engine, SLOT(fetchAddressbook()) );
00130     connect( m_engine, SIGNAL(addressbookFetched()), this, SLOT(cleanUpItems()) );
00131     connect( m_newContact, SIGNAL(triggered()), this, SLOT(requestEntryAddition()) );
00132     connect( m_deleteContact, SIGNAL(triggered()), this, SLOT(requestEntryRemoval()) );
00133     connect( m_editContact, SIGNAL(triggered()), this, SLOT(requestEntryEditing()) );
00134 
00135     connect( m_addresseeList, SIGNAL(itemSelectionChanged()), this, SLOT(checkEnableActions()) );
00136 
00137     // add actions to the action collection
00138     m_actionList.append( m_newContact );
00139     m_actionList.append( m_editContact );
00140     m_actionList.append( m_deleteContact );
00141     m_actionList.append( separator );
00142     m_actionList.append( reloadContacts );
00143     m_actionList.append( separator2 );
00144     m_actionList.append( importContacts );
00145     m_actionList.append( exportContacts );
00146 
00147     // setup poll timer
00148     m_fetchTimer = new QTimer( this );
00149     m_fetchTimer->setInterval( 10 * 1000 );
00150     connect( m_fetchTimer, SIGNAL(timeout()), m_engine, SLOT(fetchAddressbook()) );
00151 
00152     m_fetchTimer->start();
00153 
00154     // setup engine connections
00155     connect( m_engine, SIGNAL(addresseeAdded(const KMobileTools::AddressbookEntry&)),
00156              this, SLOT(addEntry(const KMobileTools::AddressbookEntry&)) );
00157     connect( m_engine, SIGNAL(addresseeRemoved(const KMobileTools::AddressbookEntry&)),
00158              this, SLOT(removeEntry(const KMobileTools::AddressbookEntry&)) );
00159 }
00160 
00161 QList<QAction*> Addressbook::actionList() const {
00162     return m_actionList;
00163 }
00164 
00165 void Addressbook::addresseeActivated( QListWidgetItem* item ) {
00166     AddressbookEntryItem* entry = dynamic_cast<AddressbookEntryItem*>( item );
00167     if( entry ) {
00168         KMobileTools::AddressbookEntry addressee = entry->addressee();
00169 
00171 
00172         // prepare html file for the browser widget
00173         QString html;
00174         html += "<html><body>";
00175 
00176         // prepare the header (ugly table style)
00177         html += "<table border=\"0\"><tr><td><img src=\"";
00178         html += KIconLoader::global()->iconPath( "x-office-contact", -KIconLoader::SizeMedium, false );
00179         html += "\"></td><td valign=\"middle\"><h2>";
00180         html += addressee.name();
00181         html += "</h2></td></tr></table>";
00182 
00183         // add the phone numbers
00184         KABC::PhoneNumber::List phoneNumbers = addressee.phoneNumbers();
00185         for( int i=0; i<phoneNumbers.size(); i++ )
00186             html += phoneNumbers.at( i ).number() + "<br/>";
00187 
00188         html += "</body></html>";
00189         m_addresseeDetails->setHtml( html );
00190     }
00191 }
00192 
00193 void Addressbook::addresseeListContextMenu( const QPoint& position ) {
00194     QMenu menu;
00195 
00196     // look if there's an item at the clicked position
00197     QListWidgetItem* item = m_addresseeList->itemAt( position );
00198     if( item ) {
00199         AddressbookEntryItem* entry = static_cast<AddressbookEntryItem*>( item );
00200         if( entry ) {
00201             if( entry->state() == AddressbookEntryItem::Default ) {
00202                 menu.addAction( m_editContact );
00203                 menu.addAction( m_deleteContact );
00204             }
00205         }
00206     } else
00207         menu.addAction( m_newContact );
00208 
00209     menu.exec( m_addresseeList->mapToGlobal( position ) );
00210 }
00211 
00212 void Addressbook::cleanUpItems() {
00213     m_mutex.lock();
00214 
00215     for( int i=0; i<m_pendingItems.size(); i++ ) {
00216         AddressbookEntryItem* item = static_cast<AddressbookEntryItem*>( m_pendingItems.at( i ) );
00217         if( item ) {
00218             if( item->state() == AddressbookEntryItem::AdditionRequested )
00219                 delete item;
00220         }
00221     }
00222 
00224     m_pendingItems.clear();
00225 
00226     m_mutex.unlock();
00227 }
00228 
00229 void Addressbook::addEntry( const KMobileTools::AddressbookEntry& entry ) {
00230     AddressbookEntryItem* item = new AddressbookEntryItem( m_addresseeList );
00231     item->setText( entry.name() );
00232     item->setIcon( KIcon( "x-office-contact" ) );
00233     item->setAddressee( entry );
00234 }
00235 
00236 void Addressbook::removeEntry( const KMobileTools::AddressbookEntry& entry ) {
00237     QList<QListWidgetItem*> items = m_addresseeList->findItems( entry.name(), Qt::MatchExactly );
00238     for( int i=0; i<items.size(); i++ ) {
00239         AddressbookEntryItem* item = static_cast<AddressbookEntryItem*>( items.at( i ) );
00240         if( item ) {
00241             if( item->addressee() == entry )
00242                 delete items.at( i );
00243         }
00244     }
00245 }
00246 
00247 void Addressbook::findAvailableSlots() {
00248     emit foundAvailableSlots( m_addressbook->availableMemorySlots() );
00249 }
00250 
00251 void Addressbook::requestEntryAddition( const KMobileTools::AddressbookEntry& entry ) {
00252     AddressbookEntryItem* item = new AddressbookEntryItem( m_addresseeList );
00253     item->setText( entry.name() );
00254     item->setIcon( KIcon( "x-office-contact" ) );
00255     item->setAddressee( entry );
00256     item->setState( AddressbookEntryItem::AdditionRequested );
00257 
00258     m_mutex.lock();
00259     m_pendingItems.append( item );
00260     m_mutex.unlock();
00261 
00262     m_addressbook->addAddressee( entry );
00263 }
00264 
00265 void Addressbook::requestEntryAddition() {
00266     if( !m_addAddresseeDialog ) {
00267         m_addAddresseeDialog = new AddAddresseeDialog();
00268         connect( this, SIGNAL(foundAvailableSlots(KMobileTools::AddressbookEntry::MemorySlots)),
00269                  m_addAddresseeDialog, SLOT(availableSlots(KMobileTools::AddressbookEntry::MemorySlots)) );
00270         connect( m_addAddresseeDialog, SIGNAL(addAddressee(const KMobileTools::AddressbookEntry&)),
00271                  this, SLOT(requestEntryAddition(const KMobileTools::AddressbookEntry&)) );
00272     }
00273 
00274     findAvailableSlots();
00275     m_addAddresseeDialog->show();
00276 }
00277 
00278 void Addressbook::requestEntryEditing() {
00280     KMessageBox::information( m_widget, QString( "Sorry, editing not implemented yet." ) );
00281 }
00282 
00283 void Addressbook::requestEntryRemoval() {
00284     QListWidgetItem* currentItem = m_addresseeList->currentItem();
00285 
00286     if( currentItem ) {
00287         AddressbookEntryItem* entry = static_cast<AddressbookEntryItem*>( currentItem );
00288         if( entry ) {
00289             // we can only remove items with no pending actions on them
00290             if( entry->state() == AddressbookEntryItem::Default ) {
00291                 int result = KMessageBox::warningYesNo(
00292                                             m_widget,
00293                                             i18n( "Do you really want to remove \"%1\" from your address book?",
00294                                                   entry->addressee().name() ),
00295                                             i18n( "Removing contact" )
00296                 );
00297 
00298                 if( result == KMessageBox::Yes ) {
00299                     entry->setState( AddressbookEntryItem::RemovalRequested );
00300                     m_mutex.lock();
00301                     m_pendingItems.append( currentItem );
00302                     m_mutex.unlock();
00303 
00304                     m_addressbook->removeAddressee( entry->addressee() );
00305                 }
00306             }
00307         }
00308     }
00309 }
00310 
00311 void Addressbook::checkEnableActions() {
00312     if( m_addresseeList->currentItem() ) {
00313         m_editContact->setEnabled( true );
00314         m_deleteContact->setEnabled( true );
00315     }
00316     else {
00317         m_editContact->setEnabled( false );
00318         m_deleteContact->setEnabled( false );
00319     }
00320 }
00321 
00322 K_EXPORT_PLUGIN( AddressbookFactory )
00323 
00324 AddressbookFactory::AddressbookFactory()
00325  : KPluginFactory("libkmtaddressbook_service")
00326 {
00327 }
00328 
00329 AddressbookFactory::~AddressbookFactory()
00330 {
00331 }
00332 
00333 QObject *AddressbookFactory::create(
00334   const char *iface, QWidget *parentWidget, QObject *parent, const QVariantList &args, const QString &keyword )
00335 {
00336     Q_UNUSED(iface)
00337     Q_UNUSED(parentWidget)
00338     Q_UNUSED(keyword)
00339     return new Addressbook( parent, args.at(0).toString() );
00340 }
00341 
00342 #include "addressbook.moc"

kmobiletools

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

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim 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