1 /*
2  * Copyright © 2015-2016 Antti Lamminsalo
3  *
4  * This file is part of Orion.
5  *
6  * Orion is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with Orion.  If not, see <http://www.gnu.org/licenses/>.
13  */
14 
15 #include "channel.h"
16 
~Channel()17 Channel::~Channel(){
18     //qDebug() << "Destroyer: Channel";
19 }
20 
updateWith(const Channel & other)21 void Channel::updateWith(const Channel &other)
22 {
23     if (this->getId() == other.getId()) {
24         this->setName(other.getName());
25         this->setLogourl(other.getLogourl());
26         this->setInfo(other.getInfo());
27         this->setViewers(other.getViewers());
28         this->setGame(other.getGame());
29     }
30 }
31 
getId() const32 quint32 Channel::getId() const
33 {
34     return id;
35 }
36 
setId(const quint32 & value)37 void Channel::setId(const quint32 &value)
38 {
39     id = value;
40 }
41 
isFavourite() const42 bool Channel::isFavourite() const
43 {
44     return favourite;
45 }
46 
setFavourite(bool value)47 void Channel::setFavourite(bool value)
48 {
49     favourite = value;
50 }
51 
Channel()52 Channel::Channel(){
53     alert = true;
54     timestamp = 0;
55     online = false;
56     viewers = 0;
57     id = 0;
58     favourite = false;
59 }
60 
Channel(const quint64 newId)61 Channel::Channel(const quint64 newId) : Channel() {
62     this->id = newId;
63 }
64 
Channel(const Channel & channel)65 Channel::Channel(const Channel &channel){
66     this->id = channel.id;
67 	this->name = channel.name;
68     this->serviceName = channel.serviceName;
69 	this->info = channel.info;
70 	this->alert = channel.alert;
71 	this->timestamp = channel.timestamp;
72 	this->online = channel.online;
73     this->logouri = channel.logouri;
74     this->previewuri = channel.previewuri;
75     this->game = channel.game;
76     this->viewers = channel.viewers;
77     this->favourite = channel.favourite;
78 }
79 
lastOnline()80 const QString Channel::lastOnline(){
81     if (timestamp == 0){
82         return "Never";
83     }
84     else{
85         return QDateTime::fromMSecsSinceEpoch(timestamp).toString("MMM dd, hh:mm");
86     }
87 }
88 
getJSON() const89 const QJsonObject Channel::getJSON() const{
90     QVariantMap map;
91     map["title"]    = QVariant(name);
92     map["uri"]      = QVariant(serviceName);
93     map["info"]     = QVariant(info);
94     map["logo"]     = QVariant(logouri);
95     map["preview"]  = QVariant(previewuri);
96     map["alert"]    = QVariant(alert);
97     map["lastSeen"] = QVariant(timestamp);
98     map["id"]       = QVariant(id);
99     return QJsonObject::fromVariantMap(map);
100 }
101 
writeToSettings(QSettings & settings) const102 void Channel::writeToSettings(QSettings &settings) const
103 {
104     settings.setValue("name", name);
105     settings.setValue("serviceName", serviceName);
106     settings.setValue("info", info);
107     settings.setValue("logouri", logouri);
108     settings.setValue("previewuri", previewuri);
109     settings.setValue("alert", alert);
110     settings.setValue("timestamp", timestamp);
111     settings.setValue("id", id);
112 }
113 
Channel(const QSettings & settings)114 Channel::Channel(const QSettings &settings)
115 {
116     //Deserialization constructor
117 
118     name = settings.value("name").toString();
119     serviceName = settings.value("serviceName").toString();
120     info = settings.value("info").toString();
121     logouri = settings.value("logouri").toString();
122     previewuri = settings.value("previewuri").toString();
123     alert = settings.value("alert").toBool();
124     timestamp = settings.value("timestamp").toLongLong();
125     id = settings.value("id").toInt();
126 }
127 
setName(const QString & newName)128 void Channel::setName(const QString &newName){
129 	name = newName;
130 
131 }
132 
setServiceName(const QString & newUri)133 void Channel::setServiceName(const QString &newUri){
134     serviceName = newUri;
135 }
136 
setInfo(const QString & newInfo)137 void Channel::setInfo(const QString &newInfo){
138 	info = newInfo;
139 }
140 
setAlert(bool newAlert)141 void Channel::setAlert(bool newAlert){
142     alert = newAlert;
143 }
144 
updateTime()145 void Channel::updateTime(){
146     timestamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
147 }
148 
getTime() const149 qint64 Channel::getTime() const{
150 	return timestamp;
151 }
152 
getName() const153 const QString Channel::getName() const{
154 	return name;
155 }
156 
getServiceName() const157 const QString Channel::getServiceName() const{
158     return serviceName;
159 }
160 
getFullUri() const161 const QString Channel::getFullUri() const{
162     return "http://twitch.tv/" + serviceName;
163 }
164 
getInfo() const165 const QString Channel::getInfo() const{
166 	return info;
167 }
168 
hasAlert()169 bool Channel::hasAlert(){
170     return alert;
171 }
172 
173 
setOnline(bool b)174 void Channel::setOnline(bool b){
175 	online = b;
176     if (online)
177         updateTime();
178     emit updated();
179 }
180 
isOnline()181 bool Channel::isOnline(){
182 	return online;
183 }
184 
setLogourl(const QString & uri)185 void Channel::setLogourl(const QString &uri){
186     logouri = uri;
187 }
188 
getLogourl() const189 const QString Channel::getLogourl() const{
190     return !logouri.isEmpty() ? logouri : DEFAULT_LOGO_URL;
191 }
192 
setPreviewurl(const QString & uri)193 void Channel::setPreviewurl(const QString &uri){
194     previewuri = uri;
195 }
196 
getPreviewurl()197 const QString Channel::getPreviewurl(){
198     return previewuri;
199 }
200 
setLastSeen(time_t time)201 void Channel::setLastSeen(time_t time){
202     this->timestamp = time;
203 }
204 
greaterThan(Channel * a,Channel * b)205 bool Channel::greaterThan (Channel* a, Channel* b) {
206     if (a->isOnline() == b->isOnline()){ //BOTH ONLINE OR BOTH OFFLINE
207         if (a->isOnline()){  //BOTH ONLINE, COMPARISON BY VIEWER COUNT
208             return (a->getViewers() >= b->getViewers());
209         }
210         else{ //BOTH OFFLINE, COMPARISON BY DEFAULT QSTRING METHOD
211             return (QString::compare(a->getName(),b->getName()) < 0);
212         }
213     }
214     return a->isOnline();    //OTHER IS ONLINE AND OTHER IS NOT
215 }
216 
getViewers() const217 qint32 Channel::getViewers() const
218 {
219     return online ? viewers : -1;
220 }
221 
setViewers(qint32 value)222 void Channel::setViewers(qint32 value)
223 {
224     viewers = value;
225 }
226 
getGame() const227 const QString Channel::getGame() const
228 {
229     return game;
230 }
231 
setGame(const QString & value)232 void Channel::setGame(const QString &value)
233 {
234     game = value;
235 }
236