Difference between revisions of "Development/Tutorials/Plasma/JavaScript/SystemMonitor"

Jump to: navigation, search
(cleanup link name to NowPlaying)
m (Fix syntax highlighting.)
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This example assumes that you are familiar with previous examples such as [[Development/Tutorials/Plasma/QtScript/NowPlaying|NowPlaying]] (from which this example was adapted).
+
This example assumes that you are familiar with previous examples such as [[Development/Tutorials/Plasma/JavaScript/NowPlaying|NowPlaying]] (from which this example was adapted).
  
 
Following code illustrates how to connect to systemmonitor and how to receive updates from various data sources.
 
Following code illustrates how to connect to systemmonitor and how to receive updates from various data sources.
  
 
You should have a working plasmoid application from previous examples. Take a copy of the plasmoid and rename it. After this you can replace contents of the <tt>contents/code/main.js</tt> with following:
 
You should have a working plasmoid application from previous examples. Take a copy of the plasmoid and rename it. After this you can replace contents of the <tt>contents/code/main.js</tt> with following:
<code>
+
<syntaxhighlight lang="javascript">
 
layout = new LinearLayout(plasmoid);
 
layout = new LinearLayout(plasmoid);
  
Line 26: Line 26:
 
}
 
}
  
plasmoid.dataUpdate = function(name, data) {
+
plasmoid.dataUpdated = function(name, data) {
 
     systemData[name] = data;
 
     systemData[name] = data;
 
     printData();
 
     printData();
 
};
 
};
  
smDataEngine = plasmoid.dataEngine("systemmonitor");
+
smDataEngine = dataEngine("systemmonitor");
  
 
smDataEngine.sourceRemoved.connect(function(name) {
 
smDataEngine.sourceRemoved.connect(function(name) {
Line 45: Line 45:
 
     });
 
     });
  
</code>
+
</syntaxhighlight>
  
In smDataEngine.sourceAdded.connect() we give a function to connect to signal. The signal connection function is called by the systemmonitor for each data source. In this example we only connect to physical memory data sources.
+
In <tt>smDataEngine.sourceAdded.connect()</tt> we give a function to connect to signal. The signal connection function is called by the systemmonitor for each data source. In this example we only connect to physical memory data sources.
  
After this the plasmoid.dataUpdate() receives updates. Update values are stored into an array. The array is printed on every update.
+
After this the <tt>plasmoid.dataUpdate()</tt> receives updates. Update values are stored into an array. The array is printed on every update.
  
Note that this example is bit inefficient since we call printData() on every update. One way to optimize is to update label text only when "mem/physical/free" data source is updated.
+
Note that this example is bit inefficient since we call <tt>printData()</tt> on every update. One way to optimize is to update label text only when "mem/physical/free" data source is updated.

Revision as of 12:17, 30 June 2011

This example assumes that you are familiar with previous examples such as NowPlaying (from which this example was adapted).

Following code illustrates how to connect to systemmonitor and how to receive updates from various data sources.

You should have a working plasmoid application from previous examples. Take a copy of the plasmoid and rename it. After this you can replace contents of the contents/code/main.js with following:

layout = new LinearLayout(plasmoid);
 
label = new Label();
layout.addItem(label);
label.text = "No connection";
 
// current values
var systemData = new Array();
 
function printData() {
    label.text = "";
    for (var name in systemData) {
	var data = systemData[name];
	label.text = label.text + name + ": ";
	for (var elt in data) {
	    label.text = label.text + " " + data[elt];
	}
	label.text = label.text + "\n";
    }
}
 
plasmoid.dataUpdated = function(name, data) {
    systemData[name] = data;
    printData();
};
 
smDataEngine = dataEngine("systemmonitor");
 
smDataEngine.sourceRemoved.connect(function(name) {
	// unsubscribe
	smDataEngine.disconnectSource(name, plasmoid);
    });
 
smDataEngine.sourceAdded.connect(function(name) {
	if (name.toString().match("^mem/physical")) {
	    // subscribe
	    smDataEngine.connectSource(name, plasmoid, 500);
	}
    });

In smDataEngine.sourceAdded.connect() we give a function to connect to signal. The signal connection function is called by the systemmonitor for each data source. In this example we only connect to physical memory data sources.

After this the plasmoid.dataUpdate() receives updates. Update values are stored into an array. The array is printed on every update.

Note that this example is bit inefficient since we call printData() on every update. One way to optimize is to update label text only when "mem/physical/free" data source is updated.


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal