Skip to content

Commit d953a98

Browse files
Put code in external JS to decouple notifications, settings, and command running since it looks like python of C++ is going to have to get involved.
1 parent ef36ebb commit d953a98

File tree

5 files changed

+80
-35
lines changed

5 files changed

+80
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
var lastRun = "Never";
3+
4+
function runCommand(commandString){
5+
lastRun = new Date();
6+
console.log("Running Command..." + commandString);
7+
return plasmoid.runCommand(commandString);
8+
}
9+
10+
function getLastRunTime(){
11+
return lastRun;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
function sendNotification(summaryText, bodyText){
3+
4+
console.log("Sending notification: " + bodyText);
5+
var service = dataEngine("notifications").serviceForSource("notification");
6+
var operation = service.operationDescription("createNotification");
7+
8+
operation["appName"] = "Min-Status-Widget";
9+
operation["appIcon"] = plasmoid.file("images", "ok.png");
10+
operation["summary"] = summaryText;
11+
operation["body"] = bodyText;
12+
operation["timeout"] = 3000;
13+
14+
service.startOperationCall(operation);
15+
console.log(service);
16+
}
17+
18+
function sendSuccessNotifiaction(name){
19+
sendNotification(
20+
name + " Succeeded!",
21+
name + " command ran successfully."
22+
);
23+
}
24+
25+
function sendFailureNotifiaction(name){
26+
sendNotification(
27+
name + " Failed!",
28+
name + " command failed."
29+
);
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
function getCommandToRun(){
3+
return "/home/jschindler/src/min-status-widget/resources/randomSuccess.sh";
4+
}
5+
6+
function doNotification(){
7+
return true;
8+
}
9+
+27-35
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,51 @@
11
import QtQuick 1.1
2+
import "../code/js/NotificationHelper.js" as NotificationHelper
3+
import "../code/js/CommandRunner.js" as CommandRunner
4+
import "../code/js/SettingsManager.js" as SettingsManager
25

36
Item {
47

58
Image {
9+
id: widgetImage
610

711
property string okImage: "plasmapackage:/images/ok.png"
812
property string errorImage: "plasmapackage:/images/error.png"
913
property string workingImage: "plasmapackage:/images/gear.png"
10-
property string nextImage: widgetImage.errorImage
11-
property int commandInterval: 1000
14+
property bool isRunning: false
1215

13-
id: widgetImage
14-
source: widgetImage.okImage
16+
source: widgetImage.workingImage
1517
anchors.centerIn: parent
1618
width: parent.width
1719
height: parent.height
1820
fillMode: Image.PreserveAspectFit
1921

2022
MouseArea {
2123
anchors.fill: parent
22-
onClicked: widgetImage.runCommand()
24+
onClicked: widgetImage.go()
2325
}
2426

25-
function switchImages(){
26-
var nextImage = widgetImage.nextImage;
27-
widgetImage.nextImage = widgetImage.source;
28-
widgetImage.source = nextImage;
29-
}
30-
31-
function sendNotification(){
32-
console.log("Clicked!");
33-
var service = dataEngine("notifications").serviceForSource("notification");
34-
var operation = service.operationDescription("createNotification");
35-
operation["appName"] = "Min-Status-Widget";
36-
operation["appIcon"] = plasmoid.file("images", "ok.png");
37-
operation["summary"] = "Something Happened!";
38-
operation["body"] = "I'm assuming that this should be longer...";
39-
operation["timeout"] = 3000;
40-
41-
service.startOperationCall(operation);
42-
console.log(service.serviceReady(service));
43-
}
44-
45-
function runCommand(){
46-
widgetImage.source = widgetImage.workingImage;
47-
console.log("Running Command...");
48-
var result = plasmoid.runCommand("/home/jschindler/src/min-status-widget/resources/randomSuccess.sh");
49-
sendNotification();
50-
console.log(result);
27+
Timer {
28+
id: myTimer
29+
interval: 3000; running: false; repeat: true
30+
onTriggered: widgetImage.go()
5131
}
5232

53-
Timer {
54-
id: "myTimer"
55-
interval: 500; running: true; repeat: true
56-
onTriggered: widgetImage.switchImages()
33+
Component.onCompleted: myTimer.running = true
34+
35+
function go(){
36+
if(!isRunning){
37+
isRunning = true;
38+
source = workingImage;
39+
var result = CommandRunner.runCommand(SettingsManager.getCommandToRun());
40+
if(result){
41+
NotificationHelper.sendSuccessNotifiaction("MyCommand");
42+
source = okImage;
43+
} else {
44+
NotificationHelper.sendFailureNotifiaction("MyCommand");
45+
source = errorImage;
46+
}
47+
isRunning = false
48+
}
5749
}
5850
}
5951
}

resources/randomSuccess.sh

+2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
result=$[ ( $RANDOM % 100 ) + 1 ]
44

55
if [ $result -lt 50 ] ; then
6+
echo "Returning error"
67
exit 1
78
fi
89

10+
echo "Returning success"
911
exit 0

0 commit comments

Comments
 (0)