From: Nikolay Tischenko Date: Sat, 11 Sep 2010 20:34:29 +0000 (+0700) Subject: * Minor fixes in database storage X-Git-Tag: release-1.0~14 X-Git-Url: http://git.maemo.org/git/?p=someplayer;a=commitdiff_plain;h=a33363a44394be6efff8593737eaa53efd9b6c01 * Minor fixes in database storage * Minor fixes in file storage * Imlementing library page (implemented walking throw library, work with playlists) --- diff --git a/someplayer.pro.user b/someplayer.pro.user index 94896bd..fdaaadf 100644 --- a/someplayer.pro.user +++ b/someplayer.pro.user @@ -202,7 +202,7 @@ 1 - 2010-09-10T01:22:53 + 2010-09-12T03:30:33 1 diff --git a/src/dbstorage.cpp b/src/dbstorage.cpp index 03db3fd..429e00c 100644 --- a/src/dbstorage.cpp +++ b/src/dbstorage.cpp @@ -27,7 +27,7 @@ void DbStorage::_prepare_queries() { _get_tracks_for_album_query = new QSqlQuery(db); _get_tracks_for_album_query->prepare("SELECT id, title, source, count, length FROM tracks WHERE artist_id IN " "(SELECT id FROM artist WHERE name = :artist_name) AND album_id IN " - "(SELECT id FROM album WHERE name =: album_name);"); + "(SELECT id FROM album WHERE name = :album_name);"); _get_favorites_query = new QSqlQuery(db); _get_favorites_query->prepare("SELECT track_id as id, title, artist, album.name as album, source, count, length FROM " diff --git a/src/filestorage.cpp b/src/filestorage.cpp index f383a1c..f32f47d 100644 --- a/src/filestorage.cpp +++ b/src/filestorage.cpp @@ -20,11 +20,12 @@ QList FileStorage::getPlaylists() { } Playlist FileStorage::getPlaylist(QString name) { - QFile playlistFile (_path_prefix+"/"+name+_PLAYLIST_FILE_EXTENSION_); + QFile playlistFile (_path_prefix+"/"+name+"."+_PLAYLIST_FILE_EXTENSION_); Playlist playlist; playlist.setName("Bad playlist"); if (playlistFile.exists()) { playlist.setName(name); + playlistFile.open(QFile::ReadOnly); QTextStream stream(&playlistFile); QString buffer = stream.readLine(); int index = 0; @@ -32,13 +33,13 @@ Playlist FileStorage::getPlaylist(QString name) { while (!stream.atEnd()) { buffer = stream.readLine(); if (_meta_regexp.indexIn(buffer) != -1) { - int seconds = _meta_regexp.cap(0).toInt(); - QString artist = _meta_regexp.cap(1); - QString album = _meta_regexp.cap(2); - QString title = _meta_regexp.cap(3); + int seconds = _meta_regexp.cap(1).toInt(); + QString artist = _meta_regexp.cap(2); + QString album = _meta_regexp.cap(3); + QString title = _meta_regexp.cap(4); buffer = stream.readLine(); if (_path_regexp.indexIn(buffer) != -1) { - QString source = _path_regexp.cap(0); + QString source = _path_regexp.cap(1); TrackMetadata meta(title, artist, album, seconds); Track track(index++, meta, source); playlist.addTrack(track); @@ -59,7 +60,8 @@ QStringList FileStorage::getPlaylistsNames() { QFileInfo info(entry); QString suffix = info.suffix().toLower(); if (suffix == _PLAYLIST_FILE_EXTENSION_) { - playlistNames.append(info.fileName()); + playlistNames.append(info.fileName() + .replace(QString(".%1").arg(_PLAYLIST_FILE_EXTENSION_), "", Qt::CaseInsensitive)); } } return playlistNames; diff --git a/src/library.h b/src/library.h index 515f3d8..c9e460a 100644 --- a/src/library.h +++ b/src/library.h @@ -9,6 +9,9 @@ #include "mediascanner.h" #include "tagresolver.h" +#define _DATABASE_PATH_ "/tmp" +#define _PLAYLISTS_PATH_ "/tmp" + // represents media library: tracks, playlists // it uses different media storages for tracks and playlists // but dynamic playlits will be stored with tracks into the same storage diff --git a/src/libraryform.cpp b/src/libraryform.cpp index b41545d..cc9e1a0 100644 --- a/src/libraryform.cpp +++ b/src/libraryform.cpp @@ -1,14 +1,202 @@ #include "libraryform.h" #include "ui_libraryform.h" +#include "library.h" +#include +#include +#include +#include +#include "track.h" +#include "playlist.h" +#include +#include -LibraryForm::LibraryForm(QWidget *parent) : +using namespace SomePlayer::DataObjects; + +inline QString __format_track_string(TrackMetadata meta) { + int minutes = meta.length() / 60; + int seconds = meta.length() % 60; + QTime time(0, minutes, seconds); + return QString("[%1] %2").arg(time.toString("mm:ss")).arg(meta.title()); + +} + +inline void __fill_model(QStandardItemModel *model, QList data) { + model->clear(); + int count = data.count(); + model->setRowCount(count); + for (int i = 0; i < count; i++) { + model->setItem(i, 0, new QStandardItem(data.at(i))); + } +} + +inline void __fill_model_tracks (QStandardItemModel *model, QList tracks) { + int count = tracks.count(); + model->setRowCount(count); + for (int i = 0; i < count; i++) { + TrackMetadata meta = tracks.at(i).metadata(); + model->setItem(i, 0, new QStandardItem(__format_track_string(meta))); + } +} + +LibraryForm::LibraryForm(Library *lib, QWidget *parent) : QWidget(parent), ui(new Ui::LibraryForm) { + _lib = lib; + _model = new QStandardItemModel(this); + _state = STATE_NONE; ui->setupUi(this); + connect(ui->playerButton, SIGNAL(clicked()), this, SLOT(_player())); + connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_view_button())); + connect(ui->playlistsButton, SIGNAL(clicked()), this, SLOT(_playlists_button())); + connect(ui->listView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_list_click(QModelIndex))); + connect(ui->addButton, SIGNAL(clicked()), this, SLOT(_add_button())); + connect(ui->backButton, SIGNAL(clicked()), this, SLOT(_back_button())); + _view_button(); } LibraryForm::~LibraryForm() { delete ui; } + +void LibraryForm::_player() { + emit player(); +} + +void LibraryForm::_view_button() { + QList artitst = _lib->getArtists(); + __fill_model(_model, artitst); + ui->listView->setModel(_model); + _state = STATE_ARTIST; + ui->backButton->setEnabled(false); + ui->listLabel->setText("Artists"); +} + +void LibraryForm::_dynamic_button() { +} + +void LibraryForm::_process_list_click(QModelIndex index) { + if (_state == STATE_NONE) return; + QString data = index.data().toString(); + switch (_state) { + case STATE_ARTIST: + __fill_model(_model, _lib->getAlbumsForArtist(data)); + _current_artist = data; + _state = STATE_ALBUM; + ui->backButton->setEnabled(true); + ui->listLabel->setText(QString("Albums by \"%1\"").arg(_current_artist)); + break; + case STATE_ALBUM: + _current_album = data; + _current_tracks = _lib->getTracksForAlbum(data, _current_artist); + __fill_model_tracks(_model, _current_tracks); + _state = STATE_TRACK; + ui->backButton->setEnabled(true); + ui->listLabel->setText(QString("Tracks from \"%1\" by \"%2\"").arg(_current_album).arg(_current_artist)); + break; + case STATE_PLAYLIST: + { + Playlist playlist = _lib->getPlaylist(data); + _current_tracks = playlist.tracks(); + __fill_model_tracks(_model, _current_tracks); + _state = STATE_PLAYLIST_TRACK; + ui->backButton->setEnabled(true); + ui->listLabel->setText(QString("Tracks in playlist \"%1\"").arg(data)); + } + break; + default: + return; + } +} + +void LibraryForm::_add_button() { + if (_state == STATE_NONE) return; + QModelIndexList selected = ui->listView->selectionModel()->selectedIndexes(); + ui->listView->selectionModel()->clearSelection(); + switch (_state) { + case STATE_ARTIST: + foreach (QModelIndex id, selected) { + _add_artist(id.data().toString()); + } + break; + case STATE_ALBUM: + foreach (QModelIndex id, selected) { + _add_album(_current_artist, id.data().toString()); + } + break; + case STATE_TRACK: + foreach (QModelIndex id, selected) { + _add_track(_current_tracks.at(id.row())); + } + break; + case STATE_PLAYLIST: + foreach (QModelIndex id, selected) { + _add_playlist(id.data().toString()); + } + break; + case STATE_PLAYLIST_TRACK: + foreach (QModelIndex id, selected) { + _add_track(_current_tracks.at(id.row())); + } + break; + default: + return; + } +} + + +void LibraryForm::_add_artist(QString artist) { + qDebug() << "adding ARTIST " << artist; + QList albums = _lib->getAlbumsForArtist(artist); + foreach(QString album, albums) { + _add_album(artist, album); + } +} + +void LibraryForm::_add_album(QString artist, QString album) { + qDebug() << "adding ALBUM " << album << " by " << artist; + QList tracks = _lib->getTracksForAlbum(album, artist); + foreach(Track track, tracks) { + _add_track(track); + } +} + +void LibraryForm::_add_track(Track track) { + qDebug() << "adding TRACK " << track.metadata().title() << " from " << track.metadata().album() << " by " << track.metadata().artist(); +} + +void LibraryForm::_add_playlist(QString name) { + qDebug() << "adding playlist \"" << name << "\""; + Playlist playlist = _lib->getPlaylist(name); + QList tracks = playlist.tracks(); + foreach (Track track, tracks) { + _add_track(track); + } +} + +void LibraryForm::_back_button() { + switch (_state) { + case STATE_ALBUM: + _view_button(); + break; + case STATE_TRACK: + __fill_model(_model, _lib->getAlbumsForArtist(_current_artist)); + _state = STATE_ALBUM; + ui->listLabel->setText(QString("Albums by \"%1\"").arg(_current_artist)); + break; + case STATE_PLAYLIST_TRACK: + _playlists_button(); + default: + return; + } +} + +void LibraryForm::_playlists_button() { + QList playlists = _lib->getPlaylistsNames(); + __fill_model(_model, playlists); + ui->listView->setModel(_model); + _state = STATE_PLAYLIST; + ui->backButton->setEnabled(false); + ui->listLabel->setText("Playlists"); +} diff --git a/src/libraryform.h b/src/libraryform.h index a4aefc5..009b692 100644 --- a/src/libraryform.h +++ b/src/libraryform.h @@ -2,21 +2,52 @@ #define LIBRARYFORM_H #include +#include "someplayer.h" + +#include +#include +#include namespace Ui { class LibraryForm; } +using SomePlayer::DataObjects::Library; +using SomePlayer::DataObjects::Track; + +enum LibraryFormListState {STATE_NONE, STATE_ARTIST, STATE_ALBUM, STATE_TRACK, STATE_PLAYLIST, STATE_PLAYLIST_TRACK}; + class LibraryForm : public QWidget { Q_OBJECT public: - explicit LibraryForm(QWidget *parent = 0); + explicit LibraryForm(Library *lib, QWidget *parent = 0); ~LibraryForm(); +signals: + void player(); +private slots: + void _player(); + void _view_button(); + void _dynamic_button(); + void _playlists_button(); + void _add_button(); + void _back_button(); + void _process_list_click(QModelIndex); private: Ui::LibraryForm *ui; + Library *_lib; + QStandardItemModel *_model; + LibraryFormListState _state; + QString _current_artist; + QString _current_album; + QList _current_tracks; + + void _add_artist(QString artist); + void _add_album(QString artist, QString album); + void _add_track(Track track); + void _add_playlist(QString name); }; #endif // LIBRARYFORM_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 69ff230..f14edf9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -8,10 +8,13 @@ #include "library.h" +using namespace SomePlayer::DataObjects; + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { + _library = new Library(_DATABASE_PATH_, _PLAYLISTS_PATH_); ui->setupUi(this); connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openMedia())); connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt())); @@ -19,13 +22,12 @@ MainWindow::MainWindow(QWidget *parent) : connect(ui->actionPlayer, SIGNAL(triggered()), this, SLOT(player())); connect(ui->actionLibrary, SIGNAL(triggered()), this, SLOT(library())); setAnimated(true); - _playerForm = new PlayerForm(); - _libraryForm = new LibraryForm(); + _playerForm = new PlayerForm(_library, ui->stackedWidget); + _libraryForm = new LibraryForm(_library, ui->stackedWidget); ui->stackedWidget->insertWidget(0, _playerForm); ui->stackedWidget->insertWidget(1, _libraryForm); - _playerForm->setAttribute(Qt::WA_Maemo5StackedWindow); - _libraryForm->setAttribute(Qt::WA_Maemo5StackedWindow); connect(_playerForm, SIGNAL(library()), this, SLOT(library())); + connect(_libraryForm, SIGNAL(player()), this, SLOT(player())); library(); } @@ -53,11 +55,13 @@ void MainWindow::about() { } void MainWindow::player() { + _playerForm->show(); ui->stackedWidget->setCurrentIndex(0); setWindowTitle("SomePlayer"); } void MainWindow::library() { + _libraryForm->show(); ui->stackedWidget->setCurrentIndex(1); setWindowTitle("SomePlayer Library"); } diff --git a/src/mainwindow.h b/src/mainwindow.h index 5acedb4..033630d 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -5,11 +5,14 @@ #include #include "playerform.h" #include "libraryform.h" +#include "library.h" namespace Ui { class MainWindow; } +using SomePlayer::DataObjects::Library; + class MainWindow : public QMainWindow { Q_OBJECT @@ -30,6 +33,7 @@ public slots: private: PlayerForm *_playerForm; LibraryForm *_libraryForm; + Library *_library; }; #endif // MAINWINDOW_H diff --git a/src/playerform.cpp b/src/playerform.cpp index 3040bb1..d6e5929 100644 --- a/src/playerform.cpp +++ b/src/playerform.cpp @@ -1,12 +1,15 @@ #include "playerform.h" #include "ui_playerform.h" -PlayerForm::PlayerForm(QWidget *parent) : +using namespace SomePlayer::DataObjects; + +PlayerForm::PlayerForm(Library* lib, QWidget *parent) : + _lib(lib), QWidget(parent), ui(new Ui::PlayerForm) { ui->setupUi(this); - connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(_library())); + connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library())); } PlayerForm::~PlayerForm() diff --git a/src/playerform.h b/src/playerform.h index 6c06393..ea57a27 100644 --- a/src/playerform.h +++ b/src/playerform.h @@ -2,17 +2,20 @@ #define PLAYERFORM_H #include +#include "someplayer.h" namespace Ui { class PlayerForm; } +using SomePlayer::DataObjects::Library; + class PlayerForm : public QWidget { Q_OBJECT public: - explicit PlayerForm(QWidget *parent = 0); + explicit PlayerForm(Library *lib, QWidget *parent = 0); ~PlayerForm(); signals: void library(); @@ -22,6 +25,7 @@ private slots: private: Ui::PlayerForm *ui; + Library *_lib; }; #endif // PLAYERFORM_H diff --git a/src/playlist.cpp b/src/playlist.cpp index a934bb1..d036714 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -7,6 +7,17 @@ Playlist::Playlist() _name = "New playlist"; } +Playlist::Playlist(const Playlist &playlist) { + _name = playlist.name(); + _tracks = playlist.tracks(); +} + +Playlist& Playlist::operator =(const Playlist &playlist) { + _name = playlist.name(); + _tracks = playlist.tracks(); + return *this; +} + QString Playlist::name() const { return _name; } diff --git a/src/playlist.h b/src/playlist.h index a937a3a..5c3ecde 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -11,6 +11,9 @@ namespace SomePlayer { class Playlist { public: Playlist(); + Playlist(const Playlist &playlist); + + Playlist &operator=(const Playlist &playlist); QString name() const; const QList &tracks() const; diff --git a/src/someplayer.h b/src/someplayer.h index b02f444..8672705 100644 --- a/src/someplayer.h +++ b/src/someplayer.h @@ -9,6 +9,7 @@ namespace SomePlayer { class TrackMetadata; class TagResolver; class Playlist; + class Library; }; namespace Storage { }; @@ -18,6 +19,7 @@ namespace SomePlayer { // common includes +#include #include #include #include diff --git a/src/ui/libraryform.ui b/src/ui/libraryform.ui index 79017b2..cdc3ddd 100644 --- a/src/ui/libraryform.ui +++ b/src/ui/libraryform.ui @@ -10,16 +10,209 @@ 480 + + + + + + + 0 + 85 + 255 + + + + + + + + + 0 + 85 + 255 + + + + + + + + + 127 + 127 + 126 + + + + + + Form - - - - - ОЛОЛО!! - - + + + + + + + View + + + true + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Dynamic + + + true + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Playlists + + + true + + + + + + + + + + + + + Back + + + true + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Add + + + true + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Player + + + true + + + + + + + + + + + false + + + + + + Qt::AutoText + + + Qt::AlignCenter + + + + + + + QAbstractItemView::NoEditTriggers + + + QAbstractItemView::MultiSelection + + + false + + + 1 + + + false + + + + + + diff --git a/src/ui/playerform.ui b/src/ui/playerform.ui index 6311270..8a8acba 100644 --- a/src/ui/playerform.ui +++ b/src/ui/playerform.ui @@ -15,9 +15,9 @@ - + - ПЫЩ!! + Library