Refactor drive utilities functionality

This commit is contained in:
UshakovVasilii
2014-06-24 02:21:34 +04:00
parent 0e43f30351
commit 83e4e14bd3
6 changed files with 217 additions and 167 deletions

View File

@@ -5,18 +5,16 @@ const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const Util = imports.misc.util;
const Mainloop = imports.mainloop;
const Shell = imports.gi.Shell;
const Clutter = imports.gi.Clutter;
const Gio = imports.gi.Gio;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
const Utilities = Me.imports.utilities;
const UDisks2 = Me.imports.udisks2;
const Gettext = imports.gettext.domain(Me.metadata['gettext-domain']);
const _ = Gettext.gettext;
let settings;
const FreonItem = new Lang.Class({
Name: 'FreonItem',
Extends: PopupMenu.PopupBaseMenuItem,
@@ -24,12 +22,7 @@ const FreonItem = new Lang.Class({
_init: function(gIcon, label, value) {
this.parent();
this._hasMainDot = false;
this.connect('activate', function () {
settings.set_string('main-sensor', label);
});
this._label = label;
this._value = value;
this.actor.add(new St.Icon({ style_class: 'system-status-icon', gicon : gIcon}));
this.actor.add(new St.Label({text: label}), {x_fill: true, expand: true});
@@ -51,6 +44,10 @@ const FreonItem = new Lang.Class({
this.setOrnament(PopupMenu.Ornament.NONE);
},
getLabel: function() {
return this._label;
},
setValue: function(value) {
this._valueLabel.text = value;
}
@@ -58,7 +55,6 @@ const FreonItem = new Lang.Class({
const FreonMenuButton = new Lang.Class({
Name: 'FreonMenuButton',
Extends: PanelMenu.Button,
_init: function(){
@@ -70,6 +66,8 @@ const FreonMenuButton = new Lang.Class({
this._hddtempOutput = '';
this._aticonfigOutput = '';
this._settings = Convenience.getSettings();
this._sensorIcons = {
temperature : Gio.icon_new_for_string(Me.path + '/icons/sensors-temperature-symbolic.svg'),
voltage : Gio.icon_new_for_string(Me.path + '/icons/sensors-voltage-symbolic.svg'),
@@ -81,27 +79,20 @@ const FreonMenuButton = new Lang.Class({
this.actor.add_actor(this.statusLabel);
this.sensorsArgv = Utilities.detectSensors();
if(settings.get_boolean('show-hdd-temp'))
this.hddtempArgv = Utilities.detectHDDTemp();
if(settings.get_boolean('show-aticonfig-temp'))
this._initDriveUtility();
if(this._settings.get_boolean('show-aticonfig-temp'))
this.aticonfigArgv = Utilities.detectAtiConfig();
this.udisksProxies = [];
Utilities.UDisks.get_drive_ata_proxies(Lang.bind(this, function(proxies) {
this.udisksProxies = proxies;
this._updateDisplay(this._sensorsOutput, this._hddtempOutput, this._aticonfigOutput);
}));
this._settingChangedSignals = [];
this._addSettingChangedSignal('update-time', Lang.bind(this, this._updateTimeChanged));
this._addSettingChangedSignal('unit', Lang.bind(this, this._querySensors));
this._addSettingChangedSignal('show-label', Lang.bind(this, this._querySensors));
this._addSettingChangedSignal('main-sensor', Lang.bind(this, this._querySensors));
this._addSettingChangedSignal('show-decimal-value', Lang.bind(this, this._querySensors));
this._addSettingChangedSignal('show-hdd-temp', Lang.bind(this, this._showHddTempChanged));
this._addSettingChangedSignal('show-fan-rpm', Lang.bind(this, this._querySensors));
this._addSettingChangedSignal('show-voltage', Lang.bind(this, this._querySensors));
this._addSettingChangedSignal('show-aticonfig-temp', Lang.bind(this, this._showAtiConfigChanged));
this._addSettingChangedSignal('drive-utility', Lang.bind(this, this._driveUtilityChanged));
this.connect('destroy', Lang.bind(this, this._onDestroy));
@@ -111,25 +102,45 @@ const FreonMenuButton = new Lang.Class({
this._addTimer();
},
_updateTimeChanged : function(){
//global.log('[FREON] readd timer');
_driveUtilityChanged : function(){
this._destroyDriveUtility();
this._initDriveUtility();
this._querySensors();
},
_initDriveUtility : function(){
switch(this._settings.get_string('drive-utility')){
case 'hddtemp':
this.hddtempArgv = Utilities.detectHDDTemp();
break;
case 'udisks2':
this._udisks2 = new UDisks2.UDisks2(Lang.bind(this, function() {
this._updateDisplay();
}));
break;
}
},
_destroyDriveUtility : function(){
if(this._udisks2){
this._udisks2.destroy();
this._udisks2 = null;
}
this.hddtempArgv = null;
},
_updateTimeChanged : function(){
Mainloop.source_remove(this._timeoutId);
this._addTimer();
},
_showHddTempChanged : function(){
this.hddtempArgv = settings.get_boolean('show-hdd-temp') ? Utilities.detectHDDTemp() : undefined;
this._querySensors();
},
_showAtiConfigChanged : function(){
this.aticonfigArgv = settings.get_boolean('show-aticonfig-temp') ? Utilities.detectAtiConfig() : undefined;
this.aticonfigArgv = this._settings.get_boolean('show-aticonfig-temp') ? Utilities.detectAtiConfig() : undefined;
this._querySensors();
},
_addTimer : function(){
this._timeoutId = Mainloop.timeout_add_seconds(settings.get_int('update-time'), Lang.bind(this, function (){
this._timeoutId = Mainloop.timeout_add_seconds(this._settings.get_int('update-time'), Lang.bind(this, function (){
this._querySensors();
// readd to update queue
return true;
@@ -137,23 +148,15 @@ const FreonMenuButton = new Lang.Class({
},
_addSettingChangedSignal : function(key, callback){
this._settingChangedSignals.push(settings.connect('changed::' + key, callback));
this._settingChangedSignals.push(this._settings.connect('changed::' + key, callback));
},
_onDestroy: function(){
for each (let proxy in this.udisksProxies){
if(proxy.drive){
proxy.drive.run_dispose();
}
if(proxy.ata){
proxy.ata.run_dispose();
}
}
this._destroyDriveUtility();
Mainloop.source_remove(this._timeoutId);
for each (let signal in this._settingChangedSignals){
settings.disconnect(signal);
this._settings.disconnect(signal);
};
},
@@ -162,47 +165,48 @@ const FreonMenuButton = new Lang.Class({
if (this.sensorsArgv){
new Utilities.Future(this.sensorsArgv, Lang.bind(this,function(stdout){
this._sensorsOutput = stdout;
this._updateDisplay(this._sensorsOutput, this._hddtempOutput, this._aticonfigOutput);
this._updateDisplay();
}));
}
if (settings.get_boolean('show-hdd-temp') && this.hddtempArgv){
if (this._settings.get_string('drive-utility') == 'hddtemp' && this.hddtempArgv){
new Utilities.Future(this.hddtempArgv, Lang.bind(this,function(stdout){
this._hddtempOutput = stdout;
this._updateDisplay(this._sensorsOutput, this._hddtempOutput, this._aticonfigOutput);
this._updateDisplay();
}));
}
if (settings.get_boolean('show-aticonfig-temp') && this.aticonfigArgv){
if (this._settings.get_boolean('show-aticonfig-temp') && this.aticonfigArgv){
new Utilities.Future(this.aticonfigArgv, Lang.bind(this,function(stdout){
this._aticonfigOutput = stdout;
this._updateDisplay(this._sensorsOutput, this._hddtempOutput, this._aticonfigOutput);
this._updateDisplay();
}));
}
},
_updateDisplay: function(sensors_output, hddtemp_output, aticonfig_output){
_updateDisplay: function(){
let tempInfo = [];
let fanInfo = [];
let voltageInfo = [];
tempInfo = Utilities.parseSensorsOutput(sensors_output,Utilities.parseSensorsTemperatureLine);
tempInfo = Utilities.parseSensorsOutput(this._sensorsOutput,Utilities.parseSensorsTemperatureLine);
tempInfo = tempInfo.filter(Utilities.filterTemperature);
if (settings.get_boolean('show-fan-rpm')){
fanInfo = Utilities.parseSensorsOutput(sensors_output,Utilities.parseFanRPMLine);
if (this._settings.get_boolean('show-fan-rpm')){
fanInfo = Utilities.parseSensorsOutput(this._sensorsOutput,Utilities.parseFanRPMLine);
fanInfo = fanInfo.filter(Utilities.filterFan);
}
if (settings.get_boolean('show-voltage')){
voltageInfo = Utilities.parseSensorsOutput(sensors_output,Utilities.parseVoltageLine);
if (this._settings.get_boolean('show-voltage')){
voltageInfo = Utilities.parseSensorsOutput(this._sensorsOutput,Utilities.parseVoltageLine);
}
if(settings.get_boolean('show-hdd-temp') && this.hddtempArgv)
tempInfo = tempInfo.concat(Utilities.parseHddTempOutput(hddtemp_output, !(/nc$/.exec(this.hddtempArgv[0])) ? ': ' : '|'));
if(this._settings.get_string('drive-utility') == 'hddtemp' && this.hddtempArgv) {
tempInfo = tempInfo.concat(Utilities.parseHddTempOutput(this._hddtempOutput, !(/nc$/.exec(this.hddtempArgv[0])) ? ': ' : '|'));
} else if(this._settings.get_string('drive-utility') == 'udisks2'){
tempInfo = tempInfo.concat(this._udisks2.getHDDTemp());
}
if(settings.get_boolean('show-aticonfig-temp') && this.aticonfigArgv)
tempInfo = tempInfo.concat(Utilities.parseAtiConfigOutput(aticonfig_output));
tempInfo = tempInfo.concat(Utilities.UDisks.create_list_from_proxies(this.udisksProxies));
if(this._settings.get_boolean('show-aticonfig-temp') && this.aticonfigArgv)
tempInfo = tempInfo.concat(Utilities.parseAtiConfigOutput(this._aticonfigOutput));
tempInfo.sort(function(a,b) { return a['label'].localeCompare(b['label']) });
fanInfo.sort(function(a,b) { return a['label'].localeCompare(b['label']) });
@@ -245,13 +249,13 @@ const FreonMenuButton = new Lang.Class({
}
let needAppendMenuItems = false;
let mainSensor = settings.get_string('main-sensor');
let mainSensor = this._settings.get_string('main-sensor');
let sensorCount = 0;
for each (let s in sensorsList) {
if(s.type != 'separator') {
sensorCount++;
if (mainSensor == s.label) {
if(settings.get_boolean('show-label'))
if(this._settings.get_boolean('show-label'))
this.statusLabel.set_text('%s: %s'.format(s.label, s.value));
else
this.statusLabel.set_text(s.value);
@@ -311,12 +315,15 @@ const FreonMenuButton = new Lang.Class({
_appendMenuItems : function(sensorsList){
this._sensorMenuItems = {};
let mainSensor = settings.get_string('main-sensor');
let mainSensor = this._settings.get_string('main-sensor');
for each (let s in sensorsList){
if(s.type == 'separator'){
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
} else {
let item = new FreonItem(this._sensorIcons[s.type], s.label, s.value);
item.connect('activate', Lang.bind(this, function (self) {
this._settings.set_string('main-sensor', self.getLabel());
}));
if (mainSensor == s.label)
item.addMainDot();
this._sensorMenuItems[s.label] = item;
@@ -346,16 +353,16 @@ const FreonMenuButton = new Lang.Class({
},
_formatTemp: function(value) {
if (settings.get_string('unit')=='fahrenheit'){
if (this._settings.get_string('unit')=='fahrenheit'){
value = this._toFahrenheit(value);
}
let format = '%.1f';
if (!settings.get_boolean('show-decimal-value')){
if (!this._settings.get_boolean('show-decimal-value')){
//ret = Math.round(value);
format = '%d';
}
format += '%s';
return format.format(value, (settings.get_string('unit')=='fahrenheit') ? "\u00b0F" : "\u00b0C");
return format.format(value, (this._settings.get_string('unit')=='fahrenheit') ? "\u00b0F" : "\u00b0C");
}
});
@@ -363,7 +370,6 @@ let freonMenu;
function init(extensionMeta) {
Convenience.initTranslations();
settings = Convenience.getSettings();
}
function enable() {

View File

@@ -71,8 +71,34 @@ const FreonPrefsWidget = new GObject.Class({
//
this._addSwitch({key : 'show-hdd-temp', y : i, x : 0,
label : _('Show Drive Temperature')});
// HDD Temperature Utility ComboBox
let hddTempUtilModel = new Gtk.ListStore();
hddTempUtilModel.set_column_types([GObject.TYPE_STRING, GObject.TYPE_STRING]);
let hddTempUtil = new Gtk.ComboBox({model: hddTempUtilModel});
let hddTempUtilRenderer = new Gtk.CellRendererText();
hddTempUtil.pack_start(hddTempUtilRenderer, true);
hddTempUtil.add_attribute(hddTempUtilRenderer, 'text', 1);
let hddTempUtilItems = ['none', 'hddtemp', 'udisks2'];
hddTempUtilModel.set(hddTempUtilModel.append(), [0, 1], [hddTempUtilItems[0], 'None']);
hddTempUtilModel.set(hddTempUtilModel.append(), [0, 1], [hddTempUtilItems[1], 'Hddtemp']);
hddTempUtilModel.set(hddTempUtilModel.append(), [0, 1], [hddTempUtilItems[2], 'UDisks2']);
hddTempUtil.set_active(hddTempUtilItems.indexOf(this._settings.get_string('drive-utility')));
hddTempUtil.connect('changed', Lang.bind(this, function(entry) {
let [success, iter] = hddTempUtil.get_active_iter();
if (!success)
return;
this._settings.set_string('drive-utility', hddTempUtilModel.get_value(iter, 0))
}));
this.attach(new Gtk.Label({ label: _('Utility for HDD/SSD temperature'), halign : Gtk.Align.END}), 0, i, 1, 1);
this.attach(hddTempUtil, 1, i, 1, 1);
//
this._addSwitch({key : 'show-fan-rpm', y : i++, x : 2,
label : _('Show Fan Speed')});
@@ -80,10 +106,9 @@ const FreonPrefsWidget = new GObject.Class({
this._addSwitch({key : 'show-voltage', y : i, x : 0,
label : _('Show Power Supply Voltage')});
this._addSwitch({key : 'show-aticonfig-temp', y : i, x : 2,
this._addSwitch({key : 'show-aticonfig-temp', y : i++, x : 2,
label : _('Use Catalyst'),
help : _('Show AMD video card temperature, use aticonfig from Catalyst driver.')});
},
_addSwitch : function(params){

View File

@@ -12,7 +12,7 @@
<key type="s" name="unit">
<default>'centigrade'</default>
<summary>Unit</summary>
<description>The unit ('centigrade' or 'fahrenheit') the extension should display the temperature in.</description>
<description>The unit ('centigrade' or 'fahrenheit') the extension should display the temperature in</description>
</key>
<key type="b" name="show-label">
@@ -33,28 +33,28 @@
<description>Show one digit after decimal</description>
</key>
<key type="b" name="show-hdd-temp">
<default>true</default>
<summary>Display HDD Temp</summary>
<description>Display hard disk drive temperature. Requires hddtemp installed.</description>
</key>
<key type="b" name="show-fan-rpm">
<default>true</default>
<summary>Display Fan RPM</summary>
<description>Display fan rotation per minute.</description>
<description>Display fan rotation per minute</description>
</key>
<key type="b" name="show-voltage">
<default>true</default>
<summary>Display voltage</summary>
<description>Display voltage of various components.</description>
<description>Display voltage of various components</description>
</key>
<key type="b" name="show-aticonfig-temp">
<default>false</default>
<summary>Show AMD Card Temperature (Catalyst)</summary>
<description>Show AMD card temperature, use aticonfig from Catalyst driver.</description>
<description>Show AMD card temperature, use aticonfig from Catalyst driver</description>
</key>
<key type="s" name="drive-utility">
<default>'none'</default>
<summary>Utility for detect HDD/SSD temperature</summary>
<description>Utility for detect HDD/SSD temperature ('none', 'hddtemp' or 'udisks2')</description>
</key>
</schema>

View File

@@ -0,0 +1,109 @@
const Lang = imports.lang;
const Gio = imports.gi.Gio;
const UDisksDriveProxy = Gio.DBusProxy.makeProxyWrapper(
'<node> \
<interface name="org.freedesktop.UDisks2.Drive"> \
<property type="s" name="Model" access="read"/> \
</interface> \
</node>');
const UDisksDriveAtaProxy = Gio.DBusProxy.makeProxyWrapper(
'<node> \
<interface name="org.freedesktop.UDisks2.Drive.Ata"> \
<property type="d" name="SmartTemperature" access="read"/> \
</interface> \
</node>');
// Poor man's async.js
const Async = {
// mapping will be done in parallel
map: function(arr, mapClb /* function(in, successClb)) */, resClb /* function(result) */) {
let counter = arr.length;
let result = [];
for (let i = 0; i < arr.length; ++i) {
mapClb(arr[i], (function(i, newVal) {
result[i] = newVal;
if (--counter == 0) resClb(result);
}).bind(null, i)); // i needs to be bound since it will be changed during the next iteration
}
}
}
// routines for handling of udisks2
const UDisks2 = new Lang.Class({
Name: 'UDisks2',
_init: function(callback) {
this._udisksProxies = [];
this._get_drive_ata_proxies(Lang.bind(this, function(proxies) {
this._udisksProxies = proxies;
callback();
}));
},
// creates a list of sensor objects from the list of proxies given
getHDDTemp: function() {
return this._udisksProxies.filter(function(proxy) {
// 0K means no data available
return proxy.ata.SmartTemperature > 0;
}).map(function(proxy) {
return {
label: proxy.drive.Model,
temp: proxy.ata.SmartTemperature - 272.15
};
});
},
// calls callback with [{ drive: UDisksDriveProxy, ata: UDisksDriveAtaProxy }, ... ] for every drive that implements both interfaces
_get_drive_ata_proxies: function(callback) {
Gio.DBusObjectManagerClient.new(Gio.DBus.system, 0, "org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", null, null, function(src, res) {
try {
let objMgr = Gio.DBusObjectManagerClient.new_finish(res); //might throw
let objPaths = objMgr.get_objects().filter(function(o) {
return o.get_interface("org.freedesktop.UDisks2.Drive") != null
&& o.get_interface("org.freedesktop.UDisks2.Drive.Ata") != null;
}).map(function(o) { return o.get_object_path() });
// now create the proxy objects, log and ignore every failure
Async.map(objPaths, function(obj, callback) {
// create the proxies object
let driveProxy = new UDisksDriveProxy(Gio.DBus.system, "org.freedesktop.UDisks2", obj, function(res, error) {
if (error) { //very unlikely - we even checked the interfaces before!
global.log('[FREON] Could not create proxy on ' + obj + ':' + error);
callback(null);
return;
}
let ataProxy = new UDisksDriveAtaProxy(Gio.DBus.system, "org.freedesktop.UDisks2", obj, function(res, error) {
if (error) {
global.log('[FREON] Could not create proxy on ' + obj + ':' + error);
callback(null);
return;
}
callback({ drive: driveProxy, ata: ataProxy });
});
});
}, function(proxies) {
// filter out failed attempts == null values
callback(proxies.filter(function(a) { return a != null; }));
});
} catch (e) {
global.log('[FREON] Could not find UDisks2 objects: ' + e);
}
});
},
destroy: function(callback) {
for each (let proxy in this._udisksProxies){
if(proxy.drive){
proxy.drive.run_dispose();
}
if(proxy.ata){
proxy.ata.run_dispose();
}
}
},
});

View File

@@ -1,25 +1,10 @@
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Gettext = imports.gettext.domain(Me.metadata['gettext-domain']);
const _ = Gettext.gettext;
const UDisksDriveProxy = Gio.DBusProxy.makeProxyWrapper(
'<node> \
<interface name="org.freedesktop.UDisks2.Drive"> \
<property type="s" name="Model" access="read"/> \
</interface> \
</node>');
const UDisksDriveAtaProxy = Gio.DBusProxy.makeProxyWrapper(
'<node> \
<interface name="org.freedesktop.UDisks2.Drive.Ata"> \
<property type="d" name="SmartTemperature" access="read"/> \
</interface> \
</node>');
function detectSensors() {
let path = GLib.find_program_in_path('sensors');
return path ? [path] : undefined;
@@ -272,78 +257,3 @@ const Future = new Lang.Class({
}
});
// Poor man's async.js
const Async = {
// mapping will be done in parallel
map: function(arr, mapClb /* function(in, successClb)) */, resClb /* function(result) */) {
let counter = arr.length;
let result = [];
for (let i = 0; i < arr.length; ++i) {
mapClb(arr[i], (function(i, newVal) {
result[i] = newVal;
if (--counter == 0) resClb(result);
}).bind(null, i)); // i needs to be bound since it will be changed during the next iteration
}
}
}
function debug(str){
//tail -f -n100 ~/.cache/gdm/session.log | grep temperature
print ('LOG temperature@xtranophilist: ' + str);
}
// routines for handling of udisks2
const UDisks = {
// creates a list of sensor objects from the list of proxies given
create_list_from_proxies: function(proxies) {
return proxies.filter(function(proxy) {
// 0K means no data available
return proxy.ata.SmartTemperature > 0;
}).map(function(proxy) {
return {
label: proxy.drive.Model,
temp: proxy.ata.SmartTemperature - 272.15
};
});
},
// calls callback with [{ drive: UDisksDriveProxy, ata: UDisksDriveAtaProxy }, ... ] for every drive that implements both interfaces
get_drive_ata_proxies: function(callback) {
Gio.DBusObjectManagerClient.new(Gio.DBus.system, 0, "org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", null, null, function(src, res) {
try {
let objMgr = Gio.DBusObjectManagerClient.new_finish(res); //might throw
let objPaths = objMgr.get_objects().filter(function(o) {
return o.get_interface("org.freedesktop.UDisks2.Drive") != null
&& o.get_interface("org.freedesktop.UDisks2.Drive.Ata") != null;
}).map(function(o) { return o.get_object_path() });
// now create the proxy objects, log and ignore every failure
Async.map(objPaths, function(obj, callback) {
// create the proxies object
let driveProxy = new UDisksDriveProxy(Gio.DBus.system, "org.freedesktop.UDisks2", obj, function(res, error) {
if (error) { //very unlikely - we even checked the interfaces before!
debug("Could not create proxy on "+obj+":"+error);
callback(null);
return;
}
let ataProxy = new UDisksDriveAtaProxy(Gio.DBus.system, "org.freedesktop.UDisks2", obj, function(res, error) {
if (error) {
debug("Could not create proxy on "+obj+":"+error);
callback(null);
return;
}
callback({ drive: driveProxy, ata: ataProxy });
});
});
}, function(proxies) {
// filter out failed attempts == null values
callback(proxies.filter(function(a) { return a != null; }));
});
} catch (e) {
debug("Could not find UDisks objects: "+e);
}
});
}
};