summaryrefslogtreecommitdiffstats
path: root/src/pref.py
blob: edd0b87ed2c3e0612036c7bf733a4980fb54a410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# -*- coding: utf-8 -*-
# pref.py
#
# Copyright (C) Zach Tibbitts 2006 <zach@collegegeek.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, write to:
#     The Free Software Foundation, Inc.,
#     51 Franklin Street, Fifth Floor
#     Boston, MA  02110-1301, USA.
#
#  In addition, as a special exception, the copyright holders give
#  permission to link the code of portions of this program with the OpenSSL
#  library.
#  You must obey the GNU General Public License in all respects for all of
#  the code used other than OpenSSL. If you modify file(s) with this
#  exception, you may extend this exception to your version of the file(s),
#  but you are not obligated to do so. If you do not wish to do so, delete
#  this exception statement from your version. If you delete this exception
#  statement from all source files in the program, then also delete it here.


# Preferences is basically a wrapper around a simple Python dictionary
# object.  However, this class provides a few extra features on top of
# the built in class that Deluge can take advantage of.

import cPickle as pickle
import common
import os.path

DEFAULT_PREFS = {
    "enabled_plugins" : "Torrent Files:Torrent Peers",
    "file_manager" : common.FileManager.xdg,
    "open_folder_stock" : True,
    "open_folder_location": "",
    "send_info" : True,
    "auto_end_seeding" : False,
    "auto_seed_ratio" : 0,
    "close_to_tray" : False,
    "enable_files_dialog" : False,
    "queue_above_completed" : False,
    "clear_max_ratio_torrents" : False,
    "default_download_path" : os.path.expanduser("~"),
    "open_torrent_dialog_path" : os.path.expanduser("~"),
    "choose_directory_dialog_path": os.path.expanduser("~"),
    "enable_dht" : True,
    "enable_system_tray" : True,
    "enabled_plugins" : "Torrent Files:Torrent Peers",
    "encin_state" : common.EncState.enabled,
    "encout_state" : common.EncState.enabled,
    "enclevel_type" : common.EncLevel.both,
    "end_seed_ratio" : 0.0,
    "gui_update_interval" : 1.0,
    "listen_on" : [6881,6889],
    "lock_tray" : False,
    "max_half_open" : 8,
    "max_active_torrents" : 8,
    "max_connections_global" : 200,
    "max_connections_per_torrent" : -1,
    "max_download_speed" : -1,
    "max_download_speed_bps": -1,
    "max_upload_slots_global" : 4,
    "max_upload_slots_per_torrent" : -1,
    "max_upload_speed" : -1,
    "max_upload_speed_bps" : -1,
    "pref_rc4" : True,
    "prioritize_first_last_pieces" : False,
    "web_proxy_type" : common.ProxyType.none,
    "peer_proxy_type" : common.ProxyType.none,
    "dht_proxy_type" : common.ProxyType.none,
    "tracker_proxy_type" : common.ProxyType.none,
    "peer_proxy" : False,
    "tracker_proxy" : False,
    "dht_proxy" : False,
    "peer_proxy_hostname" : "",
    "peer_proxy_username" : "",
    "peer_proxy_password" : "",
    "peer_proxy_port": 8080,
    "dht_proxy_hostname" : "",
    "dht_proxy_username" : "",
    "dht_proxy_password" : "",
    "dht_proxy_port": 8080,
    "web_proxy_hostname" : "",
    "web_proxy_username" : "",
    "web_proxy_password" : "",
    "web_proxy_port": 8080,
    "tracker_proxy_hostname" : "",
    "tracker_proxy_username" : "",
    "tracker_proxy_password" : "",
    "tracker_proxy_port": 8080,
    "queue_seeds_to_bottom" : False,
    "random_port" : False,
    "show_availability" : True,
    "show_dl" : True,
    "show_eta" : True,
    "show_infopane" : True,
    "show_peers" : True,
    "show_seeders" : True,
    "show_share" : True,
    "show_size" : True,
    "show_status" : True,
    "show_toolbar" : True,
    "show_ul" : True,
    "start_in_tray" : False,
    "tray_downloadspeedlist" : [5.0, 10.0, 30.0, 80.0, 300.0],
    "tray_passwd" : "",
    "tray_uploadspeedlist" : [5.0, 10.0, 30.0, 80.0, 300.0],
    "use_compact_storage" : True,
    "use_default_dir" : False,
    "use_natpmp" : False,
    "use_upnp" : True,
    "use_utpex" : True,
    "new_releases" : True,
    "window_height" : 480,
    "window_maximized" : False,
    "window_pane_position" : -1,
    "window_width" : 640,
    "window_x_pos" : 0,
    "window_y_pos" : 0,
    "availability_width" : 50,
    "queue_width" : 40,
    "name_width" : 175,
    "dl_width" : 110,
    "ul_width" : 110,
    "eta_width" : 65,
    "peer_width" : 85,
    "seed_width" : 85,
    "share_width" : 60,
    "size_width" : 71,
    "status_width" : 150,
    "filename_f_width" : 220,
    "size_f_width" : 90,
    "priority_f_width" : 140
}

class Preferences:
    def __init__(self, filename=None, global_defaults=True, defaults=None):
        self.mapping = {}
        if defaults is not None:
            for key in defaults.keys():
                self.mapping.setdefault(key, defaults[key])
        
        if global_defaults is True:
            self.mapping = DEFAULT_PREFS
                
        self.config_file = filename
        if self.config_file is not None:
            self.load(self.config_file)
    
    # Allows you to access an item in a Preferences objecy by calling
    # instance[key] rather than instance.get(key).  However, this will
    # return the value as the type it is currently in memory, so it is
    # advisable to use get() if you need the value converted.
    def __getitem__(self, key):
        return self.mapping[key]
    
    def __setitem__(self, key, value):
        self.mapping[key] = value
    
    def __delitem__(self, key):
        del self.mapping[key]
    
    def __len__(self):
        return len(self.mapping)
    
    def has_key(self, key): return self.mapping.has_key(key)
    def items(self): return self.mapping.items()
    def keys(self): return self.mapping.keys()
    def values(self): return self.mapping.values()
    
    def save(self, filename=None):
        if filename is None:
            filename = self.config_file
        try:
            pkl_file = open(filename, 'wb')
            pickle.dump(self.mapping, pkl_file)
            pkl_file.close()
        except IOError:
            pass

    def load(self, filename=None):
        if filename is None:
            filename = self.config_file
        try:
            pkl_file = open(filename, 'rb')
            self.dump = pickle.load(pkl_file)
            self.mapping.update(self.dump)
            pkl_file.close()
        except IOError:
            pass
        except EOFError:
            pkl_file.close()
            pass
    
    def set(self, key, value):
        self.mapping[key] = value
    
    def get(self, key):
        try:
            value = self.mapping[key]
            return value
        except KeyError:
            return None
    
    def remove(self, key):
        self.mapping.pop(key)
    
    def clear(self):
        self.mapping.clear()
        
    def printout(self):
        for key in self.mapping.keys():
            print key, ':', self.mapping[key]