From f330e49378a120562bc4be5a09517785302b7748 Mon Sep 17 00:00:00 2001 From: Tobias Date: Fri, 9 Jun 2023 00:28:59 +0200 Subject: [PATCH] Main loop, update location, dynamic wallpaper change --- .../extension.js | 106 +++++++++++++++--- .../image-configurator/image-configurator.py | 21 ++-- .../scripts/location.js | 14 +++ .../scripts/suntimes.js | 7 +- 4 files changed, 117 insertions(+), 31 deletions(-) create mode 100644 cinnamon-dynamic-wallpaper@TobiZog/scripts/location.js diff --git a/cinnamon-dynamic-wallpaper@TobiZog/extension.js b/cinnamon-dynamic-wallpaper@TobiZog/extension.js index 3275da2..d66fcf2 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/extension.js +++ b/cinnamon-dynamic-wallpaper@TobiZog/extension.js @@ -11,10 +11,13 @@ const St = imports.gi.St; const Main = imports.ui.main; const Util = imports.misc.util; const Settings = imports.ui.settings; +const Mainloop = imports.mainloop; +const Lang = imports.lang; const { find_program_in_path } = imports.gi.GLib; const Gio = imports.gi.Gio; let suntimes = require('./scripts/suntimes') +let location = require('./scripts/location') /******************** Constants ********************/ @@ -22,6 +25,7 @@ let suntimes = require('./scripts/suntimes') const UUID = "cinnamon-dynamic-wallpaper@TobiZog"; const APPNAME = "Cinnamon Dynamic Wallpaper" const DIRECTORY = imports.ui.extensionSystem.extensionMeta[UUID]; +const PATH = DIRECTORY.path; /******************** Global Variables ********************/ @@ -29,6 +33,14 @@ const DIRECTORY = imports.ui.extensionSystem.extensionMeta[UUID]; // The extension object let extension; +// Time and date of the last location update +let lastLocationUpdate = new Date() + +// The last calculated suntime of the day +let lastDayTime = suntimes.DAYPERIOD.NONE + +let looping = true + /******************** Objects ********************/ @@ -46,21 +58,23 @@ CinnamonDynamicWallpaperExtension.prototype = { _init: function(uuid) { this.settings = new Settings.ExtensionSettings(this, uuid); - this.bindSettings("sw_auto_location", "autolocation") + this.bindSettings("sw_auto_location", "autolocation", this.updateLocation) this.bindSettings("sc_location_refresh_time", "locationRefreshTime") - this.bindSettings("etr_latitude", "latitude") - this.bindSettings("etr_longitude", "longitude") - this.bindSettings("etr_img_morning_twilight", "img_morning_twilight") - this.bindSettings("etr_img_sunrise", "img_sunrise") - this.bindSettings("etr_img_morning", "img_morning") - this.bindSettings("etr_img_noon", "img_noon") - this.bindSettings("etr_img_afternoon", "img_afternoon") - this.bindSettings("etr_img_evening", "img_evening") - this.bindSettings("etr_img_sunset", "img_sunset") - this.bindSettings("etr_img_night_twilight", "img_night_twilight") - this.bindSettings("etr_img_night", "img_night") + this.bindSettings("etr_latitude", "latitude", this.updateLocation) + this.bindSettings("etr_longitude", "longitude", this.updateLocation) + this.bindSettings("etr_img_morning_twilight", "img_morning_twilight", this.setImageToTime) + this.bindSettings("etr_img_sunrise", "img_sunrise", this.setImageToTime) + this.bindSettings("etr_img_morning", "img_morning", this.setImageToTime) + this.bindSettings("etr_img_noon", "img_noon", this.setImageToTime) + this.bindSettings("etr_img_afternoon", "img_afternoon", this.setImageToTime) + this.bindSettings("etr_img_evening", "img_evening", this.setImageToTime) + this.bindSettings("etr_img_sunset", "img_sunset", this.setImageToTime) + this.bindSettings("etr_img_night_twilight", "img_night_twilight", this.setImageToTime) + this.bindSettings("etr_img_night", "img_night", this.setImageToTime) - // suntimes.calcTimePeriod(52.37227, 9.73815) + this.setImageToTime() + + this._loop() }, @@ -125,14 +139,54 @@ CinnamonDynamicWallpaperExtension.prototype = { }, - /******************** UI Callbacks ********************/ + setImageToTime: function() { + let times = suntimes.calcTimePeriod(this.latitude, this.longitude) + let now = new Date() - on_settings_changed: function () { - // todo + let timesArray = [ + times["morning_twilight"], times["sunrise"], times["morning"], + times["noon"], times["afternoon"], times["evening"], + times["sunset"], times["night_twilight"], times["night"] + ] + + let imageSet = [ + this.img_morning_twilight, this.img_sunrise, this.img_morning, + this.img_noon, this.img_afternoon, this.img_evening, + this.img_sunset, this.img_night_twilight, this.img_night + ] + + for(let i = 0; i < timesArray.length; i++) { + if(timesArray[i][0] <= now && now <= timesArray[i][1] && i != lastDayTime) { + this.changeWallpaper("file://" + PATH + "/res/custom_images/" + imageSet[i]) + + lastDayTime = i + break + } + } }, + updateLocation: function () { + if (this.autolocation) { + let loc = location.estimateLocation() + this.latitude = loc["latitude"] + this.longitude = loc["longitude"] + } else { + this.latitude = this.latitude + this.longitude = this.longitude + } + + // Refresh the image information, if it's necessary + this.setImageToTime() + + // Update the update information + lastLocationUpdate = new Date() + }, + + + /******************** UI Callbacks ********************/ + /** * Callback for settings-schema * Opens the external heic-importer window @@ -148,6 +202,24 @@ CinnamonDynamicWallpaperExtension.prototype = { */ openRepoWebsite: function() { Util.spawnCommandLine("xdg-open https://github.com/TobiZog/cinnamon-dynamic-wallpaper"); + }, + + + /** + * Main loop + */ + _loop: function() { + if(looping) { + this.setImageToTime() + + if (lastLocationUpdate < new Date().getTime() - this.locationRefreshTime * 1000) { + this.updateLocation() + lastLocationUpdate = new Date() + } + + // Refresh every 60 seconds + Mainloop.timeout_add_seconds(60, Lang.bind(this, this._loop)); + } } } @@ -191,5 +263,5 @@ function enable() { * Lifecycle function on disable */ function disable() { - // todo + looping = false } \ No newline at end of file diff --git a/cinnamon-dynamic-wallpaper@TobiZog/image-configurator/image-configurator.py b/cinnamon-dynamic-wallpaper@TobiZog/image-configurator/image-configurator.py index 8a2135c..0b1448d 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/image-configurator/image-configurator.py +++ b/cinnamon-dynamic-wallpaper@TobiZog/image-configurator/image-configurator.py @@ -157,7 +157,7 @@ class ImageConfigurator: # Write the settings - with open(self.pref_path, "w") as pref_file: + with open(PREF_PATH, "w") as pref_file: json.dump(pref_data, pref_file, separators=(',', ':'), indent=4) @@ -210,16 +210,19 @@ class ImageConfigurator: def createExtracted(self): """ Create the extracted images array """ - if self.image_source == Source.RESSOURCES: - self.extracted = os.listdir(RES_DIR + "/custom_images") - elif self.image_source == Source.EXPORT: - self.extracted = os.listdir(EXPORT_DIR) + try: + if self.image_source == Source.RESSOURCES: + self.extracted = os.listdir(RES_DIR + "/custom_images") + elif self.image_source == Source.EXPORT: + self.extracted = os.listdir(EXPORT_DIR) - self.extracted.sort() - self.ls_preview.clear() + self.extracted.sort() + self.ls_preview.clear() - for option in self.extracted: - self.ls_preview.append([option]) + for option in self.extracted: + self.ls_preview.append([option]) + except: + pass self.stack_main.set_visible_child_name("config") diff --git a/cinnamon-dynamic-wallpaper@TobiZog/scripts/location.js b/cinnamon-dynamic-wallpaper@TobiZog/scripts/location.js new file mode 100644 index 0000000..0b63285 --- /dev/null +++ b/cinnamon-dynamic-wallpaper@TobiZog/scripts/location.js @@ -0,0 +1,14 @@ +const Soup = imports.gi.Soup; + + +function estimateLocation() { + let sessionSync = new Soup.SessionSync(); + let msg = Soup.Message.new('GET', "https://get.geojs.io/v1/ip/geo.json"); + sessionSync.send_message(msg); + + if (msg.status_code == 200) { + return JSON.parse(msg.response_body.data); + } else { + return -1; + } +} \ No newline at end of file diff --git a/cinnamon-dynamic-wallpaper@TobiZog/scripts/suntimes.js b/cinnamon-dynamic-wallpaper@TobiZog/scripts/suntimes.js index a47fd92..b8c010a 100644 --- a/cinnamon-dynamic-wallpaper@TobiZog/scripts/suntimes.js +++ b/cinnamon-dynamic-wallpaper@TobiZog/scripts/suntimes.js @@ -24,10 +24,7 @@ const J2000 = 2451545 function fromJulian(j) { let ms_date = (j + 0.5 - J1970) * DAYMS - var utc = new Date(); - var offset = utc.getTimezoneOffset() - - return new Date(ms_date - offset * 60000) + return new Date(ms_date) } /** @@ -158,4 +155,4 @@ function calcTimePeriod(latitude, longitude) { addMinutesToTime(tomorrowSunEventsDay.dawn, -1) ], } -} +} \ No newline at end of file