~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to libbtcore/torrent/torrent.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-02-16 18:37:14 UTC
  • mfrom: (1.1.25 upstream) (0.4.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20090216183714-52tf47jrnmk4xkmp
Tags: 3.2+dfsg.1-2ubuntu1
* Merge with Debian, remaining changes: (LP: #296433)
  - Use Kubuntu's kde4.mk
  - Build-depend on libboost-serialization1.35-dev since unversioned -dev is
    in universe
  - Change plasma-applet-ktorrent to plasma-widget-ktorrent since we're
    supposed to call them widgets for the users

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include <stdlib.h>
32
32
#include <bcodec/bdecoder.h>
33
33
#include <bcodec/bnode.h>
 
34
#include <interfaces/monitorinterface.h>
34
35
 
35
36
#include <klocale.h>
36
37
 
37
38
namespace bt
38
39
{
39
40
 
40
 
        Torrent::Torrent() : piece_length(0),file_length(0),priv_torrent(false)
 
41
        Torrent::Torrent() : piece_length(0),file_length(0),priv_torrent(false),pos_cache_chunk(0),pos_cache_file(0),tmon(0)
41
42
        {
42
43
                text_codec = QTextCodec::codecForName("utf-8");
43
44
                trackers = 0;
 
45
                file_prio_listener = 0;
44
46
        }
45
47
 
46
48
 
195
197
                        if (v->data().getType() == Value::INT || v->data().getType() == Value::INT64)
196
198
                        {
197
199
                                Uint64 s = v->data().toInt64();
198
 
                                TorrentFile file(idx,path,file_length,s,piece_length);
 
200
                                TorrentFile file(this,idx,path,file_length,s,piece_length);
199
201
                                file.setUnencodedPath(unencoded_path);
200
202
 
201
203
                                // update file_length
428
430
                }
429
431
                return count;
430
432
        }
 
433
        
 
434
        
431
435
 
432
436
        void Torrent::calcChunkPos(Uint32 chunk,QList<Uint32> & file_list) const
433
437
        {
434
438
                file_list.clear();
435
439
                if (chunk >= (Uint32)hash_pieces.size() || files.empty())
436
440
                        return;
437
 
 
438
 
                for (int i = 0;i < files.count();i++)
 
441
                
 
442
                int start = (chunk >= this->pos_cache_chunk) ? this->pos_cache_file : 0;
 
443
                int end = (files.count() - 1);
 
444
                int mid = start + (end - start) / 2;
 
445
                while (start != mid && mid != end)
 
446
                {
 
447
                        //printf("start = %i ; end = %i ; mid = %i\n",start,end,mid);
 
448
                        const TorrentFile & f = files[mid];
 
449
                        if (chunk >= f.getFirstChunk() && chunk <= f.getLastChunk())
 
450
                        {
 
451
                                int i = mid;
 
452
                                while (i > 0)
 
453
                                {
 
454
                                        i--;
 
455
                                        const TorrentFile & tf = files[i];
 
456
                                        if (!(chunk >= tf.getFirstChunk() && chunk <= tf.getLastChunk()))
 
457
                                        {
 
458
                                                i++;
 
459
                                                break;
 
460
                                        }
 
461
                                }
 
462
                                mid = i;
 
463
                                break;
 
464
                        }
 
465
                        else
 
466
                        {
 
467
                                if (chunk > f.getLastChunk())
 
468
                                {
 
469
                                        // chunk comes after file
 
470
                                        start = mid + 1;
 
471
                                        mid = start + (end - start) / 2;
 
472
                                }
 
473
                                else
 
474
                                {
 
475
                                        // chunk comes before file
 
476
                                        end = mid - 1;
 
477
                                        mid = start + (end - start) / 2;
 
478
                                }
 
479
                        }
 
480
                }
 
481
                
 
482
                for (int i = mid;i < files.count();i++)
439
483
                {
440
484
                        const TorrentFile & f = files[i];
441
485
                        if (chunk >= f.getFirstChunk() && chunk <= f.getLastChunk() && f.getSize() != 0)
 
486
                        {
442
487
                                file_list.append(f.getIndex());
 
488
                        }
 
489
                        else if (chunk < f.getFirstChunk())
 
490
                                break; 
443
491
                }
 
492
                
 
493
                pos_cache_chunk = chunk;
 
494
                pos_cache_file = file_list.at(0);
444
495
        }
445
496
 
446
497
        bool Torrent::isMultimedia() const
491
542
                }
492
543
                name_suggestion = text_codec->toUnicode(unencoded_name);
493
544
        }
 
545
        
 
546
        void Torrent::downloadPriorityChanged(TorrentFile* tf,Priority newpriority,Priority oldpriority)
 
547
        {
 
548
                if (file_prio_listener)
 
549
                        file_prio_listener->downloadPriorityChanged(tf,newpriority,oldpriority);
 
550
        }
 
551
        
 
552
        void Torrent::filePercentageChanged(TorrentFile* tf,float perc)
 
553
        {
 
554
                if (tmon)
 
555
                        tmon->filePercentageChanged(tf,perc);
 
556
        }
 
557
        
 
558
        void Torrent::filePreviewChanged(TorrentFile* tf,bool preview)
 
559
        {
 
560
                if (tmon)
 
561
                        tmon->filePreviewChanged(tf,preview);
 
562
        }
494
563
}