]> code.delx.au - notipod/blobdiff - notipod_gui.py
Ignore tracks not in the library
[notipod] / notipod_gui.py
index 2d2cffdc2598b182be6365afc01866bac0950036..11c6880a86604a493a22d27dca616e220d228ce6 100644 (file)
@@ -5,6 +5,7 @@
 import logging
 import os
 import sys
+import time
 import traceback
 import uuid
 
@@ -173,12 +174,15 @@ class NotiPodController(NSObject):
 
        def runGeneratorThread(self, (gen, finish, fail)):
                pool = NSAutoreleasePool.alloc().init()
+               last_time = 0
                try:
                        for msg in gen:
                                if not self.runningGenerator:
                                        break
-                               self.loadingLabel.performSelectorOnMainThread_withObject_waitUntilDone_(
-                                       self.loadingLabel.setStringValue_, msg, True)
+                               now = time.time()
+                               if now - last_time > 0.1:
+                                       self.loadingLabel.performSelectorOnMainThread_withObject_waitUntilDone_(
+                                               self.loadingLabel.setStringValue_, msg, True)
                except Exception, e:
                        NSRunAlertPanel("Error!", str(e), "Ok", None, None)
                        traceback.print_exc()
@@ -205,7 +209,7 @@ class NotiPodController(NSObject):
                        self.playlistModel.setPlaylists(self.library.get_playlists())
                def fail():
                        NSRunAlertPanel("Error!", "Unable to load iTunes library! Exiting...", "Ok", None, None)
-                       sys.exit(0)
+                       os._exit(0)
                self.runGenerator(lambda: self.library.load_(None), finish, fail)
 
        @objc.IBAction
@@ -236,11 +240,16 @@ class NotiPodController(NSObject):
                        NSRunAlertPanel("Error!", "You must choose a folder first!", "Ok", None, None)
                        return
                folder = target["folder"]
+
                if not os.path.isdir(folder.encode("utf-8")):
                        NSRunAlertPanel("Error!", "Destination " + folder + " does not exist, try mounting it first?", "Ok", None, None)
                        return
-               target["folder"] = folder
-               target["path_prefix"] = target["path_prefix"].encode("utf-8")
+
+               folder_contents = [f for f in os.listdir(folder) if not f.startswith(".")]
+               if len(folder_contents) > 0 and "-Playlists-" not in folder_contents:
+                       NSRunAlertPanel("Error!", "Refusing to clobber files in non-empty folder: " + folder, "Ok", None, None)
+                       return
+
                return target
 
        def doPreviewThread(self):
@@ -258,7 +267,9 @@ class NotiPodController(NSObject):
 
                all_filenames = []
                for trackID in all_tracks:
-                       all_filenames.append(self.library.get_track_filename(trackID))
+                       f = self.library.get_track_filename(trackID)
+                       if f:
+                               all_filenames.append(f)
 
                gen = libnotipod.sync(
                        dry_run=True,
@@ -299,17 +310,24 @@ class NotiPodController(NSObject):
 
                all_filenames = []
                for trackID in all_tracks:
-                       all_filenames.append(self.library.get_track_filename(trackID))
+                       f = self.library.get_track_filename(trackID)
+                       if f:
+                               all_filenames.append(f)
                        all_playlists.update(self.library.get_track_playlists(trackID))
 
+               libnotipod.delete_playlists(dry_run=False, dest=target["folder"])
+
                for playlist_id in all_playlists:
                        playlist = self.library.get_playlist_pid(playlist_id)
                        if playlist is None:
                                continue
                        tracks = []
                        for trackID in playlist.tracks:
-                               if trackID in all_tracks:
-                                       tracks.append(self.library.get_track_filename(trackID))
+                               if trackID not in all_tracks:
+                                       continue
+                               f = self.library.get_track_filename(trackID)
+                               if f:
+                                       tracks.append(f)
                        if playlist_id not in orig_playlists and len(tracks) < 10:
                                continue
                        libnotipod.export_m3u(
@@ -360,7 +378,7 @@ class NotiPodController(NSObject):
                        target = {}
                        target["folder"] = f
                        target["playlists"] = list(playlists)
-                       target["uuid"] = uuid.uuid1().get_hex()
+                       target["uuid"] = uuid.uuid4().get_hex()
                        target["path_prefix"] = "../"
                        if first:
                                first = False
@@ -420,7 +438,7 @@ class NotiPodController(NSObject):
                        target = {}
                        target["folder"] = folder
                        target["playlists"] = self.playlists()
-                       target["uuid"] = uuid.uuid1().get_hex()
+                       target["uuid"] = uuid.uuid4().get_hex()
                        target["path_prefix"] = "../"
                        self.targets.insertObject_atIndex_(target, 0)