Rating is now updated immediately after pressing rating buttons
[irwi] / src / remote.cpp
1 #include "remote.h"
2
3 #include <cstdlib>
4
5 #include <QString>
6 #include <QFile>
7 #include <QNetworkAccessManager>
8 #include <QNetworkRequest>
9 #include <QNetworkReply>
10 #include <QUrl>
11
12 Remote::Remote()
13     : m_name("")
14     , m_mfg("")
15     , m_rating(0)
16     , m_voteCount(0)
17     , m_infoNAM(NULL)
18     , m_remoteNAM(NULL)
19     , m_ratingNAM(NULL)
20 {
21 }
22
23 Remote::Remote(const QString &name, const QString &mfg,
24         int rating, int voteCount)
25     : m_name(name)
26     , m_mfg(mfg)
27     , m_rating(rating)
28     , m_voteCount(voteCount)
29     , m_infoNAM(NULL)
30     , m_remoteNAM(NULL)
31     , m_ratingNAM(NULL)
32 {
33 }
34
35 Remote::Remote(const Remote &r)
36     : QObject()
37     , m_name(r.m_name)
38     , m_mfg(r.m_mfg)
39     , m_rating(r.m_rating)
40     , m_voteCount(r.m_voteCount)
41     , m_infoNAM(NULL)
42     , m_remoteNAM(NULL)
43     , m_ratingNAM(NULL)
44 {
45 }
46
47 Remote::~Remote()
48 {
49     delete m_infoNAM;
50     delete m_remoteNAM;
51     delete m_ratingNAM;
52 }
53
54 Remote &Remote::operator=(const Remote &other)
55 {
56     if (this != &other) {
57         m_name = other.m_name;
58         m_mfg = other.m_mfg;
59         m_rating = other.m_rating;
60         m_voteCount = other.m_voteCount;
61     }
62     return *this;
63 }
64
65 bool Remote::operator==(const Remote &other) const
66 {
67     return (m_name == other.m_name &&
68             m_mfg == other.m_mfg &&
69             m_rating == other.m_rating &&
70             m_voteCount == other.m_voteCount);
71 }
72
73 void Remote::saveToFile()
74 {
75     if (!m_name.isEmpty()) {
76         if (!m_remoteNAM) {
77             m_remoteNAM = new QNetworkAccessManager(this);
78             connect(m_remoteNAM, SIGNAL(finished(QNetworkReply*)),
79                     this, SLOT(remoteDownloadFinished(QNetworkReply*)));
80         }
81         QSettings settings(this);
82         QString url = settings.value("baseUrl",
83             "http://mercury.wipsl.com/irwi/").toString()
84             + "uploaded/"
85             + m_name;
86         m_remoteNAM->get(QNetworkRequest(QUrl(url)));
87         settings.setValue("remoteName", m_name);
88     }
89 }
90
91 void Remote::updateInfo()
92 {
93     if (!m_name.isEmpty()) {
94         if (!m_infoNAM) {
95             m_infoNAM = new QNetworkAccessManager(this);
96             connect(m_infoNAM, SIGNAL(finished(QNetworkReply*)),
97                     this, SLOT(infoRequestFinished(QNetworkReply *)));
98         }
99         QSettings settings(this);
100         QString url = settings.value("baseUrl",
101             "http://mercury.wipsl.com/irwi/").toString()
102             + "vote/get?name="
103             + m_name;
104         m_infoNAM->get(QNetworkRequest(QUrl(url)));
105     }
106 }
107
108 void Remote::sendRating(Rating::Rating r)
109 {
110     if (!m_name.isEmpty()) {
111         if (!m_ratingNAM) {
112             m_ratingNAM = new QNetworkAccessManager(this);
113             connect(m_ratingNAM, SIGNAL(finished(QNetworkReply *)),
114                     this, SIGNAL(ratingSent()));
115             connect(this, SIGNAL(ratingSent()), this, SLOT(updateInfo()));
116         }
117         QSettings settings(this);
118         m_ratingNAM->get(QNetworkRequest(QUrl(
119             settings.value("baseUrl",
120                 "http://mercury.wipsl.com/irwi/").toString() 
121             + "vote/"
122             + ((r == Rating::Up) ? "up" : "down")
123             + "?name=" 
124             + m_name)));
125    }
126 }
127
128 void Remote::remoteDownloadFinished(QNetworkReply *reply)
129 {
130     if (reply->error() == QNetworkReply::NoError) {
131         QFile file(QSettings(this).value("lircConf",
132             "/etc/lircd.conf").toString());
133         if(file.open(QIODevice::WriteOnly)) {
134             file.write(reply->readAll());
135             file.close();
136         }
137     }
138     reply->close();
139     reply->deleteLater();
140
141     std::system("sudo /etc/init.d/lirc reload");
142
143     emit saveFinished();
144 }
145
146 void Remote::infoRequestFinished(QNetworkReply *reply)
147 {
148     if (reply->error() == QNetworkReply::NoError) {
149         m_rating    = QString(reply->readLine(20)).toInt();
150         m_voteCount = QString(reply->readLine(20)).toInt();
151         m_mfg       = QString(reply->readLine(20)).trimmed();
152     }
153     reply->close();
154     reply->deleteLater();
155
156     emit infoUpdated();
157 }
158