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