~smaioli/azureus/upstream

« back to all changes in this revision

Viewing changes to com/aelitis/azureus/ui/swt/utils/TorrentUIUtilsV3.java

  • Committer: Stefano Maioli
  • Date: 2009-06-14 03:14:00 UTC
  • Revision ID: maio@naoweb.it-20090614031400-694ny3y9d323n9gl
Vuze 4.2.0.2 tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
package com.aelitis.azureus.ui.swt.utils;
22
22
 
 
23
import java.io.ByteArrayInputStream;
23
24
import java.io.File;
 
25
import java.util.Map;
24
26
import java.util.regex.Matcher;
25
27
import java.util.regex.Pattern;
26
28
 
 
29
import org.eclipse.swt.SWT;
 
30
import org.eclipse.swt.graphics.Image;
 
31
import org.eclipse.swt.widgets.Display;
27
32
import org.eclipse.swt.widgets.Shell;
28
33
 
29
34
import org.gudy.azureus2.core3.config.COConfigurationManager;
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;
43
48
 
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.*;
55
62
 
56
63
/**
57
64
 * @author TuxPaper
60
67
 */
61
68
public class TorrentUIUtilsV3
62
69
{
 
70
        private final static String MSG_ALREADY_EXISTS = "OpenTorrentWindow.mb.alreadyExists";
 
71
 
 
72
        private final static String MSG_ALREADY_EXISTS_NAME = MSG_ALREADY_EXISTS
 
73
                        + ".default.name";
 
74
 
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");
65
77
 
66
 
        public static void 
67
 
        loadTorrent(
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)
74
 
        {
 
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");
80
87
 
81
88
                String url = dlInfo.getDownloadURL();
82
89
                try {
83
 
                        if (playNow || playPrepare ) {
84
 
                        Matcher m = hashPattern.matcher(url);
85
 
                        if (m.find()) {
86
 
                                String hash = m.group(1);
87
 
                                GlobalManager gm = core.getGlobalManager();
88
 
                                final DownloadManager dm = gm.getDownloadManager(new HashWrapper(Base32.decode(hash)));
89
 
                                if (dm != null) {
90
 
                                        new AEThread2("playExisting", true) {
91
 
                                                
 
90
                        Matcher m = hashPattern.matcher(url);
 
91
                        if (m.find()) {
 
92
                                String hash = m.group(1);
 
93
                                GlobalManager gm = core.getGlobalManager();
 
94
                                final DownloadManager dm = gm.getDownloadManager(new HashWrapper(
 
95
                                                Base32.decode(hash)));
 
96
                                if (dm != null) {
 
97
                                        if (playNow || playPrepare) {
 
98
                                                new AEThread2("playExisting", true) {
 
99
 
92
100
                                                        public void run() {
93
 
                                                                if ( playNow ){
94
 
                                                                        Debug.outNoStack("loadTorrent already exists.. playing", false);
 
101
                                                                if (playNow) {
 
102
                                                                        Debug.outNoStack("loadTorrent already exists.. playing",
 
103
                                                                                        false);
95
104
 
96
105
                                                                        TorrentListViewsUtils.playOrStream(dm);
97
 
                                                                }else{
98
 
                                                                        Debug.outNoStack("loadTorrent already exists.. preparing", false);
 
106
                                                                } else {
 
107
                                                                        Debug.outNoStack("loadTorrent already exists.. preparing",
 
108
                                                                                        false);
99
109
 
100
110
                                                                        PlayUtils.prepareForPlay(dm);
101
111
                                                                }
102
112
                                                        }
103
 
                                                
 
113
 
104
114
                                                }.start();
105
 
                                        return;
106
 
                                }
107
 
                        }
 
115
                                        } else {
 
116
                                                Utils.openMessageBox(Utils.findAnyShell(), SWT.OK,
 
117
                                                                MSG_ALREADY_EXISTS, new String[] {
 
118
                                                                        " ",
 
119
                                                                        dm.getDisplayName(),
 
120
                                                                        MessageText.getString(MSG_ALREADY_EXISTS_NAME),
 
121
                                                                });
 
122
                                        }
 
123
                                        return;
 
124
                                }
108
125
                        }
109
 
                        
110
 
                        
 
126
 
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();
 
132
                                }
 
133
                                if (cn == null) {
 
134
                                        cn = ConstantsVuze.getDefaultContentNetwork();
 
135
                                }
 
136
                                url = cn.appendURLSuffix(url, false, true);
114
137
                        }
115
138
 
116
139
                        UIFunctionsSWT uiFunctions = (UIFunctionsSWT) UIFunctionsManager.getUIFunctions();
117
140
                        if (uiFunctions != null) {
118
141
                                if (!COConfigurationManager.getBooleanParameter("add_torrents_silently")) {
119
 
                                        if ( bringToFront ){
 
142
                                        if (bringToFront) {
120
143
                                                uiFunctions.bringToFront();
121
144
                                        }
122
145
                                }
124
147
                                Shell shell = uiFunctions.getMainShell();
125
148
                                if (shell != null) {
126
149
                                        new FileDownloadWindow(core, shell, url, dlInfo.getReferer(),
127
 
                                                        dlInfo.getRequestProperties(), 
 
150
                                                        dlInfo.getRequestProperties(),
128
151
                                                        new TorrentDownloaderCallBackInterface() {
129
152
 
130
153
                                                                public void TorrentDownloaderEvent(int state,
135
158
                                                                                file.deleteOnExit();
136
159
 
137
160
                                                                                // Do a quick check to see if it's a torrent
138
 
                                                                                if (!TorrentUtil.isFileTorrent(file, Utils.findAnyShell(),
139
 
                                                                                                file.getName())) {
 
161
                                                                                if (!TorrentUtil.isFileTorrent(file, null, file.getName())) {
 
162
                                                                                        Matcher m = hashPattern.matcher(inf.getURL());
 
163
                                                                                        if (m.find()) {
 
164
                                                                                                String hash = m.group(1);
 
165
 
 
166
                                                                                                ContentNetwork cn = null;
 
167
                                                                                                if (dlInfo instanceof DownloadUrlInfoContentNetwork) {
 
168
                                                                                                        cn = ((DownloadUrlInfoContentNetwork) dlInfo).getContentNetwork();
 
169
                                                                                                }
 
170
                                                                                                if (cn == null) {
 
171
                                                                                                        cn = ConstantsVuze.getDefaultContentNetwork();
 
172
                                                                                                }
 
173
                                                                                                TorrentListViewsUtils.viewDetails(cn, hash,
 
174
                                                                                                                "loadtorrent");
 
175
                                                                                        } else {
 
176
                                                                                                TorrentUtil.isFileTorrent(file, Utils.findAnyShell(),
 
177
                                                                                                                file.getName());
 
178
                                                                                        }
 
179
 
140
180
                                                                                        return;
141
181
                                                                                }
142
182
 
153
193
                                                                                        Debug.out("stopped loading torrent because it's not in whitelist");
154
194
                                                                                        return;
155
195
                                                                                }
156
 
                                                                                
 
196
 
157
197
                                                                                HashWrapper hw;
158
198
                                                                                try {
159
199
                                                                                        hw = torrent.getHashWrapper();
162
202
                                                                                        return;
163
203
                                                                                }
164
204
 
165
 
                                                                                if (forceDRMtoCDP && (PlatformTorrentUtils.isContentDRM(torrent)
166
 
                                                                                                || PlatformTorrentUtils.isContentPurchased(torrent))) {
167
 
                                                                                        TorrentListViewsUtils.viewDetails(hw.toBase32String(),
 
205
                                                                                if (forceDRMtoCDP
 
206
                                                                                                && (PlatformTorrentUtils.isContentDRM(torrent) || PlatformTorrentUtils.isContentPurchased(torrent))) {
 
207
                                                                                        TorrentListViewsUtils.viewDetailsFromDS(torrent,
168
208
                                                                                                        "loadtorrent");
169
209
                                                                                        return;
170
210
                                                                                }
171
211
 
172
212
                                                                                GlobalManager gm = core.getGlobalManager();
173
213
 
174
 
                                                                                if (playNow || playPrepare ) {
 
214
                                                                                if (playNow || playPrepare) {
175
215
                                                                                        DownloadManager existingDM = gm.getDownloadManager(hw);
176
216
                                                                                        if (existingDM != null) {
177
 
                                                                                                if ( playNow ){
 
217
                                                                                                if (playNow) {
178
218
                                                                                                        TorrentListViewsUtils.playOrStream(existingDM);
179
 
                                                                                                }else{
 
219
                                                                                                } else {
180
220
                                                                                                        PlayUtils.prepareForPlay(existingDM);
181
221
                                                                                                }
182
222
                                                                                                return;
226
266
                                        }
227
267
 
228
268
                                        boolean showHomeHint = true;
229
 
                                        if (playNow || playPrepare ) {
230
 
                                                if ( playNow ){
 
269
                                        if (playNow || playPrepare) {
 
270
                                                if (playNow) {
231
271
                                                        showHomeHint = !TorrentListViewsUtils.playOrStream(dm);
232
 
                                                }else{
 
272
                                                } else {
233
273
                                                        PlayUtils.prepareForPlay(dm);
234
274
                                                        showHomeHint = false;
235
275
                                                }
243
283
                        }
244
284
                }.start();
245
285
        }
246
 
        
 
286
 
247
287
        /**
248
288
         * No clue if we have a easy way to add a TOTorrent to the GM, so here it is
249
289
         * @param torrent
259
299
                        String filename = tempTorrentFile.getAbsolutePath();
260
300
                        torrent.serialiseToBEncodedFile(tempTorrentFile);
261
301
 
262
 
 
263
302
                        String savePath = COConfigurationManager.getStringParameter("Default save path");
264
303
                        if (savePath == null || savePath.length() == 0) {
265
304
                                savePath = ".";
273
312
                }
274
313
                return true;
275
314
        }
 
315
 
 
316
        /**
 
317
         * Retrieves the thumbnail for the content, pulling it from the web if
 
318
         * it can
 
319
         * 
 
320
         * @param datasource
 
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
 
325
         *
 
326
         * @since 4.0.0.5
 
327
         */
 
328
        public static Image getContentImage(Object datasource, boolean big,
 
329
                        final ContentImageLoadedListener l) {
 
330
                if (l == null) {
 
331
                        return null;
 
332
                }
 
333
                TOTorrent torrent = DataSourceUtils.getTorrent(datasource);
 
334
                if (torrent == null) {
 
335
                        l.contentImageLoaded(null, true);
 
336
                        return null;
 
337
                }
 
338
 
 
339
                ImageLoader imageLoader = ImageLoader.getInstance();
 
340
 
 
341
                String thumbnailUrl = PlatformTorrentUtils.getContentThumbnailUrl(torrent);
 
342
 
 
343
                if (thumbnailUrl != null && imageLoader.imageExists(thumbnailUrl)) {
 
344
                        l.contentImageLoaded(imageLoader.getImage(thumbnailUrl), true);
 
345
                        return null;
 
346
                }
 
347
 
 
348
                String hash = null;
 
349
                try {
 
350
                        hash = torrent.getHashWrapper().toBase32String();
 
351
                } catch (TOTorrentException e) {
 
352
                }
 
353
                if (hash == null) {
 
354
                        l.contentImageLoaded(null, true);
 
355
                        return null;
 
356
                }
 
357
 
 
358
                String id = "Thumbnail." + hash;
 
359
 
 
360
                Image image = imageLoader.imageAdded(id) ? imageLoader.getImage(id) : null;
 
361
                if (image != null && !image.isDisposed()) {
 
362
                        l.contentImageLoaded(image, true);
 
363
                        return image;
 
364
                }
 
365
 
 
366
                final byte[] imageBytes = PlatformTorrentUtils.getContentThumbnail(torrent);
 
367
                if (imageBytes != null) {
 
368
                        image = (Image) Utils.execSWTThreadWithObject("thumbcreator",
 
369
                                        new AERunnableObject() {
 
370
                                                public Object runSupport() {
 
371
 
 
372
                                                        ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
 
373
                                                        Image image = new Image(Display.getDefault(), bis);
 
374
 
 
375
                                                        return image;
 
376
                                                }
 
377
                                        }, 500);
 
378
                }
 
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);
 
384
                                                }
 
385
                                        });
 
386
                        return image;
 
387
                }
 
388
                if (image == null || image.isDisposed()) {
 
389
                        DownloadManager dm = DataSourceUtils.getDM(datasource);
 
390
                        /*
 
391
                         * Try to get an image from the OS
 
392
                         */
 
393
 
 
394
                        String path = null;
 
395
                        if (dm == null) {
 
396
                                if (torrent != null) {
 
397
                                        TOTorrentFile[] files = torrent.getFiles();
 
398
                                        if (files.length > 0) {
 
399
                                                path = files[0].getRelativePath();
 
400
                                        }
 
401
                                }
 
402
                        } else {
 
403
                                path = dm.getDownloadState().getPrimaryFile();
 
404
                        }
 
405
                        if (path != null) {
 
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());
 
409
                                if (icon != null) {
 
410
                                        image = new Image(Display.getDefault(), icon, SWT.IMAGE_COPY);
 
411
                                }
 
412
                        }
 
413
 
 
414
                        if (image == null) {
 
415
                                imageLoader.addImage(id, ImageLoader.noImage);
 
416
                        } else {
 
417
                                imageLoader.addImage(id, image);
 
418
                        }
 
419
                } else {
 
420
                        imageLoader.addImage(id, image);
 
421
                }
 
422
 
 
423
                l.contentImageLoaded(image, true);
 
424
                return image;
 
425
        }
 
426
 
 
427
        public static void releaseContentImage(Object datasource) {
 
428
                TOTorrent torrent = DataSourceUtils.getTorrent(datasource);
 
429
                if (torrent == null) {
 
430
                        return;
 
431
                }
 
432
 
 
433
                ImageLoader imageLoader = ImageLoader.getInstance();
 
434
 
 
435
                String thumbnailUrl = PlatformTorrentUtils.getContentThumbnailUrl(torrent);
 
436
 
 
437
                if (thumbnailUrl != null) {
 
438
                        imageLoader.releaseImage(thumbnailUrl);
 
439
                } else {
 
440
                        String hash = null;
 
441
                        try {
 
442
                                hash = torrent.getHashWrapper().toBase32String();
 
443
                        } catch (TOTorrentException e) {
 
444
                        }
 
445
                        if (hash == null) {
 
446
                                return;
 
447
                        }
 
448
 
 
449
                        String id = "Thumbnail." + hash;
 
450
                        imageLoader.releaseImage(id);
 
451
                }
 
452
        }
 
453
 
 
454
        public static interface ContentImageLoadedListener
 
455
        {
 
456
                /**
 
457
                 * @param image
 
458
                 * @param wasReturned  Image was also returned from getContentImage 
 
459
                 *
 
460
                 * @since 4.0.0.5
 
461
                 */
 
462
                public void contentImageLoaded(Image image, boolean wasReturned);
 
463
        }
276
464
}