diff options
Diffstat (limited to 'akonadish/state.cpp')
-rw-r--r-- | akonadish/state.cpp | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/akonadish/state.cpp b/akonadish/state.cpp new file mode 100644 index 0000000..f3f5975 --- /dev/null +++ b/akonadish/state.cpp | |||
@@ -0,0 +1,125 @@ | |||
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 "state.h" | ||
21 | |||
22 | #include <QCoreApplication> | ||
23 | #include <QDebug> | ||
24 | #include <QEventLoop> | ||
25 | #include <QTextStream> | ||
26 | |||
27 | static bool s_hasEventLoop = false; | ||
28 | |||
29 | class State::Private | ||
30 | { | ||
31 | public: | ||
32 | Private() | ||
33 | : outStream(stdout) | ||
34 | { | ||
35 | } | ||
36 | |||
37 | QEventLoop *eventLoop() | ||
38 | { | ||
39 | if (!event) { | ||
40 | event = new QEventLoop; | ||
41 | } | ||
42 | |||
43 | return event; | ||
44 | } | ||
45 | |||
46 | int debugLevel = 0; | ||
47 | QEventLoop *event = 0; | ||
48 | bool timing = false; | ||
49 | QTextStream outStream; | ||
50 | }; | ||
51 | |||
52 | State::State() | ||
53 | : d(new Private) | ||
54 | { | ||
55 | } | ||
56 | |||
57 | void State::print(const QString &message, unsigned int indentationLevel) const | ||
58 | { | ||
59 | for (unsigned int i = 0; i < indentationLevel; ++i) { | ||
60 | d->outStream << "\t"; | ||
61 | } | ||
62 | |||
63 | d->outStream << message; | ||
64 | } | ||
65 | |||
66 | void State::printLine(const QString &message, unsigned int indentationLevel) const | ||
67 | { | ||
68 | print(message, indentationLevel); | ||
69 | d->outStream << "\n"; | ||
70 | d->outStream.flush(); | ||
71 | } | ||
72 | |||
73 | void State::printError(const QString &errorMessage, const QString &errorCode) const | ||
74 | { | ||
75 | printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); | ||
76 | } | ||
77 | |||
78 | void State::setDebugLevel(unsigned int level) | ||
79 | { | ||
80 | if (level < 7) { | ||
81 | d->debugLevel = level; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | unsigned int State::debugLevel() const | ||
86 | { | ||
87 | return d->debugLevel; | ||
88 | } | ||
89 | |||
90 | int State::commandStarted() const | ||
91 | { | ||
92 | if (!s_hasEventLoop) { | ||
93 | return QCoreApplication::exec(); | ||
94 | } else if (!d->eventLoop()->isRunning()) { | ||
95 | return d->eventLoop()->exec(); | ||
96 | } | ||
97 | |||
98 | return 0; | ||
99 | } | ||
100 | |||
101 | void State::commandFinished(int returnCode) const | ||
102 | { | ||
103 | if (!s_hasEventLoop) { | ||
104 | QCoreApplication::exit(returnCode); | ||
105 | } else { | ||
106 | d->eventLoop()->exit(returnCode); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | void State::setHasEventLoop(bool evented) | ||
111 | { | ||
112 | s_hasEventLoop = evented; | ||
113 | } | ||
114 | |||
115 | void State::setCommandTiming(bool time) | ||
116 | { | ||
117 | d->timing = time; | ||
118 | } | ||
119 | |||
120 | bool State::commandTiming() const | ||
121 | { | ||
122 | return d->timing; | ||
123 | } | ||
124 | |||
125 | |||