summaryrefslogtreecommitdiffstats
path: root/reader/src/network/opds/OPDSCatalogItem.cpp
blob: 853bc4c5fcc5b0add7ae09e8bd71734653d77956 (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
/*
 * Copyright (C) 2009-2012 Geometer Plus <[email protected]>
 *
 * 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 <ZLNetworkRequest.h>
#include <ZLNetworkManager.h>

#include "OPDSCatalogItem.h"
#include "OPDSLink.h"
#include "OPDSXMLParser.h"
#include "NetworkOPDSFeedReader.h"

#include "../NetworkOperationData.h"

OPDSCatalogItem::OPDSCatalogItem(
	const OPDSLink &link,
	const std::string &title,
	const std::string &summary,
	const UrlInfoCollection &urlByType,
	AccessibilityType accessibility,
	int flags
		) : NetworkCatalogItem(link, title, summary, urlByType, accessibility, flags), myLoadingState(Link) {
}

class OPDSCatalogItemRunnable : public ZLNetworkRequest::Listener {
public:
	OPDSCatalogItemRunnable(shared_ptr<ZLNetworkRequest> request, NetworkItem::List &children, NetworkOperationData &data, shared_ptr<ZLNetworkRequest::Listener> listener) :
		myChildren(children), myLoadingData(data), myListener(listener) {
		request->setListener(this);
		ZLNetworkManager::Instance().performAsync(request);
	}
	void finished(const std::string &error) {
		myChildren.insert(myChildren.end(), myLoadingData.Items.begin(), myLoadingData.Items.end());
		myListener->finished(error);
	}
	void setUIStatus(bool enabled) {
		myListener->setUIStatus(enabled); //to hide refreshing while authentication dialog
	}

private:
	NetworkItem::List &myChildren;
	NetworkOperationData &myLoadingData;
	shared_ptr<ZLNetworkRequest::Listener> myListener;
};


std::string OPDSCatalogItem::loadChildren(NetworkItem::List &children, shared_ptr<ZLNetworkRequest::Listener> listener) {
	myLoadingState.clear();
	shared_ptr<ZLNetworkRequest> request = ((OPDSLink&)Link).createNetworkRequest(getCatalogUrl(), myLoadingState);
	new OPDSCatalogItemRunnable(request, children, myLoadingState, listener);
	return std::string();
}

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

std::string OPDSCatalogItem::resumeLoading(NetworkItem::List &children, shared_ptr<ZLNetworkRequest::Listener> listener) {
	shared_ptr<ZLNetworkRequest> request = myLoadingState.resume();
	if (request.isNull()) {
		listener->finished();
		return std::string();
	}
	new OPDSCatalogItemRunnable(request, children, myLoadingState, listener);
	return std::string();
}