Linked presentation nav buttons to unipresent interface
[kworship.git] / unipresent / kpresenter1 / UpKpr1Dcop.cpp
blob2e8831a07ff4ed5e16db4586a52fabed89adb1e1
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 3 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, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
19 /**
20 * @file UpKpr1Dcop.cpp
21 * @brief DCOP interface using command line tool.
22 * @author James Hogan <james@albanarts.com>
25 #include "UpKpr1Dcop.h"
27 #include <QProcess>
30 * Constructors + destructor
33 /// Default constructor.
34 UpKpr1Dcop::UpKpr1Dcop()
35 : m_valid(false)
36 , m_reference()
40 /// Primary constructor.
41 UpKpr1Dcop::UpKpr1Dcop(const UpKpr1Dcop& parent, QString interface)
42 : m_valid(true)
43 , m_reference(parent.m_reference)
45 m_reference << interface;
48 /// Construct from dcop reference.
49 UpKpr1Dcop::UpKpr1Dcop(QStringList reference)
50 : m_valid(true)
51 , m_reference(reference)
55 /// Destructor.
56 UpKpr1Dcop::~UpKpr1Dcop()
61 * Main interface
64 /// Find whether its valid.
65 bool UpKpr1Dcop::isValid() const
67 return m_valid;
71 * Helper methods
74 /// Get the result of a dcop query on this interface.
75 QString UpKpr1Dcop::eval(bool* const error, QStringList tail) const
77 QStringList args = m_reference;
78 args << tail;
80 QProcess dcop;
81 dcop.start("dcop", args);
82 bool localError = !dcop.waitForFinished();
83 if (0 != error)
85 *error = localError;
88 if (!localError)
90 QString result = QString::fromAscii(dcop.readAll());
91 if (result.endsWith('\n'))
93 result = result.left(result.size()-1);
95 return result;
97 else
99 return QString();
103 /// Get the resulting list of strings from a dcop query
104 QStringList UpKpr1Dcop::evalList(bool* const err, QStringList tail) const
106 return eval(err,tail).split('\n');
109 /// Get the resulting integer from a dcop query
110 int UpKpr1Dcop::evalInt(bool* const err, QStringList tail) const
112 bool evalErr;
113 int result;
114 QString string = eval(&evalErr, tail);
115 if (!evalErr)
117 result = string.toInt(&evalErr);
118 evalErr = !evalErr;
119 if (evalErr)
121 result = 0;
124 else
126 result = 0;
128 if (0 != err)
130 *err = evalErr;
132 return result;
135 /// Get a dcop reference from a string.
136 UpKpr1Dcop UpKpr1Dcop::dcopRefFromString(QString input)
138 #define DCOPREF_START "DCOPRef("
139 #define DCOPREF_END ")"
140 #define DCOPREF_NULL DCOPREF_START "," DCOPREF_END
141 if (input.startsWith(DCOPREF_START) &&
142 input.endsWith(DCOPREF_END) &&
143 input != DCOPREF_NULL)
145 return UpKpr1Dcop(input.mid(sizeof(DCOPREF_START)-1,
146 input.size() - (sizeof(DCOPREF_START)-1)
147 - (sizeof(DCOPREF_END)-1) ).split(','));
149 else
151 return UpKpr1Dcop();
155 /// Get a single dcop reference from a dcop query.
156 UpKpr1Dcop UpKpr1Dcop::evalRef(QStringList tail) const
158 bool error;
159 QStringList items = evalList(&error, tail);
160 if (!error && items.size() == 1)
162 return dcopRefFromString(items.first());
164 return UpKpr1Dcop();