def addSong(self, newSong): if isinstance(newSong, Song) and not newSong.getId() in self.songs: #check if the file still exist if path.isfile(newSong.getPath()) and path.exists(newSong.getPath()): query = Db.select("songs", ["id"], {"path": newSong.getPath()}) if query and query.next(): songInList = False for song in self.songs: if song.getPath() == newSong.getPath(): songInList = True if not songInList: if not newSong.dirty: self.songs.append(newSong) else: loadedSong = Song(id=query.value(0).toInt()[0]) if not loadedSong.dirty: self.songs.append(loadedSong) else: newSong.saveSong() if not newSong.dirty: self.songs.append(newSong) else: #else, delete its record Db.delete("songs", {"id": newSong.getId()})
def __getSongAlbum(self, artist_id): if not self.__album == None: query = Db.select("albums", ["id"], {"name": self.__album, "artist_id": artist_id}) if query and query.size() > 0 and query.next(): return query.value(0).toInt()[0] else: return Db.insert("albums", {"name": self.__album, "artist_id": artist_id}) else: query = Db.execute("SELECT a.id FROM albums as a, songs as s WHERE s.album_id = a.id AND s.id = %d" % (self.__id)) if query and query.next(): return query.value(0).toInt()[0]
def __getSongGenre(self): if not self.__genre == None: query = Db.select("genres", ["id"], {"name": self.__genre}) if query and query.size() > 0 and query.next(): return query.value(0).toInt()[0] else: return Db.insert("genres", {"name": self.__genre}) else: query = Db.execute("SELECT g.id FROM genres as g, songs as s WHERE s.genre_id = g.id AND s.id = %d" % (self.__id)) if query and query.next(): return query.value(0).toInt()[0]
def addPlaylistAction(self): playlistName, response = QInputDialog.getText(self, 'Pysawndz', 'Playlist name:') if response: sql = "SELECT * FROM playlists WHERE name = '%s'" % (playlistName) query = Db.execute(sql) if query.size() == 0: Db.insert("playlists", {"name": playlistName}) self.loadAllPlaylists() self.playlist.setCurrentRow(self.playlist.count() - 1) self.loadContextMenu() else: QMessageBox.warning(self, "Pysawndz", "Another playlist with the same name already exist.", QMessageBox.Ok);
def deleteSong(self): to_delete = self.musicTable.selectedItems() songs_to_delete = [] for i in to_delete: song_id = i.data(Qt.UserRole).toInt()[0] if not song_id == 0: songs_to_delete.append(song_id) if len(songs_to_delete) > 0: for song_id in songs_to_delete: Db.execute("DELETE FROM songs WHERE id = %d" % (song_id)) Db.execute("DELETE FROM playlist_songs WHERE song_id = %d" % (song_id)) self.sources.clear() self.clearMusicTable() self.loadAllSongs()
def editPlaylistAction(self): targetRow = self.playlist.currentRow() if targetRow == 0: return currentItem = self.playlist.currentItem() currentData = currentItem.data(Qt.UserRole).toInt()[0] playlistName, response = QInputDialog.getText(self, 'Pysawndz', 'New playlist name:') if response: Db.update("playlists", {"name": playlistName}, {"id": currentData}) self.loadAllPlaylists() self.playlist.setCurrentRow(targetRow) self.loadContextMenu()
def removePlaylistAction(self): if self.playlist.currentRow() == 0: return self.mediaObject.stop() self.mediaObject.clearQueue() currentItem = self.playlist.currentItem() currentData = currentItem.data(Qt.UserRole).toInt()[0] Db.delete("playlists", {"id": currentData}) Db.delete("playlist_songs", {"playlist_id": currentData}) self.loadAllPlaylists() self.loadContextMenu()
def addToPlaylistAction(self): action = self.sender() playlist_id = action.data().toInt()[0] to_add = self.musicTable.selectedItems() songs_to_add = [] for i in to_add: song_id = i.data(Qt.UserRole).toInt()[0] if not song_id == 0: songs_to_add.append(song_id) if len(songs_to_add) > 0: for song_id in songs_to_add: sql = "SELECT * FROM playlist_songs WHERE song_id = %d AND playlist_id = %d" % (song_id, playlist_id) query = Db.execute(sql) if query.size() == 0: Db.insert("playlist_songs", {"playlist_id": playlist_id, "song_id": song_id}) self.playlist.setCurrentRow(self.getRowByData(playlist_id))
def restorePlaylist(self): filename = QFileDialog.getOpenFileName(self, "Select playlist to restore", QDesktopServices.storageLocation(QDesktopServices.DesktopLocation), "*.songs") handle = open(filename, "rb") self.sources = pickle.load(handle) playlistName = self.phelper.getFileName(filename) sql = "SELECT * FROM playlists WHERE name = '%s'" % (playlistName) query = Db.execute(sql) if query.size() == 0: Db.insert("playlists", {"name": playlistName}) #TODO: I forgot :) self.loadAllPlaylists() self.playlist.setCurrentRow(self.playlist.count() - 1) self.loadContextMenu() else: QMessageBox.warning(self, "Pysawndz", "Another playlist with the same name already exist.", QMessageBox.Ok);
def clear(): config_file = "configures/config.cfg" configure = Configure(config_file) db_path = configure.get_attribute('main', 'db_path') db_name = configure.get_attribute('main', 'db_name') db = Db(db_path, db_name) db.clear_table("Pulses") db.clear_table("Indicators")
def updateSong(self): param = {} query = Db.select("songs", ["title", "duration", "artist_id", "genre_id", "album_id", "path"], {"id": self.__id}) if query and query.next(): if self.__title == None: self.__title = str(query.value(0).toString()) else: param["title"] = self.__title if self.__duration == None: self.__duration = query.value(1).toInt()[0] else: param["duration"] = self.__duration if self.__path == None: self.__path = str(query.value(5).toString()) else: param["path"] = self.__path #only update song's artist and album if they are both present if not self.__artist == None and not self.__album == None: param["artist_id"] = self.__getSongArtist() param["album_id"] = self.__getSongAlbum(param["artist_id"]) else: #else, just load defaults self.__artist = query.value(2).toInt()[0] self.__album = query.value(4).toInt()[0] if self.__genre == None: self.__genre = query.value(3).toInt()[0] else: param["genre_id"] = self.__getSongGenre() Db.update("songs", param, {"id": self.__id}) else: self.dirty = True
def saveSong(self): artist_id = self.__getSongArtist() param = { "title": self.__title, "duration": self.__duration, "artist_id": artist_id, "genre_id": self.__getSongGenre(), "album_id": self.__getSongAlbum(artist_id), "path": self.__path } self.__id = Db.insert("songs", param)
def loadAllPlaylists(self): self.playlist.clear() allsongs = QListWidgetItem("All Songs") allsongs.setIcon(QIcon(self.icons["all_songs"])) self.playlist.addItem(allsongs) query = Db.select(sqlTable="playlists", sqlFields=["id", "name"]) if query: while query.next(): playlistName = QListWidgetItem(query.value(1).toString()) playlistName.setIcon(QIcon(self.icons["playlist"])) playlistName.setData(Qt.UserRole, query.value(0).toInt()[0]) self.playlist.addItem(playlistName) self.playlist.setCurrentRow(0)
def loadAllSongs(self): sql = "SELECT s.id, s.title, s.path, s.duration, a.name as artist, al.name as album, g.name as genre FROM songs as s \ INNER JOIN artists as a ON a.id = s.artist_id \ INNER JOIN albums as al ON al.id = s.album_id \ INNER JOIN genres as g ON g.id = s.genre_id ORDER BY s.title ASC" query = Db.execute(sql) if query: while query.next(): song = Song(id=query.value(0).toInt()[0], title=query.value(1).toString(), path=query.value(2).toString(), duration=query.value(3).toInt()[0], artist=query.value(4).toString(), album=query.value(5).toString(), genre=query.value(6).toString()) self.sources.addSong(song) self.displayAllSongs()
def playListChanged(self, row): currentRow = self.playlist.item(row) if not row == 0 and not currentRow == None: currentPlaylist = currentRow.data(Qt.UserRole).toInt()[0] sql = "SELECT s.id, s.title, s.path, s.duration, a.name as artist, al.name as album, g.name as genre FROM songs as s \ INNER JOIN artists as a ON a.id = s.artist_id \ INNER JOIN albums as al ON al.id = s.album_id \ INNER JOIN genres as g ON g.id = s.genre_id \ INNER JOIN playlist_songs as ps ON ps.song_id = s.id \ WHERE ps.playlist_id = %d \ ORDER BY s.title ASC" % (currentPlaylist) query = Db.execute(sql) if query and query.size() > 0: self.sources.clear() while query.next(): #song_id = query.value(0).toInt()[0] song = Song(id=query.value(0).toInt()[0], title=query.value(1).toString(), path=query.value(2).toString(), duration=query.value(3).toInt()[0], artist=query.value(4).toString(), album=query.value(5).toString(), genre=query.value(6).toString()) self.sources.addSong(song) self.clearMusicTable() self.displayAllSongs() else: self.clearMusicTable() else: #all songs self.loadAllSongs() self.displayAllSongs()