Website updated.
[irwi] / src / remote.cpp
index 7ebab5a..f87c6c0 100644 (file)
-#include "irctrl.h"
 #include "remote.h"
-#include "netio.h"
 
-#include <QSettings>
+#include <cstdlib>
+
+#include <QString>
+#include <QFile>
+#include <QNetworkAccessManager>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QUrl>
 
 Remote::Remote()
-    : rated(false)
-    , model("", "")
-    , settings(new QSettings())
-    , irCtrl(new IrCtrl())
-    , netIO(NULL)
+    : m_name("")
+    , m_mfg("")
+    , m_rating(0)
+    , m_voteCount(0)
+    , m_infoNAM(NULL)
+    , m_remoteNAM(NULL)
+    , m_ratingNAM(NULL)
+{
+}
+
+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_infoNAM(NULL)
+    , m_remoteNAM(NULL)
+    , m_ratingNAM(NULL)
 {
 }
 
-Remote::Remote(const QString &id, NetIO *netIO)
-    : rated(false)
-    , settings(new QSettings())
-    , irCtrl(new IrCtrl())
-    , netIO(netIO)
+Remote::Remote(const Remote &r)
+    : QObject()
+    , m_name(r.m_name)
+    , m_mfg(r.m_mfg)
+    , m_rating(r.m_rating)
+    , m_voteCount(r.m_voteCount)
+    , m_infoNAM(NULL)
+    , m_remoteNAM(NULL)
+    , m_ratingNAM(NULL)
 {
-    setId(id);
 }
 
 Remote::~Remote()
 {
-    delete settings;
-    delete irCtrl;
+    delete m_infoNAM;
+    delete m_remoteNAM;
+    delete m_ratingNAM;
 }
 
-const QString &Remote::id() const
+Remote &Remote::operator=(const Remote &other)
 {
-    return model.id;
+    if (this != &other) {
+        m_name = other.m_name;
+        m_mfg = other.m_mfg;
+        m_rating = other.m_rating;
+        m_voteCount = other.m_voteCount;
+    }
+    return *this;
 }
 
-void Remote::setId(const QString &id)
+bool Remote::operator==(const Remote &other) const
 {
-    model.id = id;
+    return (m_name == other.m_name &&
+            m_mfg == other.m_mfg &&
+            m_rating == other.m_rating &&
+            m_voteCount == other.m_voteCount);
 }
 
-void Remote::setNetIO(NetIO *netIO)
+void Remote::saveToFile()
 {
-    this->netIO = netIO;
+    if (!m_name.isEmpty()) {
+        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("baseUrl",
+            "http://mercury.wipsl.com/irwi/").toString()
+            + "uploaded/"
+            + m_name;
+        m_remoteNAM->get(QNetworkRequest(QUrl(url)));
+        settings.setValue("remoteName", m_name);
+        settings.setValue("remoteMfg", m_mfg);
+    }
 }
 
-void Remote::updateRating()
+void Remote::updateInfo()
 {
-    connect(netIO, SIGNAL(ratingDownloaded(int)),
-            this, SIGNAL(ratingChanged(int)));
-    netIO->ratingById(model.id);
+    if (!m_name.isEmpty()) {
+        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)));
+    }
 }
 
-void Remote::sendRating(bool)
+void Remote::sendRating(Rating::Rating r)
 {
+    if (!m_name.isEmpty()) {
+        if (!m_ratingNAM) {
+            m_ratingNAM = new QNetworkAccessManager(this);
+            connect(m_ratingNAM, SIGNAL(finished(QNetworkReply *)),
+                    this, SIGNAL(ratingSent()));
+            connect(this, SIGNAL(ratingSent()), this, SLOT(updateInfo()));
+        }
+        QSettings settings(this);
+        m_ratingNAM->get(QNetworkRequest(QUrl(
+            settings.value("baseUrl",
+                "http://mercury.wipsl.com/irwi/").toString() 
+            + "vote/"
+            + ((r == Rating::Up) ? "up" : "down")
+            + "?name=" 
+            + m_name)));
+   }
 }
 
-void Remote::sendCmd(RemoteCmd cmd)
+void Remote::remoteDownloadFinished(QNetworkReply *reply)
 {
-    switch (cmd)
-    {
-        case VOLUMEUP:
-            irCtrl->sendCmd(
-                    model.name,
-                    settings->value("cmdVolumeUp", "VOLUP").toString());
-            break;
-
-        case VOLUMEDOWN:
-            irCtrl->sendCmd(
-                    model.name,
-                    settings->value("cmdVolumeDown", "VOLUP").toString());
-            break;
-
-        case CHANNELUP:
-            irCtrl->sendCmd(
-                    model.name,
-                    settings->value("cmdChannelUp", "CHUP").toString());
-            break;
-
-        case CHANNELDOWN:
-            irCtrl->sendCmd(
-                    model.name,
-                    settings->value("cmdChannelDown", "CHDOWN").toString());
-            break;
-
-        case POWER:
-            irCtrl->sendCmd(
-                    model.name,
-                    settings->value("cmdPower", "POWER").toString());
-            break;
-
-        case MUTE:
-            irCtrl->sendCmd(
-                    model.name,
-                    settings->value("cmdMute", "MUTE").toString());
-            break;
-
-        default:
-            break;
+    if (reply->error() == QNetworkReply::NoError) {
+        QFile file(QSettings(this).value("lircConf",
+            "/etc/lircd.conf").toString());
+        if(file.open(QIODevice::WriteOnly)) {
+            file.write(reply->readAll());
+            file.close();
+        }
     }
+    reply->close();
+    reply->deleteLater();
+
+    std::system("sudo /etc/init.d/lirc reload");
+
+    emit saveFinished();
 }
 
+void Remote::infoRequestFinished(QNetworkReply *reply)
+{
+    if (reply->error() == QNetworkReply::NoError) {
+        m_rating    = QString(reply->readLine(20)).toInt();
+        m_voteCount = QString(reply->readLine(20)).toInt();
+        m_mfg       = QString(reply->readLine(20)).trimmed();
+    }
+    reply->close();
+    reply->deleteLater();
 
+    emit infoUpdated();
+}