summaryrefslogtreecommitdiffstats
path: root/sinksh/state.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-20 19:07:07 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-01-20 19:07:07 +0100
commitbdb01c2c068df326f5a8328ed1492ab1bea388c5 (patch)
tree25c2ee1b29bc481b6914c244ed9ca194b1415d16 /sinksh/state.cpp
parent17e7ee40c9185c0505883853345fd6024c675b1a (diff)
downloadsink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.tar.gz
sink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.zip
Renamed Akonadi2 to Sink
(except for documentation).
Diffstat (limited to 'sinksh/state.cpp')
-rw-r--r--sinksh/state.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/sinksh/state.cpp b/sinksh/state.cpp
new file mode 100644
index 0000000..e03bf87
--- /dev/null
+++ b/sinksh/state.cpp
@@ -0,0 +1,144 @@
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#include "common/log.h"
28
29static bool s_hasEventLoop = false;
30
31class State::Private
32{
33public:
34 Private()
35 : outStream(stdout)
36 {
37 }
38
39 QEventLoop *eventLoop()
40 {
41 if (!event) {
42 event = new QEventLoop;
43 }
44
45 return event;
46 }
47
48 int debugLevel = 0;
49 QEventLoop *event = 0;
50 bool timing = false;
51 QTextStream outStream;
52};
53
54State::State()
55 : d(new Private)
56{
57}
58
59void State::print(const QString &message, unsigned int indentationLevel) const
60{
61 for (unsigned int i = 0; i < indentationLevel; ++i) {
62 d->outStream << "\t";
63 }
64
65 d->outStream << message;
66}
67
68void State::printLine(const QString &message, unsigned int indentationLevel) const
69{
70 print(message, indentationLevel);
71 d->outStream << "\n";
72 d->outStream.flush();
73}
74
75void State::printError(const QString &errorMessage, const QString &errorCode) const
76{
77 printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage);
78}
79
80void State::setDebugLevel(unsigned int level)
81{
82 if (level < 7) {
83 d->debugLevel = level;
84 }
85}
86
87unsigned int State::debugLevel() const
88{
89 return d->debugLevel;
90}
91
92int State::commandStarted() const
93{
94 if (!s_hasEventLoop) {
95 return QCoreApplication::exec();
96 } else if (!d->eventLoop()->isRunning()) {
97 return d->eventLoop()->exec();
98 }
99
100 return 0;
101}
102
103void State::commandFinished(int returnCode) const
104{
105 if (!s_hasEventLoop) {
106 QCoreApplication::exit(returnCode);
107 } else {
108 d->eventLoop()->exit(returnCode);
109 }
110}
111
112void State::setHasEventLoop(bool evented)
113{
114 s_hasEventLoop = evented;
115}
116
117bool State::hasEventLoop()
118{
119 return s_hasEventLoop;
120}
121
122void State::setCommandTiming(bool time)
123{
124 d->timing = time;
125}
126
127bool State::commandTiming() const
128{
129 return d->timing;
130}
131
132void State::setLoggingLevel(const QString &level) const
133{
134 Sink::Log::setDebugOutputLevel(Sink::Log::debugLevelFromName(level.toLatin1()));
135}
136
137QString State::loggingLevel() const
138{
139 // do not turn this into a single line return: that core dumps due to allocation of
140 // the byte array in Sink::Log
141 QByteArray rv = Sink::Log::debugLevelName(Sink::Log::debugOutputLevel());
142 return rv.toLower();
143}
144