1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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]);
}
}
}
|