1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
3 | * |
---|
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. |
---|
9 | * |
---|
10 | * $Id: session.h 10769 2010-06-16 02:58:44Z Longinus00 $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef TR_APP_SESSION_H |
---|
14 | #define TR_APP_SESSION_H |
---|
15 | |
---|
16 | #include <QObject> |
---|
17 | #include <QSet> |
---|
18 | #include <QBuffer> |
---|
19 | #include <QFileInfoList> |
---|
20 | #include <QNetworkAccessManager> |
---|
21 | #include <QString> |
---|
22 | #include <QUrl> |
---|
23 | |
---|
24 | #include <libtransmission/transmission.h> |
---|
25 | |
---|
26 | #include "speed.h" |
---|
27 | |
---|
28 | extern "C" |
---|
29 | { |
---|
30 | struct tr_benc; |
---|
31 | } |
---|
32 | |
---|
33 | class Prefs; |
---|
34 | |
---|
35 | struct Reply |
---|
36 | { |
---|
37 | QNetworkReply * networkReply; |
---|
38 | QBuffer * buffer; |
---|
39 | }; |
---|
40 | typedef QList<Reply> ReplyList; |
---|
41 | |
---|
42 | class Session: public QObject |
---|
43 | { |
---|
44 | Q_OBJECT |
---|
45 | |
---|
46 | public: |
---|
47 | Session( const char * configDir, Prefs& prefs ); |
---|
48 | ~Session( ); |
---|
49 | |
---|
50 | public: |
---|
51 | void stop( ); |
---|
52 | void restart( ); |
---|
53 | |
---|
54 | private: |
---|
55 | void start( ); |
---|
56 | |
---|
57 | public: |
---|
58 | const QUrl& getRemoteUrl( ) const { return myUrl; } |
---|
59 | const struct tr_session_stats& getStats( ) const { return myStats; } |
---|
60 | const struct tr_session_stats& getCumulativeStats( ) const { return myCumulativeStats; } |
---|
61 | const QString& sessionVersion( ) const { return mySessionVersion; } |
---|
62 | |
---|
63 | public: |
---|
64 | int64_t blocklistSize( ) const { return myBlocklistSize; } |
---|
65 | void setBlocklistSize( int64_t i ); |
---|
66 | void updateBlocklist( ); |
---|
67 | void portTest( ); |
---|
68 | void copyMagnetLinkToClipboard( int torrentId ); |
---|
69 | |
---|
70 | public: |
---|
71 | |
---|
72 | /** returns true if the transmission session is being run inside this client */ |
---|
73 | bool isServer( ) const; |
---|
74 | |
---|
75 | /** returns true if isServer() is true or if the remote address is the localhost */ |
---|
76 | bool isLocal( ) const; |
---|
77 | |
---|
78 | private: |
---|
79 | void updateStats( struct tr_benc * args ); |
---|
80 | void updateInfo( struct tr_benc * args ); |
---|
81 | void parseResponse( const char * json, size_t len ); |
---|
82 | static void localSessionCallback( tr_session *, const char *, size_t, void * ); |
---|
83 | |
---|
84 | public: |
---|
85 | void exec( const char * json ); |
---|
86 | void exec( const struct tr_benc * request ); |
---|
87 | |
---|
88 | public: |
---|
89 | int64_t getUniqueTag( ) { return nextUniqueTag++; } |
---|
90 | |
---|
91 | private: |
---|
92 | void sessionSet( const char * key, const QVariant& variant ); |
---|
93 | void pumpRequests( ); |
---|
94 | void sendTorrentRequest( const char * request, const QSet<int>& torrentIds ); |
---|
95 | static void updateStats( struct tr_benc * d, struct tr_session_stats * stats ); |
---|
96 | void refreshTorrents( const QSet<int>& torrentIds ); |
---|
97 | |
---|
98 | public: |
---|
99 | void torrentSet( const QSet<int>& ids, const QString& key, bool val ); |
---|
100 | void torrentSet( const QSet<int>& ids, const QString& key, int val ); |
---|
101 | void torrentSet( const QSet<int>& ids, const QString& key, double val ); |
---|
102 | void torrentSet( const QSet<int>& ids, const QString& key, const QList<int>& val ); |
---|
103 | void torrentSetLocation( const QSet<int>& ids, const QString& path, bool doMove ); |
---|
104 | |
---|
105 | |
---|
106 | public slots: |
---|
107 | void pauseTorrents( const QSet<int>& torrentIds = QSet<int>() ); |
---|
108 | void startTorrents( const QSet<int>& torrentIds = QSet<int>() ); |
---|
109 | void refreshSessionInfo( ); |
---|
110 | void refreshSessionStats( ); |
---|
111 | void refreshActiveTorrents( ); |
---|
112 | void refreshAllTorrents( ); |
---|
113 | void initTorrents( const QSet<int>& ids = QSet<int>() ); |
---|
114 | void addTorrent( QString filename ); |
---|
115 | void addTorrent( QString filename, QString localPath ); |
---|
116 | void removeTorrents( const QSet<int>& torrentIds, bool deleteFiles=false ); |
---|
117 | void verifyTorrents( const QSet<int>& torrentIds ); |
---|
118 | void reannounceTorrents( const QSet<int>& torrentIds ); |
---|
119 | void launchWebInterface( ); |
---|
120 | void updatePref( int key ); |
---|
121 | |
---|
122 | /** request a refresh for statistics, including the ones only used by the properties dialog, for a specific torrent */ |
---|
123 | void refreshExtraStats( const QSet<int>& ids ); |
---|
124 | |
---|
125 | private slots: |
---|
126 | void onFinished( QNetworkReply * reply ); |
---|
127 | |
---|
128 | signals: |
---|
129 | void executed( int64_t tag, const QString& result, struct tr_benc * arguments ); |
---|
130 | void sourceChanged( ); |
---|
131 | void portTested( bool isOpen ); |
---|
132 | void statsUpdated( ); |
---|
133 | void sessionUpdated( ); |
---|
134 | void blocklistUpdated( int ); |
---|
135 | void torrentsUpdated( struct tr_benc * torrentList, bool completeList ); |
---|
136 | void torrentsRemoved( struct tr_benc * torrentList ); |
---|
137 | void dataReadProgress( ); |
---|
138 | void dataSendProgress( ); |
---|
139 | void httpAuthenticationRequired( ); |
---|
140 | |
---|
141 | private: |
---|
142 | int64_t nextUniqueTag; |
---|
143 | int64_t myBlocklistSize; |
---|
144 | Prefs& myPrefs; |
---|
145 | tr_session * mySession; |
---|
146 | QString myConfigDir; |
---|
147 | QString mySessionId; |
---|
148 | QUrl myUrl; |
---|
149 | QNetworkAccessManager myNAM; |
---|
150 | ReplyList myReplies; |
---|
151 | struct tr_session_stats myStats; |
---|
152 | struct tr_session_stats myCumulativeStats; |
---|
153 | QString mySessionVersion; |
---|
154 | }; |
---|
155 | |
---|
156 | #endif |
---|
157 | |
---|