defuze.me  Client
playlists.cpp
00001 #include "playlists.hpp"
00002 
00003 #include <QSqlQuery>
00004 #include <QListWidget>
00005 
00006 using namespace Playlists;
00007 
00008 PlaylistsPlugin::PlaylistsPlugin()
00009 {
00010 }
00011 
00012 PlaylistsPlugin::~PlaylistsPlugin()
00013 {
00014 }
00015 
00016 void    PlaylistsPlugin::init()
00017 {
00018     widget = new PlaylistsWidget(this);
00019     model = new PlaylistsModel(this);
00020     playlistsList = new QList<Playlist*>();
00021         widget->getTreeView()->setModel(model);
00022     loadPlaylists();
00023         this->model->update();
00024         Gui::Module *playlistsModule = Gui::ModuleFactory::create("Playlists", QPoint(0, 0), widget, 0);
00025     playlistsModule->submitForDisplay();
00026 }
00027 
00028 void    PlaylistsPlugin::aboutToQuit()
00029 {
00030 }
00031 
00032 PlaylistsModel  *PlaylistsPlugin::getModel() const
00033 {
00034     return model;
00035 }
00036 
00037 PlaylistsWidget *PlaylistsPlugin::getWidget() const
00038 {
00039     return widget;
00040 }
00041 
00042 QList<Playlist*> *PlaylistsPlugin::getPlaylistList() const
00043 {
00044     return this->playlistsList;
00045 }
00046 
00047 void    PlaylistsPlugin::loadPlaylists()
00048 {
00049 
00050     this->playlistsList->clear();
00051 
00052         QSqlQuery query("SELECT id, name FROM playlists ORDER BY name");
00053 
00054     if (!query.exec())
00055             throw_exception(0x01, tr("Can't load playlists: %1").arg(query.lastError().text()));
00056         while (query.next())
00057         {
00058             playlistsList->push_back(new Playlist(query.value(0).toInt()));
00059             playlistsList->back()->setName(query.value(1).toString());
00060         }
00061 }
00062 
00063 void            PlaylistsPlugin::addPlaylist(QString name)
00064 {
00065     QSqlQuery query;
00066         if (name.isNull() || name.isEmpty())
00067             return;
00068         foreach (Playlist *playlist, *playlistsList)
00069         {
00070             if (playlist->getName().trimmed().compare(name.trimmed()) == 0)
00071                 return;
00072             //TODO: Affichage d'un message d'erreur;
00073         }
00074 
00075         query.prepare("INSERT INTO playlists(name, is_dynamic, definition) VALUES (:name, :is_dynamic, :definition)");
00076         query.bindValue(":name", name.trimmed());
00077         query.bindValue(":is_dynamic", false);
00078     query.bindValue(":definition", "");
00079     if (!query.exec())
00080             throw_exception(0x01, tr("Can't create playlist: %1").arg(query.lastError().text()));
00081         widget->getLineEditAdd()->setText("");
00082         loadPlaylists();
00083         foreach(Playlist *playlist, *playlistsList)
00084         {
00085             if (playlist->getName().compare(name.trimmed()) == 0)
00086                 this->model->insertPlaylist(playlist->getId());
00087         }
00088 }
00089 
00090 void            PlaylistsPlugin::deletePlaylist(int idPlaylist)
00091 {
00092     QSqlQuery query;
00093     query.prepare("DELETE FROM playlists WHERE (id = :id)");
00094     query.bindValue(":id", idPlaylist);
00095     if (!query.exec())
00096                     throw_exception(0x01, tr("Can't delete playlist: %1").arg(query.lastError().text()));
00097     loadPlaylists();
00098     this->model->deletePlaylist(idPlaylist);
00099 }
00100 
00101 Playlist  *PlaylistsPlugin::getPlaylistById(int playlistId)
00102 {
00103     foreach (Playlist *playlist, *playlistsList)
00104     {
00105         if (playlist->getId() == playlistId)
00106             return playlist;
00107     }
00108     return NULL;
00109 }