Imported Upstream version 3.2.2
[debian/gnuradio] / grc / gui / Preferences.py
1 """
2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19
20 import ConfigParser
21 import os
22
23 _platform = None
24 _config_parser = ConfigParser.ConfigParser()
25
26 def file_extension(): return '.'+_platform.get_key()
27 def _prefs_file(): return os.path.join(os.path.expanduser('~'), file_extension())
28
29 def load(platform):
30         global _platform
31         _platform = platform
32         #create sections
33         _config_parser.add_section('main')
34         _config_parser.add_section('files_open')
35         try: _config_parser.read(_prefs_file())
36         except: pass
37 def save():
38         try: _config_parser.write(open(_prefs_file(), 'w'))
39         except: pass
40
41 ###########################################################################
42 # Special methods for specific program functionalities
43 ###########################################################################
44
45 def main_window_size(size=None):
46         if size is not None:
47                 _config_parser.set('main', 'main_window_width', size[0])
48                 _config_parser.set('main', 'main_window_height', size[1])
49         else:
50                 try: return (
51                         _config_parser.getint('main', 'main_window_width'),
52                         _config_parser.getint('main', 'main_window_height'),
53                 )
54                 except: return (1, 1)
55
56 def file_open(file=None):
57         if file is not None: _config_parser.set('main', 'file_open', file)
58         else:
59                 try: return _config_parser.get('main', 'file_open')
60                 except: return ''
61
62 def files_open(files=None):
63         if files is not None:
64                 _config_parser.remove_section('files_open') #clear section
65                 _config_parser.add_section('files_open')
66                 for i, file in enumerate(files):
67                         _config_parser.set('files_open', 'file_open_%d'%i, file)
68         else:
69                 files = list()
70                 i = 0
71                 while True:
72                         try: files.append(_config_parser.get('files_open', 'file_open_%d'%i))
73                         except: return files
74                         i = i + 1
75
76 def reports_window_position(pos=None):
77         if pos is not None: _config_parser.set('main', 'reports_window_position', pos)
78         else:
79                 try: return _config_parser.getint('main', 'reports_window_position') or 1 #greater than 0
80                 except: return -1
81
82 def blocks_window_position(pos=None):
83         if pos is not None: _config_parser.set('main', 'blocks_window_position', pos)
84         else:
85                 try: return _config_parser.getint('main', 'blocks_window_position') or 1 #greater than 0
86                 except: return -1