delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / nsplugins / viewer / nsplugin.h
blob8e523ca63613f6cb5bd0417c7caeeb7fc3726863
1 /*
2 This is an encapsulation of the Netscape plugin API.
4 Copyright (c) 2000 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
5 Stefan Schimanski <1Stein@gmx.de>
6 2003-2005 George Staikos <staikos@kde.org>
7 2007, 2008 Maksim Orlovich <maksim@kde.org>
8 2006, 2007, 2008 Apple Inc.
9 2008 Collabora, Ltd.
10 2008 Sebastian Sauer <mail@dipe.org>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #ifndef __NS_PLUGIN_H__
28 #define __NS_PLUGIN_H__
30 #include <QObject>
32 #include <QMap>
33 #include <QPointer>
34 #include <QQueue>
35 #include <QList>
37 #include <KDebug>
39 #include <kparts/browserextension.h> // for OpenUrlArguments
40 #include <kio/job.h>
41 #include <QDBusObjectPath>
43 #include <QX11EmbedWidget>
45 #define XP_UNIX
46 #define MOZ_X11
47 #include "sdk/npupp.h"
49 typedef char* NP_GetMIMEDescriptionUPP(void);
50 typedef NPError NP_InitializeUPP(NPNetscapeFuncs*, NPPluginFuncs*);
51 typedef NPError NP_ShutdownUPP(void);
53 #include <X11/Intrinsic.h>
54 #include <fixx11h.h>
56 void quitXt();
58 class OrgKdeNspluginsCallBackInterface;
59 class KLibrary;
60 class QTimer;
63 class NSPluginStreamBase : public QObject
65 Q_OBJECT
66 friend class NSPluginInstance;
67 public:
68 NSPluginStreamBase( class NSPluginInstance *instance );
69 ~NSPluginStreamBase();
71 KUrl url() { return _url; }
72 int pos() { return _pos; }
73 void stop();
75 Q_SIGNALS:
76 void finished( NSPluginStreamBase *strm );
78 protected:
79 void finish( bool err );
80 bool pump();
81 bool error() { return _error; }
82 void queue( const QByteArray &data );
83 bool create( const QString& url, const QString& mimeType, void *notify, bool forceNotify = false );
84 int tries() { return _tries; }
85 void inform( );
86 void updateURL( const KUrl& newUrl );
88 class NSPluginInstance *_instance;
89 uint16 _streamType;
90 NPStream *_stream;
91 void *_notifyData;
92 KUrl _url;
93 QString _fileURL;
94 QString _mimeType;
95 QByteArray _data;
96 class KTemporaryFile *_tempFile;
98 private:
99 int process( const QByteArray &data, int start );
101 unsigned int _pos;
102 QByteArray _queue;
103 int _queuePos;
104 int _tries;
105 bool _onlyAsFile;
106 bool _error;
107 bool _informed;
108 bool _forceNotify;
112 class NSPluginStream : public NSPluginStreamBase
114 Q_OBJECT
116 public:
117 NSPluginStream( class NSPluginInstance *instance );
118 ~NSPluginStream();
120 bool get(const QString& url, const QString& mimeType, void *notifyData, bool reload = false);
121 bool post(const QString& url, const QByteArray& data, const QString& mimeType, void *notifyData,
122 const KParts::OpenUrlArguments& args,
123 const KParts::BrowserArguments& browserArgs);
125 protected Q_SLOTS:
126 void data(KIO::Job *job, const QByteArray &data);
127 void totalSize(KJob *job, qulonglong size);
128 void mimetype(KIO::Job * job, const QString &mimeType);
129 void result(KJob *job);
130 void redirection(KIO::Job *job, const KUrl& url);
131 void resume();
133 protected:
134 QPointer<KIO::TransferJob> _job;
135 QTimer *_resumeTimer;
139 class NSPluginBufStream : public NSPluginStreamBase
141 Q_OBJECT
143 public:
144 NSPluginBufStream( class NSPluginInstance *instance );
145 ~NSPluginBufStream();
147 bool get( const QString& url, const QString& mimeType, const QByteArray &buf, void *notifyData, bool singleShot=false );
149 protected Q_SLOTS:
150 void timer();
152 protected:
153 QTimer *_timer;
154 bool _singleShot;
157 class PluginHost;
159 class NSPluginInstance : public QObject
161 Q_OBJECT
163 public:
165 // constructor, destructor
166 NSPluginInstance( NPP privateData, NPPluginFuncs *pluginFuncs, KLibrary *handle,
167 const QString &src, const QString &mime,
168 const QString &appId, const QString &callbackId, bool embed,
169 QObject *parent );
170 ~NSPluginInstance();
172 // DBus-exported functions
173 void shutdown();
174 void setupWindow(int winId, int w, int h);
175 void resizePlugin(int clientWinId, int w, int h);
176 void javascriptResult(int id, const QString &result);
177 void gotFocusIn();
178 void gotFocusOut();
180 // last via NSPluginClass::newInstance() produced NSPluginInstance instance.
181 static NSPluginInstance* lastPluginInstance();
182 static void setLastPluginInstance(NSPluginInstance*);
184 // value handling
185 NPError NPGetValue(NPPVariable variable, void *value);
186 NPError NPSetValue(NPNVariable variable, void *value);
188 // window handling
189 NPError NPSetWindow(NPWindow *window);
191 // stream functions
192 NPError NPDestroyStream(NPStream *stream, NPReason reason);
193 NPError NPNewStream(NPMIMEType type, NPStream *stream, NPBool seekable, uint16 *stype);
194 void NPStreamAsFile(NPStream *stream, const char *fname);
195 int32 NPWrite(NPStream *stream, int32 offset, int32 len, void *buf);
196 int32 NPWriteReady(NPStream *stream);
198 // URL functions
199 void NPURLNotify(const QString &url, NPReason reason, void *notifyData);
201 // Event handling
202 uint16 HandleEvent(void *event);
204 // signal emitters
205 void emitStatus( const QString &message);
206 void requestURL( const QString &url, const QString &mime,
207 const QString &target, void *notify, bool forceNotify = false, bool reload = false );
208 void postURL( const QString &url, const QByteArray& data, const QString &mime,
209 const QString &target, void *notify, const KParts::OpenUrlArguments& args,
210 const KParts::BrowserArguments& browserArgs, bool forceNotify = false );
212 QString normalizedURL(const QString& url) const;
215 public Q_SLOTS:
216 void streamFinished( NSPluginStreamBase *strm );
218 void timer();
220 private:
221 friend class NSPluginStreamBase;
223 void destroy();
225 bool _destroyed;
226 bool _embedded;
227 void addTempFile(KTemporaryFile *tmpFile);
228 QList<KTemporaryFile *> _tempFiles;
229 OrgKdeNspluginsCallBackInterface *_callback;
230 QList<NSPluginStreamBase *> _streams;
231 KLibrary *_handle;
232 QTimer *_timer;
234 NPP _npp;
235 NPPluginFuncs _pluginFuncs;
237 PluginHost* _pluginHost; // Manages embedding of the plugin into us
238 int _width, _height; // last size we used;
240 QString _baseURL;
242 struct Request
244 // A GET request
245 Request( const QString &_url, const QString &_mime,
246 const QString &_target, void *_notify, bool _forceNotify = false,
247 bool _reload = false)
248 { url=_url; mime=_mime; target=_target; notify=_notify; post=false; forceNotify = _forceNotify; reload = _reload; }
250 // A POST request
251 Request( const QString &_url, const QByteArray& _data,
252 const QString &_mime, const QString &_target, void *_notify,
253 const KParts::OpenUrlArguments& _args,
254 const KParts::BrowserArguments& _browserArgs,
255 bool _forceNotify = false)
257 url=_url; mime=_mime; target=_target;
258 notify=_notify; post=true; data=_data; args=_args; browserArgs=_browserArgs;
259 forceNotify = _forceNotify;
262 QString url;
263 QString mime;
264 QString target;
265 QByteArray data;
266 bool post;
267 bool forceNotify;
268 bool reload;
269 void *notify;
270 KParts::OpenUrlArguments args;
271 KParts::BrowserArguments browserArgs;
274 QQueue<Request *> _waitingRequests;
275 QMap<int, Request*> _jsrequests;
277 static NSPluginInstance* s_lastPluginInstance;
281 class NSPluginClass : public QObject
283 Q_OBJECT
284 public:
286 NSPluginClass( const QString &library, QObject *parent );
287 ~NSPluginClass();
289 QString getMIMEDescription();
290 QDBusObjectPath newInstance(const QString &url, const QString &mimeType, bool embed,
291 const QStringList &argn, const QStringList &argv,
292 const QString &appId, const QString &callbackId, bool reload);
293 void destroyInstance( NSPluginInstance* inst );
294 bool error() { return _error; }
296 void setApp(const QByteArray& app) { _app = app; }
297 const QByteArray& app() const { return _app; }
299 protected Q_SLOTS:
300 void timer();
302 private:
303 int initialize();
304 void shutdown();
306 KLibrary *_handle;
307 QString _libname;
308 bool _constructed;
309 bool _error;
310 QTimer *_timer;
312 NP_GetMIMEDescriptionUPP *_NP_GetMIMEDescription;
313 NP_InitializeUPP *_NP_Initialize;
314 NP_ShutdownUPP *_NP_Shutdown;
316 QList<NSPluginInstance *> _instances;
317 QList<NSPluginInstance *> _trash;
319 QByteArray _app;
320 NPPluginFuncs _pluginFuncs;
321 NPNetscapeFuncs _nsFuncs;
323 // If plugins use gtk, we call the gtk_init function for them ---
324 // but only do it once.
325 static bool s_initedGTK;
329 class NSPluginViewer : public QObject
331 Q_OBJECT
332 public:
333 NSPluginViewer( QObject *parent );
334 virtual ~NSPluginViewer();
336 void shutdown();
337 QDBusObjectPath newClass( const QString& plugin, const QString& senderId );
339 private Q_SLOTS:
340 void appChanged( const QString& id, const QString& oldOwner, const QString& newOwner);
342 private:
343 QMap<QString, NSPluginClass *> _classes;
347 #endif