• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Utility Library
 

KDevelop Utility Library

  • lib
  • util
tdescriptactionmanager.cpp
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* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19***************************************************************************/
20#include "tdescriptactionmanager.h"
21#include <tdeparts/part.h>
22#include <tdeparts/componentfactory.h>
23#include <tdeapplication.h>
24#include <kdesktopfile.h>
25#include <kstandarddirs.h>
26
27#include <tdelocale.h>
28#include <tdemessagebox.h>
29#include <kdebug.h>
30#include <scriptinterface.h>
31#include <tdeaction.h>
32#include <tqfileinfo.h>
33#include <tqtimer.h>
34
35KScriptAction::KScriptAction( const TQString &scriptDesktopFile, TQObject *interface, TDEActionCollection *ac )
36 : TQObject(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 TQFileInfo scriptPath(scriptDesktopFile);
47
48 m_scriptFile = scriptPath.dirPath(true) + "/" + desktop.readEntry("X-TDE-ScriptName", "");
49 m_scriptName = desktop.readName();
50 m_scriptType = desktop.readType();
51 TQString scriptTypeQuery = "([X-TDE-Script-Runner] == '" + m_scriptType + "')";
52 TDETrader::OfferList offers = TDETrader::self()->query( "KScriptRunner/KScriptRunner", scriptTypeQuery );
53 if ( !offers.isEmpty() )
54 {
55 m_action = new TDEAction(m_scriptName, TDEShortcut(), this, TQ_SLOT(activate()), ac, "script");
56 m_isValid = true;
57 m_timeout = new TQTimer(this);
58 TQString 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, TQ_SIGNAL(timeout()), TQ_SLOT(cleanup()) );
64 }
65 }
66}
67
68KScriptAction::~KScriptAction()
69{
70 if( m_interface ) delete m_interface;
71 if( m_action ) delete m_action;
72}
73
74
75
76TDEAction * KScriptAction::action( )
77{
78 return m_action;
79}
80
81void KScriptAction::activate( )
82{
83 if( m_interface == 0L)
84 {
85 TQString scriptTypeQuery = "([X-TDE-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, TQ_SIGNAL(done(KScriptClientInterface::Result, const TQVariant &)), this, TQ_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(), TQVariant());
104 m_timeout->start(60000,false); // after 1 minute unload
105 m_refs++;
106}
107
108void KScriptAction::cleanup()
109{
110 if( m_interface && m_refs == 0)
111 {
112 delete m_interface;
113 m_interface = 0L;
114 }
115}
116
117void KScriptAction::scriptFinished()
118{
119 m_refs--;
120}
121
122KScriptActionManager::KScriptActionManager( TQObject *parent, TDEActionCollection * ac ) : TQObject(parent), m_ac(ac)
123{
124 m_actions.setAutoDelete(true);
125}
126
127KScriptActionManager::~ KScriptActionManager( )
128{
129 m_actions.clear();
130}
131
132TQPtrList< TDEAction > KScriptActionManager::scripts( TQObject * interface , const TQStringList &dirs) const
133{
134 m_actions.clear();
135 TQPtrList<TDEAction> actions;
136 TQStringList scripts;
137
138 scripts += TDEGlobal::dirs()->findAllResources("data",
139 TQString(kapp->name())+"/scripts/*.desktop", false, true );
140
141 for( TQStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it)
142 {
143 scripts += TDEGlobal::dirs()->findAllResources("data",
144 (*it)+"/*.desktop", false, true );
145 }
146
147 for (TQStringList::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, TQ_SIGNAL(error( const TQString&)), this,
156 TQ_SIGNAL(scriptError( const TQString&)));
157 connect(script, TQ_SIGNAL(warning( const TQString&)), this,
158 TQ_SIGNAL(scriptWarning( const TQString&)));
159 connect(script, TQ_SIGNAL(output( const TQString&)), this,
160 TQ_SIGNAL(scriptOutput( const TQString&)));
161 connect(script, TQ_SIGNAL(progress( int )), this,
162 TQ_SIGNAL(scriptProgress(int)));
163 connect(script, TQ_SIGNAL(done( KScriptClientInterface::Result, const TQVariant &)),this,
164 TQ_SIGNAL(scriptDone( KScriptClientInterface::Result, const TQVariant &)));
165 }
166 else
167 delete script;
168 }
169 return actions;
170}
171
172bool KScriptAction::isValid( ) const
173{
174 return m_isValid;
175}
176
177#include "tdescriptactionmanager.moc"
KScriptActionManager::scriptError
void scriptError(const TQString &msg)
Returns an error message from a script.
KScriptActionManager::scriptWarning
void scriptWarning(const TQString &msg)
Returns a warning message from a script.
KScriptActionManager::scripts
TQPtrList< TDEAction > scripts(TQObject *interface, const TQStringList &dirs=TQStringList()) const
Return all currently loaded scripts in a direcotry and attaches them to a TQObject interface.
Definition tdescriptactionmanager.cpp:132
KScriptActionManager::scriptProgress
void scriptProgress(int percent)
Returns the percentage complete of an operation in the script.
KScriptActionManager::KScriptActionManager
KScriptActionManager(TQObject *parent, TDEActionCollection *ac)
Create a script manager that is attached to an action collection.
Definition tdescriptactionmanager.cpp:122
KScriptActionManager::scriptDone
void scriptDone(KScriptClientInterface::Result result, const TQVariant &returned)
Notifies that the script has finished.
KScriptActionManager::scriptOutput
void scriptOutput(const TQString &msg)
Returns a standard out message from a script.
KScriptAction
Connects a TDEAction to a script runner.
Definition tdescriptactionmanager.h:38
KScriptAction::action
TDEAction * action()
TDEAction for the current script.
Definition tdescriptactionmanager.cpp:76
KScriptAction::isValid
bool isValid() const
Returns the validity of the current script.
Definition tdescriptactionmanager.cpp:172

KDevelop Utility Library

Skip menu "KDevelop Utility Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

KDevelop Utility Library

Skip menu "KDevelop Utility Library"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Utility Library by doxygen 1.15.0
This website is maintained by Timothy Pearson.