home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kabc / addressbook.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  12.6 KB  |  432 lines

  1. /*
  2.     This file is part of libkabc.
  3.     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
  4.  
  5.     This library is free software; you can redistribute it and/or
  6.     modify it under the terms of the GNU Library General Public
  7.     License as published by the Free Software Foundation; either
  8.     version 2 of the License, or (at your option) any later version.
  9.  
  10.     This library is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.     Library General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU Library General Public License
  16.     along with this library; see the file COPYING.LIB.  If not, write to
  17.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18.     Boston, MA 02110-1301, USA.
  19. */
  20.  
  21. #ifndef KABC_ADDRESSBOOK_H
  22. #define KABC_ADDRESSBOOK_H
  23.  
  24. #include <qobject.h>
  25. #include <qptrlist.h>
  26.  
  27. #include <kresources/manager.h>
  28.  
  29. #include "addressee.h"
  30. #include "field.h"
  31.  
  32. namespace KABC {
  33.  
  34. class ErrorHandler;
  35. class Resource;
  36. class Ticket;
  37.  
  38. /**
  39.   @short Address Book
  40.  
  41.   This class provides access to a collection of address book entries.
  42.  */
  43. class KABC_EXPORT AddressBook : public QObject
  44. {
  45.   Q_OBJECT
  46.  
  47.   friend KABC_EXPORT QDataStream &operator<<( QDataStream &, const AddressBook & );
  48.   friend KABC_EXPORT QDataStream &operator>>( QDataStream &, AddressBook & );
  49.   friend class StdAddressBook;
  50.  
  51.   public:
  52.     /**
  53.       @short Address Book Iterator
  54.  
  55.       This class provides an iterator for address book entries.
  56.      */
  57.     class KABC_EXPORT Iterator
  58.     {
  59.       public:
  60.         Iterator();
  61.         Iterator( const Iterator & );
  62.         ~Iterator();
  63.  
  64.         Iterator &operator=( const Iterator & );
  65.         const Addressee &operator*() const;
  66.         Addressee &operator*();
  67.         Addressee* operator->();
  68.         Iterator &operator++();
  69.         Iterator &operator++(int);
  70.         Iterator &operator--();
  71.         Iterator &operator--(int);
  72.         bool operator==( const Iterator &it );
  73.         bool operator!=( const Iterator &it );
  74.  
  75.         struct IteratorData;
  76.         IteratorData *d;
  77.     };
  78.  
  79.     /**
  80.       @short Address Book Const Iterator
  81.  
  82.       This class provides a const iterator for address book entries.
  83.      */
  84.     class KABC_EXPORT ConstIterator
  85.     {
  86.       public:
  87.         ConstIterator();
  88.         ConstIterator( const ConstIterator & );
  89.         ConstIterator( const Iterator & );
  90.         ~ConstIterator();
  91.  
  92.         ConstIterator &operator=( const ConstIterator & );
  93.         const Addressee &operator*() const;
  94.         const Addressee* operator->() const;
  95.         ConstIterator &operator++();
  96.         ConstIterator &operator++(int);
  97.         ConstIterator &operator--();
  98.         ConstIterator &operator--(int);
  99.         bool operator==( const ConstIterator &it );
  100.         bool operator!=( const ConstIterator &it );
  101.  
  102.         struct ConstIteratorData;
  103.         ConstIteratorData *d;
  104.     };
  105.  
  106.     /**
  107.       Constructs an address book object.
  108.       You have to add the resources manually before calling load().
  109.      */
  110.     AddressBook();
  111.  
  112.     /**
  113.       Constructs an address book object.
  114.       The resources are loaded automatically.
  115.  
  116.       @param config The config file which contains the resource settings.
  117.      */
  118.     AddressBook( const QString &config );
  119.  
  120.     /**
  121.       Destructor.
  122.      */
  123.     virtual ~AddressBook();
  124.  
  125.     /**
  126.       Requests a ticket for saving the addressbook. Calling this function locks
  127.       the addressbook for all other processes. You need the returned ticket
  128.       object for calling the save() function.
  129.  
  130.       @param resource A pointer to the resource which shall be locked. If 0,
  131.                       the default resource is locked.
  132.       @return 0 if the resource is already locked or a valid save ticket
  133.               otherwise.
  134.       @see save()
  135.      */
  136.     Ticket *requestSaveTicket( Resource *resource = 0 );
  137.  
  138.     /**
  139.       Releases the ticket requested previously with requestSaveTicket().
  140.       Call this function, if you want to release a ticket without saving.
  141.      */
  142.     void releaseSaveTicket( Ticket *ticket );
  143.  
  144.     /**
  145.       Loads all addressees synchronously.
  146.  
  147.       @return Whether the loading was successfully.
  148.      */
  149.     bool load();
  150.  
  151.     /**
  152.       Loads all addressees asynchronously. This function returns immediately
  153.       and emits the addressBookChanged() signal as soon as the loading has
  154.       finished.
  155.  
  156.       @return Whether the synchronous part of loading was successfully.
  157.      */
  158.     bool asyncLoad();
  159.  
  160.     /**
  161.       Saves all addressees of one resource synchronously. If the save is
  162.       successfull the ticket is deleted.
  163.  
  164.       @param ticket The ticket returned by requestSaveTicket().
  165.       @return Whether the saving was successfully.
  166.      */
  167.     bool save( Ticket *ticket );
  168.  
  169.     /**
  170.       Saves all addressees of one resource asynchronously. If the save is
  171.       successfull the ticket is deleted.
  172.  
  173.       @param ticket The ticket returned by requestSaveTicket().
  174.       @return Whether the synchronous part of saving was successfully.
  175.      */
  176.     bool asyncSave( Ticket *ticket );
  177.  
  178.     /**
  179.       Returns an iterator pointing to the first addressee of address book.
  180.       This iterator equals end() if the address book is empty.
  181.      */
  182.     ConstIterator begin() const;
  183.  
  184.     /**
  185.       This is an overloaded member function, provided for convenience. It
  186.       behaves essentially like the above function.
  187.      */
  188.     Iterator begin();
  189.  
  190.     /**
  191.       Returns an iterator pointing to the last addressee of address book.
  192.       This iterator equals begin() if the address book is empty.
  193.      */
  194.     ConstIterator end() const;
  195.  
  196.     /**
  197.       This is an overloaded member function, provided for convenience. It
  198.       behaves essentially like the above function.
  199.      */
  200.     Iterator end();
  201.  
  202.  
  203.     /**
  204.       Removes all addressees from the address book.
  205.      */
  206.     void clear();
  207.  
  208.     /**
  209.       Insert an addressee into the address book. If an addressee with the same
  210.       unique id already exists, it is replaced by the new one, otherwise it is
  211.       appended.
  212.  
  213.       @param addr The addressee which shall be insert.
  214.      */
  215.     void insertAddressee( const Addressee &addr );
  216.  
  217.     /**
  218.       Removes an addressee from the address book.
  219.  
  220.       @param addr The addressee which shall be removed.
  221.      */
  222.     void removeAddressee( const Addressee &addr );
  223.  
  224.     /**
  225.       This is an overloaded member function, provided for convenience. It
  226.       behaves essentially like the above function.
  227.  
  228.       @param it An iterator pointing to the addressee which shall be removed.
  229.      */
  230.     void removeAddressee( const Iterator &it );
  231.  
  232.     /**
  233.       Returns an iterator pointing to the specified addressee. It will return
  234.       end() if no addressee matched.
  235.  
  236.       @param addr The addresee you are looking for.
  237.      */
  238.     Iterator find( const Addressee &addr ); // KDE4: const
  239.  
  240.     /**
  241.       Searches an addressee with the specified unique identifier.
  242.  
  243.       @param uid The unique identifier you are looking for.
  244.       @return The addressee with the specified unique identifier or an
  245.               empty addressee.
  246.      */
  247.     Addressee findByUid( const QString &uid ); // KDE4: const
  248.  
  249.     /**
  250.       Returns a list of all addressees in the address book.
  251.      */
  252.     Addressee::List allAddressees(); // KDE4: const
  253.  
  254.     /**
  255.       Searches all addressees which match the specified name.
  256.  
  257.       @param name The name you are looking for.
  258.       @return A list of all matching addressees.
  259.      */
  260.     Addressee::List findByName( const QString &name ); // KDE4: const
  261.  
  262.     /**
  263.       Searches all addressees which match the specified email address.
  264.  
  265.       @param email The email address you are looking for.
  266.       @return A list of all matching addressees.
  267.      */
  268.     Addressee::List findByEmail( const QString &email ); // KDE4: const
  269.  
  270.     /**
  271.       Searches all addressees which belongs to the specified category.
  272.  
  273.       @param category The category you are looking for.
  274.       @return A list of all matching addressees.
  275.      */
  276.     Addressee::List findByCategory( const QString &category ); // KDE4: const
  277.  
  278.     /**
  279.       Returns a string identifying this addressbook. The identifier is
  280.       created by concatenation of the resource identifiers.
  281.      */
  282.     virtual QString identifier(); // KDE4: const
  283.  
  284.     /**
  285.       Returns a list of all Fields known to the address book which are associated
  286.       with the given field category.
  287.      */
  288.     Field::List fields( int category = Field::All ); // KDE4: const
  289.  
  290.     /**
  291.       Add custom field to address book.
  292.  
  293.       @param label    User visible label of the field.
  294.       @param category Ored list of field categories.
  295.       @param key      Identifier used as key for reading and writing the field.
  296.       @param app      String used as application key for reading and writing
  297.                       the field.
  298.      */
  299.     bool addCustomField( const QString &label, int category = Field::All,
  300.                          const QString &key = QString::null,
  301.                          const QString &app = QString::null );
  302.  
  303.     /**
  304.       Adds a resource to the address book.
  305.  
  306.       @param resource The resource you want to add.
  307.       @return Whether opening the resource was successfully.
  308.      */
  309.     bool addResource( Resource *resource );
  310.  
  311.     /**
  312.       Removes a resource from the address book.
  313.  
  314.       @param resource The resource you want to remove.
  315.       @return Whether closing the resource was successfully.
  316.      */
  317.     bool removeResource( Resource *resource );
  318.  
  319.     /**
  320.       Returns a list of all resources.
  321.      */
  322.     QPtrList<Resource> resources(); // KDE4: const
  323.  
  324.     /**
  325.       Sets the @p ErrorHandler, that is used by error() to
  326.       provide GUI independent error messages.
  327.  
  328.       @param errorHandler The error handler you want to use.
  329.      */
  330.     void setErrorHandler( ErrorHandler *errorHandler );
  331.  
  332.     /**
  333.       Shows GUI independent error messages.
  334.  
  335.       @param msg The error message that shall be displayed.
  336.      */
  337.     void error( const QString &msg );
  338.  
  339.     /**
  340.       @deprecated There is no need to call this function anymore.
  341.      */
  342.     void cleanUp() KDE_DEPRECATED;
  343.  
  344.     /**
  345.       Used for debug output. This function prints out the list
  346.       of all addressees to kdDebug(5700).
  347.      */
  348.     void dump() const;
  349.  
  350.     /**
  351.       */
  352.     void emitAddressBookLocked() { emit addressBookLocked( this ); }
  353.     void emitAddressBookUnlocked() { emit addressBookUnlocked( this ); }
  354.     void emitAddressBookChanged() { emit addressBookChanged( this ); }
  355.  
  356.     /**
  357.       Returns true when the loading of the addressbook has finished,
  358.       otherwise false.
  359.  
  360.       @since 3.5
  361.      */
  362.     bool loadingHasFinished() const;
  363.  
  364.   signals:
  365.     /**
  366.       Emitted when one of the resources discovered a change in its backend
  367.       or the asynchronous loading of all resources has finished.
  368.       You should connect to this signal to update the presentation of
  369.       the contact data in your application.
  370.  
  371.       @param addressBook The address book which emitted this signal.
  372.      */
  373.     void addressBookChanged( AddressBook *addressBook );
  374.  
  375.     /**
  376.       Emitted when one of the resources has been locked for writing.
  377.  
  378.       @param addressBook The address book which emitted this signal.
  379.      */
  380.     void addressBookLocked( AddressBook *addressBook );
  381.  
  382.     /**
  383.       Emitted when one of the resources has been unlocked.
  384.       You should connect to this signal if you want to save your changes
  385.       to a resource which is currently locked, and want to get notified when
  386.       saving is possible again.
  387.  
  388.       @param addressBook The address book which emitted this signal.
  389.      */
  390.     void addressBookUnlocked( AddressBook *addressBook );
  391.  
  392.     /**
  393.       Emitted when the asynchronous loading of one resource has finished
  394.       after calling asyncLoad().
  395.  
  396.       @param resource The resource which emitted this signal.
  397.      */
  398.     void loadingFinished( Resource *resource );
  399.  
  400.     /**
  401.       Emitted when the asynchronous saving of one resource has finished
  402.       after calling asyncSave().
  403.  
  404.       @param resource The resource which emitted this signal.
  405.      */
  406.     void savingFinished( Resource *resource );
  407.  
  408.   protected slots:
  409.     void resourceLoadingFinished( Resource* );
  410.     void resourceSavingFinished( Resource* );
  411.     void resourceLoadingError( Resource*, const QString& );
  412.     void resourceSavingError( Resource*, const QString& );
  413.  
  414.   protected:
  415.     void deleteRemovedAddressees();
  416.     void setStandardResource( Resource* );
  417.     Resource *standardResource();
  418.     KRES::Manager<Resource> *resourceManager();
  419.  
  420.   private:
  421.     QPtrList<Resource> mDummy; // Remove in KDE 4
  422.     struct AddressBookData;
  423.     AddressBookData *d;
  424. };
  425.  
  426. KABC_EXPORT QDataStream &operator<<( QDataStream &, const AddressBook & );
  427. KABC_EXPORT QDataStream &operator>>( QDataStream &, AddressBook & );
  428.  
  429. }
  430.  
  431. #endif
  432.