SHARE
TWEET

applet.js

a guest Feb 27th, 2016 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Settings = imports.ui.settings; // ++ Needed if you use Settings / Configuración
  2. const St = imports.gi.St; //Componente Visual
  3. const Pango = imports.gi.Pango; //Parametrizador de componente Visual
  4. const Mainloop = imports.mainloop; //Para realizar Blucles de Ejecución
  5. const Lang = imports.lang; //Controlador
  6. const Applet = imports.ui.applet; //Applet
  7. const GLib = imports.gi.GLib; //Libreria para el control de ejecuciones nivel Comandos Linux
  8. const PanelMenu = imports.ui.panelMenu;
  9. const PopupMenu = imports.ui.popupMenu;
  10.  
  11. function MyApplet(metadata, orientation, panelHeight, instance_id) {
  12.     this._init(metadata, orientation, panelHeight, instance_id);
  13. }
  14.  
  15. MyApplet.prototype = {
  16.     __proto__: Applet.IconApplet.prototype,
  17.     _init: function (metadata, orientation, panelHeight, instance_id) {
  18.         Applet.TextApplet.prototype._init.call(this, orientation, panelHeight, instance_id);
  19.         Applet.IconApplet.prototype._init.call(this, orientation, panelHeight, instance_id);
  20.         try {
  21.             //**********************************
  22.             //*********CONFIGURACIONES *********
  23.             this.settings = new Settings.AppletSettings(this, metadata.uuid, instance_id); // ++ Picks up UUID from metadata for Settings
  24.             this.orient = orientation;
  25.             //DEFINICIÓN DE VARIABLES
  26.             this.sensorsPath = this._detectSensors();
  27.             let labelText = "Sensors";
  28.             let icon = this.actor.get_children()[0];
  29.             this.menuManager = new PopupMenu.PopupMenuManager(this);
  30.             this.menu = new Applet.AppletPopupMenu(this, this.orient);
  31.             this.menuManager.addMenu(this.menu);
  32.  
  33.             this._contentSection = new PopupMenu.PopupMenuSection();
  34.             this.menu.addMenuItem(this._contentSection);
  35.  
  36.             //this.actor.remove_actor(icon);
  37.             //Caja Principal
  38.             let box = new St.BoxLayout({
  39.                 name: 'fanBox'
  40.             });
  41.             this.actor.add_actor(box);
  42.             //Definición de Label de Texto de Información
  43.             this._mainLabel = new St.Label();
  44.             this._mainLabel.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
  45.             //Anexamos el Texto
  46.             box.add(this._mainLabel, {
  47.                 y_align: St.Align.MIDDLE,
  48.                 y_fill: false
  49.             });
  50.             //Le asignamos texto al Label principal
  51.             this._mainLabel.set_text(labelText);
  52.  
  53.             this._SettingsSystem(); //INICIAMOS PROCESO DE LEER CONFIGURACION
  54.             //Ejecutamos el evento que actualiza la información de la velocidad del Fan
  55.             this._updateFanSensor();
  56.         } catch (e) {
  57.             //global.logError(e);
  58.         }
  59.     },
  60.     on_applet_clicked: function (event) {
  61.         //Mostrar/Ocultar Menu con cada Click en el Applet
  62.         this.menu.toggle();
  63.     },
  64.     // ++ Function called when settings are changed
  65.     on_settings_changed: function () {
  66.         this._SettingsSystem();
  67.     },
  68.  
  69.     _SettingsSystem: function () {
  70.         //CARGA DE CONFIGURACIONES DE SETTINGS-SCHEMA.JSON
  71.         this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
  72.             "refreshInterval", // The setting key
  73.             "refreshInterval", // The property to manage (this.refreshInterval)
  74.             this.on_settings_changed, // Callback when value changes
  75.             null); // Optional callback data
  76.  
  77.         this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
  78.             "SearchFan", // The setting key
  79.             "SearchFan", // The property to manage
  80.             this.on_settings_changed, // Callback when value changes
  81.             null); // Optional callback data
  82.         this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
  83.             "SearchCPU", // The setting key
  84.             "SearchCPU", // The property to manage
  85.             this.on_settings_changed, // Callback when value changes
  86.             null); // Optional callback data
  87.         this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
  88.             "ScriptFilter", // The setting key
  89.             "ScriptFilter", // The property to manage
  90.             this.on_settings_changed, // Callback when value changes
  91.             null); // Optional callback data
  92.  
  93.         this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
  94.             "ScriptFilter_Init", // The setting key
  95.             "ScriptFilter_Init", // The property to manage
  96.             this.on_settings_changed, // Callback when value changes
  97.             null); // Optional callback data
  98.  
  99.         this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
  100.             "ScriptFilter_End", // The setting key
  101.             "ScriptFilter_End", // The property to manage
  102.             this.on_settings_changed, // Callback when value changes
  103.             null); // Optional callback data
  104.  
  105.         this.settings.bindProperty(Settings.BindingDirection.IN, // Setting type
  106.             "ToolTipApplet", // The setting key
  107.             "ToolTipApplet", // The property to manage
  108.             this.on_settings_changed, // Callback when value changes
  109.             null); // Optional callback data
  110.  
  111.  
  112.         //Le anexamos tooltip al componente
  113.         this.set_applet_tooltip(_(this.ToolTipApplet));
  114.         //**********************************
  115.     },
  116.  
  117.     //EVENTO PARA ACTUALIZAR LA INFORMACION DE LA VELOCIDAD DEL FAN
  118.     _updateFanSensor: function () {
  119.         let sensors_output = GLib.spawn_command_line_sync(this.sensorsPath); //get the output of the sensors command
  120.         let tempInfo, tempCPU;
  121.         try {
  122.             let sensors_output = GLib.spawn_command_line_sync(this.sensorsPath); //get the output of the sensors command
  123.             let tempInfo;
  124.             if (sensors_output[0]) {
  125.                 tempInfo = this._findSearchOutput(sensors_output[1].toString(), this.SearchFan).replace("Right Fan: ","RF: ").replace("Left Fan: ","LF: ");
  126.                 //Reasignamos el valor false, para que no afecte al resto de busquedas
  127.                 this.ScriptFilter = false;
  128.                 tempCPU = this._findSearchOutput(sensors_output[1].toString(), this.SearchCPU);
  129.                 tempCPU = tempCPU.substring(tempCPU.indexOf("+"));
  130.             }
  131.  
  132.             this._mainLabel.set_text(tempInfo + " | " + tempCPU);
  133.             //Anexo de Menu
  134.             //Prelimpieza de menu
  135.             this.menu.box.get_children().forEach(function (c) {
  136.                 c.destroy()
  137.             });
  138.             let items = new Array();
  139.             let section = new PopupMenu.PopupMenuSection("Temperature");
  140.  
  141.             //Anexo de Información Detallada
  142.             let senses_lines = sensors_output[1].toString().split("\n");
  143.             for (let i = 0; i < senses_lines.length; i++) {
  144.                 //Corrección a espacios vacios
  145.                 if (senses_lines[i].toString().trim().length > 0) {
  146.                     let item = new PopupMenu.PopupMenuItem("");
  147.                     //-----net
  148.                     item.addActor(new St.Label({
  149.                         text: senses_lines[i]
  150.                             /*,
  151.                             style_class: "sm-label"*/
  152.                     }));
  153.                     //-----
  154.                     /* item.connect('activate', function () {
  155.                         //    Util.spawn(command);
  156.                     });*/
  157.                     //-----
  158.                     section.addMenuItem(item);
  159.                 }
  160.             }
  161.  
  162.             this.menu.addMenuItem(section);
  163.             Mainloop.timeout_add((this.refreshInterval * 1000), Lang.bind(this, this._updateFanSensor));
  164.         } catch (e) {
  165.             this._updateFanSensor();
  166.         }
  167.     },
  168.     //DETECTA LA UBICACIÓN DE LA FUNCION SENSORS
  169.     _detectSensors: function () {
  170.         //detect if sensors is installed
  171.         let ret = GLib.spawn_command_line_sync("which sensors");
  172.         if ((ret[0]) && (ret[3] == 0)) { //if yes
  173.             return ret[1].toString().split("\n", 1)[0]; //find the path of the sensors
  174.         }
  175.         return null;
  176.     },
  177.     // BUSCA LA INFORMACIÓN DE LA VELOCIDAD DEL FAN Y OTROS ELEMENTOS
  178.     // ESTE CODIGO ESTA SUJETO A CAMBIOS POR EFECTOS DE NO ESTAR ESCRITO ADECUADAMENTE - 22/02/15 - FEBRERO-15 - 03:51 PM
  179.     _findSearchOutput: function (txt, SearchElement) {
  180.         let senses_lines = txt.split("\n");
  181.         let line = '';
  182.         //iterate through each lines
  183.         for (let i = 0; i < senses_lines.length; i++) {
  184.             line = senses_lines[i];
  185.             if (line.indexOf(SearchElement) >= 0) {
  186.                 if (this.ScriptFilter) {
  187.                     return line.substring(this.ScriptFilter_Init, this.ScriptFilter_End) + ' RPM';
  188.                 }
  189.                 return line;
  190.             }
  191.         }
  192.         return senses_lines[6].substring(13);
  193.     }
  194. };
  195.  
  196.  
  197. function main(metadata, orientation, panelHeight, instance_id) {
  198.     let myApplet = new MyApplet(metadata, orientation, panelHeight, instance_id);
  199.     return myApplet;
  200. }
RAW Paste Data
Want to get better at JavaScript?
Learn to code JavaScript in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top