diff --git a/app/_windows/jukebox_control_window.rb b/app/_windows/jukebox_control_window.rb index 20ff3bd..e47f81d 100644 --- a/app/_windows/jukebox_control_window.rb +++ b/app/_windows/jukebox_control_window.rb @@ -10,7 +10,43 @@ def self.build ).tap do |win| win.setFrameAutosaveName("MAINWINDOWPositionHeightWidth") win.build_views + + win.registerForDraggedTypes([NSPasteboardTypeString]) + end + end + + def draggingEntered(sender) + source_drag_mask = sender.draggingSourceOperationMask + pboard = sender.draggingPasteboard + + if pboard.types.include?(NSPasteboardTypeString) + if source_drag_mask & NSDragOperationGeneric + return NSDragOperationGeneric + end end + + NSDragOperationNone + end + + def draggingUpdated(sender) + NSDragOperationCopy + end + + def performDragOperation(sender) + source_drag_mask = sender.draggingSourceOperationMask + pboard = sender.draggingPasteboard + + if pboard.types.include?(NSPasteboardTypeString) + str = pboard.stringForType(NSPasteboardTypeString) + + # only handle tracks at the moment + # e.g http://open.spotify.com/track/75clRtmtXSkZsPm6SjjQ5j + if str.match(/http:\/\/open\.spotify\.com\/track\/[a-zA-Z0-9]*/) + PlaylistHandler.add!(str) + end + end + + true end def canBecomeKeyWindow diff --git a/app/config/constants.rb b/app/config/constants.rb index c320109..a5ad8ce 100644 --- a/app/config/constants.rb +++ b/app/config/constants.rb @@ -1,6 +1,7 @@ WEBSOCKET_HOST = 'jukebox.local' WEBSOCKET_URL = "ws://#{WEBSOCKET_HOST}:8081" -VOTE_URL = "http://#{WEBSOCKET_HOST}/external?" +JUKEBOX_URL = "http://#{WEBSOCKET_HOST}" +VOTE_URL = "#{JUKEBOX_URL}/external?" DEFAULT_MENU_WIDTH=250.0 diff --git a/app/lib/jukebox_handler.rb b/app/lib/jukebox_handler.rb index 8cd8fc9..7c9a2dc 100644 --- a/app/lib/jukebox_handler.rb +++ b/app/lib/jukebox_handler.rb @@ -33,25 +33,7 @@ def after_update end def do_notifications - jukebox.notifications.each do |message| - gcdq = Dispatch::Queue.new('com.kyan.kyanbar') - gcdq.async do - notification = NSUserNotification.new - notification.title = message.heading - notification.subtitle = message.subtitle - notification.informativeText = message.description - - if !message.artwork_url.nil? - url = NSURL.URLWithString(message.artwork_url) - if url - artwork_image = NSImage.alloc.initWithContentsOfURL(url) - notification.contentImage = artwork_image - end - end - - NSUserNotificationCenter.defaultUserNotificationCenter.scheduleNotification(notification) - end - end + KyanBar::Notifier.send!(jukebox.notifications) end end \ No newline at end of file diff --git a/app/lib/notifier.rb b/app/lib/notifier.rb new file mode 100644 index 0000000..d3e2334 --- /dev/null +++ b/app/lib/notifier.rb @@ -0,0 +1,35 @@ +module KyanBar + class Notifier + attr_reader :notifications + + def initialize(notifications=[]) + @notifications = notifications + end + + def self.send!(notifications) + new(notifications).process! + end + + def process! + notifications.each do |message| + gcdq = Dispatch::Queue.new('com.kyan.kyanbar') + gcdq.async do + notification = NSUserNotification.new + notification.title = message.heading + notification.subtitle = message.subtitle + notification.informativeText = message.description + + if !message.artwork_url.nil? + url = NSURL.URLWithString(message.artwork_url) + if url + artwork_image = NSImage.alloc.initWithContentsOfURL(url) + notification.contentImage = artwork_image + end + end + + NSUserNotificationCenter.defaultUserNotificationCenter.scheduleNotification(notification) + end + end + end + end +end \ No newline at end of file diff --git a/app/lib/playlist_handler.rb b/app/lib/playlist_handler.rb new file mode 100644 index 0000000..bfbf0f3 --- /dev/null +++ b/app/lib/playlist_handler.rb @@ -0,0 +1,94 @@ +class PlaylistHandler + + attr_reader :data_as_json + + def initialize(track) + @data_as_json = BW::JSON.generate({'filenames' => [track]}) + end + + def self.add!(track) + handler = new(track) + handler.add + end + + def add + if valid? + AFMotion::HTTP.get(dest_url) do |response| + if response.body.to_s.include?('login details incorrect') + alert_creds_wrong + else + if response.success? + parse_json(response.body) + do_notification + end + end + end + else + alert_no_creds + false + end + end + + private + + def dest_url + json_encode = data_as_json.to_url_encoded + encoded_str = "user=#{username}&password=#{password}&argument=#{json_encode}" + "#{JUKEBOX_URL}/run/bulk_add_to_playlist?#{encoded_str}" + end + + def valid? + username && password + end + + def username + Persistence.get("jukeboxUsername") + end + + def password + Persistence.get("jukeboxPassword") + end + + def alert_creds_wrong + oops( + 'Doh?', + 'Your jukebox username or password appear to be wrong!' + ) + end + + def alert_no_creds + oops( + 'Want to vote huh?', + 'You need to set your username and password in preferences' + ) + end + + def oops(title, message) + alert = NSAlert.new + alert.messageText = title + alert.informativeText = message + alert.alertStyle = NSInformationalAlertStyle + alert.addButtonWithTitle("Ok") + response = alert.runModal + end + + def parse_json(str) + e = Pointer.new(:object) + json = NSJSONSerialization.JSONObjectWithData( String(str).to_data, + options:0, + error: e + ) + @track = json.first + end + + def do_notification + track = KyanJukebox::Track.new + track.title = 'Successfully added to Jukebox' + track.artist = "#{@track["title"]} - #{@track["artist"]}" + track.album = @track["album"] + track.artwork_url = @track["artwork_url"] + + KyanBar::Notifier.send!([track]) + end + +end \ No newline at end of file