2
* Copyright (C) 2007 Barış Metin <baris@pardus.org.tr>
3
* Copyright (C) 2006 David Faure <faure@kde.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Library General Public License version 2 as
7
* published by the Free Software Foundation
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
14
* You should have received a copy of the GNU Library General Public
15
* License 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.
20
#include "calculatorrunner.h"
24
#include <QHBoxLayout>
30
#include <kshell.h> //TODO: replace with KShell after 31/7/2007
31
#include <KStandardDirs>
33
CalculatorRunner::CalculatorRunner( QObject* parent, const QVariantList &args )
34
: Plasma::AbstractRunner( parent ),
39
setObjectName( i18n( "Calculator" ) );
42
CalculatorRunner::~CalculatorRunner()
47
QAction* CalculatorRunner::accepts( const QString& term )
49
QString cmd = term.trimmed();
52
if ( !cmd.isEmpty() &&
53
( cmd[0].isNumber() || ( cmd[0] == '(') ) &&
54
( QRegExp("[a-zA-Z\\]\\[]").indexIn(cmd) == -1 ) ) {
55
QString result = calculate(cmd);
57
if ( !result.isEmpty() ) {
58
action = new QAction( KIcon("kcalc"),
59
i18nc("Answer to a mathematical equation", "Result: %1", result),
61
action->setEnabled( false );
68
bool CalculatorRunner::exec(QAction* action, const QString& term)
74
// function taken from kdesktop/minicli.cpp
75
QString CalculatorRunner::calculate( const QString& term )
78
const QString bc = KStandardDirs::findExe( "bc" );
79
if ( !bc.isEmpty() ) {
80
cmd = QString( "echo %1 | %2" )
81
.arg(KShell::quoteArg(QString("scale=8; ") + term),
82
KShell::quoteArg(bc));
85
cmd = QString( "echo $((%1))" ).arg(term);
88
FILE *fs = popen(QFile::encodeName(cmd).data(), "r");
91
{ // scope for QTextStream
92
QTextStream ts(fs, QIODevice::ReadOnly);
93
result = ts.readAll().trimmed();
100
#include "calculatorrunner.moc"