From kde-commits Thu Nov 18 21:19:25 2010 From: Christian Weilbach Date: Thu, 18 Nov 2010 21:19:25 +0000 To: kde-commits Subject: playground/ioslaves/kio_magnet Message-Id: <20101118211925.14874AC8A0 () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=129011521904817 SVN commit 1198543 by weilbach: Use the foreach macro; Rename vars for lists and items more meaningful; cleanup unnecessary code; fix handling of yet downloading single file torrents; add a message box to ask for user confirmation as a first security measure. Torrents are not started atm. (?) in slotTorrentAdded, have to check why. M +34 -29 ktorrentdbusinterface.cpp --- trunk/playground/ioslaves/kio_magnet/ktorrentdbusinterface.cpp #1198542:1198543 @@ -196,10 +196,16 @@ m_slave->setPath(m_torrentInt->filePathOnDisk(m_file)); m_slave->setSize(m_torrentInt->fileSize(m_file)); m_slave->downloaded(); - } else { // still downloading + } else if (m_file==-1) { // still downloading + m_slave->setPath(m_torrentInt->pathOnDisk()); + m_slave->setSize(m_torrentInt->totalSize()); + m_coreInt->start(m_tor); + m_torrentInt->createTorrentFileStream(0,true); + } else { m_torrentInt->setDoNotDownload(m_file, false); m_slave->setPath(m_torrentInt->filePathOnDisk(m_file)); m_slave->setSize(m_torrentInt->fileSize(m_file)); + m_coreInt->start(m_tor); m_torrentInt->createTorrentFileStream(m_file,true); } m_slave->added(); @@ -252,48 +258,43 @@ return; qulonglong totalSize=0; - QHash tList; - QStringList torrents = MagnetSettings::managedTorrents(); - QList::const_iterator it = torrents.begin(); - QList::const_iterator end = torrents.end(); - for (;it!=end;it++) { - if ((*it)==m_tor||MagnetSettings::runningTorrents().contains((*it))) + QHash shareRatioList; + foreach( QString torrent, MagnetSettings::managedTorrents() ) { + if (torrent==m_tor || MagnetSettings::runningTorrents().contains((torrent))) break; - KTorrentTorrentInterface* torrentInt = new org::ktorrent::torrent("org.ktorrent.ktorrent", "/torrent/"+(*it), + KTorrentTorrentInterface* torrentInt = new org::ktorrent::torrent("org.ktorrent.ktorrent", "/torrent/"+torrent, QDBusConnection::sessionBus()); if (!torrentInt->isValid() || (torrentInt->bytesLeft()==0 && torrentInt->shareRatio()>=MagnetSettings::maxShareRatio())) { QStringList mt = MagnetSettings::managedTorrents(); - mt.removeAll((*it)); + mt.removeAll(torrent); MagnetSettings::setManagedTorrents( mt ); delete torrentInt; - m_coreInt->remove((*it),true); - kDebug() << "remove " << (*it) << " torrent."; + m_coreInt->remove(torrent,true); + kDebug() << "removed " << torrent << " torrent."; continue; } totalSize+=torrentInt->bytesDownloaded(); // TODO might get wrong results if network connection is buggy? if (torrentInt->bytesLeft()==0) { - tList[(*it)]=torrentInt->shareRatio(); - kDebug() << (*it) << "-> " << torrentInt->shareRatio(); + shareRatioList[torrent]=torrentInt->shareRatio(); + kDebug() << torrent << "-> " << torrentInt->shareRatio(); } delete torrentInt; } // TODO add ktorrent plugin/support to allow sane group size management? // this only removes torrents when we run out of configured space, starting with the most shared ones - if (totalSize>MagnetSettings::shareSize()*1024*1024&&m_torrentInt->isValid()) { + if ( totalSize>(MagnetSettings::shareSize()*1024*1024) && m_torrentInt->isValid() ) { totalSize+=m_file!=-1 ? m_torrentInt->fileSize(m_file) : m_torrentInt->totalSize(); - QList vals = tList.values(); - qSort( vals ); - QList::const_iterator it = vals.end(); - QList::const_iterator end = vals.begin(); - for ( ;it!=end; it-- ) { - QString tor = tList.key((*it)); + QList sortedRatios = shareRatioList.values(); + qSort( sortedRatios ); + foreach ( double ratio, sortedRatios ) { + QString tor = shareRatioList.key(ratio); KTorrentTorrentInterface* torrentInt = new org::ktorrent::torrent("org.ktorrent.ktorrent", "/torrent/"+tor, QDBusConnection::sessionBus()); totalSize-=torrentInt->bytesDownloaded(); delete torrentInt; - tList.take(tor); + shareRatioList.take(tor); m_coreInt->remove(tor,true); if (totalSizestop(tor); + if ( m_slave->messageBox(KIO::SlaveBase::QuestionYesNo + , i18n( "Do you want to download and share the Torrent: \"%1\"?" ).arg( m_torrentInt->name() ) + , i18n( "Magnet-Link confirmation" )) != KMessageBox::Yes ) { + m_slave->error(KIO::ERR_UNKNOWN, i18n("Download of torrent %1 rejected by user.").arg(tor)); + m_coreInt->remove(tor,true); + return; + } + m_coreInt->start(tor); + + QStringList mt = MagnetSettings::managedTorrents(); QStringList rt = MagnetSettings::runningTorrents(); mt.append(m_tor); @@ -328,12 +340,6 @@ QDBusReplynumFiles = m_torrentInt->numFiles(); int n = numFiles.value(); - if (m_file==-1&&n!=0) { // TODO add directory support - m_slave->error(KIO::ERR_IS_DIRECTORY, i18n("Torrent \"%1\" contains a directory but no flid given.") - .arg(m_torrentInt->name())); - delete m_coreInt; - return; - } // disable all files except the one needed kDebug() << "Number of files: " << n; @@ -343,7 +349,7 @@ m_torrentInt->setDoNotDownload(i, (i!=m_file) ); } - // stream all torrents + // TODO stream all torrents for now m_torrentInt->createTorrentFileStream(m_file,true); m_coreInt->start(m_tor); @@ -364,7 +370,6 @@ path = m_torrentInt->filePathOnDisk(m_file); if (!path.isValid()) { m_slave->error(KIO::ERR_SLAVE_DEFINED, i18n("Could not fetch path from KTorrent.")); - delete m_coreInt; return; } QString p = path.value();