No longer honours following status in JSON, instead relies solely on following list.
[larjonas-pumpa.git] / src / qasabstractobjectlist.cpp
blob15d333f310eeca5bb2d10245e5fb4ae713b498da
1 /*
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"
22 #include "util.h"
24 #include <QUrl>
25 #include <QDebug>
27 //------------------------------------------------------------------------------
29 QASAbstractObjectList::QASAbstractObjectList(int asType, QString url,
30 QObject* parent) :
31 QASAbstractObject(asType, parent),
32 m_url(url),
33 m_totalItems(0),
34 m_hasMore(false),
35 m_firstTime(true)
38 //------------------------------------------------------------------------------
40 void QASAbstractObjectList::update(QVariantMap json, bool older,
41 bool updateOnly) {
42 #ifdef DEBUG_QAS
43 qDebug() << "updating AbstractObjectList" << m_url;
44 #endif
46 bool ch = false;
47 bool dummy = false;
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
54 // stuff, so:
55 // next => older
56 // prev => newer
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.
68 if (!updateOnly) {
69 if (older || m_firstTime) {
70 m_nextLink = ""; // it's left as empty if it doesn't exist in the
71 // json
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(),
94 parent());
96 if (!obj || updateOnly || m_item_set.contains(obj))
97 continue;
99 m_items.insert(mi, obj);
100 m_item_set.insert(obj);
101 // connectSignals(obj, false, true);
103 ch = 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
111 // hack indeed :)
112 m_hasMore = !json.contains("displayName") && size() < m_totalItems;
114 m_firstTime = false;
115 if (ch)
116 emit changed();
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))
129 return;
131 #ifdef DEBUG_QAS
132 qDebug() << "addObject" << obj->apiLink();
133 #endif
135 m_items.append(obj);
136 m_item_set.insert(obj);
138 // m_totalItems++;
139 emit changed();
142 //------------------------------------------------------------------------------
144 void QASAbstractObjectList::removeObject(QASAbstractObject* obj, bool signal) {
145 #ifdef DEBUG_QAS
146 qDebug() << "removeObject" << obj->apiLink();
147 #endif
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) {
157 if (updateNextLink)
158 m_nextLink = m_url + "?before=" +
159 QUrl::toPercentEncoding(m_items.last()->apiLink());
161 if (updatePrevLink)
162 m_prevLink = m_url + "?since=" +
163 QUrl::toPercentEncoding(m_items.first()->apiLink());
165 // m_totalItems--;
166 if (signal)
167 emit changed();