#ifndef DISTRIBUTIONLIST_H
#define DISTRIBUTIONLIST_H

#include <kabc/addressee.h>

namespace KABC {
class AddressBook;
}

namespace KPIM {

/**
  @short Distribution list of email addresses

  This class represents a list of email addresses. Each email address is
  associated with an address book entry. If the address book entry changes, the
  entry in the distribution list is automatically updated.

  This should probably go into kdelibs at some point.

*/
class DistributionList : public KABC::Addressee
{
  public:
    /**
      @short Distribution List Entry

      This class represents an entry of a distribution list. It consists of an
      addressee and an email address. If the email address is null, the
      preferred email address of the addressee is used.
    */
    struct Entry
    {
      typedef QValueList<Entry> List;

      Entry() {}
      Entry( const Addressee &_addressee, const QString &_email ) :
          addressee( _addressee ), email( _email ) {}

      Addressee addressee;
      QString email;
    };

    typedef QValueList<DistributionList> List;

    /**
       Create distribution list object.

      @param manager Managing object of this list.
      @param name    Name of this list.
    */
    DistributionList();
    DistributionList( const KABC::Addressee& addr );

    /**
      Destructor.
    */
    ~DistributionList() {}

    /// HACK: reimplemented from Addressee, but it's NOT virtual there
    void setName( const QString &name );

    QString name() const { return formattedName(); }

    /**
      Insert an entry into this distribution list. If the entry already exists
      nothing happens.
    */
    void insertEntry( const Addressee &, const QString &email=QString::null );

    /**
      Remove an entry from this distribution list. If the entry doesn't exist
      nothing happens.
    */
    void removeEntry( const Addressee &, const QString &email=QString::null );

    /// Overload, used by resources to avoid looking up the addressee
    void insertEntry( const QString& uid, const QString& email=QString::null );
    /// Overload, used by resources to avoid looking up the addressee
    void removeEntry( const QString& uid, const QString& email=QString::null );


    /**
      Return list of email addresses, which belong to this distributon list.
      These addresses can be directly used by e.g. a mail client.
      @param book necessary to look up entries
    */
    QStringList emails( KABC::AddressBook* book ) const;

    /**
      Return list of entries belonging to this distribution list. This function
      is mainly useful for a distribution list editor.
      @param book necessary to look up entries
    */
    Entry::List entries( KABC::AddressBook* book ) const;

    // should be a method of Addressee
    static bool isDistributionList( const KABC::Addressee& addr );

    // should be a method of AddressBook
    static DistributionList findByName( KABC::AddressBook* book,
                                        const QString& name,
                                        bool caseSensitive = true );
    // should be a method of AddressBook
    // A bit slow (but no more than findByName).
    // From KAddressbook, use Core::distributionLists() instead.
    static QValueList<DistributionList> allDistributionLists( KABC::AddressBook* book );


  private:
    // can't have any data here, use Addressee's methods instead
};

};

#endif /* DISTRIBUTIONLIST_H */

