summaryrefslogtreecommitdiffstats
path: root/reader/src/network/litres/LitResBooksFeedItem.cpp
blob: b5972554d479156915bd5d0a6be9409b2a1cbbbc (plain)
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
/*
 * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <algorithm>

#include <ZLNetworkManager.h>
#include <ZLNetworkUtil.h>
#include <ZLStringUtil.h>

#include "../NetworkLink.h"
#include "../NetworkComparators.h"
#include "../NetworkErrors.h"
#include "../NetworkItems.h"

#include "LitResUtil.h"
#include "LitResBooksFeedParser.h"

#include "LitResBooksFeedItem.h"

LitResBooksFeedItem::LitResBooksFeedItem(
	bool shouldSort,
	const NetworkLink &link,
	const std::string &title,
	const std::string &summary,
	const UrlInfoCollection &urlByType,
	AccessibilityType accessibility,
	int flags
) : NetworkCatalogItem(
	link,
	title,
	summary,
	urlByType,
	accessibility,
	flags
), myShouldSort(shouldSort) {

}

void LitResBooksFeedItem::onDisplayItem() {
}

class LitResBooksFeedItemRunnable : public ZLNetworkRequest::Listener {
public:
	LitResBooksFeedItemRunnable(LitResBooksFeedItem &item, shared_ptr<ZLNetworkRequest> request, NetworkItem::List &children, shared_ptr<ZLNetworkRequest::Listener> listener) :
		myItem(item), myChildren(children), myListener(listener) {
		request->setListener(this);
		ZLNetworkManager::Instance().performAsync(request);
	}

	void finished(const std::string &error) {
		if (error.empty()) {
			++myItem.myLoadingState.CurrentPage;
			if (myItem.myShouldSort) {
				std::sort(myChildren.begin(), myChildren.end(), NetworkBookItemComparator());
			}
		}
		myListener->finished(error);
	}
private:
	LitResBooksFeedItem &myItem;
	NetworkItem::List &myChildren;
	shared_ptr<ZLNetworkRequest::Listener> myListener;
};


std::string LitResBooksFeedItem::loadChildren(NetworkItem::List &children, shared_ptr<ZLNetworkRequest::Listener> listener) {
//	//TODO maybe add sid parameter if possible
//	//(at LitRes API documentation it said that's adding sid _always_ is a good practice)

	myLoadingState.CurrentPage = 0;
	myLoadingState.AllPagesCount = 1;

	shared_ptr<ZLNetworkRequest> request = getRequest(children);
	new LitResBooksFeedItemRunnable(*this, request, children, listener);
	return std::string();
}

bool LitResBooksFeedItem::supportsResumeLoading() {
	return true;
}

std::string LitResBooksFeedItem::resumeLoading(NetworkItem::List &children, shared_ptr<ZLNetworkRequest::Listener> listener) {
	shared_ptr<ZLNetworkRequest> request = getRequest(children);
	if (request.isNull()) {
		listener->finished();
		return std::string();
	}
	new LitResBooksFeedItemRunnable(*this, request, children, listener);
	return std::string();
}

shared_ptr<ZLNetworkRequest> LitResBooksFeedItem::getRequest(NetworkItem::List &children) {
	if (myLoadingState.CurrentPage >= myLoadingState.AllPagesCount) {
		return 0;
	}
	return ZLNetworkManager::Instance().createXMLParserRequest(
		withLimitParameters(getCatalogUrl(), myLoadingState),
		new LitResBooksFeedParser(Link, children, &myLoadingState)
	);
}

std::string LitResBooksFeedItem::withLimitParameters(std::string query, const LoadingState &state) {
	static const unsigned int ITEMS_PER_PAGE = 40;
	unsigned int startItemNumber = (unsigned int)state.CurrentPage * ITEMS_PER_PAGE;
	std::string params;
	ZLStringUtil::appendNumber(params, startItemNumber);
	params += ",";
	ZLStringUtil::appendNumber(params, ITEMS_PER_PAGE);
	ZLNetworkUtil::appendParameter(query, "limit", params);
	return query;
}