~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to krunner/runners/calculator/calculatorrunner.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
Import upstream version 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007 Barış Metin <baris@pardus.org.tr>
 
3
 *   Copyright (C) 2006 David Faure <faure@kde.org>
 
4
 *
 
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
 
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 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.
 
18
 */
 
19
 
 
20
#include "calculatorrunner.h"
 
21
 
 
22
#include <QAction>
 
23
#include <QFile>
 
24
#include <QHBoxLayout>
 
25
#include <QLabel>
 
26
#include <QWidget>
 
27
 
 
28
#include <KIcon>
 
29
#include <KLocale>
 
30
#include <kshell.h> //TODO: replace with KShell after 31/7/2007
 
31
#include <KStandardDirs>
 
32
 
 
33
CalculatorRunner::CalculatorRunner( QObject* parent, const QVariantList &args )
 
34
    : Plasma::AbstractRunner( parent ),
 
35
      m_options( 0 )
 
36
{
 
37
    Q_UNUSED(args)
 
38
 
 
39
    setObjectName( i18n( "Calculator" ) );
 
40
}
 
41
 
 
42
CalculatorRunner::~CalculatorRunner()
 
43
{
 
44
    delete m_options;
 
45
}
 
46
 
 
47
QAction* CalculatorRunner::accepts( const QString& term )
 
48
{
 
49
    QString cmd = term.trimmed();
 
50
    QAction *action = 0;
 
51
 
 
52
    if ( !cmd.isEmpty() && 
 
53
         ( cmd[0].isNumber() || ( cmd[0] == '(') ) &&
 
54
         ( QRegExp("[a-zA-Z\\]\\[]").indexIn(cmd) == -1 ) ) {
 
55
        QString result = calculate(cmd);
 
56
 
 
57
        if ( !result.isEmpty() ) {
 
58
            action = new QAction( KIcon("kcalc"),
 
59
                                  i18nc("Answer to a mathematical equation", "Result: %1", result),
 
60
                                  this);
 
61
            action->setEnabled( false );
 
62
        }
 
63
    }
 
64
 
 
65
    return action;
 
66
}
 
67
 
 
68
bool CalculatorRunner::exec(QAction* action, const QString& term)
 
69
{
 
70
    Q_UNUSED(term)
 
71
    return true;
 
72
}
 
73
 
 
74
// function taken from kdesktop/minicli.cpp
 
75
QString CalculatorRunner::calculate( const QString& term )
 
76
{
 
77
    QString result, cmd;
 
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));
 
83
    }
 
84
    else {
 
85
        cmd = QString( "echo $((%1))" ).arg(term);
 
86
    }
 
87
 
 
88
    FILE *fs = popen(QFile::encodeName(cmd).data(), "r");
 
89
    if (fs)
 
90
    {
 
91
        { // scope for QTextStream
 
92
            QTextStream ts(fs, QIODevice::ReadOnly);
 
93
            result = ts.readAll().trimmed();
 
94
        }
 
95
        pclose(fs);
 
96
    }
 
97
    return result;
 
98
}
 
99
 
 
100
#include "calculatorrunner.moc"