]> code.delx.au - notipod/commitdiff
Auto reload library when window comes to the foreground
authorJames Bunton <jamesbunton@delx.net.au>
Thu, 12 Jul 2012 01:05:48 +0000 (11:05 +1000)
committerJames Bunton <jamesbunton@delx.net.au>
Thu, 12 Jul 2012 01:05:48 +0000 (11:05 +1000)
English.lproj/NotiPod.xib
libnotipod.py
notipod_gui.py

index 4c384a50f53501ea69daebdd5918085b05cb4f4d..b48b5284d9933fdecaafc3805fb4f1b4bab81b43 100644 (file)
@@ -12,9 +12,9 @@
                </object>
                <object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
                        <bool key="EncodedWithXMLCoder">YES</bool>
-                       <integer value="372"/>
-                       <integer value="528"/>
                        <integer value="29"/>
+                       <integer value="528"/>
+                       <integer value="372"/>
                </object>
                <object class="NSArray" key="IBDocument.PluginDependencies">
                        <bool key="EncodedWithXMLCoder">YES</bool>
                                        </object>
                                        <int key="connectionID">646</int>
                                </object>
+                               <object class="IBConnectionRecord">
+                                       <object class="IBOutletConnection" key="connection">
+                                               <string key="label">delegate</string>
+                                               <reference key="source" ref="972006081"/>
+                                               <reference key="destination" ref="719278169"/>
+                                       </object>
+                                       <int key="connectionID">647</int>
+                               </object>
                        </object>
                        <object class="IBMutableOrderedSet" key="objectRecords">
                                <object class="NSArray" key="orderedObjects">
                                </object>
                        </object>
                        <nil key="sourceID"/>
-                       <int key="maxID">646</int>
+                       <int key="maxID">647</int>
                </object>
                <object class="IBClassDescriber" key="IBDocument.Classes">
                        <object class="NSMutableArray" key="referencedPartialClassDescriptions">
index 2a996ec1aa2fc849e0ac1b9d576d5060cf9283ac..30a6a08c625afcc39d679a466e18e1195d104e22 100644 (file)
@@ -39,10 +39,13 @@ class Playlist(NSObject):
                        parent.children.append(self)
 
 class ITunesLibrary(NSObject):
-       def load_(self, filename):
+       def load_(self, filename=None):
                if filename is None:
-                       filename = "~/Music/iTunes/iTunes Music Library.xml"
-               filename = os.path.expanduser(filename)
+                       filename = getattr(self, "filename", None)
+               if filename is None:
+                       filename = os.path.expanduser("~/Music/iTunes/iTunes Music Library.xml")
+               self.filename = filename
+               self.mtime = os.stat(filename).st_mtime
                yield "Reading library..."
                plist = read_plist(os.path.expanduser(filename))
                if plist is None:
@@ -61,6 +64,9 @@ class ITunesLibrary(NSObject):
                        self.playlists.append(playlist)
                        pl_lookup[playlist.pid] = playlist
 
+       def needs_reload(self):
+               return os.stat(self.filename).st_mtime > self.mtime
+
        def loc2name(self, location):
                return urllib.splithost(urllib.splittype(urllib.unquote(location))[1])[1]
 
index 52b4456faa633bb910a0a68b0f9b6835fd39857c..283a547782f86e5a38294e95515dc38d7ad6e523 100644 (file)
@@ -143,13 +143,9 @@ class NotiPodController(NSObject):
                for target in self.targets:
                        folders.append(target["folder"])
                self.folderModel.loadFolders_(folders)
-               
+
                self.library = libnotipod.ITunesLibrary.alloc().init()
-               def finish():
-                       self.playlistModel.setPlaylists(self.library.get_playlists())
-               def fail():
-                       sys.exit(0)
-               self.runGenerator(lambda: self.library.load_(None), finish, fail)
+               self.loadLibrary()
 
        def applicationWillTerminate_(self, _):
                self.prefs().synchronize()
@@ -157,8 +153,26 @@ class NotiPodController(NSObject):
        def applicationShouldTerminateAfterLastWindowClosed_(self, _):
                return True
 
+       def windowDidBecomeKey_(self, _):
+               if self.library.needs_reload():
+                       print "needs reload!"
+                       self.loadLibrary()
+               else:
+                       print "no reloading this time"
+
 
        # Utility methods
+       def loadLibrary(self):
+               if self.runningGenerator:
+                       return
+
+               def finish():
+                       self.playlistModel.setPlaylists(self.library.get_playlists())
+               def fail():
+                       NSRunAlertPanel("Error!", "Unable to load iTunes library! Exiting...", "Ok", None, None)
+                       sys.exit(0)
+               self.runGenerator(lambda: self.library.load_(None), finish, fail)
+
        def runGenerator(self, func, finish, fail):
                assert not self.runningGenerator
                self.runningGenerator = True
@@ -301,7 +315,7 @@ class NotiPodController(NSObject):
 
        def _migratePrefs(self):
                p = self.prefs()
-               
+
                playlists = p.stringArrayForKey_("playlists")
                if playlists is not None:
                        p.removeObjectForKey_("playlists")
@@ -329,8 +343,8 @@ class NotiPodController(NSObject):
        def _loadPrefs(self):
                p = self.prefs()
 
-               self.current_target = None
-               self.setCurrentTarget_(p.stringForKey_("current_target"))
+               self.currentTarget = None
+               self.setCurrentTarget_(p.stringForKey_("currentTarget"))
 
                self.targets = self.prefs().arrayForKey_("targets")
                if self.targets is None:
@@ -343,22 +357,22 @@ class NotiPodController(NSObject):
 
        def _savePrefs(self):
                p = self.prefs()
-               p.setObject_forKey_(self.current_target, "current_target")
+               p.setObject_forKey_(self.currentTarget, "currentTarget")
                p.setObject_forKey_(self.targets, "targets")
                p.synchronize()
 
        def getCurrentTarget(self):
                for target in self.targets:
-                       if target["uuid"] == self.current_target:
+                       if target["uuid"] == self.currentTarget:
                                return target
                return None
-       
-       def setCurrentTarget_(self, target_uuid):
-               old_uuid = self.current_target
-               self.current_target = target_uuid
-               if old_uuid is None and target_uuid is not None:
+
+       def setCurrentTarget_(self, targetUuid):
+               oldUuid = self.currentTarget
+               self.currentTarget = targetUuid
+               if oldUuid is None and targetUuid is not None:
                        self.playlistModel.outlineView.setEnabled_(True)
-               if old_uuid != target_uuid:
+               if oldUuid != targetUuid:
                        self.playlistModel.outlineView.reloadItem_reloadChildren_(None, True)
 
        def playlists(self):