kexiscriptpart.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Lucijan Busch <lucijan@kde.org>
3 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
4 Copyright (C) 2005 Sebastian Sauer <mail@dipe.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20*/
21
22#include "kexiscriptpart.h"
23#include "kexiscriptdesignview.h"
24
25#include "kexiviewbase.h"
26#include "keximainwindow.h"
27#include "kexiproject.h"
28
29#include <kross/main/manager.h>
30#include <kross/main/scriptaction.h>
31#include <kross/main/scriptguiclient.h>
32
33#include <kgenericfactory.h>
34#include <kexipartitem.h>
35#include <kxmlguiclient.h>
36#include <kexidialogbase.h>
37#include <tdeconfig.h>
38#include <kdebug.h>
39
41class KexiScriptPart::Private
42{
43 public:
44 Kross::Api::ScriptGUIClient* scriptguiclient;
45};
46
47KexiScriptPart::KexiScriptPart(TQObject *parent, const char *name, const TQStringList &l)
48 : KexiPart::Part(parent, name, l)
49 , d( new Private() )
50{
51 d->scriptguiclient = 0;
52
53 // REGISTERED ID:
54 m_registeredPartID = (int)KexiPart::ScriptObjectType;
55
56 m_names["instanceName"]
57 = i18n("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
58 "Use '_' character instead of spaces. First character should be a..z character. "
59 "If you cannot use latin characters in your language, use english word.",
60 "script");
61 m_names["instanceCaption"] = i18n("Script");
62 m_supportedViewModes = Kexi::DesignViewMode;
63}
64
66{
67 delete d->scriptguiclient;
68 delete d;
69}
70
71bool KexiScriptPart::execute(KexiPart::Item* item, TQObject* sender)
72{
73 Q_UNUSED(sender);
74
75 if(! item) {
76 kdWarning() << "KexiScriptPart::execute: Invalid item." << endl;
77 return false;
78 }
79
80 KexiDialogBase* dialog = new KexiDialogBase(m_mainWin);
81 dialog->setId( item->identifier() );
82 KexiScriptDesignView* view = dynamic_cast<KexiScriptDesignView*>( createView(dialog, dialog, *item, Kexi::DesignViewMode) );
83 if(! view) {
84 kdWarning() << "KexiScriptPart::execute: Failed to create a view." << endl;
85 return false;
86 }
87
88 Kross::Api::ScriptAction* scriptaction = view->scriptAction();
89 if(scriptaction) {
90
91 const TQString dontAskAgainName = "askExecuteScript";
92 TDEConfig* config = TDEGlobal::config();
93 TQString dontask = config->readEntry(dontAskAgainName).lower();
94
95 bool exec = (dontask == "yes");
96 if( !exec && dontask != "no" ) {
97 exec = KMessageBox::warningContinueCancel(0,
98 i18n("Do you want to execute the script \"%1\"?\n\nScripts obtained from unknown sources can contain dangerous code.").arg(scriptaction->text()),
99 i18n("Execute Script?"), KGuiItem(i18n("Execute"), "exec"),
100 dontAskAgainName, KMessageBox::Notify | KMessageBox::Dangerous
101 ) == KMessageBox::Continue;
102 }
103
104 if(exec) {
105 //TQTimer::singleShot(10, scriptaction, TQ_SLOT(activate()));
106 d->scriptguiclient->executeScriptAction( scriptaction );
107 }
108 }
109
110 view->deleteLater(); // not needed any longer.
111 return true;
112}
113
115{
116 if(m_mainWin) {
117 // At this stage the KexiPart::Part::m_mainWin should be defined, so
118 // that we are able to use it's KXMLGUIClient.
119
120 // Initialize the ScriptGUIClient.
121 d->scriptguiclient = new Kross::Api::ScriptGUIClient( m_mainWin );
122
123 // Publish the KexiMainWindow singelton instance. At least the KexiApp
124 // scripting-plugin depends on this instance and loading the plugin will
125 // fail if it's not avaiable.
126 if(! Kross::Api::Manager::scriptManager()->hasChild("KexiMainWindow")) {
127 Kross::Api::Manager::scriptManager()->addTQObject(m_mainWin, "KexiMainWindow");
128
129 // Add the TDEAction's provided by the ScriptGUIClient to the
130 // KexiMainWindow.
131 //FIXME: fix+use createSharedPartAction() whyever it doesn't work as expected right now...
132 TQPopupMenu* popup = m_mainWin->findPopupMenu("tools");
133 if(popup) {
134 TDEAction* execscriptaction = d->scriptguiclient->action("executescriptfile");
135 if(execscriptaction)
136 execscriptaction->plug( popup );
137 TDEAction* configscriptaction = d->scriptguiclient->action("configurescripts");
138 if(configscriptaction)
139 configscriptaction->plug( popup );
140 TDEAction* scriptmenuaction = d->scriptguiclient->action("installedscripts");
141 if(scriptmenuaction)
142 scriptmenuaction->plug( popup );
143 /*
144 TDEAction* execscriptmenuaction = d->scriptguiclient->action("executedscripts");
145 if(execscriptmenuaction)
146 execscriptmenuaction->plug( popup );
147 TDEAction* loadedscriptmenuaction = d->scriptguiclient->action("loadedscripts");
148 if(loadedscriptmenuaction)
149 loadedscriptmenuaction->plug( popup );
150 */
151 }
152 }
153 }
154}
155
157{
158 //createSharedAction(Kexi::DesignViewMode, i18n("Execute Script"), "media-playback-start", 0, "data_execute");
159 createSharedAction(Kexi::DesignViewMode, i18n("Configure Editor..."), "configure", 0, "script_config_editor");
160}
161
162KexiViewBase* KexiScriptPart::createView(TQWidget *parent, KexiDialogBase* dialog, KexiPart::Item& item, int viewMode, TQMap<TQString,TQString>*)
163{
164 TQString partname = item.name();
165 if( ! partname.isNull() ) {
166 KexiMainWindow *win = dialog->mainWin();
167 if(!win || !win->project() || !win->project()->dbConnection())
168 return 0;
169
170 Kross::Api::ScriptActionCollection* collection = d->scriptguiclient->getActionCollection("projectscripts");
171 if(! collection) {
172 collection = new Kross::Api::ScriptActionCollection( i18n("Scripts"), d->scriptguiclient->actionCollection(), "projectscripts" );
173 d->scriptguiclient->addActionCollection("projectscripts", collection);
174 }
175
176 const char* name = partname.latin1();
177 Kross::Api::ScriptAction::Ptr scriptaction = collection->action(name);
178 if(! scriptaction) {
179 scriptaction = new Kross::Api::ScriptAction(partname);
180 collection->attach(scriptaction); //TODO remove again on unload!
181 }
182
183 if(viewMode == Kexi::DesignViewMode) {
184 return new KexiScriptDesignView(win, parent, scriptaction);
185 }
186 }
187 return 0;
188}
189
190TQString KexiScriptPart::i18nMessage(const TQCString& englishMessage) const
191{
192 if (englishMessage=="Design of object \"%1\" has been modified.")
193 return i18n("Design of script \"%1\" has been modified.");
194 if (englishMessage=="Object \"%1\" already exists.")
195 return i18n("Script \"%1\" already exists.");
196 return englishMessage;
197}
198
199K_EXPORT_COMPONENT_FACTORY( kexihandler_script, KGenericFactory<KexiScriptPart>("kexihandler_script") )
200
201#include "kexiscriptpart.moc"
Base class for child window of Kexi's main application window.
void setId(int id)
Kexi's main window interface This interface is implemented by KexiMainWindowImpl class.
virtual KexiProject * project()=0
Project data of currently opened project or NULL if no project here yet.
Information about a single object that can be instantiated using Kexi Part.
int m_supportedViewModes
Definition kexipart.h:301
int m_registeredPartID
Definition kexipart.h:318
TDEAction * createSharedAction(int mode, const TQString &text, const TQString &pix_name, const TDEShortcut &cut, const char *name, const char *subclassName=0)
Definition kexipart.cpp:170
TQMap< TQString, TQString > m_names
Set of i18n'd action names for, initialised on KexiPart::Part subclass ctor The names are useful beca...
Definition kexipart.h:297
KexiDB::Connection * dbConnection() const
The KexiScriptDesignView class provides the KexiViewBase to manage script modules in the design-view.
Kross::Api::ScriptAction * scriptAction() const
virtual bool execute(KexiPart::Item *item, TQObject *sender=0)
Implementation of the KexiPart::Part::execute method used to execute the passed item instance.
virtual ~KexiScriptPart()
Destructor.
virtual KexiViewBase * createView(TQWidget *parent, KexiDialogBase *dialog, KexiPart::Item &item, int viewMode=Kexi::DesignViewMode, TQMap< TQString, TQString > *staticObjectArgs=0)
Create a new view.
KexiScriptPart(TQObject *parent, const char *name, const TQStringList &args)
Constructor.
virtual TQString i18nMessage(const TQCString &englishMessage) const
virtual void initPartActions()
Initialize the part's actions.
virtual void initInstanceActions()
Initialize the instance actions.
Base class for single view embeddable of in KexiDialogBase.