Moved files to make zouba the only project.
[ptas] / src / locations.cpp
1 #include "locations.h"
2
3 #include <QDebug>
4 #include <QHash>
5 #include <QSettings>
6 #include <QString>
7 #include <QStringList>
8 #include <QCoreApplication>
9
10 QHash<QString,Location *> Locations::locationHash;
11 bool Locations::initialised = false;
12
13 Locations::Locations()
14 {
15     if ( !initialised ) {
16         QCoreApplication::setOrganizationName("ZouBa");
17         QCoreApplication::setOrganizationDomain("zouba.yi.org");
18         QCoreApplication::setOrganizationName("ZouBa");
19
20         restoreLocations();
21         initialised = true;
22     }
23 }
24
25 Locations::~Locations()
26 {
27 }
28
29 bool Locations::addLocation( Location *location )
30 {
31   bool succeeded=false;
32
33   // if it's valid now, save the setting
34   if ( location->isValid() ) {
35     saveLocation( location );
36   }
37
38   if ( !locationHash.contains( location->label() ) ) {
39     qDebug() << "Adding location" << location->label();
40     locationHash[ location->label() ] = location;
41     succeeded = true;
42   } else {
43     qDebug() << "FAILED to add location" << location->label();
44   }
45
46   return succeeded;
47 }
48
49 void Locations::restoreLocations()
50 {
51   QSettings settings;
52
53   settings.beginGroup( "Locations" );
54   QStringList labels = settings.childGroups();
55
56   for( int i=0; i<labels.size(); ++i ) {
57     QString label = labels[i];
58     settings.beginGroup( label );
59     QString x       = settings.value( "x" ).toString();
60     QString y       = settings.value( "y" ).toString();
61     QString address = settings.value( "address" ).toString();
62     settings.endGroup();
63
64     qDebug() << "restoring" << label;
65     Location *location = new Location( x, y, label );
66     location->setAddress( address );
67
68     locationHash[ label ] = location;
69   }
70
71   settings.endGroup();
72 }
73
74 void Locations::saveLocation( Location *location )
75 {
76   qDebug() << "Saving location" << location->label();
77   QSettings settings;
78   settings.beginGroup( "Locations" );
79   settings.beginGroup( location->label() );
80   settings.setValue( "address", location->address() );
81   settings.setValue( "x", location->x() );
82   settings.setValue( "y", location->y() );
83   settings.endGroup();
84   settings.endGroup();
85 }
86
87 void Locations::saveLocation()
88 {
89   Location *location = qobject_cast<Location*>(sender());
90
91   saveLocation( location );
92 }
93
94 Location *Locations::location( const QString &label )
95 {
96   qDebug() << "requesting location" << label;
97   Location *retVal = 0;
98
99   if ( locationHash.contains( label ) ) {
100     qDebug() << "found location" << label;
101     retVal = locationHash[ label ];
102   } else {
103     qDebug() << "didn't find location" << label;
104   }
105
106   return retVal;
107 }