Fixed layout hierarchy
[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 void Remote::saveToFile()
66 {
67     if (!m_name.isEmpty()) {
68         if (!m_remoteNAM) {
69             m_remoteNAM = new QNetworkAccessManager(this);
70             connect(m_remoteNAM, SIGNAL(finished(QNetworkReply*)),
71                     this, SLOT(remoteDownloadFinished(QNetworkReply*)));
72         }
73         QSettings settings(this);
74         QString url = settings.value("baseUrl",
75             "http://mercury.wipsl.com/irwi/").toString()
76             + "uploaded/"
77             + m_name;
78         m_remoteNAM->get(QNetworkRequest(QUrl(url)));
79         settings.setValue("remoteName", m_name);
80     }
81 }
82
83 void Remote::updateInfo()
84 {
85     if (!m_name.isEmpty()) {
86         if (!m_infoNAM) {
87             m_infoNAM = new QNetworkAccessManager(this);
88             connect(m_infoNAM, SIGNAL(finished(QNetworkReply*)),
89                     this, SLOT(infoRequestFinished(QNetworkReply *)));
90         }
91         QSettings settings(this);
92         QString url = settings.value("baseUrl",
93             "http://mercury.wipsl.com/irwi/").toString()
94             + "vote/get?name="
95             + m_name;
96         m_infoNAM->get(QNetworkRequest(QUrl(url)));
97     }
98 }
99
100 void Remote::sendRating(Rating::Rating r)
101 {
102     if (!m_name.isEmpty()) {
103         if (!m_ratingNAM) {
104             m_ratingNAM = new QNetworkAccessManager(this);
105         }
106         QSettings settings(this);
107         m_ratingNAM->get(QNetworkRequest(QUrl(
108             settings.value("baseUrl",
109                 "http://mercury.wipsl.com/irwi/").toString() 
110             + "vote/"
111             + ((r == Rating::Up) ? "up" : "down")
112             + "?name=" 
113             + m_name)));
114    }
115 }
116
117 void Remote::remoteDownloadFinished(QNetworkReply *reply)
118 {
119     if (reply->error() == QNetworkReply::NoError) {
120         QFile file(QSettings(this).value("lircConf",
121             "/etc/lircd.conf").toString());
122         if(file.open(QIODevice::WriteOnly)) {
123             file.write(reply->readAll());
124             file.close();
125         }
126     }
127     reply->close();
128     reply->deleteLater();
129
130     std::system("sudo /etc/init.d/lirc reload");
131
132     emit saveFinished();
133 }
134
135 void Remote::infoRequestFinished(QNetworkReply *reply)
136 {
137     if (reply->error() == QNetworkReply::NoError) {
138         m_rating    = QString(reply->readLine(20)).toInt();
139         m_voteCount = QString(reply->readLine(20)).toInt();
140         m_mfg       = QString(reply->readLine(20));
141     }
142     reply->close();
143     reply->deleteLater();
144
145     emit infoUpdated();
146 }
147