summaryrefslogtreecommitdiffstats
path: root/akonadi2_cli/repl/repl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'akonadi2_cli/repl/repl.cpp')
-rw-r--r--akonadi2_cli/repl/repl.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/akonadi2_cli/repl/repl.cpp b/akonadi2_cli/repl/repl.cpp
new file mode 100644
index 0000000..395661e
--- /dev/null
+++ b/akonadi2_cli/repl/repl.cpp
@@ -0,0 +1,78 @@
1/*
2 * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "repl.h"
21
22#include <readline/history.h>
23
24#include <QDir>
25#include <QFile>
26#include <QFinalState>
27#include <QStandardPaths>
28
29#include "replStates.h"
30
31Repl::Repl(QObject *parent)
32 : QStateMachine(parent)
33{
34 // readline history setup
35 using_history();
36 read_history(commandHistoryPath().toLocal8Bit());
37
38 // create all states
39 ReadState *read = new ReadState(this);
40 UnfinishedReadState *unfinishedRead = new UnfinishedReadState(this);
41 EvalState *eval = new EvalState(this);
42 PrintState *print = new PrintState(this);
43 QFinalState *final = new QFinalState(this);
44
45 // connect the transitions
46 read->addTransition(read, SIGNAL(command(QString)), eval);
47 read->addTransition(read, SIGNAL(exitRequested()), final);
48
49 unfinishedRead->addTransition(unfinishedRead, SIGNAL(command(QString)), eval);
50 unfinishedRead->addTransition(unfinishedRead, SIGNAL(exitRequested()), final);
51
52 eval->addTransition(eval, SIGNAL(completed()), read);
53 eval->addTransition(eval, SIGNAL(continueInput()), unfinishedRead);
54 eval->addTransition(eval, SIGNAL(output(QString)), print);
55
56 print->addTransition(print, SIGNAL(completed()), eval);
57
58 setInitialState(read);
59 start();
60}
61
62Repl::~Repl()
63{
64 // readline history writing
65 write_history(commandHistoryPath().toLocal8Bit());
66}
67
68QString Repl::commandHistoryPath()
69{
70 const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
71 if (!QFile::exists(path)) {
72 QDir dir;
73 dir.mkpath(path);
74 }
75 return path + "/repl_history";
76}
77
78#include "moc_repl.cpp"