Skip to content

Commit

Permalink
auto-dark-light@gihaume: Version 1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-mueller committed Oct 22, 2024
1 parent a781ade commit 24dd234
Show file tree
Hide file tree
Showing 27 changed files with 371 additions and 340 deletions.
8 changes: 7 additions & 1 deletion auto-dark-light@gihaume/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## 1.2.1 - 22.10.2024

- Custom commands launching feature:
- Corrected list's `Active` attribute default to `true`
- Added shell features support

## 1.2.0 - 22.10.2024

- Added commands launching feature
- Added custom commands launching feature

## 1.1.0 - 02.09.2024

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const Themes_handler = require('./lib/themes_handler.js');
const Background_handler = require('./lib/background_handler.js');
const Twilights_calculator = require('./lib/twilights_calculator.js');
const Twilights_calculator = require('./lib/twilights_calculator/twilights_calculator.js');
const Event_scheduler = require('./lib/event_scheduler.js');
const Timer_absolute = require('./lib/timer_absolute.js');
const Time_of_day = require('./lib/time_of_day.js');
const Time_change_listener = require('./lib/time_change_listener/time_change_listener.js');
const Timezone_change_listener = require('./lib/timezone_change_listener.js');
const Timezone_coordinates_finder = require('./lib/timezones_coordinates/timezone_coordinates_finder.js');
const Sleep_wakeup_listener = require('./lib/sleep_wakeup_listener.js');
const Sleep_wakeup_listener = require('./lib/sleep_wakeup_listener/sleep_wakeup_listener.js');
const Color_scheme_change_listener = require('./lib/color_scheme_change_listener.js');
const Commands_launcher = require('./lib/commands_launcher.js');
const Commands_launcher = require('./lib/commands_launcher/commands_launcher.js');
const { _ } = require('./lib/translator.js');

const Applet = imports.ui.applet;
Expand Down Expand Up @@ -120,7 +120,8 @@ class ThisApplet extends Applet.IconApplet {
try {
this.time_change_listener = new Time_change_listener(
`${this.metadata.path}/lib/time_change_listener`,
this._update.bind(this)
this._update.bind(this),
this._notify_error.bind(this)
);
} catch (error) {
this._notify_critical(error.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ const IO = {
}
};

/**
* A `Cinnamon desktop` background handler that detects and applies the background settings.
*/
/** A `Cinnamon desktop` background handler that detects and applies the background settings. */
class Background_handler {
/**
* @param {Settings.XletSettingsBase} settings - The settings of the desk/applet.
Expand All @@ -27,9 +25,7 @@ class Background_handler {
settings.bindWithObject(this, keys.slideshow_folder, "slideshow_folder");
}

/**
* Detects and save the current background settings applied to Cinnamon desktop.
*/
/** Detects and save the current background settings applied to Cinnamon desktop. */
detect() {
this.is_slideshow = IO.SLIDESHOW. get_boolean(IO.KEYS.IS_SLIDESHOW);
this.background_file = IO.BACKGROUND.get_string( IO.KEYS.BACKGROUND_FILE);
Expand All @@ -40,9 +36,7 @@ class Background_handler {
this.slideshow_folder = IO.SLIDESHOW.get_string(IO.KEYS.SLIDESHOW_FOLDER).replace('directory://', "file://");
}

/**
* Applies the saved background settings to Cinnamon desktop.
*/
/** Applies the saved background settings to Cinnamon desktop. */
apply() {
IO.SLIDESHOW. set_boolean(IO.KEYS.IS_SLIDESHOW, this.is_slideshow);
IO.BACKGROUND.set_string( IO.KEYS.BACKGROUND_FILE, this.background_file);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const Gio = imports.gi.Gio;

/**
* Listens for changes in the color scheme and calls the provided callback
*/
/** Listens for changes in the color scheme and calls the provided callback */
class Color_scheme_change_listener {
#interface = Gio.Settings.new('org.x.apps.portal');
#callback;
Expand All @@ -25,9 +23,7 @@ class Color_scheme_change_listener {
}
}

/**
* Declare the object as finished to release any ressource acquired.
*/
/** Declare the object as finished to release any ressource acquired. */
finalize() { this.disable(); }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const launch_command = require('./lib/launch_command.js');
const launch_command = require('./lib/commands_launcher/launch_command.js');
const { _ } = require('./lib/translator.js');

const {Gio, GLib} = imports.gi;

/** A launcher for the commands of the settings list. */
class Commands_launcher {
#callback_for_errors;

/**
* @param {Settings.XletSettingsBase} settings - The settings of the desk/applet.
* @param {object} key_of_list - The keys of the settings' commands list.
* @param {function(string): void} callback_for_errors - The callback with a message for when an error occurs.
* @param {function(string): void} callback_for_errors - The function to call with a message for when an error occurs.
*/
constructor(settings, key_of_list, callback_for_errors) {
settings.bindWithObject(this, key_of_list, "list");
Expand All @@ -19,13 +20,13 @@ class Commands_launcher {
async launch_commands() {
for (const item of this.list) {
const { name, active, expiry, command } = item;

if (!active)
continue;

try { await launch_command(command, expiry); }
catch (error) {
let msg = `${_("the command")} '${name}' ${_("failed")}`;
const name_for_error = name !== '' ? name : command;
let msg = `${_("the command")} '${name_for_error}' ${_("failed")}`;
if (error instanceof GLib.ShellError)
msg += ` ${_("due to a wrong format")}${_(":")} ${error.message}`;
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Gio._promisify(Gio.Subprocess.prototype, 'communicate_utf8_async');

/**
* Executes a command with a timeout and transmits any error on failure.
*
* @async
* @param {string} command - The shell command to execute.
* @param {number} [timeout_seconds=10] - The delay in seconds before cancelling the command. `0` means infinity/never.
Expand All @@ -14,6 +13,7 @@ Gio._promisify(Gio.Subprocess.prototype, 'communicate_utf8_async');
* @throws {Gio.IOErrorEnum.FAILED} - If the command fails with a non-zero exit code. The error message is the `stderr` output if any, otherwise the exit status.
*/
async function launch_command(command, timeout_seconds = 10) {
command = `sh -c ${GLib.shell_quote(`exec ${command}`)}`; // brings shell features
const [_ok, argvp] = GLib.shell_parse_argv(command); // can throw GLib.ShellError

const proc = new Gio.Subprocess({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const {Gio, GLib} = imports.gi;

class Dbus {
/**
* An interface to read the system screensaver lock state.
*/
/** An interface to read the system screensaver lock state. */
static Screen_lock = class {
#signal_id;

Expand Down Expand Up @@ -61,9 +59,7 @@ class Dbus {
}
}

/**
* An interface to read the system sleep/wakeup state.
*/
/** An interface to read the system sleep/wakeup state. */
static Sleep = class {
#signal_id;

Expand Down Expand Up @@ -97,9 +93,7 @@ class Dbus {
}
}

/**
* An interface to read the system timezone.
*/
/** An interface to read the system timezone. */
static Timezone = class {
#signal_id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ const Mainloop = imports.mainloop;

const Time_of_day = require('./lib/time_of_day.js');

/**
* A single-event scheduler which call a function at a specified due time of day.
*/
/** A single-event scheduler which call a function at a specified due time of day. */
class Event_scheduler {
#event_id;

Expand All @@ -29,19 +27,15 @@ class Event_scheduler {
// );
}

/**
* Unset the scheduled event if it exists.
*/
/** Unset the scheduled event if it exists. */
unset_the_event() {
if (this.#event_id !== undefined) {
Mainloop.source_remove(this.#event_id);
this.#event_id = undefined;
}
}

/**
* Declare the object as finished to release any ressource acquired.
*/
/** Declare the object as finished to release any ressource acquired. */
finalize() { this.unset_the_event(); }

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const Dbus = require('./lib/dbus.js');

/**
* A gatekeeper for the screen locked state.
*/
class Screen_lock_handler {
/** A gatekeeper for the screen locked state. */
class Screen_lock_checker {
#lock = new Dbus.Screen_lock();

/**
Expand All @@ -24,15 +22,11 @@ class Screen_lock_handler {
});
}

/**
* Cancel the `try_now_or_postpone` procedure.
*/
/** Cancel the `try_now_or_postpone` procedure. */
cancel() { this.#lock.unsubscribe_to_changes(); }

/**
* Declare the object as finished to release any ressource acquired.
*/
/** Declare the object as finished to release any ressource acquired. */
finalize() { this.cancel(); }
}

module.exports = Screen_lock_handler;
module.exports = Screen_lock_checker;
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const Dbus = require('./lib/dbus.js');
const Screen_lock_handler = require('./lib/screen_lock_handler.js');
const Screen_lock_checker = require('./lib/sleep_wakeup_listener/screen_lock_checker.js');

/**
* A gatekeeper for the sleep combined with the screen locked on wakeup.
*/
/** A gatekeeper for the sleep combined with the screen locked on wakeup. */
class Sleep_wakeup_listener {
#sleep = new Dbus.Sleep();
#lock = new Screen_lock_handler();
#sleep_dbus = new Dbus.Sleep();
#lock_checker = new Screen_lock_checker();
#on_sleep_entry_callback;
#on_wakeup_unlocked_callback;

Expand All @@ -20,27 +18,25 @@ class Sleep_wakeup_listener {
}

enable() {
this.#sleep.subscribe_to_changes((is_sleeping) => {
this.#sleep_dbus.subscribe_to_changes((is_sleeping) => {
if (is_sleeping)
this.#on_sleep_entry_callback();
else
this.#lock.try_now_or_postpone_until_unlocked(
this.#lock_checker.try_now_or_postpone_until_unlocked(
this.#on_wakeup_unlocked_callback.bind(this)
);
});
}

disable() {
this.#sleep.unsubscribe_to_changes();
this.#lock.cancel();
this.#sleep_dbus.unsubscribe_to_changes();
this.#lock_checker.cancel();
}

/**
* Declare the object as finished to release any ressource acquired.
*/
/** Declare the object as finished to release any ressource acquired. */
finalize() {
this.#sleep.unsubscribe_to_changes();
this.#lock.finalize();
this.#sleep_dbus.unsubscribe_to_changes();
this.#lock_checker.finalize();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ const IO = {
}
};

/**
* A `Cinnamon desktop` themes handler that detects and applies the themes.
*/
/** A `Cinnamon desktop` themes handler that detects and applies the themes. */
class Themes_handler {
#color_scheme;

Expand All @@ -38,9 +36,7 @@ class Themes_handler {
this.#color_scheme = is_dark ? 'prefer-dark' : 'prefer-light';
}

/**
* Detects and save the current themes applied to Cinnamon desktop.
*/
/** Detects and save the current themes applied to Cinnamon desktop. */
detect() {
this.mouse_pointer = IO.DESKTOP .get_string(IO.KEYS.MOUSE_POINTER);
this.applications = IO.DESKTOP .get_string(IO.KEYS.APPLICATIONS);
Expand All @@ -49,9 +45,7 @@ class Themes_handler {
this.has_detected = true;
}

/**
* Applies the saved themes to Cinnamon desktop.
*/
/** Applies the saved themes to Cinnamon desktop. */
apply() {
if (!this.has_detected)
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
BUILD_FOLDER := build
SOURCE_FILES := $(wildcard *.cpp)
OBJECT_FILES := $(patsubst %.cpp, $(BUILD_FOLDER)/%.o, $(SOURCE_FILES))
EXECUTABLE := time_change_listener
EXECUTABLE := auto-dark-light-time-change-listener
COMPILER := g++
COMPILER_FLAGS := -std=c++20 -Wall -pedantic -Wextra
COMPILER_FLAGS := -std=c++20
LINKER_FLAGS :=


Expand All @@ -22,11 +22,7 @@ $(BUILD_FOLDER)/%.o: %.cpp
$(COMPILER) -c $(COMPILER_FLAGS) -o $@ $< -MMD -MF $(@:.o=.d)


run: $(EXECUTABLE)
./$(EXECUTABLE)


clean:
$(RM) -rf $(BUILD_FOLDER)

.PHONY: all build run clean
.PHONY: all build clean
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
#include <exception>

int main(int argc, char* argv[]) {
std::vector<std::string> args{argv, argv + argc};
{
std::vector<std::string> args{argv, argv + argc};

if (std::find(args.begin(), args.end(), "--help") != args.end()) {
std::cout << "Usage: " << args[0] << '\n'
<< "Listens for system time changes and prints 'changed' to `stdout` each time it occurs.\n"
<< '\n'
<< "Commands via `stdin`:\n"
<< " 'enable': Enable listening for the system time changes.\n"
<< " 'disable': Disable listening for the system time changes.\n"
<< " 'exit': Exit the program.\n"
<< '\n'
<< "Options:\n"
<< " --help: Display this help message.\n"
<< std::endl;
return 0;
if (std::find(args.begin(), args.end(), "--help") != args.end()) {
std::cout
<< "Usage: " << args[0] << '\n'
<< "Listens for system time changes and prints 'changed' to `stdout` each time it occurs.\n"
<< '\n'
<< "Commands via `stdin`:\n"
<< " 'enable': Enable listening for the system time changes.\n"
<< " 'disable': Disable listening for the system time changes.\n"
<< " 'exit': Exit the program.\n"
<< '\n'
<< "Options:\n"
<< " --help: Display this help message.\n"
<< std::endl;
return 0;
}
}

try {
Expand Down
Loading

0 comments on commit 24dd234

Please sign in to comment.