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 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 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