9 #include <QCoreApplication>
11 #include <QMapIterator>
14 Locations* Locations::m_instance = 0;
16 Locations* Locations::GetInstance()
19 m_instance = new Locations();
24 /*void Locations::destroyLocations()
29 Locations::Locations() :
30 m_locationStorage(QHash<QString,Location *>()),
31 m_indexStorage(QList<QString>()),
32 m_gpsLocation(new GpsLocation())
34 this->restoreLocations();
35 qDebug() << "Size of index storage:" << this->m_indexStorage.size();
38 /*Locations::~Locations()
40 QHash<QString,Location*>::iterator it, ite;
41 for (it = this->m_locationStorage.begin(), ite = this->m_locationStorage.end(); it != ite; ++it)
45 this->m_locationStorage.empty();
49 bool Locations::addEditLocation(Location *location)
53 if ( !this->m_locationStorage.contains(location->label())) {
54 qDebug() << "Adding location" << location->label();
55 this->m_locationStorage[location->label()] = location;
56 qDebug() << "Index storage:";
57 qDebug() << this->m_indexStorage;
58 qDebug() << "Size of index storage:" << this->m_indexStorage.size();
59 this->m_indexStorage.append(location->label());
60 qDebug() << "Index storage after inserting location:";
61 qDebug() << this->m_indexStorage;
64 qDebug() << "A location with the same label (" << location->label() << ") already exists.";
65 this->m_locationStorage.remove(location->label());
66 this->m_locationStorage[location->label()] = location;
68 emit(locationsChanged());
70 // save the location to settings
71 this->saveLocation(location);
76 void Locations::restoreLocations()
80 settings.beginGroup("Locations");
81 QStringList labels = settings.childGroups();
83 QMap<QString, int> tempIndex = QMap<QString, int>();
85 for( int i=0; i<labels.size(); ++i ) {
86 QString label = labels[i];
87 settings.beginGroup(label);
88 QString address, x, y;
90 if (settings.contains("address")) {
91 address = settings.value( "address" ).toString();
92 if (settings.contains("x")) {
93 x = settings.value( "x" ).toString();
94 if (settings.contains("y")) {
95 y = settings.value( "y" ).toString();
100 int index = settings.value("index").toInt();
103 qDebug() << "Restoring " << label;
106 location = new Location( x, y, label, address );
107 this->m_locationStorage[label] = location;
108 this->m_indexStorage.append(label);
110 tempIndex.insert(label, index);
118 qDebug() << "Locations indexes before restoring positions";
119 qDebug() << this->m_indexStorage;
120 qDebug() << "Restoring these locations positions.";
121 qDebug() << tempIndex;
123 // Swap locations to correct indexes.
124 QMap<QString, int>::iterator it, ite;
125 for (it = tempIndex.begin(), ite = tempIndex.end(); it != ite; ++it)
127 int oldIndex = this->m_indexStorage.indexOf(it.key());
128 // Only operate on this item if current index is not the same as specified
129 if (it.value() != oldIndex + 1)
131 // Move to last if requested index is greater than the number of items.
132 if (it.value() >= this->m_indexStorage.size()) {
133 this->m_indexStorage.swap(oldIndex, this->m_indexStorage.size() - 1);
136 this->m_indexStorage.swap(oldIndex, it.value() - 1);
141 qDebug() << "Locations indexes after positions are restored.";
142 qDebug() << this->m_indexStorage;
145 void Locations::saveLocation(Location *location)
148 qDebug() << "Null location given to saveLocation. Aborting";
151 qDebug() << "Saving location " << location->label();
153 settings.beginGroup("Locations");
154 settings.beginGroup(location->label() );
155 if (location->isValid()) {
156 settings.setValue( "address", location->address() );
157 settings.setValue( "x", location->x() );
158 settings.setValue( "y", location->y() );
161 if (settings.contains("address")) settings.remove("address");
162 if (settings.contains("x")) settings.remove("x");
163 if (settings.contains("y")) settings.remove("y");
165 settings.setValue("index", this->m_indexStorage.indexOf(location->label()) + 1);
170 bool Locations::removeLocation(Location *location)
172 bool succeeded = false;
173 qDebug() << "Trying to remove location " << location->label();
175 settings.beginGroup("Locations");
176 if (settings.contains("" + location->label() + "/index"))
178 qDebug() << "Given location exists in settings -> removing it";
179 settings.remove(location->label());
184 if (this->m_locationStorage.contains(location->label()))
186 qDebug() << "Given location exists in locations list -> removing it";
187 this->m_locationStorage.remove(location->label());
188 //int remIndex = this->m_indexStorage.value(location->label());
189 this->m_indexStorage.removeOne(location->label());
190 /*for (int ind = 0; ind < this->m_indexStorage.size(); ++ind)
192 if (this->m_indexStorage.value(this->m_indexStorage > remIndex)
195 this->saveLocation(this->getLocation(it.key()), it.value());
198 emit(locationsChanged());
203 Location *Locations::getLocation(const QString &label) const
205 qDebug() << "requesting location " << label;
206 Location *retVal = NULL;
208 if (this->m_locationStorage.contains(label)) {
209 qDebug() << "found location " << label;
210 retVal = this->m_locationStorage[label];
212 qDebug() << "didn't find location " << label;
218 /*void Locations::changeIndex(const QString &label, const int &index, bool signal)
220 int oldIndex = this->m_indexStorage.value(label);
221 if (index == oldIndex)
224 qDebug() << "Index map before moving " << label << " from index " << oldIndex << " to index " << index;
225 qDebug() << this->m_indexStorage;
226 QHash<QString, int>::iterator it, ite;
227 if (index < oldIndex)
229 for (it = this->m_indexStorage.begin(), ite = this->m_indexStorage.end(); it != ite; ++it)
231 if (it.value() >= index && it.value() < oldIndex)
233 this->saveLocation(this->getLocation(label), ++(it.value()));
238 for (it = this->m_indexStorage.begin(), ite = this->m_indexStorage.end(); it != ite; ++it)
239 if (it.value() <= index && it.value() > oldIndex)
240 this->saveLocation(this->getLocation(label), --(it.value()));
242 this->m_indexStorage[label] = index;
243 this->saveLocation(this->getLocation(label), index);
245 qDebug() << "Index map after move";
246 qDebug() << this->m_indexStorage;
248 emit(locationsChanged());
251 Location *Locations::getLocation(const int &index) const
253 qDebug() << "Getting location for index" << index;
254 Location *loc = NULL;
256 if (this->findLabel(index, label))
258 qDebug() << "Found a label with given index " << index;
259 qDebug() << "Found label is " << label;
260 loc = this->getLocation(label);
262 if (index <= 0 || index > this->m_indexStorage.size())
265 QString label = this->m_indexStorage.at(index - 1);
266 loc = this->m_locationStorage.value(label);
270 /*bool Locations::findLabel(const int &index, QString &label) const
272 qDebug() << "Finding label for index" << index << ". Number of items in indexStorage:" << this->m_indexStorage.size() << ". Number of items in locationStorage:" << this->m_locationStorage.size();
273 qDebug() << "Location storage";
274 qDebug() << this->m_locationStorage;
275 qDebug() << "Index storage";
276 qDebug() << this->m_indexStorage;
278 if (index > this->m_indexStorage.size() || index < 1)
281 QHash<QString, int>::const_iterator it, ite;
282 for (it = this->m_indexStorage.constBegin(), ite = this->m_indexStorage.constEnd(); it != ite; ++it)
284 if (it.value() == index)
287 qDebug() << "Found label is " << label;
292 qDebug() << "Returning from label search.";
296 /*const QHash<QString, Location *>& Locations::getLocations() const
298 return this->m_locationStorage;
301 GpsLocation *Locations::getGpsLocation() const
303 qDebug() << "GPS location requested.";
304 return this->m_gpsLocation;
307 bool Locations::increaseLocationIndex(const QString &label)
309 if (!this->m_indexStorage.contains(label))
311 qDebug() << "Given label \"" << label << "\" does not exist in indexStorage.";
312 qDebug() << "Contents of indexStorage: " << this->m_indexStorage;
315 qDebug() << "Increasing index by one for label" << label;
316 int oldIndex = this->m_indexStorage.indexOf(label);
319 if (oldIndex == this->m_indexStorage.size() - 1)
321 this->m_indexStorage.move(oldIndex, oldIndex + 1);
322 this->saveLocation(this->m_locationStorage.value(label));
323 emit(locationsChanged());
324 /*QString otherLabel;
325 if (this->findLabel(oldIndex + 1, otherLabel))
327 this->m_indexStorage[label] = oldIndex + 1;
328 this->m_indexStorage[otherLabel] = oldIndex;
330 emit(locationsChanged());
335 bool Locations::lowerLocationIndex(const QString &label)
337 if (!this->m_indexStorage.contains(label))
339 qDebug() << "Lowering index by one for label" << label;
340 int oldIndex = this->m_indexStorage.indexOf(label);
341 if (oldIndex == -1) //Not found
343 if (oldIndex == 0) // Already first
345 this->m_indexStorage.move(oldIndex, oldIndex - 1);
346 this->saveLocation(this->m_locationStorage.value(label));
347 emit(locationsChanged());
348 /*QString otherLabel;
349 if (this->findLabel(oldIndex - 1, otherLabel))
351 this->m_indexStorage[label] = oldIndex - 1;
352 this->m_indexStorage[otherLabel] = oldIndex;
354 emit(locationsChanged());
359 int Locations::size() const
361 return this->m_locationStorage.size();