source: trunk/qt/prefs.cc @ 8401

Last change on this file since 8401 was 8401, checked in by charles, 7 years ago

(trunk qt) #2077: doesn't show/modify the "Stop seeding torrents at ratio"

File size: 12.5 KB
Line 
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:$
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
31Prefs::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
66    /* libtransmission settings */
67    { ALT_SPEED_LIMIT_UP, TR_PREFS_KEY_ALT_SPEED_UP, QVariant::Int },
68    { ALT_SPEED_LIMIT_DOWN, TR_PREFS_KEY_ALT_SPEED_DOWN, QVariant::Int },
69    { ALT_SPEED_LIMIT_ENABLED, TR_PREFS_KEY_ALT_SPEED_ENABLED, QVariant::Bool },
70    { ALT_SPEED_LIMIT_TIME_BEGIN, TR_PREFS_KEY_ALT_SPEED_TIME_BEGIN, QVariant::Int },
71    { ALT_SPEED_LIMIT_TIME_END, TR_PREFS_KEY_ALT_SPEED_TIME_END, QVariant::Int },
72    { ALT_SPEED_LIMIT_TIME_ENABLED, TR_PREFS_KEY_ALT_SPEED_TIME_ENABLED, QVariant::Bool },
73    { ALT_SPEED_LIMIT_TIME_DAY, TR_PREFS_KEY_ALT_SPEED_TIME_DAY, QVariant::Int },
74    { BLOCKLIST_ENABLED, TR_PREFS_KEY_BLOCKLIST_ENABLED, QVariant::Bool },
75    { DSPEED, TR_PREFS_KEY_DSPEED, QVariant::Int },
76    { DSPEED_ENABLED, TR_PREFS_KEY_DSPEED_ENABLED, QVariant::Bool },
77    { DOWNLOAD_DIR, TR_PREFS_KEY_DOWNLOAD_DIR, QVariant::String },
78    { ENCRYPTION, TR_PREFS_KEY_ENCRYPTION, QVariant::Int },
79    { LAZY_BITFIELD, TR_PREFS_KEY_LAZY_BITFIELD, QVariant::Bool },
80    { MSGLEVEL, TR_PREFS_KEY_MSGLEVEL, QVariant::Int },
81    { OPEN_FILE_LIMIT, TR_PREFS_KEY_OPEN_FILE_LIMIT, QVariant::Int },
82    { PEER_LIMIT_GLOBAL, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, QVariant::Int },
83    { PEER_LIMIT_TORRENT, TR_PREFS_KEY_PEER_LIMIT_TORRENT, QVariant::Int },
84    { PEER_PORT, TR_PREFS_KEY_PEER_PORT, QVariant::Int },
85    { PEER_PORT_RANDOM_ON_START, TR_PREFS_KEY_PEER_PORT_RANDOM_ON_START, QVariant::Bool },
86    { PEER_PORT_RANDOM_LOW, TR_PREFS_KEY_PEER_PORT_RANDOM_LOW, QVariant::Int },
87    { PEER_PORT_RANDOM_HIGH, TR_PREFS_KEY_PEER_PORT_RANDOM_HIGH, QVariant::Int },
88    { SOCKET_TOS, TR_PREFS_KEY_PEER_SOCKET_TOS, QVariant::Int },
89    { PEX_ENABLED, TR_PREFS_KEY_PEX_ENABLED, QVariant::Bool },
90    { PORT_FORWARDING, TR_PREFS_KEY_PORT_FORWARDING, QVariant::Bool },
91    { PROXY_AUTH_ENABLED, TR_PREFS_KEY_PROXY_AUTH_ENABLED, QVariant::Bool },
92    { PREALLOCATION, TR_PREFS_KEY_PREALLOCATION, QVariant::Int },
93    { PROXY_ENABLED, TR_PREFS_KEY_PROXY_ENABLED, QVariant::Bool },
94    { PROXY_PASSWORD, TR_PREFS_KEY_PROXY_PASSWORD, QVariant::String },
95    { PROXY_PORT, TR_PREFS_KEY_PROXY_PORT, QVariant::Int },
96    { PROXY, TR_PREFS_KEY_PROXY, QVariant::String },
97    { PROXY_TYPE, TR_PREFS_KEY_PROXY_TYPE, QVariant::Int },
98    { PROXY_USERNAME, TR_PREFS_KEY_PROXY_USERNAME, QVariant::String },
99    { RATIO, TR_PREFS_KEY_RATIO, QVariant::Double },
100    { RATIO_ENABLED, TR_PREFS_KEY_RATIO_ENABLED, QVariant::Bool },
101    { RPC_AUTH_REQUIRED, TR_PREFS_KEY_RPC_AUTH_REQUIRED, QVariant::Bool },
102    { RPC_ENABLED, TR_PREFS_KEY_RPC_ENABLED, QVariant::Bool },
103    { RPC_PASSWORD, TR_PREFS_KEY_RPC_PASSWORD, QVariant::String },
104    { RPC_PORT, TR_PREFS_KEY_RPC_PORT, QVariant::Int },
105    { RPC_USERNAME, TR_PREFS_KEY_RPC_USERNAME, QVariant::String },
106    { RPC_WHITELIST_ENABLED, TR_PREFS_KEY_RPC_WHITELIST_ENABLED, QVariant::Bool },
107    { RPC_WHITELIST, TR_PREFS_KEY_RPC_WHITELIST, QVariant::String },
108    { USPEED_ENABLED, TR_PREFS_KEY_USPEED_ENABLED, QVariant::Bool },
109    { USPEED, TR_PREFS_KEY_USPEED, QVariant::Int },
110    { UPLOAD_SLOTS_PER_TORRENT, TR_PREFS_KEY_UPLOAD_SLOTS_PER_TORRENT, QVariant::Int }
111};
112
113/***
114****
115***/
116
117Prefs :: Prefs( const char * configDir ):
118    myConfigDir( configDir )
119{
120    assert( sizeof(myItems) / sizeof(myItems[0]) == PREFS_COUNT );
121    for( int i=0; i<PREFS_COUNT; ++i )
122        assert( myItems[i].id == i );
123
124    tr_benc top;
125    tr_bencInitDict( &top, 0 );
126    initDefaults( &top );
127    tr_sessionLoadSettings( &top, configDir, NULL );
128    for( int i=0; i<PREFS_COUNT; ++i )
129    {
130        double d;
131        tr_bool boolVal;
132        int64_t intVal;
133        const char * str;
134        tr_benc * b( tr_bencDictFind( &top, myItems[i].key ) );
135
136        switch( myItems[i].type )
137        {
138            case QVariant::Int:
139                if( tr_bencGetInt( b, &intVal ) )
140                    myValues[i].setValue( qlonglong(intVal) );
141                break;
142            case TrTypes::SortModeType:
143                if( tr_bencGetStr( b, &str ) )
144                    myValues[i] = QVariant::fromValue( SortMode( str ) );
145                break;
146            case TrTypes::FilterModeType:
147                if( tr_bencGetStr( b, &str ) )
148                    myValues[i] = QVariant::fromValue( FilterMode( str ) );
149                break;
150            case QVariant::String:
151                if( tr_bencGetStr( b, &str ) )
152                    myValues[i].setValue( QString::fromUtf8(str) );
153                break;
154            case QVariant::Bool:
155                if( tr_bencGetBool( b, &boolVal ) )
156                    myValues[i].setValue( bool(boolVal) );
157                break;
158            case QVariant::Double:
159                if( tr_bencGetReal( b, &d ) )
160                    myValues[i].setValue( d );
161                break;
162            case QVariant::DateTime:
163                if( tr_bencGetInt( b, &intVal ) )
164                    myValues[i].setValue( QDateTime :: fromTime_t( intVal ) );
165                break;
166            default:
167                assert( "unhandled type" && 0 );
168                break;
169        }
170    }
171
172    tr_bencFree( &top );
173}
174
175Prefs :: ~Prefs( )
176{
177    tr_benc top;
178
179    /* load in the existing preferences file */
180    QFile file( QDir( myConfigDir ).absoluteFilePath( "settings.json" ) );
181    file.open( QIODevice::ReadOnly | QIODevice::Text );
182    const QByteArray oldPrefs = file.readAll( );
183    file.close( );
184    if( tr_jsonParse( oldPrefs.data(), oldPrefs.length(), &top, NULL ) )
185        tr_bencInitDict( &top, PREFS_COUNT );
186
187    /* merge our own settings with the ones already in the file */
188    for( int i=0; i<PREFS_COUNT; ++i ) {
189        const char * key = myItems[i].key;
190        const QVariant& val = myValues[i];
191        switch( myItems[i].type ) {
192            case QVariant::Int:
193                tr_bencDictAddInt( &top, key, val.toInt() );
194                break;
195            case TrTypes::SortModeType:
196                tr_bencDictAddStr( &top, key, val.value<SortMode>().name().toUtf8().constData() );
197                break;
198            case TrTypes::FilterModeType:
199                tr_bencDictAddStr( &top, key, val.value<FilterMode>().name().toUtf8().constData() );
200                break;
201            case QVariant::String:
202                tr_bencDictAddStr( &top, key, val.toString().toUtf8().constData() );
203                break;
204            case QVariant::Bool:
205                tr_bencDictAddBool( &top, key, val.toBool() );
206                break;
207            case QVariant::Double:
208                tr_bencDictAddReal( &top, key, val.toDouble() );
209                break;
210            case QVariant::DateTime:
211                tr_bencDictAddInt( &top, key, val.toDateTime().toTime_t() );
212                break;
213            default:
214                assert( "unhandled type" && 0 );
215                break;
216        }
217    }
218
219    /* write back out the serialized preferences */
220    char * json = tr_bencToJSON( &top );
221    if( json && *json ) {
222        file.open( QIODevice::WriteOnly | QIODevice::Text );
223        file.write( json );
224        file.close( );
225    }
226    tr_free( json );
227    tr_bencFree( &top );
228}
229
230/**
231 * This is where we initialize the preferences file with the default values.
232 * If you add a new preferences key, you /must/ add a default value here.
233 */
234void
235Prefs :: initDefaults( tr_benc * d )
236{
237    tr_bencDictAddStr( d, keyStr(DIR_WATCH), tr_getDefaultDownloadDir( ) );
238    tr_bencDictAddInt( d, keyStr(DIR_WATCH_ENABLED), false );
239    tr_bencDictAddInt( d, keyStr(INHIBIT_HIBERNATION), false );
240    tr_bencDictAddInt( d, keyStr(BLOCKLIST_DATE), 0 );
241    tr_bencDictAddInt( d, keyStr(BLOCKLIST_UPDATES_ENABLED), true );
242    tr_bencDictAddStr( d, keyStr(OPEN_DIALOG_FOLDER), QDir::home().absolutePath().toLatin1() );
243    tr_bencDictAddInt( d, keyStr(TOOLBAR), true );
244    tr_bencDictAddInt( d, keyStr(FILTERBAR), true );
245    tr_bencDictAddInt( d, keyStr(STATUSBAR), true );
246    tr_bencDictAddInt( d, keyStr(SHOW_TRAY_ICON), false );
247    tr_bencDictAddInt( d, keyStr(SHOW_DESKTOP_NOTIFICATION), true );
248    tr_bencDictAddStr( d, keyStr(STATUSBAR_STATS), "total-ratio" );
249    tr_bencDictAddInt( d, keyStr(OPTIONS_PROMPT), true );
250    tr_bencDictAddInt( d, keyStr(MAIN_WINDOW_HEIGHT), 500 );
251    tr_bencDictAddInt( d, keyStr(MAIN_WINDOW_WIDTH), 300 );
252    tr_bencDictAddInt( d, keyStr(MAIN_WINDOW_X), 50 );
253    tr_bencDictAddInt( d, keyStr(MAIN_WINDOW_Y), 50 );
254    tr_bencDictAddStr( d, keyStr(FILTER_MODE), "all" );
255    tr_bencDictAddStr( d, keyStr(MAIN_WINDOW_LAYOUT_ORDER), "menu,toolbar,filter,list,statusbar" );
256    tr_bencDictAddStr( d, keyStr(DOWNLOAD_DIR), tr_getDefaultDownloadDir( ) );
257    tr_bencDictAddInt( d, keyStr(ASKQUIT), true );
258    tr_bencDictAddStr( d, keyStr(SORT_MODE), "sort-by-name" );
259    tr_bencDictAddInt( d, keyStr(SORT_REVERSED), false );
260    tr_bencDictAddInt( d, keyStr(MINIMAL_VIEW), false );
261    tr_bencDictAddInt( d, keyStr(START), true );
262    tr_bencDictAddInt( d, keyStr(TRASH_ORIGINAL), false );
263    tr_bencDictAddStr( d, keyStr(SESSION_REMOTE_HOST), "localhost" );
264    tr_bencDictAddInt( d, keyStr(SESSION_REMOTE_PORT), atoi(TR_DEFAULT_RPC_PORT_STR) );
265    tr_bencDictAddBool( d, keyStr(SESSION_IS_REMOTE), false );
266    tr_bencDictAddBool( d, keyStr(SESSION_REMOTE_AUTH), false );
267}
268
269/***
270****
271***/
272
273bool
274Prefs :: getBool( int key ) const
275{
276    assert( myItems[key].type == QVariant::Bool );
277    return myValues[key].toBool( );
278}
279
280QString
281Prefs :: getString( int key ) const
282{
283    assert( myItems[key].type == QVariant::String );
284    return myValues[key].toString( );
285}
286
287int
288Prefs :: getInt( int key ) const
289{
290    assert( myItems[key].type == QVariant::Int );
291    return myValues[key].toInt( );
292}
293
294double
295Prefs :: getDouble( int key ) const
296{
297    assert( myItems[key].type == QVariant::Double );
298    return myValues[key].toDouble( );
299}
300
301QDateTime
302Prefs :: getDateTime( int key ) const
303{
304    assert( myItems[key].type == QVariant::DateTime );
305    return myValues[key].toDateTime( );
306}
307
308/***
309****
310***/
311
312void
313Prefs :: toggleBool( int key )
314{
315    set( key, !getBool( key ) );
316}
Note: See TracBrowser for help on using the repository browser.