/* * This file Copyright (C) 2009 Charles Kerr * * This file is licensed by the GPL version 2. Works owned by the * Transmission project are granted a special exemption to clause 2(b) * so that the bulk of its code can remain under the MIT license. * This exemption does not extend to derived works not owned by * the Transmission project. * * $Id$ */ #include #include #include #include #include #include #include #include #include #include #include /* tr_new0, tr_strdup */ #include "app.h" #include "prefs.h" #include "qticonloader.h" #include "torrent.h" #include "utils.h" Torrent :: Torrent( Prefs& prefs, int id ): myPrefs( prefs ) { for( int i=0; istandardIcon( QStyle::SP_FileIcon ) ); } Torrent :: ~Torrent( ) { } /*** **** ***/ Torrent :: Property Torrent :: myProperties[] = { { ID, "id", QVariant::Int, INFO, }, { UPLOAD_SPEED, "rateUpload", QVariant::Int, STAT } /* B/s */, { DOWNLOAD_SPEED, "rateDownload", QVariant::Int, STAT }, /* B/s */ { SWARM_SPEED, "swarmSpeed", QVariant::Int, STAT_EXTRA },/* KB/s */ { DOWNLOAD_DIR, "downloadDir", QVariant::String, STAT }, { ACTIVITY, "status", QVariant::Int, STAT }, { NAME, "name", QVariant::String, INFO }, { ERROR, "error", QVariant::Int, STAT }, { ERROR_STRING, "errorString", QVariant::String, STAT }, { SIZE_WHEN_DONE, "sizeWhenDone", QVariant::ULongLong, STAT }, { LEFT_UNTIL_DONE, "leftUntilDone", QVariant::ULongLong, STAT }, { HAVE_UNCHECKED, "haveUnchecked", QVariant::ULongLong, STAT }, { HAVE_VERIFIED, "haveValid", QVariant::ULongLong, STAT }, { TOTAL_SIZE, "totalSize", QVariant::ULongLong, INFO }, { PIECE_SIZE, "pieceSize", QVariant::ULongLong, INFO }, { PIECE_COUNT, "pieceCount", QVariant::Int, INFO }, { PEERS_GETTING_FROM_US, "peersGettingFromUs", QVariant::Int, STAT }, { PEERS_SENDING_TO_US, "peersSendingToUs", QVariant::Int, STAT }, { WEBSEEDS_SENDING_TO_US, "webseedsSendingToUs", QVariant::Int, STAT_EXTRA }, { PERCENT_DONE, "percentDone", QVariant::Double, STAT }, { PERCENT_VERIFIED, "recheckProgress", QVariant::Double, STAT }, { DATE_ACTIVITY, "activityDate", QVariant::DateTime, STAT_EXTRA }, { DATE_ADDED, "addedDate", QVariant::DateTime, INFO }, { DATE_STARTED, "startDate", QVariant::DateTime, STAT_EXTRA }, { DATE_CREATED, "dateCreated", QVariant::DateTime, INFO }, { PEERS_CONNECTED, "peersConnected", QVariant::Int, STAT }, { ETA, "eta", QVariant::Int, STAT }, { RATIO, "uploadRatio", QVariant::Double, STAT }, { DOWNLOADED_EVER, "downloadedEver", QVariant::ULongLong, STAT }, { UPLOADED_EVER, "uploadedEver", QVariant::ULongLong, STAT }, { FAILED_EVER, "corruptEver", QVariant::ULongLong, STAT_EXTRA }, { TRACKERS, "trackers", QVariant::StringList, INFO }, { MIME_ICON, "ccc", QVariant::Icon, DERIVED }, { SEED_RATIO_LIMIT, "seedRatioLimit", QVariant::Double, STAT_EXTRA }, { SEED_RATIO_MODE, "seedRatioMode", QVariant::Int, STAT_EXTRA }, { DOWN_LIMIT, "downloadLimit", QVariant::Int, STAT_EXTRA }, /* KB/s */ { DOWN_LIMITED, "downloadLimited", QVariant::Bool, STAT_EXTRA }, { UP_LIMIT, "uploadLimit", QVariant::Int, STAT_EXTRA }, /* KB/s */ { UP_LIMITED, "uploadLimited", QVariant::Bool, STAT_EXTRA }, { HONORS_SESSION_LIMITS, "honorsSessionLimits", QVariant::Bool, STAT_EXTRA }, { PEER_LIMIT, "peer-limit", QVariant::Int, STAT_EXTRA }, { HASH_STRING, "hashString", QVariant::String, INFO }, { IS_PRIVATE, "isPrivate", QVariant::Bool, INFO }, { COMMENT, "comment", QVariant::String, INFO }, { CREATOR, "creator", QVariant::String, INFO }, { LAST_ANNOUNCE_TIME, "lastAnnounceTime", QVariant::DateTime, STAT_EXTRA }, { LAST_SCRAPE_TIME, "lastScrapeTime", QVariant::DateTime, STAT_EXTRA }, { MANUAL_ANNOUNCE_TIME, "manualAnnounceTime", QVariant::DateTime, STAT_EXTRA }, { NEXT_ANNOUNCE_TIME, "nextAnnounceTime", QVariant::DateTime, STAT_EXTRA }, { NEXT_SCRAPE_TIME, "nextScrapeTime", QVariant::DateTime, STAT_EXTRA }, { SCRAPE_RESPONSE, "scrapeResponse", QVariant::String, STAT_EXTRA }, { ANNOUNCE_RESPONSE, "announceResponse", QVariant::String, STAT_EXTRA }, { ANNOUNCE_URL, "announceURL", QVariant::String, STAT_EXTRA }, { SEEDERS, "seeders", QVariant::Int, STAT_EXTRA }, { LEECHERS, "leechers", QVariant::Int, STAT_EXTRA }, { TIMES_COMPLETED, "timesCompleted", QVariant::Int, STAT_EXTRA }, { PEERS, "peers", TrTypes::PeerList, STAT_EXTRA }, { TORRENT_FILE, "torrentFile", QVariant::String, STAT_EXTRA }, { BANDWIDTH_PRIORITY, "bandwidthPriority", QVariant::Int, STAT_EXTRA } }; Torrent :: KeyList Torrent :: buildKeyList( Group group ) { KeyList keys; if( keys.empty( ) ) for( int i=0; i(); } /*** **** ***/ bool Torrent :: getSeedRatio( double& ratio ) const { bool isLimited; switch( seedRatioMode( ) ) { case TR_RATIOLIMIT_SINGLE: isLimited = true; ratio = seedRatioLimit( ); break; case TR_RATIOLIMIT_GLOBAL: if(( isLimited = myPrefs.getBool( Prefs :: RATIO_ENABLED ))) ratio = myPrefs.getDouble( Prefs :: RATIO ); break; case TR_RATIOLIMIT_UNLIMITED: isLimited = false; break; } return isLimited; } bool Torrent :: hasFileSubstring( const QString& substr ) const { foreach( const TrFile file, myFiles ) if( file.filename.contains( substr, Qt::CaseInsensitive ) ) return true; return false; } bool Torrent :: hasTrackerSubstring( const QString& substr ) const { foreach( QString s, myValues[TRACKERS].toStringList() ) if( s.contains( substr, Qt::CaseInsensitive ) ) return true; return false; } int Torrent :: compareRatio( const Torrent& that ) const { const double a = ratio( ); const double b = that.ratio( ); if( (int)a == TR_RATIO_INF && (int)b == TR_RATIO_INF ) return 0; if( (int)a == TR_RATIO_INF ) return 1; if( (int)b == TR_RATIO_INF ) return -1; if( a < b ) return -1; if( a > b ) return 1; return 0; } int Torrent :: compareETA( const Torrent& that ) const { const bool haveA( hasETA( ) ); const bool haveB( that.hasETA( ) ); if( haveA && haveB ) return getETA() - that.getETA(); if( haveA ) return -1; if( haveB ) return 1; return 0; } int Torrent :: compareTracker( const Torrent& that ) const { Q_UNUSED( that ); // FIXME return 0; } /*** **** ***/ void Torrent :: updateMimeIcon( ) { const FileList& files( myFiles ); QIcon icon; if( files.size( ) > 1 ) icon = QtIconLoader :: icon( "folder", QApplication::style()->standardIcon( QStyle::SP_DirIcon ) ); else icon = Utils :: guessMimeIcon( files.at(0).filename ); setIcon( MIME_ICON, icon ); } /*** **** ***/ void Torrent :: notifyComplete( ) const { // if someone wants to implement notification, here's the hook. } /*** **** ***/ void Torrent :: update( tr_benc * d ) { bool changed = false; for( int i=0; i