~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to lib/util/kscriptactionmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
*   Copyright (C) 2004 by ian geiser                                      *
3
 
*   geiseri@sourcextreme.com                                              *
4
 
*                                                                         *
5
 
*   This program 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
 
*   This program 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 this program; if not, write to the                         *
17
 
*   Free Software Foundation, Inc.,                                       *
18
 
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 
***************************************************************************/
20
 
#include "kscriptactionmanager.h"
21
 
#include <kparts/part.h>
22
 
#include <kparts/componentfactory.h>
23
 
#include <kapplication.h>
24
 
#include <kdesktopfile.h>
25
 
#include <kstandarddirs.h>
26
 
 
27
 
#include <klocale.h>
28
 
#include <kmessagebox.h>
29
 
#include <kdebug.h>
30
 
#include <scriptinterface.h>
31
 
#include <kaction.h>
32
 
#include <qfileinfo.h>
33
 
#include <qtimer.h>
34
 
 
35
 
KScriptAction::KScriptAction( const QString &scriptDesktopFile, QObject *interface, KActionCollection *ac )
36
 
    : QObject(interface), KScriptClientInterface( )
37
 
{
38
 
    m_interface = 0L;
39
 
    m_action = 0L;
40
 
    m_isValid = false;
41
 
    m_refs = 0;
42
 
    // Read the desktop file
43
 
    if(KDesktopFile::isDesktopFile(scriptDesktopFile))
44
 
    {
45
 
        KDesktopFile desktop(scriptDesktopFile, true);
46
 
        QFileInfo scriptPath(scriptDesktopFile);
47
 
        
48
 
        m_scriptFile = scriptPath.dirPath(true) + "/" + desktop.readEntry("X-KDE-ScriptName", "");
49
 
        m_scriptName = desktop.readName();
50
 
        m_scriptType = desktop.readType();
51
 
        QString scriptTypeQuery = "([X-KDE-Script-Runner] == '" + m_scriptType + "')";
52
 
        KTrader::OfferList offers = KTrader::self()->query( "KScriptRunner/KScriptRunner", scriptTypeQuery );
53
 
        if ( !offers.isEmpty() )
54
 
        {
55
 
            m_action = new KAction(m_scriptName, KShortcut(), this, SLOT(activate()), ac, "script");
56
 
            m_isValid = true;
57
 
            m_timeout = new QTimer(this);
58
 
            QString icon = desktop.readIcon();
59
 
            m_action->setStatusText(desktop.readComment());
60
 
                if( !icon.isEmpty() ) 
61
 
                    m_action->setIcon(icon);
62
 
            m_action->setShortcutConfigurable(true);
63
 
            connect( m_timeout, SIGNAL(timeout()), SLOT(cleanup()) );
64
 
        }
65
 
    }
66
 
}
67
 
 
68
 
KScriptAction::~KScriptAction()
69
 
{
70
 
    if( m_interface ) delete m_interface;
71
 
    if( m_action ) delete m_action;
72
 
}
73
 
 
74
 
 
75
 
 
76
 
KAction * KScriptAction::action( )
77
 
{
78
 
    return m_action;
79
 
}
80
 
 
81
 
void KScriptAction::activate( )
82
 
{
83
 
    if( m_interface == 0L)
84
 
    {
85
 
        QString scriptTypeQuery = "([X-KDE-Script-Runner] == '" + m_scriptType + "')";
86
 
        m_interface= KParts::ComponentFactory::createInstanceFromQuery<KScriptInterface>( "KScriptRunner/KScriptRunner", scriptTypeQuery, this );
87
 
        if ( m_interface != 0L)
88
 
        {
89
 
            m_interface->ScriptClientInterface= this;
90
 
            if( m_scriptMethod.isEmpty() )
91
 
                m_interface->setScript(m_scriptFile);
92
 
            else
93
 
                m_interface->setScript(m_scriptFile, m_scriptMethod);
94
 
            connect(this, SIGNAL(done(KScriptClientInterface::Result, const QVariant &)), this, SLOT(scriptFinished()));
95
 
        }
96
 
        else
97
 
        {
98
 
            KMessageBox::sorry(0, i18n("Unable to get KScript Runner for type \"%1\".").arg(m_scriptType), i18n("KScript Error"));
99
 
            kdDebug() << "Query string: " << scriptTypeQuery << endl;
100
 
            return;
101
 
        }
102
 
    }
103
 
    m_interface->run(parent(), QVariant());
104
 
    m_timeout->start(60000,FALSE); // after 1 minute unload
105
 
    m_refs++;
106
 
}
107
 
 
108
 
void KScriptAction::cleanup() 
109
 
{
110
 
    if( m_interface && m_refs == 0)
111
 
    {
112
 
        delete m_interface;
113
 
        m_interface = 0L;
114
 
    }
115
 
}
116
 
 
117
 
void KScriptAction::scriptFinished()
118
 
{
119
 
    m_refs--;
120
 
}
121
 
 
122
 
KScriptActionManager::KScriptActionManager(  QObject *parent, KActionCollection * ac ) : QObject(parent), m_ac(ac)
123
 
{
124
 
    m_actions.setAutoDelete(true);
125
 
}
126
 
 
127
 
KScriptActionManager::~ KScriptActionManager( )
128
 
{
129
 
    m_actions.clear();
130
 
}
131
 
 
132
 
QPtrList< KAction > KScriptActionManager::scripts( QObject * interface , const QStringList &dirs) const
133
 
{
134
 
    m_actions.clear();
135
 
    QPtrList<KAction> actions;
136
 
    QStringList scripts;
137
 
 
138
 
    scripts += KGlobal::dirs()->findAllResources("data",
139
 
        QString(kapp->name())+"/scripts/*.desktop", false, true );
140
 
 
141
 
    for( QStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it)
142
 
    {
143
 
        scripts += KGlobal::dirs()->findAllResources("data",
144
 
        (*it)+"/*.desktop", false, true );
145
 
    }
146
 
 
147
 
    for (QStringList::Iterator it = scripts.begin(); it != scripts.end(); ++it )
148
 
    {
149
 
        kdDebug() << "Loading " << *it << endl;
150
 
        KScriptAction *script = new KScriptAction(*it, interface, m_ac);
151
 
        if( script->isValid())
152
 
        {
153
 
          actions.append(script->action());
154
 
          m_actions.append(script);
155
 
          connect(script, SIGNAL(error( const QString&)), this, 
156
 
                SIGNAL(scriptError( const QString&)));
157
 
          connect(script, SIGNAL(warning( const QString&)), this, 
158
 
                SIGNAL(scriptWarning( const QString&)));
159
 
          connect(script, SIGNAL(output( const QString&)), this,
160
 
                SIGNAL(scriptOutput( const QString&)));
161
 
          connect(script, SIGNAL(progress( int )), this, 
162
 
                SIGNAL(scriptProgress(int)));
163
 
          connect(script, SIGNAL(done( KScriptClientInterface::Result, const QVariant &)),this,
164
 
                SIGNAL(scriptDone( KScriptClientInterface::Result, const QVariant &)));
165
 
        }
166
 
        else
167
 
          delete script;
168
 
    }
169
 
    return actions;
170
 
}
171
 
 
172
 
bool KScriptAction::isValid( ) const
173
 
{
174
 
  return m_isValid;
175
 
}
176
 
 
177
 
#include "kscriptactionmanager.moc"