1 | /* |
---|
2 | * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com> |
---|
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: prefs.cc 9237 2009-10-02 22:53:19Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <cassert> |
---|
14 | #include <iostream> |
---|
15 | #include <cstdlib> // strtod |
---|
16 | |
---|
17 | #include <QDir> |
---|
18 | #include <QFile> |
---|
19 | |
---|
20 | #include <libtransmission/transmission.h> |
---|
21 | #include <libtransmission/bencode.h> |
---|
22 | #include <libtransmission/json.h> |
---|
23 | #include <libtransmission/utils.h> |
---|
24 | #include "prefs.h" |
---|
25 | #include "types.h" |
---|
26 | |
---|
27 | /*** |
---|
28 | **** |
---|
29 | ***/ |
---|
30 | |
---|
31 | Prefs::PrefItem Prefs::myItems[] = |
---|
32 | { |
---|
33 | /* gui settings */ |
---|
34 | { OPTIONS_PROMPT, "show-options-window", QVariant::Bool }, |
---|
35 | { OPEN_DIALOG_FOLDER, "open-dialog-dir", QVariant::String }, |
---|
36 | { INHIBIT_HIBERNATION, "inhibit-desktop-hibernation", QVariant::Bool }, |
---|
37 | { DIR_WATCH, "watch-dir", QVariant::String }, |
---|
38 | { DIR_WATCH_ENABLED, "watch-dir-enabled", QVariant::Bool }, |
---|
39 | { SHOW_TRAY_ICON, "show-notification-area-icon", QVariant::Bool }, |
---|
40 | { SHOW_DESKTOP_NOTIFICATION, "show-desktop-notification", QVariant::Bool }, |
---|
41 | { START, "start-added-torrents", QVariant::Bool }, |
---|
42 | { TRASH_ORIGINAL, "trash-original-torrent-files", QVariant::Bool }, |
---|
43 | { ASKQUIT, "prompt-before-exit", QVariant::Bool }, |
---|
44 | { SORT_MODE, "sort-mode", TrTypes::SortModeType }, |
---|
45 | { SORT_REVERSED, "sort-reversed", QVariant::Bool }, |
---|
46 | { MINIMAL_VIEW, "minimal-view", QVariant::Bool }, |
---|
47 | { FILTERBAR, "show-filterbar", QVariant::Bool }, |
---|
48 | { STATUSBAR, "show-statusbar", QVariant::Bool }, |
---|
49 | { STATUSBAR_STATS, "statusbar-stats", QVariant::String }, |
---|
50 | { TOOLBAR, "show-toolbar" , QVariant::Bool }, |
---|
51 | { BLOCKLIST_DATE, "blocklist-date", QVariant::DateTime }, |
---|
52 | { BLOCKLIST_UPDATES_ENABLED, "blocklist-updates-enabled" , QVariant::Bool }, |
---|
53 | { MAIN_WINDOW_LAYOUT_ORDER, "main-window-layout-order", QVariant::String }, |
---|
54 | { MAIN_WINDOW_HEIGHT, "main-window-height", QVariant::Int }, |
---|
55 | { MAIN_WINDOW_WIDTH, "main-window-width", QVariant::Int }, |
---|
56 | { MAIN_WINDOW_X, "main-window-x", QVariant::Int }, |
---|
57 | { MAIN_WINDOW_Y, "main-window-y", QVariant::Int }, |
---|
58 | { FILTER_MODE, "filter-mode", TrTypes::FilterModeType }, |
---|
59 | { SESSION_IS_REMOTE, "remote-session-enabled", QVariant::Bool }, |
---|
60 | { SESSION_REMOTE_HOST, "remote-session-host", QVariant::String }, |
---|
61 | { SESSION_REMOTE_PORT, "remote-session-port", QVariant::Int }, |
---|
62 | { SESSION_REMOTE_AUTH, "remote-session-requres-authentication", QVariant::Bool }, |
---|
63 | { SESSION_REMOTE_USERNAME, "remote-session-username", QVariant::String }, |
---|
64 | { SESSION_REMOTE_PASSWORD, "remote-session-password", QVariant::String }, |
---|
65 | { USER_HAS_GIVEN_INFORMED_CONSENT, "user-has-given-informed-consent", QVariant::Bool }, |
---|
66 | |
---|
67 | /* libtransmission settings */ |
---|
68 | { ALT_SPEED_LIMIT_UP, TR_PREFS_KEY_ALT_SPEED_UP, QVariant::Int }, |
---|
69 | { ALT_SPEED_LIMIT_DOWN, TR_PREFS_KEY_ALT_SPEED_DOWN, QVariant::Int }, |
---|
70 | { ALT_SPEED_LIMIT_ENABLED, TR_PREFS_KEY_ALT_SPEED_ENABLED, QVariant::Bool }, |
---|
71 | { ALT_SPEED_LIMIT_TIME_BEGIN, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, QVariant::Int }, |
---|
72 | { ALT_SPEED_LIMIT_TIME_END, TR_PREFS_KEY_ALT_SPEED_TIME_END, QVariant::Int }, |
---|
73 | { ALT_SPEED_LIMIT_TIME_ENABLED, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, QVariant::Bool }, |
---|
74 | { ALT_SPEED_LIMIT_TIME_DAY, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, QVariant::Int }, |
---|
75 | { BLOCKLIST_ENABLED, TR_PREFS_KEY_BLOCKLIST_ENABLED, QVariant::Bool }, |
---|
76 | { DSPEED, TR_PREFS_KEY_DSPEED, QVariant::Int }, |
---|
77 | { DSPEED_ENABLED, TR_PREFS_KEY_DSPEED_ENABLED, QVariant::Bool }, |
---|
78 | { DOWNLOAD_DIR, TR_PREFS_KEY_DOWNLOAD_DIR, QVariant::String }, |
---|
79 | { ENCRYPTION, TR_PREFS_KEY_ENCRYPTION, QVariant::Int }, |
---|
80 | { LAZY_BITFIELD, TR_PREFS_KEY_LAZY_BITFIELD, QVariant::Bool }, |
---|
81 | { MSGLEVEL, TR_PREFS_KEY_MSGLEVEL, QVariant::Int }, |
---|
82 | { OPEN_FILE_LIMIT, TR_PREFS_KEY_OPEN_FILE_LIMIT, QVariant::Int }, |
---|
83 | { PEER_LIMIT_GLOBAL, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, QVariant::Int }, |
---|
84 | { PEER_LIMIT_TORRENT, TR_PREFS_KEY_PEER_LIMIT_TORRENT, QVariant::Int }, |
---|
85 | { PEER_PORT, TR_PREFS_KEY_PEER_PORT, QVariant::Int }, |
---|
86 | { PEER_PORT_RANDOM_ON_START, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, QVariant::Bool }, |
---|
87 | { PEER_PORT_RANDOM_LOW, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW, QVariant::Int }, |
---|
88 | { PEER_PORT_RANDOM_HIGH, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH, QVariant::Int }, |
---|
89 | { SOCKET_TOS, TR_PREFS_KEY_PEER_SOCKET_TOS, QVariant::Int }, |
---|
90 | { PEX_ENABLED, TR_PREFS_KEY_PEX_ENABLED, QVariant::Bool }, |
---|
91 | { DHT_ENABLED, TR_PREFS_KEY_DHT_ENABLED, QVariant::Bool }, |
---|
92 | { PORT_FORWARDING, TR_PREFS_KEY_PORT_FORWARDING, QVariant::Bool }, |
---|
93 | { PROXY_AUTH_ENABLED, TR_PREFS_KEY_PROXY_AUTH_ENABLED, QVariant::Bool }, |
---|
94 | { PREALLOCATION, TR_PREFS_KEY_PREALLOCATION, QVariant::Int }, |
---|
95 | { PROXY_ENABLED, TR_PREFS_KEY_PROXY_ENABLED, QVariant::Bool }, |
---|
96 | { PROXY_PASSWORD, TR_PREFS_KEY_PROXY_PASSWORD, QVariant::String }, |
---|
97 | { PROXY_PORT, TR_PREFS_KEY_PROXY_PORT, QVariant::Int }, |
---|
98 | { PROXY, TR_PREFS_KEY_PROXY, QVariant::String }, |
---|
99 | { PROXY_TYPE, TR_PREFS_KEY_PROXY_TYPE, QVariant::Int }, |
---|
100 | { PROXY_USERNAME, TR_PREFS_KEY_PROXY_USERNAME, QVariant::String }, |
---|
101 | { RATIO, TR_PREFS_KEY_RATIO, QVariant::Double }, |
---|
102 | { RATIO_ENABLED, TR_PREFS_KEY_RATIO_ENABLED, QVariant::Bool }, |
---|
103 | { RPC_AUTH_REQUIRED, TR_PREFS_KEY_RPC_AUTH_REQUIRED, QVariant::Bool }, |
---|
104 | { RPC_ENABLED, TR_PREFS_KEY_RPC_ENABLED, QVariant::Bool }, |
---|
105 | { RPC_PASSWORD, TR_PREFS_KEY_RPC_PASSWORD, QVariant::String }, |
---|
106 | { RPC_PORT, TR_PREFS_KEY_RPC_PORT, QVariant::Int }, |
---|
107 | { RPC_USERNAME, TR_PREFS_KEY_RPC_USERNAME, QVariant::String }, |
---|
108 | { RPC_WHITELIST_ENABLED, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, QVariant::Bool }, |
---|
109 | { RPC_WHITELIST, TR_PREFS_KEY_RPC_WHITELIST, QVariant::String }, |
---|
110 | { USPEED_ENABLED, TR_PREFS_KEY_USPEED_ENABLED, QVariant::Bool }, |
---|
111 | { USPEED, TR_PREFS_KEY_USPEED, QVariant::Int }, |
---|
112 | { UPLOAD_SLOTS_PER_TORRENT, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, QVariant::Int } |
---|
113 | }; |
---|
114 | |
---|
115 | /*** |
---|
116 | **** |
---|
117 | ***/ |
---|
118 | |
---|
119 | Prefs :: Prefs( const char * configDir ): |
---|
120 | myConfigDir( configDir ) |
---|
121 | { |
---|
122 | assert( sizeof(myItems) / sizeof(myItems[0]) == PREFS_COUNT ); |
---|
123 | for( int i=0; i<PREFS_COUNT; ++i ) |
---|
124 | assert( myItems[i].id == i ); |
---|
125 | |
---|
126 | tr_benc top; |
---|
127 | tr_bencInitDict( &top, 0 ); |
---|
128 | initDefaults( &top ); |
---|
129 | tr_sessionLoadSettings( &top, configDir, NULL ); |
---|
130 | for( int i=0; i<PREFS_COUNT; ++i ) |
---|
131 | { |
---|
132 | double d; |
---|
133 | tr_bool boolVal; |
---|
134 | int64_t intVal; |
---|
135 | const char * str; |
---|
136 | tr_benc * b( tr_bencDictFind( &top, myItems[i].key ) ); |
---|
137 | |
---|
138 | switch( myItems[i].type ) |
---|
139 | { |
---|
140 | case QVariant::Int: |
---|
141 | if( tr_bencGetInt( b, &intVal ) ) |
---|
142 | myValues[i].setValue( qlonglong(intVal) ); |
---|
143 | break; |
---|
144 | case TrTypes::SortModeType: |
---|
145 | if( tr_bencGetStr( b, &str ) ) |
---|
146 | myValues[i] = QVariant::fromValue( SortMode( str ) ); |
---|
147 | break; |
---|
148 | case TrTypes::FilterModeType: |
---|
149 | if( tr_bencGetStr( b, &str ) ) |
---|
150 | myValues[i] = QVariant::fromValue( FilterMode( str ) ); |
---|
151 | break; |
---|
152 | case QVariant::String: |
---|
153 | if( tr_bencGetStr( b, &str ) ) |
---|
154 | myValues[i].setValue( QString::fromUtf8(str) ); |
---|
155 | break; |
---|
156 | case QVariant::Bool: |
---|
157 | if( tr_bencGetBool( b, &boolVal ) ) |
---|
158 | myValues[i].setValue( bool(boolVal) ); |
---|
159 | break; |
---|
160 | case QVariant::Double: |
---|
161 | if( tr_bencGetReal( b, &d ) ) |
---|
162 | myValues[i].setValue( d ); |
---|
163 | break; |
---|
164 | case QVariant::DateTime: |
---|
165 | if( tr_bencGetInt( b, &intVal ) ) |
---|
166 | myValues[i].setValue( QDateTime :: fromTime_t( intVal ) ); |
---|
167 | break; |
---|
168 | default: |
---|
169 | assert( "unhandled type" && 0 ); |
---|
170 | break; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | tr_bencFree( &top ); |
---|
175 | } |
---|
176 | |
---|
177 | Prefs :: ~Prefs( ) |
---|
178 | { |
---|
179 | tr_benc top; |
---|
180 | |
---|
181 | /* load in the existing preferences file */ |
---|
182 | QFile file( QDir( myConfigDir ).absoluteFilePath( "settings.json" ) ); |
---|
183 | file.open( QIODevice::ReadOnly | QIODevice::Text ); |
---|
184 | const QByteArray oldPrefs = file.readAll( ); |
---|
185 | file.close( ); |
---|
186 | if( tr_jsonParse( "settings.json", oldPrefs.data(), oldPrefs.length(), &top, NULL ) ) |
---|
187 | tr_bencInitDict( &top, PREFS_COUNT ); |
---|
188 | |
---|
189 | /* merge our own settings with the ones already in the file */ |
---|
190 | for( int i=0; i<PREFS_COUNT; ++i ) { |
---|
191 | const char * key = myItems[i].key; |
---|
192 | const QVariant& val = myValues[i]; |
---|
193 | switch( myItems[i].type ) { |
---|
194 | case QVariant::Int: |
---|
195 | tr_bencDictAddInt( &top, key, val.toInt() ); |
---|
196 | break; |
---|
197 | case TrTypes::SortModeType: |
---|
198 | tr_bencDictAddStr( &top, key, val.value<SortMode>().name().toUtf8().constData() ); |
---|
199 | break; |
---|
200 | case TrTypes::FilterModeType: |
---|
201 | tr_bencDictAddStr( &top, key, val.value<FilterMode>().name().toUtf8().constData() ); |
---|
202 | break; |
---|
203 | case QVariant::String: |
---|
204 | tr_bencDictAddStr( &top, key, val.toString().toUtf8().constData() ); |
---|
205 | break; |
---|
206 | case QVariant::Bool: |
---|
207 | tr_bencDictAddBool( &top, key, val.toBool() ); |
---|
208 | break; |
---|
209 | case QVariant::Double: |
---|
210 | tr_bencDictAddReal( &top, key, val.toDouble() ); |
---|
211 | break; |
---|
212 | case QVariant::DateTime: |
---|
213 | tr_bencDictAddInt( &top, key, val.toDateTime().toTime_t() ); |
---|
214 | break; |
---|
215 | default: |
---|
216 | assert( "unhandled type" && 0 ); |
---|
217 | break; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /* write back out the serialized preferences */ |
---|
222 | tr_bencToFile( &top, TR_FMT_JSON, file.fileName().toUtf8().constData() ); |
---|
223 | tr_bencFree( &top ); |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | * This is where we initialize the preferences file with the default values. |
---|
228 | * If you add a new preferences key, you /must/ add a default value here. |
---|
229 | */ |
---|
230 | void |
---|
231 | Prefs :: initDefaults( tr_benc * d ) |
---|
232 | { |
---|
233 | tr_bencDictAddStr( d, keyStr(DIR_WATCH), tr_getDefaultDownloadDir( ) ); |
---|
234 | tr_bencDictAddInt( d, keyStr(DIR_WATCH_ENABLED), false ); |
---|
235 | tr_bencDictAddInt( d, keyStr(INHIBIT_HIBERNATION), false ); |
---|
236 | tr_bencDictAddInt( d, keyStr(BLOCKLIST_DATE), 0 ); |
---|
237 | tr_bencDictAddInt( d, keyStr(BLOCKLIST_UPDATES_ENABLED), true ); |
---|
238 | tr_bencDictAddStr( d, keyStr(OPEN_DIALOG_FOLDER), QDir::home().absolutePath().toLatin1() ); |
---|
239 | tr_bencDictAddInt( d, keyStr(TOOLBAR), true ); |
---|
240 | tr_bencDictAddInt( d, keyStr(FILTERBAR), true ); |
---|
241 | tr_bencDictAddInt( d, keyStr(STATUSBAR), true ); |
---|
242 | tr_bencDictAddInt( d, keyStr(SHOW_TRAY_ICON), false ); |
---|
243 | tr_bencDictAddInt( d, keyStr(SHOW_DESKTOP_NOTIFICATION), true ); |
---|
244 | tr_bencDictAddStr( d, keyStr(STATUSBAR_STATS), "total-ratio" ); |
---|
245 | tr_bencDictAddInt( d, keyStr(OPTIONS_PROMPT), true ); |
---|
246 | tr_bencDictAddInt( d, keyStr(MAIN_WINDOW_HEIGHT), 500 ); |
---|
247 | tr_bencDictAddInt( d, keyStr(MAIN_WINDOW_WIDTH), 300 ); |
---|
248 | tr_bencDictAddInt( d, keyStr(MAIN_WINDOW_X), 50 ); |
---|
249 | tr_bencDictAddInt( d, keyStr(MAIN_WINDOW_Y), 50 ); |
---|
250 | tr_bencDictAddStr( d, keyStr(FILTER_MODE), "all" ); |
---|
251 | tr_bencDictAddStr( d, keyStr(MAIN_WINDOW_LAYOUT_ORDER), "menu,toolbar,filter,list,statusbar" ); |
---|
252 | tr_bencDictAddStr( d, keyStr(DOWNLOAD_DIR), tr_getDefaultDownloadDir( ) ); |
---|
253 | tr_bencDictAddInt( d, keyStr(ASKQUIT), true ); |
---|
254 | tr_bencDictAddStr( d, keyStr(SORT_MODE), "sort-by-name" ); |
---|
255 | tr_bencDictAddInt( d, keyStr(SORT_REVERSED), false ); |
---|
256 | tr_bencDictAddInt( d, keyStr(MINIMAL_VIEW), false ); |
---|
257 | tr_bencDictAddInt( d, keyStr(START), true ); |
---|
258 | tr_bencDictAddInt( d, keyStr(TRASH_ORIGINAL), false ); |
---|
259 | tr_bencDictAddStr( d, keyStr(SESSION_REMOTE_HOST), "localhost" ); |
---|
260 | tr_bencDictAddInt( d, keyStr(SESSION_REMOTE_PORT), atoi(TR_DEFAULT_RPC_PORT_STR) ); |
---|
261 | tr_bencDictAddBool( d, keyStr(SESSION_IS_REMOTE), false ); |
---|
262 | tr_bencDictAddBool( d, keyStr(SESSION_REMOTE_AUTH), false ); |
---|
263 | tr_bencDictAddBool( d, keyStr(USER_HAS_GIVEN_INFORMED_CONSENT), false ); |
---|
264 | } |
---|
265 | |
---|
266 | /*** |
---|
267 | **** |
---|
268 | ***/ |
---|
269 | |
---|
270 | bool |
---|
271 | Prefs :: getBool( int key ) const |
---|
272 | { |
---|
273 | assert( myItems[key].type == QVariant::Bool ); |
---|
274 | return myValues[key].toBool( ); |
---|
275 | } |
---|
276 | |
---|
277 | QString |
---|
278 | Prefs :: getString( int key ) const |
---|
279 | { |
---|
280 | assert( myItems[key].type == QVariant::String ); |
---|
281 | return myValues[key].toString( ); |
---|
282 | } |
---|
283 | |
---|
284 | int |
---|
285 | Prefs :: getInt( int key ) const |
---|
286 | { |
---|
287 | assert( myItems[key].type == QVariant::Int ); |
---|
288 | return myValues[key].toInt( ); |
---|
289 | } |
---|
290 | |
---|
291 | double |
---|
292 | Prefs :: getDouble( int key ) const |
---|
293 | { |
---|
294 | assert( myItems[key].type == QVariant::Double ); |
---|
295 | return myValues[key].toDouble( ); |
---|
296 | } |
---|
297 | |
---|
298 | QDateTime |
---|
299 | Prefs :: getDateTime( int key ) const |
---|
300 | { |
---|
301 | assert( myItems[key].type == QVariant::DateTime ); |
---|
302 | return myValues[key].toDateTime( ); |
---|
303 | } |
---|
304 | |
---|
305 | /*** |
---|
306 | **** |
---|
307 | ***/ |
---|
308 | |
---|
309 | void |
---|
310 | Prefs :: toggleBool( int key ) |
---|
311 | { |
---|
312 | set( key, !getBool( key ) ); |
---|
313 | } |
---|