Thursday, May 2, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 665  / 2 Years ago, fri, may 20, 2022, 7:39:05

I am looking at creating some Ubuntu applications, but finding good resoures are hard.



I am using the quickly toolkit, but would really like some more insight. How does one normally store application preferences and settings in Linux / Ubuntu.



Is it as simple as creating a XML file and saving the information and then reading from said file on application bootstrap?



If someone can point me in a direction it would be greatly appreciated.



EDIT



This is something I wrote whilst waiting for a reply. Probably exactly what preferences does, but just coded out. You might find it usefull:





import ConfigParser, os # We need to be able to store and recal settings

#first try to read the config.cfg file
config = ConfigParser.RawConfigParser()
configFile = 'data/config.cfg'

# We need to set some defaults
monState = False
tueState = False
wedState = False
thurState = False
friState = False
satState = False
sunState = False

# Creating the Config file
def createConfigFile(fileName):
print "CREATING CONFIG" # not needed, but nice for debugging
config.add_section('Preferences')
config.set('Preferences', 'mon', False)
config.set('Preferences', 'tues', False)
config.set('Preferences', 'wed', False)
config.set('Preferences', 'thur', False)
config.set('Preferences', 'fri', False)
config.set('Preferences', 'sat', False)
config.set('Preferences', 'sun', False)
rewriteConfigFile(filename)

# After reading the config file, we can update configs in memory.
# But this will save it to file for when the application is started up again.
def rewriteConfigFile(filename):
with open(filename, 'wb') as configfile:
config.write(configfile)

# Reading the config file
def readConfigFile(fileName):
print "READING CONFIG" # not needed, but nice for debugging
global monState, tueState, wedState, thurState, friState, satState, sunState
monState = config.getboolean('Preferences', 'mon')
tueState = config.getboolean('Preferences', 'tues')
wedState = config.getboolean('Preferences', 'wed')
thurState = config.getboolean('Preferences', 'thur')
friState = config.getboolean('Preferences', 'fri')
satState = config.getboolean('Preferences', 'sat')
sunState = config.getboolean('Preferences', 'sun')

# If the config does not exist, create it, then read it. Otherwise simply read it
if not config.read(configFile):
createConfigFile(configFile)
readConfigFile(configFile)
else:
readConfigFile(configFile)

More From » application-development

 Answers
3

Quickly applications use the glib schemas for application preferences. A default schema is created in data/glib-2.0/schemas. It's an xml file called net.launchpad.XXXX.gschema.xml where XXXX is your application name.



Here's an example entry:



<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="drawers">
<schema id="net.launchpad.drawers" path="/net/launchpad/drawers/">
<key name="maxicons-row" type="i">
<range min="1" max="15"/>
<default>5</default>
<summary>Maximum Icons per Row</summary>
<description>Minimum value = 1 Max=15</description>
</key>
</schema>
</schemalist>


Key's can be integers (type="i"), boolean (type="b"), or floats (type="d"). Use only lower case and - in key names.



To access the settings and bind them to widgets, you can get them as follows (taken from the PreferencesXXXXWindow.py generated by quickly):



def finish_initializing(self, builder):# pylint: disable=E1002
"""Set up the preferences dialog"""
super(PreferencesDrawersDialog, self).finish_initializing(builder)

# Bind each preference widget to gsettings
self.settings = Gio.Settings("net.launchpad.drawers")
widget = self.builder.get_object('maxicons_row')
self.settings.bind("maxicons-row", widget, "value", Gio.SettingsBindFlags.DEFAULT)


To read values for variables within a program you can do the following:



from gi.repository import Gio
settings = Gio.Settings("net.launchpad.drawers")
integer=settings.get_int("intsetting")
float = settings.get_double("floatsetting")
bool = settings.get_boolean("booleansetting")


Hope that helps.


[#34421] Friday, May 20, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bearous

Total Points: 226
Total Questions: 116
Total Answers: 136

Location: Guernsey
Member since Sun, Jan 10, 2021
3 Years ago
;