Very rough initial implementation of torrent adding working.
[qtrapids] / src / qml-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
35 class QDeclarativeContext;
36
37 namespace qtrapids
38 {
39
40 typedef QHash<QString, TorrentState> DownloadItems_t;
41 typedef QMap<int, QString> ItemIndex_t;
42
43 class QDeclarativeDownloadListModelPrivate;
44
45 class QDeclarativeDownloadListModel :
46             public QAbstractListModel, public QDeclarativeParserStatus
47 {
48     Q_OBJECT
49     Q_INTERFACES(QDeclarativeParserStatus);
50
51     /*
52     Q_PROPERTY(QString hash READ hash);
53     Q_PROPERTY(QString name READ name WRITE setName);
54     Q_PROPERTY(QString size READ size);
55     Q_PROPERTY(QString status READ status);
56     Q_PROPERTY(uint state READ state);
57     Q_PROPERTY(uint progress READ progress);
58     Q_PROPERTY(uint downRate READ downRate);
59     Q_PROPERTY(uint upRate READ upRate);
60     Q_PROPERTY(uint seeds READ seeds);
61     Q_PROPERTY(uint leeches READ leeches);
62     Q_PROPERTY(qulonglong ratio READ ratio);
63     Q_PROPERTY(qulonglong totaSize READ totalDone);
64     Q_PROPERTY(qulonglong totalDone READ totalDone);
65 */
66
67 public:
68     QDeclarativeDownloadListModel(QObject *parent = 0);
69     virtual ~QDeclarativeDownloadListModel();
70
71     // TODO: Learn to use roles..
72     enum Roles {
73         DownloadRole = Qt::UserRole + 1, 
74         SeedRole,
75         HashRole,
76         NameRole,
77         SizeRole,
78         StatusRole,
79         StateRole,
80         ProgressRole,
81         DownRateRole,
82         UpRateRole,
83         SeedsRole,
84         LeechesRole,
85         RatioRole,
86         TotaSizeRole,
87         TotalDoneRole
88     };
89
90     // Updates the torrent status in the model.
91     void updateItem(TorrentState const& info, ParamsMap_t other_info);
92     // Returns the hash of the torrent be removed.
93     QString prepareRemoveSelected();
94
95     // -- QAbstractListModel
96     virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
97     virtual QVariant data(const QModelIndex &index, int role) const;
98     virtual bool insertRow(int row, const QModelIndex &parent);
99     virtual bool setData (const QModelIndex & index, const QVariant &value, int role = Qt::EditRole);
100     // -- End QAbstractListModel
101
102     // -- Overrides from QDeclarativeParserStatus
103     virtual void classBegin();
104     virtual void componentComplete();
105     // -- End QDeclarativeParserStatus
106
107 Q_SIGNALS:
108     void downloadItemChanged(QString hash);
109
110 // Private functions.
111 private:
112     Q_DISABLE_COPY(QDeclarativeDownloadListModel)
113     QDeclarativeDownloadListModelPrivate *d;
114
115     void addItem_(TorrentState const& info, ParamsMap_t other_info);
116     void updateItem_(TorrentState const& info, ParamsMap_t other_info);
117     void removeItem_(TorrentState const& info);
118     QString GetStatusString(TorrentStatus::Id status);
119 private:
120
121 };
122
123
124 } // namespace qtrapids
125
126 // We need to declare the type
127 QML_DECLARE_TYPE(qtrapids::QDeclarativeDownloadListModel);
128
129
130
131 #endif