From: Max Waterman Date: Mon, 22 Mar 2010 21:33:52 +0000 (+0200) Subject: Added facility to restore and save locations, and set existing location in home and... X-Git-Url: http://vcs.maemo.org/git/?p=ptas;a=commitdiff_plain;h=1813f83c17c45d31039487a7612db00e5870008c Added facility to restore and save locations, and set existing location in home and work fields. --- diff --git a/zouba/location.cpp b/zouba/location.cpp index 974d1e6..3b289ca 100644 --- a/zouba/location.cpp +++ b/zouba/location.cpp @@ -126,6 +126,7 @@ void Location::replyFinished( QNetworkReply * reply ) q->parseReply( reply->readAll() ); if ( isValid() ) { + qDebug() << label() << "becomeValid" << this; emit( becomeValid() ); } } diff --git a/zouba/location_p.cpp b/zouba/location_p.cpp index b9861fb..fd32e12 100644 --- a/zouba/location_p.cpp +++ b/zouba/location_p.cpp @@ -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; } } diff --git a/zouba/locations.cpp b/zouba/locations.cpp index 2d73fbe..ed818c9 100644 --- a/zouba/locations.cpp +++ b/zouba/locations.cpp @@ -1,36 +1,107 @@ #include "locations.h" #include +#include +#include +#include +#include +#include QHash 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; isetAddress( 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(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; diff --git a/zouba/locations.h b/zouba/locations.h index fe32bf4..40f8be0 100644 --- a/zouba/locations.h +++ b/zouba/locations.h @@ -5,18 +5,29 @@ #include #include +#include -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 locationHash; + static Locations singleton; + + void saveLocation( Location *location ); }; #endif // LOCATIONS_H diff --git a/zouba/main.cpp b/zouba/main.cpp index 62402e9..47402c8 100644 --- a/zouba/main.cpp +++ b/zouba/main.cpp @@ -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); diff --git a/zouba/ui.cpp b/zouba/ui.cpp index 115da01..330b71e 100644 --- a/zouba/ui.cpp +++ b/zouba/ui.cpp @@ -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 ); diff --git a/zouba/uicontroller.cpp b/zouba/uicontroller.cpp index 891886a..f1a32cf 100644 --- a/zouba/uicontroller.cpp +++ b/zouba/uicontroller.cpp @@ -15,21 +15,27 @@ 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 ); } diff --git a/zouba/zouba.pro b/zouba/zouba.pro index d56ef81..c2708c0 100644 --- a/zouba/zouba.pro +++ b/zouba/zouba.pro @@ -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 \