~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/plugins/generic/python/pythonplugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * pythonscriptingplugin.h - Psi plugin providing Python scripting
 
3
 * Copyright (C) 2006  Kevin Smith
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * You can also redistribute and/or modify this program under the
 
11
 * terms of the Psi License, specified in the accompanied COPYING
 
12
 * file, as published by the Psi Project; either dated January 1st,
 
13
 * 2005, or (at your option) any later version.
 
14
 *
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this library; if not, write to the Free Software
 
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 *
 
24
 */
 
25
 
 
26
#include <Python.h>
 
27
 
 
28
#include <QtCore>
 
29
#include <QDebug>
 
30
 
 
31
#include "psiplugin.h"
 
32
 
 
33
extern int Py_VerboseFlag;
 
34
 
 
35
/**
 
36
 * Function to obtain all the directories in which plugins can be stored
 
37
 * \return List of plugin directories
 
38
 */ 
 
39
static QStringList scriptDirs()
 
40
{
 
41
        QStringList l;
 
42
        l += "/Users/kismith/.psi/python";
 
43
        return l;
 
44
}
 
45
 
 
46
class PythonPlugin : public PsiPlugin
 
47
{
 
48
        Q_OBJECT
 
49
        Q_INTERFACES(PsiPlugin)
 
50
 
 
51
public:
 
52
        PythonPlugin();
 
53
        ~PythonPlugin();
 
54
        virtual QString name() const; 
 
55
        virtual QString shortName() const;
 
56
        virtual QString version() const;
 
57
        virtual bool processEvent( const QString& account, QDomNode &event );
 
58
        
 
59
private:
 
60
        QStringList scriptObjects_;
 
61
        void loadScripts();
 
62
        QString loadScript( const QString& fileName);
 
63
        PyObject* main_module_;
 
64
        PyObject* main_dict_;
 
65
 
 
66
};
 
67
 
 
68
Q_EXPORT_PLUGIN(PythonPlugin);
 
69
 
 
70
PythonPlugin::PythonPlugin()
 
71
{
 
72
        Py_Initialize();
 
73
        main_module_ = PyImport_AddModule("__main__");
 
74
        main_dict_ = PyModule_GetDict(main_module_);
 
75
        QString command="print \"Python running\"";
 
76
        PyRun_SimpleString(qPrintable(command));
 
77
        loadScripts();
 
78
        
 
79
}
 
80
 
 
81
PythonPlugin::~PythonPlugin()
 
82
{
 
83
        Py_Finalize();
 
84
}
 
85
 
 
86
QString PythonPlugin::name() const
 
87
{
 
88
        return "Python Scripting Plugin";
 
89
}
 
90
 
 
91
QString PythonPlugin::shortName() const
 
92
{
 
93
        return "python";
 
94
}
 
95
 
 
96
QString PythonPlugin::version() const
 
97
{
 
98
        return "0.0";
 
99
}
 
100
 
 
101
bool PythonPlugin::processEvent( const QString& account, QDomNode &event )
 
102
{
 
103
        foreach (QString script, scriptObjects_)
 
104
        {
 
105
                qDebug() << (qPrintable(QString("running it on script %1").arg(script)));
 
106
                QString scriptCall=QString("%1.processEvent(\"\"\"%2\"\"\")").arg(script).arg(toString(event));
 
107
                QString command=QString("%1").arg(scriptCall);
 
108
                qDebug() << (qPrintable(QString("Running python command:\n%1").arg(command)));
 
109
                PyRun_SimpleString(qPrintable(command));
 
110
        }
 
111
        return true;
 
112
}
 
113
 
 
114
void PythonPlugin::loadScripts()
 
115
{
 
116
        foreach(QString d, scriptDirs()) {
 
117
                QDir dir(d);
 
118
                foreach(QString file, dir.entryList()) {
 
119
                        file=dir.absoluteFilePath(file);
 
120
                        if (file.contains(".py"))
 
121
                                loadScript(file);
 
122
                }
 
123
        }
 
124
}
 
125
 
 
126
QString PythonPlugin::loadScript(const QString& fileName)
 
127
{
 
128
        FILE *file;
 
129
        if ((file = fopen(qPrintable(fileName),"r")) == NULL )
 
130
                return "";
 
131
        qDebug() << (qPrintable(QString("Found script file %1").arg(fileName)));
 
132
        PyObject* pyName = PyRun_File(file, qPrintable(fileName), Py_file_input, main_dict_, main_dict_);
 
133
        qDebug("well, we got this far");
 
134
        fclose(file);
 
135
        pyName = PyDict_GetItemString( main_dict_, "name" );
 
136
        if (pyName == NULL || !PyString_Check(pyName))
 
137
        {
 
138
                qWarning(qPrintable(QString("Tried to load %1 but it didn't return a string for its name").arg(fileName)));
 
139
                return "";
 
140
        }
 
141
        QString name( PyString_AsString( pyName ) );
 
142
        scriptObjects_.append(name);
 
143
        qDebug() << (qPrintable(QString("Found script %1 in the file").arg(name)));
 
144
        return name;
 
145
}
 
146
 
 
147
#include "pythonplugin.moc"