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 *>()),
32 m_indexStorage(QList<QString>()),
33 m_gpsLocation(new GpsLocation())
35 m_indexStorage(QList<QString>())
38 this->restoreLocations();
39 qDebug() << "Size of index storage:" << this->m_indexStorage.size();
42 /*Locations::~Locations()
44 QHash<QString,Location*>::iterator it, ite;
45 for (it = this->m_locationStorage.begin(), ite = this->m_locationStorage.end(); it != ite; ++it)
49 this->m_locationStorage.empty();
53 bool Locations::addEditLocation(Location *location)
57 if ( !this->m_locationStorage.contains(location->label())) {
58 qDebug() << "Adding location" << location->label();
59 this->m_locationStorage[location->label()] = location;
60 qDebug() << "Index storage:";
61 qDebug() << this->m_indexStorage;
62 qDebug() << "Size of index storage:" << this->m_indexStorage.size();
63 this->m_indexStorage.append(location->label());
64 qDebug() << "Index storage after inserting location:";
65 qDebug() << this->m_indexStorage;
68 qDebug() << "A location with the same label (" << location->label() << ") already exists.";
69 this->m_locationStorage.remove(location->label());
70 this->m_locationStorage[location->label()] = location;
72 emit(locationsChanged());
74 // save the location to settings
75 this->saveLocation(location);
80 void Locations::restoreLocations()
84 settings.beginGroup("Locations");
85 QStringList labels = settings.childGroups();
87 QMap<QString, int> tempIndex = QMap<QString, int>();
89 for( int i=0; i<labels.size(); ++i ) {
90 QString label = labels[i];
91 settings.beginGroup(label);
92 QString address, x, y;
94 if (settings.contains("address")) {
95 address = settings.value( "address" ).toString();
96 if (settings.contains("x")) {
97 x = settings.value( "x" ).toString();
98 if (settings.contains("y")) {
99 y = settings.value( "y" ).toString();
104 int index = settings.value("index").toInt();
107 qDebug() << "Restoring " << label;
110 location = new Location( x, y, label, address );
111 this->m_locationStorage[label] = location;
112 this->m_indexStorage.append(label);
114 tempIndex.insert(label, index);
122 qDebug() << "Locations indexes before restoring positions";
123 qDebug() << this->m_indexStorage;
124 qDebug() << "Restoring these locations positions.";
125 qDebug() << tempIndex;
127 // Swap locations to correct indexes.
128 QMap<QString, int>::iterator it, ite;
129 for (it = tempIndex.begin(), ite = tempIndex.end(); it != ite; ++it)
131 int oldIndex = this->m_indexStorage.indexOf(it.key());
132 // Only operate on this item if current index is not the same as specified
133 if (it.value() != oldIndex + 1)
135 // Move to last if requested index is greater than the number of items.
136 if (it.value() >= this->m_indexStorage.size()) {
137 this->m_indexStorage.swap(oldIndex, this->m_indexStorage.size() - 1);
140 this->m_indexStorage.swap(oldIndex, it.value() - 1);
145 qDebug() << "Locations indexes after positions are restored.";
146 qDebug() << this->m_indexStorage;
149 void Locations::saveLocation(Location *location)
152 qDebug() << "Null location given to saveLocation. Aborting";
155 qDebug() << "Saving location " << location->label();
157 settings.beginGroup("Locations");
158 settings.beginGroup(location->label() );
159 if (location->isValid()) {
160 settings.setValue( "address", location->address() );
161 settings.setValue( "x", location->x() );
162 settings.setValue( "y", location->y() );
165 if (settings.contains("address")) settings.remove("address");
166 if (settings.contains("x")) settings.remove("x");
167 if (settings.contains("y")) settings.remove("y");
169 settings.setValue("index", this->m_indexStorage.indexOf(location->label()) + 1);
174 bool Locations::removeLocation(Location *location)
176 bool succeeded = false;
177 qDebug() << "Trying to remove location " << location->label();
179 settings.beginGroup("Locations");
180 if (settings.contains("" + location->label() + "/index"))
182 qDebug() << "Given location exists in settings -> removing it";
183 settings.remove(location->label());
188 if (this->m_locationStorage.contains(location->label()))
190 qDebug() << "Given location exists in locations list -> removing it";
191 this->m_locationStorage.remove(location->label());
192 //int remIndex = this->m_indexStorage.value(location->label());
193 this->m_indexStorage.removeOne(location->label());
194 /*for (int ind = 0; ind < this->m_indexStorage.size(); ++ind)
196 if (this->m_indexStorage.value(this->m_indexStorage > remIndex)
199 this->saveLocation(this->getLocation(it.key()), it.value());
202 emit(locationsChanged());
207 Location *Locations::getLocation(const QString &label) const
209 qDebug() << "requesting location " << label;
210 Location *retVal = NULL;
212 if (this->m_locationStorage.contains(label)) {
213 qDebug() << "found location " << label;
214 retVal = this->m_locationStorage[label];
216 qDebug() << "didn't find location " << label;
222 /*void Locations::changeIndex(const QString &label, const int &index, bool signal)
224 int oldIndex = this->m_indexStorage.value(label);
225 if (index == oldIndex)
228 qDebug() << "Index map before moving " << label << " from index " << oldIndex << " to index " << index;
229 qDebug() << this->m_indexStorage;
230 QHash<QString, int>::iterator it, ite;
231 if (index < oldIndex)
233 for (it = this->m_indexStorage.begin(), ite = this->m_indexStorage.end(); it != ite; ++it)
235 if (it.value() >= index && it.value() < oldIndex)
237 this->saveLocation(this->getLocation(label), ++(it.value()));
242 for (it = this->m_indexStorage.begin(), ite = this->m_indexStorage.end(); it != ite; ++it)
243 if (it.value() <= index && it.value() > oldIndex)
244 this->saveLocation(this->getLocation(label), --(it.value()));
246 this->m_indexStorage[label] = index;
247 this->saveLocation(this->getLocation(label), index);
249 qDebug() << "Index map after move";
250 qDebug() << this->m_indexStorage;
252 emit(locationsChanged());
255 Location *Locations::getLocation(const int &index) const
257 qDebug() << "Getting location for index" << index;
258 Location *loc = NULL;
260 if (this->findLabel(index, label))
262 qDebug() << "Found a label with given index " << index;
263 qDebug() << "Found label is " << label;
264 loc = this->getLocation(label);
266 if (index <= 0 || index > this->m_indexStorage.size())
269 QString label = this->m_indexStorage.at(index - 1);
270 loc = this->m_locationStorage.value(label);
274 /*bool Locations::findLabel(const int &index, QString &label) const
276 qDebug() << "Finding label for index" << index << ". Number of items in indexStorage:" << this->m_indexStorage.size() << ". Number of items in locationStorage:" << this->m_locationStorage.size();
277 qDebug() << "Location storage";
278 qDebug() << this->m_locationStorage;
279 qDebug() << "Index storage";
280 qDebug() << this->m_indexStorage;
282 if (index > this->m_indexStorage.size() || index < 1)
285 QHash<QString, int>::const_iterator it, ite;
286 for (it = this->m_indexStorage.constBegin(), ite = this->m_indexStorage.constEnd(); it != ite; ++it)
288 if (it.value() == index)
291 qDebug() << "Found label is " << label;
296 qDebug() << "Returning from label search.";
300 /*const QHash<QString, Location *>& Locations::getLocations() const
302 return this->m_locationStorage;
305 bool Locations::increaseLocationIndex(const QString &label)
307 if (!this->m_indexStorage.contains(label))
309 qDebug() << "Given label \"" << label << "\" does not exist in indexStorage.";
310 qDebug() << "Contents of indexStorage: " << this->m_indexStorage;
313 qDebug() << "Increasing index by one for label" << label;
314 int oldIndex = this->m_indexStorage.indexOf(label);
317 if (oldIndex == this->m_indexStorage.size() - 1)
319 this->m_indexStorage.move(oldIndex, oldIndex + 1);
320 this->saveLocation(this->m_locationStorage.value(label));
321 emit(locationsChanged());
322 /*QString otherLabel;
323 if (this->findLabel(oldIndex + 1, otherLabel))
325 this->m_indexStorage[label] = oldIndex + 1;
326 this->m_indexStorage[otherLabel] = oldIndex;
328 emit(locationsChanged());
333 bool Locations::lowerLocationIndex(const QString &label)
335 if (!this->m_indexStorage.contains(label))
337 qDebug() << "Lowering index by one for label" << label;
338 int oldIndex = this->m_indexStorage.indexOf(label);
339 if (oldIndex == -1) //Not found
341 if (oldIndex == 0) // Already first
343 this->m_indexStorage.move(oldIndex, oldIndex - 1);
344 this->saveLocation(this->m_locationStorage.value(label));
345 emit(locationsChanged());
346 /*QString otherLabel;
347 if (this->findLabel(oldIndex - 1, otherLabel))
349 this->m_indexStorage[label] = oldIndex - 1;
350 this->m_indexStorage[otherLabel] = oldIndex;
352 emit(locationsChanged());
357 int Locations::size() const
359 return this->m_locationStorage.size();
363 GpsLocation *Locations::getGpsLocation() const
365 qDebug() << "GPS location requested.";
366 return this->m_gpsLocation;