Skip to content

Commit

Permalink
[email protected]: Add force refresh menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
nabetaro committed Dec 26, 2024
1 parent 70d4c3f commit 6ac92ff
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions [email protected]/files/[email protected]/applet.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Mainloop = imports.mainloop;
const Lang = imports.lang;
const PopupMenu = imports.ui.popupMenu;
const St = imports.gi.St;

const logging = false;

Expand Down Expand Up @@ -45,10 +47,19 @@ BingWallpaperApplet.prototype = {
this.wallpaperPath = `${this.wallpaperDir}/BingWallpaper.jpg`;
this.metaDataPath = `${this.wallpaperDir}/meta.json`;

let refreshBg = new PopupMenu.PopupIconMenuItem(_("Refresh Now"), "view-refresh", St.IconType.SYMBOLIC);
refreshBg.connect('activate', Lang.bind(this, function() {
this.forceReload = true;
this._refresh()
}));
this._applet_context_menu.addMenuItem(refreshBg);

// Begin refresh loop
this._refresh();
},

forceReload: false,

_refresh: function () {
log(`Beginning refresh`);
this._getMetaData();
Expand Down Expand Up @@ -100,7 +111,7 @@ BingWallpaperApplet.prototype = {
const end_date = start_date.add_days(1);
const now = GLib.DateTime.new_now_utc();

if (now.to_unix() < end_date.to_unix()) {
if (now.to_unix() < end_date.to_unix() && !this.forceReload) {
log('metadata up to date');

// Look for image file, check this is up to date
Expand All @@ -111,7 +122,6 @@ BingWallpaperApplet.prototype = {
let image_file_info = image_file.query_info('*', Gio.FileQueryInfoFlags.NONE, null);
let image_file_size = image_file_info.get_size();
let image_file_mod_secs = image_file_info.get_modification_time().tv_sec;

if ((image_file_mod_secs > end_date.to_unix()) || !image_file_size) { // Is the image old, or empty?
this._downloadImage();

Expand All @@ -126,7 +136,8 @@ BingWallpaperApplet.prototype = {

}
else {
log('metadata is old, requesting new...');
log(this.forceReload ? 'force reload, requesting new...' : 'metadata is old, requesting new...');
this.forceReload = false;
this._downloadMetaData();
}

Expand All @@ -140,7 +151,7 @@ BingWallpaperApplet.prototype = {

_downloadMetaData: function () {
const process_result = data => {

// Write to meta data file
let gFile = Gio.file_new_for_path(this.metaDataPath);
let fStream = gFile.replace(null, false, Gio.FileCreateFlags.NONE, null);
Expand All @@ -157,7 +168,7 @@ BingWallpaperApplet.prototype = {
this._downloadImage();

};

// Retrieve json metadata, either from local file or remote
let request = Soup.Message.new('GET', `${bingHost}${bingRequestPath}`);
if (Soup.MAJOR_VERSION === 2) {
Expand All @@ -168,7 +179,7 @@ BingWallpaperApplet.prototype = {
log(`Failed to acquire image metadata (${message.status_code})`);
this._setTimeout(60) // Try again
}

});
} else { //version 3
_httpSession.send_and_read_async(request, Soup.MessagePriority.NORMAL, null, (_httpSession, message) => {
Expand Down

0 comments on commit 6ac92ff

Please sign in to comment.