Changeset 71

Show
Ignore:
Timestamp:
05/26/07 02:37:45 (1 year ago)
Author:
chris
Message:

r7762@flan: chris | 2007-05-26 00:58:59 -0700
major refactor of the code to support mock tracks and submissions

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/org/lastpod/LastPod.java

    r70 r71  
    1919package org.lastpod; 
    2020 
    21 import org.lastpod.util.MiscUtilities; 
    22  
    23 import java.io.IOException; 
    24  
    2521import java.util.ArrayList; 
    2622import java.util.List; 
    2723import java.util.logging.Level; 
    2824import java.util.logging.Logger; 
    29 import java.util.prefs.Preferences; 
    30  
    31 import javax.swing.JOptionPane; 
    3225 
    3326/** 
     
    3831 */ 
    3932public class LastPod { 
    40     private 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 = 
     33    public final static String NO_PREFS_ERROR = 
    4534        "You have not setup your preferences.\n" 
    4635        + "Please click Preferences below to configure the location of " 
    4736        + "your iTunesDB (it's on your iPod's drive) and your AudioScrobbler " 
    4837        + "username and password."; 
     38    private UI userInterface; 
     39    private List recentplayed; //parsed using DbReader class 
    4940 
    5041    /** 
     
    5243     * GUI to display it. 
    5344     */ 
    54     private static void load() { 
     45    private void load() { 
    5546        recentplayed = new ArrayList(); 
    56         UI = new UI(); 
    57         UI.buildUI(); 
    5847 
    59         logger = Logger.getLogger(LastPod.class.getPackage().getName()); 
     48        Model model = new ModelImpl(); 
     49        model.setRecentlyPlayed(recentplayed); 
     50        userInterface = new UI(model); 
     51        userInterface.buildUI(); 
     52 
     53        Logger logger = Logger.getLogger(LastPod.class.getPackage().getName()); 
    6054        logger.setLevel(Level.ALL); 
    61         logger.addHandler(new LogHandler(UI)); 
     55        logger.addHandler(new LogHandler(userInterface)); 
    6256 
    63         parsePlayCounts(); 
     57        model.parsePlayCounts(userInterface); 
    6458 
    65         UI.makeVisable(); 
    66     } 
    67  
    68     /** 
    69      * Parses the play counts and track information from the iPod. 
    70      */ 
    71     public static void parsePlayCounts() { 
    72         Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); 
    73         String iTunesPath = fPrefs.get("iTunes Path", "default"); 
    74         String parseVariousArtistsStr = fPrefs.get("parseVariousArtists", "1"); 
    75         boolean parseVariousArtists = parseVariousArtistsStr.equals("1") ? true : false; 
    76  
    77         if (iTunesPath.equals("default")) { 
    78             logger.log(Level.INFO, NO_PREFS_ERROR); 
    79  
    80             return; 
    81         } 
    82  
    83         DbReader reader = new DbReader(iTunesPath, parseVariousArtists); 
    84  
    85         try { 
    86             reader.parse(); 
    87             recentplayed = reader.getRecentplays(); 
    88             UI.newTrackListAvailable(); 
    89         } catch (IOException e) { 
    90             StackTraceElement[] trace = e.getStackTrace(); 
    91  
    92             for (int i = 0; i < trace.length; i++) { 
    93                 if (trace[i].getClassName().startsWith("org.lastpod")) { 
    94                     logger.log(Level.SEVERE, trace[i].toString()); 
    95                 } 
    96             } 
    97  
    98             logger.log(Level.SEVERE, e.toString()); 
    99         } 
    100     } 
    101  
    102     /** 
    103      * Submits the tracks to Last.fm 
    104      * @return  A status message upon completion. 
    105      */ 
    106     public static Object submitTracks() { 
    107         Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); 
    108         String username = fPrefs.get("Username", "default"); 
    109         String password = fPrefs.get("Password", "default"); 
    110         String encryptedPassword = fPrefs.get("encryptedPassword", "default"); 
    111  
    112         if (!password.equals("default")) { 
    113             encryptedPassword = MiscUtilities.md5DigestPassword(password); 
    114             fPrefs.put("encryptedPassword", encryptedPassword); 
    115             fPrefs.remove("Password"); 
    116  
    117             String message = 
    118                 "Your password was stored unencrypted on your system." 
    119                 + " This version of LastPod has encrypted this password for future usage."; 
    120             JOptionPane.showMessageDialog(UI.getFrame(), message); 
    121  
    122             logger = Logger.getLogger(LastPod.class.getPackage().getName()); 
    123             logger.log(Level.WARNING, message); 
    124         } 
    125  
    126         String encryptedDefault = MiscUtilities.md5DigestPassword("default"); 
    127  
    128         if (username.equals("default") && encryptedPassword.equals(encryptedDefault)) { 
    129             logger.log(Level.INFO, NO_PREFS_ERROR); 
    130  
    131             return NO_PREFS_ERROR; 
    132         } 
    133  
    134         String backupUrl = fPrefs.get("backupUrl", ""); 
    135  
    136         try { 
    137             scrobbler = new Scrobbler(username, encryptedPassword, backupUrl); 
    138  
    139             List activeRecentPlayed = onlyActiveTrackItems(recentplayed); 
    140             List inactiveRecentPlayed = onlyInactiveTrackItems(recentplayed); 
    141  
    142             scrobbler.setChunkProgress(UI); 
    143             scrobbler.setTracksToSubmit(activeRecentPlayed); 
    144             scrobbler.handshake(); 
    145             scrobbler.submitTracks(); 
    146             scrobbler.addHistories(activeRecentPlayed, inactiveRecentPlayed); 
    147  
    148             /* Refresh track list. */ 
    149             UI.newTrackListAvailable(); 
    150         } catch (Exception e) { 
    151             StackTraceElement[] trace = e.getStackTrace(); 
    152  
    153             for (int i = 0; i < trace.length; i++) { 
    154                 if (trace[i].getClassName().startsWith("org.lastpod")) { 
    155                     logger.log(Level.SEVERE, trace[i].toString()); 
    156                 } 
    157             } 
    158  
    159             logger.log(Level.SEVERE, e.toString()); 
    160             JOptionPane.showMessageDialog(null, e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE); 
    161         } 
    162  
    163         return "Success"; 
    164     } 
    165  
    166     private static List onlyActiveTrackItems(List recentPlayed) { 
    167         return filterTrackItems(recentPlayed, true); 
    168     } 
    169  
    170     private static List onlyInactiveTrackItems(List recentPlayed) { 
    171         return filterTrackItems(recentPlayed, false); 
    172     } 
    173  
    174     private static List filterTrackItems(List recentPlayed, boolean filterActive) { 
    175         List filteredRecentPlayed = new ArrayList(); 
    176  
    177         for (int i = 0; i < recentPlayed.size(); i++) { 
    178             TrackItem trackItem = (TrackItem) recentPlayed.get(i); 
    179  
    180             boolean trackActive = trackItem.isActive().booleanValue(); 
    181  
    182             if (trackActive && filterActive) { 
    183                 filteredRecentPlayed.add(trackItem); 
    184             } else if (!trackActive && !filterActive) { 
    185                 filteredRecentPlayed.add(trackItem); 
    186             } 
    187         } 
    188  
    189         return filteredRecentPlayed; 
    190     } 
    191  
    192     public static void selectAll() { 
    193         setupSelections(true); 
    194     } 
    195  
    196     public static void unselectAll() { 
    197         setupSelections(false); 
    198     } 
    199  
    200     private static void setupSelections(boolean select) { 
    201         for (int i = 0; i < recentplayed.size(); i++) { 
    202             TrackItem trackItem = (TrackItem) recentplayed.get(i); 
    203             trackItem.setActive(new Boolean(select)); 
    204         } 
     59        userInterface.makeVisable(); 
    20560    } 
    20661 
     
    20863        javax.swing.SwingUtilities.invokeLater(new Runnable() { 
    20964                public void run() { 
    210                     load(); 
     65                    LastPod lastPod = new LastPod(); 
     66                    lastPod.load(); 
    21167                } 
    21268            }); 
  • trunk/src/main/org/lastpod/PreferencesEditor.java

    r70 r71  
    3232import java.io.File; 
    3333 
    34 import java.util.ArrayList; 
    3534import java.util.logging.Level; 
    3635import java.util.logging.Logger; 
     
    6463     */ 
    6564    private UI userInterface = null; 
     65 
     66    /** 
     67     * The application's model. 
     68     */ 
     69    private Model model = null; 
    6670    private JDialog frame; 
    6771    private SpringLayout layout; 
     
    7983     * Constructs this object. 
    8084     * @param userInterface  The application's user interface. 
     85     * @param model  The application's model. 
    8186     */ 
    82     public PreferencesEditor(UI userInterface) { 
     87    public PreferencesEditor(UI userInterface, Model model) { 
    8388        this.userInterface = userInterface; 
     89        this.model = model; 
    8490    } 
    8591 
     
    403409         */ 
    404410        if (!oldItunesPath.equals(newItunesPath)) { 
    405             LastPod.recentplayed = new ArrayList(); 
    406             LastPod.parsePlayCounts(); 
    407             userInterface.newTrackListAvailable(); 
     411            model.clearRecentlyPlayed(); 
     412            model.parsePlayCounts(userInterface); 
     413            userInterface.newTrackListAvailable(model.getRecentlyPlayed()); 
    408414        } 
    409415    } 
  • trunk/src/main/org/lastpod/RecentPanel.java

    r52 r71  
    2424 
    2525import java.util.Date; 
     26import java.util.List; 
    2627 
    2728import javax.swing.JPanel; 
     
    3132 
    3233/** 
     34 * @author Chris Tilden 
    3335 * @author muti 
    3436 * @version $Id$ 
    3537 */ 
    3638public class RecentPanel extends JPanel { 
     39    /** 
     40     * Required for serializable classes. 
     41     */ 
     42    public static final long serialVersionUID = 200705252321L; 
    3743    private JTable table; 
    3844    private RecentModel model; 
    3945 
     46    /** 
     47     * Constructs the panel for recently played tracks. 
     48     */ 
    4049    public RecentPanel() { 
    4150        super(new GridLayout(1, 1)); 
     
    5564    } 
    5665 
    57     public void newTrackListAvailable() { 
    58         this.model.fireTableDataChanged(); 
     66    public void newTrackListAvailable(List recentlyPlayed) { 
     67        model.setRecentlyPlayed(recentlyPlayed); 
     68        model.fireTableDataChanged(); 
    5969    } 
    6070 
    6171    private class RecentModel extends AbstractTableModel { 
     72        /** 
     73         * Required for serializable classes. 
     74         */ 
     75        public static final long serialVersionUID = 200705252320L; 
     76 
     77        /** 
     78         * A list of recently played tracks. 
     79         */ 
     80        private List recentlyPlayed = null; 
    6281        private String[] columnData = 
    6382            new String[] { "#", "Submit", "Artist", "Album", "Track", "Length", "Play Time" }; 
     83 
     84        /** 
     85         * Sets the list of recenlty played tracks. 
     86         * @param recentlyPlayed  The list of recently played tracks. 
     87         */ 
     88        public void setRecentlyPlayed(List recentlyPlayed) { 
     89            this.recentlyPlayed = recentlyPlayed; 
     90        } 
    6491 
    6592        public int getColumnCount() { 
     
    6895 
    6996        public int getRowCount() { 
    70             if (LastPod.recentplayed != null) { 
    71                 return LastPod.recentplayed.size(); 
     97            if (recentlyPlayed != null) { 
     98                return recentlyPlayed.size(); 
    7299            } 
    73100 
     
    87114            TrackItem track; 
    88115 
    89             if (LastPod.recentplayed != null) { 
    90                 track = (TrackItem) LastPod.recentplayed.get(row); 
     116            if (recentlyPlayed != null) { 
     117                track = (TrackItem) recentlyPlayed.get(row); 
    91118            } else { 
    92119                return new Object(); 
     
    131158                } 
    132159 
    133                 if (LastPod.recentplayed == null) { 
     160                if (recentlyPlayed == null) { 
    134161                    throw new RuntimeException("Recent Played list is NULL!"); 
    135162                } 
    136163 
    137                 track = (TrackItem) LastPod.recentplayed.get(row); 
     164                track = (TrackItem) recentlyPlayed.get(row); 
    138165                track.setActive(((Boolean) value)); 
    139166                fireTableCellUpdated(row, col); 
  • trunk/src/main/org/lastpod/UI.java

    r70 r71  
    3636import java.awt.event.WindowEvent; 
    3737 
     38import java.util.List; 
     39 
    3840import javax.swing.Action; 
    3941import javax.swing.ImageIcon; 
     
    101103     * Constructs the user interface and some icon elements. 
    102104     */ 
    103     public UI() { 
     105    public UI(Model model) { 
    104106        frame = new JFrame("LastPod"); 
    105107 
     
    117119        ImageIcon iconExit = SwingUtils.createImageIcon(UI.class, "images/application-exit.png"); 
    118120 
    119         actionOpenPreferences = new OpenPreferencesEditor(this, "Preferences", iconOpenPreferences
    120                 "Opens Preferences Editor", KeyEvent.VK_P); 
    121         actionUnselectAll = new UnselectAll(frame, "Unselect All", iconUnselectAll, 
     121        actionOpenPreferences = new OpenPreferencesEditor(this, model, "Preferences"
     122                iconOpenPreferences, "Opens Preferences Editor", KeyEvent.VK_P); 
     123        actionUnselectAll = new UnselectAll(frame, model, "Unselect All", iconUnselectAll, 
    122124                "Unselects All Tracks", KeyEvent.VK_A); 
    123         actionSubmitTracks = new SubmitTracks(statusAnimationLabel, "Submit Tracks", 
     125        actionSubmitTracks = new SubmitTracks(this, model, statusAnimationLabel, "Submit Tracks", 
    124126                iconSubmitTracks, "Submits the selected tracks to Last.fm", KeyEvent.VK_S); 
    125         actionDeletePlayCounts = new DeletePlayCounts(this, "Delete Play Counts", 
     127        actionDeletePlayCounts = new DeletePlayCounts(this, model, "Delete Play Counts", 
    126128                iconDeletePlayCounts, "Removes the play counts file from the iPod.", KeyEvent.VK_D); 
    127129        actionExit = new ExitApplication(frame, "Exit", iconExit, 
     
    272274    } 
    273275 
    274     public void newTrackListAvailable() { 
    275         this.recentpanel.newTrackListAvailable(); 
     276    public void newTrackListAvailable(List recentlyPlayed) { 
     277        recentpanel.newTrackListAvailable(recentlyPlayed); 
    276278    } 
    277279 
  • trunk/src/main/org/lastpod/action/DeletePlayCounts.java

    r70 r71  
    1919package org.lastpod.action; 
    2020 
    21 import org.lastpod.LastPod
     21import org.lastpod.Model
    2222import org.lastpod.UI; 
    2323 
     
    2626import java.io.File; 
    2727 
    28 import java.util.ArrayList; 
    2928import java.util.prefs.Preferences; 
    3029 
     
    5049 
    5150    /** 
     51     * The application's model. 
     52     */ 
     53    private Model model = null; 
     54 
     55    /** 
    5256     * Constructs this action. 
    5357     * @param userInterface  The application's user interface. 
     58     * @param model  The application's model. 
    5459     * @param text  The action's text. 
    5560     * @param icon  The action's icon. 
     
    5762     * @param mnemonic  The action's mnemonic. 
    5863     */ 
    59     public DeletePlayCounts(UI userInterface, String text, ImageIcon icon, String desc, int mnemonic) { 
     64    public DeletePlayCounts(UI userInterface, Model model, String text, ImageIcon icon, 
     65        String desc, int mnemonic) { 
    6066        super(text, icon); 
    6167        this.userInterface = userInterface; 
     68        this.model = model; 
    6269        putValue(SHORT_DESCRIPTION, desc); 
    6370        putValue(MNEMONIC_KEY, new Integer(mnemonic)); 
     
    101108        if (succuss) { 
    102109            /* Clear recent track list. */ 
    103             LastPod.recentplayed = new ArrayList(); 
     110            model.clearRecentlyPlayed(); 
     111 
    104112            /* Refresh track list. */ 
    105             userInterface.newTrackListAvailable(); 
     113            userInterface.newTrackListAvailable(model.getRecentlyPlayed()); 
    106114        } else { 
    107115            String message = "The play counts file was not deleted."; 
  • trunk/src/main/org/lastpod/action/OpenPreferencesEditor.java

    r70 r71  
    1919package org.lastpod.action; 
    2020 
     21import org.lastpod.Model; 
    2122import org.lastpod.PreferencesEditor; 
    2223import org.lastpod.UI; 
     
    4445 
    4546    /** 
     47     * The application's model. 
     48     */ 
     49    private Model model = null; 
     50 
     51    /** 
    4652     * Constructs this action. 
    4753     * @param userInterface  The application's user interface. 
     54     * @param model  The application's model. 
    4855     * @param text  The action's text. 
    4956     * @param icon  The action's icon. 
     
    5158     * @param mnemonic  The action's mnemonic. 
    5259     */ 
    53     public OpenPreferencesEditor(UI userInterface, String text, ImageIcon icon, String desc
    54         int mnemonic) { 
     60    public OpenPreferencesEditor(UI userInterface, Model model, String text, ImageIcon icon
     61        String desc, int mnemonic) { 
    5562        super(text, icon); 
    5663        putValue(SHORT_DESCRIPTION, desc); 
    5764        putValue(MNEMONIC_KEY, new Integer(mnemonic)); 
    5865        this.userInterface = userInterface; 
     66        this.model = model; 
    5967    } 
    6068 
     
    6472     */ 
    6573    public void actionPerformed(ActionEvent e) { 
    66         PreferencesEditor prefeditor = new PreferencesEditor(userInterface); 
     74        PreferencesEditor prefeditor = new PreferencesEditor(userInterface, model); 
    6775        prefeditor.buildUI(); 
    6876    } 
  • trunk/src/main/org/lastpod/action/SubmitTracks.java

    r65 r71  
    1919package org.lastpod.action; 
    2020 
    21 import org.lastpod.LastPod
     21import org.lastpod.Model
    2222import org.lastpod.UI; 
    2323 
     
    4444     */ 
    4545    public static final long serialVersionUID = 200705180016L; 
     46 
     47    /** 
     48     * The application's user interface. 
     49     */ 
     50    private UI userInterface = null; 
     51 
     52    /** 
     53     * The application's model. 
     54     */ 
     55    private Model model = null; 
    4656 
    4757    /** 
     
    7888    /** 
    7989     * Constructs this action. 
     90     * @param userInterface  The application's user interface. 
     91     * @param model  The application's model. 
    8092     * @param text  The action's text. 
    8193     * @param icon  The action's icon. 
     
    8395     * @param mnemonic  The action's mnemonic. 
    8496     */ 
    85     public SubmitTracks(JLabel statusAnimationLabel, String text, ImageIcon icon, String desc
    86         int mnemonic) { 
     97    public SubmitTracks(UI userInterface, Model model, JLabel statusAnimationLabel, String text
     98        ImageIcon icon, String desc, int mnemonic) { 
    8799        super(text, icon); 
    88100        putValue(SHORT_DESCRIPTION, desc); 
    89101        putValue(MNEMONIC_KEY, new Integer(mnemonic)); 
     102        this.userInterface = userInterface; 
     103        this.model = model; 
    90104        this.statusAnimationLabel = statusAnimationLabel; 
    91105 
     
    132146                        busyIconTimer.start(); 
    133147 
    134                         return LastPod.submitTracks(); 
     148                        return model.submitTracks(userInterface); 
    135149                    } 
    136150 
  • trunk/src/main/org/lastpod/action/UnselectAll.java

    r65 r71  
    1919package org.lastpod.action; 
    2020 
    21 import org.lastpod.LastPod
     21import org.lastpod.Model
    2222 
    2323import java.awt.event.ActionEvent; 
     
    4343 
    4444    /** 
     45     * The application's model. 
     46     */ 
     47    private Model model = null; 
     48 
     49    /** 
    4550     * If <code>true</code> this action selects all tracks.  If 
    4651     * <code>false</code> this action unselects all tracks. 
     
    5156     * Constructs this action. 
    5257     * @param mainAppFrame  The main frame for this application. 
     58     * @param model  The application's model. 
    5359     * @param text  The action's text. 
    5460     * @param icon  The action's icon. 
     
    5662     * @param mnemonic  The action's mnemonic. 
    5763     */ 
    58     public UnselectAll(JFrame mainAppFrame, String text, ImageIcon icon, String desc, int mnemonic) { 
     64    public UnselectAll(JFrame mainAppFrame, Model model, String text, ImageIcon icon, String desc, 
     65        int mnemonic) { 
    5966        super(text, icon); 
    6067        putValue(SHORT_DESCRIPTION, desc); 
    6168        putValue(MNEMONIC_KEY, new Integer(mnemonic)); 
    6269        this.mainAppFrame = mainAppFrame; 
     70        this.model = model; 
    6371    } 
    6472 
     
    8593    public void actionPerformed(ActionEvent e) { 
    8694        if (selectAllType.booleanValue()) { 
    87             LastPod.selectAll(); 
     95            model.selectAll(); 
    8896            setSelectAllType(Boolean.FALSE); 
    8997            putValue(NAME, "Unselect All"); 
    9098            putValue(SHORT_DESCRIPTION, "Unselects All Tracks"); 
    9199        } else { 
    92             LastPod.unselectAll(); 
     100            model.unselectAll(); 
    93101            setSelectAllType(Boolean.TRUE); 
    94102            putValue(NAME, "Select All");