Debian Codegraph @

src/qdjviewplugin.cpp

raw
1 //C- -*- C++ -*-
2 //C- -------------------------------------------------------------------
3 //C- DjView4
4 //C- Copyright (c) 2006- Leon Bottou
5 //C-
6 //C- This software is subject to, and may be distributed under, the
7 //C- GNU General Public License, either version 2 of the license,
8 //C- or (at your option) any later version. The license should have
9 //C- accompanied the software or you may obtain a copy of the license
10 //C- from the Free Software Foundation at http://www.fsf.org .
11 //C-
12 //C- This program is distributed in the hope that it will be useful,
13 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 //C- GNU General Public License for more details.
16 //C- ------------------------------------------------------------------
17
18 #if AUTOCONF
19 # include "config.h"
20 #endif
21
22 #include <QApplication>
23 #include <QByteArray>
24 #include <QDebug>
25 #include <QEvent>
26 #include <QEventLoop>
27 #include <QHBoxLayout>
28 #include <QList>
29 #include <QObject>
30 #include <QPair>
31 #include <QPointer>
32 #include <QRegExp>
33 #include <QSet>
34 #include <QSocketNotifier>
35 #include <QString>
36 #include <QTimer>
37 #include <QTimerEvent>
38 #include <QUrl>
39 #include <QWidget>
40
41 #include "qdjvu.h"
42 #include "qdjview.h"
43 #include "qdjviewplugin.h"
44 #include "djview.h"
45
46 #include <libdjvu/miniexp.h>
47 #include <libdjvu/ddjvuapi.h>
48
49 #include <stddef.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <errno.h>
53 #include <signal.h>
54 #ifdef Q_OS_UNIX
55 # include <unistd.h>
56 # include <fcntl.h>
57 #endif
58 #ifdef Q_OS_WIN32
59 # include <io.h>
60 # include <fcntl.h>
61 #endif
62
63 #if QT_VERSION < 0x50000
64 # if defined(Q_WS_X11)
65 # include <QX11Info>
66 # include <QX11EmbedWidget>
67 # define HAVE_QX11EMBED 1
68 # endif
69 #endif
70 #if QT_VERSION >= 0x50000
71 # include <QWindow>
72 # include <QAbstractNativeEventFilter>
73 # define HAVE_QT5EMBED 1
74 #endif
75 #if QT_VERSION >= 0x60000
76 # define reReplace(s,r,a) (r).replaceIn((s),(a))
77 # define reIndex(s,r,p) (r).indexIn((s),(p))
78 #else
79 # define reReplace(s,r,a) (s).replace((r),(a))
80 # define reIndex(s,r,p) (s).indexOf((r),(p))
81 #endif
82
83
84 #if WITH_X11
85 # ifndef X_DISPLAY_MISSING
86 # if QT_VERSION < 0x50000
87 # ifdef Q_WS_X11
88 # define HAVE_X11 1
89 # endif
90 # endif
91 # endif
92 # if HAVE_X11
93 # include <X11/Xlib.h>
94 # include <X11/Xutil.h>
95 # include <X11/Xatom.h>
96 # undef FocusOut
97 # undef FocusIn
98 # undef KeyPress
99 # undef KeyRelease
100 # undef None
101 # undef RevertToParent
102 # undef GrayScale
103 # undef CursorShape
104 # endif
105 #endif
106
107
108
109 // ========================================
110 // STRUCTURES
111 // ========================================
112
113
114
115 class QDjViewPlugin::Document : public QDjVuDocument
116 {
117 Q_OBJECT
118 public:
119 QDjViewPlugin::Instance * const instance;
120 Document(QDjViewPlugin::Instance *instance);
121 virtual void newstream(int streamid, QString name, QUrl url);
122 };
123
124
125 class QDjViewPlugin::Forwarder : public QObject
126 {
127 Q_OBJECT
128 public:
129 QDjViewPlugin * const dispatcher;
130 Forwarder(QDjViewPlugin *dispatcher);
131 #if HAVE_X11
132 virtual bool eventFilter(QObject*, QEvent*);
133 #endif
134
135 public slots:
136 void containerClosed();
137 void showStatus(QString message);
138 void getUrl(QUrl url, QString target);
139 void onChange();
140 void quit();
141 void dispatch();
142 void lastViewerClosed();
143 void continueExec();
144 };
145
146
147 // QDjViewPlugin, Stream and Instance cannot be QObjects
148 // because subtle errors occur when one creates QObjects
149 // before the QApplication.
150
151
152 struct QDjViewPlugin::Stream
153 {
154 typedef QDjViewPlugin::Instance Instance;
155 QUrl url;
156 Instance *instance;
157 int streamid;
158 bool started;
159 bool checked;
160 bool closed;
161 ~Stream();
162 Stream(int streamid, QUrl url, Instance *instance);
163 };
164
165
166 struct QDjViewPlugin::Instance
167 {
168 QUrl url;
169 QDjViewPlugin *dispatcher;
170 QPointer<QDjViewPlugin::Document> document;
171 QPointer<QWidget> shell;
172 QPointer<QDjView> djview;
173 QPointer<QObject> containerwin;
174 WId containerid;
175 QStringList args;
176 QByteArray saved;
177 int savedformat;
178 int onchange;
179 QDjView::ViewerMode viewerMode;
180 ~Instance();
181 Instance(QDjViewPlugin *dispatcher);
182 void open();
183 void destroy();
184 void save(QDjVuWidget*);
185 void restore(QDjVuWidget*);
186 };
187
188
189
190 // ========================================
191 // WORKAROUND QT55 XEMBED BUG
192 // ========================================
193
194
195 #if QT_VERSION >= 0x50500 /* && QT_VERSION < 0x50XXX */
196 # define WORKAROUND_QT55_XEMBED_BUG 1
197 #endif
198
199 #if WORKAROUND_QT55_XEMBED_BUG
200
201 struct my_xcb_configure_notify_event_t
202 {
203 quint8 response_type, pad0, pad1, pad2;
204 quint32 pad3, window, pad4;
205 quint16 x, y, width, height;
206 };
207
208 class my_timer_object_t : public QObject
209 {
210 Q_OBJECT
211 quint32 wid; quint16 w,h; int tid; bool tidp;
212 public:
213 my_timer_object_t(quint32 wid)
214 : wid(wid), tid(0), tidp(false) {}
215 void start(quint16 ww, quint16 hh) {
216 if (tidp) killTimer(tid);
217 tidp = true; w = ww; h = hh; startTimer(100); }
218 void timerEvent(QTimerEvent*);
219 };
220
221 struct my_event_filter_t : public QAbstractNativeEventFilter
222 {
223 #if QT_VERSION >= 0x60000
224 virtual bool nativeEventFilter(const QByteArray &type, void *msg, qintptr*);
225 #else
226 virtual bool nativeEventFilter(const QByteArray &type, void *msg, long*);
227 #endif
228 QMap<quint32,QDjViewPlugin::Instance*> instances;
229 QMap<quint32,my_timer_object_t*> timers;
230 static my_event_filter_t *instance() {
231 static my_event_filter_t *filter = 0;
232 return filter ? filter : filter = new my_event_filter_t; }
233 };
234
235 bool my_event_filter_t::nativeEventFilter(const QByteArray &type, void *msg,
236 #if QT_VERSION >= 0x60000
237 qintptr*
238 #else
239 long*
240 #endif
241 )
242 {
243 if (type != "xcb_generic_event_t") return false;
244 my_xcb_configure_notify_event_t *ev = (my_xcb_configure_notify_event_t*)msg;
245 if (! (ev->response_type == 22 && instances.contains(ev->window))) return false;
246 if (! timers.contains(ev->window)) timers.insert(ev->window, new my_timer_object_t(ev->window));
247 timers.value(ev->window)->start(ev->width, ev->height);
248 return false;
249 }
250
251 void my_timer_object_t::timerEvent(QTimerEvent*)
252 {
253 my_event_filter_t *f = my_event_filter_t::instance();
254 QDjViewPlugin::Instance *instance = f->instances.value(wid,0);
255 if (instance && instance->shell) // test for resizing bug
256 if (instance->shell->width() != w || instance->shell->height() != h )
257 instance->shell->resize((int)w, (int)h);
258 f->timers.remove(wid);
259 killTimer(tid);
260 deleteLater();
261 }
262
263 #endif
264
265
266
267
268 // ========================================
269 // FIXING TRANSIENT WINDOW PROPERTIES (QT4)
270 // ========================================
271
272 #if HAVE_X11
273
274 static Atom wm_state;
275 static Atom wm_client_leader;
276
277 static Window
278 x11ToplevelWindow(Display *dpy, Window start)
279 {
280 Window shell = 0;
281 if (!dpy || !start)
282 return 0;
283 if (! wm_state)
284 wm_state = XInternAtom(dpy, "WM_STATE", True);
285 if (! wm_state)
286 return 0;
287 while (1)
288 {
289 int nprops;
290 Atom *props = XListProperties(dpy, start, &nprops);
291 if (props && nprops)
292 {
293 for(int i=0; i<nprops; i++)
294 if (props[i] == wm_state)
295 shell = start;
296 XFree(props);
297 }
298 Window root, parent;
299 Window *children = 0;
300 unsigned int nchildren;
301 if (! XQueryTree(dpy, start, &root, &parent, &children, &nchildren))
302 break;
303 if (children)
304 XFree(children);
305 if (parent == root)
306 break;
307 start = parent;
308 }
309 return shell;
310 }
311
312 static void
313 x11SetTransientForHint(QWidget *widget)
314 {
315 QWidget *parent = widget->parentWidget();
316 Display *dpy = QX11Info::display();
317 if (dpy && parent && (widget->windowFlags() & Qt::Window))
318 {
319 Window transient = 0;
320 XGetTransientForHint(dpy, widget->winId(), &transient);
321 if (transient)
322 {
323 Window group = 0;
324 XWMHints *wmhints = XGetWMHints(dpy, widget->winId());
325 if (wmhints && (wmhints->flags & WindowGroupHint))
326 group = wmhints->window_group;
327 Window ntransient = x11ToplevelWindow(dpy, parent->winId());
328 Window ngroup = 0;
329 if (ntransient)
330 {
331 XWMHints *morehints = XGetWMHints(dpy, ntransient);
332 if (morehints && (morehints->flags & WindowGroupHint))
333 ngroup = morehints->window_group;
334 if (morehints)
335 XFree(morehints);
336 if (transient == group && ngroup)
337 ntransient = ngroup;
338 XSetTransientForHint(dpy, widget->winId(), ntransient);
339 }
340 if (ngroup)
341 {
342 wmhints->flags |= WindowGroupHint;
343 wmhints->window_group = ngroup;
344 XSetWMHints(dpy, widget->winId(), wmhints);
345 if (! wm_client_leader)
346 wm_client_leader = XInternAtom(dpy, "WM_CLIENT_LEADER", True);
347 if (wm_client_leader)
348 XChangeProperty(dpy, widget->winId(), wm_client_leader,
349 XA_WINDOW, 32, PropModeReplace,
350 (unsigned char*)&ngroup, 1);
351 }
352 if (wmhints)
353 XFree(wmhints);
354 }
355 }
356 }
357
358 #endif
359
360
361
362
363 // ========================================
364 // STREAM
365 // ========================================
366
367
368 QDjViewPlugin::Stream::Stream(int streamid, QUrl url, Instance *instance)
369 : url(url), instance(instance), streamid(streamid),
370 started(false), checked(false), closed(false)
371 {
372 if (instance->dispatcher)
373 instance->dispatcher->streamCreated(this);
374 }
375
376
377 QDjViewPlugin::Stream::~Stream()
378 {
379 if (instance->dispatcher)
380 instance->dispatcher->streamDestroyed(this);
381 }
382
383
384
385 // ========================================
386 // DOCUMENT
387 // ========================================
388
389
390
391 QDjViewPlugin::Document::Document(QDjViewPlugin::Instance *instance)
392 : QDjVuDocument(true), instance(instance)
393 {
394 QUrl docurl = QDjView::removeDjVuCgiArguments(instance->url);
395 setUrl(instance->dispatcher->context, docurl);
396 }
397
398
399 void
400 QDjViewPlugin::Document::newstream(int streamid, QString, QUrl url)
401 {
402 if (streamid > 0)
403 {
404 new QDjViewPlugin::Stream(streamid, url, instance);
405 QString message = tr("Requesting %1.").arg(url.toString());
406 instance->dispatcher->showStatus(instance, message);
407 instance->dispatcher->getUrl(instance, url, QString());
408 }
409 }
410
411
412
413
414 // ========================================
415 // FORWARDER
416 // ========================================
417
418
419
420 QDjViewPlugin::Forwarder::Forwarder(QDjViewPlugin *dispatcher)
421 : QObject(), dispatcher(dispatcher)
422 {
423 // The purpose of the Forwarder is to redirect
424 // certain signals towards the QDjViewPlugin
425 // (which cannot be a QObject.)
426 }
427
428
429 void
430 QDjViewPlugin::Forwarder::containerClosed()
431 {
432 #if HAVE_QT5EMBED
433 QDjView *shell = qobject_cast<QDjView*>(sender());
434 Instance *instance = dispatcher->findInstance(shell);
435 if (instance && dispatcher->xembedFlag)
436 instance->destroy();
437 #elif HAVE_QX11EMBED
438 QX11EmbedWidget *shell = qobject_cast<QX11EmbedWidget*>(sender());
439 Instance *instance = dispatcher->findInstance(shell);
440 if (instance && dispatcher->xembedFlag)
441 instance->destroy();
442 #endif
443 }
444
445
446 void
447 QDjViewPlugin::Forwarder::showStatus(QString message)
448 {
449 QDjView *viewer = qobject_cast<QDjView*>(sender());
450 Instance *instance = dispatcher->findInstance(viewer);
451 if (instance)
452 dispatcher->showStatus(instance, message);
453 }
454
455
456 void
457 QDjViewPlugin::Forwarder::getUrl(QUrl url, QString target)
458 {
459 QDjView *viewer = qobject_cast<QDjView*>(sender());
460 Instance *instance = dispatcher->findInstance(viewer);
461 if (! target.size())
462 target = "_self";
463 if (instance)
464 dispatcher->getUrl(instance, url, target);
465 }
466
467
468 void
469 QDjViewPlugin::Forwarder::onChange()
470 {
471 QDjView *viewer = qobject_cast<QDjView*>(sender());
472 Instance *instance = dispatcher->findInstance(viewer);
473 if (instance)
474 dispatcher->onChange(instance);
475 }
476
477
478 void
479 QDjViewPlugin::Forwarder::quit()
480 {
481 dispatcher->quit();
482 }
483
484
485 void
486 QDjViewPlugin::Forwarder::dispatch()
487 {
488 dispatcher->dispatch();
489 }
490
491
492 void
493 QDjViewPlugin::Forwarder::lastViewerClosed()
494 {
495 dispatcher->lastViewerClosed();
496 }
497
498 void
499 QDjViewPlugin::Forwarder::continueExec()
500 {
501 dispatcher->continueExec();
502 }
503
504 #if HAVE_X11
505 static bool x11EventFilter(void *message, long *)
506 {
507 // We define a low level handler because we need to make
508 // the window active before calling the QX11EmbedWidget event filter.
509 XEvent *event = reinterpret_cast<XEvent*>(message);
510 if (event->type == ButtonPress)
511 {
512 QWidget *w = QWidget::find(event->xbutton.window);
513 if (w && w->window()->objectName() == "djvu_shell")
514 {
515 QApplication *app = (QApplication*)QCoreApplication::instance();
516 app->setActiveWindow(w->window());
517 }
518 }
519 return false;
520 }
521 #endif
522
523 #if HAVE_X11
524 bool
525 QDjViewPlugin::Forwarder::eventFilter(QObject *o, QEvent *e)
526 {
527 if (o->isWidgetType())
528 {
529 QWidget *w = static_cast<QWidget*>(o);
530 switch( e->type() )
531 {
532 // Try to fix transient windows properties.
533 // - this does not work too well...
534 case QEvent::Show:
535 if (w->windowFlags() & Qt::Window)
536 QApplication::postEvent(w, new QEvent(QEvent::User));
537 break;
538 case QEvent::User:
539 if (w->windowFlags() & Qt::Window)
540 x11SetTransientForHint(w);
541 break;
542 default:
543 break;
544 }
545 }
546 // Keep processing
547 return false;
548 }
549 #endif
550
551
552
553
554 // ========================================
555 // INSTANCE
556 // ========================================
557
558
559 QDjViewPlugin::Instance::~Instance()
560 {
561 destroy();
562 delete shell;
563 delete djview;
564 delete containerwin;
565 if (document)
566 document->deref();
567 document = 0;
568 }
569
570
571 QDjViewPlugin::Instance::Instance(QDjViewPlugin *parent)
572 : url(), dispatcher(parent),
573 document(0), shell(0), djview(0), containerid(0),
574 onchange(0)
575 {
576 }
577
578
579 void
580 QDjViewPlugin::Instance::open()
581 {
582 if (!document && url.isValid())
583 {
584 document = new QDjViewPlugin::Document(this);
585 document->ref();
586 }
587 if (document && djview && !djview->getDocument())
588 {
589 djview->open(document, url);
590 restore(djview->getDjVuWidget());
591 shell->show();
592 }
593 }
594
595 void
596 QDjViewPlugin::Instance::destroy()
597 {
598 if (djview)
599 {
600 save(djview->getDjVuWidget());
601 djview->close();
602 djview = 0;
603 }
604 if (shell)
605 {
606 #if HAVE_QT5EMBED
607 QWindow *window = shell->windowHandle();
608 if (window) {
609 window->setVisible(false);
610 window->setParent(0);
611 # if WORKAROUND_QT55_XEMBED_BUG
612 my_event_filter_t::instance()->instances.remove((quint32)shell->winId());
613 # endif
614 }
615 #elif HAVE_X11
616 Display *dpy = QX11Info::display();
617 XUnmapWindow(dpy, shell->winId());
618 XReparentWindow(dpy, shell->winId(), QX11Info::appRootWindow(), 0,0);
619 #endif
620 shell->close();
621 dispatcher->registerForDeletion(shell);
622 containerid = 0;
623 shell = 0;
624 }
625 }
626
627
628 void
629 QDjViewPlugin::Instance::save(QDjVuWidget *widget)
630 {
631 if (widget)
632 {
633 int q[4];
634 // get info
635 int zoom = widget->zoom();
636 int rotation = widget->rotation();
637 bool sideBySide = widget->sideBySide();
638 bool continuous = widget->continuous();
639 QDjVuWidget::DisplayMode mode = widget->displayMode();
640 QDjVuWidget::Position pos = widget->position();
641 // pack info into four bytes for compatibility.
642 q[0] = 0;
643 q[1] = pos.pageNo;
644 q[2] = pos.posView.x();
645 q[3] = pos.posView.y();
646 q[0] |= (rotation & 0x3) << 12;
647 if (zoom >= 0)
648 q[0] = zoom & 0x7ff;
649 else
650 q[0] = 0x1000 - ((- zoom) & 0x7ff);
651 if (sideBySide)
652 q[0] |= 0x4000;
653 if (continuous)
654 q[0] |= 0x8000;
655 q[0] |= (((int)mode) & 0xf) << 16;
656 // make sure first two integers are positive.
657 q[0] |= 0x40000000;
658 q[1] |= 0x40000000;
659 // store
660 saved = QByteArray((const char*)q, sizeof(q));
661 }
662 }
663
664 void
665 QDjViewPlugin::Instance::restore(QDjVuWidget *widget)
666 {
667 int q[4];
668 if (saved.size()==sizeof(q) && widget)
669 {
670 // unpack
671 memcpy((void*)q, (const char*)saved, sizeof(q));
672 int zoom = q[0] & 0xfff;
673 if (zoom & 0x800)
674 zoom -= 0x1000;
675 int rotation = (q[0] & 0x3000) >> 12;
676 bool sideBySide = !! (q[0] & 0x4000);
677 bool continuous = !! (q[0] & 0x8000);
678 QDjVuWidget::DisplayMode mode
679 = (QDjVuWidget::DisplayMode)((q[0] & 0xf0000) >> 16);
680 QDjVuWidget::Position pos;
681 pos.inPage = false;
682 pos.pageNo = (q[1] & 0xfffffff);
683 pos.posView.rx() = q[2];
684 pos.posView.ry() = q[3];
685 // apply
686 widget->setZoom(zoom);
687 widget->setRotation(rotation);
688 widget->setSideBySide(sideBySide);
689 widget->setContinuous(continuous);
690 widget->setDisplayMode(mode);
691 widget->setPosition(pos);
692 }
693 }
694
695
696
697
698
699 // ========================================
700 // PROTOCOL
701 // ========================================
702
703
704 enum {
705 TYPE_INTEGER = 1,
706 TYPE_DOUBLE = 2,
707 TYPE_STRING = 3,
708 TYPE_POINTER = 4,
709 TYPE_ARRAY = 5
710 };
711
712 enum {
713 CMD_SHUTDOWN = 0,
714 CMD_NEW = 1,
715 CMD_DETACH_WINDOW = 2,
716 CMD_ATTACH_WINDOW = 3,
717 CMD_RESIZE = 4,
718 CMD_DESTROY = 5,
719 CMD_PRINT = 6,
720 CMD_NEW_STREAM = 7,
721 CMD_WRITE = 8,
722 CMD_DESTROY_STREAM = 9,
723 CMD_SHOW_STATUS = 10,
724 CMD_GET_URL = 11,
725 CMD_GET_URL_NOTIFY = 12,
726 CMD_URL_NOTIFY = 13,
727 CMD_HANDSHAKE = 14,
728 CMD_SET_DJVUOPT = 15,
729 CMD_GET_DJVUOPT = 16,
730 CMD_ON_CHANGE = 17,
731 };
732
733 #define OK_STRING "OK"
734 #define ERR_STRING "ERR"
735
736
737 void
738 QDjViewPlugin::write(int fd, const char *ptr, int size)
739 {
740 while(size>0)
741 {
742 errno = 0;
743 int res = ::write(fd, ptr, size);
744 if (res<0 && errno==EINTR)
745 continue;
746 if (res<=0)
747 throw res;
748 size -= res;
749 ptr += res;
750 }
751 }
752
753 void
754 QDjViewPlugin::read(int fd, char *buffer, int size)
755 {
756 char *ptr = buffer;
757 while(size>0)
758 {
759 errno = 0;
760 int res = ::read(fd, ptr, size);
761 if (res<0 && errno==EINTR)
762 continue;
763 if (res <= 0)
764 throw res;
765 size -= res;
766 ptr += res;
767 }
768 }
769
770
771 void
772 QDjViewPlugin::writeString(int fd, QByteArray s)
773 {
774 int type = TYPE_STRING;
775 int length = s.size();
776 write(fd, (const char*)&type, sizeof(type));
777 write(fd, (const char*)&length, sizeof(length));
778 write(fd, s.data(), length+1);
779 }
780
781
782 void
783 QDjViewPlugin::writeString(int fd, QString x)
784 {
785 writeString(fd, x.toUtf8());
786 }
787
788
789 void
790 QDjViewPlugin::writeInteger(int fd, int x)
791 {
792 int type = TYPE_INTEGER;
793 write(fd, (const char*)&type, sizeof(type));
794 write(fd, (const char*)&x, sizeof(x));
795 }
796
797
798 void
799 QDjViewPlugin::writeDouble(int fd, double x)
800 {
801 int type = TYPE_DOUBLE;
802 write(fd, (const char*)&type, sizeof(type));
803 write(fd, (const char*)&x, sizeof(x));
804 }
805
806
807 void
808 QDjViewPlugin::writePointer(int fd, const void *x)
809 {
810 int type = TYPE_POINTER;
811 write(fd, (const char*)&type, sizeof(type));
812 write(fd, (const char*)&x, sizeof(x));
813 }
814
815
816 void
817 QDjViewPlugin::writeArray(int fd, QByteArray s)
818 {
819 int type = TYPE_ARRAY;
820 int length = s.size();
821 write(fd, (const char*)&type, sizeof(type));
822 write(fd, (const char*)&length, sizeof(length));
823 write(fd, s.data(), length);
824 }
825
826
827 QString
828 QDjViewPlugin::readString(int fd)
829 {
830 int type, length;
831 read(fd, (char*)&type, sizeof(type));
832 if (type != TYPE_STRING)
833 throw 1;
834 read(fd, (char*)&length, sizeof(length));
835 if (length < 0)
836 throw 2;
837 QByteArray utf(length+1, 0);
838 read(fd, utf.data(), length+1);
839 return QString::fromUtf8(utf);
840 }
841
842
843 QByteArray
844 QDjViewPlugin::readRawString(int fd)
845 {
846 int type, length;
847 read(fd, (char*)&type, sizeof(type));
848 if (type != TYPE_STRING)
849 throw 1;
850 read(fd, (char*)&length, sizeof(length));
851 if (length < 0)
852 throw 2;
853 QByteArray x(length+1, 0);
854 read(fd, x.data(), length+1);
855 x.truncate(length);
856 return x;
857 }
858
859
860 int
861 QDjViewPlugin::readInteger(int fd)
862 {
863 int type;
864 read(fd, (char*)&type, sizeof(type));
865 if (type != TYPE_INTEGER)
866 throw 1;
867 int x;
868 read(fd, (char*)&x, sizeof(x));
869 return x;
870 }
871
872
873 double
874 QDjViewPlugin::readDouble(int fd)
875 {
876 int type;
877 read(fd, (char*)&type, sizeof(type));
878 if (type != TYPE_DOUBLE)
879 throw 1;
880 double x;
881 read(fd, (char*)&x, sizeof(x));
882 return x;
883 }
884
885
886 void*
887 QDjViewPlugin::readPointer(int fd)
888 {
889 int type;
890 read(fd, (char*)&type, sizeof(type));
891 if (type != TYPE_POINTER)
892 throw 1;
893 void *x;
894 read(fd, (char*)&x, sizeof(x));
895 return x;
896 }
897
898
899 QByteArray
900 QDjViewPlugin::readArray(int fd)
901 {
902 int type, length;
903 read(fd, (char*)&type, sizeof(type));
904 if (type != TYPE_ARRAY)
905 throw 1;
906 read(fd, (char*)&length, sizeof(length));
907 if (length < 0)
908 throw 2;
909 QByteArray x(length, 0);
910 read(fd, x.data(), length);
911 return x;
912 }
913
914
915
916 // ========================================
917 // COMMANDS
918 // ========================================
919
920
921
922 void
923 QDjViewPlugin::cmdNew()
924 {
925 // read arguments
926 bool fullPage = !!readInteger(pipeRead);
927 readString(pipeRead); // djvuDir (unused)
928 int argc = readInteger(pipeRead);
929 QStringList args;
930 for (int i=0; i<argc; i++)
931 {
932 QString key = readString(pipeRead);
933 QString val = readString(pipeRead);
934 QString k = key.toLower();
935 if (k == "flags")
936 #if QT_VERSION >= 0x60000
937 args += QRegExp("\\s+").splitString(val, Qt::SkipEmptyParts);
938 #elif QT_VERSION >= 0x50E00
939 args += val.split(QRegExp("\\s+"), Qt::SkipEmptyParts);
940 #else
941 args += val.split(QRegExp("\\s+"), QString::SkipEmptyParts);
942 #endif
943 else
944 args += key + QString("=") + val;
945 }
946
947 // read saved data
948 QByteArray saved;
949 int savedformat = readInteger(pipeRead);
950 if (savedformat == 1)
951 {
952 int q[4];
953 q[0] = readInteger(pipeRead);
954 q[1] = readInteger(pipeRead);
955 q[2] = readInteger(pipeRead);
956 q[3] = readInteger(pipeRead);
957 saved = QByteArray((const char*)&q[0], sizeof(q));
958 }
959 else if (savedformat)
960 saved = readArray(pipeRead);
961
962 // create instance
963 Instance *instance = new Instance(this);
964 if (fullPage)
965 instance->viewerMode = QDjView::FULLPAGE_PLUGIN;
966 else
967 instance->viewerMode = QDjView::EMBEDDED_PLUGIN;
968 instance->saved = saved;
969 instance->savedformat = savedformat;
970 instance->args = args;
971 // success
972 instances.insert(instance);
973 writeString(pipeWrite, QByteArray(OK_STRING));
974 writePointer(pipeWrite, (void*)instance);
975 }
976
977
978 void
979 QDjViewPlugin::cmdAttachWindow()
980 {
981 Instance *instance = (Instance*) readPointer(pipeRead);
982 QByteArray display = readRawString(pipeRead);
983 QString protocol = readString(pipeRead);
984 WId window = (WId)readInteger(pipeRead);
985 readInteger(pipeRead); // colormap
986 readInteger(pipeRead); // visualid
987 int width = readInteger(pipeRead);
988 int height = readInteger(pipeRead);
989 // check
990 if (!instances.contains(instance))
991 {
992 fprintf(stderr, "djview dispatcher: bad instance\n");
993 writeString(pipeWrite, QByteArray(ERR_STRING));
994 return;
995 }
996 // create application object
997 if (! application)
998 {
999 argc = 0;
1000 argv[argc++] = progname;
1001 if (! display.isEmpty())
1002 {
1003 argv[argc++] = "-display";
1004 argv[argc++] = (const char*) display;
1005 }
1006 application = new QDjViewApplication(argc, const_cast<char**>(argv));
1007 application->setQuitOnLastWindowClosed(false);
1008 argc = 1;
1009 context = application->djvuContext();
1010 forwarder = new Forwarder(this);
1011 #if HAVE_X11
1012 application->setEventFilter(x11EventFilter);
1013 application->installEventFilter(forwarder);
1014 #endif
1015 #if WORKAROUND_QT55_XEMBED_BUG
1016 application->installNativeEventFilter(my_event_filter_t::instance());
1017 #endif
1018 QObject::connect(application, SIGNAL(lastWindowClosed()),
1019 forwarder, SLOT(lastViewerClosed()));
1020 notifier = new QSocketNotifier(pipeRead, QSocketNotifier::Read);
1021 QObject::connect(notifier, SIGNAL(activated(int)),
1022 forwarder, SLOT(dispatch()));
1023 timer = new QTimer();
1024 timer->setSingleShot(true);
1025 timer->start(5*60*1000);
1026 QObject::connect(timer, SIGNAL(timeout()),
1027 forwarder, SLOT(quit()));
1028 #if HAVE_QX11EMBED || HAVE_QT5EMBED
1029 if (protocol.startsWith("XEMBED"))
1030 xembedFlag = true;
1031 #endif
1032 }
1033
1034 // create djview object
1035 QWidget *shell = instance->shell;
1036 QDjView *djview = instance->djview;
1037 if (! shell)
1038 {
1039 #if HAVE_QT5EMBED
1040 if (xembedFlag)
1041 {
1042 djview = new QDjView(*context, instance->viewerMode, 0);
1043 djview->winId();
1044 shell = djview;
1045 QObject::connect(shell, SIGNAL(destroyed()),
1046 forwarder, SLOT(containerClosed()) );
1047 shell->setObjectName("djvu_shell");
1048 QWindow *dwindow = djview->windowHandle();
1049 QWindow *cwindow = QWindow::fromWinId(window);
1050 instance->containerwin = cwindow;
1051 dwindow->setParent(cwindow);
1052 # if WORKAROUND_QT55_XEMBED_BUG
1053 my_event_filter_t::instance()->instances.insert((quint32)shell->winId(), instance);
1054 # endif
1055 }
1056 else
1057 #elif HAVE_QX11EMBED
1058 if (xembedFlag)
1059 {
1060 QX11EmbedWidget *embed = new QX11EmbedWidget();
1061 shell = embed;
1062 QObject::connect(shell, SIGNAL(containerClosed()),
1063 forwarder, SLOT(containerClosed()) );
1064 shell->setObjectName("djvu_shell");
1065 shell->setGeometry(0, 0, width, height);
1066 djview = new QDjView(*context, instance->viewerMode, shell);
1067 djview->setWindowFlags(djview->windowFlags() & ~Qt::Window);
1068 djview->setAttribute(Qt::WA_DeleteOnClose, false);
1069 instance->containerid = window;
1070 embed->embedInto(window);
1071 }
1072 else
1073 #endif
1074 {
1075 shell = new QWidget();
1076 shell->setObjectName("djvu_shell");
1077 shell->setGeometry(0, 0, width, height);
1078 djview = new QDjView(*context, instance->viewerMode, shell);
1079 djview->setWindowFlags(djview->windowFlags() & ~Qt::Window);
1080 djview->setAttribute(Qt::WA_DeleteOnClose, false);
1081 #if HAVE_X11
1082 instance->containerid = window;
1083 Display *dpy = QX11Info::display();
1084 XReparentWindow(dpy, shell->winId(), (Window)window, 0,0);
1085 #elif HAVE_QT5EMBED
1086 shell->winId();
1087 QWindow *dwindow = shell->windowHandle();
1088 QWindow *cwindow = QWindow::fromWinId(window);
1089 instance->containerwin = cwindow;
1090 dwindow->setParent(cwindow);
1091 #else
1092 qWarning("djview: unable to embed the djview window.");
1093 #endif
1094 }
1095 if (shell && djview && shell != djview)
1096 {
1097 QLayout *layout = new QHBoxLayout(shell);
1098 #if QT_VERSION >= 0x60000
1099 layout->setContentsMargins(QMargins(0,0,0,0));
1100 #else
1101 layout->setMargin(0);
1102 #endif
1103 layout->setSpacing(0);
1104 layout->addWidget(djview);
1105 }
1106 QObject::connect(djview, SIGNAL(pluginStatusMessage(QString)),
1107 forwarder, SLOT(showStatus(QString)) );
1108 QObject::connect(djview, SIGNAL(pluginGetUrl(QUrl,QString)),
1109 forwarder, SLOT(getUrl(QUrl,QString)) );
1110 QObject::connect(djview, SIGNAL(pluginOnChange()),
1111 forwarder, SLOT(onChange()) );
1112 instance->shell = shell;
1113 instance->djview = djview;
1114 // apply arguments
1115 while (instance->args.size() > 0)
1116 djview->parseArgument(instance->args.takeFirst());
1117 instance->args.clear();
1118 }
1119 // map and reparent djview object
1120 instance->open();
1121 writeString(pipeWrite, QByteArray(OK_STRING));
1122 timer->stop();
1123 }
1124
1125
1126 void
1127 QDjViewPlugin::cmdDetachWindow()
1128 {
1129 Instance *instance = (Instance*) readPointer(pipeRead);
1130 if (instances.contains(instance))
1131 instance->destroy();
1132 writeString(pipeWrite, QByteArray(OK_STRING));
1133 }
1134
1135
1136 void
1137 QDjViewPlugin::cmdResize()
1138 {
1139 Instance *instance = (Instance*) readPointer(pipeRead);
1140 int width = readInteger(pipeRead);
1141 int height = readInteger(pipeRead);
1142 if (instances.contains(instance) && width>0 && height>0)
1143 if (instance->shell && application)
1144 instance->shell->resize(width, height);
1145 writeString(pipeWrite, QByteArray(OK_STRING));
1146 }
1147
1148
1149 void
1150 QDjViewPlugin::cmdDestroy()
1151 {
1152 int savedformat = 0;
1153 QByteArray saved;
1154 Instance *instance = (Instance*) readPointer(pipeRead);
1155 if (instances.contains(instance))
1156 {
1157 instance->destroy();
1158 QList<Stream*> streamList = streams.values();
1159 foreach(Stream *stream, streamList)
1160 if (stream->instance == instance)
1161 delete(stream);
1162 savedformat = instance->savedformat;
1163 saved = instance->saved;
1164 delete instance;
1165 instances.remove(instance);
1166 }
1167 writeString(pipeWrite, QByteArray(OK_STRING));
1168 if (savedformat == 1 || savedformat == 0)
1169 {
1170 int q[4];
1171 q[0] = q[1] = q[2] = q[3] = 0;
1172 if (saved.size() == sizeof(q))
1173 memcpy((void*)q, (const char*)saved, sizeof(q));
1174 writeInteger(pipeWrite, q[0]);
1175 writeInteger(pipeWrite, q[1]);
1176 writeInteger(pipeWrite, q[2]);
1177 writeInteger(pipeWrite, q[3]);
1178 }
1179 else
1180 writeArray(pipeWrite, saved);
1181 }
1182
1183
1184 void
1185 QDjViewPlugin::cmdNewStream()
1186 {
1187 // read
1188 Instance *instance = (Instance*) readPointer(pipeRead);
1189 QUrl url = QUrl::fromEncoded(readRawString(pipeRead));
1190 // check
1191 if (! url.isValid() )
1192 {
1193 fprintf(stderr,"djview dispatcher: invalid url '%s'\n",
1194 (const char*) url.toEncoded());
1195 writeString(pipeWrite, QByteArray(ERR_STRING));
1196 return;
1197 }
1198 // proceed
1199 Stream *stream = 0;
1200 if (instances.contains(instance))
1201 {
1202 // first stream
1203 if (! instance->url.isValid())
1204 {
1205 instance->url = url;
1206 url = QDjView::removeDjVuCgiArguments(url);
1207 Stream *s = new QDjViewPlugin::Stream(0, url, instance);
1208 s->checked = true;
1209 instance->open();
1210 }
1211 // search stream
1212 foreach(Stream *s, streams)
1213 if (!stream && !s->started)
1214 if (s->instance == instance && s->url == url)
1215 stream = s;
1216 }
1217 // mark
1218 if (stream)
1219 stream->started = true;
1220 // reply (null means discard stream)
1221 writeString(pipeWrite, QByteArray(OK_STRING));
1222 writePointer(pipeWrite, (void*)stream);
1223 }
1224
1225
1226 void
1227 QDjViewPlugin::cmdWrite()
1228 {
1229 Stream *stream = (Stream*) readPointer(pipeRead);
1230 QByteArray data = readArray(pipeRead);
1231 // locate stream
1232 if (!streams.contains(stream) || stream->closed)
1233 {
1234 // -- null return value means that stream must be discarded.
1235 writeString(pipeWrite, QByteArray(OK_STRING));
1236 writeInteger(pipeWrite, 0);
1237 return;
1238 }
1239 // check stream
1240 if (!stream->checked && data.size()>0)
1241 {
1242 stream->checked = true;
1243 // -- if the stream is not a djvu file,
1244 // this is probably a html error message.
1245 // But we need at least 8 bytes to check
1246 if (data.size() >= 8)
1247 {
1248 const char *cdata = (const char*)data;
1249 if (strncmp(cdata, "FORM", 4) && strncmp(cdata+4, "FORM", 4))
1250 {
1251 getUrl(stream->instance, stream->url, QString("_self"));
1252 delete stream;
1253 writeString(pipeWrite, QByteArray(OK_STRING));
1254 writeInteger(pipeWrite, 0);
1255 return;
1256 }
1257 }
1258 }
1259 // pass data
1260 Document *document = stream->instance->document;
1261 ddjvu_stream_write(*document, stream->streamid, data, data.size());
1262 writeString(pipeWrite, QByteArray(OK_STRING));
1263 writeInteger(pipeWrite, data.size());
1264 }
1265
1266
1267 void
1268 QDjViewPlugin::cmdDestroyStream()
1269 {
1270 Stream *stream = (Stream*) readPointer(pipeRead);
1271 int okay = readInteger(pipeRead);
1272 if (streams.contains(stream))
1273 {
1274 Document *document = stream->instance->document;
1275 if (document && !stream->closed)
1276 ddjvu_stream_close(*document, stream->streamid, !okay);
1277 stream->closed = true;
1278 showStatus(stream->instance, QString());
1279 delete stream;
1280 }
1281 writeString(pipeWrite, QByteArray(OK_STRING));
1282 }
1283
1284
1285 void
1286 QDjViewPlugin::cmdUrlNotify()
1287 {
1288 QUrl url = QUrl::fromEncoded(readRawString(pipeRead));
1289 readInteger(pipeRead); // notification code (unused)
1290 Stream *stream = 0;
1291 QList<Stream*> streamList = streams.values();
1292 foreach(stream, streamList)
1293 if (!stream->started && stream->url == url)
1294 delete stream;
1295 writeString(pipeWrite, QByteArray(OK_STRING));
1296 }
1297
1298
1299 void
1300 QDjViewPlugin::cmdPrint()
1301 {
1302 Instance *instance = (Instance*) readPointer(pipeRead);
1303 readInteger(pipeRead); // full mode (unused)
1304 if (instances.contains(instance))
1305 if (instance->djview)
1306 QTimer::singleShot(0, instance->djview, SLOT(print()));
1307 writeString(pipeWrite, QByteArray(OK_STRING));
1308 }
1309
1310
1311 void
1312 QDjViewPlugin::cmdHandshake()
1313 {
1314 writeString(pipeWrite, QByteArray(OK_STRING));
1315 }
1316
1317
1318 void
1319 QDjViewPlugin::cmdSetDjVuOpt()
1320 {
1321 // protocol extension for plugin scripting
1322 Instance *instance = (Instance*) readPointer(pipeRead);
1323 QString key = readString(pipeRead);
1324 QString value = readString(pipeRead);
1325 // check instance
1326 if (!instances.contains(instance))
1327 {
1328 fprintf(stderr, "djview dispatcher: bad instance\n");
1329 writeString(pipeWrite, QByteArray(ERR_STRING));
1330 return;
1331 }
1332 // return code
1333 writeString(pipeWrite, QByteArray(OK_STRING));
1334 // apply options
1335 QStringList errors;
1336 if (instance->djview)
1337 errors = instance->djview->parseArgument(key, value);
1338 else
1339 instance->args += key + QString("=") + value;
1340 // print error messages
1341 if (errors.size() > 0)
1342 {
1343 foreach(QString error, errors)
1344 qWarning("djvuopt: %s",(const char*)error.toLocal8Bit());
1345 }
1346 }
1347
1348
1349 void
1350 QDjViewPlugin::cmdGetDjVuOpt()
1351 {
1352 // protocol extension for plugin scripting.
1353 Instance *instance = (Instance*) readPointer(pipeRead);
1354 QString key = readString(pipeRead);
1355 // check instance.
1356 if (!instances.contains(instance))
1357 {
1358 fprintf(stderr, "djview dispatcher: bad instance\n");
1359 writeString(pipeWrite, QByteArray(ERR_STRING));
1360 return;
1361 }
1362 // get argument.
1363 QString value;
1364 if (instance->djview)
1365 value = instance->djview->getArgument(key);
1366 writeString(pipeWrite, QByteArray(OK_STRING));
1367 writeString(pipeWrite, value);
1368 }
1369
1370
1371 void
1372 QDjViewPlugin::cmdOnChange()
1373 {
1374 Instance *instance = (Instance*) readPointer(pipeRead);
1375 instance->onchange = readInteger(pipeRead);
1376 writeString(pipeWrite, QByteArray(OK_STRING));
1377 }
1378
1379
1380 void
1381 QDjViewPlugin::cmdShutdown()
1382 {
1383 QList<Stream*> streamList = streams.values();
1384 QList<Instance*> instanceList = instances.values();
1385 foreach(Instance *s, instanceList)
1386 s->destroy();
1387 foreach(Stream *s, streamList)
1388 delete(s);
1389 foreach(Instance *s, instanceList)
1390 delete(s);
1391 streams.clear();
1392 instances.clear();
1393 lastViewerClosed();
1394 }
1395
1396
1397
1398
1399
1400 // ========================================
1401 // QDJVIEWPLUGIN
1402 // ========================================
1403
1404
1405
1406 static QDjViewPlugin *thePlugin;
1407
1408
1409 QDjViewPlugin::~QDjViewPlugin()
1410 {
1411 thePlugin = 0;
1412 QList<Stream*> streamList = streams.values();
1413 QList<Instance*> instanceList = instances.values();
1414 foreach(Stream *s, streamList)
1415 delete(s);
1416 foreach(Instance *s, instanceList)
1417 delete(s);
1418 delete forwarder;
1419 delete notifier;
1420 delete timer;
1421 delete application;
1422 }
1423
1424
1425 QDjViewPlugin::QDjViewPlugin(const char *progname)
1426 : progname(progname),
1427 context(0),
1428 timer(0),
1429 notifier(0),
1430 application(0),
1431 forwarder(0),
1432 xembedFlag(false),
1433 eventLoop(0),
1434 returnCode(0),
1435 quitFlag(false),
1436 pipeRead(3),
1437 pipeWrite(4),
1438 pipeRequest(5)
1439 {
1440 // Disable SIGPIPE
1441 #ifdef SIGPIPE
1442 # ifndef HAVE_SIGACTION
1443 # ifdef SA_RESTART
1444 # define HAVE_SIGACTION 1
1445 # endif
1446 # endif
1447 # if HAVE_SIGACTION
1448 sigset_t mask;
1449 struct sigaction act;
1450 sigemptyset(&mask);
1451 sigaddset(&mask, SIGPIPE);
1452 sigprocmask(SIG_BLOCK, &mask, 0);
1453 sigaction(SIGPIPE, 0, &act);
1454 act.sa_handler = SIG_IGN;
1455 sigaction(SIGPIPE, &act, 0);
1456 # else
1457 signal(SIGPIPE, SIG_IGN);
1458 # endif
1459 #endif
1460 // Set global dispatcher
1461 if (thePlugin)
1462 qWarning("Constructing multiple dispatchers");
1463 thePlugin = this;
1464 }
1465
1466
1467 QDjViewPlugin *
1468 QDjViewPlugin::instance()
1469 {
1470 return thePlugin;
1471 }
1472
1473
1474 int
1475 QDjViewPlugin::exec()
1476 {
1477 #ifdef Q_OS_UNIX
1478 #ifndef QT_NO_DEBUG
1479 const char *s = ::getenv("DJVIEW_DEBUG");
1480 if (s && strcmp(s,"0"))
1481 {
1482 static int loop = 1;
1483 qWarning("QDjViewPlugin::exec() looping for gdb");
1484 while (loop)
1485 sleep(1);
1486 }
1487 # endif
1488 #endif
1489 returnCode = 0;
1490 quitFlag = false;
1491 qWarning("QDjViewPlugin::exec() begin");
1492 try
1493 {
1494 // startup message announcing capabilities
1495 #if HAVE_QX11EMBED || HAVE_QT5EMBED
1496 const char *djviewName = "DJVIEW/4 SCRIPT XEMBED";
1497 #else
1498 const char *djviewName = "DJVIEW/4 SCRIPT";
1499 #endif
1500 writeString(pipeWrite, QByteArray(djviewName));
1501 // dispatch until we get display
1502 while (!application && !quitFlag)
1503 dispatch();
1504 // start application
1505 if (application && forwarder)
1506 {
1507 QTimer::singleShot(0, forwarder, SLOT(continueExec()));
1508 application->exec();
1509 }
1510 }
1511 catch(int err)
1512 {
1513 reportError(err);
1514 returnCode = 10;
1515 }
1516 qWarning("QDjViewPlugin::exec() end code=%d", returnCode);
1517 return returnCode;
1518 }
1519
1520
1521 void
1522 QDjViewPlugin::continueExec()
1523 {
1524 try
1525 {
1526 // handle events in private loop
1527 while (!quitFlag)
1528 {
1529 QEventLoop loop;
1530 eventLoop = &loop;
1531 eventLoop->exec();
1532 eventLoop = 0;
1533 // see registerForDeletion()
1534 QObjectList toDelete = pendingDelete;
1535 pendingDelete.clear();
1536 foreach(QObject *o, toDelete)
1537 delete o;
1538 }
1539 }
1540 catch(int err)
1541 {
1542 reportError(err);
1543 returnCode = 10;
1544 }
1545 if (application)
1546 application->exit(returnCode);
1547 }
1548
1549
1550 void
1551 QDjViewPlugin::exit(int retcode)
1552 {
1553 returnCode = retcode;
1554 quitFlag = true;
1555 if (timer)
1556 timer->stop();
1557 if (eventLoop)
1558 eventLoop->exit(retcode);
1559 else if (application)
1560 application->exit(retcode);
1561 }
1562
1563
1564 void
1565 QDjViewPlugin::quit()
1566 {
1567 this->exit(0);
1568 }
1569
1570
1571 void
1572 QDjViewPlugin::reportError(int err)
1573 {
1574 if (err < 0)
1575 perror("djview dispatcher");
1576 else if (err > 0)
1577 fprintf(stderr, "djview dispatcher: protocol error (%d)\n", err);
1578 }
1579
1580
1581 void
1582 QDjViewPlugin::streamCreated(Stream *stream)
1583 {
1584 streams.insert(stream);
1585 }
1586
1587
1588 void
1589 QDjViewPlugin::streamDestroyed(Stream *stream)
1590 {
1591 if (streams.contains(stream))
1592 {
1593 if (instances.contains(stream->instance))
1594 {
1595 Document *document = stream->instance->document;
1596 if (document && !stream->closed)
1597 ddjvu_stream_close(*document, stream->streamid, true);
1598 }
1599 stream->closed = true;
1600 stream->started = stream->checked = false;
1601 streams.remove(stream);
1602 }
1603 }
1604
1605
1606 QDjViewPlugin::Instance*
1607 QDjViewPlugin::findInstance(QWidget *widget)
1608 {
1609 if (widget)
1610 {
1611 foreach(Instance *instance, instances)
1612 if (widget == instance->djview || widget == instance->shell)
1613 return instance;
1614 }
1615 return 0;
1616 }
1617
1618
1619 void
1620 QDjViewPlugin::getUrl(Instance *instance, QUrl url, QString target)
1621 {
1622 try
1623 {
1624 writeInteger(pipeRequest, CMD_GET_URL);
1625 writePointer(pipeRequest, (void*) instance);
1626 writeString(pipeRequest, url.toEncoded());
1627 writeString(pipeRequest, target);
1628 }
1629 catch(int err)
1630 {
1631 reportError(err);
1632 exit(10);
1633 }
1634 }
1635
1636
1637 void
1638 QDjViewPlugin::onChange(Instance *instance)
1639 {
1640 try
1641 {
1642 if (!instance->onchange)
1643 return;
1644 writeInteger(pipeRequest, CMD_ON_CHANGE);
1645 writePointer(pipeRequest, (void*) instance);
1646 }
1647 catch(int err)
1648 {
1649 reportError(err);
1650 exit(10);
1651 }
1652 }
1653
1654
1655 void
1656 QDjViewPlugin::showStatus(Instance *instance, QString message)
1657 {
1658 try
1659 {
1660 message = reReplace(message, QRegExp("\\s"), " ");
1661 writeInteger(pipeRequest, CMD_SHOW_STATUS);
1662 writePointer(pipeRequest, (void*) instance);
1663 writeString(pipeRequest, message);
1664 }
1665 catch(int err)
1666 {
1667 reportError(err);
1668 exit(10);
1669 }
1670 }
1671
1672
1673 void
1674 QDjViewPlugin::dispatch()
1675 {
1676 try
1677 {
1678 int cmd = readInteger(pipeRead);
1679 switch (cmd)
1680 {
1681 case CMD_SHUTDOWN: cmdShutdown(); break;
1682 case CMD_NEW: cmdNew(); break;
1683 case CMD_DETACH_WINDOW: cmdDetachWindow(); break;
1684 case CMD_ATTACH_WINDOW: cmdAttachWindow(); break;
1685 case CMD_RESIZE: cmdResize(); break;
1686 case CMD_DESTROY: cmdDestroy(); break;
1687 case CMD_PRINT: cmdPrint(); break;
1688 case CMD_NEW_STREAM: cmdNewStream(); break;
1689 case CMD_WRITE: cmdWrite(); break;
1690 case CMD_DESTROY_STREAM: cmdDestroyStream(); break;
1691 case CMD_URL_NOTIFY: cmdUrlNotify(); break;
1692 case CMD_HANDSHAKE: cmdHandshake(); break;
1693 case CMD_SET_DJVUOPT: cmdSetDjVuOpt(); break;
1694 case CMD_GET_DJVUOPT: cmdGetDjVuOpt(); break;
1695 case CMD_ON_CHANGE: cmdOnChange(); break;
1696 default: throw 3;
1697 }
1698 }
1699 catch(int err)
1700 {
1701 reportError(err);
1702 exit(err ? 10 : 0);
1703 }
1704 }
1705
1706
1707 void
1708 QDjViewPlugin::lastViewerClosed()
1709 {
1710 if (timer)
1711 {
1712 timer->stop();
1713 timer->start(5*60*1000);
1714 return;
1715 }
1716 exit(0);
1717 }
1718
1719
1720 void
1721 QDjViewPlugin::registerForDeletion(QObject *p)
1722 {
1723 // QObject::deleteLater() deletes when
1724 // returning to the current event loop.
1725 // Here we want deletions to happen when
1726 // returning to the main loop.
1727 // Otherwise Instance::destroy() might
1728 // delete window modal dialogs while
1729 // they are running.
1730 if (application)
1731 {
1732 pendingDelete.append(p);
1733 if (eventLoop)
1734 eventLoop->exit(0);
1735 return;
1736 }
1737 // Fallback
1738 p->deleteLater();
1739 }
1740
1741
1742
1743
1744
1745 // ----------------------------------------
1746 // MOC
1747
1748 #include "qdjviewplugin.moc"
1749
1750
1751 // ----------------------------------------
1752 // END
1753
1754
1755
1756 /* -------------------------------------------------------------
1757 Local Variables:
1758 c++-font-lock-extra-types: ( "\\sw+_t" "[A-Z]\\sw*[a-z]\\sw*" )
1759 End:
1760 ------------------------------------------------------------- */