2 Copyright 2013-2015 Mats Sjöberg
4 This file is part of the Pumpa programme.
6 Pumpa is free software: you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Pumpa is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Pumpa. If not, see <http://www.gnu.org/licenses/>.
20 #include "qasabstractobjectlist.h"
27 //------------------------------------------------------------------------------
29 QASAbstractObjectList::QASAbstractObjectList(int asType
, QString url
,
31 QASAbstractObject(asType
, parent
),
38 //------------------------------------------------------------------------------
40 void QASAbstractObjectList::update(QVariantMap json
, bool older
,
43 qDebug() << "updating AbstractObjectList" << m_url
;
49 updateVar(json
, m_displayName
, "displayName", ch
);
50 updateVar(json
, m_totalItems
, "totalItems", ch
, true);
51 updateVar(json
, m_proxyUrl
, "pump_io", "proxyURL", ch
);
53 // In pump.io the next link goes "next" in the UI, i.e. to older
58 // If we are loading older stuff, update only the next link. If it
59 // is empty it means we have reached the oldest stuff.
61 // If we are loading newer stuff, update only the prev link, except
62 // if it is empty, then don't touch it. (That means there's no newer
63 // stuff, but there's bound to be more later :-)
65 // And a special case is when we load it the first time, then both
66 // next and prev links should be updated.
69 if (older
|| m_firstTime
) {
70 m_nextLink
= ""; // it's left as empty if it doesn't exist in the
72 updateVar(json
, m_nextLink
, "links", "next", "href", dummy
);
73 // if (m_url.contains("/inbox/"))
74 // qDebug() << "***" << m_url << "next" << m_nextLink;
76 if (!older
|| m_firstTime
) {
77 // updateVar doesn't touch it if it is empty in the json
78 updateVar(json
, m_prevLink
, "links", "prev", "href", dummy
);
79 // if (m_url.contains("/inbox/"))
80 // qDebug() << "***" << m_url << "prev" << m_prevLink;
84 // Items need to be processed chronologically. We assume that
85 // collections come in as newest first, so we need to start
86 // processing them from the end.
88 // Start adding from the top or bottom, depending on value of older.
89 int mi
= older
? m_items
.size() : 0;
91 QVariantList items_json
= json
["items"].toList();
92 for (int i
=items_json
.count()-1; i
>=0; --i
) {
93 QASAbstractObject
* obj
= getAbstractObject(items_json
.at(i
).toMap(),
96 if (!obj
|| updateOnly
|| m_item_set
.contains(obj
))
99 m_items
.insert(mi
, obj
);
100 m_item_set
.insert(obj
);
101 // connectSignals(obj, false, true);
106 // In theory, there should be more to be fetched if size <
107 // totalItems. Sometimes those missing items still do not appear in
108 // the fetched list, and we will have a perpetual "load more"
109 // button. It turns out that the fetched replies lists has a
110 // displayName, while the short one has not... this is a very ugly
112 m_hasMore
= !json
.contains("displayName") && size() < m_totalItems
;
119 //------------------------------------------------------------------------------
121 QString
QASAbstractObjectList::urlOrProxy() const {
122 return m_proxyUrl
.isEmpty() ? m_url
: m_proxyUrl
;
125 //------------------------------------------------------------------------------
127 void QASAbstractObjectList::addObject(QASAbstractObject
* obj
) {
128 if (m_item_set
.contains(obj
))
132 qDebug() << "addObject" << obj
->apiLink();
136 m_item_set
.insert(obj
);
142 //------------------------------------------------------------------------------
144 void QASAbstractObjectList::removeObject(QASAbstractObject
* obj
, bool signal
) {
146 qDebug() << "removeObject" << obj
->apiLink();
149 int idx
= m_items
.indexOf(obj
);
150 bool updatePrevLink
= idx
== 0;
151 bool updateNextLink
= idx
== m_items
.count()-1;
153 m_items
.removeAt(idx
);
154 m_item_set
.remove(obj
);
156 if (m_items
.count() > 0) {
158 m_nextLink
= m_url
+ "?before=" +
159 QUrl::toPercentEncoding(m_items
.last()->apiLink());
162 m_prevLink
= m_url
+ "?since=" +
163 QUrl::toPercentEncoding(m_items
.first()->apiLink());