Log file system implemented

This commit is contained in:
2023-08-25 17:41:42 +02:00
parent 0d8fa6449c
commit 5ca5b76d7c
5 changed files with 260 additions and 117 deletions

View File

@@ -1,14 +1,13 @@
/**
* @name Cinnamon-Dynamic-Wallpaper
* @alias TobiZog
* @since 2023
* @since 2023-05-17
*
* @description Main application file
*/
/******************** Imports ********************/
const MessageTray = imports.ui.messageTray;
const St = imports.gi.St;
const Main = imports.ui.main;
const Util = imports.misc.util;
const Settings = imports.ui.settings;
const Mainloop = imports.mainloop;
@@ -18,6 +17,7 @@ const Gio = imports.gi.Gio;
let suntimes = require('./scripts/suntimes')
let location = require('./scripts/location')
let communication = require('./scripts/communication')
/******************** Constants ********************/
@@ -39,6 +39,7 @@ let lastLocationUpdate = new Date()
// The last calculated suntime of the day
let lastDayTime = suntimes.DAYPERIOD.NONE
// Loop state
let looping = true
@@ -50,6 +51,9 @@ function CinnamonDynamicWallpaperExtension(uuid) {
CinnamonDynamicWallpaperExtension.prototype = {
/******************** Lifecycle ********************/
/**
* Initialization
*
@@ -58,6 +62,7 @@ CinnamonDynamicWallpaperExtension.prototype = {
_init: function(uuid) {
this.settings = new Settings.ExtensionSettings(this, uuid);
/** Configuration */
// Image set
this.bindSettings("sw_image_stretch", "imageStretch", this.settingsUpdated)
@@ -67,9 +72,15 @@ CinnamonDynamicWallpaperExtension.prototype = {
this.bindSettings("etr_latitude", "latitude", this.settingsUpdated)
this.bindSettings("etr_longitude", "longitude", this.settingsUpdated)
// Time periods
this.bindSettings("tv_times", "tvTimes")
/** Debugging */
// Logs
this.bindSettings("tv_logs", "tvLogs")
// Image Configurator
this.bindSettings("etr_img_morning_twilight", "img_morning_twilight", this.settingsUpdated)
this.bindSettings("etr_img_sunrise", "img_sunrise", this.settingsUpdated)
@@ -84,8 +95,10 @@ CinnamonDynamicWallpaperExtension.prototype = {
// Check for the first startup
if (this.settings.getValue("first_start")) {
this.writeToLogs("First time start")
// Welcome notification
this.showNotification("Welcome to Cinnamon Dynamic Wallpaper",
communication.showNotification("Welcome to Cinnamon Dynamic Wallpaper",
"Check the preferences to choose a dynamic wallpaper", true)
// Hide the notification on system restart
@@ -102,6 +115,7 @@ CinnamonDynamicWallpaperExtension.prototype = {
}
}
this.writeToLogs("Initialization completed")
// Set image initial at desktop wallpaper
this.setImageToTime()
@@ -114,9 +128,9 @@ CinnamonDynamicWallpaperExtension.prototype = {
/**
* Binding the settings objects
*
* @param {*} ui_name Name of preference in settings-schema.json
* @param {*} js_name Name of preference in JavaScript
* @param {*} func Function to call on change
* @param {string} ui_name Name of preference in settings-schema.json
* @param {string} js_name Name of preference in JavaScript
* @param {Function} func Function to call on change
*/
bindSettings: function (ui_name, js_name, func = this.on_settings_changed) {
this.settings.bindProperty(
@@ -128,142 +142,6 @@ CinnamonDynamicWallpaperExtension.prototype = {
},
/**
* Handles changes in settings
*/
settingsUpdated: function() {
lastDayTime = suntimes.DAYPERIOD.NONE
this.updateLocation()
this.setImageToTime()
},
/**
* Displaying a desktop notification
*
* @param {string} title The Title in the notification
* @param {string} text The text in the notification
* @param {boolean} showOpenSettings Display the "Open settings" button in the notification,
* defaults to false
*/
showNotification: function (title, text, showOpenSettings = false) {
let source = new MessageTray.Source(this.uuid);
// Parameter
let params = {
icon: new St.Icon({
icon_name: "icon",
icon_type: St.IconType.FULLCOLOR,
icon_size: source.ICON_SIZE
})
};
// The notification itself
let notification = new MessageTray.Notification(source, title, text, params);
// Display the "Open settings" button, if showOpenSettings
if (showOpenSettings) {
notification.addButton("open-settings", _("Open settings"));
notification.connect("action-invoked", () =>
Util.spawnCommandLine("xlet-settings extension " + UUID));
}
// Put all together
Main.messageTray.add(source);
// Display it
source.notify(notification);
},
/**
* Changes the desktop background image
*
* @param {jpg} imageURI The new desktop image
*/
changeWallpaper: function(imageURI) {
let gSetting = new Gio.Settings({schema: 'org.cinnamon.desktop.background'});
gSetting.set_string('picture-uri', imageURI);
if (this.imageStretch) {
gSetting.set_string('picture-options', 'spanned')
}
else
{
gSetting.set_string('picture-options', 'zoom')
}
Gio.Settings.sync();
gSetting.apply();
},
/**
* Estimate the right image based on time period of the day
*/
setImageToTime: function() {
let times = suntimes.calcTimePeriod(this.latitude, this.longitude)
let now = new Date()
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 + "/images/selected/" + imageSet[i])
lastDayTime = i
break
}
}
function convertToTimeString(time) {
return time.getHours().toString().padStart(2, "0") + ":" + time.getMinutes().toString().padStart(2, "0")
}
this.tvTimes =
"Morning Twilight:\t\t" + convertToTimeString(timesArray[0][0]) + " - " + convertToTimeString(timesArray[0][1]) +
"\nSunrise:\t\t\t\t" + convertToTimeString(timesArray[1][0]) + " - " + convertToTimeString(timesArray[1][1]) +
"\nMorning:\t\t\t" + convertToTimeString(timesArray[2][0]) + " - " + convertToTimeString(timesArray[2][1]) +
"\nNoon:\t\t\t\t" + convertToTimeString(timesArray[3][0]) + " - " + convertToTimeString(timesArray[3][1]) +
"\nAfternoon:\t\t\t" + convertToTimeString(timesArray[4][0]) + " - " + convertToTimeString(timesArray[4][1]) +
"\nEvening:\t\t\t" + convertToTimeString(timesArray[5][0]) + " - " + convertToTimeString(timesArray[5][1]) +
"\nSunset:\t\t\t\t" + convertToTimeString(timesArray[6][0]) + " - " + convertToTimeString(timesArray[6][1]) +
"\nNight Twilight:\t\t" + convertToTimeString(timesArray[7][0]) + " - " + convertToTimeString(timesArray[7][1]) +
"\nNight:\t\t\t\t" + convertToTimeString(timesArray[8][0]) + " - " + convertToTimeString(timesArray[8][1])
},
/**
* Get the location of the user
* Callback for changes in preferences
*/
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
}
// Update the update information
lastLocationUpdate = new Date()
},
/**
* Main loop
*/
@@ -278,11 +156,22 @@ CinnamonDynamicWallpaperExtension.prototype = {
// Refresh every 60 seconds
Mainloop.timeout_add_seconds(60, Lang.bind(this, this._loop));
this.writeToLogs("Main loop runs...")
}
},
/******************** UI Callbacks ********************/
/******************** Settings handling ********************/
/**
* Handles changes in settings
*/
settingsUpdated: function() {
lastDayTime = suntimes.DAYPERIOD.NONE
this.updateLocation()
this.setImageToTime()
},
/**
* Callback for settings-schema
@@ -318,6 +207,104 @@ CinnamonDynamicWallpaperExtension.prototype = {
*/
openIssueWebsite: function () {
Util.spawnCommandLine("xdg-open https://github.com/TobiZog/cinnamon-dynamic-wallpaper/issues/new")
},
/**
* Changes the desktop background image
*
* @param {jpg} imageURI The new desktop image
*/
changeWallpaper: function(imageURI) {
let gSetting = new Gio.Settings({schema: 'org.cinnamon.desktop.background'});
gSetting.set_string('picture-uri', imageURI);
if (this.imageStretch) {
gSetting.set_string('picture-options', 'spanned')
}
else
{
gSetting.set_string('picture-options', 'zoom')
}
Gio.Settings.sync();
gSetting.apply();
this.writeToLogs("Set new image: " + imageURI)
},
/**
* Estimate the right image based on time period of the day
*/
setImageToTime: function() {
let times = suntimes.calcTimePeriod(this.latitude, this.longitude)
let now = new Date()
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 + "/images/selected/" + imageSet[i])
lastDayTime = i
break
}
}
function convertToTimeString(time) {
return time.getHours().toString().padStart(2, "0") + ":" + time.getMinutes().toString().padStart(2, "0")
}
this.tvTimes =
"Morning Twilight:\t\t" + convertToTimeString(timesArray[0][0]) + " - " + convertToTimeString(timesArray[0][1]) +
"\nSunrise:\t\t\t\t" + convertToTimeString(timesArray[1][0]) + " - " + convertToTimeString(timesArray[1][1]) +
"\nMorning:\t\t\t" + convertToTimeString(timesArray[2][0]) + " - " + convertToTimeString(timesArray[2][1]) +
"\nNoon:\t\t\t\t" + convertToTimeString(timesArray[3][0]) + " - " + convertToTimeString(timesArray[3][1]) +
"\nAfternoon:\t\t\t" + convertToTimeString(timesArray[4][0]) + " - " + convertToTimeString(timesArray[4][1]) +
"\nEvening:\t\t\t" + convertToTimeString(timesArray[5][0]) + " - " + convertToTimeString(timesArray[5][1]) +
"\nSunset:\t\t\t\t" + convertToTimeString(timesArray[6][0]) + " - " + convertToTimeString(timesArray[6][1]) +
"\nNight Twilight:\t\t" + convertToTimeString(timesArray[7][0]) + " - " + convertToTimeString(timesArray[7][1]) +
"\nNight:\t\t\t\t" + convertToTimeString(timesArray[8][0]) + " - " + convertToTimeString(timesArray[8][1])
},
/**
* Get the location of the user
* Callback for changes in preferences
*/
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
}
// Update the update information
lastLocationUpdate = new Date()
this.writeToLogs("Location updated")
},
/**
* Adding text to the logs
*
* @param {string} msg New message string
*/
writeToLogs: function(msg) {
this.tvLogs = communication.createLogs(this.tvLogs, msg)
}
}

View File

@@ -0,0 +1,84 @@
/**
* @name Cinnamon-Dynamic-Wallpaper
* @alias TobiZog
* @since 2023-08-25
*
* @description Handles communications with the user (notifications, logs)
*/
/******************** Imports ********************/
const St = imports.gi.St;
const Main = imports.ui.main;
const Util = imports.misc.util;
const MessageTray = imports.ui.messageTray;
/******************** Functions ********************/
/**
* Displaying a desktop notification
*
* @param {string} title The Title in the notification
* @param {string} text The text in the notification
* @param {boolean} showOpenSettings Display the "Open settings" button in the notification,
* defaults to false
*/
function showNotification(title, text, showOpenSettings = false) {
let source = new MessageTray.Source(this.uuid);
// Parameter
let params = {
icon: new St.Icon({
icon_name: "icon",
icon_type: St.IconType.FULLCOLOR,
icon_size: source.ICON_SIZE
})
};
// The notification itself
let notification = new MessageTray.Notification(source, title, text, params);
// Display the "Open settings" button, if showOpenSettings
if (showOpenSettings) {
notification.addButton("open-settings", _("Open settings"));
notification.connect("action-invoked", () =>
Util.spawnCommandLine("xlet-settings extension " + UUID));
}
// Put all together
Main.messageTray.add(source);
// Display it
source.notify(notification);
}
/**
* Adding a message to the logs
*
* @param {string} logMsg New log message to add
*/
function createLogs(tvLogs, logMsg) {
/**
* Pad a number with leading zeros
*
* @param {number} num Number to format
* @param {number} size Final string length
*
* @returns String with defined length
*/
function pad(num, size) {
var s = "00" + num
return s.substring(s.length - size)
}
// Estimate date and time
let date = new Date()
let formattedDate = pad(date.getHours(), 2) + ":" + pad(date.getMinutes(), 2) + ":" + pad(date.getSeconds(), 2)
// Add the the logs
return formattedDate + "\t" + logMsg + "\n" + tvLogs
}

View File

@@ -1,6 +1,23 @@
/**
* @name Cinnamon-Dynamic-Wallpaper
* @alias TobiZog
* @since 2023
*
* @description Functions to estimate the user location
*/
/******************** Imports ********************/
const Soup = imports.gi.Soup;
/******************** Functions ********************/
/**
* Estimate the location of the user
*
* @returns Location data if succeded or -1 if failed
*/
function estimateLocation() {
let sessionSync = new Soup.SessionSync();
let msg = Soup.Message.new('GET', "https://get.geojs.io/v1/ip/geo.json");

View File

@@ -2,8 +2,12 @@
* @name Cinnamon-Dynamic-Wallpaper
* @alias TobiZog
* @since 2023
*
* @description Functions to calculate sun time periods
*/
/******************** Constants ********************/
const DAYPERIOD = {
MTWILIGHT: 0,
SUNRISE: 1,
@@ -22,6 +26,9 @@ const J1970 = 2440588
const J2000 = 2451545
/******************** Functions ********************/
function fromJulian(j) {
let ms_date = (j + 0.5 - J1970) * DAYMS
return new Date(ms_date)
@@ -83,6 +90,14 @@ function subTimesToMinutes(date1, date2) {
}
/**
* Calculating the start and end time of all time periods of the day
*
* @param {float} latitude Location latitude
* @param {float} longitude Location longitude
*
* @returns JSON of time periods
*/
function calcTimePeriod(latitude, longitude) {
let todaySunEventsDay = sunEventsOfDay(latitude, longitude, Date.now())
let tomorrowSunEventsDay = sunEventsOfDay(latitude, longitude, addMinutesToTime(new Date(), 1440))

View File

@@ -3,8 +3,12 @@
"type": "layout",
"pages": [
"pg_config",
"pg_logs",
"pg_about"
],
"pg_config": {
"type": "page",
"title": "Configuration",
@@ -14,17 +18,25 @@
"sec_times"
]
},
"pg_logs": {
"type": "page",
"title": "Debugging",
"sections": [
"sec_logs",
"sec_report_issue"
]
},
"pg_about": {
"type": "page",
"title": "About",
"sections": [
"sec_project",
"sec_github",
"sec_report_issue"
"sec_github"
]
},
"sec_image_configuration": {
"type": "section",
"title": "Image set",
@@ -51,6 +63,26 @@
"tv_times"
]
},
"sec_logs": {
"type": "section",
"title": "Logs",
"keys": [
"tv_log_description",
"tv_logs"
]
},
"sec_report_issue": {
"type": "section",
"title": "Report an issue",
"keys": [
"lb_report_issue",
"btn_report_issue"
]
},
"sec_project": {
"type": "section",
"title": "About the project",
@@ -68,18 +100,11 @@
"lb_repository",
"btn_open_repository"
]
},
"sec_report_issue": {
"type": "section",
"title": "Report an issue",
"keys": [
"lb_report_issue",
"btn_report_issue"
]
}
},
"lb_image_configuration": {
"type": "label",
"description": "Choose an included image set or import a heic-file with the Image Configurator"
@@ -92,8 +117,9 @@
"sw_image_stretch": {
"type": "switch",
"description": "Expand image over all displays",
"default": true
"default": false
},
"sw_auto_location": {
"type": "switch",
"default": true,
@@ -120,6 +146,7 @@
"description": "Longitude",
"dependency": "!sw_auto_location"
},
"tv_times": {
"type": "textview",
"description": "Time sections today",
@@ -127,6 +154,28 @@
},
"tv_log_description": {
"type": "label",
"description": "Logs contains informations about time, date and successful or failed operations of the extension.",
"default": ""
},
"tv_logs": {
"type": "textview",
"description": "See all logs",
"default": ""
},
"lb_report_issue": {
"type": "label",
"description": "Do you find an issue? Or want a new feature? Go to the GitHub repository and create a new issue. If you find an error message in the logs above, add it to the issue report."
},
"btn_report_issue": {
"type": "button",
"description": "Submit an Issue",
"callback": "openIssueWebsite"
},
"lb_about": {
"type": "label",
"description": "Based on a location, this extension calculates the periods of a day and switch the background image of your Cinnamon desktop. The extension offers the choice between a set of predownloaded wallpapers or to select a custom set of images."
@@ -155,16 +204,7 @@
"callback": "openRepoWebsite"
},
"lb_report_issue": {
"type": "label",
"description": "Do you find an issue? Or want a new feature? Go to the GitHub repository and create a new issue."
},
"btn_report_issue": {
"type": "button",
"description": "Submit an Issue",
"callback": "openIssueWebsite"
},
"etr_choosen_image_set": {