openlp/openlp/core/lib/settingsmanager.py

169 lines
6.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2011-12-27 10:33:55 +00:00
# Copyright (c) 2008-2012 Raoul Snyman #
# Portions copyright (c) 2008-2012 Tim Bentley, Gerald Britton, Jonathan #
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
2012-10-21 13:16:22 +00:00
# Meinert Jordan, Armin Köhler, Eric Ludin, Edwin Lunando, Brian T. Meyer, #
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
# Erode Woldsund #
# --------------------------------------------------------------------------- #
# 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-06-19 17:31:42 +00:00
"""
Provide handling for persisting OpenLP settings. OpenLP uses QSettings to
manage settings persistence. QSettings provides a single API for saving and
retrieving settings from the application but writes to disk in an OS dependant
format.
"""
2010-04-27 22:51:16 +00:00
import os
2010-04-26 16:41:31 +00:00
from PyQt4 import QtCore
from openlp.core.lib.settings import Settings
2010-04-27 16:27:57 +00:00
from openlp.core.utils import AppLocation
class SettingsManager(object):
2009-07-08 17:18:48 +00:00
"""
2011-05-07 20:55:11 +00:00
Class to provide helper functions for the loading and saving of application
settings.
2009-07-08 17:18:48 +00:00
"""
2009-09-07 19:14:01 +00:00
2010-04-27 16:27:57 +00:00
@staticmethod
def get_last_dir(section, num=None):
"""
Read the last directory used for plugin.
``section``
2011-02-25 17:05:01 +00:00
The section of code calling the method. This is used in the
2010-04-27 16:27:57 +00:00
settings key.
``num``
Defaults to *None*. A further qualifier.
"""
if num:
name = u'last directory %d' % num
else:
name = u'last directory'
last_dir = unicode(Settings().value(
2010-04-28 14:17:42 +00:00
section + u'/' + name, QtCore.QVariant(u'')).toString())
2010-04-27 16:27:57 +00:00
return last_dir
@staticmethod
def set_last_dir(section, directory, num=None):
"""
Save the last directory used for plugin.
``section``
2011-02-25 17:05:01 +00:00
The section of code calling the method. This is used in the
2010-04-27 16:27:57 +00:00
settings key.
``directory``
The directory being stored in the settings.
``num``
Defaults to *None*. A further qualifier.
"""
if num:
name = u'last directory %d' % num
else:
name = u'last directory'
Settings().setValue(
2010-04-27 16:27:57 +00:00
section + u'/' + name, QtCore.QVariant(directory))
@staticmethod
def set_list(section, name, list):
"""
Save a list to application settings.
``section``
The section of the settings to store this list.
``name``
The name of the list to save.
``list``
The list of values to save.
"""
settings = Settings()
2010-04-28 14:17:42 +00:00
settings.beginGroup(section)
2010-04-27 16:27:57 +00:00
old_count = settings.value(
2010-04-28 14:17:42 +00:00
u'%s count' % name, QtCore.QVariant(0)).toInt()[0]
2010-04-27 16:27:57 +00:00
new_count = len(list)
2010-04-28 14:17:42 +00:00
settings.setValue(u'%s count' % name, QtCore.QVariant(new_count))
2012-04-16 07:02:24 +00:00
for counter in range(new_count):
2010-04-27 16:27:57 +00:00
settings.setValue(
2010-04-28 14:17:42 +00:00
u'%s %d' % (name, counter), QtCore.QVariant(list[counter-1]))
2010-04-27 16:27:57 +00:00
if old_count > new_count:
2010-04-28 01:28:37 +00:00
# Tidy up any old list items
2010-04-27 16:27:57 +00:00
for counter in range(new_count, old_count):
2010-04-28 14:17:42 +00:00
settings.remove(u'%s %d' % (name, counter))
settings.endGroup()
2010-04-27 16:27:57 +00:00
@staticmethod
def load_list(section, name):
"""
Load a list from the config file.
``section``
The section of the settings to load the list from.
``name``
The name of the list.
"""
settings = Settings()
2010-04-28 14:17:42 +00:00
settings.beginGroup(section)
2010-04-27 16:27:57 +00:00
list_count = settings.value(
2010-04-28 14:17:42 +00:00
u'%s count' % name, QtCore.QVariant(0)).toInt()[0]
2010-04-27 16:27:57 +00:00
list = []
if list_count:
2012-04-16 07:02:24 +00:00
for counter in range(list_count):
2010-04-28 14:17:42 +00:00
item = unicode(
settings.value(u'%s %d' % (name, counter)).toString())
2010-04-27 16:27:57 +00:00
if item:
list.append(item)
2010-04-28 14:17:42 +00:00
settings.endGroup()
2010-04-27 16:27:57 +00:00
return list
@staticmethod
2010-04-27 23:49:21 +00:00
def get_files(section=None, extension=None):
2010-04-27 16:27:57 +00:00
"""
Get a list of files from the data files path.
2010-04-27 22:51:16 +00:00
``section``
2011-02-25 17:05:01 +00:00
Defaults to *None*. The section of code getting the files - used
2010-04-27 23:49:21 +00:00
to load from a section's data subdirectory.
2010-04-27 22:51:16 +00:00
2010-04-27 23:49:21 +00:00
``extension``
2011-02-25 17:05:01 +00:00
Defaults to *None*. The extension to search for.
2010-04-27 16:27:57 +00:00
"""
2010-04-27 22:51:16 +00:00
path = AppLocation.get_data_path()
if section:
path = os.path.join(path, section)
2010-04-27 16:27:57 +00:00
try:
2010-04-27 22:51:16 +00:00
files = os.listdir(path)
2010-05-27 14:41:47 +00:00
except OSError:
2010-04-27 16:27:57 +00:00
return []
2010-04-27 23:49:21 +00:00
if extension:
2010-05-27 14:41:47 +00:00
return [filename for filename in files
if extension == os.path.splitext(filename)[1]]
2010-04-27 16:27:57 +00:00
else:
# no filtering required
return files