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