31
36
import org.gudy.azureus2.core3.global.GlobalManager;
32
37
import org.gudy.azureus2.core3.global.GlobalManagerAdapter;
33
38
import org.gudy.azureus2.core3.global.GlobalManagerListener;
39
import org.gudy.azureus2.core3.internat.MessageText;
34
40
import org.gudy.azureus2.core3.torrent.TOTorrent;
35
41
import org.gudy.azureus2.core3.torrent.TOTorrentException;
42
import org.gudy.azureus2.core3.torrent.TOTorrentFile;
36
43
import org.gudy.azureus2.core3.torrentdownloader.TorrentDownloader;
37
44
import org.gudy.azureus2.core3.torrentdownloader.TorrentDownloaderCallBackInterface;
38
45
import org.gudy.azureus2.core3.util.*;
39
import org.gudy.azureus2.ui.swt.FileDownloadWindow;
40
import org.gudy.azureus2.ui.swt.TorrentUtil;
41
import org.gudy.azureus2.ui.swt.Utils;
46
import org.gudy.azureus2.ui.swt.*;
42
47
import org.gudy.azureus2.ui.swt.mainwindow.TorrentOpener;
44
49
import com.aelitis.azureus.core.AzureusCore;
45
50
import com.aelitis.azureus.core.AzureusCoreFactory;
46
import com.aelitis.azureus.core.messenger.config.PlatformConfigMessenger;
51
import com.aelitis.azureus.core.cnetwork.ContentNetwork;
47
52
import com.aelitis.azureus.core.torrent.PlatformTorrentUtils;
48
53
import com.aelitis.azureus.ui.UIFunctionsManager;
49
54
import com.aelitis.azureus.ui.selectedcontent.DownloadUrlInfo;
55
import com.aelitis.azureus.ui.selectedcontent.DownloadUrlInfoContentNetwork;
50
56
import com.aelitis.azureus.ui.swt.UIFunctionsSWT;
51
57
import com.aelitis.azureus.ui.swt.browser.listener.DownloadUrlInfoSWT;
58
import com.aelitis.azureus.ui.swt.imageloader.ImageLoader;
59
import com.aelitis.azureus.ui.swt.imageloader.ImageLoader.ImageDownloaderListener;
52
60
import com.aelitis.azureus.ui.swt.views.skin.TorrentListViewsUtils;
53
import com.aelitis.azureus.util.ConstantsV3;
54
import com.aelitis.azureus.util.PlayUtils;
61
import com.aelitis.azureus.util.*;
61
68
public class TorrentUIUtilsV3
70
private final static String MSG_ALREADY_EXISTS = "OpenTorrentWindow.mb.alreadyExists";
72
private final static String MSG_ALREADY_EXISTS_NAME = MSG_ALREADY_EXISTS
63
75
//catches http://www.vuze.com/download/CHJW43PLS277RC7U3S5XRS2PZ4UUG7RS.torrent
64
76
private static final Pattern hashPattern = Pattern.compile("download/([A-Z0-9]{32})\\.torrent");
68
final AzureusCore core,
69
DownloadUrlInfo dlInfo,
70
final boolean playNow, // open player
71
final boolean playPrepare, // as for open player but don't actually open it
72
final boolean bringToFront,
73
final boolean forceDRMtoCDP)
78
public static void loadTorrent(final AzureusCore core,
79
final DownloadUrlInfo dlInfo, final boolean playNow, // open player
80
final boolean playPrepare, // as for open player but don't actually open it
81
final boolean bringToFront, final boolean forceDRMtoCDP) {
75
82
if (dlInfo instanceof DownloadUrlInfoSWT) {
76
83
DownloadUrlInfoSWT dlInfoSWT = (DownloadUrlInfoSWT) dlInfo;
77
84
dlInfoSWT.invoke(playNow ? "play" : "download");
81
88
String url = dlInfo.getDownloadURL();
83
if (playNow || playPrepare ) {
84
Matcher m = hashPattern.matcher(url);
86
String hash = m.group(1);
87
GlobalManager gm = core.getGlobalManager();
88
final DownloadManager dm = gm.getDownloadManager(new HashWrapper(Base32.decode(hash)));
90
new AEThread2("playExisting", true) {
90
Matcher m = hashPattern.matcher(url);
92
String hash = m.group(1);
93
GlobalManager gm = core.getGlobalManager();
94
final DownloadManager dm = gm.getDownloadManager(new HashWrapper(
95
Base32.decode(hash)));
97
if (playNow || playPrepare) {
98
new AEThread2("playExisting", true) {
92
100
public void run() {
94
Debug.outNoStack("loadTorrent already exists.. playing", false);
102
Debug.outNoStack("loadTorrent already exists.. playing",
96
105
TorrentListViewsUtils.playOrStream(dm);
98
Debug.outNoStack("loadTorrent already exists.. preparing", false);
107
Debug.outNoStack("loadTorrent already exists.. preparing",
100
110
PlayUtils.prepareForPlay(dm);
116
Utils.openMessageBox(Utils.findAnyShell(), SWT.OK,
117
MSG_ALREADY_EXISTS, new String[] {
120
MessageText.getString(MSG_ALREADY_EXISTS_NAME),
111
127
// If it's going to our URLs, add some extra authenication
112
if (PlatformConfigMessenger.urlCanRPC(url)) {
113
url = ConstantsV3.appendURLSuffix(url);
128
if (UrlFilter.getInstance().urlCanRPC(url)) {
129
ContentNetwork cn = null;
130
if (dlInfo instanceof DownloadUrlInfoContentNetwork) {
131
cn = ((DownloadUrlInfoContentNetwork) dlInfo).getContentNetwork();
134
cn = ConstantsVuze.getDefaultContentNetwork();
136
url = cn.appendURLSuffix(url, false, true);
116
139
UIFunctionsSWT uiFunctions = (UIFunctionsSWT) UIFunctionsManager.getUIFunctions();
117
140
if (uiFunctions != null) {
118
141
if (!COConfigurationManager.getBooleanParameter("add_torrents_silently")) {
120
143
uiFunctions.bringToFront();
135
158
file.deleteOnExit();
137
160
// Do a quick check to see if it's a torrent
138
if (!TorrentUtil.isFileTorrent(file, Utils.findAnyShell(),
161
if (!TorrentUtil.isFileTorrent(file, null, file.getName())) {
162
Matcher m = hashPattern.matcher(inf.getURL());
164
String hash = m.group(1);
166
ContentNetwork cn = null;
167
if (dlInfo instanceof DownloadUrlInfoContentNetwork) {
168
cn = ((DownloadUrlInfoContentNetwork) dlInfo).getContentNetwork();
171
cn = ConstantsVuze.getDefaultContentNetwork();
173
TorrentListViewsUtils.viewDetails(cn, hash,
176
TorrentUtil.isFileTorrent(file, Utils.findAnyShell(),
165
if (forceDRMtoCDP && (PlatformTorrentUtils.isContentDRM(torrent)
166
|| PlatformTorrentUtils.isContentPurchased(torrent))) {
167
TorrentListViewsUtils.viewDetails(hw.toBase32String(),
206
&& (PlatformTorrentUtils.isContentDRM(torrent) || PlatformTorrentUtils.isContentPurchased(torrent))) {
207
TorrentListViewsUtils.viewDetailsFromDS(torrent,
172
212
GlobalManager gm = core.getGlobalManager();
174
if (playNow || playPrepare ) {
214
if (playNow || playPrepare) {
175
215
DownloadManager existingDM = gm.getDownloadManager(hw);
176
216
if (existingDM != null) {
178
218
TorrentListViewsUtils.playOrStream(existingDM);
180
220
PlayUtils.prepareForPlay(existingDM);
317
* Retrieves the thumbnail for the content, pulling it from the web if
321
* @param l When the thumbnail is available, this listener is triggered
322
* @return If the image is immediately available, the image will be returned
323
* as well as the trigger being fired. If the image isn't available
324
* null will be returned and the listener will trigger when avail
328
public static Image getContentImage(Object datasource, boolean big,
329
final ContentImageLoadedListener l) {
333
TOTorrent torrent = DataSourceUtils.getTorrent(datasource);
334
if (torrent == null) {
335
l.contentImageLoaded(null, true);
339
ImageLoader imageLoader = ImageLoader.getInstance();
341
String thumbnailUrl = PlatformTorrentUtils.getContentThumbnailUrl(torrent);
343
if (thumbnailUrl != null && imageLoader.imageExists(thumbnailUrl)) {
344
l.contentImageLoaded(imageLoader.getImage(thumbnailUrl), true);
350
hash = torrent.getHashWrapper().toBase32String();
351
} catch (TOTorrentException e) {
354
l.contentImageLoaded(null, true);
358
String id = "Thumbnail." + hash;
360
Image image = imageLoader.imageAdded(id) ? imageLoader.getImage(id) : null;
361
if (image != null && !image.isDisposed()) {
362
l.contentImageLoaded(image, true);
366
final byte[] imageBytes = PlatformTorrentUtils.getContentThumbnail(torrent);
367
if (imageBytes != null) {
368
image = (Image) Utils.execSWTThreadWithObject("thumbcreator",
369
new AERunnableObject() {
370
public Object runSupport() {
372
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
373
Image image = new Image(Display.getDefault(), bis);
379
if ((image == null || image.isDisposed()) && thumbnailUrl != null) {
380
image = imageLoader.getUrlImage(thumbnailUrl,
381
new ImageDownloaderListener() {
382
public void imageDownloaded(Image image, boolean returnedImmediately) {
383
l.contentImageLoaded(image, returnedImmediately);
388
if (image == null || image.isDisposed()) {
389
DownloadManager dm = DataSourceUtils.getDM(datasource);
391
* Try to get an image from the OS
396
if (torrent != null) {
397
TOTorrentFile[] files = torrent.getFiles();
398
if (files.length > 0) {
399
path = files[0].getRelativePath();
403
path = dm.getDownloadState().getPrimaryFile();
406
// Don't ever dispose of PathIcon, it's cached and may be used elsewhere
407
Image icon = ImageRepository.getPathIcon(path, big, torrent != null
408
&& !torrent.isSimpleTorrent());
410
image = new Image(Display.getDefault(), icon, SWT.IMAGE_COPY);
415
imageLoader.addImage(id, ImageLoader.noImage);
417
imageLoader.addImage(id, image);
420
imageLoader.addImage(id, image);
423
l.contentImageLoaded(image, true);
427
public static void releaseContentImage(Object datasource) {
428
TOTorrent torrent = DataSourceUtils.getTorrent(datasource);
429
if (torrent == null) {
433
ImageLoader imageLoader = ImageLoader.getInstance();
435
String thumbnailUrl = PlatformTorrentUtils.getContentThumbnailUrl(torrent);
437
if (thumbnailUrl != null) {
438
imageLoader.releaseImage(thumbnailUrl);
442
hash = torrent.getHashWrapper().toBase32String();
443
} catch (TOTorrentException e) {
449
String id = "Thumbnail." + hash;
450
imageLoader.releaseImage(id);
454
public static interface ContentImageLoadedListener
458
* @param wasReturned Image was also returned from getContentImage
462
public void contentImageLoaded(Image image, boolean wasReturned);