1# Copyright (c) 2017-2021 Cedric Bellegarde <cedric.bellegarde@adishatz.org> 2# This program is free software: you can redistribute it and/or modify 3# it under the terms of the GNU General Public License as published by 4# the Free Software Foundation, either version 3 of the License, or 5# (at your option) any later version. 6# This program is distributed in the hope that it will be useful, 7# but WITHOUT ANY WARRANTY; without even the implied warranty of 8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9# GNU General Public License for more details. 10# You should have received a copy of the GNU General Public License 11# along with this program. If not, see <http://www.gnu.org/licenses/>. 12 13from gi.repository import Gio, GLib, Gtk 14 15from eolie.logger import Logger 16 17 18class NightApplication: 19 """ 20 Auto switch to night mode base on GSD state 21 """ 22 23 def __init__(self): 24 """ 25 Initiate connexion to DBus 26 """ 27 try: 28 self.__on_night_mode_changed(self.settings) 29 self.settings.connect("changed::night-mode", 30 self.__on_night_mode_changed) 31 Gio.bus_get(Gio.BusType.SESSION, None, self.__on_get_bus) 32 self.__wanted_temperature = None 33 settings = Gio.Settings.new( 34 "org.gnome.settings-daemon.plugins.color") 35 self.__wanted_temperature = settings.get_value( 36 "night-light-temperature").get_uint32() 37 except Exception as e: 38 Logger.error("NightApplication::__init__(): %s", e) 39 40####################### 41# PRIVATE # 42####################### 43 def __on_get_bus(self, source, result): 44 """ 45 Get bus and set proxy 46 @param source as GObject.Object 47 @param result as Gio.AsyncResult 48 """ 49 try: 50 bus = Gio.bus_get_finish(result) 51 Gio.DBusProxy.new( 52 bus, 53 Gio.DBusProxyFlags.GET_INVALIDATED_PROPERTIES, 54 None, 55 "org.gnome.SettingsDaemon.Color", 56 "/org/gnome/SettingsDaemon/Color", 57 "org.gnome.SettingsDaemon.Color", 58 None, 59 self.__on_get_proxy) 60 except Exception as e: 61 Logger.error("NightApplication::__on_get_bus(): %s", e) 62 63 def __on_get_proxy(self, source, result): 64 """ 65 Get proxy and connect signal 66 @param source as GObject.Object 67 @param result as Gio.AsyncResult 68 """ 69 try: 70 self.__proxy = source.new_finish(result) 71 self.__proxy.connect("g-properties-changed", 72 self.__on_property_changed) 73 if self.settings.get_value("auto-night-mode"): 74 value = self.__proxy.get_cached_property( 75 "Temperature").get_uint32() 76 self.__on_property_changed(self.__proxy, 77 {"Temperature": value}, 78 []) 79 except Exception as e: 80 Logger.error("NightApplication::__on_get_bus(): %s", e) 81 82 def __on_property_changed(self, proxy, changed_properties, 83 invalidated_properties): 84 """ 85 Set Eolie night mode based on current temperature 86 @param proxy as Gio.DBusProxy 87 @param changed_properties as {} 88 @param invalidated_properties as [str] 89 """ 90 if not self.settings.get_value("auto-night-mode"): 91 return 92 if "Temperature" in changed_properties.keys(): 93 temperature = changed_properties["Temperature"] 94 night_mode = self.settings.get_value("night-mode") 95 new_night_mode = temperature < self.__wanted_temperature + 100 96 if night_mode != new_night_mode: 97 self.settings.set_value("night-mode", 98 GLib.Variant("b", new_night_mode)) 99 100 def __on_night_mode_changed(self, settings, *ignore): 101 """ 102 Update GTK style 103 @param settings as Settings 104 """ 105 night_mode = settings.get_value("night-mode") 106 settings = Gtk.Settings.get_default() 107 settings.set_property("gtk-application-prefer-dark-theme", night_mode) 108 for window in self.windows: 109 GLib.idle_add(window.toolbar.title.entry.update_style) 110 for webview in window.container.webviews: 111 webview.night_mode() 112