Merge branch 'master' of mercury.wipsl.com:/var/git/irwi
[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         }
114         QSettings settings(this);
115         m_ratingNAM->get(QNetworkRequest(QUrl(
116             settings.value("baseUrl",
117                 "http://mercury.wipsl.com/irwi/").toString() 
118             + "vote/"
119             + ((r == Rating::Up) ? "up" : "down")
120             + "?name=" 
121             + m_name)));
122    }
123 }
124
125 void Remote::remoteDownloadFinished(QNetworkReply *reply)
126 {
127     if (reply->error() == QNetworkReply::NoError) {
128         QFile file(QSettings(this).value("lircConf",
129             "/etc/lircd.conf").toString());
130         if(file.open(QIODevice::WriteOnly)) {
131             file.write(reply->readAll());
132             file.close();
133         }
134     }
135     reply->close();
136     reply->deleteLater();
137
138     std::system("sudo /etc/init.d/lirc reload");
139
140     emit saveFinished();
141 }
142
143 void Remote::infoRequestFinished(QNetworkReply *reply)
144 {
145     if (reply->error() == QNetworkReply::NoError) {
146         m_rating    = QString(reply->readLine(20)).toInt();
147         m_voteCount = QString(reply->readLine(20)).toInt();
148         m_mfg       = QString(reply->readLine(20)).trimmed();
149     }
150     reply->close();
151     reply->deleteLater();
152
153     emit infoUpdated();
154 }
155