1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/* This file is part of the KDE project
Copyright (C) 2004 Joseph Wenninger <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
//BEGIN includes
#include "plugin_katekjswrapper.h"
#include "plugin_katekjswrapper.moc"
#include "bindings.h"
#include <kjsembed/kjsembedpart.h>
#include <kjsembed/jssecuritypolicy.h>
#include <kjsembed/jsfactory.h>
#include <kjsembed/jsconsolewidget.h>
#include <kjs/interpreter.h>
#include <kjs/value.h>
#include <kjs/object.h>
#include <kgenericfactory.h>
#include <kdebug.h>
#include <tqlayout.h>
#include <kstandarddirs.h>
#include <kate/mainwindow.h>
#include <kate/toolviewmanager.h>
#include <kdockwidget.h>
#include <tqvbox.h>
//END includes
K_EXPORT_COMPONENT_FACTORY( katekjswrapperplugin, KGenericFactory<PluginKateKJSWrapper>( "katekjswrapper" ) )
PluginKateKJSWrapperView::~PluginKateKJSWrapperView() {
}
void PluginKateKJSWrapperView::removeFromWindow() {
kdDebug()<<"PluginKateKJSWrapperView::removeFromWindow"<<endl;
for (TQValueList<TQGuardedPtr<KMDI::ToolViewAccessor> >::iterator it=toolviews.begin();it!=toolviews.end();it=toolviews.begin()) {
kdDebug()<<"removeFromWindow: removing a toolview"<<endl;
KMDI::ToolViewAccessor* tva=(*it);
toolviews.remove(it);
win->toolViewManager()->removeToolView (tva);
}
win->guiFactory()->removeClient (this);
}
PluginKateKJSWrapper::PluginKateKJSWrapper( TQObject* parent, const char* name, const TQStringList& list)
: Kate::Plugin ( (Kate::Application *)parent, name ) {
m_views.setAutoDelete(true);
m_scriptname=list[0];
m_kateAppBindings=new Kate::JS::Bindings(this);
KJSEmbed::JSSecurityPolicy::setDefaultPolicy( KJSEmbed::JSSecurityPolicy::CapabilityAll );
m_part = new KJSEmbed::KJSEmbedPart(this);
KJS::Interpreter *js = m_part->interpreter();
KJSEmbed::JSFactory *factory=m_part->factory();
/* factories for kate app classes */
factory->addTQObjectPlugin("Kate::Application",m_kateAppBindings);
factory->addTQObjectPlugin("Kate::DocumentManager",m_kateAppBindings);
factory->addTQObjectPlugin("Kate::MainWindow",m_kateAppBindings);
factory->addTQObjectPlugin("Kate::PluginManager",m_kateAppBindings);
factory->addTQObjectPlugin("Kate::InitPluginManager",m_kateAppBindings);
factory->addTQObjectPlugin("Kate::ProjectManager",m_kateAppBindings);
factory->addTQObjectPlugin("Kate::Project",m_kateAppBindings);
factory->addTQObjectPlugin("Kate::ViewManager",m_kateAppBindings);
factory->addTQObjectPlugin("Kate::View",m_kateAppBindings);
/* toplevel objects*/
KJS::Object appobj=m_part->addObject(Kate::application(),"KATE");
js->globalObject().put( js->globalExec(), "addConfigPage", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::AddConfigPage,this )));
js->globalObject().put( js->globalExec(), "setConfigPages", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::SetConfigPages,this )));
js->globalObject().put( js->globalExec(), "removeConfigPage", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::RemoveConfigPage,this )));
js->globalObject().put( js->globalExec(), "setWindowConfiguration", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::SetWindowConfiguration,this )));
js->globalObject().put( js->globalExec(), "KJSConsole", KJS::Object(new Kate::JS::Management(js->globalExec(),Kate::JS::Management::KJSConsole,this )));
/* KJSEmbed::JSConsoleWidget *w=m_part->view();
w->show();
//w->show();*/
kdDebug()<<"m_scriptname="<<m_scriptname<<endl;
m_part->runFile(locate("appdata",TQString("plugins/%1/%2.js").tqarg(m_scriptname).tqarg(m_scriptname)));
//"/home/jowenn/development/kde/cvs/kdeaddons/kate/kjswrapper/samples/test1.js");
}
PluginKateKJSWrapper::~PluginKateKJSWrapper()
{
delete m_part;
m_part=0;
}
uint PluginKateKJSWrapper::configPages () const {
KJS::Interpreter *js = m_part->interpreter();
KJS::ExecState *exec=js->globalExec();
if (! (m_configPageFactories.isNull() || (m_configPageFactories.type()==KJS::NullType))) {
KJS::Object constrs=m_configPageFactories.toObject(exec);
if (!exec->hadException()) {
if (TQString(constrs.classInfo()->className)=="Array") {
kdDebug()<<"config page constructor array detected"<<endl;
uint size=constrs.get(exec,KJS::Identifier("length")).toInteger(exec);
if (exec->hadException()) {
exec->clearException();
kdDebug()<<"Error while retrieving array length"<<endl;
}
else return size;
} else return 1;
}
}
exec->clearException();
return 0;
}
static KJS::Object getObj(KJS::Interpreter *js, KJS::Value mightBeArray, int id) {
KJS::ExecState *exec=js->globalExec();
KJS::Object constrs=mightBeArray.toObject(exec);
KJS::Value constr;
if (!exec->hadException()) {
if (TQString(constrs.classInfo()->className)=="Array") {
kdDebug()<<"config page constructor array detected"<<endl;
constr=constrs.get(exec,id);
} else constr=mightBeArray;
}
return constr.toObject(js->globalExec());
}
TQString PluginKateKJSWrapper::configPageName(uint id) const {
if (id>=configPages()) return "";
KJS::Interpreter *js = m_part->interpreter();
KJS::Object constr=getObj(js,m_configPageFactories,id);
KJS::Value o=constr.get(js->globalExec(),KJS::Identifier("name"));
TQString retVal( o.toString(js->globalExec()).qstring() );
kdDebug()<<"=============================================================================================="<<endl;
kdDebug()<<"PluginKateKJSWrapper::configPageName: "<<retVal<<endl;
kdDebug()<<"=============================================================================================="<<endl;
js->globalExec()->clearException();
return retVal;
}
TQString PluginKateKJSWrapper::configPageFullName(uint id) const {
if (id>=configPages()) return "";
KJS::Interpreter *js = m_part->interpreter();
KJS::Object constr=getObj(js,m_configPageFactories,id);
KJS::Value o=constr.get(js->globalExec(),KJS::Identifier("fullName"));
TQString retVal( o.toString(js->globalExec()).qstring() );
kdDebug()<<"=============================================================================================="<<endl;
kdDebug()<<"PluginKateKJSWrapper::configPageFullName: "<<retVal<<endl;
kdDebug()<<"=============================================================================================="<<endl;
js->globalExec()->clearException();
return retVal;
}
TQPixmap PluginKateKJSWrapper::configPagePixmap (uint /*number = 0*/,
int /*size = KIcon::SizeSmall*/) const {
return 0;
}
Kate::PluginConfigPage* PluginKateKJSWrapper::configPage (uint id,
TQWidget *w, const char */*name*/) {
kdDebug()<<"PluginKateKJSWrapper::configPage"<<endl;
if (id>=configPages()) return 0;
KJS::Interpreter *js = m_part->interpreter();
KJS::Object constr=getObj(js,m_configPageFactories,id);
if (js->globalExec()->hadException()) {
kdDebug()<<"PluginKateKJSWrapper::configPage: exit 1"<<endl;
js->globalExec()->clearException();
return 0;
}
if (!constr.implementsConstruct()) {
kdWarning()<<"config page factory has to be an object constructor"<<endl;
return 0;
}
KateKJSWrapperConfigPage *p=new KateKJSWrapperConfigPage(constr,this,w);
return (Kate::PluginConfigPage*)p;
/*
KateKJSWrapperConfigPage* p = new KateKJSWrapperConfigPage(this, w);
//init
connect( p, TQT_SIGNAL(configPageApplyRequest(KateKJSWrapperConfigPage*)),
this, TQT_SLOT(applyConfig(KateKJSWrapperConfigPage*)) );
return (Kate::PluginConfigPage*);*/
}
static KMDI::ToolViewAccessor *createToolView(KJSEmbed::JSFactory *factory,KJS::Interpreter *js, Kate::MainWindow *winN,KJS::Object win,KJS::Object viewConstructor) {
KJS::List params;
KJS::ExecState *exec = js->globalExec();
params.append(win);
exec->clearException();
int dockPos;
if (!viewConstructor.implementsConstruct()) return 0;
KJS::Value dockPosV=viewConstructor.get(exec,KJS::Identifier("startPosition"));
if (exec->hadException()) {
dockPos=KDockWidget::DockLeft;
exec->clearException();
} else {
dockPos=dockPosV.toInteger(exec);
if (exec->hadException()) {
dockPos=KDockWidget::DockLeft;
exec->clearException();
}
}
TQString viewName;
KJS::Value viewNameV=viewConstructor.get(exec,KJS::Identifier("name"));
if (exec->hadException()) {
viewName="kjs_unknown";
exec->clearException();
} else {
viewName=TQString( viewNameV.toString(exec).qstring() );
if (exec->hadException()) {
viewName="kjs_unknown";
exec->clearException();
}
}
Kate::JS::ToolView *tv=new Kate::JS::ToolView(viewConstructor,exec,factory,params,viewName.utf8());
//params.append(factory->createProxy(exec,tv));
//KJS::Object otv=viewConstructor.construct(exec,params);
if (exec->hadException()) {
kdDebug()<<"Error while calling constructor"<<endl;
delete tv;
kdDebug()<<exec->exception().toString(exec).qstring()<<endl;
exec->clearException();
return 0;
}
KMDI::ToolViewAccessor *tva=winN->toolViewManager()->addToolView((KDockWidget::DockPosition)dockPos,tv,
tv->icon()?(*(tv->icon())):TQPixmap(),tv->caption());
kdDebug()<<"****************************************************************************************"<<endl;
kdDebug()<<"PluginKateKJSWrapper: Toolview has been added"<<endl;
kdDebug()<<"****************************************************************************************"<<endl;
return tva;
}
PluginKateKJSWrapperView *PluginKateKJSWrapper::getViewObject(Kate::MainWindow *win) {
PluginKateKJSWrapperView * view=m_views[win];
if (!view) {
view=new PluginKateKJSWrapperView();
view->win=win;
connect(win,TQT_SIGNAL(destroyed()),this,TQT_SLOT(slotWindowDestroyed()));
m_views.insert(win,view);
KJS::Interpreter *js = m_part->interpreter();
KJS::ExecState *exec = js->globalExec();
view->actionCollectionObj=m_part->factory()->createProxy(exec,view->actionCollection());
view->winObj=m_part->factory()->createProxy(exec,win);
} else kdDebug()<<"returning cached View/Window Object"<<endl;
return view;
}
void PluginKateKJSWrapper::addView(Kate::MainWindow *win)
{
PluginKateKJSWrapperView * view=getViewObject(win); // this is needed to ensure correct caching the javascript object
KJS::Interpreter *js = m_part->interpreter();
KJS::ExecState *exec = js->globalExec();
exec->clearException();
kdDebug()<<"****************************************************************************************"<<endl;
kdDebug()<<"PluginKateKJSWrapper::addView"<<endl;
kdDebug()<<"****************************************************************************************"<<endl;
kdDebug()<<"checking for newWindowHandler"<<endl;
if (!m_newWindowHandler.isNull()) {
KJS::List param;
param.append(view->winObj);
KJS::Object newWinFunc=m_newWindowHandler.toObject(exec);
if (exec->hadException()) {
exec->clearException();
} else {
if (newWinFunc.implementsCall()) {
newWinFunc.call(exec,js->globalObject(),param);
if (exec->hadException()) {
kdDebug()<<"Error while calling newWindowHandler"<<endl;
exec->clearException();
}
}
}
}
if (exec->hadException()) kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): exec had an exception - 1"<<endl;
kdDebug()<<"checking for toolview constructors"<<endl;
if (! (m_toolViewConstructors.isNull() || (m_toolViewConstructors.type()==KJS::NullType))) {
KJS::Object constrs=m_toolViewConstructors.toObject(exec);
if (!exec->hadException()) {
if (TQString(constrs.classInfo()->className)=="Array") {
kdDebug()<<"Toolview constructor array detected"<<endl;
int size=constrs.get(exec,KJS::Identifier("length")).toInteger(exec);
if (exec->hadException()) {
exec->clearException();
kdDebug()<<"Error while retrieving array length"<<endl;
}
else {
for (int i=0;i<size;i++) {
KJS::Object constrO=constrs.get(exec,i).toObject(exec);
if (exec->hadException()) {
exec->clearException();
} else {
KMDI::ToolViewAccessor *w=createToolView(m_part->factory(),js,win,view->winObj,constrO);
if (w) {
view->toolviews.append(TQGuardedPtr<KMDI::ToolViewAccessor>(w));
}
exec->clearException();
}
}
}
} else {
kdDebug()<<"Single toolview constructor detected"<<endl;
if (!constrs.implementsConstruct()) {
kdWarning()<<"wrong object type"<<endl;
} else {
KMDI::ToolViewAccessor *w=createToolView(m_part->factory(),js,win,view->winObj,constrs);
if (w) {
view->toolviews.append(TQGuardedPtr<KMDI::ToolViewAccessor>(w));
}
exec->clearException();
}
}
}
} else kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): no toolview constructors"<<endl;
if (exec->hadException()) kdDebug()<<"void PluginKateKJSWrapper::addView(Kate::MainWindow *win): exec had an exception - 2"<<endl;
view->setInstance (new KInstance("kate"));
view->setXMLFile(TQString("plugins/%1/%2.rc").tqarg(m_scriptname).tqarg(m_scriptname));
win->guiFactory()->addClient (view);
}
void PluginKateKJSWrapper::slotWindowDestroyed() {
m_views.remove((void*)sender());
}
void PluginKateKJSWrapper::removeView(Kate::MainWindow *win)
{
//here toolviews must not be destroyed. Only cleanup functions called the view should be removed in the slot connected to the windows destroy signal only
m_views[win]->removeFromWindow();
}
void PluginKateKJSWrapper::applyConfig( KateKJSWrapperConfigPage *p )
{
#if 0
config->writeEntry( "Command History Length", p->sb_cmdhistlen->value() );
// truncate the cmd hist if nessecary?
config->writeEntry( "Start In", p->rg_startin->id(p->rg_startin->selected()) );
config->sync();
#endif
}
KateKJSWrapperConfigPage::KateKJSWrapperConfigPage(KJS::Object pageConstructor,PluginKateKJSWrapper* parent,
TQWidget *parentWidget)
: Kate::PluginConfigPage( parentWidget ),m_plugin(parent)
{
TQVBoxLayout *l=new TQVBoxLayout(this);
l->setAutoAdd(true);
l->activate();
KJS::Interpreter *js = parent->m_part->interpreter();
KJS::ExecState *exec = js->globalExec();
exec->clearException();
KJS::List param;
param.append(parent->m_part->factory()->createProxy(exec,this,0));
m_pageObject=pageConstructor.construct(exec,param);
}
static void callJS(KJSEmbed::KJSEmbedPart *p,KJS::Object o,const TQString& funcName){
KJS::Interpreter *js = p->interpreter();
KJS::ExecState *exec = js->globalExec();
KJS::List param;
exec->clearException();
KJS::Value funcV=o.get(exec,KJS::Identifier(funcName));
if (exec->hadException()) {
#warning clear exception ?
return;
}
KJS::Object func=funcV.toObject(exec);
if (exec->hadException()) {
#warning clear exception ?
return;
}
if (func.implementsCall()) {
func.call(exec,o,param);
if (js->globalExec()->hadException()) {
#warning clear exception ?
return;
}
}
}
void KateKJSWrapperConfigPage::apply()
{
callJS(m_plugin->m_part,m_pageObject,"apply");
}
void KateKJSWrapperConfigPage::reset()
{
callJS(m_plugin->m_part,m_pageObject,"reset");
}
void KateKJSWrapperConfigPage::defaults()
{
callJS(m_plugin->m_part,m_pageObject,"defaults");
}
Kate::JS::ToolView::ToolView(KJS::Object constr, KJS::ExecState *exec, KJSEmbed::JSFactory *factory, KJS::List parameters, const char *name):TQVBox(0,name) {
parameters.append(factory->createProxy(exec,this));
handler=constr.construct(exec,parameters);
}
Kate::JS::ToolView::~ToolView() {
}
|