| 1 |
/* |
|---|
| 2 |
* LastPod is an application used to publish one's iPod play counts to Last.fm. |
|---|
| 3 |
* Copyright (C) 2007 Chris Tilden |
|---|
| 4 |
* |
|---|
| 5 |
* This program is free software; you can redistribute it and/or |
|---|
| 6 |
* modify it under the terms of the GNU General Public License |
|---|
| 7 |
* as published by the Free Software Foundation; either version 2 |
|---|
| 8 |
* of the License, or (at your option) any later version. |
|---|
| 9 |
* |
|---|
| 10 |
* This program is distributed in the hope that it will be useful, |
|---|
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 |
* GNU General Public License for more details. |
|---|
| 14 |
* |
|---|
| 15 |
* You should have received a copy of the GNU General Public License |
|---|
| 16 |
* along with this program; if not, write to the Free Software |
|---|
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 18 |
*/ |
|---|
| 19 |
package org.lastpod; |
|---|
| 20 |
|
|---|
| 21 |
import org.lastpod.util.MiscUtilities; |
|---|
| 22 |
|
|---|
| 23 |
import java.io.IOException; |
|---|
| 24 |
|
|---|
| 25 |
import java.util.ArrayList; |
|---|
| 26 |
import java.util.List; |
|---|
| 27 |
import java.util.logging.Level; |
|---|
| 28 |
import java.util.logging.Logger; |
|---|
| 29 |
import java.util.prefs.Preferences; |
|---|
| 30 |
|
|---|
| 31 |
import javax.swing.JOptionPane; |
|---|
| 32 |
|
|---|
| 33 |
/** |
|---|
| 34 |
* The LastPod controller. |
|---|
| 35 |
* @author muti |
|---|
| 36 |
* @author Chris Tilden |
|---|
| 37 |
* @version $Id$ |
|---|
| 38 |
*/ |
|---|
| 39 |
public class LastPod { |
|---|
| 40 |
public static UI UI; |
|---|
| 41 |
public static List recentplayed; //parsed using DbReader class |
|---|
| 42 |
private static Scrobbler scrobbler; |
|---|
| 43 |
private static Logger logger; |
|---|
| 44 |
private final static String NO_PREFS_ERROR = |
|---|
| 45 |
"You have not setup your preferences.\n" |
|---|
| 46 |
+ "Please click Preferences below to configure the location of " |
|---|
| 47 |
+ "your iTunesDB (it's on your iPod's drive) and your AudioScrobbler " |
|---|
| 48 |
+ "username and password."; |
|---|
| 49 |
|
|---|
| 50 |
private static void Load() { |
|---|
| 51 |
LastPod.recentplayed = new ArrayList(); |
|---|
| 52 |
LastPod.UI = new UI(); |
|---|
| 53 |
LastPod.UI.buildUI(); |
|---|
| 54 |
|
|---|
| 55 |
logger = Logger.getLogger(LastPod.class.getPackage().getName()); |
|---|
| 56 |
logger.setLevel(Level.ALL); |
|---|
| 57 |
logger.addHandler(new LogHandler()); |
|---|
| 58 |
|
|---|
| 59 |
LastPod.ParsePlayCounts(); |
|---|
| 60 |
|
|---|
| 61 |
LastPod.UI.makeVisable(); |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
public static void ParsePlayCounts() { |
|---|
| 65 |
Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); |
|---|
| 66 |
String iTunesPath = fPrefs.get("iTunes Path", "default"); |
|---|
| 67 |
String parseVariousArtistsStr = fPrefs.get("parseVariousArtists", "1"); |
|---|
| 68 |
boolean parseVariousArtists = parseVariousArtistsStr.equals("1") ? true : false; |
|---|
| 69 |
|
|---|
| 70 |
if (iTunesPath.equals("default")) { |
|---|
| 71 |
logger.log(Level.INFO, LastPod.NO_PREFS_ERROR); |
|---|
| 72 |
|
|---|
| 73 |
return; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
DbReader reader = new DbReader(iTunesPath, parseVariousArtists); |
|---|
| 77 |
|
|---|
| 78 |
try { |
|---|
| 79 |
reader.parse(); |
|---|
| 80 |
LastPod.recentplayed = reader.getRecentplays(); |
|---|
| 81 |
LastPod.UI.newTrackListAvailable(); |
|---|
| 82 |
} catch (IOException e) { |
|---|
| 83 |
StackTraceElement[] trace = e.getStackTrace(); |
|---|
| 84 |
|
|---|
| 85 |
for (int i = 0; i < trace.length; i++) { |
|---|
| 86 |
if (trace[i].getClassName().startsWith("org.lastpod")) { |
|---|
| 87 |
logger.log(Level.SEVERE, trace[i].toString()); |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
logger.log(Level.SEVERE, e.toString()); |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
public static Object SubmitTracks() { |
|---|
| 96 |
Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); |
|---|
| 97 |
String username = fPrefs.get("Username", "default"); |
|---|
| 98 |
String password = fPrefs.get("Password", "default"); |
|---|
| 99 |
String encryptedPassword = fPrefs.get("encryptedPassword", "default"); |
|---|
| 100 |
|
|---|
| 101 |
if (!password.equals("default")) { |
|---|
| 102 |
encryptedPassword = MiscUtilities.md5DigestPassword(password); |
|---|
| 103 |
fPrefs.put("encryptedPassword", encryptedPassword); |
|---|
| 104 |
fPrefs.remove("Password"); |
|---|
| 105 |
|
|---|
| 106 |
String message = |
|---|
| 107 |
"Your password was stored unencrypted on your system." |
|---|
| 108 |
+ " This version of LastPod has encrypted this password for future usage."; |
|---|
| 109 |
JOptionPane.showMessageDialog(UI.getFrame(), message); |
|---|
| 110 |
|
|---|
| 111 |
logger = Logger.getLogger(LastPod.class.getPackage().getName()); |
|---|
| 112 |
logger.log(Level.WARNING, message); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
String encryptedDefault = MiscUtilities.md5DigestPassword("default"); |
|---|
| 116 |
|
|---|
| 117 |
if (username.equals("default") && encryptedPassword.equals(encryptedDefault)) { |
|---|
| 118 |
logger.log(Level.INFO, LastPod.NO_PREFS_ERROR); |
|---|
| 119 |
|
|---|
| 120 |
return LastPod.NO_PREFS_ERROR; |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
String backupUrl = fPrefs.get("backupUrl", ""); |
|---|
| 124 |
|
|---|
| 125 |
try { |
|---|
| 126 |
LastPod.scrobbler = new Scrobbler(username, encryptedPassword, backupUrl); |
|---|
| 127 |
|
|---|
| 128 |
List activeRecentPlayed = onlyActiveTrackItems(recentplayed); |
|---|
| 129 |
List inactiveRecentPlayed = onlyInactiveTrackItems(recentplayed); |
|---|
| 130 |
LastPod.scrobbler.handshake(activeRecentPlayed); |
|---|
| 131 |
LastPod.scrobbler.submitTracks(activeRecentPlayed, LastPod.UI); |
|---|
| 132 |
LastPod.scrobbler.addHistories(activeRecentPlayed, inactiveRecentPlayed); |
|---|
| 133 |
|
|---|
| 134 |
/* Refresh track list. */ |
|---|
| 135 |
LastPod.UI.newTrackListAvailable(); |
|---|
| 136 |
} catch (Exception e) { |
|---|
| 137 |
StackTraceElement[] trace = e.getStackTrace(); |
|---|
| 138 |
|
|---|
| 139 |
for (int i = 0; i < trace.length; i++) { |
|---|
| 140 |
if (trace[i].getClassName().startsWith("org.lastpod")) { |
|---|
| 141 |
logger.log(Level.SEVERE, trace[i].toString()); |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
logger.log(Level.SEVERE, e.toString()); |
|---|
| 146 |
JOptionPane.showMessageDialog(null, e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
return "Success"; |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
private static List onlyActiveTrackItems(List recentPlayed) { |
|---|
| 153 |
return filterTrackItems(recentPlayed, true); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
private static List onlyInactiveTrackItems(List recentPlayed) { |
|---|
| 157 |
return filterTrackItems(recentPlayed, false); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
private static List filterTrackItems(List recentPlayed, boolean filterActive) { |
|---|
| 161 |
List filteredRecentPlayed = new ArrayList(); |
|---|
| 162 |
|
|---|
| 163 |
for (int i = 0; i < recentPlayed.size(); i++) { |
|---|
| 164 |
TrackItem trackItem = (TrackItem) recentPlayed.get(i); |
|---|
| 165 |
|
|---|
| 166 |
boolean trackActive = trackItem.isActive().booleanValue(); |
|---|
| 167 |
|
|---|
| 168 |
if (trackActive && filterActive) { |
|---|
| 169 |
filteredRecentPlayed.add(trackItem); |
|---|
| 170 |
} else if (!trackActive && !filterActive) { |
|---|
| 171 |
filteredRecentPlayed.add(trackItem); |
|---|
| 172 |
} |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
return filteredRecentPlayed; |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
public static void selectAll() { |
|---|
| 179 |
setupSelections(true); |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
public static void unselectAll() { |
|---|
| 183 |
setupSelections(false); |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
private static void setupSelections(boolean select) { |
|---|
| 187 |
for (int i = 0; i < recentplayed.size(); i++) { |
|---|
| 188 |
TrackItem trackItem = (TrackItem) recentplayed.get(i); |
|---|
| 189 |
trackItem.setActive(new Boolean(select)); |
|---|
| 190 |
} |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
public static void main(String[] args) { |
|---|
| 194 |
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
|---|
| 195 |
public void run() { |
|---|
| 196 |
LastPod.Load(); |
|---|
| 197 |
} |
|---|
| 198 |
}); |
|---|
| 199 |
} |
|---|
| 200 |
} |
|---|