From c3eded904d411ca18d88796c0ae672345c4c8438 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Sat, 18 Oct 2014 17:17:08 +0100 Subject: [PATCH] Miscellaneous improvements --- build.gradle | 4 +- .../com/dt/iTunesController/ITPlaylist.java | 27 ++++++++ .../iTunesController/ITTrackCollection.java | 8 +++ .../java/com/dt/iTunesController/iTunes.java | 64 +++++++++++++++++-- 4 files changed, 95 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 0c31a43..2501608 100644 --- a/build.gradle +++ b/build.gradle @@ -4,14 +4,14 @@ apply plugin: 'eclipse' group = 'com.dt' archivesBaseName = 'iTunesController' -version = '0.2' +version = '0.3' repositories { mavenCentral() } dependencies { - compile 'net.sf.jacob-project:jacob:1.+' + compile 'net.sf.jacob-project:jacob:1.14.3' } uploadArchives { diff --git a/src/main/java/com/dt/iTunesController/ITPlaylist.java b/src/main/java/com/dt/iTunesController/ITPlaylist.java index 25cb5f4..6c937cf 100644 --- a/src/main/java/com/dt/iTunesController/ITPlaylist.java +++ b/src/main/java/com/dt/iTunesController/ITPlaylist.java @@ -117,6 +117,14 @@ public class ITPlaylist extends ITObject { Dispatch.put(object, "SongRepeat", repeatMode.ordinal()); } + /** + * Cycle repeat modes. + */ + public void cycleSongRepeat() { + int repeat = Dispatch.get(object, "SongRepeat").getInt(); + Dispatch.put(object, "SongRepeat", (repeat + 1) % 3); + } + /** * Returns the playback repeat mode. * @return Returns the playback repeat mode. @@ -150,4 +158,23 @@ public class ITPlaylist extends ITObject { Dispatch tracks = Dispatch.get(object, "Tracks").toDispatch(); return new ITTrackCollection(tracks); } + + /** + * Returns the shuffle state. + * @return true if current state is shuffle. + */ + public boolean getShuffle() { + return Dispatch.get(object, "Shuffle").getBoolean(); + } + + /** + * Toggle the shuffle state. + */ + public void toggleShuffle() { + setShuffle(!getShuffle()); + } + + public boolean containsTrack(ITTrack track) { + return getTracks().containsTrack(track); + } } diff --git a/src/main/java/com/dt/iTunesController/ITTrackCollection.java b/src/main/java/com/dt/iTunesController/ITTrackCollection.java index b87208f..3e1f3bf 100644 --- a/src/main/java/com/dt/iTunesController/ITTrackCollection.java +++ b/src/main/java/com/dt/iTunesController/ITTrackCollection.java @@ -88,4 +88,12 @@ public class ITTrackCollection { return new ITTrack(item); } + public boolean containsTrack(ITTrack track) { + String name = track.getName(); + try { + return ItemByName(name).getName().equals(name); + } catch (IllegalStateException e) { + return false; + } + } } diff --git a/src/main/java/com/dt/iTunesController/iTunes.java b/src/main/java/com/dt/iTunesController/iTunes.java index 5d113ec..6a54d07 100644 --- a/src/main/java/com/dt/iTunesController/iTunes.java +++ b/src/main/java/com/dt/iTunesController/iTunes.java @@ -1,5 +1,7 @@ package com.dt.iTunesController; + import com.jacob.activeX.ActiveXComponent; +import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.DispatchEvents; @@ -13,18 +15,18 @@ import com.jacob.com.DispatchEvents; * @version 0.2 */ public class iTunes { - ActiveXComponent iTunes; iTunesEvents iTunesEvents; DispatchEvents dispatchEvents; - + /** * Initiate iTunes Controller. + * @return */ - public iTunes() { + public void connect() { iTunes = new ActiveXComponent("iTunes.Application"); } - + /** * Add an event handler to the iTunes controller. * @param itef The class that will handle the iTunes events. @@ -32,7 +34,7 @@ public class iTunes { public void addEventHandler(iTunesEventsInterface itef) { iTunesEvents = new iTunesEvents(itef); dispatchEvents = new DispatchEvents(iTunes, iTunesEvents); - System.out.println("New event handler added."); + //System.out.println("New event handler added."); } /** @@ -333,7 +335,21 @@ public class iTunes { public boolean getMute() { return iTunes.getPropertyAsBoolean("Mute"); } - + + /** + * Toggle the mute state. + */ + public void toggleMute() { + setMute(!getMute()); + } + + /** + * Toggle the shuffle state. + */ + public void toggleShuffle() { + getCurrentPlaylist().toggleShuffle(); + } + /** * Returns the current player state. * @return Returns the current player state. @@ -484,4 +500,40 @@ public class iTunes { Dispatch window = iTunes.getProperty("BrowserWindow").toDispatch(); return new ITBrowserWindow(window); } + + public void cycleSongRepeat() { + getCurrentPlaylist().cycleSongRepeat(); + } + + public ITPlaylist getPlaylist(String name) { + ITPlaylistCollection playlistCollection = getLibrarySource().getPlaylists(); + ITPlaylist playlist = playlistCollection.ItemByName(name); + try { + playlist.getName(); + } catch (IllegalStateException e) { + playlist = createPlaylist(name); + } + return playlist; + } + + public ITUserPlaylist getUserPlaylist(String name) { + ITPlaylist playlist = getPlaylist(name); + ITUserPlaylist userPlaylist = new ITUserPlaylist(playlist.fetchDispatch()); + return userPlaylist; + } + + public void playlistAddTrack(String name, ITTrack track) { + ITUserPlaylist userPlaylist = getUserPlaylist(name); + if (!userPlaylist.containsTrack(track)) { + userPlaylist.addTrack(track); + } + } + + public void playlistAddCurrentTrack(String name) { + playlistAddTrack(name, getCurrentTrack()); + } + + public void release() { + ComThread.Release(); + } }