Skip to content

Commit 2f7acd7

Browse files
committed
graceful exit for all interactive scripts
1 parent f7980d5 commit 2f7acd7

File tree

14 files changed

+219
-100
lines changed

14 files changed

+219
-100
lines changed

doc/SETTINGS.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ All local HTML page settings are stored in a single JavaScript object named ``pe
1010

1111
```javascript
1212
var pebSettings = {};
13-
pebSettings.autoStartScripts = ['interactive_one', 'interactive_two'];
13+
pebSettings.onStartScripts = ['interactive_one', 'interactive_two'];
1414
pebSettings.cutLabel = "Custom Cut Label";
1515
pebSettings.copyLabel = "Custom Copy Label";
1616
pebSettings.pasteLabel = "Custom Paste Label";
@@ -22,11 +22,12 @@ pebSettings.noLabel = "Custom No Label";
2222
pebSettings.closeConfirmation =
2323
'Text was entered in a form and it is going to be lost!\n' +
2424
'Are you sure you want to close the window?';
25+
pebSettings.onExitScripts = ['interactive_one', 'interactive_two'];
2526
```
2627

2728
The ``pebSettings`` JavaScript object may have the following properties:
2829

29-
* **autoStartScripts**
30+
* **onStartScripts**
3031
``Array`` of Perl scripts that are started immediately after a local page is loaded
3132

3233
* **cutLabel**
@@ -54,7 +55,10 @@ The ``pebSettings`` JavaScript object may have the following properties:
5455
``String`` displayed as a label for the 'No' button on JavaScript Confirm popup box.
5556

5657
* **closeConfirmation**
57-
``String`` displayed in a JavaScript Confirm popup box when the close button is pressed, but unsaved data in local HTML forms is detected. If no ``closeConfirmation`` object property is found, PEB shuts down all running Perl scripts and exits.
58+
``String`` displayed in a JavaScript Confirm popup box when the close button is pressed, but unsaved data in local HTML forms is detected. If no ``closeConfirmation`` object property is found, PEB shuts down all running Perl scripts and exits.
59+
60+
* **onExitScripts**
61+
``Array`` of Perl scripts that are invoked before application exit
5862

5963
## Perl Scripts API
6064

@@ -114,7 +118,7 @@ A JavaScript settings object for a Perl script run by PEB has the following prop
114118
```
115119

116120
Please note that many Perl scripts do not give their STDOUT data in a single shot.
117-
If several chunks of output have to be combined, this should also be done at JavaScript level:
121+
If several chunks of output have to be combined, this should be done at JavaScript level:
118122

119123
```javascript
120124
var accumulatedOutput;
@@ -138,6 +142,11 @@ A JavaScript settings object for a Perl script run by PEB has the following prop
138142
}
139143
```
140144

145+
* **exitData**
146+
``String`` or ``Function`` supplying script exit command as its return value
147+
``exitData`` is written on script STDIN.
148+
149+
141150
## Interactive Perl Scripts
142151

143152
Each PEB interactive Perl script must have its own event loop waiting constantly for new data on STDIN or in a temporary file for a bidirectional connection with PEB. Many interactive scripts can be started simultaneously in one PEB instance. One script may be started in many instances, provided that each of them has a JavaScript settings object with an unique name.

resources/app/index-windows.html

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<script>
1010
// PEB page settings:
1111
var pebSettings = {}; // 'pebSettings' object name is hard-coded.
12-
pebSettings.autoStartScripts = ['interactive_one', 'interactive_two'];
12+
pebSettings.onStartScripts = ['interactive_one', 'interactive_two'];
1313
pebSettings.cutLabel = '- Cut -';
1414
pebSettings.copyLabel = '- Copy -';
1515
pebSettings.pasteLabel = '- Paste -';
@@ -21,6 +21,7 @@
2121
pebSettings.closeConfirmation =
2222
'Text was entered in a form and it is going to be lost!\n' +
2323
'Are you sure you want to close the application?';
24+
pebSettings.onExitScripts = ['messenger_one', 'messenger_two'];
2425

2526
// PEB settings objects for auto-started Perl interactive scripts:
2627
var messenger_one = {};
@@ -34,6 +35,11 @@
3435
return JSON.stringify(messenger_one.message);
3536
}
3637

38+
messenger_one.exitData = function() {
39+
messenger_one.message.user_input = "peb-exit";
40+
return JSON.stringify(messenger_one.message);
41+
}
42+
3743
var messenger_two = {};
3844
messenger_two.scriptRelativePath = 'perl-scripts/messenger.pl';
3945
messenger_two.message = {};
@@ -45,6 +51,11 @@
4551
return JSON.stringify(messenger_two.message);
4652
}
4753

54+
messenger_two.exitData = function() {
55+
messenger_two.message.user_input = "peb-exit";
56+
return JSON.stringify(messenger_two.message);
57+
}
58+
4859
var interactive_one = {};
4960
interactive_one.scriptRelativePath = 'perl-scripts/interactive-windows.pl';
5061

@@ -156,11 +167,13 @@
156167
<li>
157168
<a href="javascript:location.reload();">Reload</a>
158169
</li>
170+
171+
<li>
172+
<a href="https://www.google.com/">Google</a>
173+
</li>
159174
</ul>
160175
</li>
161176

162-
<li><a href="https://www.google.com/">Google</a></li>
163-
164177
<li><a href="about.function">About</a></li>
165178
</ul>
166179
</div>

resources/app/index.html

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<script>
1010
// PEB page settings:
1111
var pebSettings = {}; // 'pebSettings' object name is hard-coded.
12-
pebSettings.autoStartScripts = ['interactive_one', 'interactive_two'];
12+
pebSettings.onStartScripts = ['interactive_one', 'interactive_two'];
1313
pebSettings.cutLabel = '- Cut -';
1414
pebSettings.copyLabel = '- Copy -';
1515
pebSettings.pasteLabel = '- Paste -';
@@ -21,17 +21,18 @@
2121
pebSettings.closeConfirmation =
2222
'Text was entered in a form and it is going to be lost!\n' +
2323
'Are you sure you want to close the application?';
24+
pebSettings.onExitScripts = ['interactive_one', 'interactive_two'];
2425

2526
// PEB settings objects for auto-started Perl interactive scripts:
2627
var interactive_one = {};
2728
interactive_one.scriptRelativePath = 'perl-scripts/interactive.pl';
2829

2930
interactive_one.inputData = function() {
30-
var input = {}
31-
input.mode = "unix-epoch";
32-
input.user_input = document.getElementById("interactive-one-input").value;
31+
var data = {}
32+
data.mode = "unix-epoch";
33+
data.user_input = document.getElementById("interactive-one-input").value;
3334
$('#form-one').trigger('reset');
34-
return JSON.stringify(input);
35+
return JSON.stringify(data);
3536
}
3637

3738
interactive_one.stdoutFunction = function (stdout) {
@@ -46,15 +47,22 @@
4647
target.innerHTML = html;
4748
}
4849

50+
interactive_one.exitData = function() {
51+
var data = {}
52+
data.mode = "unix-epoch";
53+
data.user_input = "peb-exit";
54+
return JSON.stringify(data);
55+
}
56+
4957
var interactive_two = {};
5058
interactive_two.scriptRelativePath = 'perl-scripts/interactive.pl';
5159

5260
interactive_two.inputData = function() {
53-
var input = {}
54-
input.mode = "local-time";
55-
input.user_input = document.getElementById("interactive-two-input").value;
56-
$('#form-one').trigger('reset');
57-
return JSON.stringify(input);
61+
var data = {}
62+
data.mode = "local-time";
63+
data.user_input = document.getElementById("interactive-two-input").value;
64+
$('#form-two').trigger('reset');
65+
return JSON.stringify(data);
5866
}
5967

6068
interactive_two.stdoutFunction = function (stdout) {
@@ -68,6 +76,13 @@
6876
};
6977
target.innerHTML = html;
7078
}
79+
80+
interactive_two.exitData = function() {
81+
var data = {}
82+
data.mode = "unix-epoch";
83+
data.user_input = "peb-exit";
84+
return JSON.stringify(data);
85+
}
7186
</script>
7287

7388
<script src="jquery/jquery-1.12.4.min.js"></script>
@@ -132,11 +147,13 @@
132147
<li>
133148
<a href="javascript:location.reload();">Reload</a>
134149
</li>
150+
151+
<li>
152+
<a href="https://www.google.com/">Google</a>
153+
</li>
135154
</ul>
136155
</li>
137156

138-
<li><a href="https://www.google.com/">Google</a></li>
139-
140157
<li><a href="about.function">About</a></li>
141158
</ul>
142159
</div>

resources/app/perl-scripts/interactive-windows.pl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
1;
6161
};
6262

63+
if ($user_input =~ "peb-exit") {
64+
shutdown_procedure();
65+
}
66+
6367
my $time;
6468

6569
if ($mode =~ "unix-epoch") {

resources/app/perl-scripts/interactive.pl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535

3636
my $input = get_input($stdin);
3737
$user_input = decode('UTF-8', $input->{user_input});
38+
39+
if ($user_input =~ "peb-exit") {
40+
shutdown_procedure();
41+
}
3842
}
3943
);
4044

src/resources/peb.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,60 @@
22
var peb = {};
33

44
peb.getPageSettings = function() {
5-
if (window[pebSettings] !== null) {
6-
return JSON.stringify(pebSettings);
7-
}
5+
if (window[pebSettings] !== null) {
6+
return JSON.stringify(pebSettings);
7+
}
88
}
99

1010
peb.getDialogSettings = function(dialogSettings) {
11-
if (window[dialogSettings] !== null) {
12-
return JSON.stringify(dialogSettings);
13-
}
11+
if (window[dialogSettings] !== null) {
12+
return JSON.stringify(dialogSettings);
13+
}
1414
}
1515

1616
peb.getScriptSettings = function(scriptSettings) {
17-
if (window[scriptSettings] !== null) {
18-
if (typeof scriptSettings.inputData === 'function') {
19-
scriptSettings.scriptInput = scriptSettings.inputData();
20-
} else {
21-
scriptSettings.scriptInput = scriptSettings.inputData;
17+
if (window[scriptSettings] !== null) {
18+
if (typeof scriptSettings.inputData === 'function') {
19+
scriptSettings.scriptInput = scriptSettings.inputData();
20+
} else {
21+
scriptSettings.scriptInput = scriptSettings.inputData;
22+
}
23+
24+
if (typeof scriptSettings.exitData === 'function') {
25+
scriptSettings.exitCommand = scriptSettings.exitData();
26+
} else {
27+
scriptSettings.exitCommand = scriptSettings.exitData;
28+
}
29+
30+
return JSON.stringify(scriptSettings);
2231
}
23-
24-
return JSON.stringify(scriptSettings);
25-
}
2632
}
2733

2834
peb.checkUserInputBeforeClose = function() {
29-
var textEntered = false;
30-
var close = true;
35+
var textEntered = false;
36+
var close = true;
3137

32-
var textFields = [];
33-
textFields = document.getElementsByTagName('textarea');
38+
var textFields = [];
39+
textFields = document.getElementsByTagName('textarea');
3440

35-
for (index = 0; index < textFields.length; index++) {
36-
if (textFields[index].value.length > 0) {
37-
textEntered = true;
41+
for (index = 0; index < textFields.length; index++) {
42+
if (textFields[index].value.length > 0) {
43+
textEntered = true;
44+
}
3845
}
39-
}
4046

41-
var inputBoxes = [];
42-
inputBoxes = document.querySelectorAll('input[type=text]');
47+
var inputBoxes = [];
48+
inputBoxes = document.querySelectorAll('input[type=text]');
4349

44-
for (index = 0; index < inputBoxes.length; index++) {
45-
if (inputBoxes[index].value.length > 0) {
46-
textEntered = true;
50+
for (index = 0; index < inputBoxes.length; index++) {
51+
if (inputBoxes[index].value.length > 0) {
52+
textEntered = true;
53+
}
4754
}
48-
}
4955

50-
if (textEntered === true && pebSettings.closeConfirmation !== null) {
51-
close = confirm(pebSettings.closeConfirmation);
52-
}
56+
if (textEntered === true && pebSettings.closeConfirmation !== null) {
57+
close = confirm(pebSettings.closeConfirmation);
58+
}
5359

54-
return close;
60+
return close;
5561
}

src/webengine-page.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ QPage::QPage()
5252
QObject::connect(this, SIGNAL(loadFinished(bool)),
5353
this, SLOT(qPageLoadedSlot(bool)));
5454

55-
// Signal and slot for closing window from JavaScript:
56-
QObject::connect(this, SIGNAL(windowCloseRequested()),
57-
this, SLOT(qCloseAllScriptsSlot()));
58-
5955
// Default dialog and context menu labels:
6056
okLabel = "Ok";
6157
cancelLabel = "Cancel";
@@ -95,7 +91,7 @@ bool QPage::acceptNavigationRequest(const QUrl &url,
9591

9692
aboutText.replace("APPLICATION_VERSION",
9793
QApplication::applicationVersion());
98-
aboutText.replace("QT_VERSION", QT_VERSION_STR, QT_);
94+
aboutText.replace("QT_VERSION", QT_VERSION_STR);
9995

10096
QPixmap icon(32, 32);
10197
icon.load(":/icon/camel.png");

0 commit comments

Comments
 (0)