KDevelop API Documentation

parts/scripting/scriptingpart.cpp

Go to the documentation of this file.
00001 /*************************************************************************** 00002 * Copyright (C) 2001 by Bernd Gehrmann * 00003 * bernd@kdevelop.org * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 00012 #include "scriptingpart.h" 00013 #include <Python.h> 00014 #include <stdlib.h> 00015 #include <dcopclient.h> 00016 #include <dcopobject.h> 00017 #include <kaction.h> 00018 #include <kdebug.h> 00019 #include <kdevgenericfactory.h> 00020 #include <kstandarddirs.h> 00021 00022 #include "kdevcore.h" 00023 #include <qfile.h> 00024 00025 00026 static ScriptingPart *scripting_part; 00027 extern "C" { 00028 void initkdevelopc(); 00029 void initpydcopc(); 00030 int __kde_do_not_unload; 00031 } 00032 00033 typedef KDevGenericFactory<ScriptingPart> ScriptingFactory; 00034 static const KAboutData data("kdevscripting", ("Python Scripting Support"), "1.0"); 00035 K_EXPORT_COMPONENT_FACTORY( libkdevscripting, ScriptingFactory( &data ) ) 00036 00037 ScriptingPart::ScriptingPart(QObject *parent, const char *name, const QStringList &) 00038 : KDevPlugin("PythonScripting", "scripting", parent, name ? name : "ScriptingPart") 00039 { 00040 setInstance(ScriptingFactory::instance()); 00041 00042 QString xml = QString::fromLatin1("<!DOCTYPE kpartgui SYSTEM \"kpartgui.dtd\">\n" 00043 "<kpartgui version=\"1\" name=\"editorpart\">\n" 00044 "<MenuBar>\n" 00045 "</MenuBar>\n" 00046 "</kpartgui>"); 00047 guiDocument.setContent(xml); 00048 setDOMDocument(guiDocument); 00049 00050 scripting_part = this; 00051 00052 QString moddir = KGlobal::dirs()->findResourceDir("data", "kdevscripting/kdevelop.py") + "kdevscripting"; 00053 char *env = strdup(QString::fromLatin1("PYTHONPATH=%1").arg(moddir).latin1()); 00054 putenv(env); 00055 Py_Initialize(); 00056 free(env); 00057 00058 kdDebug(9011) << "Init kdevelopc" << endl; 00059 initkdevelopc(); 00060 00061 kdDebug(9011) << "Init pydcopc" << endl; 00062 initpydcopc(); 00063 00064 kdDebug(9011) << "import kdevelop" << endl; 00065 PyRun_SimpleString((char*)"import kdevelop"); 00066 00067 kdDebug(9011) << "from init import *" << endl; 00068 PyRun_SimpleString((char*)"from init import *"); 00069 00070 #if 0 00071 QString initfile = locate("data", "kdevpython/init.py"); 00072 FILE *f1 = fopen(QFile::encodeName(initfile), "r"); 00073 kdDebug(9011) << "evaluate init.py" << endl; 00074 PyRun_SimpleFile(f1, QFile::encodeName(initfile)); 00075 fclose(f1); 00076 #endif 00077 } 00078 00079 00080 extern DCOPObject *pydcopc_dispatcher; 00081 00082 00083 ScriptingPart::~ScriptingPart() 00084 { 00085 delete pydcopc_dispatcher; 00086 pydcopc_dispatcher = 0; 00087 } 00088 00089 00090 PyObject *ScriptingPart::addMenuItem(PyObject *args) 00091 { 00092 char *menu, *submenu; 00093 PyObject *func; 00094 00095 if (!PyArg_ParseTuple(args, (char*)"ssO", &menu, &submenu, &func)) 00096 return 0; 00097 00098 if (!PyCallable_Check(func)) { 00099 kdDebug(9011) << "Scripting function not callable" << endl; 00100 return 0; 00101 } 00102 00103 QString menustr = QString::fromLatin1(menu); 00104 QString submenustr = QString::fromLatin1(submenu); 00105 QString ident = menustr + submenustr; 00106 00107 Py_XINCREF(func); 00108 00109 actions.insert(ident, func); 00110 00111 core()->insertNewAction( new KAction(submenustr, 0, this, SLOT(slotScriptAction()), 00112 actionCollection(), ident.latin1()) ); 00113 00114 QDomElement el = guiDocument.documentElement(); 00115 el = el.namedItem("MenuBar").toElement(); 00116 QDomElement child = el.firstChild().toElement(); 00117 while (!child.isNull()) { 00118 if (child.tagName() == "Menu" && child.attribute("name") == menustr) 00119 break; 00120 child = child.nextSibling().toElement(); 00121 } 00122 if (child.isNull()) { 00123 child = guiDocument.createElement(QString::fromLatin1("Menu")); 00124 child.setAttribute(QString::fromLatin1("name"), menustr); 00125 el.appendChild(child); 00126 } 00127 el = child; 00128 00129 child = guiDocument.createElement(QString::fromLatin1("Action")); 00130 child.setAttribute(QString::fromLatin1("name"), ident); 00131 el.appendChild(child); 00132 00133 kdDebug(9011) << "New dom document: " << guiDocument.toString() << endl; 00134 00135 setDOMDocument(guiDocument); 00136 00137 Py_INCREF(Py_None); 00138 return Py_None; 00139 } 00140 00141 00142 00143 PyObject *ScriptingPart::appId(PyObject *args) 00144 { 00145 if (!PyArg_ParseTuple(args, (char*)"")) 00146 return NULL; 00147 00148 return Py_BuildValue((char*)"s", DCOPClient::mainClient()->appId().data()); 00149 } 00150 00151 00152 void ScriptingPart::slotScriptAction() 00153 { 00154 QString ident = QString::fromLatin1(sender()->name()); 00155 kdDebug(9011) << "Action " << ident << " activated" << endl; 00156 00157 PyObject *arglist = Py_BuildValue((char*)"()"); 00158 PyEval_CallObject(actions[ident], arglist); 00159 } 00160 00161 00162 extern "C" { 00163 00164 static PyObject *kdevelopc_addMenuItem(PyObject */*self*/, PyObject *args) 00165 { 00166 return scripting_part->addMenuItem(args); 00167 } 00168 static PyObject *kdevelopc_appId(PyObject */*self*/, PyObject *args) 00169 { 00170 return scripting_part->appId(args); 00171 } 00172 00173 static struct PyMethodDef kdevelopc_methods[] = { 00174 { (char*)"appId", kdevelopc_appId, METH_VARARGS, NULL }, 00175 { (char*)"addMenuItem", kdevelopc_addMenuItem, METH_VARARGS, NULL }, 00176 { NULL, NULL, 0, NULL } 00177 }; 00178 00179 void initkdevelopc() 00180 { 00181 (void) Py_InitModule((char*)"kdevelopc", kdevelopc_methods); 00182 } 00183 } 00184 00185 #include "scriptingpart.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Oct 19 08:01:52 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003