Database: sqlite default to appdata kworship.db
[kworship.git] / unipresent / kpresenter1 / UpKpr1Dcop.cpp
blob982021627551e1c1cd6e786465930a963b2598e2
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file UpKpr1Dcop.cpp
22 * @brief DCOP interface using command line tool.
23 * @author James Hogan <james@albanarts.com>
26 #include "UpKpr1Dcop.h"
28 #include <QProcess>
31 * Constructors + destructor
34 /// Default constructor.
35 UpKpr1Dcop::UpKpr1Dcop()
36 : m_valid(false)
37 , m_reference()
41 /// Primary constructor.
42 UpKpr1Dcop::UpKpr1Dcop(const UpKpr1Dcop& parent, QString interface)
43 : m_valid(true)
44 , m_reference(parent.m_reference)
46 m_reference << interface;
49 /// Construct from dcop reference.
50 UpKpr1Dcop::UpKpr1Dcop(QStringList reference)
51 : m_valid(true)
52 , m_reference(reference)
56 /// Destructor.
57 UpKpr1Dcop::~UpKpr1Dcop()
62 * Main interface
65 /// Find whether its valid.
66 bool UpKpr1Dcop::isValid() const
68 return m_valid;
71 /// Get the dcop reference.
72 const QStringList& UpKpr1Dcop::reference() const
74 return m_reference;
78 * Helper methods
81 /// Get the result of a dcop query on this interface.
82 QString UpKpr1Dcop::eval(bool* const error, QStringList tail) const
84 QStringList args = m_reference;
85 args << tail;
87 QProcess dcop;
88 dcop.start("dcop", args);
89 bool localError = !dcop.waitForFinished();
90 if (0 != error)
92 *error = localError;
95 if (!localError)
97 QString result = QString::fromAscii(dcop.readAll());
98 if (result.endsWith('\n'))
100 result = result.left(result.size()-1);
102 return result;
104 else
106 return QString();
110 /// Get the resulting list of strings from a dcop query
111 QStringList UpKpr1Dcop::evalList(bool* const err, QStringList tail) const
113 return eval(err,tail).split('\n');
116 /// Get the resulting integer from a dcop query
117 int UpKpr1Dcop::evalInt(bool* const err, QStringList tail) const
119 bool evalErr;
120 int result;
121 QString string = eval(&evalErr, tail);
122 if (!evalErr)
124 result = string.toInt(&evalErr);
125 evalErr = !evalErr;
126 if (evalErr)
128 result = 0;
131 else
133 result = 0;
135 if (0 != err)
137 *err = evalErr;
139 return result;
142 /// Get a dcop reference from a string.
143 UpKpr1Dcop UpKpr1Dcop::dcopRefFromString(QString input)
145 #define DCOPREF_START "DCOPRef("
146 #define DCOPREF_END ")"
147 #define DCOPREF_NULL DCOPREF_START "," DCOPREF_END
148 if (input.startsWith(DCOPREF_START) &&
149 input.endsWith(DCOPREF_END) &&
150 input != DCOPREF_NULL)
152 return UpKpr1Dcop(input.mid(sizeof(DCOPREF_START)-1,
153 input.size() - (sizeof(DCOPREF_START)-1)
154 - (sizeof(DCOPREF_END)-1) ).split(','));
156 else
158 return UpKpr1Dcop();
162 /// Get a single dcop reference from a dcop query.
163 UpKpr1Dcop UpKpr1Dcop::evalRef(QStringList tail) const
165 bool error;
166 QStringList items = evalList(&error, tail);
167 if (!error && items.size() == 1)
169 return dcopRefFromString(items.first());
171 return UpKpr1Dcop();