kcm_wineconfig.cpp
#include <pythonize.h>
#include <kcmodule.h>
#include <kglobal.h>
#include <klocale.h>
#include <klibloader.h>
#include <kstandarddirs.h>
#include <ksimpleconfig.h>
#include <qstring.h>
#include <sip.h>
#define MODULE_DIR "/home/jr/src/guidance/kde-guidance-0.8.0/debian/tmp/usr/share/apps/guidance"
#define MODULE_NAME "wineconfig"
#define FACTORY "create_wineconfig"
#define CPP_FACTORY create_wineconfig
#define LIB_PYTHON "libpython2.5.so"
#define debug 1
static KCModule *report_error(char *msg) {
if (debug) printf ("error: %s\n", msg);
return NULL;
}
static KCModule* return_instance( QWidget *parent, const char *name ) {
KCModule* kcmodule;
PyObject *pyKCModuleTuple;
PyObject *pyKCModule;
Pythonize *pyize;
int isErr;
QString script(MODULE_NAME);
KLibLoader::self()->globalLibrary(LIB_PYTHON);
pyize = initialize();
if (!pyize) {
return report_error ("***Failed to start interpreter\n");
}
QString path = QString(MODULE_DIR);
if(path == QString::null) {
return report_error ("***Failed to locate script path");
}
if(!pyize->appendToSysPath (path.latin1 ())) {
return report_error ("***Failed to set sys.path\n");
}
PyObject *pyModule = pyize->importModule ((char *)script.latin1 ());
if(!pyModule) {
PyErr_Print();
return report_error ("***failed to import module\n");
}
QString bridge = QString("import sip\n"
"import qt\n"
"def kcontrol_bridge_" FACTORY "(parent,name):\n"
" if parent!=0:\n"
#if SIP_VERSION >= 0x040200
" wparent = sip.wrapinstance(parent,qt.QWidget)\n"
#else
" wparent = sip.wrapinstance(parent,'QWidget')\n"
#endif
" else:\n"
" wparent = None\n"
" inst = " FACTORY "(wparent, name)\n"
" return (inst,sip.unwrapinstance(inst))\n");
PyRun_String(bridge.latin1(),Py_file_input,PyModule_GetDict(pyModule),PyModule_GetDict(pyModule));
PyObject *kcmFactory = pyize->getNewObjectRef(pyModule, "kcontrol_bridge_" FACTORY);
if(!kcmFactory) {
return report_error ("***failed to find module factory\n");
}
PyObject *pyParent = PyLong_FromVoidPtr(parent);
PyObject *pyName = PyString_FromString(MODULE_NAME);
PyObject *args = Py_BuildValue ("NN", pyParent, pyName);
if(pyName && pyParent && args) {
pyKCModuleTuple = pyize->runFunction(kcmFactory, args);
if(!pyKCModuleTuple) {
PyErr_Print();
return report_error ("*** runFunction failure\n;");
}
} else {
return report_error ("***failed to create args\n");
}
pyize->decref(args);
pyize->decref(kcmFactory);
Py_INCREF(PyTuple_GET_ITEM(pyKCModuleTuple,0));
isErr = 0;
pyKCModule = PyTuple_GET_ITEM(pyKCModuleTuple,1);
kcmodule = (KCModule *)PyLong_AsVoidPtr(pyKCModule);
if(!kcmodule) {
return report_error ("***failed sip conversion to C++ pointer\n");
}
pyize->decref(pyKCModuleTuple);
KGlobal::locale()->insertCatalogue(script);
return kcmodule;
}
extern "C" {
KCModule* CPP_FACTORY(QWidget *parent, const char *name) {
return return_instance(parent, name);
}
}