forked from sehraf/Lua4RS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLua4RSWidget.cpp
686 lines (557 loc) · 20 KB
/
Lua4RSWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#include <assert.h>
#include <QMessageBox>
#include <QModelIndex>
#include <QTreeWidgetItem>
#include <QWidget>
#include "ui_Lua4RSWidget.h"
#include "Lua4RSWidget.h"
#include "Lua/LuaCore.h"
#include "Lua/LuaList.h"
#define ALL_SCRIPTS_ENABLE_COLUMN 4
Lua4RSWidget::Lua4RSWidget(QWidget *parent) :
MainPage(parent),
ui(new Ui::Lua4RSWidget),
_activeContainer(NULL),
_disableOutput( false )
{
ui->setupUi(this);
_lua = LuaCore::getInstance();
_lua->setUi(this);
setLuaCodes(_lua->codeList());
cleanUi();
luaContainerToUi(_activeContainer);
// Fill Hints TreeWidget with main items
_lua->setupRsFunctionsAndTw(ui->tw_hints);
// f*c: Set header resize mode of tw_allscripts to content dependant
ui->tw_allscripts->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
// Help Button
QString help_str = tr(
"<h1><img width=\"32\" src=\":/images/64px_help.png\"> Lua4RS</h1> \
<p>With Lua4RS you get three things with one Plugin: </p> \
<ul> \
<li>You can write, save, load and run Lua programs within RetroShare.</li> \
<li>You can use Lua programs like macros (think of macros in LibreOffice) \
to control and automate many features of RetroShare. </li> \
<li>You can execute your Lua programs either by timer control (think of \
cron or at) or by certain RetroShare events (e.g. <i>a friend comes \
online</i> or <i>a chat message is received</i> and many more).</li> \
</ul> \
");
registerHelpButton(ui->helpButton, help_str);
}
Lua4RSWidget::~Lua4RSWidget()
{
delete ui;
}
void Lua4RSWidget::disableOutput()
{
_disableOutput = true;
}
void Lua4RSWidget::setLuaCodes(LuaList* list)
{
ui->tw_allscripts->setRowCount(0);
// disable sorting (better performance)
ui->tw_allscripts->setSortingEnabled(false);
LuaContainerList::const_iterator it;
for(it = list->begin(); it != list->end(); ++it)
allScriptsAddRow(*it);
ui->tw_allscripts->setSortingEnabled(true);
}
void Lua4RSWidget::clearOutput()
{
if(!_disableOutput)
ui->tb_output->clear();
}
void Lua4RSWidget::appendOutput(const std::string& s)
{
appendOutput(QString::fromStdString(s));
}
void Lua4RSWidget::appendOutput(const QString& s)
{
if(!_disableOutput)
ui->tb_output->appendPlainText(s);
}
void Lua4RSWidget::appendLog(const std::string& s)
{
appendLog(QString::fromStdString(s));
}
void Lua4RSWidget::appendLog(const QString& s)
{
if(!_disableOutput)
ui->tb_log->appendPlainText(QDateTime::currentDateTime().toString("dd.MM.yy hh:mm:ss") + QString(" > ") + s);
}
/* #############################################################
* # helper
* #############################################################
*/
LuaContainer* Lua4RSWidget::allScriptsGetLuaContainerFromSelectedRow()
{
// get corresponding LuaContainer
QModelIndexList rows = ui->tw_allscripts->selectionModel()->selectedRows();
if(rows.count() != 1)
return NULL;
return allScriptsGetLuaContainerFromRow(rows[0].row());
}
LuaContainer* Lua4RSWidget::allScriptsGetLuaContainerFromRow(const int row)
{
if(row < 0)
return NULL;
// get script name
QTableWidgetItem* name = ui->tw_allscripts->item(row, 0);
std::cout << "[Lua] Lua4RSWidget::allScriptsGetLuaContainerFromRow : trying to load LuaContaienr for " << name->text().toStdString() << " ...";
// get container by name
LuaContainer* container;
if(LuaCore::getInstance()->codeList()->itemByName(name->text(), container))
{
std::cout << " got it!" << std::endl;
return container;
}
else
{
std::cout << " failed!" << std::endl;
return NULL;
}
}
void Lua4RSWidget::allScriptsAddRow(LuaContainer* container)
{
int rows = ui->tw_allscripts->rowCount();
ui->tw_allscripts->setRowCount(rows + 1);
QTableWidgetItem* name = new QTableWidgetItem();
QTableWidgetItem* desc = new QTableWidgetItem();
QTableWidgetItem* lastRun = new QTableWidgetItem();
QTableWidgetItem* trigger = new QTableWidgetItem();
QTableWidgetItem* enabled = new QTableWidgetItem();
name->setText(container->getName());
desc->setText(container->getDesc());
lastRun->setText(container->getLastTriggered().toString());
trigger->setText("TODO");
enabled->setCheckState(container->getEnabled() ? Qt::Checked : Qt::Unchecked);
ui->tw_allscripts->setItem(rows, 0, name);
ui->tw_allscripts->setItem(rows, 1, desc);
ui->tw_allscripts->setItem(rows, 2, lastRun);
ui->tw_allscripts->setItem(rows, 3, trigger);
ui->tw_allscripts->setItem(rows, 4, enabled);
}
// init the gui at startup and after a container switch before the ini is loaded
void Lua4RSWidget::cleanUi()
{
ui->cbx_enable->setChecked(false);
ui->cbx_timeconstraint->setChecked(false);
ui->tied_timefrom->setTime(QTime(0,0,0));
ui->tied_timeto->setTime(QTime(0,0,0));
ui->le_scriptname->clear();
ui->le_scriptdesc->clear();
ui->pte_luacode->clear();
ui->rb_every->setChecked(false);
ui->rb_once->setChecked(false);
ui->rb_startup->setChecked(false);
ui->rb_shutdown->setChecked(false);
ui->rb_runonevent->setChecked(false);
ui->dd_events->setCurrentIndex(0);
ui->spb_everycount->setValue(5);
ui->dd_everyunits->setCurrentIndex(1);
ui->dte_runonce->setDate(QDate(1970,1,1));
ui->dte_runonce->setTime(QTime(0,0,0));
}
void Lua4RSWidget::luaContainerToUi(LuaContainer* container)
{
// for settings things to default / resetting
if(container == NULL)
{
cleanUi();
///TODO there might be better ways that this - good enough for the moment
ui->pte_luacode->setEnabled(false);
} else
{
// name, desc, code
ui->le_scriptname->setText(container->getName());
ui->le_scriptdesc->setText(container->getDesc());
ui->pte_luacode->setPlainText(container->getCode());
ui->cbx_enable->setChecked(container->getEnabled());
ui->cbx_timeconstraint->setChecked(container->getConstraintEnabled());
{
QTime from, to;
container->getConstraintFromTo(from, to);
ui->tied_timefrom->setTime(from);
ui->tied_timeto->setTime(to);
}
///TODO rest
ui->pte_luacode->setEnabled(true);
}
}
bool Lua4RSWidget::uiToLuaContainer(LuaContainer* container)
{
if(!saneValues())
{
std::cerr << "[Lua] Lua4RSWidget::uiToLuaContainer : wrong values detected - aborting" << std::endl;
return false;
}
// name, desc, code
container->setName(ui->le_scriptname->text());
container->setDesc(ui->le_scriptdesc->text());
container->setCode(ui->pte_luacode->toPlainText());
// enable, constraint
container->setEnabled(ui->cbx_enable->isChecked());
container->setConstraintEnabled(ui->cbx_timeconstraint->isChecked());
{
QTime from, to;
from = ui->tied_timefrom->time();
to = ui->tied_timeto->time();
container->setConstraintFromTo(from, to);
}
///TODO rest
return true;
}
void Lua4RSWidget::switchContainer(LuaContainer* container)
{
// remember conatiner
_activeContainer = container;
// update UI
cleanUi();
luaContainerToUi(_activeContainer);
if(_activeContainer != NULL)
std::cout << "[Lua] Lua4RSWidget::switchContainer : switched to " << _activeContainer->getName().toStdString() << std::endl;
else
std::cout << "[Lua] Lua4RSWidget::switchContainer : switched to NULL "<< std::endl;
}
void saneValuesHelper(const QString& msg, QString& allMsgs)
{
std::cerr << "[Lua] Lua4RSWidget::saneValues : " << msg.toStdString() << std::endl;
allMsgs += "- " + msg + '\n';
}
bool Lua4RSWidget::saneValues()
{
QString msg = tr("The following problem(s) was/were found:\n");
bool ret = true;
if(ui->le_scriptname->text().isEmpty())
{
saneValuesHelper(tr("script name is empty"), msg);
ret = false;
}
if(ui->rb_once->isChecked() && ui->dte_runonce->dateTime() < QDateTime::currentDateTime())
{
saneValuesHelper(tr("runOnce value lies in the past"), msg);
ret = false;
}
if(ui->cbx_timeconstraint->isChecked() && ui->rb_once->isChecked() && (( // contraint enabled + run once
ui->tied_timefrom->time() < ui->tied_timeto->time() && // from < to e.g. from 09:00 to 15:00
(ui->dte_runonce->time() < ui->tied_timefrom->time() || ui->dte_runonce->time() > ui->tied_timeto->time()) // run once is outside of time window
) || (
ui->tied_timefrom->time() > ui->tied_timeto->time() && // from > to e.g. from 23:00 to 06:00
(ui->dte_runonce->time() <= ui->tied_timefrom->time() && ui->dte_runonce->time() >= ui->tied_timeto->time()) // run once is outside of time window
// !(ui->dte_runonce->time() > ui->tied_timefrom->time() || ui->dte_runonce->time() < ui->tied_timeto->time()) equivalent - maybe easier to understand
)))
{
saneValuesHelper(tr("runOnce value lies outside of constraint"), msg);
ret = false;
}
///TODO check rest
if(!ret)
{
// show errors to user
QMessageBox mbox;
mbox.setIcon(QMessageBox::Warning);
mbox.setText(tr("Error(s) while checking"));
mbox.setInformativeText(msg);
mbox.setStandardButtons(QMessageBox::Ok);
mbox.exec();
}
return ret;
}
void Lua4RSWidget::newScript()
{
_activeContainer = _lua->codeList()->createItem();
// add new container to list
_lua->codeList()->addItem(_activeContainer);
// update all scripts
setLuaCodes(_lua->codeList());
// update ui
luaContainerToUi(_activeContainer);
}
bool Lua4RSWidget::saveScript(bool showErrorMsg)
{
if(_activeContainer == NULL)
return true;
// check for rename
{
QString oldName = _activeContainer->getName();
// get values from ui
if(!uiToLuaContainer(_activeContainer))
return false;
if(_activeContainer->getName() != oldName)
{
std::cout << "[Lua] Lua4RSWidget::on_pb_save_clicked() : renaming " << oldName.toStdString() << " to " << _activeContainer->getName().toStdString() << std::endl;
_lua->codeList()->rename(oldName, _activeContainer->getName());
}
}
bool rc = _lua->codeList()->saveAll();
if(!rc && showErrorMsg)
{
QMessageBox mbox;
mbox.setIcon(QMessageBox::Warning);
mbox.setText(tr("Error"));
mbox.setInformativeText(tr("an error occured while saving"));
mbox.setStandardButtons( QMessageBox::Ok );
mbox.exec();
}
return rc;
}
/* #############################################################
* # slots
* #############################################################
*/
// "Run" clicked : execute the script in the editor control
void Lua4RSWidget::on_pb_run_clicked()
{
appendLog(QString("running: ") + ui->le_scriptname->text());
QString code = ui->pte_luacode->toPlainText();
_lua->runLuaByString(code);
}
// "New" clicked : create a new empty script
void Lua4RSWidget::on_pb_newscript_clicked()
{
newScript();
}
// "Edit" clicked : edit the script selected in AllMyScripts
void Lua4RSWidget::on_pb_editscript_clicked()
{
// get corresponding LuaContainer
LuaContainer* container = allScriptsGetLuaContainerFromSelectedRow();
if(container == NULL)
{
std::cerr << "[Lua] Lua4RSWidget::on_pb_editscript_clicked : got NULL" << std::endl;
return;
}
switchContainer(container);
}
// "Delete" clicked : delete the script selected in AllMyScripts
void Lua4RSWidget::on_pb_deletescript_clicked()
{
LuaContainer* container = allScriptsGetLuaContainerFromSelectedRow();
if(container == NULL)
return;
// update UI when necessary
if(_activeContainer == container)
{
_activeContainer = NULL;
luaContainerToUi(_activeContainer);
}
_lua->codeList()->removeItemAndDelete(container);
// container is deleted now
container = NULL;
// update all scripts
setLuaCodes(_lua->codeList());
}
// "Load" clicked : load a scriptfile from disk into the editor control
void Lua4RSWidget::on_pb_load_clicked()
{
QString name = "";
if(_activeContainer != NULL)
{
// a file was opened -> save its name
name = _activeContainer->getName();
// ask for confirmation
QMessageBox mbox;
mbox.setIcon(QMessageBox::Information);
mbox.setText(tr("Continue?"));
mbox.setInformativeText(tr("You have a Lua script opened. Save it before closing it?"));
mbox.setStandardButtons( QMessageBox::Save | QMessageBox::Discard | QMessageBox::Abort);
int ret = mbox.exec();
if(ret == QMessageBox::Abort)
return;
if(ret == QMessageBox::Save)
saveScript();
}
LuaList* list = _lua->codeList();
list->loadAll();
// _activeContainer is invalid from now on!
_activeContainer = NULL;
setLuaCodes(list);
if(name == "")
// no file was opened - were are done
return;
LuaContainer* lc;
if(list->itemByName(name, lc))
switchContainer(lc);
else
// couldn't find the file one was working one ...
switchContainer(NULL);
}
// "Save" clicked : save the contents of the editor control to a file on disk
void Lua4RSWidget::on_pb_save_clicked()
{
saveScript();
}
// "Enabled Script" toggled :
void Lua4RSWidget::on_cbx_enable_toggled(bool checked)
{
if(_activeContainer == NULL)
return;
_activeContainer->setEnabled(checked);
// update all scripts
LuaContainer* lc;
for(int i = 0; i < ui->tw_allscripts->rowCount(); ++i)
{
lc = allScriptsGetLuaContainerFromRow(i);
if(lc == _activeContainer)
{
QTableWidgetItem* enabled = ui->tw_allscripts->item(i, ALL_SCRIPTS_ENABLE_COLUMN);
enabled->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
break;
}
}
}
//------------------------------------------------------------------------------
// Execution Constraint
//------------------------------------------------------------------------------
// "...between" toggled : Constraint enabled/disabled has changed
// note: think about disabling constraint from and to timeedits if unchecked
void Lua4RSWidget::on_cbx_timeconstraint_toggled(bool checked)
{
if(_activeContainer == NULL)
{
std::cerr << "[Lua] Lua4RSWidget::on_cbx_timeconstraint_toggled : got no activeContainer" << std::endl;
return;
}
_activeContainer->setConstraintEnabled(checked);
}
// from : Constraint "from"-time has changed
// note: dont forget to check if from < to!
void Lua4RSWidget::on_tied_timefrom_editingFinished()
{
}
// to : Constraint "to"-time has changed
// note: dont forget to check if from < to!
void Lua4RSWidget::on_tied_timeto_editingFinished()
{
}
//------------------------------------------------------------------------------
// All Scripts
//------------------------------------------------------------------------------
void Lua4RSWidget::on_tw_allscripts_cellClicked(int row, int column)
{
if(column == ALL_SCRIPTS_ENABLE_COLUMN) // 4 = enabled
{
LuaContainer* container = allScriptsGetLuaContainerFromRow(row);
QTableWidgetItem* cell = ui->tw_allscripts->item(row, column);
container->setEnabled(cell->checkState() == Qt::Checked ? true : false);
if(container == _activeContainer)
// update ui->cbx_enable
ui->cbx_enable->setChecked(_activeContainer->getEnabled());
}
}
void Lua4RSWidget::on_tw_allscripts_cellDoubleClicked(int row, int /*column*/)
{
if(row < 0)
return;
// save then load
saveScript();
// get container
LuaContainer* container = allScriptsGetLuaContainerFromRow(row);
if(container == NULL)
{
std::cerr << "[Lua] Lua4RSWidget::on_tw_allscripts_doubleClicked : got NULL" << std::endl;
return;
}
switchContainer(container);
}
//------------------------------------------------------------------------------
// Tabpage "By Timer"
//------------------------------------------------------------------------------
void Lua4RSWidget::on_spb_everycount_editingFinished()
{
return;
}
// "Run Every" : amount of timer units has changed
// note: if changed, rb_runevery should be selected
void Lua4RSWidget::on_dd_everyunits_currentIndexChanged(int index)
{
uint TIME_UNITS[5] = {1, 60, 3600, 86400, 604800};
uint unit,amount,interval;
amount = ui->spb_everycount->value();
unit = TIME_UNITS[index];
interval = amount * unit;
ui->l_runeveryhelper->setText( QString::number(interval) + " secs" ); // just to see
}
// "RunEvery" : unit of timer units has changed
// note: if changed, rb_runevery should be selected
void Lua4RSWidget::on_spb_everycount_valueChanged(int arg1)
{
uint TIME_UNITS[5] = {1, 60, 3600, 86400, 604800};
uint unit,amount,interval;
amount = arg1;
unit = TIME_UNITS[ui->dd_everyunits->currentIndex()];
interval = amount * unit;
ui->l_runeveryhelper->setText( QString::number(interval) + " secs" );
}
// Run Every was selected
void Lua4RSWidget::on_rb_every_toggled(bool checked)
{
if (checked == true){
ui->rb_every->setStyleSheet ("background:lime;");
ui->rb_once->setStyleSheet ("background:transparent;");
ui->rb_startup->setStyleSheet ("background:transparent;");
ui->rb_shutdown->setStyleSheet ("background:transparent;");
_activeContainer->setRunEveryChecked(true);
}
}
// Run Once was selected
void Lua4RSWidget::on_rb_once_toggled(bool checked)
{
if (checked == true){
ui->rb_every->setStyleSheet ("background:transparent;");
ui->rb_once->setStyleSheet ("background:lime;");
ui->rb_startup->setStyleSheet ("background:transparent;");
ui->rb_shutdown->setStyleSheet ("background:transparent;");
_activeContainer->setRunOnceChecked(true, ui->dte_runonce->dateTime());
}
}
// Run at startup was selected
void Lua4RSWidget::on_rb_startup_toggled(bool checked)
{
if (checked == true){
ui->rb_every->setStyleSheet ("background:transparent;");
ui->rb_once->setStyleSheet ("background:transparent;");
ui->rb_startup->setStyleSheet ("background:lime;");
ui->rb_shutdown->setStyleSheet ("background:transparent;");
_activeContainer->setRunStartupChecked(true);
}
}
// Run at shutdown was selected
void Lua4RSWidget::on_rb_shutdown_toggled(bool checked)
{
if (checked == true){
ui->rb_every->setStyleSheet ("background:transparent;");
ui->rb_once->setStyleSheet ("background:transparent;");
ui->rb_startup->setStyleSheet ("background:transparent;");
ui->rb_shutdown->setStyleSheet ("background:lime;");
_activeContainer->setRunShutdownChecked(true);
}
}
//------------------------------------------------------------------------------
// Tabpage "By Event"
//------------------------------------------------------------------------------
void Lua4RSWidget::on_rb_runonevent_toggled(bool /*checked*/)
{
}
void Lua4RSWidget::on_dd_events_currentIndexChanged(int /*index*/)
{
}
//------------------------------------------------------------------------------
// hints
//------------------------------------------------------------------------------
void Lua4RSWidget::on_pb_pastehint_released()
{
QList<QTreeWidgetItem*> items = ui->tw_hints->selectedItems();
if(items.empty() || items.size() != 1 || !ui->pte_luacode->isEnabled())
return;
ui->pte_luacode->insertPlainText(items.at(0)->text(1));
}
void Lua4RSWidget::on_tw_hints_itemDoubleClicked(QTreeWidgetItem *item, int /*column*/)
{
QString hint = item->text(1);
// when you want to expant a namespace, you double click it --> don't append namespaces
if(hint.endsWith('.') || !ui->pte_luacode->isEnabled())
return;
ui->pte_luacode->insertPlainText(hint);
}