summaryrefslogtreecommitdiffstats
path: root/dcoppython/shell/pcop.h
blob: f1ce9c2bd78cd026c10a062a98d45710ada263b3 (plain)
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
/***************************************************************************
 *   Copyright (C) 2003 by Julian Rockey (linux@jrockey.com)               *
 *   Original code by Torben Weis                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#ifndef __pcop_h__
#define __pcop_h__

#include <Python.h>

#include <tqcstring.h>
#include <tqlist.h>
#include <tqasciidict.h>

#include <dcopclient.h>
#include <dcopobject.h>

class TQDataStream;

namespace PythonDCOP {
  class Client;
  class PCOPMethod;
  class ImportedModules;

  // Python interface
  PyObject *dcop_call( PyObject* self, PyObject* args );
  PyObject *application_list( PyObject *self, PyObject *args );
  PyObject *object_list(PyObject *self, PyObject *args );
  PyObject *method_list(PyObject *self, PyObject *args );
  PyObject *register_as( PyObject *self, PyObject *args);
  PyObject *create_dcop_object( PyObject *self, PyObject *args);
  PyObject *set_method_list( PyObject *self, PyObject *args);
  PyObject *connect_DCOP_Signal( PyObject *self, PyObject *args);
  PyObject *disconnect_DCOP_Signal( PyObject *self, PyObject *args);


  // helpers...
  void delete_dcop_object(void *vp);
  PyObject *make_py_list(const QCStringList &qt_list);

  /**
   * Used by the Python interface to talk to DCOP
   */
  class Client {
  public:
    Client();
    ~Client();
    void processEvents();
    DCOPClient *dcop();
//     ImportedModules *module() const { return m_module; }
    static Client *instance();
  protected:
    DCOPClient *m_dcop;
//     ImportedModules *m_module;
    static Client *s_instance;
    TQApplication *m_qapp;
  };

  /**
   * Class representing a DCOPObject.
   * This class represents a DCOP object in a "server" capacity.
   */
  class PCOPObject : public DCOPObject
  {
  public:
    /**
     * Construct from a pointer to the Python object holding this CObject.
     */
    PCOPObject(PyObject *py_obj);

    /**
     * Construct from a pointer to the Python object holding this CObject and
     * a DCOP object ID.
     */
    PCOPObject(PyObject *py_obj, const char *objid);

    virtual ~PCOPObject();

    /**
     * Process method fun, whose arguments are marshalled in data.
     * Set replyType to be the reply type and marshall the reply data into replyData.
     */
    virtual bool process(const TQCString &fun, const TQByteArray &data, TQCString& replyType, TQByteArray &replyData);

    /**
     * Return list of supported functions (methods).
     */
    virtual QCStringList functions();

    /**
     * Set the list of methods that this object handles.
     * The key of the QT dictionary is the method signature; the data in
     * the dictionary is a pointer to the python method to which it corresponds.
     */
    virtual bool setMethodList(TQAsciiDict<PyObject> meth_list);

    /**
     * Returns the current list of methods, as set by setMethodList.
     */
    virtual PyObject *methodList();

    /**
     * Matches an 'incoming' method signature (fun) and returns a PCOPMethod pointer,
     * or NULL if none match.
     */
    PCOPMethod *matchMethod(const TQCString &fun);

  private:
    virtual bool py_process(const TQCString &fun, const TQByteArray &data, TQCString& replyType, TQByteArray &replyData);

    /**
     * The Python object holding this CObject.
     */
    PyObject *m_py_obj;

    /**
     * The list of methods this object supports.
     */
    TQAsciiDict<PCOPMethod> m_methods;

  };

  /**
   * Class representing a data type, with methods for DCOP marshalling and unmarshalling.
   */
  class PCOPType
  {
  public:
    PCOPType( const TQCString& dcop_representation);
    ~PCOPType();

    TQCString signature() const;

    PyObject* demarshal( TQDataStream& str ) const;
    bool marshal( PyObject* obj, TQDataStream& str ) const;

    // checks if the given PyObject can be marshalled as this PCOPType
    bool isMarshallable( PyObject *obj ) const;

    const TQCString &type() const { return m_type; }
    const PCOPType *leftType() const { return m_leftType; }
    const PCOPType *rightType() const { return m_rightType; }

    // TODO: make these private
    TQCString m_type;
    PCOPType* m_leftType;
    PCOPType* m_rightType;

  };

  /**
   * Class representing a DCOP method
   */
  class PCOPMethod
  {
  public:
    PCOPMethod( const TQCString& dcop_signature );
    ~PCOPMethod();

    int paramCount() const;
//     TQCString signature() const;
//     TQCString name() const;
    PCOPType* param( int );
    const PCOPType* param( int ) const;

    bool setPythonMethod(PyObject *py_method);
    PyObject *pythonMethod() const { return m_py_method; }
    const TQCString &signature() const { return m_signature; }
    const TQCString &name() const { return m_name; }
    const PCOPType *type() const { return m_type; }

    TQCString m_signature;
    TQCString m_name;
    PCOPType* m_type;
    TQList<PCOPType> m_params;
  private:
    PyObject *m_py_method;
  };

  /**
   * Class representing a DCOP class.
   */
  class PCOPClass
  {
  public:
    PCOPClass( const QCStringList& dcop_style_methods);
    ~PCOPClass();

    const PCOPMethod* method( const TQCString &name, PyObject *argTuple = 0 );

    QCStringList m_ifaces;
    TQAsciiDict<PCOPMethod> m_methods;
  };

}

#endif