|
| 1 | +const Applet = imports.ui.applet; |
| 2 | +const PopupMenu = imports.ui.popupMenu; |
| 3 | +const St = imports.gi.St; |
| 4 | +const Settings = imports.ui.settings; |
| 5 | +const Util = imports.misc.util; |
| 6 | +const GLib = imports.gi.GLib; |
| 7 | +const Lang = imports.lang; |
| 8 | +const Main = imports.ui.main; |
| 9 | +const Mainloop = imports.mainloop; |
| 10 | + |
| 11 | + |
| 12 | +function AptUpdates(metadata, orientation, panel_height, instance_id) { |
| 13 | + this._init(metadata, orientation, panel_height, instance_id); |
| 14 | +} |
| 15 | + |
| 16 | +AptUpdates.prototype = { |
| 17 | + __proto__: Applet.TextIconApplet.prototype, |
| 18 | + |
| 19 | + _init: function(metadata, orientation, panel_height, instance_id) { |
| 20 | + this.applet_path = metadata.path; |
| 21 | + |
| 22 | + Applet.TextIconApplet.prototype._init.call(this, orientation, panel_height, instance_id); |
| 23 | + |
| 24 | + this.set_applet_icon_name("update-none"); |
| 25 | + this.hide_applet_label(true); |
| 26 | + |
| 27 | + this.menuManager = new PopupMenu.PopupMenuManager(this); |
| 28 | + this.menu = new Applet.AppletPopupMenu(this, orientation); |
| 29 | + this.menuManager.addMenu(this.menu); |
| 30 | + |
| 31 | + if (!GLib.find_program_in_path("gnome-terminal")) { |
| 32 | + this.hide_applet_icon(true); |
| 33 | + this.hide_applet_label(false); |
| 34 | + this.set_applet_label("missing dependencies"); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + this.uuid = metadata.uuid; |
| 39 | + this.settings = new Settings.AppletSettings(this, metadata.uuid, instance_id); |
| 40 | + |
| 41 | + this.settings.bind("update-refresh", "refreshTimeout", null, null); |
| 42 | + this.settings.bind("hide-applet", "hideApplet", this._update, null); |
| 43 | + |
| 44 | + this.settings.bind("different-levels", "differentLevels", this._update, null); |
| 45 | + this.settings.bind("level-1", "level1", this._update, null); |
| 46 | + this.settings.bind("level-2", "level2", this._update, null); |
| 47 | + |
| 48 | + this.settings.bind("commandUpdate", "commandUpdate", null, null); |
| 49 | + this.settings.bind("commandUpgrade", "commandUpgrade", null, null); |
| 50 | + |
| 51 | + this.packages_count = 0; |
| 52 | + |
| 53 | + this.enabled = true; |
| 54 | + this.run(); |
| 55 | + }, |
| 56 | + |
| 57 | + _update: function() { |
| 58 | + const count = this.packages_count; |
| 59 | + |
| 60 | + this.set_applet_enabled(!this.hideApplet || count != 0); |
| 61 | + |
| 62 | + if (this.differentLevels) { |
| 63 | + if (count <= 0) { |
| 64 | + this.set_applet_tooltip("No updates available"); |
| 65 | + this.set_applet_icon_name("update-none"); |
| 66 | + } else if (count < this.level1) { |
| 67 | + this.set_applet_tooltip(count.toString() + " updates available"); |
| 68 | + this.set_applet_icon_name("update-low"); |
| 69 | + } else if (count < this.level2) { |
| 70 | + this.set_applet_tooltip(count.toString() + " updates available"); |
| 71 | + this.set_applet_icon_name("update-medium"); |
| 72 | + } else { |
| 73 | + this.set_applet_tooltip(count.toString() + " updates available"); |
| 74 | + this.set_applet_icon_name("update-high"); |
| 75 | + } |
| 76 | + } else { |
| 77 | + if (count <= 0) { |
| 78 | + this.set_applet_tooltip("No updates available"); |
| 79 | + this.set_applet_icon_name("update-none"); |
| 80 | + } else { |
| 81 | + this.set_applet_tooltip(count.toString() + " updates available"); |
| 82 | + this.set_applet_icon_name("update-low"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + this.menu.removeAll(); |
| 87 | + this._contentSection = new PopupMenu.PopupMenuSection(); |
| 88 | + this.menu.addMenuItem(this._contentSection); |
| 89 | + |
| 90 | + const iViewStr = count > 0 ? "View " + count.toString() + " updates" : "No updates to view"; |
| 91 | + let iView = new PopupMenu.PopupIconMenuItem(iViewStr, "view-list-bullet-symbolic", St.IconType.SYMBOLIC, {reactive: count > 0}); |
| 92 | + iView.connect('activate', Lang.bind(this, function () { |
| 93 | + Util.spawn_async(['/bin/bash', this.applet_path + '/updates.sh', "view"]); |
| 94 | + })); |
| 95 | + |
| 96 | + let iCheck = new PopupMenu.PopupIconMenuItem("Check for new updates", "view-refresh-symbolic", St.IconType.SYMBOLIC); |
| 97 | + iCheck.connect('activate', Lang.bind(this, function () { |
| 98 | + const args = ['/bin/bash', this.applet_path + '/updates.sh', "command", this.commandUpdate]; |
| 99 | + Util.spawn_async(args, Lang.bind(this, function() { |
| 100 | + this._refreshUpdatesInfo(); |
| 101 | + })); |
| 102 | + })); |
| 103 | + |
| 104 | + const iUpgradeStr = count > 0 ? "Upgrade " + count.toString() + " packages" : "No packages to upgrade"; |
| 105 | + let iUpgrade = new PopupMenu.PopupIconMenuItem(iUpgradeStr, "system-run-symbolic", St.IconType.SYMBOLIC, {reactive: count > 0}); |
| 106 | + iUpgrade.connect('activate', Lang.bind(this, function () { |
| 107 | + const args = ['/bin/bash', this.applet_path + '/updates.sh', "command", this.commandUpgrade]; |
| 108 | + Util.spawn_async(args, Lang.bind(this, function() { |
| 109 | + this._refreshUpdatesInfo(); |
| 110 | + })); |
| 111 | + })); |
| 112 | + |
| 113 | + this.menu.addMenuItem(iCheck); |
| 114 | + this.menu.addMenuItem(iView); |
| 115 | + this.menu.addMenuItem(iUpgrade); |
| 116 | + }, |
| 117 | + |
| 118 | + on_applet_clicked: function() { |
| 119 | + this.menu.toggle(); |
| 120 | + }, |
| 121 | + |
| 122 | + on_applet_removed: function() { |
| 123 | + this.enabled = false; |
| 124 | + }, |
| 125 | + |
| 126 | + _refreshUpdatesInfo: function() { |
| 127 | + Util.spawn_async(['/bin/bash', this.applet_path + '/updates.sh', "check"], Lang.bind(this, function(stdout){ |
| 128 | + this.packages_count = parseInt(stdout.trim()); |
| 129 | + this._update(); |
| 130 | + })); |
| 131 | + }, |
| 132 | + |
| 133 | + run: function() { |
| 134 | + if (!this.enabled) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + Util.spawn_async(['/bin/bash', this.applet_path + '/updates.sh', "check"], Lang.bind(this, function(stdout){ |
| 139 | + this.packages_count = parseInt(stdout.trim()); |
| 140 | + this._update(); |
| 141 | + |
| 142 | + Util.spawn_async(['/usr/bin/sleep', (parseInt(this.refreshTimeout) * 60).toString()], Lang.bind(this, function(_) { |
| 143 | + this.run(); |
| 144 | + })); |
| 145 | + })); |
| 146 | + }, |
| 147 | +}; |
| 148 | + |
| 149 | +function main(metadata, orientation, panel_height, instance_id) { |
| 150 | + return new AptUpdates(metadata, orientation, panel_height, instance_id); |
| 151 | +} |
0 commit comments