2
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
4
* This file is licensed by the GPL version 2. Works owned by the
5
* Transmission project are granted a special exemption to clause 2(b)
6
* so that the bulk of its code can remain under the MIT license.
7
* This exemption does not extend to derived works not owned by
8
* the Transmission project.
17
#include <QFileSystemWatcher>
19
#include <libtransmission/transmission.h>
22
#include "torrent-model.h"
29
WatchDir :: WatchDir( const TorrentModel& model ):
35
WatchDir :: ~WatchDir( )
44
WatchDir :: metainfoTest( const QString& filename ) const
48
tr_ctor * ctor = tr_ctorNew( 0 );
51
tr_ctorSetMetainfoFromFile( ctor, filename.toUtf8().constData() );
52
const int err = tr_torrentParse( ctor, &inf );
55
else if( myModel.hasTorrent( inf.hashString ) )
62
tr_metainfoFree( &inf );
68
WatchDir :: onTimeout( )
70
QTimer * t = qobject_cast<QTimer*>(sender());
71
const QString filename = t->objectName( );
72
if( metainfoTest( filename ) == OK )
73
emit torrentFileAdded( filename );
78
WatchDir :: setPath( const QString& path, bool isEnabled )
80
// clear out any remnants of the previous watcher, if any
81
myWatchDirFiles.clear( );
87
// maybe create a new watcher
89
myWatcher = new QFileSystemWatcher( );
90
myWatcher->addPath( path );
91
connect( myWatcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(watcherActivated(const QString&)));
92
//std::cerr << "watching " << qPrintable(path) << " for new .torrent files" << std::endl;
93
watcherActivated( path ); // trigger the watchdir for .torrent files in there already
98
WatchDir :: watcherActivated( const QString& path )
100
const QDir dir(path);
102
// get the list of files currently in the watch directory
104
foreach( QString str, dir.entryList( QDir::Readable|QDir::Files ) )
107
// try to add any new files which end in .torrent
108
const QSet<QString> newFiles( files - myWatchDirFiles );
109
foreach( QString name, newFiles ) {
110
if( name.endsWith( ".torrent", Qt::CaseInsensitive ) ) {
111
const QString filename = dir.absoluteFilePath( name );
112
switch( metainfoTest( filename ) ) {
114
emit torrentFileAdded( filename );
119
// give the .torrent a few seconds to finish downloading
120
QTimer * t = new QTimer( this );
121
t->setObjectName( dir.absoluteFilePath( name ) );
122
t->setSingleShot( true );
123
connect( t, SIGNAL(timeout()), this, SLOT(onTimeout()));
130
// update our file list so that we can use it
131
// for comparison the next time around
132
myWatchDirFiles = files;