1 /*
2  * Copyright (C) 2009-2010 Geometer Plus <contact@geometerplus.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 #include "NetworkNodesFactory.h"
21 
22 #include "NetworkNodes.h"
23 
24 #include "../network/NetworkBookCollection.h"
25 
createNetworkNode(NetworkCatalogNode * parent,shared_ptr<NetworkItem> item,size_t atPosition)26 FBReaderNode *NetworkNodesFactory::createNetworkNode(NetworkCatalogNode *parent, shared_ptr<NetworkItem> item, size_t atPosition) {
27 	if (item->isInstanceOf(NetworkCatalogItem::TYPE_ID)) {
28 		NetworkCatalogNode *ptr = new NetworkCatalogNode(parent, item, atPosition);
29 		ptr->item().onDisplayItem();
30 		return ptr;
31 	} else if (item->isInstanceOf(NetworkBookItem::TYPE_ID)) {
32 		return new NetworkBookNode(parent, item, NetworkBookNode::AUTHORS);
33 	}
34 	return 0;
35 }
36 
createSubnodes(SearchResultNode * parent,NetworkBookCollection & books)37 void NetworkNodesFactory::createSubnodes(SearchResultNode *parent, NetworkBookCollection &books) {
38 	const NetworkAuthorBooksMap &map = books.authorBooksMap();
39 	for (NetworkAuthorBooksMap::const_iterator it = map.begin(); it != map.end(); ++it) {
40 		fillAuthorNode(
41 			new NetworkAuthorNode(parent, it->first),
42 			it->second
43 		);
44 	}
45 }
46 
fillAuthorNode(NetworkContainerNode * parent,const NetworkItem::List & books)47 void NetworkNodesFactory::fillAuthorNode(NetworkContainerNode *parent, const NetworkItem::List &books) {
48 	NetworkSeriesNode *seriesNode = 0;
49 
50 	NetworkSeriesNode::SummaryType seriesSummaryType = NetworkSeriesNode::AUTHORS;
51 	NetworkBookNode::SummaryType booksSummaryType = NetworkBookNode::AUTHORS;
52 	if ((parent->isInstanceOf(NetworkCatalogNode::TYPE_ID) &&
53 			((NetworkCatalogNode*)parent)->item().Type == NetworkCatalogItem::BY_AUTHORS) ||
54 			 parent->isInstanceOf(NetworkAuthorNode::TYPE_ID)) {
55 		seriesSummaryType = NetworkSeriesNode::BOOKS;
56 		booksSummaryType = NetworkBookNode::NONE;
57 	}
58 
59 	for (NetworkItem::List::const_iterator it = books.begin(); it != books.end(); ++it) {
60 		if (!(*it)->isInstanceOf(NetworkBookItem::TYPE_ID)) {
61 			continue;
62 		}
63 		const NetworkBookItem &book = (const NetworkBookItem &) **it;
64 		std::string seriesTitle = book.SeriesTitle;
65 
66 		if (!seriesTitle.empty() && (seriesNode == 0 || seriesNode->title() != seriesTitle)) {
67 			NetworkItem::List::const_iterator jt = it + 1;
68 			while (jt != books.end() && !(*jt)->isInstanceOf(NetworkBookItem::TYPE_ID)) {
69 				++jt;
70 			}
71 			if (jt == books.end()) {
72 				seriesTitle.clear();
73 			} else {
74 				const NetworkBookItem &next = (const NetworkBookItem&)**jt;
75 				if (next.SeriesTitle != seriesTitle) {
76 					seriesTitle.clear();
77 				}
78 			}
79 		}
80 
81 		if (seriesTitle.empty()) {
82 			seriesNode = 0;
83 			new NetworkBookNode(parent, *it, booksSummaryType);
84 		} else {
85 			if (seriesNode == 0 || seriesNode->title() != seriesTitle) {
86 				seriesNode = new NetworkSeriesNode(parent, seriesTitle, seriesSummaryType);
87 			}
88 			new NetworkBookNode(seriesNode, *it, booksSummaryType);
89 		}
90 	}
91 }
92