Debian Codegraph @

src/qdjvu.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 <stddef.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <libdjvu/miniexp.h>
26 #include <libdjvu/ddjvuapi.h>
27
28 #include "qdjvu.h"
29
30 #include <QByteArray>
31 #include <QCoreApplication>
32 #include <QDebug>
33 #include <QFile>
34 #include <QFileInfo>
35 #include <QList>
36 #include <QMutex>
37 #include <QMutexLocker>
38 #include <QPair>
39 #include <QSet>
40 #include <QString>
41 #include <QTimer>
42 #include <QUrl>
43 #include <QVector>
44 #include <QMutex>
45 #if QT_VERSION >= 0x50000
46 # include <QUrlQuery>
47 #endif
48 #if QT_VERSION >= 0x50D00
49 # include <QRecursiveMutex>
50 #else
51 class QRecursiveMutex: public QMutex {
52 public:
53 QRecursiveMutex() : QMutex(QMutex::Recursive) { }
54 };
55 #endif
56 #if DDJVUAPI_VERSION < 17
57 # error "DDJVUAPI_VERSION>=17 is required !"
58 #endif
59
60
61 // ----------------------------------------
62 // QDJVUCONTEXT
63
64
65 /*! \class QDjVuContext
66 \brief Represents a \a ddjvu_context_t object.
67
68 This QObject subclass holds a \a ddjvu_context_t object and
69 transparently hooks the DDJVUAPI message queue into
70 the Qt messaging system. DDJVUAPI messages are then
71 forwarded to the virtual function \a QDjVuContext::handle
72 which transforms then into signals emitted by
73 the proper object from the message loop thread.
74 */
75
76 /*! Construct a \a QDJVuContext object.
77 Argument \a programname is the name of the program.
78 This name is used to report error messages and locate
79 localized messages. Argument \a parent defines its
80 parent in the \a QObject hierarchy. */
81
82 QDjVuContext::QDjVuContext(const char *programname, QObject *parent)
83 : QObject(parent), context(0), flag(false)
84 {
85 context = ddjvu_context_create(programname);
86 ddjvu_message_set_callback(context, callback, (void*)this);
87 ddjvu_cache_set_size(context, 30*1024*1024);
88 }
89
90 QDjVuContext::~QDjVuContext()
91 {
92 ddjvu_context_release(context);
93 context = 0;
94 }
95
96 /*! \property QDjVuContext::cacheSize
97 \brief The size of the decoded page cache in bytes.
98 The default cache size is 30 megabytes. */
99
100 long
101 QDjVuContext::cacheSize() const
102 {
103 return ddjvu_cache_get_size(context);
104 }
105
106 void
107 QDjVuContext::setCacheSize(long size)
108 {
109 ddjvu_cache_set_size(context, size);
110 }
111
112 void
113 QDjVuContext::callback(ddjvu_context_t *, void *closure)
114 {
115 QDjVuContext *qcontext = (QDjVuContext*)closure;
116 if (! qcontext->flag)
117 {
118 qcontext->flag = true;
119 QEvent *qevent = new QEvent(QEvent::User);
120 QCoreApplication::postEvent(qcontext, qevent);
121 }
122 }
123
124
125 bool
126 QDjVuContext::event(QEvent *event)
127 {
128 if (event->type() == QEvent::User)
129 {
130 flag = false;
131 ddjvu_message_t *message;
132 while ((message = ddjvu_message_peek(context)))
133 {
134 handle(message);
135 ddjvu_message_pop(context);
136 }
137 return true;
138 }
139 return QObject::event(event);
140 }
141
142
143 /*! Processes DDJVUAPI messages.
144 The Qt message loop automatically passes all DDJVUAPI messages
145 to this virtual function . This function then forwards them
146 to the \a handle function of the suitable \a QDjVuDocument,
147 \a QDjVuPage or \a QDjVuJob object. Most messages eventually
148 result into the emission of a suitable signal. */
149
150 bool
151 QDjVuContext::handle(ddjvu_message_t *msg)
152 {
153 if (msg->m_any.page)
154 {
155 QObject *p = (QObject*)ddjvu_page_get_user_data(msg->m_any.page);
156 QDjVuPage *q = (p) ? qobject_cast<QDjVuPage*>(p) : 0;
157 if (q && q->handle(msg))
158 return true;
159 }
160 if (msg->m_any.job)
161 {
162 ddjvu_job_t *djob = 0;
163 ddjvu_job_t *pjob = 0;
164 if (msg->m_any.document)
165 djob = ddjvu_document_job(msg->m_any.document);
166 if (msg->m_any.page)
167 pjob = ddjvu_page_job(msg->m_any.page);
168 if (msg->m_any.job != djob && msg->m_any.job != pjob)
169 {
170 QObject *p = (QObject*)ddjvu_job_get_user_data(msg->m_any.job);
171 QDjVuJob *q = (p) ? qobject_cast<QDjVuJob*>(p) : 0;
172 if (q && q->handle(msg))
173 return true;
174 }
175 }
176 if (msg->m_any.document)
177 {
178 QObject *p = (QObject*)ddjvu_document_get_user_data(msg->m_any.document);
179 QDjVuDocument *q = (p) ? qobject_cast<QDjVuDocument*>(p) : 0;
180 if (q && q->handle(msg))
181 return true;
182 }
183 switch(msg->m_any.tag)
184 {
185 case DDJVU_ERROR:
186 emit error(QString::fromLocal8Bit(msg->m_error.message),
187 QString::fromLocal8Bit(msg->m_error.filename),
188 msg->m_error.lineno);
189 return true;
190 case DDJVU_INFO:
191 emit info(QString::fromLocal8Bit(msg->m_info.message));
192 return true;
193 default:
194 break;
195 }
196 return false;
197 }
198
199 /*! \fn QDjVuContext::isValid()
200 Indicate if this object is associated with a
201 \a ddjvu_context_t object. Use this function
202 to determine if the construction was successful. */
203
204 /*! \fn QDjVuContext::operator ddjvu_context_t*()
205 Return a pointer to the corresponding
206 \a ddjvu_context_t object. */
207
208 /*! \fn QDjVuContext::error(QString msg, QString filename, int lineno)
209
210 This signal is emitted when processing a DDJVUAPI error message
211 that is not attached to any document, page, or job.
212 This only hapens when a DDJVUAPI function is used incorrectly
213 or when calling \a QDjVuDocument::setFileName or
214 \a QDjVuDocument::setUrl.
215 */
216
217 /*! \fn QDjVuContext::info(QString msg)
218
219 This signal is emitted when processing a DDJVUAPI info message
220 that is not attached to any document, page, or job.
221 This is quite rare and not very useful.
222 */
223
224
225 // ----------------------------------------
226 // QDJVUDOCUMENTPRIVATE
227
228
229 class QDjVuDocumentPrivate : public QObject
230 {
231 Q_OBJECT
232 public:
233 QRecursiveMutex mutex;
234 bool autoDelete;
235 int refCount;
236 QSet<QObject*> running;
237 bool docReady;
238 QDjVuDocument *docPointer;
239 minivar_t documentOutline;
240 minivar_t documentAnnotations;
241 QVector<minivar_t> pageAnnotations;
242 QVector<minivar_t> pageText;
243
244 QDjVuDocumentPrivate();
245 void add(QObject *p);
246 void add(QDjVuPage *p);
247 public slots:
248 void docinfo();
249 protected slots:
250 void remove(QObject *p);
251 void pageinfo();
252 void emitidle();
253 signals:
254 void idle();
255 };
256
257 QDjVuDocumentPrivate::QDjVuDocumentPrivate()
258 : mutex(),
259 autoDelete(false),
260 refCount(0),
261 docReady(false),
262 docPointer(0)
263 {
264 }
265
266 void
267 QDjVuDocumentPrivate::add(QObject *p)
268 {
269 connect(p, SIGNAL(destroyed(QObject*)), this, SLOT(remove(QObject*)));
270 mutex.lock();
271 running.insert(p);
272 mutex.unlock();
273 }
274
275 void
276 QDjVuDocumentPrivate::emitidle()
277 {
278 emit idle();
279 }
280
281 void
282 QDjVuDocumentPrivate::remove(QObject *p)
283 {
284 mutex.lock();
285 running.remove(p);
286 int size = running.size();
287 disconnect(p, 0, this, 0);
288 mutex.unlock();
289 if (! size)
290 QTimer::singleShot(0, this, SLOT(emitidle()));
291 }
292
293 void
294 QDjVuDocumentPrivate::add(QDjVuPage *p)
295 {
296 if (ddjvu_page_decoding_done(*p)) return;
297 connect(p, SIGNAL(pageinfo()), this, SLOT(pageinfo()));
298 add((QObject*)(p));
299 }
300
301 void
302 QDjVuDocumentPrivate::pageinfo()
303 {
304 QDjVuPage *p = qobject_cast<QDjVuPage*>(sender());
305 if (p && ddjvu_page_decoding_done(*p))
306 remove(p);
307 }
308
309
310 void
311 QDjVuDocumentPrivate::docinfo()
312 {
313 if (!docReady && ddjvu_document_decoding_done(*docPointer))
314 {
315 QMutexLocker locker(&mutex);
316 int pageNum = ddjvu_document_get_pagenum(*docPointer);
317 documentOutline = miniexp_dummy;
318 documentAnnotations = miniexp_dummy;
319 pageAnnotations.resize(pageNum);
320 pageText.resize(pageNum);
321 for (int i=0; i<pageNum; i++)
322 pageAnnotations[i] = pageText[i] = miniexp_dummy;
323 docReady = true;
324 }
325 }
326
327
328
329
330
331 // ----------------------------------------
332 // QDJVUDOCUMENT
333
334
335 /*! \class QDjVuDocument
336 \brief Represents a \a ddjvu_document_t object. */
337
338 QDjVuDocument::~QDjVuDocument()
339 {
340 if (document)
341 {
342 ddjvu_document_set_user_data(document, 0);
343 ddjvu_document_release(document);
344 document = 0;
345 }
346 if (priv)
347 {
348 delete priv;
349 priv = 0;
350 }
351 }
352
353 /*! Construct an empty \a QDjVuDocument object.
354 Argument \a parent indicates its parent in the \a QObject hierarchy.
355 When argument \a autoDelete is set to true, the object also
356 maintains a reference count that can be modified using functions
357 \a ref() and \a deref(). The object is deleted when function \a deref()
358 decrements the reference count to zero. */
359
360 QDjVuDocument::QDjVuDocument(bool autoDelete, QObject *parent)
361 : QObject(parent), document(0), priv(new QDjVuDocumentPrivate)
362 {
363 priv->autoDelete = autoDelete;
364 priv->docPointer = this;
365 connect(priv, SIGNAL(idle()), this, SIGNAL(idle()));
366 connect(this, SIGNAL(docinfo()), priv, SLOT(docinfo()));
367 }
368
369 /*! \overload */
370
371 QDjVuDocument::QDjVuDocument(QObject *parent)
372 : QObject(parent), document(0), priv(new QDjVuDocumentPrivate)
373 {
374 priv->autoDelete = false;
375 priv->docPointer = this;
376 connect(priv, SIGNAL(idle()), this, SIGNAL(idle()));
377 connect(this, SIGNAL(docinfo()), priv, SLOT(docinfo()));
378 }
379
380 /*! Increments the reference count. */
381
382 void
383 QDjVuDocument::ref()
384 {
385 priv->mutex.lock();
386 priv->refCount++;
387 priv->mutex.unlock();
388 }
389
390 /*! Decrements the reference count.
391 If the object was created with flag \a autoDelete,
392 function \a deleteLater() is called when the
393 reference count reaches 0. */
394
395 void
396 QDjVuDocument::deref()
397 {
398 priv->mutex.lock();
399 bool finished = !(--priv->refCount) && priv->autoDelete;
400 priv->mutex.unlock();
401 if (finished)
402 delete this;
403 }
404
405
406 /*! Associates the \a QDjVuDocument object with the
407 \a QDjVuContext object \ctx in order to decode
408 the DjVu file \a f. */
409
410 bool
411 QDjVuDocument::setFileName(QDjVuContext *ctx, QString f, bool cache)
412 {
413 QMutexLocker locker(&priv->mutex);
414 if (isValid())
415 {
416 ddjvu_document_set_user_data(document, 0);
417 ddjvu_document_release(document);
418 priv->running.clear();
419 document = 0;
420 }
421 QFileInfo info(f);
422 if (! info.isReadable())
423 {
424 qWarning("QDjVuDocument::setFileName: cannot read file");
425 return false;
426 }
427 #if DDJVUAPI_VERSION >= 19
428 QByteArray b = f.toUtf8();
429 document = ddjvu_document_create_by_filename_utf8(*ctx, b, cache);
430 #else
431 QByteArray b = QFile::encodeName(f);
432 document = ddjvu_document_create_by_filename(*ctx, b, cache);
433 #endif
434 if (! document)
435 {
436 qWarning("QDjVuDocument::setFileName: cannot create decoder");
437 return false;
438 }
439 ddjvu_document_set_user_data(document, (void*)this);
440 priv->docinfo();
441 return true;
442 }
443
444 /*! Associates the \a QDjVuDocument object with
445 with the \a QDjVuContext object \ctx in order
446 to decode the DjVu data located at URL \a url.
447 This is only useful inside a subclass of this class
448 because you must redefine virtual function \a newstream
449 in order to provide access to the data. */
450
451 bool
452 QDjVuDocument::setUrl(QDjVuContext *ctx, QUrl url, bool cache)
453 {
454 QMutexLocker locker(&priv->mutex);
455 if (isValid())
456 {
457 ddjvu_document_set_user_data(document, 0);
458 ddjvu_document_release(document);
459 priv->running.clear();
460 document = 0;
461 }
462 QByteArray b = url.toEncoded();
463 if (! b.size())
464 {
465 qWarning("QDjVuDocument::setUrl: invalid url");
466 return false;
467 }
468
469 document = ddjvu_document_create(*ctx, b, cache);
470 if (! document)
471 {
472 qWarning("QDjVuDocument::setUrl: cannot create");
473 return false;
474 }
475 ddjvu_document_set_user_data(document, (void*)this);
476 priv->docinfo();
477 return true;
478 }
479
480
481 static bool
482 string_is_on(QString val)
483 {
484 QString v = val.toLower();
485 return v == "yes" || v == "on" || v == "true" || v == "1";
486 }
487
488 static bool
489 string_is_off(QString val)
490 {
491 QString v = val.toLower();
492 return v == "no" || v == "off" || v == "false" || v == "0";
493 }
494
495
496 /*! Overloaded version of \a setUrl.
497 Cache setup is determined heuristically from the url
498 and the url arguments. */
499
500 bool
501 QDjVuDocument::setUrl(QDjVuContext *ctx, QUrl url)
502 {
503 bool cache = true;
504 if (url.path().section('/', -1).indexOf('.') < 0)
505 cache = false;
506 bool djvuopts = false;
507 QPair<QString,QString> pair;
508 #if QT_VERSION >= 0x50000
509 QList<QPair<QString,QString> > qitems = QUrlQuery(url).queryItems();
510 #else
511 QList<QPair<QString,QString> > qitems = url.queryItems();
512 #endif
513 foreach(pair, qitems)
514 {
515 if (pair.first.toLower() == "djvuopts")
516 djvuopts = true;
517 else if (!djvuopts || pair.first.toLower() != "cache")
518 continue;
519 else if (string_is_on(pair.second))
520 cache = true;
521 else if (string_is_off(pair.second))
522 cache = false;
523 }
524 return setUrl(ctx, url, cache);
525 }
526
527
528 /*! This virtual function is called when receiving
529 a DDJVUAPI \a m_newstream message. This happens
530 when the decoder has been setup with functon \a setUrl.
531 You must then override this virtual function in order
532 to setup the data transfer. Data is passed to the decoder
533 using the \a streamWrite and \a streamClose member functions
534 with the specified \a streamid. See also the DDJVUAPI
535 documentation for the \a m_newstream message. */
536
537 void
538 QDjVuDocument::newstream(int, QString, QUrl)
539 {
540 qWarning("QDjVuDocument::newstream called but not implemented");
541 }
542
543 /*! Write data into the decoder stream \a streamid. */
544
545 void
546 QDjVuDocument::streamWrite(int streamid,
547 const char *data, unsigned long len )
548 {
549 QMutexLocker locker(&priv->mutex);
550 if (! isValid())
551 qWarning("QDjVuDocument::streamWrite: invalid document");
552 else
553 ddjvu_stream_write(document, streamid, data, len);
554 }
555
556 /*! Close the decoder stream \a streamid.
557 Setting argument \a stop to \a true indicates
558 that the stream was closed because the data
559 transfer was interrupted by the user. */
560
561 void
562 QDjVuDocument::streamClose(int streamid, bool stop)
563 {
564 QMutexLocker locker(&priv->mutex);
565 if (! isValid())
566 qWarning("QDjVuDocument::streamClose: invalid document");
567 else
568 ddjvu_stream_close(document, streamid, stop);
569 }
570
571 /*! Processes DDJVUAPI messages for this document.
572 The default implementation emits signals for
573 the \a m_error, \a m_info, \a m_docinfo, \a m_pageinfo
574 and \a m_thumbnail messsages. It also calls
575 the virtual function \a newstream when processing
576 a \m newstream message. The return value is a boolean indicating
577 if the message has been processed or rejected. */
578
579 bool
580 QDjVuDocument::handle(ddjvu_message_t *msg)
581 {
582 switch(msg->m_any.tag)
583 {
584 case DDJVU_DOCINFO:
585 ddjvu_document_check_pagedata(document, 0);
586 emit docinfo();
587 return true;
588 case DDJVU_PAGEINFO:
589 emit pageinfo();
590 return true;
591 case DDJVU_THUMBNAIL:
592 emit thumbnail(msg->m_thumbnail.pagenum);
593 return true;
594 case DDJVU_NEWSTREAM:
595 {
596 QUrl url;
597 QString name;
598 if (msg->m_newstream.url)
599 url = QUrl::fromEncoded(msg->m_newstream.url);
600 if (msg->m_newstream.name)
601 name = QString::fromLatin1(msg->m_newstream.name);
602 newstream(msg->m_newstream.streamid, name, url);
603 }
604 return true;
605 case DDJVU_ERROR:
606 emit error(QString::fromLocal8Bit(msg->m_error.message),
607 QString::fromLocal8Bit(msg->m_error.filename),
608 msg->m_error.lineno);
609 return true;
610 case DDJVU_INFO:
611 emit info(QString::fromLocal8Bit(msg->m_info.message));
612 return true;
613 default:
614 break;
615 }
616 return false;
617 }
618
619 /*! Return number of decoding threads running for this document. */
620
621 int
622 QDjVuDocument::runningProcesses(void)
623 {
624 return priv->running.size();
625 }
626
627
628 /*! Obtains the cached document annotations.
629 This function returns \a miniexp_dummy if this
630 information is not yet available.
631 Check again some time after
632 receiving signal \a docinfo(). */
633
634 miniexp_t
635 QDjVuDocument::getDocumentAnnotations()
636 {
637 QMutexLocker locker(&priv->mutex);
638 if (! priv->docReady)
639 return miniexp_dummy;
640 if (priv->documentAnnotations != miniexp_dummy)
641 return priv->documentAnnotations;
642 #if DDJVUAPI_VERSION <= 17
643 priv->documentAnnotations = miniexp_nil;
644 #else
645 priv->documentAnnotations = ddjvu_document_get_anno(document,1);
646 ddjvu_miniexp_release(document, priv->documentAnnotations);
647 #endif
648 return priv->documentAnnotations;
649 }
650
651 /*! Obtains the cached document outline.
652 This function returns \a miniexp_dummy if this
653 information is not yet available.
654 Check again some time after
655 receiving signal \a docinfo(). */
656
657 miniexp_t
658 QDjVuDocument::getDocumentOutline()
659 {
660 QMutexLocker locker(&priv->mutex);
661 if (! priv->docReady)
662 return miniexp_dummy;
663 if (priv->documentOutline != miniexp_dummy)
664 return priv->documentOutline;
665 priv->documentOutline = ddjvu_document_get_outline(document);
666 ddjvu_miniexp_release(document, priv->documentOutline);
667 return priv->documentOutline;
668 }
669
670 /*! Obtains the cached annotations for page \a pageno.
671 If this information is not yet available,
672 this function returns \a miniexp_dummy
673 and, if \a start is true, starts loading the page data.
674 Check again after receiving signal \a pageinfo(). */
675
676 miniexp_t
677 QDjVuDocument::getPageAnnotations(int pageno, bool start)
678 {
679 QMutexLocker locker(&priv->mutex);
680 if (! priv->docReady)
681 return miniexp_dummy;
682 if (pageno<0 || pageno>=priv->pageAnnotations.size())
683 return miniexp_dummy;
684 minivar_t expr = priv->pageAnnotations[pageno];
685 if (expr != miniexp_dummy)
686 return expr;
687 if (! (start || ddjvu_document_check_pagedata(*this, pageno)))
688 return expr;
689 expr = ddjvu_document_get_pageanno(document, pageno);
690 ddjvu_miniexp_release(document, expr);
691 if (expr != miniexp_dummy)
692 priv->pageAnnotations[pageno] = expr;
693 return expr;
694 }
695
696 /*! Obtains the cached hidden text for page \a pageno.
697 If this information is not yet available,
698 this function returns \a miniexp_dummy
699 and, if \a start is true, starts loading the page data.
700 Check again after receiving signal \a pageinfo(). */
701
702 miniexp_t
703 QDjVuDocument::getPageText(int pageno, bool start)
704 {
705 QMutexLocker locker(&priv->mutex);
706 if (! priv->docReady)
707 return miniexp_dummy;
708 if (pageno<0 || pageno>=priv->pageText.size())
709 return miniexp_dummy;
710 minivar_t expr = priv->pageText[pageno];
711 if (expr != miniexp_dummy)
712 return expr;
713 if (! (start || ddjvu_document_check_pagedata(*this, pageno)))
714 return expr;
715 expr = ddjvu_document_get_pagetext(document, pageno, 0);
716 ddjvu_miniexp_release(document, expr);
717 if (expr != miniexp_dummy)
718 priv->pageText[pageno] = expr;
719 return expr;
720 }
721
722
723
724 /*! \fn QDjVuDocument::isValid()
725 Indicate if this object is associated with a valid
726 \a ddjvu_document_t object. Use this function to
727 determine whether \a setFileName or \a setUrl
728 was successful. */
729
730 /*! \fn QDjVuDocument::operator ddjvu_document_t*()
731 Return a pointer to the corresponding
732 \a ddjvu_document_t object. */
733
734 /*! \fn QDjVuDocument::error(QString msg, QString filename, int lineno)
735 This signal is emitted when processing a DDJVUAPI \a m_error
736 message that is related to this document. */
737
738 /*! \fn QDjVuDocument::info(QString msg)
739 This signal is emitted when processing a DDJVUAPI \a m_info
740 message that is related to this document. */
741
742 /*! \fn QDjVuDocument::docinfo()
743 This signal is emitted when the document initialization
744 is complete. Use \a ddjvu_document_decoding_status to
745 determine whether the operation was successful. */
746
747 /*! \fn QDjVuDocument::pageinfo()
748 This signal is emitted when the data for a new page
749 has been received in full. This is useful when used
750 in relation with the DDJVUAPI function
751 \a ddjvu_document_get_pageinfo. */
752
753 /*! \fn QDjVuDocument::thumbnail(int pagenum)
754 This signal is emitted when the thumbnail for page
755 \a pagenum is ready. This is useful when used
756 in relation with the DDJVUAPI functions
757 \a ddjvu_thumnail_status and \a ddjvu_thumbnail_render. */
758
759 /*! \fn QDjVuDocument::idle()
760 This signal is emitted when all decoding threads
761 for this document are terminated. */
762
763
764
765 // ----------------------------------------
766 // QDJVUPAGE
767
768 /*! \class QDjVuPage
769 \brief Represent a \a ddjvu_page_t object. */
770
771 /*! Construct a \a QDjVuPage object for page \a pageno
772 of document \a doc. Argument \a parent indicates its
773 parent in the \a QObject hierarchy. */
774
775 QDjVuPage::QDjVuPage(QDjVuDocument *doc, int pageno, QObject *parent)
776 : QObject(parent), page(0), pageno(pageno)
777 {
778 page = ddjvu_page_create_by_pageno(*doc, pageno);
779 if (! page)
780 {
781 qWarning("QDjVuPage: invalid page number");
782 return;
783 }
784 ddjvu_page_set_user_data(page, (void*)this);
785 doc->priv->add(this);
786 }
787
788 QDjVuPage::~QDjVuPage()
789 {
790 pageno = -1;
791 if (page)
792 {
793 ddjvu_page_set_user_data(page, 0);
794 ddjvu_page_release(page);
795 page = 0;
796 }
797 }
798
799 /*! Processes DDJVUAPI messages for this page.
800 The default implementation emits signals for
801 the \a m_error, \a m_info, \a m_pageinfo, \a m_chunk
802 and \a m_relayout and \a m_redisplay messsages.
803 The return value is a boolean indicating
804 if the message has been processed or rejected. */
805
806 bool
807 QDjVuPage::handle(ddjvu_message_t *msg)
808 {
809
810 switch(msg->m_any.tag)
811 {
812 case DDJVU_PAGEINFO:
813 emit pageinfo();
814 return true;
815 case DDJVU_CHUNK:
816 emit chunk(QString::fromLatin1(msg->m_chunk.chunkid));
817 return true;
818 case DDJVU_RELAYOUT:
819 emit relayout();
820 return true;
821 case DDJVU_REDISPLAY:
822 emit redisplay();
823 return true;
824 case DDJVU_ERROR:
825 emit error(QString::fromLocal8Bit(msg->m_error.message),
826 QString::fromLocal8Bit(msg->m_error.filename),
827 msg->m_error.lineno);
828 return true;
829 case DDJVU_INFO:
830 emit info(QString::fromLocal8Bit(msg->m_info.message));
831 return true;
832 default:
833 break;
834 }
835 return false;
836 }
837
838 /*! Returns the page number associated with this page. */
839
840 int
841 QDjVuPage::pageNo()
842 {
843 return pageno;
844 }
845
846 /*! \fn QDjVuPage::isValid()
847 Indicate if this object is associated with a valid
848 \a ddjvu_page_t object. Use this function to
849 determine whether the construction was successful. */
850
851 /*! \fn QDjVuPage::operator ddjvu_page_t * ()
852 Return a pointer to the corresponding
853 \a ddjvu_page_t object. */
854
855 /*! \fn QDjVuPage::error(QString msg, QString filename, int lineno)
856 This signal is emitted when processing a DDJVUAPI \a m_error
857 message that is related to this page. */
858
859 /*! \fn QDjVuPage::info(QString msg)
860 This signal is emitted when processing a DDJVUAPI \a m_info
861 message that is related to this page. */
862
863 /*! \fn QDjVuPage::pageinfo()
864 This signal is emitted when processing a DDJVUAPI \a m_pageinfo
865 message that is related to this page. This happens twice
866 during the page decoding process. The first time when the
867 basic page information is present, and the second time
868 when the decoding process terminates.
869 Use \a ddjvu_page_decoding_status function
870 to determine what is going on. */
871
872 /*! \fn QDjVuPage::relayout()
873 This signal is emitted when processing a DDJVUAPI \a m_relayout
874 message that is related to this page. */
875
876 /*! \fn QDjVuPage::redisplay()
877 This signal is emitted when processing a DDJVUAPI \a m_redisplay
878 message that is related to this page. */
879
880 /*! \fn QDjVuPage::chunk(QString chunkid)
881 This signal is emitted when processing a DDJVUAPI \a m_chunk
882 message that is related to this page. */
883
884
885
886 // ----------------------------------------
887 // QDJVUJOB
888
889 /*! \class QDjVuJob
890 \brief Represents a \a ddjvu_job_t object. */
891
892 /*! Construct a \a QDjVuJob object.
893 Argument \a job is the \a ddjvu_job_t object.
894 Argument \a parent defines its parent in the \a QObject hierarchy. */
895
896 QDjVuJob::QDjVuJob(ddjvu_job_t *job, QObject *parent)
897 : QObject(parent), job(job)
898 {
899 if (! job)
900 qWarning("QDjVuJob: invalid job");
901 else
902 ddjvu_job_set_user_data(job, (void*)this);
903 }
904
905 QDjVuJob::~QDjVuJob()
906 {
907 ddjvu_job_set_user_data(job, 0);
908 ddjvu_job_release(job);
909 job = 0;
910 }
911
912 /*! Processes DDJVUAPI messages for this job.
913 The default implementation emits signals for
914 the \a m_error, \a m_info, and \a m_progress
915 messages. The return value is a boolean indicating
916 if the message has been processed or rejected. */
917
918 bool
919 QDjVuJob::handle(ddjvu_message_t *msg)
920 {
921 switch(msg->m_any.tag)
922 {
923 case DDJVU_ERROR:
924 emit error(QString::fromLocal8Bit(msg->m_error.message),
925 QString::fromLocal8Bit(msg->m_error.filename),
926 msg->m_error.lineno);
927 return true;
928 case DDJVU_INFO:
929 emit info(QString::fromLocal8Bit(msg->m_info.message));
930 return true;
931 case DDJVU_PROGRESS:
932 emit progress(msg->m_progress.percent);
933 return true;
934 default:
935 break;
936 }
937 return false;
938 }
939
940 /*! \fn QDjVuJob::isValid()
941 Indicate if this object is associated with a
942 \a ddjvu_job_t object. */
943
944 /*! \fn QDjVuJob::operator ddjvu_job_t * ()
945 Return a pointer to the corresponding
946 \a ddjvu_job_t object. */
947
948 /*! \fn QDjVuJob::error(QString msg, QString filename, int lineno)
949 This signal is emitted when processing a DDJVUAPI \a m_error
950 message that is related to this job. */
951
952 /*! \fn QDjVuJob::info(QString msg)
953 This signal is emitted when processing a DDJVUAPI \a m_info
954 message that is related to this job. */
955
956 /*! \fn QDjVuJob::progress(int percent)
957 This signal is emitted when processing a DDJVUAPI \a m_progress
958 message that is related to this job.
959 Argument <percent> indicates the progress level from 1 to 100. */
960
961
962
963
964 // ----------------------------------------
965 // MOC
966
967 #include "qdjvu.moc"
968
969
970 /* -------------------------------------------------------------
971 Local Variables:
972 c++-font-lock-extra-types: ( "\\sw+_t" "Q[A-Z]\\sw*[a-z]\\sw*" )
973 End:
974 ------------------------------------------------------------- */