- added settingsdialog
[buliscores] / src / backendkicker.cpp
index 847cb56..ed71b36 100644 (file)
@@ -5,30 +5,33 @@
 #include <QDebug>
 #include <QStringList>
 #include <QDateTime>
+#include <QSettings>
 
 #include "backendkicker.h"
-#include "livescores.h"
 
 BackendKicker::BackendKicker(QObject *parent) :
     QObject(parent)
 {
+    QSettings settings("David Solbach", "BuliScores");
+    this->setLeague(settings.value("League", "1. Bundesliga").toString());
+
     this->update();
 }
 
-Match* BackendKicker::getMatch(QString hometeam, QString awayteam)
+Match* BackendKicker::getMatch(QString hometeam, QString awayteam, QDateTime date)
 {
     QListIterator<Match*> iter(m_matchlist);
     Match*        match;
 
     while (iter.hasNext()) {
         match = iter.next();
-        if (match->awayteam() == awayteam &&
-            match->hometeam() == hometeam) {
+        if (match->awayTeam() == awayteam &&
+            match->homeTeam() == hometeam) {
             return match;
         }
     }
 
-    match = new Match(hometeam, awayteam, this);
+    match = new Match(hometeam, awayteam, date, this);
     m_matchlist.append(match);
 
     emit matchListChanged();
@@ -83,24 +86,45 @@ static QString parseTeam(QString teamhtml)
     return team;
 }
 
-static int parseScore(QString scorehtml, bool home, bool firsthalf)
+static int parseScore(Match* match, QString scorehtml)
 {
     int         score = -1;
-    int         index = 0;
     QStringList tokens;
 
     qDebug() << "parseScore in: " << scorehtml;
-
     tokens = scorehtml.split(QRegExp("[>&();:<]"), QString::SkipEmptyParts);
     qDebug() << tokens;
 
-    index = 1;
-    if (!home)      { index++;    }
-    if (firsthalf)  { index += 3; }
-
-    score = tokens.at(index).toInt();
+    if (tokens.count() == 7) {
+        // no extra color tag -> either not started, halftime or finished
+        if (tokens.at(4) == "-") {
+            // no first half results -> match not started yet
+            match->setState(Match::NotStarted);
+        } else if (tokens.at(1) == "-") {
+            // second half has not been started but there are first
+            // half results -> currently half time
+            match->setScore(tokens.at(4).toInt(), tokens.at(5).toInt());
+            match->setState(Match::HalfTime);
+        } else {
+            // no color tag and no "-" -> game is finished
+            match->setScore(tokens.at(1).toInt(), tokens.at(3).toInt());
+            match->setState(Match::Finished);
+        }
+    } else {
+        // there is a color tag which means that either first
+        // half or second half are currently running
+        if (tokens.at(4).contains("color")) {
+            // first half score marked red -> first half running
+            match->setScore(tokens.at(5).toInt(), tokens.at(6).toInt());
+            match->setState(Match::FirstHalf);
+        } else if (tokens.at(1).contains("color")) {
+            // second half score marked res -> second half running
+            match->setState(Match::SecondHalf);
+            match->setScore(tokens.at(2).toInt(), tokens.at(3).toInt());
+        }
 
-    qDebug() << "parseScore out: " << score;
+    }
+    qDebug() << "match out: " << match;
 
     return score;
 }
@@ -115,9 +139,6 @@ void BackendKicker::parsePage (QString htmlstr)
 
     int             pos     = 0;
     int             count   = 0;
-    int             homescore, awayscore;
-    int             homescorefh, awayscorefh;
-
 
     //qDebug() << "parsePage in: " << htmlstr;
 
@@ -149,35 +170,39 @@ void BackendKicker::parsePage (QString htmlstr)
                  break;
              case 5: // awayteam
                  awayteam = parseTeam(tmp);
+                 match = getMatch(hometeam, awayteam, date);
                  break;
              case 6: // scores
-                 homescore   = parseScore(tmp, true, false);
-                 homescorefh = parseScore(tmp, true, true);
-                 awayscore   = parseScore(tmp, false, false);
-                 awayscorefh = parseScore(tmp, false, true);
+                 parseScore(match, tmp);
                  break;
              default:
                 ;;
              }
         }
-        match = getMatch(hometeam, awayteam);
-        match->setScore(homescore, awayscore);
     }
 
 
 }
 
-void BackendKicker::update()
+void BackendKicker::setLeague(QString league)
 {
-    //QString URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/1-bundesliga/2010-11/spieltag.html";
-    QString URL = "http://www.kicker.de/news/fussball/2bundesliga/spieltag/2-bundesliga/2010-11/spieltag.html";
+    if (league == "1. Bundesliga") {
+        m_URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/1-bundesliga/2010-11/spieltag.html";
+    } else if (league == "2. Bundesliga") {
+        m_URL = "http://www.kicker.de/news/fussball/bundesliga/spieltag/2-bundesliga/2010-11/spieltag.html";
+    } else if (league == "tipp3 Bundesliga") {
+        m_URL = "http://www.kicker.de/news/fussball/intligen/oesterreich/tipp3-bundesliga/2010-11/spieltag.html";
+    }
+}
 
+void BackendKicker::update()
+{
     QNetworkAccessManager *manager = new QNetworkAccessManager(this);
     connect(manager, SIGNAL(finished(QNetworkReply*)),
             this, SLOT(dlndFinished(QNetworkReply*)));
 
-    qDebug() << "URL: " << URL;
-    manager->get(QNetworkRequest(QUrl(URL)));
+    qDebug() << "URL: " << m_URL;
+    manager->get(QNetworkRequest(QUrl(m_URL)));
 }
 
 void BackendKicker::dlndFinished(QNetworkReply *reply)