summaryrefslogtreecommitdiffstats
path: root/source/client/swing/ZPlayer.java
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2009-05-02 01:37:43 -0400
committerJason A. Donenfeld <Jason@zx2c4.com>2009-05-02 01:37:43 -0400
commite90600f2885c322cce0dafa25b526c8fdf88a56f (patch)
tree2b59eb4a46f438fafa5b10493680d944d0c5ba63 /source/client/swing/ZPlayer.java
downloadZPlayer-master.tar.xz
ZPlayer-master.zip
Initial commit.HEADmaster
Diffstat (limited to 'source/client/swing/ZPlayer.java')
-rw-r--r--source/client/swing/ZPlayer.java506
1 files changed, 506 insertions, 0 deletions
diff --git a/source/client/swing/ZPlayer.java b/source/client/swing/ZPlayer.java
new file mode 100644
index 0000000..b265641
--- /dev/null
+++ b/source/client/swing/ZPlayer.java
@@ -0,0 +1,506 @@
+/*
+ * Created on May 7, 2003
+ *
+ * To change the template for this generated file go to
+ * Window>Preferences>Java>Code Generation>Code and Comments
+ *
+Copyright 2003 Joseph Barnett
+
+This File is part of "ZPlayer"
+
+"ZPlayer" is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+Free Software Foundation; either version 2 of the License, or
+your option) any later version.
+
+"ZPlayer" is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with "ZPlayer"; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+ */
+package itunes.client.swing;
+import itunes.client.*;
+import itunes.client.request.*;
+import com.strangeberry.rendezvous.*;
+import javax.swing.*;
+import javax.swing.event.*;
+import javax.swing.table.TableColumn;
+import java.awt.*;
+import java.awt.event.*;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collections;
+public class ZPlayer implements ServiceListener {
+ protected String host;
+ protected String connectedHost;
+ public JFrame frame;
+ protected JPanel serverLibrary;
+ protected JPanel pane;
+ protected JComboBox servicesCB;
+ protected JComboBox cb;
+ protected JButton browseLib;
+ protected JButton songDl;
+ protected ArrayList knownIPs;
+ protected int sessionId;
+ protected SongTableModel songModel;
+ protected JTable songTable;
+ protected JScrollPane scroller;
+ protected TableSorter sorter;
+ protected boolean hostConnected;
+ protected boolean librariesQueried;
+ protected boolean browsingLibrary;
+ protected static String iTunesService = "_daap._tcp.local.";
+ protected Rendezvous r;
+ private int fullRowPlaying;
+ protected ProgressMonitor progressMonitor;
+ protected Player p;
+ protected boolean playstop;
+ protected int playingRow;
+ protected int playdb;
+ protected String playhost;
+ protected JTextField searchQuery;
+
+ public void stopPlaying() {
+ if (p != null)
+ p.stopMusic();
+ p = null;
+ frame.setTitle("ZPlayer");
+ if (songDl != null) {
+ songDl.setText("Play Selected");
+ songDl.validate();
+ }
+ playhost = "";
+ playstop = !playstop;
+ }
+
+ public void playNext(int dir) {
+ if (p != null) {
+ p.stopMusic();
+ p = null;
+ }
+ if (playingRow >= 0 && playingRow < songTable.getRowCount()-1 && ((MusicProvider)servicesCB.getSelectedItem()).address.equals(playhost)){
+ playingRow+=dir;
+ fullRowPlaying = sorter.getFullIndexRow(playingRow);
+ SongRequest sr=null;
+ try {
+ sr = new SongRequest(host, Request.ITUNES_PORT,playdb,Integer.parseInt((String)sorter.getValueAt(playingRow,5)),(String)sorter.getValueAt(playingRow,6),sessionId);
+ } catch (NoServerPermissionException e) {
+ e.printStackTrace();
+ }
+ frame.setTitle("ZPlayer: Now Playing \""+(String)sorter.getValueAt(playingRow,2)+"\" by "+(String)sorter.getValueAt(playingRow,0));
+ p = new Player(this,sr.getStream());
+ p.start();
+ songDl.setText("Stop playing");
+ songDl.validate();
+ } else {
+ stopPlaying();
+ }
+ }
+
+ protected void playSong() {
+ final int selection = songTable.getSelectedRow();
+ final int fdb = playdb;
+ if (playstop) {
+ SongRequest sr=null;
+ try {
+ sr = new SongRequest(host, Request.ITUNES_PORT,fdb,Integer.parseInt((String)sorter.getValueAt(selection,5)),(String)sorter.getValueAt(selection,6),sessionId);
+ } catch (NoServerPermissionException e) {
+ e.printStackTrace();
+ JOptionPane.showMessageDialog(frame,"Error streaming song!");
+ stopPlaying();
+ return;
+ }
+ p = new Player(this,sr.getStream());
+ p.start();
+ System.out.println("Selected: "+selection+"\n"+(String)sorter.getValueAt(selection,5)+"\n"+(String)sorter.getValueAt(selection,2));
+ playhost = ((MusicProvider)servicesCB.getSelectedItem()).address;
+ frame.setTitle("ZPlayer: Now Playing \""+(String)sorter.getValueAt(selection,2)+"\" by "+(String)sorter.getValueAt(selection,0));
+ songDl.setText("Stop playing");
+ songDl.validate();
+ playingRow = selection;
+ fullRowPlaying = sorter.getFullIndexRow(playingRow);
+ playstop = !playstop;
+ } else {
+ stopPlaying();
+ playingRow = -1;
+ fullRowPlaying = sorter.getFullIndexRow(playingRow);
+ }
+ }
+
+ public void resolveService(Rendezvous r, String type, String name, ServiceInfo info) {
+ if (info == null) {
+ System.out.println("Service not found");
+ } else {
+ if (name.endsWith("." + type)) {
+ name = name.substring(0, name.length() - (type.length() + 1));
+ }
+ if (!knownIPs.contains(info.getAddress())) {
+ ServerInfoRequest rq=null;
+ try {
+ rq = new ServerInfoRequest(info.getAddress(), Request.ITUNES_PORT);
+ } catch (NoServerPermissionException e) {
+ return;
+ }
+ // if (rq.getServerVersion() >=2) {
+ // System.out.println("Ignoring "+name+ " at address "+ info.getAddress() + ": Unsupported iTunes Version 4.0.1 ("+rq.getServerVersion()+")");
+ // return;
+ // }
+ System.out.println("Discovered "+name+" at address "+info.getAddress() + ": (Server Version "+rq.getServerVersion()+")");
+ knownIPs.add(info.getAddress());
+ MusicProvider m = new MusicProvider(info.getAddress(),name);
+ servicesCB.addItem(m);
+ servicesCB.validate();
+ } else {
+ System.out.println("Ignoring new service " + name + " from already known IP "+info.getAddress());
+ }
+ }
+ }
+
+ public void addService(Rendezvous r, String type, String name) {
+ if (name.endsWith("." + type)) {
+ name = name.substring(0, name.length() - (type.length() + 1));
+ }
+ r.requestServiceInfo(iTunesService,name);
+ }
+
+ public void removeService(Rendezvous r, String type, String name) {
+ if (name.endsWith("." + type)) {
+ name = name.substring(0, name.length() - (type.length() + 1));
+ }
+ knownIPs.remove(r.getServiceInfo(type,name).getAddress());
+ System.out.println(name + " went offline");
+ servicesCB.removeItem(new MusicProvider("",name));
+ servicesCB.validate();
+ }
+ private class MyDocumentListner implements DocumentListener {
+ public void insertUpdate(DocumentEvent e){
+ changeFilter();
+ }
+ public void removeUpdate(DocumentEvent e) {
+ changeFilter();
+ }
+ public void changedUpdate(DocumentEvent e) {
+ }
+ }
+ private void changeFilter()
+ {
+ sorter.reFilter(searchQuery);
+ playingRow = sorter.getFilteredIndexRow(fullRowPlaying);
+ }
+ private JPanel buildSearch()
+ {
+ JPanel row1 = new JPanel();
+
+ searchQuery = new JTextField(20);
+ searchQuery.getDocument().addDocumentListener(new MyDocumentListner());
+ JButton clearSearch = new JButton("Clear Search");
+
+ clearSearch.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e){
+ searchQuery.setText("");
+ }
+ });
+
+ JLabel searchLabel = new JLabel("Search:");
+
+ row1.add(searchLabel);
+ row1.add(searchQuery);
+ row1.add(clearSearch);
+ row1.add(Box.createRigidArea(new Dimension(5,0)));
+ JButton select = new JButton("Select Playing");
+ select.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if(playingRow == -1 && fullRowPlaying != -1)
+ {
+ searchQuery.setText("");
+ }
+ if(playingRow>-1)
+ {
+ songTable.setRowSelectionInterval(playingRow,playingRow);
+ songTable.scrollRectToVisible(songTable.getCellRect(playingRow, 0, true));
+ }
+ }
+ });
+ row1.add(select);
+ return row1;
+ }
+ protected void createSongChooser() throws NoServerPermissionException {
+ SingleDatabaseRequest sr = new SingleDatabaseRequest(host,Request.ITUNES_PORT,sessionId, playdb);
+ ArrayList songs = sr.getSongs();
+ Collections.sort(songs);
+ if (songModel == null) {
+ songModel = new SongTableModel();
+ } else {
+ songModel.clear();
+ }
+ for (int i = 0; i < songs.size();i++) {
+ Song s = (Song)songs.get(i);
+ songModel.AddRow(s);
+ }
+ if (songTable == null) {
+ pane.remove(pane.getComponentCount()-1);
+ sorter = new TableSorter(songModel);
+ songTable = new JTable(sorter);
+ songTable.addMouseListener(new MouseAdapter() {
+ public void mouseClicked(MouseEvent e) {
+ if (e.getClickCount() == 2) {
+ stopPlaying();
+ playstop = true;
+ playSong();
+ }
+ }
+ });
+ sorter.addMouseListenerToHeaderInTable(songTable);
+ songTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+ songTable.setPreferredScrollableViewportSize(new
+ Dimension(1500, 1600));
+ for (int i = 0; i < 5; i++) {
+ TableColumn column = songTable.getColumnModel().getColumn(i);
+ if (i == 2) {
+ column.setPreferredWidth(300);
+ } else if (i==0||i==1){
+ column.setPreferredWidth(150);
+ } else {
+ column.setPreferredWidth(10);
+ }
+ }
+ scroller = new JScrollPane(songTable);
+ pane.add(buildSearch());
+ pane.add(scroller);
+
+ } else {
+ songTable.validate();
+ songTable.repaint();
+ }
+ if (songDl == null) {
+ JPanel buttonRow = new JPanel();
+ buttonRow.setAlignmentX(Component.CENTER_ALIGNMENT);
+ JButton back = new JButton("Back Track");
+ back.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ playNext(-1);
+ }
+ });
+ buttonRow.add(back);
+ buttonRow.add(Box.createRigidArea(new Dimension(5,0)));
+ songDl = new JButton("Play Selected");
+ back.setAlignmentX(Component.CENTER_ALIGNMENT);
+ songDl.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ playSong();
+ }
+ });
+ buttonRow.add(songDl);
+ buttonRow.add(Box.createRigidArea(new Dimension(5,0)));
+ JButton next = new JButton("Forward Track");
+ next.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ playNext(1);
+ }
+ });
+ buttonRow.add(next);
+ pane.add(buttonRow);
+ }
+ songModel.fireTableDataChanged();
+ pane.validate();
+ scroller.validate();
+ frame.validate();
+ }
+
+ protected void connectToHost() throws NoServerPermissionException{
+ if (sessionId != -1) {
+ try {
+ LogoutRequest lr = new LogoutRequest(connectedHost,Request.ITUNES_PORT, sessionId);
+ } catch (NoServerPermissionException e) {}
+ sessionId = -1;
+ }
+ LoginRequest l=null;
+ l = new LoginRequest(host,Request.ITUNES_PORT);
+ connectedHost = host;
+ sessionId = l.getSessionId();
+ if (sessionId == -1) {
+ JOptionPane.showMessageDialog(frame, "Error connecting to "+host);
+ return;
+ }
+ System.out.println("logged in: session "+ sessionId);
+ JPanel col1 = new JPanel();
+ col1.setLayout(new BoxLayout(col1,BoxLayout.Y_AXIS));
+ DatabasesRequest db = new DatabasesRequest(host, Request.ITUNES_PORT, sessionId);
+ int dbCount = db.getLibraryCount();
+ ArrayList dbs = db.getDbs();
+ if (cb == null) {
+ cb = new JComboBox();
+ cb.setAlignmentX(Component.CENTER_ALIGNMENT);
+ col1.add(cb);
+ } else {
+ cb.removeAllItems();
+ }
+ for (int i = 0; i < dbCount; i++) {
+ cb.addItem(dbs.get(i));
+ }
+ if (browseLib == null) {
+ browseLib = new JButton("Browse Selected Library");
+ browseLib.setAlignmentX(Component.CENTER_ALIGNMENT);
+ browseLib.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ try {
+ playdb = ((Database)cb.getSelectedItem()).id;
+ createSongChooser();
+ } catch (NoServerPermissionException ex) {
+ }
+ }
+ });
+ col1.add(browseLib);
+ }
+ if(col1.getComponentCount() > 0)
+ {
+ serverLibrary.add(Box.createRigidArea(new Dimension(5,0)));
+ serverLibrary.add(col1);
+ serverLibrary.validate();
+ }
+ }
+
+ public Component createComponents() {
+ JButton button = new JButton("Browse Host");
+ button.setAlignmentX(Component.CENTER_ALIGNMENT);
+ button.setMnemonic(KeyEvent.VK_I);
+ button.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ try {
+ MusicProvider m = ((MusicProvider)servicesCB.getSelectedItem());
+ if (m.address.equals("")) {
+ host = JOptionPane.showInputDialog("Enter hostname: ","");
+ } else {
+ host = m.address;
+ }
+
+ try {
+ connectToHost();
+ } catch (NoServerPermissionException e1) {
+ JOptionPane.showMessageDialog(frame,"Need Authorization! (not yet implemented)");
+ return;
+ }
+ playdb = ((Database)cb.getItemAt(0)).id;
+ createSongChooser();
+ } catch (NoServerPermissionException ex) {
+ }
+ }
+ });
+ serverLibrary = new JPanel();
+ serverLibrary.setLayout(new BoxLayout(serverLibrary,BoxLayout.X_AXIS));
+ pane = new JPanel();
+ pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
+ pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
+ MusicProvider m = new MusicProvider("","Other...");
+ servicesCB.addItem(m);
+ JPanel col1 = new JPanel();
+ col1.setLayout(new BoxLayout(col1,BoxLayout.Y_AXIS));
+ col1.add(servicesCB);
+ col1.add(button);
+ serverLibrary.add(col1);
+ pane.add(serverLibrary);
+ pane.add(Box.createRigidArea(new Dimension(0,20)));
+ pane.add(Box.createRigidArea(new Dimension(0,400)));
+ return pane;
+ }
+ public void logOut()
+ {
+ if (sessionId != -1) {
+ LogoutRequest lr;
+ try {
+ lr = new LogoutRequest(connectedHost, Request.ITUNES_PORT, sessionId);
+ System.out.println("disconnected from session: " + sessionId);
+ } catch (NoServerPermissionException ex) { }
+ sessionId = -1;
+ }
+ stopPlaying();
+ }
+ public void connectToHost(String initialHost)
+ {
+ if(initialHost != null && !initialHost.equals(""))
+ try
+ {
+ host = initialHost;
+ connectToHost();
+ playdb = ((Database)cb.getItemAt(0)).id;
+ createSongChooser();
+ }
+ catch (NoServerPermissionException ex) {}
+ }
+ public ZPlayer(boolean applet) {
+ playhost = "";
+ playingRow = -1;
+ fullRowPlaying = -1;
+ playstop = true;
+ p = null;
+ servicesCB = null;
+ cb = null;
+ browseLib = null;
+ songDl = null;
+ sessionId = -1;
+ songModel = null;
+ sorter = null;
+ songTable = null;
+ scroller = null;
+ hostConnected = false;
+ librariesQueried = false;
+ browsingLibrary = false;
+ knownIPs = new ArrayList();
+ try {
+ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ servicesCB = new JComboBox();
+ servicesCB.setAlignmentX(Component.CENTER_ALIGNMENT);
+
+ try {
+ r = new Rendezvous();
+ r.addServiceListener(iTunesService,this);
+ } catch (Exception e) {
+ System.out.println("Died while establishing rendezvous:\n");
+ e.printStackTrace();
+ }
+ //Create the top-level container and add contents to it.
+ frame = new JFrame("ZPlayer");
+ Component contents = createComponents();
+ frame.getContentPane().add(contents, BorderLayout.CENTER);
+
+ if(!applet)
+ {
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ }
+ frame.pack();
+
+ frame.setLocation(100,100);
+ frame.setSize(800,400);
+ frame.addWindowListener(new WindowListener() {
+ public void windowClosed(WindowEvent e) {
+ logOut();
+ }
+ public void windowClosing(WindowEvent e) {
+ logOut();
+ }
+ public void windowDeiconified(WindowEvent e) {}
+ public void windowIconified(WindowEvent e) {}
+ public void windowActivated(WindowEvent e) {}
+ public void windowDeactivated(WindowEvent e) {}
+ public void windowOpened(WindowEvent e) {}
+ });
+ frame.setVisible(true);
+ }
+ public static void main(String[] args) {
+ ZPlayer app = new ZPlayer(false);
+ if(args.length > 0)
+ {
+ app.connectToHost(args[0]);
+ }
+ }
+}