Changeset 17
- Timestamp:
- 12/30/07 20:55:36 (4 years ago)
- Files:
-
- 4 added
- 11 edited
-
QtMPC.pro (modified) (2 diffs)
-
gui/main_window.cpp (modified) (8 diffs)
-
gui/main_window.h (modified) (3 diffs)
-
gui/musiclibraryitem.cpp (added)
-
gui/musiclibraryitem.h (added)
-
gui/musiclibrarymodel.cpp (added)
-
gui/musiclibrarymodel.h (added)
-
lib/mpdconnection.cpp (modified) (5 diffs)
-
lib/mpdconnection.h (modified) (6 diffs)
-
lib/mpdparseutils.cpp (modified) (4 diffs)
-
lib/mpdparseutils.h (modified) (1 diff)
-
lib/mpdstatus.cpp (modified) (1 diff)
-
lib/mpdstatus.h (modified) (1 diff)
-
lib/song.cpp (modified) (1 diff)
-
lib/song.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
QtMPC.pro
r4 r17 4 4 5 5 TEMPLATE = app 6 TARGET = 6 TARGET = 7 7 DEPENDPATH += . gui lib 8 8 INCLUDEPATH += . lib 9 9 CONFIG += qt debug 10 10 QT += network 11 #DEFINES += QT_NO_DEBUG_OUTPUT 11 12 12 13 # Input 13 14 HEADERS += gui/main_window.h \ 15 gui/musiclibraryitem.h \ 16 gui/musiclibrarymodel.h \ 14 17 gui/playlisttablemodel.h \ 15 18 lib/mpdconnection.h \ … … 22 25 SOURCES += main.cpp \ 23 26 gui/main_window.cpp \ 27 gui/musiclibraryitem.cpp \ 28 gui/musiclibrarymodel.cpp \ 24 29 gui/playlisttablemodel.cpp \ 25 30 lib/mpdconnection.cpp \ -
gui/main_window.cpp
r12 r17 7 7 setupUi(this); 8 8 9 mpd_status = NULL;9 //mpd_status = NULL; 10 10 slidingVolume = false; 11 11 … … 18 18 } 19 19 20 libraryTreeView->setModel(&musicLibraryModel); 21 20 22 playlistTableView->setModel(&playlistModel); 21 23 playlistTableView->verticalHeader()->hide(); … … 23 25 24 26 // MPD 25 connect(&mpd, SIGNAL(statusUpdated( MPDStatus *)), this, SLOT(updateStatus(MPDStatus *)));27 connect(&mpd, SIGNAL(statusUpdated(const MPDStatus &)), this, SLOT(updateStatus(const MPDStatus &))); 26 28 connect(&mpd, SIGNAL(playlistUpdated(QList<Song *> *)), &playlistModel, SLOT(updateSongs(QList<Song *> *))); 27 29 connect(&mpd, SIGNAL(currentSongUpdated(Song *)), this, SLOT(updateCurrentSong(Song *))); 30 connect(&mpd, SIGNAL(musicLibraryUpdated(QList<MusicLibraryItem *> *)), &musicLibraryModel, SLOT(updateLibrary(QList<MusicLibraryItem *> *))); 28 31 29 32 // GUI … … 42 45 43 46 mpd.getStatus(); 47 mpd.listAllInfo(); 44 48 45 49 statusTimer.start(1000); … … 73 77 void MainWindow::playPauseTrack() 74 78 { 75 if(mpd_status ->state == MPDStatus::State_Playing) {79 if(mpd_status.state == MPDStatus::State_Playing) { 76 80 mpd.setPause(true); 77 } else if(mpd_status ->state == MPDStatus::State_Paused) {81 } else if(mpd_status.state == MPDStatus::State_Paused) { 78 82 mpd.setPause(false); 79 83 } else { … … 103 107 } 104 108 105 void MainWindow::updateStatus( MPDStatus *status)109 void MainWindow::updateStatus(const MPDStatus &status) 106 110 { 107 111 QString timeElapsedFormattedString; 108 112 109 positionSlider->setMaximum(status ->time_total);110 positionSlider->setValue(status ->time_elapsed);113 positionSlider->setMaximum(status.time_total); 114 positionSlider->setValue(status.time_elapsed); 111 115 112 116 if(!slidingVolume) 113 volumeSlider->setValue(status->volume); 114 115 // Time elapsed 116 timeElapsedFormattedString += QString::number(floor(status->time_elapsed / 60.0)); 117 timeElapsedFormattedString += ":"; 118 if(status->time_elapsed % 60 < 10) { 119 timeElapsedFormattedString += "0"; 120 } 121 timeElapsedFormattedString += QString::number(status->time_elapsed % 60); 122 123 timeElapsedFormattedString += " / "; 124 125 // Time total 126 timeElapsedFormattedString += QString::number(floor(status->time_total / 60.0)); 127 timeElapsedFormattedString += ":"; 128 if(status->time_total % 60 < 10) { 129 timeElapsedFormattedString += "0"; 130 } 131 timeElapsedFormattedString += QString::number(status->time_total % 60); 117 volumeSlider->setValue(status.volume); 118 119 if(status.time_total == -1) { 120 timeElapsedFormattedString = "00:00 / 00:00"; 121 } else { 122 // Time elapsed 123 timeElapsedFormattedString += QString::number(floor(status.time_elapsed / 60.0)); 124 timeElapsedFormattedString += ":"; 125 if(status.time_elapsed % 60 < 10) { 126 timeElapsedFormattedString += "0"; 127 } 128 timeElapsedFormattedString += QString::number(status.time_elapsed % 60); 129 130 timeElapsedFormattedString += " / "; 131 132 // Time total 133 timeElapsedFormattedString += QString::number(floor(status.time_total / 60.0)); 134 timeElapsedFormattedString += ":"; 135 if(status.time_total % 60 < 10) { 136 timeElapsedFormattedString += "0"; 137 } 138 timeElapsedFormattedString += QString::number(status.time_total % 60); 139 } 132 140 133 141 songTimeElapsedLabel->setText(timeElapsedFormattedString); 134 142 135 switch(status ->state) {143 switch(status.state) { 136 144 case MPDStatus::State_Playing: 137 145 playPauseTrackButton->setText("Pause"); … … 140 148 stopTrackButton->setEnabled(true); 141 149 break; 150 case MPDStatus::State_Inactive: 142 151 case MPDStatus::State_Stopped: 143 152 playPauseTrackButton->setText("Play"); … … 157 166 158 167 // Check if song has changed and update if needed 159 if(mpd_status == NULL || mpd_status->song_id != status->song_id) {168 if(mpd_status.state == MPDStatus::State_Inactive || mpd_status.song_id != status.song_id) { 160 169 mpd.currentSong(); 161 170 } 162 171 163 172 // Check if playlist has changed and update if needed 164 if(mpd_status == NULL || mpd_status->playlist < status->playlist) {173 if(mpd_status.state == MPDStatus::State_Inactive || mpd_status.playlist < status.playlist) { 165 174 mpd.playListInfo(); 166 175 } 167 176 168 177 // Update status info 169 if(mpd_status != NULL) {170 delete mpd_status;171 }172 178 mpd_status = status; 173 179 } -
gui/main_window.h
r11 r17 6 6 7 7 #include "ui_main_window.h" 8 #include "gui/musiclibrarymodel.h" 8 9 #include "gui/playlisttablemodel.h" 9 10 #include "lib/mpdconnection.h" … … 20 21 private: 21 22 MPDConnection mpd; 22 MPDStatus *mpd_status;23 MPDStatus mpd_status; 23 24 QTimer statusTimer; 24 25 PlaylistTableModel playlistModel; 26 MusicLibraryModel musicLibraryModel; 25 27 bool slidingVolume; 26 28 … … 34 36 void setVolume(); 35 37 void updateCurrentSong(Song *song); 36 void updateStatus( MPDStatus *status);38 void updateStatus(const MPDStatus &status); 37 39 void playlistItemActivated(const QModelIndex &); 38 40 void removeFromPlaylist(); -
lib/mpdconnection.cpp
r11 r17 17 17 } 18 18 19 /*MPDConnection::~MPDConnection() 20 { 21 qDebug("Closing MPDConnection..."); 22 }*/ 19 MPDConnection::~MPDConnection() 20 { 21 sock.disconnectFromHost(); 22 quit(); 23 24 while(isRunning()) 25 msleep(100); 26 } 23 27 24 28 void MPDConnection::run() … … 69 73 70 74 if(hostname.isEmpty() || port == 0) { 71 q Debug("MPDConnection: no hostname and/or port supplied.");75 qWarning("MPDConnection: no hostname and/or port supplied."); 72 76 return false; 73 77 } … … 104 108 return true; 105 109 } else { 106 q Debug("Couldn't connect");110 qWarning("Couldn't connect"); 107 111 return false; 108 112 } … … 152 156 */ 153 157 158 void MPDConnection::listAllInfo() 159 { 160 QByteArray *data; 161 162 sendCommand("listallinfo"); 163 data = readFromSocket(); 164 165 emit musicLibraryUpdated(MPDParseUtils::parseLibraryItems(data)); 166 167 delete data; 168 } 169 170 void MPDConnection::lsInfo() 171 { 172 QByteArray *data; 173 174 sendCommand("lsinfo"); 175 data = readFromSocket(); 176 177 emit musicLibraryUpdated(MPDParseUtils::parseLibraryItems(data)); 178 179 delete data; 180 } 181 182 154 183 /* 155 184 * Playlist commands … … 369 398 { 370 399 QByteArray *data; 400 MPDStatus status; 371 401 372 402 sendCommand("status"); 373 403 data = readFromSocket(); 374 375 emit statusUpdated(MPDParseUtils::parseStatus(data)); 404 MPDParseUtils::parseStatus(data, status); 405 406 emit statusUpdated(status); 376 407 377 408 delete data; -
lib/mpdconnection.h
r11 r17 6 6 #include <QTcpSocket> 7 7 8 #include "gui/musiclibraryitem.h" 8 9 #include "mpdstatus.h" 9 10 #include "song.h" … … 16 17 MPDConnection(QObject *parent = 0); 17 18 MPDConnection(const QString &host, const quint16 port, QObject *parent = 0); 18 //~MPDConnection();19 ~MPDConnection(); 19 20 void run(); 20 21 bool connectToMPD(); … … 26 27 27 28 // Database 29 void listAllInfo(); 30 void lsInfo(); 28 31 // TODO 29 32 30 33 // Playlist 34 void currentSong(); 35 void playListInfo(); 31 36 void removeSongs(const QList<qint32> &items); 32 37 // TODO … … 56 61 57 62 public slots: 58 // Playlist59 void currentSong();60 void playListInfo();61 62 63 // Miscellaneous 63 64 void getStatus(); 64 65 65 pr otected:66 private: 66 67 QString hostname; 67 68 quint16 port; … … 69 70 QTcpSocket sock; 70 71 bool connected; 72 QMutex mutex; 71 73 72 74 bool commandOk(); 73 75 void sendCommand(const QByteArray &command); 74 75 private:76 QMutex mutex;77 76 78 77 private slots: … … 80 79 81 80 signals: 81 void musicLibraryUpdated(QList<MusicLibraryItem *> *items); 82 82 void currentSongUpdated(Song *song); 83 83 void playlistUpdated(QList<Song *> *songs); 84 void statusUpdated( MPDStatus *status);84 void statusUpdated(const MPDStatus &status); 85 85 }; 86 86 -
lib/mpdparseutils.cpp
r7 r17 4 4 5 5 6 MPDStatus * MPDParseUtils::parseStatus(const QByteArray * const data)6 void MPDParseUtils::parseStatus(const QByteArray * const data, MPDStatus &destStatus) 7 7 { 8 MPDStatus *status = new MPDStatus;9 10 8 QList<QByteArray> lines = data->split('\n'); 11 9 QList<QByteArray> tokens; … … 15 13 16 14 if(tokens.at(0) == "volume") { 17 status->volume = tokens.at(1).toUInt();15 destStatus.volume = tokens.at(1).toUInt(); 18 16 } else if(tokens.at(0) == "repeat") { 19 17 if(tokens.at(1) == "1") { 20 status->repeat = true;18 destStatus.repeat = true; 21 19 } else { 22 status->repeat = false;20 destStatus.repeat = false; 23 21 } 24 22 } else if(tokens.at(0) == "random") { 25 23 if(tokens.at(1) == "1") { 26 status->random = "1";24 destStatus.random = "1"; 27 25 } else { 28 status->random = "0";26 destStatus.random = "0"; 29 27 } 30 28 } else if(tokens.at(0) == "playlist") { 31 status->playlist = tokens.at(1).toUInt();29 destStatus.playlist = tokens.at(1).toUInt(); 32 30 } else if(tokens.at(0) == "playlistlength") { 33 status->playlistlength = tokens.at(1).toInt();31 destStatus.playlistlength = tokens.at(1).toInt(); 34 32 } else if(tokens.at(0) == "playlistqueue") { 35 status->playlistqueue = tokens.at(1).toInt();33 destStatus.playlistqueue = tokens.at(1).toInt(); 36 34 } else if(tokens.at(0) == "xfade") { 37 status->xfade = tokens.at(1).toInt();35 destStatus.xfade = tokens.at(1).toInt(); 38 36 } else if(tokens.at(0) == "state") { 39 37 if(tokens.at(1).contains("play")) { 40 status->state = MPDStatus::State_Playing;38 destStatus.state = MPDStatus::State_Playing; 41 39 } else if(tokens.at(1).contains("stop")) { 42 status->state = MPDStatus::State_Stopped;40 destStatus.state = MPDStatus::State_Stopped; 43 41 } else { 44 status->state = MPDStatus::State_Paused;42 destStatus.state = MPDStatus::State_Paused; 45 43 } 46 44 } else if(tokens.at(0) == "song") { 47 status->song = tokens.at(1).toInt();45 destStatus.song = tokens.at(1).toInt(); 48 46 } else if(tokens.at(0) == "songid") { 49 status->song_id = tokens.at(1).toInt();47 destStatus.song_id = tokens.at(1).toInt(); 50 48 } else if(tokens.at(0) == "time") { 51 status->time_elapsed = tokens.at(1).toInt();52 status->time_total = tokens.at(2).toInt();49 destStatus.time_elapsed = tokens.at(1).toInt(); 50 destStatus.time_total = tokens.at(2).toInt(); 53 51 } else if(tokens.at(0) == "bitrate") { 54 status->bitrate = tokens.at(1).toUInt();52 destStatus.bitrate = tokens.at(1).toUInt(); 55 53 } else if(tokens.at(0) == "audio") { 56 54 } else if(tokens.at(0) == "updating_db") { 57 status->updating_db = tokens.at(1).toInt();55 destStatus.updating_db = tokens.at(1).toInt(); 58 56 } else if(tokens.at(0) == "error") { 59 status->error = tokens.at(1);57 destStatus.error = tokens.at(1); 60 58 } 61 59 } 62 63 return status;64 60 } 65 61 … … 77 73 song->file = tokens.at(1); 78 74 } else if(tokens.at(0) == "Time") { 75 song->time = tokens.at(1).toUInt(); 79 76 } else if(tokens.at(0) == "Album") { 80 song->album = tokens.at(1); 77 for(qint32 j = 1; j < tokens.size(); j++) { 78 if(j != 1 && j != tokens.size() - 1) 79 song->album += ":"; 80 song->album += tokens.at(j); 81 } 81 82 } else if(tokens.at(0) == "Artist") { 82 song->artist = tokens.at(1); 83 for(qint32 j = 1; j < tokens.size(); j++) { 84 if(j != 1 && j != tokens.size() - 1) 85 song->artist += ":"; 86 song->artist += tokens.at(j); 87 } 83 88 } else if(tokens.at(0) == "Title") { 84 song->title = tokens.at(1); 89 for(qint32 j = 1; j < tokens.size(); j++) { 90 if(j != 1 && j != tokens.size() - 1) 91 song->title += ":"; 92 song->title += tokens.at(j); 93 } 85 94 } else if(tokens.at(0) == "Track") { 95 song->track = tokens.at(1).toInt(); 86 96 } else if(tokens.at(0) == "Pos") { 87 97 } else if(tokens.at(0) == "Id") { … … 111 121 return songs; 112 122 } 123 124 QList<MusicLibraryItem *> * MPDParseUtils::parseLibraryItems(const QByteArray * const data) 125 { 126 QList<MusicLibraryItem *> *artists = new QList<MusicLibraryItem *>; 127 QByteArray currentItem; 128 MusicLibraryItem *artistItem, *albumItem, *songItem; 129 Song *currentSong; 130 bool found = false; 131 132 QList<QByteArray> lines = data->split('\n'); 133 134 for(qint32 i = 0; i < lines.size(); i++) { 135 currentItem += lines.at(i); 136 currentItem += "\n"; 137 if(i == lines.size() - 1 || lines.at(i + 1).startsWith("file:")) { 138 currentSong = parseSong(¤tItem); 139 currentItem.clear(); 140 141 if(currentSong->isEmpty()) { 142 delete currentSong; 143 continue; 144 } 145 146 currentSong->fillEmptyFields(); 147 148 // Check if artist already exists 149 for(qint32 i = 0; i < artists->size(); i++) { 150 if(artists->at(i)->data(0) == currentSong->artist) { 151 artistItem = artists->at(i); 152 found = true; 153 } 154 } 155 156 if(!found) { 157 artistItem = new MusicLibraryItem(currentSong->artist, MusicLibraryItem::Type_Artist); 158 artists->append(artistItem); 159 } 160 161 found = false; 162 163 // Check if album already exists 164 for(qint32 i = 0; i < artistItem->childCount(); i++) { 165 if(artistItem->child(i)->data(0) == currentSong->album) { 166 albumItem = artistItem->child(i); 167 found = true; 168 } 169 } 170 171 if(!found) { 172 albumItem = new MusicLibraryItem(currentSong->album, MusicLibraryItem::Type_Album, artistItem); 173 artistItem->appendChild(albumItem); 174 } 175 176 found = false; 177 178 // Add song to album (possibly in track order) 179 songItem = new MusicLibraryItem(currentSong->title, MusicLibraryItem::Type_Song, albumItem); 180 songItem->setFile(currentSong->file); 181 albumItem->appendChild(songItem); 182 183 delete currentSong; 184 } 185 } 186 187 return artists; 188 } -
lib/mpdparseutils.h
r4 r17 4 4 #include "mpdstatus.h" 5 5 #include "song.h" 6 #include "gui/musiclibraryitem.h" 6 7 7 8 class MPDParseUtils 8 9 { 9 10 public: 10 static MPDStatus * parseStatus(const QByteArray * const data);11 static void parseStatus(const QByteArray * const data, MPDStatus &destStatus); 11 12 static Song * parseSong(const QByteArray * const data); 12 13 static QList<Song *> * parseSongs(const QByteArray * const data); 14 static QList<MusicLibraryItem *> * parseLibraryItems(const QByteArray * const data); 13 15 }; 14 16 -
lib/mpdstatus.cpp
r3 r17 10 10 playlistqueue = -1; 11 11 xfade = 0; 12 state = MPDStatus::State_ Stopped;12 state = MPDStatus::State_Inactive; 13 13 song = -1; 14 14 song_id = -1; -
lib/mpdstatus.h
r3 r17 10 10 public: 11 11 enum State { 12 State_Inactive, 12 13 State_Playing, 13 14 State_Stopped, -
lib/song.cpp
r15 r17 5 5 id = -1; 6 6 time = 0; 7 track = 0;7 track = -1; 8 8 pos = 0; 9 9 } 10 11 bool Song::isEmpty() const 12 { 13 return (artist.isEmpty() && album.isEmpty() && title.isEmpty()); 14 } 15 16 void Song::fillEmptyFields() 17 { 18 if(artist.isEmpty()) 19 artist = "<Unknown>"; 20 21 if(album.isEmpty()) 22 album = "<Unknown>"; 23 24 if(title.isEmpty()) 25 title = "<Unknown>"; 26 } -
lib/song.h
r15 r17 14 14 QString artist; 15 15 QString title; 16 q uint32 track;16 qint32 track; 17 17 quint32 pos; 18 18 19 19 Song(); 20 bool isEmpty() const; 21 void fillEmptyFields(); 20 22 }; 21 23
Note: See TracChangeset
for help on using the changeset viewer.
