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 "library.hpp" 00012 #include "exception.hpp" 00013 #include "playqueue.hpp" 00014 #include "queuetrack.hpp" 00015 #include "sourcesparams.hpp" 00016 #include <QSqlQueryModel> 00017 #include <QFutureWatcher> 00018 00019 using namespace Library; 00020 00021 QStringList LibraryPlugin::allowedExtension; 00022 00023 LibraryPlugin::LibraryPlugin() 00024 { 00025 allowedExtension << "mp3" << "wma" << "ogg" << "oga" << "wav" << "flac" << "aac" << "m4a"; 00026 } 00027 00028 LibraryPlugin::~LibraryPlugin() 00029 { 00030 } 00031 00032 void LibraryPlugin::init() 00033 { 00034 setParamsName("library"); 00035 setParamsBackEnd(Params::Parameterizable::DATABASE, cores->db()); 00036 registerToParamsCore(cores->params()); 00037 00038 widget = new LibraryWidget(this); 00039 model = new LibraryModel(this); 00040 widget->getTreeViewWidget()->setModel(model); 00041 00042 Gui::Module *playerModule = Gui::ModuleFactory::create("Library", QPoint(0, 0), widget); 00043 playerModule->addParametersPage(new SourcesParams("Sources", "Library")); 00044 playerModule->submitForDisplay(); 00045 loadSources(); 00046 model->update(); 00047 } 00048 00049 void LibraryPlugin::aboutToQuit() 00050 { 00051 00052 } 00053 00054 LibraryWidget *LibraryPlugin::getWidget() const 00055 { 00056 return widget; 00057 } 00058 00059 LibraryModel *LibraryPlugin::getModel() const 00060 { 00061 return model; 00062 } 00063 00064 void LibraryPlugin::loadSources() 00065 { 00066 QSqlQuery query("SELECT id FROM sources"); 00067 query.exec(); 00068 while (query.next()) 00069 { 00070 sources << Source::getSource(query.value(0).toInt()); 00071 } 00072 } 00073 00074 void LibraryPlugin::addSource(const QUrl &url, bool recursive) 00075 { 00076 assertNotLocked("sources"); 00077 setLock("sources"); 00078 00079 QList<Source*> toMerge; 00080 bool useless = false; 00081 00082 if (!url.isValid()) 00083 throw_exception(0x01, tr("Invalid source : %1").arg(url.toString())); 00084 qDebug() << "Adding" << url.toString() << "to the sources"; 00085 foreach(Source *source, sources) 00086 { 00087 if (source->isRecursive() && source->toUrl().isParentOf(url)) 00088 { 00089 useless = true; 00090 qDebug() << " - is alreay included in" << source->toUrl().toString(); 00091 } 00092 else if (recursive && url.isParentOf(source->toUrl())) 00093 { 00094 toMerge << source; 00095 qDebug() << " - will replace" << source->toUrl().toString(); 00096 } 00097 else if (source->toUrl() == url) 00098 { 00099 if (source->isRecursive() == recursive || (source->isRecursive() && !recursive)) 00100 { 00101 useless = true; 00102 qDebug() << " - is alreay a source :" << source->toUrl().toString(); 00103 } 00104 else 00105 { 00106 toMerge << source; 00107 qDebug() << " - will replace" << source->toUrl().toString() << "[Non-recursive]"; 00108 } 00109 } 00110 } 00111 if (!useless) 00112 { 00113 Source *newSource = new Source(Source::typeFromScheme(url.scheme()), 00114 url.path(), recursive, url.host(), url.port(0), 00115 url.userName(), url.password()); 00116 foreach (Source *source, toMerge) 00117 { 00118 newSource->merge(source); 00119 } 00120 foreach (Source *source, toMerge) 00121 { 00122 sources.takeAt(sources.indexOf(source))->remove(); 00123 } 00124 sources << newSource; 00125 qDebug() << " - Done"; 00126 } 00127 unLock("sources"); 00128 } 00129 00130 void LibraryPlugin::updateSource(quintptr &sourceAddr) 00131 { 00132 Source *source; 00133 source = (Source*)sourceAddr; 00134 source->update(); 00135 } 00136 00137 void LibraryPlugin::updateSources() 00138 { 00139 assertNotLocked("sources"); 00140 setLock("sources"); 00141 00142 watcher = new QFutureWatcher<void>; 00143 connect(watcher, SIGNAL(started()), this, SLOT(updateStarted())); 00144 connect(watcher, SIGNAL(finished()), this, SLOT(updateFinished())); 00145 //connect(watcher, SIGNAL(progressValueChanged(int)), this, SLOT(updateProgress(int))); 00146 //connect(watcher, SIGNAL(progressRangeChanged(int,int)), this, SLOT(updateProgressRange(int,int))); 00147 sourcesAddr.clear(); 00148 foreach (Source *source, sources) 00149 { 00150 connect(source, SIGNAL(progressValueChange(int)), this, SLOT(updateProgress(int))); 00151 connect(source, SIGNAL(addToMaximumProgress(int)), this, SLOT(updateProgressMax(int))); 00152 sourcesAddr << (quintptr)((void*)source); 00153 } 00154 QFuture<void> sourceUpdate = QtConcurrent::map(sourcesAddr, LibraryPlugin::updateSource); 00155 watcher->setFuture(sourceUpdate); 00156 } 00157 00158 void LibraryPlugin::updateStarted() 00159 { 00160 widget->updatingSources(); 00161 } 00162 00163 void LibraryPlugin::updateFinished() 00164 { 00165 delete watcher; 00166 widget->endUpdatingSources(); 00167 getModel()->update(); 00168 unLock("sources"); 00169 } 00170 00171 void LibraryPlugin::updateProgress(int progressValue) 00172 { 00173 static int value = 0; 00174 value += progressValue; 00175 getWidget()->getUpdateProgressBar()->setValue(value); 00176 } 00177 00178 void LibraryPlugin::updateProgressMax(int maximum) 00179 { 00180 static int max = 0; 00181 00182 max += maximum; 00183 getWidget()->getUpdateProgressBar()->setRange(0, max); 00184 } 00185 00186 void LibraryPlugin::addTrackToQueue(int id) 00187 { 00188 Queue::PlayQueue *queue = dynamic_cast<Queue::PlayQueue*>(plugins->get("queue")); 00189 AudioTrack *track = AudioTrack::getTrack(id); 00190 if (queue && track) 00191 queue->add(new Queue::QueueTrack(*track)); 00192 } 00193 00194 void LibraryPlugin::defineParams() 00195 { 00196 }