2 # Copyright 2009 James Bunton <jamesbunton@fastmail.fm>
3 # Licensed for distribution under the GPL version 2, check COPYING for details
6 from Foundation
import *
8 from PyObjCTools
import AppHelper
13 class PlaylistModel(NSObject
):
14 outlineView
= objc
.IBOutlet()
16 def awakeFromNib(self
):
19 self
.outlineView
.setDataSource_(self
)
21 def setPlaylists(self
, playlists
):
23 self
.playlists
= playlists
24 for playlist
in self
.playlists
:
25 if playlist
.parent
is None:
26 self
.root
.append(playlist
)
27 self
.outlineView
.reloadData()
29 def outlineView_child_ofItem_(self
, _
, childIndex
, playlist
):
31 return self
.root
[childIndex
]
33 return playlist
.children
[childIndex
]
35 def outlineView_isItemExpandable_(self
, _
, playlist
):
39 return len(playlist
.children
) > 0
41 def outlineView_numberOfChildrenOfItem_(self
, _
, playlist
):
45 return len(playlist
.children
)
47 def outlineView_objectValueForTableColumn_byItem_(self
, _
, col
, playlist
):
48 col
= col
.identifier() if col
else "playlist"
51 selected
= NSApp
.delegate().playlists()
52 return playlist
.pid
in selected
53 if col
== None or col
== "playlist":
56 def outlineView_setObjectValue_forTableColumn_byItem_(self
, _
, v
, col
, playlist
):
57 col
= col
.identifier() if col
else "playlist"
62 NSApp
.delegate().addPlaylist_(playlist
.pid
)
64 NSApp
.delegate().delPlaylist_(playlist
.pid
)
67 class NotiPodController(NSObject
):
68 playlistModel
= objc
.IBOutlet()
69 folderPopup
= objc
.IBOutlet()
70 window
= objc
.IBOutlet()
71 loadingSheet
= objc
.IBOutlet()
74 def awakeFromNib(self
):
75 self
.performSelectorInBackground_withObject_(self
.loadLibrary
, None)
77 def finishLoading(self
):
78 self
.playlistModel
.setPlaylists(self
.library
.get_playlists())
79 self
.folderPopup
.addItemsWithTitles_(NSApp
.delegate().folders())
81 def loadLibrary(self
):
82 pool
= NSAutoreleasePool
.alloc().init()
83 NSApp
.beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(self
.loadingSheet
, self
.window
, None, None, None)
84 self
.library
= libsyncitunes
.ITunesLibrary
.alloc().init()
85 self
.loadingSheet
.close()
86 self
.performSelectorOnMainThread_withObject_waitUntilDone_(self
.finishLoading
, None, True)
89 @objc.signature("v@:@ii")
90 def selectFolderEnd_returnCode_contextInfo_(self
, panel
, ret
, _
):
92 assert len(panel
.filenames()) == 1
93 folder
= panel
.filenames()[0]
94 ### NSApp.delegate().addFolder_(folder)
95 self
.folderPopup
.insertItemWithTitle_atIndex_(folder
, 2)
96 self
.folderPopup
.selectItemAtIndex_(2)
99 def doSelectFolder_(self
, sender
):
100 print "select folder"
102 folders
= NSApp
.delegate().folders()
107 panel
= NSOpenPanel
.openPanel()
108 panel
.setCanChooseFiles_(False)
109 panel
.setCanChooseDirectories_(True)
110 panel
.setAllowsMultipleSelection_(False)
111 panel
.beginSheetForDirectory_file_types_modalForWindow_modalDelegate_didEndSelector_contextInfo_(folder
, None, None, self
.window
, self
, self
.selectFolderEnd_returnCode_contextInfo_
, None)
114 traceback
.print_exc()
117 def doSync_(self
, sender
):
121 class NotiPodAppDelegate(NSObject
):
124 def applicationWillFinishLaunching_(self
, _
):
127 def applicationDidFinishLaunching_(self
, _
):
130 def applicationWillTerminate_(self
, _
):
131 self
.prefs().synchronize()
133 def applicationShouldTerminateAfterLastWindowClosed_(self
, _
):
140 return NSUserDefaults
.standardUserDefaults()
142 def _getArray(self
, key
):
143 res
= self
.prefs().stringArrayForKey_(key
)
144 return list(res
) if res
else []
146 def _saveArray(self
, key
, array
):
147 self
.prefs().setObject_forKey_(array
, key
)
150 return self
._getArray
("playlists")
153 return self
._getArray
("folders")
155 def addFolder_(self
, folder
):
156 folders
= self
.folders()
157 folders
.append(folder
)
158 self
._saveArray
("folders", folders
)
160 def delPlaylist_(self
, playlist
):
161 playlists
= self
.playlists()
162 playlists
.remove(playlist
)
163 self
._saveArray
("playlists", playlists
)
165 def addPlaylist_(self
, playlist
):
166 playlists
= self
.playlists()
167 playlists
.append(playlist
)
168 self
._saveArray
("playlists", playlists
)
172 AppHelper
.runEventLoop()