On the way to fix #45.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 21 Aug 2012 22:11:58 +0000 (22:11 +0000)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 21 Aug 2012 22:11:58 +0000 (22:11 +0000)
src/gui/conferenceeditor.cpp
src/gui/conferenceeditor.h
src/gui/mainwindow.cpp
src/gui/mainwindow.h
src/sql/schedulexmlparser.cpp
src/sql/schedulexmlparser.h
src/sql/sqlengine.cpp
src/sql/sqlengine.h

index bfb73ea..5de436b 100644 (file)
@@ -103,8 +103,8 @@ void ConferenceEditor::addClicked()
 {
     UrlInputDialog url_input(this);
     switch (url_input.exec()) {
-    case UrlInputDialog::HaveUrl: emit haveConferenceUrl(url_input.url()); break;
-    case UrlInputDialog::HaveFile: emit haveConferenceFile(url_input.url()); break;
+    case UrlInputDialog::HaveUrl: emit haveConferenceUrl(url_input.url(), 0); break;
+    case UrlInputDialog::HaveFile: emit haveConferenceFile(url_input.url(), 0); break;
     case UrlInputDialog::Cancel: return;
     }
 }
@@ -143,7 +143,7 @@ void ConferenceEditor::changeUrlClicked()
 
 void ConferenceEditor::refreshClicked()
 {
-    if (selected_id < 0) return;
+    if (selected_id <= 0) return;
     const Conference& selectedConf = Conference::getById(selected_id);
     QString url = selectedConf.url();
 
@@ -157,7 +157,7 @@ void ConferenceEditor::refreshClicked()
     }
     // fetch
     importStarted(); // just to show the progress bar
-    emit haveConferenceUrl(url);
+    emit haveConferenceUrl(url, selected_id);
 }
 
 void ConferenceEditor::importStarted()
index 39af759..33454c1 100644 (file)
@@ -47,8 +47,8 @@ signals:
     void haveConferenceSelected(int id);
     void noneConferenceSelected();
 
-    void haveConferenceUrl(const QString& url);
-    void haveConferenceFile(const QString& path);
+    void haveConferenceUrl(const QString& url, int conferenceId);
+    void haveConferenceFile(const QString& path, int conferenceId);
     void removeConferenceRequested(int id);
     void changeUrlRequested(int, const QString&);
 
index 67cf5bd..a54b7f1 100644 (file)
@@ -147,7 +147,7 @@ void MainWindow::on_reloadAction_triggered() {
     if (confId== -1) return;
     Conference active = Conference::getById(confId);
     if (active.url().isEmpty()) return;
-    importFromNetwork(active.url());
+    importFromNetwork(active.url(), confId);
     setEnabled(false);
 }
 
@@ -317,8 +317,8 @@ void MainWindow::on_conferencesAction_triggered()
 {
     ConferenceEditor dialog(conferenceModel, this);
 
-    connect(&dialog, SIGNAL(haveConferenceUrl(const QString&)), SLOT(importFromNetwork(const QString&)));
-    connect(&dialog, SIGNAL(haveConferenceFile(const QString&)), SLOT(importFromFile(const QString&)));
+    connect(&dialog, SIGNAL(haveConferenceUrl(const QString&, int)), SLOT(importFromNetwork(const QString&, int)));
+    connect(&dialog, SIGNAL(haveConferenceFile(const QString&, int)), SLOT(importFromFile(const QString&, int)));
     connect(&dialog, SIGNAL(removeConferenceRequested(int)), SLOT(removeConference(int)));
     connect(&dialog, SIGNAL(changeUrlRequested(int, const QString&)),
                     SLOT(changeConferenceUrl(int, const QString&)));
@@ -349,33 +349,34 @@ void MainWindow::networkQueryFinished(QNetworkReply *aReply) {
         QUrl redirectUrl = aReply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
         if (!redirectUrl.isEmpty()) {
             if (redirectUrl != aReply->request().url()) {
-                importFromNetwork(redirectUrl.toString());
+                importFromNetwork(redirectUrl.toString(), aReply->request().attribute(QNetworkRequest::User).toInt());
                 return; // don't enable controls
             } else {
                 error_message(QString("Error: Cyclic redirection from %1 to itself.").arg(redirectUrl.toString()));
             }
         } else {
-            importData(aReply->readAll(), aReply->url().toEncoded());
+            importData(aReply->readAll(), aReply->url().toEncoded(), aReply->request().attribute(QNetworkRequest::User).toInt());
         }
     }
     setEnabled(true);
 }
 
-void MainWindow::importData(const QByteArray &aData, const QString& url)
+void MainWindow::importData(const QByteArray &aData, const QString& url, int conferenceId)
 {
-    mXmlParser->parseData(aData, url);
+    mXmlParser->parseData(aData, url, conferenceId);
 }
 
-void MainWindow::importFromNetwork(const QString& url)
+void MainWindow::importFromNetwork(const QString& url, int conferenceId)
 {
     QNetworkRequest request;
     request.setUrl(QUrl(url));
+    request.setAttribute(QNetworkRequest::User, conferenceId);
 
     mNetworkAccessManager->setProxy(QNetworkProxy::applicationProxy());
     mNetworkAccessManager->get(request);
 }
 
-void MainWindow::importFromFile(const QString& filename)
+void MainWindow::importFromFile(const QString& filename, int conferenceId)
 {
     QFile file(filename);
     if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {    
@@ -383,7 +384,7 @@ void MainWindow::importFromFile(const QString& filename)
         error_message(format.arg(filename, QString::number(file.error())));
     }
 
-    importData(file.readAll(), "");
+    importData(file.readAll(), "", conferenceId);
 }
 
 void MainWindow::removeConference(int id)
index fd9a0d1..611f45d 100644 (file)
@@ -54,8 +54,8 @@ private slots:
     void onSearchResultChanged();
     // TODO: remove
     void networkQueryFinished(QNetworkReply*);
-    void importFromNetwork(const QString&);
-    void importFromFile(const QString&);
+    void importFromNetwork(const QString&, int conferenceId);
+    void importFromFile(const QString&, int conferenceId);
     void removeConference(int);
     void changeConferenceUrl(int, const QString&);
 
@@ -65,7 +65,7 @@ private:
     void fillAndShowConferenceHeader();
     void initTabs();
     void clearTabs();
-    void importData(const QByteArray &aData, const QString& url);
+    void importData(const QByteArray &aData, const QString& url, int conferenceId);
 
     QString saved_title;
     ConferenceModel* conferenceModel;
index 6e361ab..2e7ba1d 100644 (file)
@@ -32,7 +32,7 @@ ScheduleXmlParser::ScheduleXmlParser(QObject *aParent)
 {
 }
 
-void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
+void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, int conferenceId)
 {
     QDomDocument document;
     QString xml_error;
@@ -47,7 +47,6 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
 
     SqlEngine::beginTransaction();
 
-    int confId = 0;
     QString conference_title;
     if (!scheduleElement.isNull())
     {
@@ -56,7 +55,7 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
         {
             emit(parsingScheduleBegin());
             QHash<QString,QString> conference;
-            conference["id"] = QString::number(0); // conference ID is assigned automatically, or obtained from the DB
+            conference["id"] = conferenceId; // conference ID is assigned automatically if 0
             conference["title"] = conferenceElement.firstChildElement("title").text();
             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
             conference["venue"] = conferenceElement.firstChildElement("venue").text();
@@ -67,8 +66,8 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
             conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
             conference["url"] = url;
-            SqlEngine::addConferenceToDB(conference);
-            confId = conference["id"].toInt();
+            SqlEngine::addConferenceToDB(conference, conferenceId);
+            conferenceId = conference["id"].toInt();
             conference_title = conference["title"];
         }
 
@@ -104,13 +103,13 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
                         QHash<QString,QString> room;
                         room["name"] = roomElement.attribute("name");
                         room["event_id"] = eventElement.attribute("id");
-                        room["conference_id"] = QString::number(confId,10);
+                        room["conference_id"] = QString::number(conferenceId,10);
                         SqlEngine::addRoomToDB(room);
 
                         // process event's nodes
                         QHash<QString,QString> event;
                         event["id"] = eventElement.attribute("id");;
-                        event["conference_id"] = QString::number(confId, 10);
+                        event["conference_id"] = QString::number(conferenceId, 10);
                         event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00
                         event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07
                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
@@ -125,7 +124,6 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
                         event["description"] = eventElement.firstChildElement("description").text(); // string
                         SqlEngine::addEventToDB(event);
                         // process persons' nodes
-                        QList<QString> persons;
                         QDomElement personsElement = eventElement.firstChildElement("persons");
                         QDomNodeList personList = personsElement.elementsByTagName("person");
                         for(int i = 0;i < personList.count();i++){
@@ -133,8 +131,7 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
                             person["id"] = personList.at(i).toElement().attribute("id");
                             person["name"] = personList.at(i).toElement().text();
                             person["event_id"] = eventElement.attribute("id");
-                            person["conference_id"] = QString::number(confId, 10);
-                            //qDebug() << "adding Person: " << person["name"];
+                            person["conference_id"] = QString::number(conferenceId, 10);
                             SqlEngine::addPersonToDB(person);
                         }
                         // process links' nodes
@@ -145,7 +142,7 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
                             link["name"] = linkList.at(i).toElement().text();
                             link["url"] = linkList.at(i).toElement().attribute("href");
                             link["event_id"] = eventElement.attribute("id");
-                            link["conference_id"] = QString::number(confId, 10);
+                            link["conference_id"] = QString::number(conferenceId, 10);
                             SqlEngine::addLinkToDB(link);
                         }
                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
index cf3f001..63ce92e 100644 (file)
@@ -29,7 +29,7 @@ class ScheduleXmlParser : public QObject
         ScheduleXmlParser (QObject *aParent = NULL);
 
     public slots:
-        void parseData(const QByteArray &aData, const QString& url);
+        void parseData(const QByteArray &aData, const QString& url, int conferenceId);
 
     signals:
         void progressStatus(int aStatus);
index 4212e02..be60c05 100644 (file)
@@ -86,37 +86,20 @@ void SqlEngine::initialize()
     login("QSQLITE",databaseName);
 }
 
-void SqlEngine::addConferenceToDB(QHash<QString,QString> &aConference)
+void SqlEngine::addConferenceToDB(QHash<QString,QString> &aConference, int conferenceId)
 {
     QSqlDatabase db = QSqlDatabase::database();
 
     if (db.isValid() && db.isOpen())
     {
-        int confId = 0;
-        QList<Conference> confsList = Conference::getAll();
-        if(confsList.count())
-        {
-            QListIterator<Conference> i(confsList);
-            while (i.hasNext())
-            {
-                Conference conf = i.next();
-                if( aConference["title"] == conf.title() )
-                {
-                    confId = conf.id();
-                    aConference["id"] = QString::number(confId);
-                    break;
-                }
-            }
-        }
+        // HACK
+        // When city is empty, assign a dummy value. We probably want to find a way to change the database scheme ...
+        // cf. #32
+        if (aConference["city"].isEmpty()) aConference["city"] = "n/a";
 
-        if(!confId) // conference 'aConference' isn't in the table => insert
+        QSqlQuery query(db);
+        if (conferenceId <= 0) // insert conference
         {
-            // HACK
-            // When city is empty, assign a dummy value. We probably want to find a way to change the database scheme ...
-            // cf. #32
-            if (aConference["city"].isEmpty()) aConference["city"] = "n/a";
-
-            QSqlQuery query(db);
             query.prepare("INSERT INTO CONFERENCE (title,url,subtitle,venue,city,start,end,days,"
                                                     "day_change,timeslot_duration,active) "
                             " VALUES (:title,:url,:subtitle,:venue,:city,:start,:end,:days,"
@@ -127,11 +110,28 @@ void SqlEngine::addConferenceToDB(QHash<QString,QString> &aConference)
             query.bindValue(":start", QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
             query.bindValue(":end", QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
             query.bindValue(":day_change", -QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0)));
-            query.bindValue(":day_change", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
-            query.bindValue(":active", confsList.count() > 0 ? 0 : 1);
+            query.bindValue(":timeslot_duration", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
+            query.bindValue(":active", 1);
             if (!query.exec()) qDebug() << "Could not execute query to insert a conference:" << query.lastError();
             aConference["id"] = query.lastInsertId().toString(); // 'id' is assigned automatically
         }
+        else // update conference
+        {
+            query.prepare("UPDATE CONFERENCE set title=:title, url=:url, subtitle=:subtitle, venue=:venue, city=:city, start=:start, end=:end, days=:days,"
+                                                "day_change=:day_change, timeslot_duration=:timeslot_duration, active=:active "
+                          "WHERE id=:id");
+            foreach (QString prop_name, (QList<QString>() << "title" << "url" << "subtitle" << "venue" << "city" << "days")) {
+                query.bindValue(QString(":") + prop_name, aConference[prop_name]);
+            }
+            query.bindValue(":start", QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
+            query.bindValue(":end", QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
+            query.bindValue(":day_change", -QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0)));
+            query.bindValue(":timeslot_duration", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
+            query.bindValue(":active", 1);
+            query.bindValue(":id", conferenceId);
+            if (!query.exec()) qDebug() << "Could not execute query to update a conference:" << query.lastError();
+            aConference["id"] = conferenceId;
+        }
     }
 }
 
index 2781f3f..8272702 100644 (file)
@@ -32,7 +32,8 @@ class SqlEngine : public QObject
         SqlEngine(QObject *aParent = NULL);
         ~SqlEngine();
         static void initialize();
-        static void addConferenceToDB(QHash<QString,QString> &aConference);
+        // if a conferneceId != 0 is given, the confernece is updated instead of inserted.
+        static void addConferenceToDB(QHash<QString,QString> &aConference, int conferenceId);
         static void addEventToDB(QHash<QString,QString> &aEvent);
         static void addPersonToDB(QHash<QString,QString> &aPerson);
         static void addLinkToDB(QHash<QString,QString> &aLink);