From: David Solbach Date: Sat, 16 Oct 2010 21:38:21 +0000 (+0200) Subject: - write logfile for debugging purposes X-Git-Url: http://vcs.maemo.org/git/?p=buliscores;a=commitdiff_plain;h=cd2c9eace78eafe859f6b68aa925707d2e5ff5f2 - write logfile for debugging purposes - don't hide table during update - better calculation of update times for more efficient polling - some ui clean up --- diff --git a/BundesligaWidget.pro b/BundesligaWidget.pro index 0006098..b5cbe1b 100644 --- a/BundesligaWidget.pro +++ b/BundesligaWidget.pro @@ -3,6 +3,7 @@ include (./qmaemo5homescreenadaptor/qmaemo5homescreenadaptor.pri) QT += network QT += phonon + OTHER_FILES += \ bundesligawidget.desktop diff --git a/BundesligaWidget.pro.user b/BundesligaWidget.pro.user index dc0c1de..b8fd4da 100644 --- a/BundesligaWidget.pro.user +++ b/BundesligaWidget.pro.user @@ -212,7 +212,7 @@ 1 - 2010-10-15T23:23:32 + 2010-10-16T21:17:22 1 diff --git a/bundesligawidget.desktop b/bundesligawidget.desktop index eaa82ed..73ca659 100644 --- a/bundesligawidget.desktop +++ b/bundesligawidget.desktop @@ -1,5 +1,5 @@ [Desktop Entry] -Name=Bundesliga Widget +Name=BuLi Scores Comment=Live scores of the German Bundesliga. Type=qt X-Path=BundesligaWidget diff --git a/src/backendkicker.cpp b/src/backendkicker.cpp index f6a1a6d..d69280f 100644 --- a/src/backendkicker.cpp +++ b/src/backendkicker.cpp @@ -10,8 +10,7 @@ #include "backendkicker.h" -const int BackendKicker::INTERVAL_FAST = 100 * 1000; -const int BackendKicker::INTERVAL_SLOW = 3600 * 1000; +const int BackendKicker::INTERVAL_FAST = 180; BackendKicker::BackendKicker(QObject *parent) : MatchDayBackend(parent), @@ -28,6 +27,8 @@ BackendKicker::BackendKicker(QObject *parent) : connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(update())); + m_updateTimer.setSingleShot(true); + this->update(); } @@ -46,6 +47,7 @@ Match* BackendKicker::getMatch(QString hometeam, QString awayteam, QDateTime dat match = new Match(hometeam, awayteam, date, this); m_matchlist.append(match); + emit matchAdded(match); return match; } @@ -134,9 +136,9 @@ static void parseScore(Match* match, QString scorehtml) } } -// qDebug() << "match state: " << match->state(); -// qDebug() << "match home: " << match->homeScore(); -// qDebug() << "match away: " << match->awayScore(); + qDebug() << "match state: " << match->state(); + qDebug() << "match home: " << match->homeScore(); + qDebug() << "match away: " << match->awayScore(); } void BackendKicker::parsePage (QString htmlstr) @@ -207,62 +209,70 @@ bool BackendKicker::selectLeague(QString league) } // delete last data + this->m_matchlist.clear(); + this->update(); + return leagueIsSupported; } void BackendKicker::update() { - if (matchRunning()) { - m_updateTimer.start(INTERVAL_FAST); - } else { - m_updateTimer.start(INTERVAL_SLOW); - } - emit updateStarted(); - //qDebug() << "URL: " << m_URL; + qDebug() << "Start Update with URL: " << m_URL; m_netaccmgr->get(QNetworkRequest(QUrl(m_URL))); } void BackendKicker::dlndFinished(QNetworkReply *reply) { QString rawdata; + int secstonextupdate; if (reply->error() != QNetworkReply::NoError) { // TODO proper user friendly error handling here! - qDebug() << "dlnd failed: error: " << reply->error(); + qWarning() << "Download failed with error: " << reply->error(); } - this->m_matchlist.clear(); - rawdata = reply->readAll(); parsePage(rawdata); - emit matchListChanged(); + emit updateFinished(); + + secstonextupdate = secsToNextGame(); + if (secstonextupdate < INTERVAL_FAST) { + secstonextupdate = INTERVAL_FAST; + } else if (secstonextupdate > 6 * 3600) { + secstonextupdate = 6 * 3600; + } + m_updateTimer.start(secstonextupdate * 1000); + qDebug() << "Update finished, next update in: " << secstonextupdate << "seconds."; } -bool BackendKicker::matchRunning() +int BackendKicker::secsToNextGame() { QListIterator iter(m_matchlist); Match* match; - bool matchrunning = false; int secstogame; + int secstonextgame = -1; while (iter.hasNext()) { match = iter.next(); if (match->state() == Match::FirstHalf || - match->state() == Match::SecondHalf) { + match->state() == Match::SecondHalf || + match->state() == Match::HalfTime) { - matchrunning = true; + secstonextgame = 0; + return secstonextgame; break; - } else { + } else if (match->state() == Match::NotStarted) { secstogame = QDateTime::currentDateTime().secsTo(match->date()); - if (secstogame >= 0 && secstogame < INTERVAL_SLOW) { - matchrunning = true; - break; + if (secstonextgame == -1) { + secstonextgame = secstogame; + } else if (secstogame < secstonextgame) { + secstonextgame = secstogame; } } } - return matchrunning; + return secstonextgame; } diff --git a/src/backendkicker.h b/src/backendkicker.h index 8b1a9a6..d8b6690 100644 --- a/src/backendkicker.h +++ b/src/backendkicker.h @@ -11,6 +11,8 @@ class BackendKicker : public MatchDayBackend { Q_OBJECT + + // TODO clear matchlist on match day change! public: explicit BackendKicker(QObject *parent = 0); @@ -21,7 +23,7 @@ public: void setAutomaticUpdate(bool); signals: - void matchListChanged(void); + void updateFinished(void); void updateStarted(void); public slots: @@ -35,10 +37,9 @@ private: QNetworkAccessManager* m_netaccmgr; static const int INTERVAL_FAST; - static const int INTERVAL_SLOW; void parsePage (QString htmlstr); - bool matchRunning (void); + int secsToNextGame(void); private slots: void dlndFinished(QNetworkReply *reply); diff --git a/src/main.cpp b/src/main.cpp index 655cb33..5d4cef7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,12 +43,65 @@ #include "backendkicker.h" #include +#include +#include #include + + +void messageHandler(QtMsgType type, const char *msg) +{ + static QFile logfile; + static QTextStream fw; + static const QString LOGFILE_PATH = "/tmp/buliscores.log"; + static const QtMsgType LOGLEVEL = QtDebugMsg; + QString out; + + if (type < LOGLEVEL) { + return; + } + + if (logfile.isOpen() == false) { + logfile.setFileName(LOGFILE_PATH); + if (logfile.open(QIODevice::Append) == true) { + fw.setDevice((QIODevice*)&logfile); + fw << "Logfile Opened: " << QDateTime::currentDateTime().toString(); + } + } + + switch (type) { + case QtDebugMsg: + out = "%1 Debug: %2\n"; + break; + case QtWarningMsg: + out = "%1 Warning: %2\n"; + break; + case QtCriticalMsg: + out = "%1 Critical: %2\n"; + break; + case QtFatalMsg: + out = "%1 Fatal: %2\n"; + break; + } + + out = out.arg(QDateTime::currentDateTime().toString(), msg); + + if (logfile.isOpen()) { + fw << out; + fw.flush(); + } + printf("%s", out.toAscii().constData()); + + if (type == QtFatalMsg) { + abort(); + } +} + int main(int argc, char *argv[]) { // enforce native graphics system for now due to a bug in transparency handling // you can remove this line if you only target PR 1.2 + qInstallMsgHandler(messageHandler); QApplication::setGraphicsSystem("native"); QApplication app(argc, argv); MainWidget mw; @@ -56,6 +109,8 @@ int main(int argc, char *argv[]) app.setApplicationName("Buli Scores"); app.setApplicationVersion("0.1"); app.setOrganizationName("David Solbach"); + + mw.resize(400,250); QMaemo5HomescreenAdaptor *adaptor = new QMaemo5HomescreenAdaptor(&mw); diff --git a/src/mainwidget.cpp b/src/mainwidget.cpp index dbffeee..9103add 100644 --- a/src/mainwidget.cpp +++ b/src/mainwidget.cpp @@ -2,8 +2,6 @@ #include #include #include -#include -#include #include "mainwidget.h" #include "backendkicker.h" @@ -12,6 +10,8 @@ MainWidget::MainWidget(QWidget *parent) : QWidget(parent), + m_mediaObject(new Phonon::MediaObject(this)), + m_audioOutput(new Phonon::AudioOutput(Phonon::MusicCategory, this)), m_backend(new BackendKicker(this)), m_datamodel(new MatchDayModel(this, m_backend)), m_scoretbl(new ScoreTable(m_datamodel)), @@ -21,12 +21,14 @@ MainWidget::MainWidget(QWidget *parent) : QFont f; QPalette palette; + this->setAttribute(Qt::WA_TranslucentBackground); + // label + m_statuslbl.hide(); m_statuslbl.setText(tr("BuLi Scores!")); f.setPixelSize(40); palette.setColor(QPalette::Background, QColor(0, 0, 0, 127)); palette.setColor(QPalette::Foreground, QColor(255, 255, 255, 127)); - m_statuslbl.show(); m_statuslbl.setPalette(palette); m_statuslbl.setAttribute(Qt::WA_TransparentForMouseEvents); m_statuslbl.setAutoFillBackground(true); @@ -36,27 +38,25 @@ MainWidget::MainWidget(QWidget *parent) : m_statuslbl.setFont(f); // table - m_scoretbl->hide(); - - this->setAttribute(Qt::WA_TranslucentBackground); + m_statuslbl.show(); this->setLayout(&m_layout); m_layout.addWidget(&m_statuslbl); m_layout.addWidget(m_scoretbl); - // sounds - Phonon::MediaObject *m_mediaObject = new Phonon::MediaObject(this); - Phonon::AudioOutput *m_audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this); - Phonon::createPath(m_mediaObject, m_audioOutput); - connect(m_settingsdlg, SIGNAL(accepted()), this, SLOT(update())); - connect(m_backend, SIGNAL(matchListChanged()), + connect(m_backend, SIGNAL(updateFinished()), this, SLOT(onBackendUpdateFinished())); connect(m_backend, SIGNAL(updateStarted()), this, SLOT(onBackendUpdateStarted())); + + connect(m_backend, SIGNAL(matchAdded(Match*)), + this, SLOT(onMatchAdded(Match*))); + + Phonon::Path path = Phonon::createPath(m_mediaObject, m_audioOutput); } // only needed for testing on desktop @@ -75,19 +75,37 @@ void MainWidget::update(void) void MainWidget::onBackendUpdateStarted() { - m_statuslbl.setText(tr("Updating...")); - m_statuslbl.show(); - m_scoretbl->hide(); +// m_statuslbl.setText(tr("Updating...")); +// m_statuslbl.show(); +// m_scoretbl->hide(); } void MainWidget::onBackendUpdateFinished() { - //mediaObject->play(); - m_scoretbl->show(); m_statuslbl.hide(); } +void MainWidget::onMatchAdded(Match* match) { + connect(match, SIGNAL(scoreChanged(int,int,int,int)), + this, SLOT(onScoreChange())); + + connect(match, SIGNAL(stateChanged(Match::MatchState)), + this, SLOT(onMatchStateChanged(Match::MatchState))); +} + +void MainWidget::onScoreChange() +{ + m_mediaObject->setCurrentSource(Phonon::MediaSource("/usr/share/buliscores/tor.wav")); + m_mediaObject->play(); +} + +void MainWidget::onMatchStateChanged(Match::MatchState state) +{ + m_mediaObject->setCurrentSource(Phonon::MediaSource("/usr/share/buliscores/trillerpfeife.wav")); + m_mediaObject->play(); +} + void MainWidget::showSettingsDialog() { m_settingsdlg->show(); diff --git a/src/mainwidget.h b/src/mainwidget.h index 1b68c1e..6cbd0eb 100644 --- a/src/mainwidget.h +++ b/src/mainwidget.h @@ -6,8 +6,11 @@ #include #include "matchdaybackend.h" +#include "match.h" #include "scoretable.h" #include "settingsdialog.h" +#include +#include class MainWidget : public QWidget @@ -22,10 +25,16 @@ public slots: void showSettingsDialog(); protected slots: - void onBackendUpdateStarted(); + void onBackendUpdateStarted(void); void onBackendUpdateFinished(void); + void onMatchAdded(Match* match); + void onScoreChange(); + void onMatchStateChanged(Match::MatchState state); + private: + Phonon::MediaObject *m_mediaObject; + Phonon::AudioOutput *m_audioOutput; MatchDayBackend* m_backend; MatchDayModel* m_datamodel; ScoreTable* m_scoretbl; diff --git a/src/match.cpp b/src/match.cpp index 20bc393..9850d7f 100644 --- a/src/match.cpp +++ b/src/match.cpp @@ -7,6 +7,8 @@ Match::Match(QString hometeam, QString awayteam, QDateTime date, QObject *parent m_homeTeam = hometeam; m_awayTeam = awayteam; + m_homeScore = -1; + m_awayScore = -1; m_homeEmblem = getEmblemByName(hometeam); m_awayEmblem = getEmblemByName(awayteam); @@ -64,7 +66,9 @@ QIcon Match::getEmblemByName(QString team) void Match::setScore(int home, int away) { - bool changed; + bool changed = false; + int oldhomescore = m_homeScore; + int oldawayscore = m_awayScore; if (m_homeScore != home) { m_homeScore = home; @@ -77,10 +81,14 @@ void Match::setScore(int home, int away) } if (changed) { - emit scoreChanged(home, away); + emit scoreChanged(oldhomescore, oldawayscore, + home, away); } } void Match::setState(MatchState state) { - m_state = state; + if (m_state != state) { + m_state = state; + emit stateChanged(state); + } } diff --git a/src/match.h b/src/match.h index 081e174..d37b5d5 100644 --- a/src/match.h +++ b/src/match.h @@ -68,8 +68,9 @@ public: void setState(MatchState state); signals: - void scoreChanged(int homescore, int awayscore); - void stateChanged(MatchState state); + void scoreChanged(int oldhomescore, int oldawayscore, + int homescore, int awayscore); + void stateChanged(Match::MatchState state); public slots: diff --git a/src/matchdaybackend.h b/src/matchdaybackend.h index 7944e5d..02eab60 100644 --- a/src/matchdaybackend.h +++ b/src/matchdaybackend.h @@ -23,7 +23,8 @@ public: virtual bool selectLeague(QString league) = 0; signals: - void matchListChanged(void); + void matchAdded(Match* match); + void updateFinished(void); void updateStarted(void); public slots: diff --git a/src/matchdaymodel.cpp b/src/matchdaymodel.cpp index 0ebb41c..7528a97 100644 --- a/src/matchdaymodel.cpp +++ b/src/matchdaymodel.cpp @@ -17,8 +17,8 @@ MatchDayModel::MatchDayModel(QObject *parent, MatchDayBackend *backend) : { m_backend = backend; - connect(m_backend, SIGNAL(matchListChanged()), - this, SLOT(onMatchListChanged())); + connect(m_backend, SIGNAL(updateFinished()), + this, SLOT(onUpdateFinished())); } int MatchDayModel::rowCount(const QModelIndex&) const @@ -30,7 +30,7 @@ int MatchDayModel::rowCount(const QModelIndex&) const int MatchDayModel::columnCount(const QModelIndex&) const { - return 10; + return 11; } QVariant MatchDayModel::data(const QModelIndex& index, int role) const @@ -49,6 +49,7 @@ QVariant MatchDayModel::data(const QModelIndex& index, int role) const case Qt::BackgroundRole: return QBrush(QColor(20, 20, 20, 100)); break; + case Qt::DecorationRole: switch (index.column()) { case AwayIcon: @@ -120,8 +121,9 @@ QVariant MatchDayModel::data(const QModelIndex& index, int role) const s.setHeight(25); switch (index.column()) { case Spacer: - s.setWidth(3); - break; + case Spacer2: + s.setWidth(2); + break; case MatchState: s.setWidth(15); break; @@ -179,18 +181,17 @@ QVariant MatchDayModel::data(const QModelIndex& index, int role) const } -// only adds for now -void MatchDayModel::onMatchListChanged(void) +void MatchDayModel::onUpdateFinished(void) { //remove all rows - //qDebug() << "beginRemoveRows: " << 0 << ", " << rowCount(QModelIndex()) - 1; + qDebug() << "beginRemoveRows: " << 0 << ", " << rowCount(QModelIndex()) - 1; beginRemoveRows(QModelIndex(), 0, m_lastRowCount); endRemoveRows(); //add rows - //qDebug() << "beginInsertRows: " << 0 << ", " << m_backend->matchList().count() - 1; + qDebug() << "beginInsertRows: " << 0 << ", " << m_backend->matchList().count() - 1; beginInsertRows(QModelIndex(), 0, m_backend->matchList().count() - 1); @@ -199,7 +200,7 @@ void MatchDayModel::onMatchListChanged(void) m_lastRowCount = m_backend->matchList().count() - 1; // invalidate complete data - //qDebug() << "rowCount @ emit dataChanged: " << rowCount(QModelIndex()); + qDebug() << "rowCount @ emit dataChanged: " << rowCount(QModelIndex()); emit dataChanged(index(0, 0), index(rowCount(QModelIndex()) - 1, columnCount(QModelIndex()) - 1)); diff --git a/src/matchdaymodel.h b/src/matchdaymodel.h index 0c9ec95..4d48000 100644 --- a/src/matchdaymodel.h +++ b/src/matchdaymodel.h @@ -14,6 +14,7 @@ class MatchDayModel : public QAbstractTableModel enum { Spacer = 0, MatchState, + Spacer2, HomeIcon, HomeTeam, HomeScore, @@ -39,7 +40,7 @@ public: QVariant data(const QModelIndex& index, int role) const; protected slots: - void onMatchListChanged(void); + void onUpdateFinished(void); };