summaryrefslogtreecommitdiff
path: root/navit/gui/qml/searchProxy.h
blob: 3d76dc0dd3aa800999c5eb6ce5fba109f10c93b1 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#ifndef NAVIT_GUI_QML_SEARCHPROXY_H
#define NAVIT_GUI_QML_SEARCHPROXY_H

void __setNewPoint(struct gui_priv* this_,struct pcoord* pc, NGQPointTypes type);

class NGQProxySearch : public NGQProxy {
	Q_OBJECT;

	Q_PROPERTY(QString countryName READ countryName WRITE setCountryName NOTIFY countryNameSignal);
	Q_PROPERTY(QString countryISO2 READ countryISO2 WRITE setCountryISO2 NOTIFY countryISO2Signal);
	Q_PROPERTY(QString townName READ townName WRITE setTownName NOTIFY townNameSignal);
	Q_PROPERTY(QString streetName READ streetName WRITE setStreetName NOTIFY streetNameSignal);

	Q_PROPERTY(QString searchContext READ searchContext WRITE setSearchContext);

public:
	NGQProxySearch(struct gui_priv* this_,QObject* parent) : NGQProxy(this_,parent) {
		struct attr search_attr, country_name, country_iso2, *country_attr;
		struct item *item;
		struct country_search *cs;
		struct tracking *tracking;
		struct search_list_result *res;

		this->sl=search_list_new(navit_get_mapset(this->object->nav));
		this->search_context="country";

		country_attr=country_default();
		tracking=navit_get_tracking(this->object->nav);
		if (tracking && tracking_get_attr(tracking, attr_country_id, &search_attr, NULL))
			country_attr=&search_attr;
		if (country_attr) {
			cs=country_search_new(country_attr, 0);
			item=country_search_get_item(cs);
			if (item && item_attr_get(item, attr_country_name, &country_name)) {
				search_attr.type=attr_country_all;
				dbg(lvl_debug,"country %s", country_name.u.str);
				this->country_name=QString::fromLocal8Bit(country_name.u.str);
				search_attr.u.str=country_name.u.str;
				search_list_search(this->sl, &search_attr, 0);
				while((res=search_list_get_result(this->sl)));
				if (item_attr_get(item, attr_country_iso2, &country_iso2)) {
					this->country_iso2=QString::fromLocal8Bit(country_iso2.u.str);
				}
			}
			country_search_destroy(cs);
		} else {
			dbg(lvl_error,"warning: no default country found");
			if (!this->country_iso2.isEmpty()) {
				dbg(lvl_debug,"attempting to use country '%s'",this->country_iso2.toStdString().c_str());
				search_attr.type=attr_country_iso2;
				search_attr.u.str=(char*)this->country_iso2.toStdString().c_str();
				search_list_search(this->sl, &search_attr, 0);
				while((res=search_list_get_result(this->sl)));
			}
		}
	}
	~NGQProxySearch() {
		search_list_destroy(this->sl);
	}

signals:
	void countryNameSignal(QString);
	void countryISO2Signal(QString);
	void townNameSignal(QString);
	void streetNameSignal(QString);

public slots:
	void setPointToResult() {
		struct attr attr;
		struct search_list_result *res;

		if (this->street_name.length()>0) {
			attr.type=attr_street_name;
			attr.u.str=this->street_name.toLocal8Bit().data();
		} else if (this->town_name.length()>0) {
			attr.type=attr_town_or_district_name;
			attr.u.str=this->town_name.toLocal8Bit().data();
		} else if (this->country_name.length()>0) {
			attr.type=attr_country_name;
			attr.u.str=this->country_name.toLocal8Bit().data();
		}
		search_list_search(this->sl,&attr,0);
		if ((res=search_list_get_result(this->sl))) {
			__setNewPoint(this->object,res->c,PointOfInterest);
		}
		return;
	}
	QString searchXml() {
		NGQStandardItemModel* ret=new NGQStandardItemModel(this);
		struct attr attr;
		struct search_list_result *res;
		int counter=0;
		QDomDocument retDoc;
		QDomElement entries;

		entries=retDoc.createElement("search");
		retDoc.appendChild(entries);

		if (this->search_context=="country") {
			attr.type=attr_country_name;
			attr.u.str=this->country_name.toLocal8Bit().data();
		}
		if (this->search_context=="town") {
			if (this->town_name.length()<3) {
				return retDoc.toString();
			}
			attr.type=attr_town_or_district_name;
			attr.u.str=this->town_name.toLocal8Bit().data();
		}
		if (this->search_context=="street") {
			attr.type=attr_street_name;
			attr.u.str=this->street_name.toLocal8Bit().data();
		}

		search_list_search(this->sl,&attr,1);

		while ((res=search_list_get_result(this->sl))) {
			QStandardItem* curItem=new QStandardItem();
			QDomElement entry=retDoc.createElement("item");
			entries.appendChild(entry);
			//Result processing depends on search type
			if (this->search_context=="country") {
					entry.appendChild(this->_fieldValueHelper(retDoc,QString("id"), QString::number(counter)));
					entry.appendChild(this->_fieldValueHelper(retDoc,QString("name"), QString::fromLocal8Bit(res->country->name)));
					entry.appendChild(this->_fieldValueHelper(retDoc,QString("icon"), QString("country_%1%2").arg(res->country->iso2).arg(".svgz")));
			}
			if (this->search_context=="town") {
				entry.appendChild(this->_fieldValueHelper(retDoc,QString("id"), QString::number(counter)));
				if (res->town->common.town_name) {
					entry.appendChild(this->_fieldValueHelper(retDoc,QString("name"),QString::fromLocal8Bit(res->town->common.town_name)));
				}
				if (res->town->common.district_name) {
					entry.appendChild(this->_fieldValueHelper(retDoc,QString("name"), QString::fromLocal8Bit(res->town->common.district_name)));
				}
			}
			if (this->search_context=="street") {
				entry.appendChild(this->_fieldValueHelper(retDoc,QString("id"), QString::number(counter)));
				entry.appendChild(this->_fieldValueHelper(retDoc,QString("name"),QString::fromLocal8Bit(res->street->name)));
			}
			counter++;
			ret->appendRow(curItem);
		}

		return retDoc.toString();
	}
	QString countryName() {
		return this->country_name;
	}
	void setCountryName(QString countryName) {
		this->country_name=countryName;
		struct attr attr;
		struct search_list_result *res;

		//We need to update ISO2
		attr.type=attr_country_name;
		attr.u.str=countryName.toLocal8Bit().data();
		search_list_search(this->sl,&attr,0);
		while ((res=search_list_get_result(this->sl))) {
				this->setCountryISO2(QString::fromLocal8Bit(res->country->iso2));
		}
		//...and current town
		this->town_name="";
		this->street_name="";

		countryNameSignal(countryName);
	}
	QString countryISO2() {
		return this->country_iso2;
	}
	void setCountryISO2(QString countryISO2) {
		this->country_iso2=countryISO2;
		countryISO2Signal(countryISO2);
	}
	QString townName() {
		return this->town_name;
	}
	void setTownName(QString townName) {
		struct attr attr;

		this->town_name=townName;

		//Specialize search
		attr.type=attr_town_or_district_name;
		attr.u.str=townName.toLocal8Bit().data();
		search_list_search(this->sl,&attr,0);

		//...and street
		this->street_name="";

		townNameSignal(townName);
	}
	QString streetName() {
		return this->street_name;
	}
	void setStreetName(QString streetName) {
		struct attr attr;

		this->street_name=streetName;

		//Specialize search
		attr.type=attr_street_name;
		attr.u.str=streetName.toLocal8Bit().data();
		search_list_search(this->sl,&attr,0);

		streetNameSignal(streetName);
	}
	QString searchContext() {
		return this->search_context;
	}
	void setSearchContext(QString searchContext) {
		this->search_context=searchContext;
	}

protected:
	virtual int getAttrFunc(enum attr_type type, struct attr *attr, struct attr_iter *iter) {
		return 0;
	}
	virtual int setAttrFunc(struct attr *attr) {
		return 0;
	}
private:
	struct search_list *sl;
	QString search_context;
	QString country_iso2,country_name,town_name,street_name;
};

#include "searchProxy.moc"

#endif /* NAVIT_GUI_QML_SEARCHPROXY_H */