2009-09-08 19:58:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
2008-10-23 19:49:13 +00:00
|
|
|
|
2009-09-08 19:58:05 +00:00
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2009-12-31 12:52:01 +00:00
|
|
|
# Copyright (c) 2008-2010 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
|
2010-03-21 23:58:01 +00:00
|
|
|
# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin #
|
|
|
|
# Thompson, Jon Tibble, Carsten Tinggaard #
|
2009-09-08 19:58:05 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# 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., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
2010-02-21 06:32:43 +00:00
|
|
|
|
|
|
|
import os
|
2010-02-21 12:43:03 +00:00
|
|
|
import sys
|
2009-10-12 04:43:02 +00:00
|
|
|
import logging
|
|
|
|
import urllib2
|
|
|
|
from datetime import datetime
|
2008-10-23 19:49:13 +00:00
|
|
|
|
2010-03-27 19:59:35 +00:00
|
|
|
from PyQt4 import QtCore
|
|
|
|
|
2010-03-18 21:36:30 +00:00
|
|
|
import openlp
|
|
|
|
|
2009-10-14 17:52:50 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2010-02-21 12:43:03 +00:00
|
|
|
class AppLocation(object):
|
|
|
|
"""
|
|
|
|
Retrieve a directory based on the directory type.
|
|
|
|
"""
|
|
|
|
AppDir = 1
|
|
|
|
ConfigDir = 2
|
|
|
|
DataDir = 3
|
|
|
|
PluginsDir = 4
|
2010-04-10 22:24:22 +00:00
|
|
|
VersionDir = 5
|
2010-02-21 12:43:03 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2010-04-10 22:24:22 +00:00
|
|
|
def get_directory(dir_type=1):
|
2010-03-21 22:16:43 +00:00
|
|
|
"""
|
|
|
|
Return the appropriate directory according to the directory type.
|
|
|
|
|
|
|
|
``dir_type``
|
|
|
|
The directory type you want, for instance the data directory.
|
|
|
|
"""
|
2010-02-21 12:43:03 +00:00
|
|
|
if dir_type == AppLocation.AppDir:
|
2010-03-18 15:43:27 +00:00
|
|
|
return os.path.abspath(os.path.split(sys.argv[0])[0])
|
2010-02-21 12:43:03 +00:00
|
|
|
elif dir_type == AppLocation.ConfigDir:
|
2010-03-12 18:31:13 +00:00
|
|
|
if sys.platform == u'win32':
|
2010-02-21 12:43:03 +00:00
|
|
|
path = os.path.join(os.getenv(u'APPDATA'), u'openlp')
|
2010-03-12 18:31:13 +00:00
|
|
|
elif sys.platform == u'darwin':
|
2010-02-21 12:43:03 +00:00
|
|
|
path = os.path.join(os.getenv(u'HOME'), u'Library',
|
|
|
|
u'Application Support', u'openlp')
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
from xdg import BaseDirectory
|
2010-04-23 16:00:32 +00:00
|
|
|
path = os.path.join(
|
|
|
|
BaseDirectory.xdg_config_home, u'openlp')
|
2010-02-21 12:43:03 +00:00
|
|
|
except ImportError:
|
|
|
|
path = os.path.join(os.getenv(u'HOME'), u'.openlp')
|
|
|
|
return path
|
|
|
|
elif dir_type == AppLocation.DataDir:
|
2010-03-12 18:31:13 +00:00
|
|
|
if sys.platform == u'win32':
|
2010-02-21 12:43:03 +00:00
|
|
|
path = os.path.join(os.getenv(u'APPDATA'), u'openlp', u'data')
|
2010-03-12 18:31:13 +00:00
|
|
|
elif sys.platform == u'darwin':
|
2010-02-21 12:43:03 +00:00
|
|
|
path = os.path.join(os.getenv(u'HOME'), u'Library',
|
|
|
|
u'Application Support', u'openlp', u'Data')
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
from xdg import BaseDirectory
|
|
|
|
path = os.path.join(BaseDirectory.xdg_data_home, u'openlp')
|
|
|
|
except ImportError:
|
|
|
|
path = os.path.join(os.getenv(u'HOME'), u'.openlp', u'data')
|
|
|
|
return path
|
|
|
|
elif dir_type == AppLocation.PluginsDir:
|
2010-03-18 15:43:27 +00:00
|
|
|
plugin_path = None
|
2010-02-21 12:43:03 +00:00
|
|
|
app_path = os.path.abspath(os.path.split(sys.argv[0])[0])
|
2010-04-10 22:24:22 +00:00
|
|
|
if hasattr(sys, u'frozen') and sys.frozen == 1:
|
2010-03-18 15:43:27 +00:00
|
|
|
plugin_path = os.path.join(app_path, u'plugins')
|
2010-02-21 12:43:03 +00:00
|
|
|
else:
|
2010-03-18 15:52:37 +00:00
|
|
|
plugin_path = os.path.join(
|
|
|
|
os.path.split(openlp.__file__)[0], u'plugins')
|
2010-03-18 15:43:27 +00:00
|
|
|
return plugin_path
|
2010-04-10 22:24:22 +00:00
|
|
|
elif dir_type == AppLocation.VersionDir:
|
|
|
|
if hasattr(sys, u'frozen') and sys.frozen == 1:
|
|
|
|
plugin_path = os.path.abspath(os.path.split(sys.argv[0])[0])
|
|
|
|
else:
|
|
|
|
plugin_path = os.path.split(openlp.__file__)[0]
|
|
|
|
return plugin_path
|
2010-02-21 12:43:03 +00:00
|
|
|
|
|
|
|
|
2009-10-12 04:43:02 +00:00
|
|
|
def check_latest_version(config, current_version):
|
2010-03-21 22:16:43 +00:00
|
|
|
"""
|
|
|
|
Check the latest version of OpenLP against the version file on the OpenLP
|
|
|
|
site.
|
|
|
|
|
|
|
|
``config``
|
|
|
|
The OpenLP config object.
|
|
|
|
|
|
|
|
``current_version``
|
|
|
|
The current version of OpenLP.
|
|
|
|
"""
|
2010-03-27 12:37:21 +00:00
|
|
|
version_string = current_version[u'full']
|
2010-02-05 19:08:47 +00:00
|
|
|
#set to prod in the distribution confif file.
|
2009-11-30 18:29:22 +00:00
|
|
|
last_test = config.get_config(u'last version test', datetime.now().date())
|
|
|
|
this_test = unicode(datetime.now().date())
|
|
|
|
config.set_config(u'last version test', this_test)
|
|
|
|
if last_test != this_test:
|
2009-10-12 04:43:02 +00:00
|
|
|
version_string = u''
|
2010-03-27 12:37:21 +00:00
|
|
|
if current_version[u'build']:
|
2010-04-23 16:00:32 +00:00
|
|
|
req = urllib2.Request(
|
|
|
|
u'http://www.openlp.org/files/dev_version.txt')
|
2010-03-27 12:37:21 +00:00
|
|
|
else:
|
|
|
|
req = urllib2.Request(u'http://www.openlp.org/files/version.txt')
|
|
|
|
req.add_header(u'User-Agent', u'OpenLP/%s' % current_version[u'full'])
|
2009-10-12 04:43:02 +00:00
|
|
|
try:
|
2010-03-27 12:37:21 +00:00
|
|
|
version_string = unicode(urllib2.urlopen(req, None).read()).strip()
|
2009-10-12 04:43:02 +00:00
|
|
|
except IOError, e:
|
|
|
|
if hasattr(e, u'reason'):
|
|
|
|
log.exception(u'Reason for failure: %s', e.reason)
|
|
|
|
return version_string
|
2010-02-21 06:32:43 +00:00
|
|
|
|
2010-03-27 19:59:35 +00:00
|
|
|
def string_to_unicode(string):
|
|
|
|
"""
|
|
|
|
Converts a QString to a Python unicode object.
|
|
|
|
"""
|
|
|
|
if isinstance(string, QtCore.QString):
|
|
|
|
string = unicode(string.toUtf8(), u'utf8')
|
|
|
|
return string
|
|
|
|
|
2010-03-21 22:16:43 +00:00
|
|
|
def variant_to_unicode(variant):
|
|
|
|
"""
|
2010-03-27 19:59:35 +00:00
|
|
|
Converts a QVariant to a Python unicode object.
|
2010-03-21 22:16:43 +00:00
|
|
|
|
|
|
|
``variant``
|
|
|
|
The QVariant instance to convert to unicode.
|
|
|
|
"""
|
2010-03-27 19:59:35 +00:00
|
|
|
if isinstance(variant, QtCore.QVariant):
|
|
|
|
string = variant.toString()
|
2010-03-21 22:16:43 +00:00
|
|
|
if not isinstance(string, unicode):
|
2010-03-27 19:59:35 +00:00
|
|
|
string = string_to_unicode(string)
|
2010-03-21 22:16:43 +00:00
|
|
|
return string
|
|
|
|
|
2010-04-23 16:00:32 +00:00
|
|
|
def add_actions(target, actions):
|
|
|
|
"""
|
|
|
|
Adds multiple actions to a menu or toolbar in one command.
|
|
|
|
|
|
|
|
``target``
|
|
|
|
The menu or toolbar to add actions to.
|
|
|
|
|
|
|
|
``actions``
|
|
|
|
The actions to be added.
|
|
|
|
"""
|
|
|
|
for action in actions:
|
|
|
|
if action is None:
|
|
|
|
target.addSeparator()
|
|
|
|
else:
|
|
|
|
target.addAction(action)
|
|
|
|
|
2010-02-21 06:32:43 +00:00
|
|
|
from registry import Registry
|
|
|
|
from confighelper import ConfigHelper
|
|
|
|
|
2010-04-23 16:00:32 +00:00
|
|
|
__all__ = [u'Registry', u'ConfigHelper', u'AppLocation',
|
|
|
|
u'check_latest_version', u'add_actions']
|