Read/Write system for Cinnamon preference file
This commit is contained in:
@@ -15,12 +15,15 @@ Based on a location, this extension calculates the periods of a day and switches
|
||||
- Image stretching over multiple displays or repeat image for every display
|
||||
|
||||
### Tested Cinnamon versions
|
||||
- 4.8 (Mint 20.1)
|
||||
- 5.0 (Mint 20.2)
|
||||
- 5.2 (Mint 20.3)
|
||||
- 5.4 (Mint 21)
|
||||
- 5.6 (Mint 21.1)
|
||||
- 5.8 (Mint 21.2)
|
||||
- 6.0 (Mint 21.3)
|
||||
|
||||
#### Only supported with version 1.x
|
||||
- 4.8 (Mint 20.1)
|
||||
- 5.0 (Mint 20.2)
|
||||
- 5.2 (Mint 20.3)
|
||||
|
||||
### Technology
|
||||
- Using `JavaScript` for
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
../5.4/extension.js
|
||||
@@ -1 +0,0 @@
|
||||
../5.4/icons/icon.png
|
||||
@@ -1 +0,0 @@
|
||||
../5.4/icons/
|
||||
@@ -1 +0,0 @@
|
||||
../../5.4/image-configurator/data/
|
||||
@@ -1 +0,0 @@
|
||||
../../5.4/image-configurator/image-configurator.glade
|
||||
@@ -1,6 +0,0 @@
|
||||
import os
|
||||
import windowHandler
|
||||
|
||||
if __name__ == "__main__":
|
||||
wh = windowHandler.WindowHandler(os.path.expanduser("~") + "/.cinnamon/configs/cinnamon-dynamic-wallpaper@TobiZog/cinnamon-dynamic-wallpaper@TobiZog.json")
|
||||
wh.showMainWindow()
|
||||
@@ -1 +0,0 @@
|
||||
../../5.4/image-configurator/windowHandler.py
|
||||
@@ -1 +0,0 @@
|
||||
../5.4/images/
|
||||
@@ -1 +0,0 @@
|
||||
../5.4/scripts/
|
||||
@@ -1 +0,0 @@
|
||||
../5.4/settings-schema.json
|
||||
@@ -0,0 +1,56 @@
|
||||
import os, json
|
||||
from enums.PreferenceEnums import PrefenceEnums
|
||||
|
||||
# Location of the Cinnamon preference file since Cinnamon 5.4
|
||||
pref_location = os.path.expanduser("~") + \
|
||||
"/.config/cinnamon/spices/cinnamon-dynamic-wallpaper@TobiZog/cinnamon-dynamic-wallpaper@TobiZog.json"
|
||||
|
||||
|
||||
def write_to_preferences(parameter: PrefenceEnums, value: str):
|
||||
""" Write a preference value to the JSON file
|
||||
|
||||
Args:
|
||||
parameter (PrefenceEnums): Name of the parameter
|
||||
value (str): Value to write
|
||||
"""
|
||||
with open(pref_location, "r") as pref_file:
|
||||
pref_data = json.load(pref_file)
|
||||
|
||||
if parameter in pref_data:
|
||||
pref_data[parameter]["value"] = value
|
||||
else:
|
||||
pref_data[parameter] = {
|
||||
"type": "entry",
|
||||
"default": "",
|
||||
"description": "",
|
||||
"value": value
|
||||
}
|
||||
|
||||
with open(pref_location, "w") as pref_file:
|
||||
json.dump(pref_data, pref_file, separators=(',', ':'), indent=4)
|
||||
|
||||
|
||||
def read_str_from_preferences(parameter: PrefenceEnums) -> str:
|
||||
""" Read a value from the JSON file
|
||||
|
||||
Args:
|
||||
parameter (PrefenceEnums): Name of the parameter to get
|
||||
|
||||
Returns:
|
||||
str: Value of the parameter
|
||||
"""
|
||||
with open(pref_location, "r") as pref_file:
|
||||
pref_data = json.load(pref_file)
|
||||
|
||||
if parameter in pref_data:
|
||||
return pref_data[parameter]["value"]
|
||||
else:
|
||||
return ""
|
||||
|
||||
def read_int_from_preferences(parameter: PrefenceEnums) -> int:
|
||||
value = read_str_from_preferences(parameter)
|
||||
|
||||
if value == "":
|
||||
return 0
|
||||
else:
|
||||
return int(value)
|
||||
@@ -0,0 +1,4 @@
|
||||
class ImageSourceEnum(enumerate):
|
||||
IMAGESET = "image_set"
|
||||
HEICFILE = "heic_file"
|
||||
SOURCEFOLDER = "source_folder"
|
||||
@@ -0,0 +1,4 @@
|
||||
class PeriodSourceEnum(enumerate):
|
||||
NETWORKLOCATION = "network_location"
|
||||
CUSTOMLOCATION = "custom_location"
|
||||
CUSTOMTIMEPERIODS = "custom_time_periods"
|
||||
@@ -0,0 +1,41 @@
|
||||
class PrefenceEnums(enumerate):
|
||||
EXPANDOVERALLDISPLAY = "expand_over_all_displays"
|
||||
SHOWONLOCKSCREEN = "show_on_lock_screen"
|
||||
|
||||
# Which type of image source will be used
|
||||
# image_set, heic_file, source_folder
|
||||
IMAGESOURCE = "image_source"
|
||||
|
||||
SELECTEDIMAGESET = "selected_image_set"
|
||||
SELECTEDSOURCEFOLDER = "selected_source_folder"
|
||||
|
||||
PERIOD1IMAGE = "period_1_image"
|
||||
PERIOD2IMAGE = "period_2_image"
|
||||
PERIOD3IMAGE = "period_3_image"
|
||||
PERIOD4IMAGE = "period_4_image"
|
||||
PERIOD5IMAGE = "period_5_image"
|
||||
PERIOD6IMAGE = "period_6_image"
|
||||
PERIOD7IMAGE = "period_7_image"
|
||||
PERIOD8IMAGE = "period_8_image"
|
||||
PERIOD9IMAGE = "period_9_image"
|
||||
PERIOD10IMAGE = "period_10_image"
|
||||
|
||||
|
||||
# How the period will estimage
|
||||
# network_location, custom_location, custom_time_periods
|
||||
PERIODSOURCE = "period_source"
|
||||
|
||||
LOCATIONREFRESHINTERVALS = "location_refresh_intervals"
|
||||
LATITUDE = "latitude"
|
||||
LONGITUDE = "longitude"
|
||||
|
||||
PERIOD1STARTTIME = "period_1_start_time"
|
||||
PERIOD2STARTTIME = "period_2_start_time"
|
||||
PERIOD3STARTTIME = "period_3_start_time"
|
||||
PERIOD4STARTTIME = "period_4_start_time"
|
||||
PERIOD5STARTTIME = "period_5_start_time"
|
||||
PERIOD6STARTTIME = "period_6_start_time"
|
||||
PERIOD7STARTTIME = "period_7_start_time"
|
||||
PERIOD8STARTTIME = "period_8_start_time"
|
||||
PERIOD9STARTTIME = "period_9_start_time"
|
||||
PERIOD10STARTTIME = "period_10_start_time"
|
||||
@@ -839,7 +839,7 @@
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkSwitch">
|
||||
<object class="GtkSwitch" id="sw_expand_over_all_displays">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="halign">end</property>
|
||||
@@ -880,7 +880,7 @@
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkSwitch">
|
||||
<object class="GtkSwitch" id="sw_show_on_lock_screen">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="halign">end</property>
|
||||
@@ -1156,7 +1156,7 @@
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkSpinButton">
|
||||
<object class="GtkSpinButton" id="spb_network_location_refresh_time">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
<property name="adjustment">adjustment1</property>
|
||||
@@ -1198,7 +1198,7 @@
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkEntry">
|
||||
<object class="GtkEntry" id="etr_latitude">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
</object>
|
||||
@@ -1239,7 +1239,7 @@
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkEntry">
|
||||
<object class="GtkEntry" id="etr_longitude">
|
||||
<property name="visible">True</property>
|
||||
<property name="can-focus">True</property>
|
||||
</object>
|
||||
@@ -2530,6 +2530,7 @@
|
||||
<property name="double-buffered">False</property>
|
||||
<property name="use-stock">True</property>
|
||||
<property name="always-show-image">True</property>
|
||||
<signal name="clicked" handler="onApply" swapped="no"/>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
# Imports
|
||||
import gi, os, subprocess
|
||||
from time_bar import create_bar_chart
|
||||
from cinnamon_pref_handler import *
|
||||
from enums.PreferenceEnums import PrefenceEnums
|
||||
from enums.ImageSourceEnum import ImageSourceEnum
|
||||
from enums.PeriodSourceEnum import PeriodSourceEnum
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from gi.repository import Gtk, GdkPixbuf
|
||||
@@ -34,15 +38,20 @@ class Preferences:
|
||||
self.lbrHeicFile = self.builder.get_object("lbr_heic_file")
|
||||
self.lbrSourceFolder = self.builder.get_object("lbr_source_folder")
|
||||
self.imgBar = self.builder.get_object("img_bar")
|
||||
self.swExpandOverAllDisplays = self.builder.get_object("sw_expand_over_all_displays")
|
||||
self.swShowOnLockScreen = self.builder.get_object("sw_show_on_lock_screen")
|
||||
|
||||
## Location & Times
|
||||
self.tbNetworkLocation = self.builder.get_object("tb_network_location")
|
||||
self.tbCustomLocation = self.builder.get_object("tb_custom_location")
|
||||
self.tbTimePeriods = self.builder.get_object("tb_time_periods")
|
||||
self.lbrNetworkLocation = self.builder.get_object("lbr_network_location")
|
||||
self.spbNetworkLocationRefreshTime = self.builder.get_object("spb_network_location_refresh_time")
|
||||
self.lbrCustomLocationLongitude = self.builder.get_object("lbr_custom_location_longitude")
|
||||
self.lbrCustomLocationLatitude = self.builder.get_object("lbr_custom_location_latitude")
|
||||
self.lbrTimePeriods = self.builder.get_object("lbr_time_periods")
|
||||
self.etrLongitude = self.builder.get_object("etr_longitude")
|
||||
self.etrLatitude = self.builder.get_object("etr_latitude")
|
||||
|
||||
|
||||
# Time bar
|
||||
@@ -58,12 +67,61 @@ class Preferences:
|
||||
window = self.builder.get_object("window_main")
|
||||
window.show_all()
|
||||
|
||||
self.tbImageSet.set_active(True)
|
||||
self.tbNetworkLocation.set_active(True)
|
||||
|
||||
# Load from preferences
|
||||
if read_str_from_preferences(PrefenceEnums.IMAGESOURCE) == ImageSourceEnum.IMAGESET:
|
||||
self.tbImageSet.set_active(True)
|
||||
elif read_str_from_preferences(PrefenceEnums.IMAGESOURCE) == ImageSourceEnum.HEICFILE:
|
||||
self.tbHeicFile.set_active(True)
|
||||
elif read_str_from_preferences(PrefenceEnums.IMAGESOURCE) == ImageSourceEnum.SOURCEFOLDER:
|
||||
self.tbSourceFolder.set_active(True)
|
||||
|
||||
self.swExpandOverAllDisplays.set_active(read_str_from_preferences(PrefenceEnums.EXPANDOVERALLDISPLAY))
|
||||
self.swShowOnLockScreen.set_active(read_str_from_preferences(PrefenceEnums.SHOWONLOCKSCREEN))
|
||||
|
||||
|
||||
if read_str_from_preferences(PrefenceEnums.PERIODSOURCE) == PeriodSourceEnum.NETWORKLOCATION:
|
||||
self.tbNetworkLocation.set_active(True)
|
||||
elif read_str_from_preferences(PrefenceEnums.PERIODSOURCE) == PeriodSourceEnum.CUSTOMLOCATION:
|
||||
self.tbCustomLocation.set_active(True)
|
||||
elif read_str_from_preferences(PrefenceEnums.PERIODSOURCE) == PeriodSourceEnum.CUSTOMTIMEPERIODS:
|
||||
self.tbTimePeriods.set_active(True)
|
||||
|
||||
self.spbNetworkLocationRefreshTime.set_value(read_int_from_preferences(PrefenceEnums.LOCATIONREFRESHINTERVALS))
|
||||
self.etrLatitude.set_text(read_str_from_preferences(PrefenceEnums.LATITUDE))
|
||||
self.etrLongitude.set_text(read_str_from_preferences(PrefenceEnums.LONGITUDE))
|
||||
|
||||
Gtk.main()
|
||||
|
||||
|
||||
def onApply(self, *args):
|
||||
# todo: Store all values to settings
|
||||
if self.tbImageSet.get_active():
|
||||
write_to_preferences(PrefenceEnums.IMAGESOURCE, ImageSourceEnum.IMAGESET)
|
||||
elif self.tbHeicFile.get_active():
|
||||
write_to_preferences(PrefenceEnums.IMAGESOURCE, ImageSourceEnum.HEICFILE)
|
||||
elif self.tbSourceFolder.get_active():
|
||||
write_to_preferences(PrefenceEnums.IMAGESOURCE, ImageSourceEnum.SOURCEFOLDER)
|
||||
|
||||
write_to_preferences(PrefenceEnums.EXPANDOVERALLDISPLAY, self.swExpandOverAllDisplays.get_active())
|
||||
write_to_preferences(PrefenceEnums.SHOWONLOCKSCREEN, self.swShowOnLockScreen.get_active())
|
||||
|
||||
|
||||
write_to_preferences(PrefenceEnums.LOCATIONREFRESHINTERVALS, self.spbNetworkLocationRefreshTime.get_value())
|
||||
write_to_preferences(PrefenceEnums.LATITUDE, self.etrLatitude.get_text())
|
||||
write_to_preferences(PrefenceEnums.LONGITUDE, self.etrLongitude.get_text())
|
||||
|
||||
if self.tbNetworkLocation.get_active():
|
||||
write_to_preferences(PrefenceEnums.PERIODSOURCE, PeriodSourceEnum.NETWORKLOCATION)
|
||||
elif self.tbCustomLocation.get_active():
|
||||
write_to_preferences(PrefenceEnums.PERIODSOURCE, PeriodSourceEnum.CUSTOMLOCATION)
|
||||
elif self.tbTimePeriods.get_active():
|
||||
write_to_preferences(PrefenceEnums.PERIODSOURCE, PeriodSourceEnum.CUSTOMTIMEPERIODS)
|
||||
|
||||
|
||||
self.onDestroy()
|
||||
|
||||
|
||||
def onDestroy(self, *args):
|
||||
""" Lifecycle handler when window will be destroyed
|
||||
"""
|
||||
@@ -134,6 +192,9 @@ class Preferences:
|
||||
self.lbrCustomLocationLatitude.set_visible(False)
|
||||
self.lbrTimePeriods.set_visible(True)
|
||||
|
||||
|
||||
# About
|
||||
|
||||
def onCinnamonSpicesWebsiteButtonClicked(self, button):
|
||||
subprocess.Popen(["xdg-open", "https://cinnamon-spices.linuxmint.com/extensions/view/97"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user