~ubuntu-branches/ubuntu/natty/miro/natty

« back to all changes in this revision

Viewing changes to platform/windows-xul/plat/config.py

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2011-01-22 02:46:33 UTC
  • mfrom: (1.4.10 upstream) (1.7.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20110122024633-kjme8u93y2il5nmf
Tags: 3.5.1-1ubuntu1
* Merge from debian.  Remaining ubuntu changes:
  - Use python 2.7 instead of python 2.6
  - Relax dependency on python-dbus to >= 0.83.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Miro - an RSS based video player application
2
 
# Copyright (C) 2005-2010 Participatory Culture Foundation
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU General Public License
15
 
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17
 
#
18
 
# In addition, as a special exception, the copyright holders give
19
 
# permission to link the code of portions of this program with the OpenSSL
20
 
# library.
21
 
#
22
 
# You must obey the GNU General Public License in all respects for all of
23
 
# the code used other than OpenSSL. If you modify file(s) with this
24
 
# exception, you may extend this exception to your version of the file(s),
25
 
# but you are not obligated to do so. If you do not wish to do so, delete
26
 
# this exception statement from your version. If you delete this exception
27
 
# statement from all source files in the program, then also delete it here.
28
 
 
29
 
import os
30
 
import _winreg
31
 
import cPickle
32
 
import logging
33
 
import string
34
 
import tempfile
35
 
import traceback
36
 
import shutil
37
 
 
38
 
from miro import app
39
 
from miro import prefs
40
 
from miro import util
41
 
from miro import u3info
42
 
from miro import fileutil
43
 
from miro.plat import proxyfind
44
 
from miro.plat import resources
45
 
from miro.plat import specialfolders
46
 
 
47
 
proxy_info = proxyfind.get_proxy_info()
48
 
 
49
 
def _getSupportDirectory():
50
 
    if u3info.u3_active:
51
 
        path = u3info.APP_DATA_PREFIX
52
 
    else:
53
 
        # We don't get the publisher and long app name from the config so
54
 
        # changing the app name doesn't change the support directory
55
 
        path = os.path.join(specialfolders.appDataDirectory,
56
 
                            u'Participatory Culture Foundation',
57
 
                            u'Miro',
58
 
                            u'Support')
59
 
 
60
 
    try:
61
 
        fileutil.makedirs(path)
62
 
    except:
63
 
        pass
64
 
    return path
65
 
 
66
 
def _getConfigFile():
67
 
    return fileutil.expand_filename(os.path.join(_getSupportDirectory(), "preferences.bin"))
68
 
 
69
 
def load():
70
 
    save_file = _getConfigFile()
71
 
 
72
 
    # if Miro died while saving the config file, then it's likely there's
73
 
    # a save_file.new floating around and that's the one we want to use.
74
 
    new_save_file = save_file + ".new"
75
 
    if os.path.exists(new_save_file):
76
 
        save_file = new_save_file
77
 
 
78
 
    if os.path.exists(save_file):
79
 
        try:
80
 
            return cPickle.load(open(save_file))
81
 
        except:
82
 
            logging.warn("Error loading config: %s", traceback.format_exc())
83
 
    return {}
84
 
 
85
 
def save(data):
86
 
    # save to a new file and if that's successful then rename it.  this
87
 
    # reduces the chance that the user ends up with a hosed preferences
88
 
    # file.
89
 
    save_file = _getConfigFile()
90
 
    new_file = save_file + ".new"
91
 
    try:
92
 
        f = open(new_file, 'w')
93
 
        cPickle.dump(data, f)
94
 
        f.close()
95
 
 
96
 
        if not os.path.exists(save_file):
97
 
            shutil.move(new_file, save_file)
98
 
            return
99
 
 
100
 
        os.remove(save_file)
101
 
        shutil.move(new_file, save_file)
102
 
    except:
103
 
        raise
104
 
 
105
 
def get(descriptor):
106
 
    if descriptor == prefs.MOVIES_DIRECTORY:
107
 
        return os.path.join(specialfolders.baseMoviesDirectory, app.configfile['shortAppName'])
108
 
 
109
 
    elif descriptor == prefs.NON_VIDEO_DIRECTORY:
110
 
        return specialfolders.nonVideoDirectory
111
 
 
112
 
    elif descriptor == prefs.GETTEXT_PATHNAME:
113
 
        return resources.path("locale")
114
 
 
115
 
    elif descriptor == prefs.SUPPORT_DIRECTORY:
116
 
        return fileutil.expand_filename(_getSupportDirectory())
117
 
 
118
 
    elif descriptor == prefs.ICON_CACHE_DIRECTORY:
119
 
        return os.path.join(_getSupportDirectory(), 'icon-cache')
120
 
 
121
 
    elif descriptor == prefs.DB_PATHNAME:
122
 
        path = get(prefs.SUPPORT_DIRECTORY)
123
 
        return os.path.join(path, 'tvdump')
124
 
 
125
 
    elif descriptor == prefs.BSDDB_PATHNAME:
126
 
        path = get(prefs.SUPPORT_DIRECTORY)
127
 
        return os.path.join(path, 'database')
128
 
 
129
 
    elif descriptor == prefs.SQLITE_PATHNAME:
130
 
        path = get(prefs.SUPPORT_DIRECTORY)
131
 
        return os.path.join(path, 'sqlitedb')
132
 
 
133
 
    elif descriptor == prefs.LOG_PATHNAME:
134
 
        if u3info.u3_active:
135
 
            directory = u3info.app_data_path
136
 
        else:
137
 
            directory = tempfile.gettempdir()
138
 
        return os.path.join(directory, 
139
 
                ('%s.log' % app.configfile['shortAppName']))
140
 
 
141
 
    elif descriptor == prefs.DOWNLOADER_LOG_PATHNAME:
142
 
        if u3info.u3_active:
143
 
            directory = u3info.app_data_path
144
 
        else:
145
 
            directory = tempfile.gettempdir()
146
 
        return os.path.join(directory,
147
 
            ('%s-downloader.log' % app.configfile['shortAppName']))
148
 
 
149
 
    elif descriptor == prefs.RUN_AT_STARTUP:
150
 
        import logging
151
 
        # We use the legacy startup registry key, so legacy versions
152
 
        # of Windows have a chance
153
 
        # http://support.microsoft.com/?kbid=270035
154
 
 
155
 
        try:
156
 
            folder = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
157
 
                                     "Software\Microsoft\Windows\CurrentVersion\Run")
158
 
        except WindowsError, e:
159
 
            # 2 indicates that the key doesn't exist yet, so
160
 
            # RUN_AT_STARTUP is clearly False
161
 
            if e.errno == 2:
162
 
                logging.exception("=== windowserror kicked up at open key ===")
163
 
                return False
164
 
            raise
165
 
        long_app_name = app.configfile['longAppName']
166
 
        count = 0
167
 
        while True:
168
 
            try:
169
 
                name, val, type_ = _winreg.EnumValue(folder, count)
170
 
                count += 1
171
 
                if name == long_app_name:
172
 
                    return True                    
173
 
            except WindowsError, e:
174
 
                # 22 indicates there are no more items in this folder to
175
 
                # iterate through.
176
 
                if e.errno == 22:
177
 
                    return False
178
 
                else:
179
 
                    raise
180
 
        return False
181
 
 
182
 
    elif descriptor == prefs.HTTP_PROXY_ACTIVE:
183
 
        return proxy_info.host is not None
184
 
    elif descriptor == prefs.HTTP_PROXY_HOST:
185
 
        return proxy_info.host
186
 
    elif descriptor == prefs.HTTP_PROXY_PORT:
187
 
        return proxy_info.port
188
 
    elif descriptor == prefs.HTTP_PROXY_IGNORE_HOSTS:
189
 
        return poxy_info.ignore_hosts
190
 
    # Proxy authorization isn't suppored on windows, so the following keps are
191
 
    # ignored:
192
 
    # 
193
 
    # HTTP_PROXY_AUTHORIZATION_ACTIVE
194
 
    # HTTP_PROXY_AUTHORIZATION_USERNAME
195
 
    # HTTP_PROXY_AUTHORIZATION_PASSWORD
196
 
    else:
197
 
        return descriptor.default