diff --git a/tidal_dl_ng/gui.py b/tidal_dl_ng/gui.py index 074bd19..df5c3d7 100644 --- a/tidal_dl_ng/gui.py +++ b/tidal_dl_ng/gui.py @@ -581,13 +581,35 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): if not index.isValid() or not index.parent().data(): return + # Get the media item to determine type + item = self.tr_lists_user.itemAt(point) + media = get_user_list_media_item(item) + # We build the menu. menu = QtWidgets.QMenu() - menu.addAction("Download Playlist", lambda: self.thread_download_list_media(point)) - menu.addAction( - "Download All Albums in Playlist", lambda: self.thread_it(self.on_download_all_albums_from_playlist, point) - ) - menu.addAction("Copy Share URL", lambda: self.on_copy_url_share(self.tr_lists_user, point)) + + if isinstance(media, Folder): + # Folder-specific menu items + menu.addAction( + "Download All Playlists in Folder", lambda: self.thread_it(self.on_download_folder_playlists, point) + ) + menu.addAction( + "Download All Albums from Folder", lambda: self.thread_it(self.on_download_folder_albums, point) + ) + elif isinstance(media, str): + # Favorites items (stored as string keys like "fav_tracks", "fav_albums") + menu.addAction("Download All Items", lambda: self.thread_it(self.on_download_favorites, point)) + menu.addAction( + "Download All Albums from Items", lambda: self.thread_it(self.on_download_albums_from_favorites, point) + ) + else: + # Playlist/Mix menu items (existing) + menu.addAction("Download Playlist", lambda: self.thread_download_list_media(point)) + menu.addAction( + "Download All Albums in Playlist", + lambda: self.thread_it(self.on_download_all_albums_from_playlist, point), + ) + menu.addAction("Copy Share URL", lambda: self.on_copy_url_share(self.tr_lists_user, point)) menu.exec(self.tr_lists_user.mapToGlobal(point)) @@ -872,6 +894,270 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): if queue_dl_item: self.queue_download_media(queue_dl_item) + def on_download_folder_playlists(self, point: QtCore.QPoint) -> None: + """Download all playlists in a folder. + + Args: + point (QPoint): The point in the tree where the folder was right-clicked. + """ + try: + # Get and validate the folder + item = self.tr_lists_user.itemAt(point) + media = get_user_list_media_item(item) + + if not isinstance(media, Folder): + logger_gui.error("Please select a folder.") + return + + # Fetch all playlists in the folder + logger_gui.info(f"Fetching playlists from folder: {media.name}") + playlists = self._get_folder_playlists(media) + + if not playlists: + logger_gui.info(f"No playlists found in folder: {media.name}") + return + + # Queue each playlist for download + logger_gui.info(f"Queueing {len(playlists)} playlists from folder: {media.name}") + + for playlist in playlists: + queue_dl_item: QueueDownloadItem | None = self.media_to_queue_download_model(playlist) + + if queue_dl_item: + self.queue_download_media(queue_dl_item) + + logger_gui.info(f"✅ Successfully queued {len(playlists)} playlists from folder: {media.name}") + + except Exception as e: + logger_gui.exception(f"Error downloading playlists from folder: {e}") + logger_gui.error("Failed to download playlists from folder. See log for details.") + + def on_download_folder_albums(self, point: QtCore.QPoint) -> None: + """Download all unique albums from all playlists in a folder. + + Args: + point (QPoint): The point in the tree where the folder was right-clicked. + """ + try: + # Get and validate the folder + item = self.tr_lists_user.itemAt(point) + media = get_user_list_media_item(item) + + if not isinstance(media, Folder): + logger_gui.error("Please select a folder.") + return + + # Fetch all playlists in the folder + logger_gui.info(f"Fetching playlists from folder: {media.name}") + playlists = self._get_folder_playlists(media) + + if not playlists: + logger_gui.info(f"No playlists found in folder: {media.name}") + return + + logger_gui.info(f"Found {len(playlists)} playlists in folder: {media.name}") + + # Collect all tracks from all playlists + all_tracks: list[Track] = [] + + for playlist in playlists: + try: + tracks = self._get_playlist_tracks(playlist) + all_tracks.extend(tracks) + logger_gui.debug(f"Collected {len(tracks)} tracks from playlist: {playlist.name}") + except Exception as e: + logger_gui.error(f"Error getting tracks from playlist '{playlist.name}': {e}") + continue + + if not all_tracks: + logger_gui.info(f"No tracks found in folder playlists: {media.name}") + return + + logger_gui.info(f"Collected {len(all_tracks)} total tracks from all playlists") + + # Extract unique album IDs + album_ids = self._extract_album_ids_from_tracks(all_tracks) + logger_gui.info(f"Found {len(album_ids)} unique albums across all playlists in folder: {media.name}") + + if not album_ids: + logger_gui.info("No albums found to download.") + return + + # Load full album objects with rate limiting + albums_dict = self._load_albums_with_rate_limiting(album_ids) + + if not albums_dict: + logger_gui.error("Failed to load any albums.") + return + + # Queue the albums for download + self._queue_loaded_albums(albums_dict) + + logger_gui.info(f"✅ Successfully queued {len(albums_dict)} unique albums from folder: {media.name}") + + except Exception as e: + logger_gui.exception(f"Error downloading albums from folder: {e}") + logger_gui.error("Failed to download albums from folder. See log for details.") + + def on_download_favorites(self, point: QtCore.QPoint) -> None: + """Download all items from a Favorites category. + + Args: + point (QPoint): The point in the tree where the favorites item was right-clicked. + """ + try: + # Get and validate the favorites item + item = self.tr_lists_user.itemAt(point) + media = get_user_list_media_item(item) + + if not isinstance(media, str): + logger_gui.error("Please select a favorites category.") + return + + # Get the favorites category name for logging + favorite_name = FAVORITES.get(media, {}).get("name", media) + logger_gui.info(f"Fetching all items from favorites: {favorite_name}") + + # Use the factory to get the appropriate favorites function + favorite_function = favorite_function_factory(self.tidal, media) + + # Fetch all items from this favorites category + media_items = favorite_function() + + if not media_items: + logger_gui.info(f"No items found in favorites: {favorite_name}") + return + + logger_gui.info(f"Found {len(media_items)} items in favorites: {favorite_name}") + + # Queue each item for download + queued_count = 0 + + for media_item in media_items: + queue_dl_item: QueueDownloadItem | None = self.media_to_queue_download_model(media_item) + + if queue_dl_item: + self.queue_download_media(queue_dl_item) + queued_count += 1 + + logger_gui.info(f"✅ Successfully queued {queued_count} items from favorites: {favorite_name}") + + except Exception as e: + logger_gui.exception(f"Error downloading favorites: {e}") + logger_gui.error("Failed to download favorites. See log for details.") + + def _download_albums_from_favorites_albums(self, media_items: list, favorite_name: str) -> None: + """Download albums from favorite albums list. + + Args: + media_items (list): List of favorite albums. + favorite_name (str): Name of the favorites category for logging. + """ + logger_gui.info(f"Queueing {len(media_items)} albums from favorites: {favorite_name}") + albums_dict = {album.id: album for album in media_items if isinstance(album, Album) and album.id} + self._queue_loaded_albums(albums_dict) + logger_gui.info(f"✅ Successfully queued {len(albums_dict)} albums from favorites: {favorite_name}") + + def _download_albums_from_favorites_artists(self, media_items: list, favorite_name: str) -> None: + """Download albums from favorite artists list. + + Args: + media_items (list): List of favorite artists. + favorite_name (str): Name of the favorites category for logging. + """ + logger_gui.info(f"Fetching albums from {len(media_items)} artists...") + all_albums = {} + + for artist in media_items: + if isinstance(artist, Artist): + try: + artist_albums = items_results_all(artist) + for album in artist_albums: + if isinstance(album, Album) and album.id: + all_albums[album.id] = album + logger_gui.debug(f"Found {len(artist_albums)} albums from artist: {artist.name}") + except Exception as e: + logger_gui.error(f"Error getting albums from artist '{artist.name}': {e}") + continue + + if not all_albums: + logger_gui.info("No albums found from favorite artists.") + return + + logger_gui.info(f"Found {len(all_albums)} unique albums from favorite artists") + self._queue_loaded_albums(all_albums) + logger_gui.info(f"✅ Successfully queued {len(all_albums)} albums from favorites: {favorite_name}") + + def _download_albums_from_favorites_tracks(self, media_items: list, favorite_name: str) -> None: + """Download albums from favorite tracks/videos/mixes list. + + Args: + media_items (list): List of favorite tracks/videos/mixes. + favorite_name (str): Name of the favorites category for logging. + """ + logger_gui.info("Extracting albums from tracks...") + album_ids = self._extract_album_ids_from_tracks(media_items) + + if not album_ids: + logger_gui.info(f"No albums found in favorites: {favorite_name}") + return + + logger_gui.info(f"Found {len(album_ids)} unique albums. Loading with rate limiting...") + + # Load full album objects with rate limiting + albums_dict = self._load_albums_with_rate_limiting(album_ids) + + if not albums_dict: + logger_gui.error("Failed to load any albums from favorites.") + return + + # Queue the albums for download + self._queue_loaded_albums(albums_dict) + logger_gui.info(f"✅ Successfully queued {len(albums_dict)} unique albums from favorites: {favorite_name}") + + def on_download_albums_from_favorites(self, point: QtCore.QPoint) -> None: + """Download all unique albums from items in a Favorites category. + + Args: + point (QPoint): The point in the tree where the favorites item was right-clicked. + """ + try: + # Get and validate the favorites item + item = self.tr_lists_user.itemAt(point) + media = get_user_list_media_item(item) + + if not isinstance(media, str): + logger_gui.error("Please select a favorites category.") + return + + # Get the favorites category name for logging + favorite_name = FAVORITES.get(media, {}).get("name", media) + logger_gui.info(f"Fetching all items from favorites: {favorite_name}") + + # Use the factory to get the appropriate favorites function + favorite_function = favorite_function_factory(self.tidal, media) + + # Fetch all items from this favorites category + media_items = favorite_function() + + if not media_items: + logger_gui.info(f"No items found in favorites: {favorite_name}") + return + + logger_gui.info(f"Found {len(media_items)} items in favorites: {favorite_name}") + + # Delegate to appropriate handler based on favorites type + if media == "fav_albums": + self._download_albums_from_favorites_albums(media_items, favorite_name) + elif media == "fav_artists": + self._download_albums_from_favorites_artists(media_items, favorite_name) + else: + self._download_albums_from_favorites_tracks(media_items, favorite_name) + + except Exception as e: + logger_gui.exception(f"Error downloading albums from favorites: {e}") + logger_gui.error("Failed to download albums from favorites. See log for details.") + def search_populate_results(self, query: str, type_media: Any) -> None: """Populate the results tree with search results. @@ -1572,6 +1858,43 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): return folders, playlists + def _get_folder_playlists(self, folder: Folder) -> list[Playlist]: + """Fetch all playlists from a folder. + + Args: + folder (Folder): The folder to fetch playlists from. + + Returns: + list[Playlist]: List of playlists in the folder. + """ + # Use existing method to fetch folder contents + # Since folders can't contain folders, we ignore the folders return value + _, playlists = self._fetch_folder_contents(folder) + + logger_gui.debug(f"Found {len(playlists)} playlists in folder: {folder.name}") + + return playlists + + def _get_playlist_tracks(self, playlist: Playlist | UserPlaylist | Mix) -> list[Track]: + """Fetch all tracks from a playlist. + + Args: + playlist (Playlist | UserPlaylist | Mix): The playlist to fetch tracks from. + + Returns: + list[Track]: List of tracks in the playlist. + """ + playlist_name = getattr(playlist, "name", "unknown") + logger_gui.debug(f"Fetching tracks from playlist: {playlist_name}") + media_items = items_results_all(playlist) + + # Filter for Track objects only (items_results_all may return Videos too) + tracks = [item for item in media_items if isinstance(item, Track)] + + logger_gui.debug(f"Found {len(tracks)} tracks in playlist: {playlist_name}") + + return tracks + def on_list_items_show(self, item: QtWidgets.QTreeWidgetItem) -> None: """Show the items in the selected playlist or mix.