New architecture
authorJari Jarvi <t7jaja00@students.oamk.fi>
Mon, 21 Jun 2010 12:43:00 +0000 (15:43 +0300)
committerJari Jarvi <t7jaja00@students.oamk.fi>
Mon, 21 Jun 2010 12:43:00 +0000 (15:43 +0300)
src/remote.cpp
src/remote.h
src/remotedbmgr.cpp
src/settingsdlg.cpp
src/settingsdlg.h

index c9cf83b..006e907 100644 (file)
@@ -9,22 +9,27 @@
 #include <QNetworkReply>
 #include <QUrl>
 
-Remote::Remote(const QString &name)
-    : m_name(name)
+Remote::Remote()
+    : m_name("")
     , m_mfg("")
     , m_rating(0)
     , m_voteCount(0)
+    , m_infoNAM(NULL)
+    , m_remoteNAM(NULL)
+    , m_ratingNAM(NULL)
 {
-    init();
-    updateInfo();
 }
 
 Remote::Remote(const QString &name, const QString &mfg,
         int rating, int voteCount)
-    : m_name(name), m_mfg(mfg),
-    m_rating(rating), m_voteCount(voteCount)
+    : m_name(name)
+    , m_mfg(mfg)
+    , m_rating(rating)
+    , m_voteCount(voteCount)
+    , m_infoNAM(NULL)
+    , m_remoteNAM(NULL)
+    , m_ratingNAM(NULL)
 {
-    init();
 }
 
 Remote::Remote(const Remote &r)
@@ -33,8 +38,17 @@ Remote::Remote(const Remote &r)
     , m_mfg(r.m_mfg)
     , m_rating(r.m_rating)
     , m_voteCount(r.m_voteCount)
+    , m_infoNAM(NULL)
+    , m_remoteNAM(NULL)
+    , m_ratingNAM(NULL)
+{
+}
+
+Remote::~Remote()
 {
-    init();
+    delete m_infoNAM;
+    delete m_remoteNAM;
+    delete m_ratingNAM;
 }
 
 Remote &Remote::operator=(const Remote &other)
@@ -48,41 +62,49 @@ Remote &Remote::operator=(const Remote &other)
     return *this;
 }
 
-void Remote::init()
-{
-    connect(&m_remoteNAM, SIGNAL(finished(QNetworkReply*)),
-            this, SLOT(remoteDownloadFinished(QNetworkReply*)));
-    connect(&m_infoNAM, SIGNAL(finished(QNetworkReply*)),
-            this, SLOT(infoRequestFinished(QNetworkReply *)));
-}
-
 void Remote::saveToFile()
 {
     if (!m_name.isEmpty()) {
-        QString url = m_settings.value("remoteUrl",
+        if (!m_remoteNAM) {
+            m_remoteNAM = new QNetworkAccessManager(this);
+            connect(m_remoteNAM, SIGNAL(finished(QNetworkReply*)),
+                    this, SLOT(remoteDownloadFinished(QNetworkReply*)));
+        }
+        QSettings settings(this);
+        QString url = settings.value("remoteUrl",
             "http://mercury.wipsl.com/irwi/uploaded/").toString()
             + m_name;
-        m_remoteNAM.get(QNetworkRequest(QUrl(url)));
-        m_settings.setValue("remoteName", m_name);
+        m_remoteNAM->get(QNetworkRequest(QUrl(url)));
+        settings.setValue("remoteName", m_name);
     }
 }
 
 void Remote::updateInfo()
 {
     if (!m_name.isEmpty()) {
-        QString url = m_settings.value("baseUrl",
+        if (!m_infoNAM) {
+            m_infoNAM = new QNetworkAccessManager(this);
+            connect(m_infoNAM, SIGNAL(finished(QNetworkReply*)),
+                    this, SLOT(infoRequestFinished(QNetworkReply *)));
+        }
+        QSettings settings(this);
+        QString url = settings.value("baseUrl",
             "http://mercury.wipsl.com/irwi/").toString()
             + "vote/get?name="
             + m_name;
-        m_infoNAM.get(QNetworkRequest(QUrl(url)));
+        m_infoNAM->get(QNetworkRequest(QUrl(url)));
     }
 }
 
 void Remote::sendRating(Rating::Rating r)
 {
     if (m_name != "") {
-        m_ratingNAM.get(QNetworkRequest(QUrl(
-            m_settings.value("baseUrl",
+        if (!m_ratingNAM) {
+            m_ratingNAM = new QNetworkAccessManager(this);
+        }
+        QSettings settings(this);
+        m_ratingNAM->get(QNetworkRequest(QUrl(
+            settings.value("baseUrl",
                 "http://mercury.wipsl.com/irwi/db.xml").toString() 
             + "vote/"
             + ((r == Rating::Up) ? "up" : "down")
@@ -94,9 +116,9 @@ void Remote::sendRating(Rating::Rating r)
 void Remote::remoteDownloadFinished(QNetworkReply *reply)
 {
     if (reply->error() == QNetworkReply::NoError) {
-        QFile file(m_settings.value("lircConf", "/etc/lircd.conf").toString());
-        if(file.open(QIODevice::WriteOnly))
-        {
+        QFile file(QSettings(this).value("lircConf",
+            "/etc/lircd.conf").toString());
+        if(file.open(QIODevice::WriteOnly)) {
             file.write(reply->readAll());
             file.close();
         }
@@ -105,8 +127,6 @@ void Remote::remoteDownloadFinished(QNetworkReply *reply)
     reply->deleteLater();
 
     std::system("sudo /etc/init.d/lirc reload");
-
-    //emit remoteDownloaded();
 }
 
 void Remote::infoRequestFinished(QNetworkReply *reply)
index 39a6ee3..32b8f61 100644 (file)
 class Remote : public QObject
 {
     Q_OBJECT
+
 public:
-    Remote(const QString &name);
-    Remote(const QString &name, const QString &mfg,
-            int rating, int voteCount);
+    Remote();
+    Remote(const QString &name, const QString &mfg = "",
+            int rating = 0, int voteCount = 0);
     Remote(const Remote &);
     Remote &operator=(const Remote &);
+    ~Remote();
 
     //getters
     QString name()  const { return m_name; }
@@ -43,12 +45,9 @@ private:
     int m_rating;
     int m_voteCount;
 
-    QSettings m_settings;
-    QNetworkAccessManager m_infoNAM;
-    QNetworkAccessManager m_remoteNAM;
-    QNetworkAccessManager m_ratingNAM;
-
-    void init();
+    QNetworkAccessManager *m_infoNAM;
+    QNetworkAccessManager *m_remoteNAM;
+    QNetworkAccessManager *m_ratingNAM;
 };
 
 #endif
index 3e517e4..b4d6fe6 100644 (file)
@@ -13,7 +13,7 @@
 RemoteDBMgr::RemoteDBMgr()
 {
     connect(&netAM, SIGNAL(finished(QNetworkReply *)), 
-            this,  SLOT(dbDownloadFinished(QNetworkReply *)));
+            this, SLOT(dbDownloadFinished(QNetworkReply *)));
 }
 
 RemoteDBMgr::~RemoteDBMgr()
index 19d78aa..4b7d5b3 100644 (file)
@@ -2,7 +2,6 @@
 #include "advsettingsdlg.h"
 #include "selectremotedlg.h"
 #include "aboutdlg.h"
-#include "remote.h"
 
 #include <QHBoxLayout>
 #include <QVBoxLayout>
 
 SettingsDlg::SettingsDlg(QWidget *parent)
     : QDialog(parent)
-    , remote(NULL)
 {
-    layout = new QVBoxLayout(this);
-    btnLayout = new QHBoxLayout(this);
-    remoteNameLayout = new QHBoxLayout(this);
+    QSettings settings(this);
+    m_layout = new QVBoxLayout(this);
+    m_btnLayout = new QHBoxLayout(this);
+    m_remoteNameLayout = new QHBoxLayout(this);
     
-    advSettingsBtn = new QPushButton(tr("Advanced"), this);
-    selectRemoteBtn = new QPushButton(tr("Select remote"), this);
-    aboutBtn = new QPushButton(tr("About"), this);
-    rateUpBtn = new QPushButton(
+    m_advSettingsBtn = new QPushButton(tr("Advanced"), this);
+    m_selectRemoteBtn = new QPushButton(tr("Select m_remote"), this);
+    m_aboutBtn = new QPushButton(tr("About"), this);
+    m_rateUpBtn = new QPushButton(
             QIcon(settings.value("rateUpIcon",
                 "/usr/share/icons/hicolor/48x48/hildon/chat_smiley_happy.png").
                 toString()),
             "", this);
-    rateDownBtn = new QPushButton(
+    m_rateDownBtn = new QPushButton(
             QIcon(settings.value("rateDownIcon",
                 "/usr/share/icons/hicolor/48x48/hildon/chat_smiley_sad.png").
                 toString()),
             "", this);
-    rateUpBtn->setMaximumSize(72, 72);
-    rateDownBtn->setMaximumSize(72, 72);
+    m_rateUpBtn->setMaximumSize(72, 72);
+    m_rateDownBtn->setMaximumSize(72, 72);
 
-    btnLayout->addWidget(advSettingsBtn);
-    btnLayout->addWidget(selectRemoteBtn);
-    btnLayout->addWidget(aboutBtn);
+    m_btnLayout->addWidget(m_advSettingsBtn);
+    m_btnLayout->addWidget(m_selectRemoteBtn);
+    m_btnLayout->addWidget(m_aboutBtn);
 
-    remoteNameLabel = new QLabel(this);
-    ratingLabel = new QLabel(this);
-    remoteNameLayout->addWidget(remoteNameLabel);
-    remoteNameLayout->addWidget(ratingLabel);
-    remoteNameLayout->addWidget(rateUpBtn);
-    remoteNameLayout->addWidget(rateDownBtn);
+    m_remoteNameLabel = new QLabel(this);
+    m_ratingLabel = new QLabel(this);
+    m_remoteNameLayout->addWidget(m_remoteNameLabel);
+    m_remoteNameLayout->addWidget(m_ratingLabel);
+    m_remoteNameLayout->addWidget(m_rateUpBtn);
+    m_remoteNameLayout->addWidget(m_rateDownBtn);
 
-    connect(advSettingsBtn, SIGNAL(clicked()),
+    connect(m_advSettingsBtn, SIGNAL(clicked()),
             this, SLOT(showAdvSettingsDlg()));
-    connect(selectRemoteBtn, SIGNAL(clicked()),
+    connect(m_selectRemoteBtn, SIGNAL(clicked()),
             this, SLOT(showSelectRemoteDlg()));
-    connect(aboutBtn, SIGNAL(clicked()),
+    connect(m_aboutBtn, SIGNAL(clicked()),
             this, SLOT(showAboutDlg()));
-    connect(rateUpBtn, SIGNAL(clicked()),
+    connect(m_rateUpBtn, SIGNAL(clicked()),
             this, SLOT(rateUpClicked()));
-    connect(rateDownBtn, SIGNAL(clicked()),
+    connect(m_rateDownBtn, SIGNAL(clicked()),
             this, SLOT(rateDownClicked()));
  
-    layout->addLayout(remoteNameLayout);
-    layout->addLayout(btnLayout);
-    this->setLayout(layout);
+    m_layout->addLayout(m_remoteNameLayout);
+    m_layout->addLayout(m_btnLayout);
+    this->setLayout(m_layout);
 
-    changeRemote();
+    initRemote();
 }
 
 SettingsDlg::~SettingsDlg()
 {
-    delete advSettingsBtn;
-    delete selectRemoteBtn;
-    delete rateUpBtn;
-    delete rateDownBtn;
-    delete aboutBtn;
-    delete remoteNameLabel;
-    delete ratingLabel;
-    delete btnLayout;
-    delete remoteNameLayout;
-    delete layout;
-    if (remote) {
-        delete remote;
-    }
+    delete m_advSettingsBtn;
+    delete m_selectRemoteBtn;
+    delete m_rateUpBtn;
+    delete m_rateDownBtn;
+    delete m_aboutBtn;
+    delete m_remoteNameLabel;
+    delete m_ratingLabel;
+    delete m_btnLayout;
+    delete m_remoteNameLayout;
+    delete m_layout;
 }
 
 void SettingsDlg::showAdvSettingsDlg()
@@ -91,7 +87,8 @@ void SettingsDlg::showAdvSettingsDlg()
 void SettingsDlg::showSelectRemoteDlg()
 {
     SelectRemoteDlg dlg(this);
-    changeRemote();
+    connect(&dlg, SIGNAL(m_remoteChanged(Remote)),
+            this, SLOT(setRemote(Remote)));
     dlg.exec();
 }
 
@@ -101,46 +98,51 @@ void SettingsDlg::showAboutDlg()
     dlg.exec();
 }
 
-void SettingsDlg::changeRemote()
+void SettingsDlg::initRemote()
 {
-    QString selectedRemote = settings.value("remoteName", "").toString();
+    QString selectedRemote = QSettings(this).value("remoteName", "").toString();
     if (selectedRemote == "") {
-        remoteNameLabel->setText("No remote selected");
+        m_remoteNameLabel->setText("No remote selected");
         enableRateBtns(false);
     } else {
-        if (remote) {
-            delete remote;
-        }
-        remote = new Remote(selectedRemote);
-        connect(remote, SIGNAL(infoUpdated()),
+        m_remote = Remote(selectedRemote);
+        connect(&m_remote, SIGNAL(infoUpdated()),
                 this, SLOT(updateRemoteInfo()));
-        remote->updateInfo();
+        m_remote.updateInfo();
         enableRateBtns();
     }
 }
 
+void SettingsDlg::setRemote(Remote r)
+{
+    m_remote = r;
+    updateRemoteInfo();
+    enableRateBtns();
+}
+
 void SettingsDlg::updateRemoteInfo()
 {
-    remoteNameLabel->setText(remote->mfg() + " " + remote->name());
-    ratingLabel->setText(tr("Rating") + ": " + remote->rating());
+    m_remoteNameLabel->setText(m_remote.mfg() + " " + m_remote.name());
+    m_ratingLabel->setText(tr("Rating") + ": "
+            + QString::number(m_remote.rating()));
 }
 
 void SettingsDlg::rateUpClicked()
 {
-    remote->sendRating(Rating::Up);
+    m_remote.sendRating(Rating::Up);
     enableRateBtns(false);
 }
 
 void SettingsDlg::rateDownClicked()
 {
-    remote->sendRating(Rating::Down);
+    m_remote.sendRating(Rating::Down);
     enableRateBtns(false);
 }
 
 void SettingsDlg::enableRateBtns(bool enable)
 {
-    rateUpBtn->setEnabled(enable);
-    rateDownBtn->setEnabled(enable);
+    m_rateUpBtn->setEnabled(enable);
+    m_rateDownBtn->setEnabled(enable);
 }
 
 
index 0f08d80..d583048 100644 (file)
@@ -4,6 +4,8 @@
 #include <QDialog>
 #include <QSettings>
 
+#include "remote.h"
+
 class QWidget;
 class QHBoxLayout;
 class QVBoxLayout;
@@ -22,28 +24,28 @@ private slots:
     void showAdvSettingsDlg();
     void showSelectRemoteDlg();
     void showAboutDlg();
-    void changeRemote();
+    void setRemote(Remote);
     void updateRemoteInfo();
     void rateUpClicked();
     void rateDownClicked();
 
 private:
     void enableRateBtns(bool enable = true);
+    void initRemote();
 
 private:
-    QSettings settings;
-
-    QVBoxLayout *layout;
-    QHBoxLayout *btnLayout;
-    QHBoxLayout *remoteNameLayout;
-    QPushButton *advSettingsBtn;
-    QPushButton *selectRemoteBtn;
-    QPushButton *rateUpBtn;
-    QPushButton *rateDownBtn;
-    QPushButton *aboutBtn;
-    QLabel *remoteNameLabel;
-    QLabel *ratingLabel;
-    Remote *remote;
+    Remote m_remote;
+
+    QVBoxLayout *m_layout;
+    QHBoxLayout *m_btnLayout;
+    QHBoxLayout *m_remoteNameLayout;
+    QPushButton *m_advSettingsBtn;
+    QPushButton *m_selectRemoteBtn;
+    QPushButton *m_rateUpBtn;
+    QPushButton *m_rateDownBtn;
+    QPushButton *m_aboutBtn;
+    QLabel *m_remoteNameLabel;
+    QLabel *m_ratingLabel;
 };
 
 #endif