fix
[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(const QString &name)
13     : m_name(name)
14     , m_mfg("")
15     , m_rating(0)
16     , m_voteCount(0)
17 {
18     init();
19     updateInfo();
20 }
21
22 Remote::Remote(const QString &name, const QString &mfg,
23         int rating, int voteCount)
24     : m_name(name), m_mfg(mfg),
25     m_rating(rating), m_voteCount(voteCount)
26 {
27     init();
28 }
29
30 Remote::Remote(const Remote &r)
31     : QObject()
32     , m_name(r.m_name)
33     , m_mfg(r.m_mfg)
34     , m_rating(r.m_rating)
35     , m_voteCount(r.m_voteCount)
36 {
37     init();
38 }
39
40 Remote &Remote::operator=(const Remote &other)
41 {
42     if (this != &other) {
43         m_name = other.m_name;
44         m_mfg = other.m_mfg;
45         m_rating = other.m_rating;
46         m_voteCount = other.m_voteCount;
47     }
48     return *this;
49 }
50
51 void Remote::init()
52 {
53     connect(&m_remoteNAM, SIGNAL(finished(QNetworkReply*)),
54             this, SLOT(remoteDownloadFinished(QNetworkReply*)));
55     connect(&m_infoNAM, SIGNAL(finished(QNetworkReply*)),
56             this, SLOT(infoRequestFinished(QNetworkReply *)));
57 }
58
59 void Remote::saveToFile()
60 {
61     if (!m_name.isEmpty()) {
62         QString url = m_settings.value("remoteUrl",
63             "http://mercury.wipsl.com/irwi/uploaded/").toString()
64             + m_name;
65         m_remoteNAM.get(QNetworkRequest(QUrl(url)));
66         m_settings.setValue("remoteName", m_name);
67     }
68 }
69
70 void Remote::updateInfo()
71 {
72     if (!m_name.isEmpty()) {
73         QString url = m_settings.value("baseUrl",
74             "http://mercury.wipsl.com/irwi/").toString()
75             + "vote/get?name="
76             + m_name;
77         m_infoNAM.get(QNetworkRequest(QUrl(url)));
78     }
79 }
80
81 void Remote::sendRating(Rating::Rating r)
82 {
83     if (m_name != "") {
84         m_ratingNAM.get(QNetworkRequest(QUrl(
85             m_settings.value("baseUrl",
86                 "http://mercury.wipsl.com/irwi/db.xml").toString() 
87             + "vote/"
88             + ((r == Rating::Up) ? "up" : "down")
89             + "?name=" 
90             + m_name)));
91    }
92 }
93
94 void Remote::remoteDownloadFinished(QNetworkReply *reply)
95 {
96     if (reply->error() == QNetworkReply::NoError) {
97         QFile file(m_settings.value("lircConf", "/etc/lircd.conf").toString());
98         if(file.open(QIODevice::WriteOnly))
99         {
100             file.write(reply->readAll());
101             file.close();
102         }
103     }
104     reply->close();
105     reply->deleteLater();
106
107     std::system("sudo /etc/init.d/lirc reload");
108
109     //emit remoteDownloaded();
110 }
111
112 void Remote::infoRequestFinished(QNetworkReply *reply)
113 {
114     if (reply->error() == QNetworkReply::NoError) {
115         m_rating    = QString(reply->readLine(20)).toInt();
116         m_voteCount = QString(reply->readLine(20)).toInt();
117     }
118     reply->close();
119     reply->deleteLater();
120
121     emit infoUpdated();
122 }
123