/*
 *  Copyright © 2003-2007 Fredrik Höglund <fredrik@kde.org>
 *
 *  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 of the License, 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef KCMCURSORTHEME_H
#define KCMCURSORTHEME_H

#include <KQuickAddons/ConfigModule>
#include <QScopedPointer>

class QStandardItemModel;
class QTemporaryFile;

class CursorThemeModel;
class SortProxyModel;
class CursorTheme;

namespace KIO
{
    class FileCopyJob;
}

class CursorThemeConfig : public KQuickAddons::ConfigModule
{
    Q_OBJECT
    Q_PROPERTY(bool canInstall READ canInstall WRITE setCanInstall NOTIFY canInstallChanged)
    Q_PROPERTY(bool canResize READ canResize WRITE setCanResize NOTIFY canResizeChanged)
    Q_PROPERTY(bool canConfigure READ canConfigure WRITE setCanConfigure NOTIFY canConfigureChanged)
    Q_PROPERTY(QAbstractItemModel *cursorsModel READ cursorsModel CONSTANT)
    Q_PROPERTY(QAbstractItemModel *sizesModel READ sizesModel CONSTANT)
    Q_PROPERTY(int selectedThemeRow READ selectedThemeRow WRITE setSelectedThemeRow NOTIFY selectedThemeRowChanged)
    Q_PROPERTY(int selectedSizeRow READ selectedSizeRow WRITE setSelectedSizeRow NOTIFY selectedSizeRowChanged)

    Q_PROPERTY(bool downloadingFile READ downloadingFile NOTIFY downloadingFileChanged)

public:
    CursorThemeConfig(QObject *parent, const QVariantList &);
    ~CursorThemeConfig() override;

public:
    void load() override;
    void save() override;
    void defaults() override;

    //for QML properties
    bool canInstall() const;
    void setCanInstall(bool can);

    bool canResize() const;
    void setCanResize(bool can);

    bool canConfigure() const;
    void setCanConfigure(bool can);

    int selectedThemeRow() const;
    void setSelectedThemeRow(int row);

    int selectedSizeRow() const;
    void setSelectedSizeRow(int row);

    bool downloadingFile() const;

    QAbstractItemModel *cursorsModel();
    QAbstractItemModel *sizesModel();

Q_SIGNALS:
    void canInstallChanged();
    void canResizeChanged();
    void canConfigureChanged();
    void selectedThemeRowChanged();
    void selectedSizeRowChanged();
    void downloadingFileChanged();

    void showSuccessMessage(const QString &message);
    void showInfoMessage(const QString &message);
    void showErrorMessage(const QString &message);

public Q_SLOTS:
    void getNewClicked();
    void installThemeFromFile(const QUrl &url);
    void removeTheme(int row);

private Q_SLOTS:
    void selectionChanged();
    /** Updates the size combo box. It loads the size list of the selected cursor
        theme with the corresponding icons and chooses an appropriate entry. It
        enables the combo box and the label if the theme provides more than one
        size, otherwise it disables it. If the size setting is looked in kiosk
        mode, it stays always disabled. */
    void updateSizeComboBox();


private:
    QModelIndex selectedIndex() const;
    void installThemeFile(const QString &path);
    /** Applies a given theme, using XFixes, XCursor and KGlobalSettings.
        @param theme The cursor theme to be applied. It is save to pass 0 here
            (will result in \e false as return value).
        @param size The size hint that is used to select the cursor size.
        @returns If the changes could be applied. Will return \e false if \e theme is
            0 or if the XFixes and XCursor libraries aren't available in the required
            version, otherwise returns \e true. */
    bool applyTheme(const CursorTheme *theme, const int size);
    bool iconsIsWritable() const;


    CursorThemeModel *m_model;
    SortProxyModel *m_proxyModel;
    QStandardItemModel *m_sizesModel;

    int m_appliedSize;
    // This index refers to the CursorThemeModel, not the proxy or the view
    QPersistentModelIndex m_appliedIndex;

/** Holds the last size that was chosen by the user. Example: The user chooses
    theme1 which provides the sizes 24 and 36. He chooses 36. preferredSize gets
    set to 36. Now, he switches to theme2 which provides the sizes 30 and 40.
    preferredSize still is 36, so the UI will default to 40, which is next to 36.
    Now, he chooses theme3 which provides the sizes 34 and 44. preferredSize is
    still 36, so the UI defaults to 34. Now the user changes manually to 44. This
    will also change preferredSize. */
    int m_preferredSize;
    int m_originalPreferredSize;

    int m_selectedThemeRow;
    int m_selectedSizeRow;
    int m_originalSelectedThemeRow;
    bool m_canInstall;
    bool m_canResize;
    bool m_canConfigure;

    QScopedPointer<QTemporaryFile> m_tempInstallFile;
    QPointer<KIO::FileCopyJob> m_tempCopyJob;
};

#endif
