Website updated.
[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         settings.setValue("remoteMfg", m_mfg);
89     }
90 }
91
92 void Remote::updateInfo()
93 {
94     if (!m_name.isEmpty()) {
95         if (!m_infoNAM) {
96             m_infoNAM = new QNetworkAccessManager(this);
97             connect(m_infoNAM, SIGNAL(finished(QNetworkReply*)),
98                     this, SLOT(infoRequestFinished(QNetworkReply *)));
99         }
100         QSettings settings(this);
101         QString url = settings.value("baseUrl",
102             "http://mercury.wipsl.com/irwi/").toString()
103             + "vote/get?name="
104             + m_name;
105         m_infoNAM->get(QNetworkRequest(QUrl(url)));
106     }
107 }
108
109 void Remote::sendRating(Rating::Rating r)
110 {
111     if (!m_name.isEmpty()) {
112         if (!m_ratingNAM) {
113             m_ratingNAM = new QNetworkAccessManager(this);
114             connect(m_ratingNAM, SIGNAL(finished(QNetworkReply *)),
115                     this, SIGNAL(ratingSent()));
116             connect(this, SIGNAL(ratingSent()), this, SLOT(updateInfo()));
117         }
118         QSettings settings(this);
119         m_ratingNAM->get(QNetworkRequest(QUrl(
120             settings.value("baseUrl",
121                 "http://mercury.wipsl.com/irwi/").toString() 
122             + "vote/"
123             + ((r == Rating::Up) ? "up" : "down")
124             + "?name=" 
125             + m_name)));
126    }
127 }
128
129 void Remote::remoteDownloadFinished(QNetworkReply *reply)
130 {
131     if (reply->error() == QNetworkReply::NoError) {
132         QFile file(QSettings(this).value("lircConf",
133             "/etc/lircd.conf").toString());
134         if(file.open(QIODevice::WriteOnly)) {
135             file.write(reply->readAll());
136             file.close();
137         }
138     }
139     reply->close();
140     reply->deleteLater();
141
142     std::system("sudo /etc/init.d/lirc reload");
143
144     emit saveFinished();
145 }
146
147 void Remote::infoRequestFinished(QNetworkReply *reply)
148 {
149     if (reply->error() == QNetworkReply::NoError) {
150         m_rating    = QString(reply->readLine(20)).toInt();
151         m_voteCount = QString(reply->readLine(20)).toInt();
152         m_mfg       = QString(reply->readLine(20)).trimmed();
153     }
154     reply->close();
155     reply->deleteLater();
156
157     emit infoUpdated();
158 }
159