Initial commit for the QML client:
[qtrapids] / src / client / models / QDeclarativeDownloadListModel.h
1 /***************************************************************************
2  *   Copyright (C) 2010 by Ixonos Plc   *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; version 2 of the License.               *
7  *                                                                         *
8  *   This program is distributed in the hope that it will be useful,       *
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
11  *   GNU General Public License for more details.                          *
12  *                                                                         *
13  *   You should have received a copy of the GNU General Public License     *
14  *   along with this program; if not, write to the                         *
15  *   Free Software Foundation, Inc.,                                       *
16  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
17  ***************************************************************************/
18 #ifndef QDECLARATIVEDOWNLOADLISTMODEL_H
19 #define QDECLARATIVEDOWNLOADLISTMODEL_H
20
21
22 #include <QDeclarativeParserStatus>
23 #include <QAbstractListModel>
24 #include <QtDeclarative>
25 #include <QModelIndex>
26
27 #include "qtrapids/dbus.hpp"
28 #include "qtrapids/info.hpp"
29
30 //#include "../DownloadView.h"
31
32 QT_MODULE(Declarative);
33
34 namespace qtrapids
35 {
36
37 typedef QHash<QString, TorrentState> DownloadItems_t;
38 typedef QMap<int, QString> ItemIndex_t;
39
40 class QDeclarativeContext;
41 class QDeclarativeDownloadListModelPrivate;
42
43 class QDeclarativeDownloadListModel :
44             public QAbstractListModel, public QDeclarativeParserStatus
45 {
46     Q_OBJECT
47     Q_INTERFACES(QDeclarativeParserStatus);
48
49     /*
50     Q_PROPERTY(QString hash READ hash);
51     Q_PROPERTY(QString name READ name WRITE setName);
52     Q_PROPERTY(QString size READ size);
53     Q_PROPERTY(QString status READ status);
54     Q_PROPERTY(uint state READ state);
55     Q_PROPERTY(uint progress READ progress);
56     Q_PROPERTY(uint downRate READ downRate);
57     Q_PROPERTY(uint upRate READ upRate);
58     Q_PROPERTY(uint seeds READ seeds);
59     Q_PROPERTY(uint leeches READ leeches);
60     Q_PROPERTY(qulonglong ratio READ ratio);
61     Q_PROPERTY(qulonglong totaSize READ totalDone);
62     Q_PROPERTY(qulonglong totalDone READ totalDone);
63 */
64
65 public:
66     QDeclarativeDownloadListModel(QObject *parent = 0);
67     virtual ~QDeclarativeDownloadListModel();
68
69     // TODO: Learn to use roles..
70     enum Roles {
71         DownloadRole = Qt::UserRole + 1, 
72         SeedRole,
73         HashRole,
74         NameRole,
75         SizeRole,
76         StatusRole,
77         StateRole,
78         ProgressRole,
79         DownRateRole,
80         UpRateRole,
81         SeedsRole,
82         LeechesRole,
83         RatioRole,
84         TotaSizeRole,
85         TotalDoneRole
86     };
87
88     // Updates the torrent status in the model.
89     void updateItem(TorrentState const& info, ParamsMap_t other_info);
90     // Returns the hash of the torrent be removed.
91     QString prepareRemoveSelected();
92
93     // -- QAbstractListModel
94     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
95     virtual QVariant data(const QModelIndex &index, int role) const;
96     virtual bool insertRow(int row, const QModelIndex &parent);
97     virtual bool setData (const QModelIndex & index, const QVariant &value, int role = Qt::EditRole);
98     // -- End QAbstractListModel
99
100     // -- Overrides from QDeclarativeParserStatus
101     virtual void classBegin();
102     virtual void componentComplete();
103     // -- End QDeclarativeParserStatus
104
105 Q_SIGNALS:
106     void downloadItemChanged(QString hash);
107
108 // Private functions.
109 private:
110     Q_DISABLE_COPY(QDeclarativeDownloadListModel)
111     QDeclarativeDownloadListModelPrivate *d;
112
113     void addItem_(TorrentState const& info, ParamsMap_t other_info);
114     void updateItem_(TorrentState const& info, ParamsMap_t other_info);
115     void removeItem_(TorrentState const& info);
116     QString GetStatusString(TorrentStatus::Id status);
117 private:
118
119 };
120
121
122 } // namespace qtrapids
123
124 // We need to declare the type
125 QML_DECLARE_TYPE(qtrapids::QDeclarativeDownloadListModel);
126
127
128
129 #endif