diff --git a/wobuzz/player/playlist.py b/wobuzz/player/playlist.py index 1e55753..2265ebe 100644 --- a/wobuzz/player/playlist.py +++ b/wobuzz/player/playlist.py @@ -9,6 +9,11 @@ class Playlist: def __init__(self, app, title: str): self.app = app self.title = title # playlist title + + # add to unique names so if the playlist is loaded from disk, + # no other playlist can be created using the same name + self.app.utils.unique_names.append(self.title) + self.sorting: list[Qt.SortOrder] | None = None # Custom sort order if None self.tracks: list[Track] = [] self.current_track_index = 0 @@ -124,8 +129,8 @@ class Playlist: wbz.close() def delete(self): - self.app.utils.unique_names.pop(self.title) # remove from unique names so a new playlist can have the old name - + # remove from unique names so a new playlist can have the old name + self.app.utils.unique_names.remove(self.title) path = f"{self.app.settings.library_path}/playlists/{self.title.replace(" ", "_")}.wbz.m3u" if os.path.exists(path): diff --git a/wobuzz/ui/playlist_tabs/tab_title.py b/wobuzz/ui/playlist_tabs/tab_title.py index b48ad61..b471482 100644 --- a/wobuzz/ui/playlist_tabs/tab_title.py +++ b/wobuzz/ui/playlist_tabs/tab_title.py @@ -37,7 +37,7 @@ class TabTitle(QLineEdit): self.playlist_view.playlist.delete() - name = self.app.utils.unique_name(self.text()) + name = self.app.utils.unique_name(self.text(), ignore=self.playlist_view.playlist.title) self.setText(name) self.playlist_view.playlist.title = name diff --git a/wobuzz/utils.py b/wobuzz/utils.py index f9c0dde..901b57b 100644 --- a/wobuzz/utils.py +++ b/wobuzz/utils.py @@ -11,7 +11,7 @@ class Utils: def __init__(self, app): self.app = app - self.unique_names = {} + self.unique_names = [] def format_time(self, ms): seconds = int(ms / 1000) % 60 @@ -30,19 +30,26 @@ class Utils: return output - def unique_name(self, name: str) -> str: + def unique_name(self, name: str, ignore: str = None) -> str: """ Makes a name unique by adding a number to it if the name already exists. """ - if name in self.unique_names: - num_duplicates = self.unique_names[name] - self.unique_names[name] += 1 + # just return the original name if it is the ignore name + # this can be useful when e.g. renaming something bc. u don't want to append a number when the user doesn't + # change anything + if name == ignore: + return name - name = f"{name} {str(num_duplicates).zfill(2)}" + occurrences = 0 - else: - self.unique_names[name] = 1 + unique_name = name - return name + while unique_name in self.unique_names: + occurrences += 1 + unique_name = f"{name} {str(occurrences).zfill(2)}" + + self.unique_names.append(unique_name) + + return unique_name