Added facility to restore and save locations, and set existing location in home and...
authorMax Waterman <davidmaxwaterman+maemogit@fastmail.co.uk>
Mon, 22 Mar 2010 21:33:52 +0000 (23:33 +0200)
committerMax Waterman <davidmaxwaterman+maemogit@fastmail.co.uk>
Mon, 22 Mar 2010 21:33:52 +0000 (23:33 +0200)
zouba/location.cpp
zouba/location_p.cpp
zouba/locations.cpp
zouba/locations.h
zouba/main.cpp
zouba/ui.cpp
zouba/uicontroller.cpp
zouba/zouba.pro

index 974d1e6..3b289ca 100644 (file)
@@ -126,6 +126,7 @@ void Location::replyFinished( QNetworkReply * reply )
   q->parseReply( reply->readAll() );
 
   if ( isValid() ) {
+    qDebug() << label() << "becomeValid" << this;
     emit( becomeValid() );
   }
 }
index b9861fb..fd32e12 100644 (file)
@@ -51,6 +51,7 @@ void LocationPrivate::parseReply( const QByteArray &reply )
     m_valid = false;
   } else {
     qDebug() << "(" << m_x << "," << m_y << ")";
+    qDebug() << "is now valid";
     m_valid = true;
   }
 }
index 2d73fbe..ed818c9 100644 (file)
 #include "locations.h"
 
 #include <QDebug>
+#include <QHash>
+#include <QSettings>
+#include <QString>
+#include <QStringList>
+#include <QCoreApplication>
 
 QHash<QString,Location *> Locations::locationHash;
+Locations Locations::singleton;
 
 Locations::Locations()
 {
+  QCoreApplication::setOrganizationName("ZouBa");
+  QCoreApplication::setOrganizationDomain("zouba.yi.org");
+  QCoreApplication::setOrganizationName("ZouBa");
+
+  restoreLocations();
 }
 
 Locations::~Locations()
 {
 }
 
+Locations *Locations::instance()
+{
+  return &singleton;
+}
+
 bool Locations::addLocation( Location *location )
 {
   bool succeeded=false;
 
+  // if it's valid now, save the setting
+  if ( location->isValid() ) {
+    saveLocation( location );
+  }
+
   if ( !locationHash.contains( location->label() ) ) {
     qDebug() << "Adding location" << location->label();
     locationHash[ location->label() ] = location;
     succeeded = true;
+  } else {
+    qDebug() << "FAILED to add location" << location->label();
   }
 
   return succeeded;
 }
 
+void Locations::restoreLocations()
+{
+  QSettings settings;
+
+  settings.beginGroup( "Locations" );
+  QStringList labels = settings.childGroups();
+
+  for( int i=0; i<labels.size(); ++i ) {
+    QString label = labels[i];
+    qDebug() << "restoring" << label;
+    settings.beginGroup( label );
+    QString x = settings.value( "x" ).toString();
+    QString y = settings.value( "y" ).toString();
+    settings.endGroup();
+
+    Location *location = new Location( x, y, label );
+    location->setAddress( settings.value( "address" ).toString() );
+
+    locationHash[ location->label() ] = location;
+  }
+
+  settings.endGroup();
+}
+
+void Locations::saveLocation( Location *location )
+{
+  qDebug() << "Saving location" << location->label();
+  QSettings settings;
+  settings.beginGroup( "Locations" );
+  settings.beginGroup( location->label() );
+  settings.setValue( "address", location->address() );
+  settings.setValue( "x", location->x() );
+  settings.setValue( "y", location->y() );
+  settings.endGroup();
+  settings.endGroup();
+}
+
+void Locations::saveLocation()
+{
+  Location *location = qobject_cast<Location*>(sender());
+
+  saveLocation( location );
+}
+
 Location *Locations::location( const QString &label )
 {
+  qDebug() << "requesting location" << label;
   Location *retVal = 0;
 
   if ( locationHash.contains( label ) ) {
+    qDebug() << "found location" << label;
     retVal = locationHash[ label ];
+  } else {
+    qDebug() << "didn't find location" << label;
   }
 
   return retVal;
index fe32bf4..40f8be0 100644 (file)
@@ -5,18 +5,29 @@
 
 #include <QHash>
 #include <QString>
+#include <QObject>
 
-class Locations
+class Locations: public QObject
 {
+  Q_OBJECT
+
 public:
   Locations();
   ~Locations();
 
+  static Locations *instance();
   bool addLocation( Location *location );
 
   Location *location( const QString &label );
 
+public Q_SLOTS:
+  void saveLocation();
+
 private:
+  void restoreLocations();
   static QHash<QString,Location *> locationHash;
+  static Locations singleton;
+
+  void saveLocation( Location *location );
 };
 #endif // LOCATIONS_H
index 62402e9..47402c8 100644 (file)
@@ -17,6 +17,7 @@ int main(int argc, char *argv[] )
 {
   qInstallMsgHandler( messageHandler );
   QApplication app(argc, argv);
+
   QMainWindow *mainWindow = new QMainWindow;
   Ui ui;
   ui.setupUi(mainWindow);
index 115da01..330b71e 100644 (file)
@@ -106,13 +106,16 @@ void Ui::setWorkAddress()
 
 void Ui::setAddress( const QString &label )
 {
+  Locations *locations=Locations::instance();
+  Location *location=locations->location( label );
+
   bool ok;
   QString address = QInputDialog::getText(
      centralWidget,
      tr("Enter address for \""+QString(label).toLatin1()+"\""),
      tr("Address"),
      QLineEdit::Normal,
-     "",
+     location->address(),
      &ok
      );
 
@@ -120,8 +123,8 @@ void Ui::setAddress( const QString &label )
 
   if ( ok ) {
     qDebug() << "new address" << address;
-    Locations locations;
-    Location *location = locations.location( label );
+    Locations *locations=Locations::instance();
+    Location *location = locations->location( label );
     qDebug() << "location" << location;
     if ( location ) {
       location->resolveAddress( address );
index 891886a..f1a32cf 100644 (file)
 UiController::UiController( Ui *ui ) :
   ui(ui)
 {
-  Location *homeLocation = new Location( "home" );
-  Location *workLocation = new Location( "work" );
-
-  Locations locations;
-  locations.addLocation( homeLocation );
-  locations.addLocation( workLocation );
+  Locations *locations = Locations::instance();
+  Location *homeLocation = locations->location( "home" );
+  Location *workLocation = locations->location( "work" );
 
   connect(
       homeLocation, SIGNAL( becomeValid() ),
       this, SLOT( setHomeButtonValid() )
   );
   connect(
+      homeLocation, SIGNAL( becomeValid() ),
+      locations, SLOT( saveLocation() )
+      );
+
+  connect(
       workLocation, SIGNAL( becomeValid() ),
       this, SLOT( setWorkButtonValid() )
   );
+  connect(
+      workLocation, SIGNAL( becomeValid() ),
+      locations, SLOT( saveLocation() )
+      );
 
   homeLocation->resolveAddress( Ytv::Home );
   workLocation->resolveAddress( Ytv::Work );
@@ -49,6 +55,7 @@ UiController::~UiController()
 
 void UiController::setHomeButtonValid()
 {
+  qDebug() << "setting home button valid";
   setButtonValid( Ui::HomeButtonId );
 }
 
index d56ef81..c2708c0 100644 (file)
@@ -23,8 +23,8 @@ SOURCES += \
     route_p.cpp \
     uicontroller.cpp \
     location.cpp \
-    locations.cpp \
     location_p.cpp \
+    locations.cpp \
     gpscontroller.cpp \
     ui.cpp \
     messagetable.cpp \
@@ -36,6 +36,7 @@ HEADERS += \
     uicontroller.h \
     location.h \
     location_p.h \
+    locations.h \
     ytv.h \
     gpscontroller.h \
     ui.h \