/* This file is part of the KDE libraries
    Copyright (C) 2009 Norbert Frese <nf2.email@gmail.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
 */

#ifndef GTKBOOKMARKS_H_
#define GTKBOOKMARKS_H_

#include <QtCore/QList>
#include <QtCore/QPair>
#include <QtCore/QString>
#include <KUrl>
#include <KDirWatch>

class GtkBookmark
{
public:
    GtkBookmark(const KUrl &url, const QString &text) :
        m_url(url), m_text(text)
        {}

    KUrl url() const
    {
        return m_url;
    }

    QString text() const
    {
        return m_text;
    }

    void update(const KUrl &url, const QString &text)
    {
        m_url = url;
        m_text = text;
    }

    enum CompareResult
    {
        Same,
        Modified,
        Different
    };

    CompareResult compare(const GtkBookmark& other)
    {
        if (m_url == other.url() && m_text == other.text())
        {
            return Same;
        }
        else if (m_url == other.url())
        {
            return Modified;
        }
        else
        {
            return Different;
        }
    }


private:
    KUrl m_url;
    QString m_text;
};

//////

class GtkBookmarks : public QObject
{
    Q_OBJECT
public:

    GtkBookmarks();

    ~GtkBookmarks();

    QList<GtkBookmark> allBookmarks()
    {
        return previousBookmarks;
    }

    void createBookmark(const KUrl &url, const QString &text );
    void updateBookmark(int index, const KUrl &url, const QString &text );
    void removeBookmark(int index);
    void moveBookmark(int index, int afterIndex);
    void insertUrls(int index, const QList<KUrl> &urls);


Q_SIGNALS:
    void added(GtkBookmark bookmark, int index);
    void removed(GtkBookmark bookmark, int index);
    void modified(GtkBookmark bookmark, int index);

private Q_SLOTS:
    void slotFileChanged(const QString& path);

private:
    QList<GtkBookmark> read();
    void write(const QList<GtkBookmark> &list);

    void findChanges(QList<GtkBookmark> bookmarks);

    QList<GtkBookmark> previousBookmarks;

    QString m_bookmarksFilename;
    KDirWatch * m_kDirWatch;

};

#endif /* GTKBOOKMARKS_H_ */
