1 /*
2  * Copyright 2011 kubtek <kubtek@mail.com>
3  *
4  * This file is part of StarDict.
5  *
6  * StarDict is free software: you can redistribute it and/or modify
7  * it 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.
10  *
11  * StarDict is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with StarDict.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <algorithm>
21 #include <glib.h>
22 #include "compositelookup.h"
23 
CompositeLookup(void)24 CompositeLookup::CompositeLookup(void)
25 :
26 BuildingLookup(false),
27 StarDictNetSeq(0)
28 {
29 }
30 
new_lookup(void)31 void CompositeLookup::new_lookup(void)
32 {
33 	BuildingLookup = true;
34 	NetDictRequests.clear();
35 	StarDictNetSeq = 0;
36 }
37 
done_lookup(void)38 void CompositeLookup::done_lookup(void)
39 {
40 	BuildingLookup = false;
41 }
42 
is_got_all_responses(void) const43 bool CompositeLookup::is_got_all_responses(void) const
44 {
45 	if(BuildingLookup)
46 		return false;
47 	return StarDictNetSeq == 0 && NetDictRequests.empty();
48 }
49 
send_net_dict_request(const std::string & dict_id,const std::string & key)50 void CompositeLookup::send_net_dict_request(const std::string& dict_id, const std::string& key)
51 {
52 	NetDictRequest request(dict_id, key);
53 	//g_assert(NetDictRequests.end() == std::find(NetDictRequests.begin(), NetDictRequests.end(), request));
54 	//NetDictRequests.push_back(request);
55 	if (NetDictRequests.end() == std::find(NetDictRequests.begin(), NetDictRequests.end(), request)) {
56 		NetDictRequests.push_back(request);
57 	}
58 }
59 
60 /* returns true if got expected response */
got_net_dict_responce(const std::string & dict_id,const std::string & key)61 bool CompositeLookup::got_net_dict_responce(const std::string& dict_id, const std::string& key)
62 {
63 	NetDictRequest response(dict_id, key);
64 	NetDictRequestsList::iterator it = std::find(NetDictRequests.begin(), NetDictRequests.end(), response);
65 	if(it != NetDictRequests.end()) {
66 		NetDictRequests.erase(it);
67 		return true;
68 	}
69 	return false;
70 }
71 
send_StarDict_net_request(unsigned int seq)72 void CompositeLookup::send_StarDict_net_request(unsigned int seq)
73 {
74 	StarDictNetSeq = seq;
75 }
76 
77 /* returns true if got expected response */
got_StarDict_net_responce(unsigned int seq)78 bool CompositeLookup::got_StarDict_net_responce(unsigned int seq)
79 {
80 	if(StarDictNetSeq == 0)
81 		return false;
82 	if(seq == StarDictNetSeq) {
83 		StarDictNetSeq = 0;
84 		return true;
85 	}
86 	return false;
87 }
88