1 /*
2  * This file Copyright (C) 2009-2015 Mnemosyne LLC
3  *
4  * It may be used under the GNU GPL versions 2 or 3
5  * or any future license endorsed by Mnemosyne LLC.
6  *
7  */
8 
9 #pragma once
10 
11 #include <QObject>
12 #include <QSet>
13 #include <QString>
14 #include <QVariant>
15 
16 #include <libtransmission/quark.h>
17 
18 #include "Filters.h"
19 
20 class QDateTime;
21 
22 extern "C"
23 {
24 struct tr_variant;
25 }
26 
27 class Prefs : public QObject
28 {
29     Q_OBJECT
30 
31 public:
32     enum
33     {
34         /* client prefs */
35         OPTIONS_PROMPT,
36         OPEN_DIALOG_FOLDER,
37         INHIBIT_HIBERNATION,
38         DIR_WATCH,
39         DIR_WATCH_ENABLED,
40         SHOW_TRAY_ICON,
41         START_MINIMIZED,
42         SHOW_NOTIFICATION_ON_ADD,
43         SHOW_NOTIFICATION_ON_COMPLETE,
44         ASKQUIT,
45         SORT_MODE,
46         SORT_REVERSED,
47         COMPACT_VIEW,
48         FILTERBAR,
49         STATUSBAR,
50         STATUSBAR_STATS,
51         SHOW_TRACKER_SCRAPES,
52         SHOW_BACKUP_TRACKERS,
53         TOOLBAR,
54         BLOCKLIST_DATE,
55         BLOCKLIST_UPDATES_ENABLED,
56         MAIN_WINDOW_LAYOUT_ORDER,
57         MAIN_WINDOW_HEIGHT,
58         MAIN_WINDOW_WIDTH,
59         MAIN_WINDOW_X,
60         MAIN_WINDOW_Y,
61         FILTER_MODE,
62         FILTER_TRACKERS,
63         FILTER_TEXT,
64         SESSION_IS_REMOTE,
65         SESSION_REMOTE_HOST,
66         SESSION_REMOTE_PORT,
67         SESSION_REMOTE_AUTH,
68         SESSION_REMOTE_USERNAME,
69         SESSION_REMOTE_PASSWORD,
70         COMPLETE_SOUND_COMMAND,
71         COMPLETE_SOUND_ENABLED,
72         USER_HAS_GIVEN_INFORMED_CONSENT,
73         /* core prefs */
74         FIRST_CORE_PREF,
75         ALT_SPEED_LIMIT_UP = FIRST_CORE_PREF,
76         ALT_SPEED_LIMIT_DOWN,
77         ALT_SPEED_LIMIT_ENABLED,
78         ALT_SPEED_LIMIT_TIME_BEGIN,
79         ALT_SPEED_LIMIT_TIME_END,
80         ALT_SPEED_LIMIT_TIME_ENABLED,
81         ALT_SPEED_LIMIT_TIME_DAY,
82         BLOCKLIST_ENABLED,
83         BLOCKLIST_URL,
84         DSPEED,
85         DSPEED_ENABLED,
86         DOWNLOAD_DIR,
87         DOWNLOAD_QUEUE_ENABLED,
88         DOWNLOAD_QUEUE_SIZE,
89         ENCRYPTION,
90         IDLE_LIMIT,
91         IDLE_LIMIT_ENABLED,
92         INCOMPLETE_DIR,
93         INCOMPLETE_DIR_ENABLED,
94         MSGLEVEL,
95         PEER_LIMIT_GLOBAL,
96         PEER_LIMIT_TORRENT,
97         PEER_PORT,
98         PEER_PORT_RANDOM_ON_START,
99         PEER_PORT_RANDOM_LOW,
100         PEER_PORT_RANDOM_HIGH,
101         QUEUE_STALLED_MINUTES,
102         SCRIPT_TORRENT_DONE_ENABLED,
103         SCRIPT_TORRENT_DONE_FILENAME,
104         SOCKET_TOS,
105         START,
106         TRASH_ORIGINAL,
107         PEX_ENABLED,
108         DHT_ENABLED,
109         UTP_ENABLED,
110         LPD_ENABLED,
111         PORT_FORWARDING,
112         PREALLOCATION,
113         RATIO,
114         RATIO_ENABLED,
115         RENAME_PARTIAL_FILES,
116         RPC_AUTH_REQUIRED,
117         RPC_ENABLED,
118         RPC_PASSWORD,
119         RPC_PORT,
120         RPC_USERNAME,
121         RPC_WHITELIST_ENABLED,
122         RPC_WHITELIST,
123         USPEED_ENABLED,
124         USPEED,
125         UPLOAD_SLOTS_PER_TORRENT,
126         LAST_CORE_PREF = UPLOAD_SLOTS_PER_TORRENT,
127         //
128         PREFS_COUNT
129     };
130 
131 public:
132     Prefs(QString const& configDir);
133     virtual ~Prefs();
134 
isCore(int key)135     bool isCore(int key) const
136     {
137         return FIRST_CORE_PREF <= key && key <= LAST_CORE_PREF;
138     }
139 
isClient(int key)140     bool isClient(int key) const
141     {
142         return !isCore(key);
143     }
144 
keyStr(int i)145     char const* keyStr(int i) const
146     {
147         return tr_quark_get_string(myItems[i].key, nullptr);
148     }
149 
getKey(int i)150     tr_quark getKey(int i) const
151     {
152         return myItems[i].key;
153     }
154 
type(int i)155     int type(int i) const
156     {
157         return myItems[i].type;
158     }
159 
variant(int i)160     QVariant const& variant(int i) const
161     {
162         return myValues[i];
163     }
164 
165     int getInt(int key) const;
166     bool getBool(int key) const;
167     QString getString(int key) const;
168     double getDouble(int key) const;
169     QDateTime getDateTime(int key) const;
170 
171     template<typename T>
get(int key)172     T get(int key) const
173     {
174         return myValues[key].value<T>();
175     }
176 
177     template<typename T>
set(int key,T const & value)178     void set(int key, T const& value)
179     {
180         QVariant& v(myValues[key]);
181         QVariant const tmp = QVariant::fromValue(value);
182 
183         if (v.isNull() || v != tmp)
184         {
185             v = tmp;
186             emit changed(key);
187         }
188     }
189 
190     void toggleBool(int key);
191 
192 signals:
193     void changed(int key);
194 
195 private:
196     struct PrefItem
197     {
198         int id;
199         tr_quark key;
200         int type;
201     };
202 
203 private:
204     void initDefaults(tr_variant*);
205 
206     // Intentionally not implemented
207     void set(int key, char const* value);
208 
209 private:
210     QString const myConfigDir;
211 
212     QSet<int> myTemporaryPrefs;
213     QVariant mutable myValues[PREFS_COUNT];
214 
215     static PrefItem myItems[];
216 };
217