/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2004-06-15
 * Description : Albums manager interface.
 *
 * Copyright (C) 2004 by Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Copyright (C) 2006-2009 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * Copyright (C) 2006-2009 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ============================================================ */

/** @file albummanager.h */

#ifndef ALBUMMANAGER_H
#define ALBUMMANAGER_H

// Qt includes

#include <QList>
#include <QObject>
#include <QString>
#include <QMap>

// KDE includes

#include <kurl.h>

// Local includes

#include "albuminfo.h"
#include "digikam_export.h"

class QDate;

class KJob;
namespace KIO
{
class Job;
}

namespace Digikam
{

class CollectionLocation;
class Album;
class PAlbum;
class TAlbum;
class DAlbum;
class SAlbum;
class AlbumItemHandler;
class AlbumChangeset;
class TagChangeset;
class SearchChangeset;
class CollectionImageChangeset;
class ImageTagChangeset;
class AlbumManagerPriv;

typedef QList<Album*> AlbumList;

/**
 * \class AlbumManager
 *
 * There are two primary managers which manage the listing and 
 * lifetime of Album and ImageInfo: AlbumManager and AlbumLister
 *
 * AlbumManager manages albums: does listing of albums and controls the lifetime of it. 
 * For PAlbums and TAlbums, the listing is done by reading the db directly and
 * building the hierarchy of the albums. For DAlbums, since the listing takes
 * time, the work is delegated to a kioslave. Interested frontend entities can
 * connect to the albummanager to receive notifications of new Albums, when
 * Albums are deleted and when the current album is changed.
 *
 * Additional operations are provided for: creating/deleting/rename Albums, 
 * updating icons and moving Albums.
 *
 */
class AlbumManager : public QObject
{
    Q_OBJECT

public:

    /**
     * A convenience function to get the instance of the AlbumManager
     */
    static AlbumManager* instance();

    /** @name Library path And Scanning
     */
    //@{
    /**
    * Initialize. Informs the user about failures.
    * Returns true on success, false on failure.
    * A return value of false during startup indicates termination of the program
    * (user is informed)
    */
    bool setDatabase(const QString &dbPath, bool priority, const QString &suggestedAlbumRoot = QString());

    /**
    * Sets new database when chosen by the user in setup.
    * Handles user notification about problems.
    * Call this instead of setDatabase when digikam is up and running.
    */
    void changeDatabase(const QString &dbPath);

    /**
    * Stop ongoing operations, prepare for application shutdown
    */
    void cleanUp();

    /**
    * Checks if the given database path is equal to the current one
    */
    bool databaseEqual(const QString &dbPath) const;

    /**
    * starts scanning the libraryPath and listing the albums. If the
    * libraryPath has not changed since the lastscan, then nothing happens
    * @see setLibraryPath
    * @see refresh
    */
    void       startScan();

    /**
    * This is similar to startScan, except that it assumes you have run
    * startScan at least once. It checks the database to see if any new albums
    * have been added and updates them accordingly. Use this when a change in the
    * filesystem is detected (but the album library path hasn't changed)
    * @see startScan
    */
    void       refresh();

    /**
    * Ensures that valid item counts for physical and tag albums are available
    */
    void       prepareItemCounts();
    //@}

    /** @name List of Albums and current Album
     */
    //@{
    /**
     * @return a list of all PAlbums
     */
    AlbumList allPAlbums() const;

    /**
     * @return a list of all TAlbums
     */
    AlbumList allTAlbums() const;

    /**
     * @return a list of all SAlbums
     */
    AlbumList allSAlbums() const;

    /**
     * @return a list of all SAlbums
     */
    AlbumList allDAlbums() const;

    /**
    * set the current album to @p album. Call this from views which show
    * listing of albums. This also causes it to fire the signal
    * signalAlbumCurrentChanged()
    */
    void      setCurrentAlbum(Album *album);
 
    /**
    * @returns the current album
    */
    Album*    currentAlbum() const;
    //@}

    /** @name Finding Albums
     */
    //@{
    /**
     * Given a complete file url (kde url with file protocol), it will try to find
     * a PAlbum corresponding to it.
     * \warning This should not be used, unless really necessary
     * @return PAlbum corresponding to supplied @p url
     * @param url the url we need to check
     */
    PAlbum*   findPAlbum(const KUrl& url) const;

    /**
     * @return a PAlbum with given ID
     * @param id the id for the PAlbum
     */
    PAlbum*   findPAlbum(int id) const;

    /**
     * @return a TAlbum with given ID
     * @param id the id for the TAlbum
     */
    TAlbum*   findTAlbum(int id) const;

    /**
     * @return a SAlbum with given ID
     * @param id the id for the SAlbum
     */
    SAlbum*   findSAlbum(int id) const;

    /**
     * @return a DAlbum with given ID
     * @param id the id for the DAlbum
     */
    DAlbum*   findDAlbum(int id) const;

    /**
     * @return a Album with the given globalID
     * @param gid the global id for the album
     */
    Album*    findAlbum(int gid) const;

    /**
     * @return a TAlbum with given tag path, or 0 if not found
     * @param tagPath the tag path ("People/Friend/John")
     */
    TAlbum*   findTAlbum(const QString &tagPath) const;

    /**
     * @return a SAlbum with given name, or 0 if not found
     * @param tagPath the name of the search
     */
    SAlbum*   findSAlbum(const QString &name) const;
    //@}

    /** @name Operations on PAlbum
     */
    //@{
    /**
     * Create a new PAlbum with supplied properties as a child of the parent
     * This is equivalent to creating a new folder on the disk with supplied
     * name in the parent's folder path. Also the supplied attributes are written
     * out to the database
     * \note the signalAlbumAdded will be fired before this function returns. Its
     * recommended to connect to that signal to get notification of new album added
     * @return the newly created PAlbum or 0 if it fails
     * @param parent  The parent album under which to create the new Album.
     *                Parent must not be root. Otherwise, use the other variants of this method.
     *                If parent is root, the albumRootPath must be supplied.
     * @param name    the name of the new album
     * @param caption the caption for the new album
     * @param date    the date for the new album
     * @param collection the collection for the new album
     * @param errMsg  this will contain the error message describing why the
     * operation failed
     */
    PAlbum* createPAlbum(PAlbum* parent, const QString& name,
                         const QString& caption, const QDate& date,
                         const QString& category,
                         QString& errMsg);

    /**
     * Overloaded method. Here you can supply an albumRootPath which must
     * correspond to an available collection location.
     */
    PAlbum* createPAlbum(const QString& albumRootPath, const QString& name,
                         const QString& caption, const QDate& date,
                         const QString& category,
                         QString& errMsg);

    /**
     * Overloaded method. Here you can supply a collection location (which
     * must be available).
     */
    PAlbum* createPAlbum(const CollectionLocation &location, const QString& name,
                         const QString& caption, const QDate& date,
                         const QString& category,
                         QString& errMsg);

    /**
     * Renames a PAlbum. This is equivalent to actually renaming the corresponding
     * folder on the disk.
     * @return true if the operation succeeds, false otherwise
     * @param album the Album which should be renamed
     * @param newName the new name for the album
     * @param errMsg this will contain the error message describing why the
     * operation failed
     */ 
    bool renamePAlbum(PAlbum* album, const QString& newName,
                      QString& errMsg);

    /**
     * Update the icon for an album. The @p icon is the name (and not full path)
     * of the file in the album
     * @return true if the operation succeeds, false otherwise
     * @param album  the album for which icon should be changed
     * @param iconID the filename of the new icon
     * @param errMsg if the operation fails, this will contain the error message
     * describing why the operation failed
     */
    bool updatePAlbumIcon(PAlbum *album, qlonglong iconID, QString& errMsg);
    //@}

    /**
     * @return A hash with the titles for all album IDs.
     */
    QHash<int, QString> albumTitles() const;

    /** @name Operations on TAlbum
     */
    //@{
    /**
     * Create a new TAlbum with supplied properties as a child of the parent
     * The tag is added to the database
     * \note the signalAlbumAdded will be fired before this function returns. Its
     * recommended to connect to that signal to get notification of new album added
     * @return the newly created TAlbum or 0 if it fails
     * @param parent  the parent album under which to create the new Album
     * @param name    the name of the new album
     * @param iconkde the iconkde for the new album (this is a filename which
     * kde iconloader can load up
     * @param errMsg  this will contain the error message describing why the
     * operation failed
     */
    TAlbum* createTAlbum(TAlbum* parent, const QString& name, 
                         const QString& iconkde, QString& errMsg);

    /**
     * A list of tag paths is supplied.
     * If no corresponding TAlbum exists, a new one will be created.
     * @param tagPath A list of tag paths
     * @returns A list of all TAlbums for the list (already existing or newly created)
    */
    AlbumList findOrCreateTAlbums(const QStringList &tagPaths);

    /**
     * Delete a TAlbum.
     * The tag is removed from the database
     * \note the signalAlbumDeleted will be fired before this function returns. Its
     * recommended to connect to that signal to get notification of album deletes
     * @return true if the operation succeeds or false otherwise
     * @param album   the TAlbum to delete
     * @param errMsg  this will contain the error message describing why the
     * operation failed
     */
    bool deleteTAlbum(TAlbum* album, QString& errMsg);

    /**
     * Renames a TAlbum.
     * This updates the tag name in the database
     * @return true if the operation succeeds, false otherwise
     * @param album the Album which should be renamed
     * @param name the new name for the album
     * @param errMsg this will contain the error message describing why the
     * operation failed
     */
    bool renameTAlbum(TAlbum* album, const QString& name, QString& errMsg);

    /**
     * Move a TAlbum to a new parent.
     * This updates the tag parent ID in the database
     * @return true if the operation succeeds, false otherwise
     * @param album the Album which should be moved
     * @param newParent the Parent Album to which album should be moved
     * @param errMsg this will contain the error message describing why the
     * operation failed
     */
    bool moveTAlbum(TAlbum* album, TAlbum *newParent, QString &errMsg);

    /**
     * Update the icon for a TAlbum. 
     * @return true if the operation succeeds, false otherwise
     * @param album the album for which icon should be changed
     * @param iconKDE  a simple filename which can be loaded by KIconLoader
     * @param iconID   id of the icon image file
     * @param errMsg this will contain the error message describing why the
     * operation failed
     * \note if iconKDE is not empty then iconID is used. So if you want to set
     * the icon to a file which can be loaded by KIconLoader, pass it in as
     * iconKDE. otherwise pass a null QString to iconKDE and set iconID
     */
    bool updateTAlbumIcon(TAlbum* album, const QString& iconKDE,
                          qlonglong iconID, QString& errMsg);

    /**
     * Get a list of recently assigned tags (only last 6 tags are listed)
     * @return the list of recently assigned TAlbums
     */
    AlbumList getRecentlyAssignedTags() const;

    /**
     * @return A list with the tag paths for a list of tag IDs.
     * @param tagIDs list of tag album IDs
     */
    QStringList tagPaths(const QList<int> &tagIDs, bool leadingSlash=true) const;

    /**
     * @return A list with the tag names for a list of tag IDs.
     * @param tagIDs list of tag album IDs
     */
    QStringList tagNames(const QList<int> &tagIDs) const;

    /**
     * @return A hash with the tag paths for all tag IDs.
     */
    QHash<int, QString> tagPaths(bool leadingSlash=true) const;

    /**
     * @return A hash with the tag names for all tag IDs.
     */
    QHash<int, QString> tagNames() const;

    //@}

    /** @name Operations on SAlbum
     */
    //@{
    /**
     * Create a new SAlbum with supplied url. If an existing SAlbum with same name
     * exists this function will return a pointer to that album, instead of creating
     * a new one. A newly created search album is added to the database. For an
     * existing SAlbum, the url is updated and written out to the database
     * \note the signalAlbumAdded will be fired before this function returns. Its
     * recommended to connect to that signal to get notification of new album added
     * @return the newly created SAlbum or an existing SAlbum with same name
     * @param url    the url of the album
     * @param simple indicates whether the Search album is of simple type or
     * extended type
     */
    SAlbum* createSAlbum(const QString &name, DatabaseSearch::Type type, const QString &query);

    /**
     * Update the url for a SAlbum
     * @return true if the operation succeeds, false otherwise
     * @param album the album to update
     * @param query the new query data of the album
     * @param changedName a new name, or null to keep the current name
     * @param type a new type, or UndefinedType to keep the current type
     */
    bool updateSAlbum(SAlbum* album, const QString &changedQuery,
                      const QString &changedName = QString(), DatabaseSearch::Type type = DatabaseSearch::UndefinedType);

    /**
     * Delete a SAlbum from the database
     * \note the signalAlbumDeleted will be fired before this function returns. Its
     * recommended to connect to that signal to get notification of album deletes
     * @return true if the operation succeeds, false otherwise
     * @param album the album to delete
     */
    bool deleteSAlbum(SAlbum* album);
    //@}

    void setItemHandler(AlbumItemHandler *handler);
    AlbumItemHandler* getItemHandler();
    void refreshItemHandler(const KUrl::List& itemList=KUrl::List());
    void emitAlbumItemsSelected(bool val);

Q_SIGNALS:

    /// An album is about to be added to the given parent (0 if album is root)
    /// after the item given by prev (prev is 0 if parent has no children yet)
    void signalAlbumAboutToBeAdded(Album *album, Album *parent, Album *prev);
    /// The album has been added
    void signalAlbumAdded(Album* album);
    /// The album is about to be deleted, but is still fully valid
    void signalAlbumAboutToBeDeleted(Album *album);
    /// The album is deleted, but the object can still be accessed
    void signalAlbumDeleted(Album* album);
    /// The album is deleted, the object can no longer be accessed.
    /// For identification purposes, the former album pointer is passed.
    void signalAlbumHasBeenDeleted(void *);
    void signalAlbumItemsSelected(bool selected);
    void signalAlbumsCleared();
    void signalAlbumCurrentChanged(Album* album);
    void signalAllAlbumsLoaded();
    void signalAllDAlbumsLoaded();
    void signalAlbumIconChanged(Album* album);
    void signalAlbumRenamed(Album* album);
    void signalSearchUpdated(SAlbum *album);
    void signalTAlbumMoved(TAlbum* album, TAlbum* newParent);
    void signalPAlbumsDirty(const QMap<int, int>&);
    void signalTAlbumsDirty(const QMap<int, int>&);
    void signalDAlbumsDirty(const QMap<YearMonth, int>&);
    void signalDatesMapDirty(const QMap<QDateTime, int>&);

private Q_SLOTS:

    void slotDatesJobResult(KJob* job);
    void slotDatesJobData(KIO::Job* job, const QByteArray& data);
    void slotAlbumsJobResult(KJob* job);
    void slotAlbumsJobData(KIO::Job* job, const QByteArray& data);
    void slotTagsJobResult(KJob* job);
    void slotTagsJobData(KIO::Job* job, const QByteArray& data);
    void slotDirWatchDirty(const QString& path);
    void slotKioFileMoved(const QString& urlFrom, const QString& urlTo);
    void slotKioFilesDeleted(const QStringList& urls);
    void slotKioFilesAdded(const QString& directory);
    void slotNotifyFileChange(const QString &directory);
    void slotCollectionLocationStatusChanged(const CollectionLocation &, int);
    void slotCollectionLocationPropertiesChanged(const CollectionLocation &location);
    void slotAlbumChange(const AlbumChangeset &changeset);
    void slotTagChange(const TagChangeset &changeset);
    void slotSearchChange(const SearchChangeset &changeset);
    void slotCollectionImageChange(const CollectionImageChangeset &changeset);
    void slotImageTagChange(const ImageTagChangeset &changeset);

    /**
     * Scan albums directly from database and creates new PAlbums
     * It only creates those PAlbums which haven't already been
     * created
     */
    void scanPAlbums();
    void updateChangedPAlbums();
    /**
     * Scan tags directly from database and creates new TAlbums
     * It only creates those TAlbums which haven't already been
     * created
     */
    void scanTAlbums();
    /**
     * Scan searches directly from database and creates new SAlbums
     * It only creates those SAlbums which haven't already been
     * created
     */
    void scanSAlbums();
    /**
     * Scan dates from the database (via IOSlave) and
     * updates the DAlbums.
     */
    void scanDAlbums();

    void getAlbumItemsCount();
    void getTagItemsCount();

private:

    friend class AlbumManagerCreator;
    AlbumManager();
    ~AlbumManager();

    void insertPAlbum(PAlbum *album, PAlbum *parent);
    void removePAlbum(PAlbum *album);
    void insertTAlbum(TAlbum *album, TAlbum *parent);
    void removeTAlbum(TAlbum *album);
    void updateAlbumPathHash();

    void notifyAlbumDeletion(Album *album);

    void addAlbumRoot(const CollectionLocation &location);
    void removeAlbumRoot(const CollectionLocation &location);

    void handleKioNotification(const KUrl &url);

    template <class T> friend class AlbumPointer;
    friend class Album;
    static AlbumManager *internalInstance;
    void addGuardedPointer(Album *a, Album **pointer);
    void removeGuardedPointer(Album *a, Album **pointer);
    void changeGuardedPointer(Album *oldAlbum, Album *a, Album **pointer);
    void invalidateGuardedPointers(Album *album);

private:

    AlbumManagerPriv* const d;
};

/**
 * You can use AlbumPointer to store a guarded pointer to Album
 * or one of the subclasses (use template parameter).
 * The pointer will be set to 0 when the album object is deleted.
 */
template <class T = Album>

class AlbumPointer
{
public:

    AlbumPointer() : album(0) {}
    AlbumPointer(T *a) : album(a)
    { AlbumManager::instance()->addGuardedPointer(album, &album); }
    AlbumPointer(const AlbumPointer<T> &p) : album(p.album)
    { AlbumManager::instance()->addGuardedPointer(album, &album); }

    ~AlbumPointer()
    { AlbumManager::instance()->removeGuardedPointer(album, &album); }

    AlbumPointer<T> operator=(T* a)
    {
        Album *oldAlbum = album;
        album = a;
        AlbumManager::instance()->changeGuardedPointer(oldAlbum, album, &album);
        return *this;
    }
    AlbumPointer<T> operator=(const AlbumPointer<T> &p)
    {
        Album *oldAlbum = album;
        album = p.album;
        AlbumManager::instance()->changeGuardedPointer(oldAlbum, album, &album);
        return *this;
    }

    T* operator->() const { return static_cast<T*>(const_cast<Album*>(album)); }
    T& operator*() const { return *static_cast<T*>(const_cast<Album*>(album)); }
    operator T*() const { return static_cast<T*>(const_cast<Album*>(album)); }

private:

    friend class AlbumManager;
    Album *album;
};

}  // namespace Digikam

#endif /* ALBUMMANAGER_H */
