-
Notifications
You must be signed in to change notification settings - Fork 0
/
knotsdirectory.cpp
executable file
·208 lines (154 loc) · 4.89 KB
/
knotsdirectory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <qdebug.h>
#include "knotsdirectory.h"
#include "knots.h"
KnotsDirectory::~KnotsDirectory()
{
delete _xmlReader;
delete _xmlSource;
delete _parser;
}
KnotsDirectory::KnotsDirectory( QObject *parent )
: QAbstractListModel( parent )
, _xmlReader(0)
, _xmlSource(0)
, _currentDownload(0)
, _parser(0)
{
_roles[IdRole] = "id";
_roles[NameRole] = "name";
_roles[ImageRole] = "thumbnail";
_roles[WidthRole] = "width";
_roles[HeightRole] = "height";
_roles[ViewsRole] = "views";
_roles[AddedRole] = "addedOn";
_roles[DurationRole] = "duration";
_roles[LastViewedPositionRole] = "lastposition";
_roles[MediaIdRole] = "mediaId";
setRoleNames(_roles);
_parser = new SaxKnotsItemHandler();
_parser->setItemHandler(this);
connect(&Knots::instance().serverConnection(), SIGNAL(finished(QNetworkReply*)),SLOT(onDirectoryFetchFinished(QNetworkReply*)));
}
void KnotsDirectory::onDirectoryFetchFinished( QNetworkReply* reply )
{
if( reply == _currentDownload )
{
// qWarning() << "Fetched from " << reply->url() ;
// qWarning() << "Read " << reply->bytesAvailable() << " Bytes";
// qWarning() << reply->peek( 4096 );
_xmlReader->parse(_xmlSource );
_currentDownload->deleteLater();
emit directoryChanged();
QUrl request( _basePath );
if( ++_currentPage < _totalPages )
{
request.addQueryItem("page", QString::number(_currentPage ));
loadPath( request );
}
}
}
void KnotsDirectory::browseByPath( QString &pathToLoad )
{
_basePath = pathToLoad;
_currentPage = 0;
_totalPages = 1;
beginRemoveRows(createIndex(0,0), 0, _items.count());
qDeleteAll(_items.begin(), _items.end());
_items.clear();
endRemoveRows();
reset();
loadPath( _basePath );
}
void KnotsDirectory::loadPath(QUrl &pathToLoad)
{
try {
QNetworkRequest request( pathToLoad );
_currentDownload = Knots::instance().serverConnection().get(request);
_xmlSource = new QXmlInputSource( _currentDownload );
_xmlReader = new QXmlSimpleReader();
_xmlReader->setContentHandler(_parser);
}
catch(...)
{
qWarning() << "Failed to fetch path";
}
}
int KnotsDirectory::getCurrentPage() const
{
return _currentPage;
}
int KnotsDirectory::getTotalPages() const
{
return _totalPages;
}
void KnotsDirectory::handlePages( int totalPages, int currentPage)
{
_currentPage = currentPage;
_totalPages = totalPages;
}
void KnotsDirectory::handleNewItem(KnotsItem* newItem )
{
QModelIndex parent;
int start = _items.count();
beginInsertRows(parent, start, start+1);
_items.append(newItem);
newItem->_modelIndex = start;
endInsertRows();
emit dataChanged(createIndex(start,0,0),createIndex(start+1,1,0));
}
int KnotsDirectory::rowCount ( const QModelIndex & /*parent*/) const
{
int count = 0;
count = _items.count();
//qDebug() << "Directory has " << count << " entries";
return count;
}
QVariant KnotsDirectory::data ( const QModelIndex & index, int role ) const
{
if (!index.isValid())
return QVariant(); // Return Null variant if index is invalid
if (index.row() > (_items.count()) )
return QVariant();
KnotsItem *dobj = _items.at(index.row());
if( dobj )
{
//qDebug() << "Qml asking item " << index.row() << "Name: " << dobj->getText() << "Id: " << dobj->getId() << "thumbnail" << dobj->getItemImage();
switch (role) {
case Qt::DisplayRole: // The default display role now displays the first name as well
case IdRole:
return QVariant::fromValue(dobj->_modelIndex);
case NameRole:
return QVariant::fromValue(dobj->getText());
case ImageRole:
return QVariant::fromValue(dobj->getItemImage().toString());
case WidthRole:
return QVariant::fromValue(dobj->getFields()["width"]);
case HeightRole:
return QVariant::fromValue(dobj->getFields()["height"]);
case ViewsRole:
return QVariant::fromValue(dobj->getFields()["views"]);
case AddedRole:
return QVariant::fromValue(dobj->getFields()["added"]);
case DurationRole:
return QVariant::fromValue(dobj->getFields()["duration"]);
case LastViewedPositionRole:
return QVariant::fromValue(dobj->getFields()["position"]);
case MediaIdRole:
return QVariant::fromValue(dobj->getId());
default:
return QVariant();
}
} else {
return QVariant();
}
}
QString KnotsDirectory::itemSelected( QString itemId )
{
QString newState = "Browsing";
KnotsItem* item = 0;
item = _items.at(itemId.toInt());
newState = item->itemSelected();
return newState;
}