defuze.me
Client
|
00001 /************************************************************************** 00002 ** defuze.me Epitech Innovative Project 00003 ** 00004 ** Copyright 2010-2011 00005 ** Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard 00006 ** Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier 00007 ** 00008 ** All rights reserved. 00009 **************************************************************************/ 00010 00011 #include "queueable.hpp" 00012 #include "jsonparser.hpp" 00013 #include "exception.hpp" 00014 #include "audiotrack.hpp" 00015 #include "queuebreak.hpp" 00016 #include "queuetrack.hpp" 00017 #include <QSqlQuery> 00018 #include <QSqlError> 00019 #include <QTextCodec> 00020 00021 using namespace Queue; 00022 00023 Queueable::Queueable(QObject *parent) : QObject(parent), container(NULL), 00024 controls_active(false), controls(0) 00025 { 00026 position = -1; 00027 } 00028 00029 Queueable::~Queueable() 00030 { 00031 } 00032 00033 void Queueable::setContainer(Container* c) 00034 { 00035 container = c; 00036 } 00037 00038 bool Queueable::hasContainer() const 00039 { 00040 return(container != NULL); 00041 } 00042 00043 Container* Queueable::getContainer() const 00044 { 00045 return container; 00046 } 00047 00048 int Queueable::getPosition() const 00049 { 00050 return position; 00051 } 00052 00053 const QVariantMap& Queueable::getQueueAttributes() const 00054 { 00055 return attributes; 00056 } 00057 00058 QVariantMap Queueable::getContent(bool forWeb) const 00059 { 00060 Q_UNUSED(forWeb); 00061 return QVariantMap(); 00062 } 00063 00064 bool Queueable::queueIsFinite() const 00065 { 00066 return true; 00067 } 00068 00069 int Queueable::queueDuration(QDateTime) const 00070 { 00071 return 1; 00072 } 00073 00074 void Queueable::setPlayTime(const QDateTime& time) 00075 { 00076 play_at = time; 00077 } 00078 00079 const QDateTime& Queueable::getPlayTime() const 00080 { 00081 return play_at; 00082 } 00083 00084 int Queueable::queueId() const 00085 { 00086 return 0; 00087 } 00088 00089 QVariantMap Queueable::getNetData() const 00090 { 00091 QVariantMap evt; 00092 00093 evt["position"] = getPosition(); 00094 evt["type"] = queueType(); 00095 if (!getQueueAttributes().empty()) 00096 evt["attributes"] = getQueueAttributes(); 00097 if (!getContent().empty()) 00098 evt["content"] = getContent(); 00099 return evt; 00100 } 00101 00102 void Queueable::queueAt(int position) 00103 { 00104 QSqlQuery query; 00105 query.prepare("INSERT INTO queue(position, queueable_type, queueable_id, attributes) " 00106 "VALUES(:position, :type, :id, :attributes)"); 00107 query.bindValue(":position", position); 00108 query.bindValue(":type", queueType()); 00109 query.bindValue(":id", queueId()); 00110 query.bindValue(":attributes", Network::JsonParser().serialize(attributes)); 00111 if (!query.exec()) 00112 throw_exception(0x01, tr("Can't insert queueable: %1").arg(query.lastError().text())); 00113 this->position = position; 00114 } 00115 00116 void Queueable::saveQueueAttributes() const 00117 { 00118 QSqlQuery query; 00119 00120 if (position < 0) 00121 return; 00122 query.prepare("UPDATE queue SET attributes = :attributes WHERE position = :position"); 00123 query.bindValue(":position", position); 00124 query.bindValue(":attributes", Network::JsonParser().serialize(attributes)); 00125 if (!query.exec()) 00126 throw_exception(0x02, tr("Can't update queueable: %1").arg(query.lastError().databaseText())); 00127 } 00128 00129 void Queueable::remove(int DBposition) 00130 { 00131 QSqlQuery rmquery; 00132 rmquery.prepare("DELETE FROM queue WHERE position = :position"); 00133 rmquery.bindValue(":position", DBposition); 00134 rmquery.exec(); 00135 if (!rmquery.exec()) 00136 throw_exception(0x03, tr("Can't remove queue element: %1").arg(rmquery.lastError().databaseText())); 00137 } 00138 00139 void Queueable::correctPosition(int DBposition) 00140 { 00141 QSqlQuery query; 00142 query.prepare("UPDATE queue SET position = :newPosition WHERE position = :position"); 00143 query.bindValue(":newPosition", position); 00144 query.bindValue(":position", DBposition); 00145 if (!query.exec()) 00146 throw_exception(0x04, tr("Can't update queueable: %1").arg(query.lastError().text())); 00147 } 00148 00149 QString Queueable::name() const 00150 { 00151 // This generic method should be overriden to give more details 00152 QString out = QString("Queueable<%1>").arg((qlonglong)this); 00153 if (hasContainer()) 00154 out += " in: " + container->name(); 00155 return out; 00156 } 00157 00158 QString Queueable::textDuration() const 00159 { 00160 if (queueIsFinite()) 00161 { 00162 int seconds = queueDuration() % 60; 00163 int minutes = (queueDuration() - seconds) / 60; 00164 return QString("%1:%2").arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0')); 00165 } 00166 else 00167 { 00168 return "--:--"; 00169 } 00170 } 00171 00172 bool Queueable::isTrack() const 00173 { 00174 return queueType() == "QueueTrack"; 00175 } 00176 00177 bool Queueable::isBreak() const 00178 { 00179 return queueType() == "QueueBreak"; 00180 } 00181 00182 QueueTrack* Queueable::toQueueTrack() 00183 { 00184 return static_cast<QueueTrack*>(this); 00185 } 00186 00187 QueueBreak* Queueable::toQueueBreak() 00188 { 00189 return static_cast<QueueBreak*>(this); 00190 } 00191 00192 void Queueable::remove() 00193 { 00194 emit remove(this); 00195 } 00196 00197 void Queueable::showControls() 00198 { 00199 /* 00200 if (controls_active) 00201 return; 00202 if (!controls) 00203 { 00204 controls = new ControlsWidget(this); 00205 } 00206 controls_active = true; 00207 */ 00208 } 00209 00210 void Queueable::hideControls() 00211 { 00212 /* 00213 if (!controls_active) 00214 return; 00215 controls->dissapear(); 00216 controls = 0; 00217 controls_active = false; 00218 */ 00219 } 00220 00221 void Queueable::setEvent(int id) 00222 { 00223 attributes["event"] = id; 00224 saveQueueAttributes(); 00225 } 00226 00227 int Queueable::getEvent() const 00228 { 00229 if (attributes.contains("event")) 00230 return attributes["event"].toInt(); 00231 else 00232 return -1; 00233 }