Skip to content

Commit

Permalink
Check for updates: use last date to calculate interval.
Browse files Browse the repository at this point in the history
* Store the last date as a double
* Use this to check for elapsed time
  • Loading branch information
franglais125 committed Jan 21, 2017
1 parent 30c5643 commit c3b7ce6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
50 changes: 43 additions & 7 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ const AptUpdateIndicator = new Lang.Class({
this._checkCMD();

// Add a check at intervals
this._checkInterval();
this._initializeInterval();

this._bindSettings();
},
Expand Down Expand Up @@ -239,6 +239,40 @@ const AptUpdateIndicator = new Lang.Class({
CHECK_CMD = PREPEND_CMD + this._settings.get_string('check-cmd-no-passwd');
},

_initializeInterval: function() {
// Remove the periodic check before adding a new one
if (this._TimeoutId)
GLib.source_remove(this._TimeoutId);

// Interval in seconds from settings
let CHECK_INTERVAL = this._settings.get_int('check-interval') * 60;
if (CHECK_INTERVAL) {
// This has to be relative to the last check!
// Date is in milliseconds, convert to seconds
let last_check = this._settings.get_double('last-check-date-double');
let now = new Date();
let elapsed = (now - last_check)/1000; // In seconds

CHECK_INTERVAL -= elapsed;
if (CHECK_INTERVAL < 0) {
if (this._initializing)
CHECK_INTERVAL = 60;
else
CHECK_INTERVAL = 1;
}


let that = this;
this._TimeoutId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT,
CHECK_INTERVAL,
function() {
that._checkUpdates();
that._checkInterval();
return false;
});
}
},

_checkInterval: function() {
// Remove the periodic check before adding a new one
if (this._TimeoutId)
Expand Down Expand Up @@ -313,7 +347,7 @@ const AptUpdateIndicator = new Lang.Class({
],[
this._settings,
'changed::check-interval',
Lang.bind(this, this._checkInterval)
Lang.bind(this, this._initializeInterval)
],[
this._settings,
'changed::allow-no-passwd',
Expand Down Expand Up @@ -402,13 +436,15 @@ const AptUpdateIndicator = new Lang.Class({
*/

_lastCheck: function() {
let date = this._settings.get_string('last-check-date');
let date;

// If not just initalizing, update the date string to 'now'
if (!this._initializing) {
if (this._initializing) {
let last_check = new Date(this._settings.get_double('last-check-date-double'));
date = last_check.toLocaleFormat("%a %b %d, %H:%M").toString();
} else {
let now = new Date();
date = now.toLocaleFormat("%a %b %d, %H:%M");
this._settings.set_string('last-check-date', date);
date = now.toLocaleFormat("%a %b %d, %H:%M").toString();
this._settings.set_double('last-check-date-double', now);
}

if (date != '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@
<range min="0" max="100"/>
</key>

<key name="last-check-date" type="s">
<default>""</default>
<key name="last-check-date-double" type="d">
<default>0</default>
<summary>Date and time of the last time a check for updates was run.</summary>
<description>This is stored and used on the next boot.</description>
</key>
Expand Down

0 comments on commit c3b7ce6

Please sign in to comment.