Okular

document.cpp
1 /***************************************************************************
2  * Copyright (C) 2004-2005 by Enrico Ros <[email protected]> *
3  * Copyright (C) 2004-2008 by Albert Astals Cid <[email protected]> *
4  * Copyright (C) 2017, 2018 Klarälvdalens Datakonsult AB, a KDAB Group *
5  * company, [email protected] Work sponsored by the *
6  * LiMux project of the city of Munich *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  ***************************************************************************/
13 
14 #include "document.h"
15 #include "document_p.h"
16 #include "documentcommands_p.h"
17 
18 #include <limits.h>
19 #include <memory>
20 #ifdef Q_OS_WIN
21 #define _WIN32_WINNT 0x0500
22 #include <windows.h>
23 #elif defined(Q_OS_FREEBSD)
24 #include <sys/types.h>
25 #include <sys/sysctl.h>
26 #include <vm/vm_param.h>
27 #endif
28 
29 // qt/kde/system includes
30 #include <QtAlgorithms>
31 #include <QDir>
32 #include <QFile>
33 #include <QFileInfo>
34 #include <QMap>
35 #include <qtemporaryfile.h>
36 #include <QTextStream>
37 #include <QTimer>
38 #include <QApplication>
39 #include <QLabel>
40 #include <QPrinter>
41 #include <QPrintDialog>
42 #include <QScreen>
43 #include <QWindow>
44 #include <QStack>
45 #include <QUndoCommand>
46 #include <QMimeDatabase>
47 #include <QDesktopServices>
48 #include <QPageSize>
49 #include <QRegularExpression>
50 #include <QStandardPaths>
51 
52 #include <kauthorized.h>
53 #include <kconfigdialog.h>
54 #include <kmacroexpander.h>
55 #include <kmessagebox.h>
56 #include <kmimetypetrader.h>
57 #include <kprocess.h>
58 #include <KRun>
59 #include <kshell.h>
60 #include <kzip.h>
61 #include <KIO/Global>
62 #include <KFormat>
63 #include <KLocalizedString>
64 #include <KPluginMetaData>
65 #include <Kdelibs4Migration>
66 
67 // local includes
68 #include "action.h"
69 #include "annotations.h"
70 #include "annotations_p.h"
71 #include "audioplayer.h"
72 #include "audioplayer_p.h"
73 #include "bookmarkmanager.h"
74 #include "chooseenginedialog_p.h"
75 #include "debug_p.h"
76 #include "generator_p.h"
77 #include "interfaces/configinterface.h"
78 #include "interfaces/guiinterface.h"
79 #include "interfaces/printinterface.h"
80 #include "interfaces/saveinterface.h"
81 #include "observer.h"
82 #include "misc.h"
83 #include "page.h"
84 #include "page_p.h"
85 #include "pagecontroller_p.h"
86 #include "scripter.h"
87 #include "script/event_p.h"
88 #include "settings_core.h"
89 #include "sourcereference.h"
90 #include "sourcereference_p.h"
91 #include "texteditors_p.h"
92 #include "tile.h"
93 #include "tilesmanager_p.h"
94 #include "utils_p.h"
95 #include "view.h"
96 #include "view_p.h"
97 #include "form.h"
98 #include "utils.h"
99 
100 #include <config-okular.h>
101 
102 #if HAVE_MALLOC_TRIM
103 #include "malloc.h"
104 #endif
105 
106 using namespace Okular;
107 
108 struct AllocatedPixmap
109 {
110  // owner of the page
111  DocumentObserver *observer;
112  int page;
113  qulonglong memory;
114  // public constructor: initialize data
115  AllocatedPixmap( DocumentObserver *o, int p, qulonglong m ) : observer( o ), page( p ), memory( m ) {}
116 };
117 
118 struct ArchiveData
119 {
120  ArchiveData()
121  {
122  }
123 
124  QString originalFileName;
125  QTemporaryFile document;
126  QTemporaryFile metadataFile;
127 };
128 
129 struct RunningSearch
130 {
131  // store search properties
132  int continueOnPage;
133  RegularAreaRect continueOnMatch;
134  QSet< int > highlightedPages;
135 
136  // fields related to previous searches (used for 'continueSearch')
137  QString cachedString;
138  Document::SearchType cachedType;
139  Qt::CaseSensitivity cachedCaseSensitivity;
140  bool cachedViewportMove : 1;
141  bool isCurrentlySearching : 1;
142  QColor cachedColor;
143  int pagesDone;
144 };
145 
146 #define foreachObserver( cmd ) {\
147  QSet< DocumentObserver * >::const_iterator it=d->m_observers.constBegin(), end=d->m_observers.constEnd();\
148  for ( ; it != end ; ++ it ) { (*it)-> cmd ; } }
149 
150 #define foreachObserverD( cmd ) {\
151  QSet< DocumentObserver * >::const_iterator it = m_observers.constBegin(), end = m_observers.constEnd();\
152  for ( ; it != end ; ++ it ) { (*it)-> cmd ; } }
153 
154 #define OKULAR_HISTORY_MAXSTEPS 100
155 #define OKULAR_HISTORY_SAVEDSTEPS 10
156 
157 // how often to run slotTimedMemoryCheck
158 const int kMemCheckTime = 2000; // in msec
159 
160 /***** Document ******/
161 
162 QString DocumentPrivate::pagesSizeString() const
163 {
164  if (m_generator)
165  {
166  if (m_generator->pagesSizeMetric() != Generator::None)
167  {
168  QSizeF size = m_parent->allPagesSize();
169  // Single page size
170  if (size.isValid()) return localizedSize(size);
171 
172  // Multiple page sizes
173  QString sizeString;
174  QHash<QString, int> pageSizeFrequencies;
175 
176  // Compute frequencies of each page size
177  for (int i = 0; i < m_pagesVector.count(); ++i)
178  {
179  const Page *p = m_pagesVector.at(i);
180  sizeString = localizedSize( QSizeF( p->width(), p->height() ) );
181  pageSizeFrequencies[sizeString] = pageSizeFrequencies.value(sizeString, 0) + 1;
182  }
183 
184  // Figure out which page size is most frequent
185  int largestFrequencySeen = 0;
186  QString mostCommonPageSize = QString();
187  QHash<QString, int>::const_iterator i = pageSizeFrequencies.constBegin();
188  while (i != pageSizeFrequencies.constEnd())
189  {
190  if (i.value() > largestFrequencySeen)
191  {
192  largestFrequencySeen = i.value();
193  mostCommonPageSize = i.key();
194  }
195  ++i;
196  }
197  QString finalText = i18nc( "@info %1 is a page size", "Most pages are %1.", mostCommonPageSize );
198 
199  return finalText;
200  }
201  else return QString();
202  }
203  else return QString();
204 }
205 
206 QString DocumentPrivate::namePaperSize(double inchesWidth, double inchesHeight) const
207 {
208  const QPrinter::Orientation orientation = inchesWidth > inchesHeight ? QPrinter::Landscape : QPrinter::Portrait;
209 
210  const QSize pointsSize(inchesWidth *72.0, inchesHeight*72.0);
211  const QPageSize::PageSizeId paperSize = QPageSize::id(pointsSize, QPageSize::FuzzyOrientationMatch);
212 
213  const QString paperName = QPageSize::name(paperSize);
214 
215  if (orientation == QPrinter::Portrait) {
216  return i18nc("paper type and orientation (eg: Portrait A4)", "Portrait %1", paperName);
217  } else {
218  return i18nc("paper type and orientation (eg: Portrait A4)", "Landscape %1", paperName);
219  }
220 }
221 
222 QString DocumentPrivate::localizedSize(const QSizeF size) const
223 {
224  double inchesWidth = 0, inchesHeight = 0;
225  switch (m_generator->pagesSizeMetric())
226  {
227  case Generator::Points:
228  inchesWidth = size.width() / 72.0;
229  inchesHeight = size.height() / 72.0;
230  break;
231 
232  case Generator::Pixels:
233  {
234  const QSizeF dpi = m_generator->dpi();
235  inchesWidth = size.width() / dpi.width();
236  inchesHeight = size.height() / dpi.height();
237  }
238  break;
239 
240  case Generator::None:
241  break;
242  }
243  if (QLocale::system().measurementSystem() == QLocale::ImperialSystem)
244  {
245  return i18nc("%1 is width, %2 is height, %3 is paper size name", "%1 x %2 in (%3)", inchesWidth, inchesHeight, namePaperSize(inchesWidth, inchesHeight));
246  }
247  else
248  {
249  return i18nc("%1 is width, %2 is height, %3 is paper size name", "%1 x %2 mm (%3)", QString::number(inchesWidth * 25.4, 'd', 0), QString::number(inchesHeight * 25.4, 'd', 0), namePaperSize(inchesWidth, inchesHeight));
250  }
251 }
252 
253 qulonglong DocumentPrivate::calculateMemoryToFree()
254 {
255  // [MEM] choose memory parameters based on configuration profile
256  qulonglong clipValue = 0;
257  qulonglong memoryToFree = 0;
258 
259  switch ( SettingsCore::memoryLevel() )
260  {
261  case SettingsCore::EnumMemoryLevel::Low:
262  memoryToFree = m_allocatedPixmapsTotalMemory;
263  break;
264 
265  case SettingsCore::EnumMemoryLevel::Normal:
266  {
267  qulonglong thirdTotalMemory = getTotalMemory() / 3;
268  qulonglong freeMemory = getFreeMemory();
269  if (m_allocatedPixmapsTotalMemory > thirdTotalMemory) memoryToFree = m_allocatedPixmapsTotalMemory - thirdTotalMemory;
270  if (m_allocatedPixmapsTotalMemory > freeMemory) clipValue = (m_allocatedPixmapsTotalMemory - freeMemory) / 2;
271  }
272  break;
273 
274  case SettingsCore::EnumMemoryLevel::Aggressive:
275  {
276  qulonglong freeMemory = getFreeMemory();
277  if (m_allocatedPixmapsTotalMemory > freeMemory) clipValue = (m_allocatedPixmapsTotalMemory - freeMemory) / 2;
278  }
279  break;
280  case SettingsCore::EnumMemoryLevel::Greedy:
281  {
282  qulonglong freeSwap;
283  qulonglong freeMemory = getFreeMemory( &freeSwap );
284  const qulonglong memoryLimit = qMin( qMax( freeMemory, getTotalMemory()/2 ), freeMemory+freeSwap );
285  if (m_allocatedPixmapsTotalMemory > memoryLimit) clipValue = (m_allocatedPixmapsTotalMemory - memoryLimit) / 2;
286  }
287  break;
288  }
289 
290  if ( clipValue > memoryToFree )
291  memoryToFree = clipValue;
292 
293  return memoryToFree;
294 }
295 
296 void DocumentPrivate::cleanupPixmapMemory()
297 {
298  cleanupPixmapMemory( calculateMemoryToFree() );
299 }
300 
301 void DocumentPrivate::cleanupPixmapMemory( qulonglong memoryToFree )
302 {
303  if ( memoryToFree < 1 )
304  return;
305 
306  const int currentViewportPage = (*m_viewportIterator).pageNumber;
307 
308  // Create a QMap of visible rects, indexed by page number
309  QMap< int, VisiblePageRect * > visibleRects;
310  QVector< Okular::VisiblePageRect * >::const_iterator vIt = m_pageRects.constBegin(), vEnd = m_pageRects.constEnd();
311  for ( ; vIt != vEnd; ++vIt )
312  visibleRects.insert( (*vIt)->pageNumber, (*vIt) );
313 
314  // Free memory starting from pages that are farthest from the current one
315  int pagesFreed = 0;
316  while ( memoryToFree > 0 )
317  {
318  AllocatedPixmap * p = searchLowestPriorityPixmap( true, true );
319  if ( !p ) // No pixmap to remove
320  break;
321 
322  qCDebug(OkularCoreDebug).nospace() << "Evicting cache pixmap observer=" << p->observer << " page=" << p->page;
323 
324  // m_allocatedPixmapsTotalMemory can't underflow because we always add or remove
325  // the memory used by the AllocatedPixmap so at most it can reach zero
326  m_allocatedPixmapsTotalMemory -= p->memory;
327  // Make sure memoryToFree does not underflow
328  if ( p->memory > memoryToFree )
329  memoryToFree = 0;
330  else
331  memoryToFree -= p->memory;
332  pagesFreed++;
333  // delete pixmap
334  m_pagesVector.at( p->page )->deletePixmap( p->observer );
335  // delete allocation descriptor
336  delete p;
337  }
338 
339  // If we're still on low memory, try to free individual tiles
340 
341  // Store pages that weren't completely removed
342 
343  QLinkedList< AllocatedPixmap * > pixmapsToKeep;
344  while (memoryToFree > 0)
345  {
346  int clean_hits = 0;
347  for (DocumentObserver *observer : qAsConst(m_observers))
348  {
349  AllocatedPixmap * p = searchLowestPriorityPixmap( false, true, observer );
350  if ( !p ) // No pixmap to remove
351  continue;
352 
353  clean_hits++;
354 
355  TilesManager *tilesManager = m_pagesVector.at( p->page )->d->tilesManager( observer );
356  if ( tilesManager && tilesManager->totalMemory() > 0 )
357  {
358  qulonglong memoryDiff = p->memory;
359  NormalizedRect visibleRect;
360  if ( visibleRects.contains( p->page ) )
361  visibleRect = visibleRects[ p->page ]->rect;
362 
363  // Free non visible tiles
364  tilesManager->cleanupPixmapMemory( memoryToFree, visibleRect, currentViewportPage );
365 
366  p->memory = tilesManager->totalMemory();
367  memoryDiff -= p->memory;
368  memoryToFree = (memoryDiff < memoryToFree) ? (memoryToFree - memoryDiff) : 0;
369  m_allocatedPixmapsTotalMemory -= memoryDiff;
370 
371  if ( p->memory > 0 )
372  pixmapsToKeep.append( p );
373  else
374  delete p;
375  }
376  else
377  pixmapsToKeep.append( p );
378  }
379 
380  if (clean_hits == 0) break;
381  }
382 
383  m_allocatedPixmaps += pixmapsToKeep;
384  //p--rintf("freeMemory A:[%d -%d = %d] \n", m_allocatedPixmaps.count() + pagesFreed, pagesFreed, m_allocatedPixmaps.count() );
385 }
386 
387 /* Returns the next pixmap to evict from cache, or NULL if no suitable pixmap
388  * if found. If unloadableOnly is set, only unloadable pixmaps are returned. If
389  * thenRemoveIt is set, the pixmap is removed from m_allocatedPixmaps before
390  * returning it
391  */
392 AllocatedPixmap * DocumentPrivate::searchLowestPriorityPixmap( bool unloadableOnly, bool thenRemoveIt, DocumentObserver *observer )
393 {
394  QLinkedList< AllocatedPixmap * >::iterator pIt = m_allocatedPixmaps.begin();
395  QLinkedList< AllocatedPixmap * >::iterator pEnd = m_allocatedPixmaps.end();
396  QLinkedList< AllocatedPixmap * >::iterator farthestPixmap = pEnd;
397  const int currentViewportPage = (*m_viewportIterator).pageNumber;
398 
399  /* Find the pixmap that is farthest from the current viewport */
400  int maxDistance = -1;
401  while ( pIt != pEnd )
402  {
403  const AllocatedPixmap * p = *pIt;
404  // Filter by observer
405  if ( observer == nullptr || p->observer == observer )
406  {
407  const int distance = qAbs( p->page - currentViewportPage );
408  if ( maxDistance < distance && ( !unloadableOnly || p->observer->canUnloadPixmap( p->page ) ) )
409  {
410  maxDistance = distance;
411  farthestPixmap = pIt;
412  }
413  }
414  ++pIt;
415  }
416 
417  /* No pixmap to remove */
418  if ( farthestPixmap == pEnd )
419  return nullptr;
420 
421  AllocatedPixmap * selectedPixmap = *farthestPixmap;
422  if ( thenRemoveIt )
423  m_allocatedPixmaps.erase( farthestPixmap );
424  return selectedPixmap;
425 }
426 
427 qulonglong DocumentPrivate::getTotalMemory()
428 {
429  static qulonglong cachedValue = 0;
430  if ( cachedValue )
431  return cachedValue;
432 
433 #if defined(Q_OS_LINUX)
434  // if /proc/meminfo doesn't exist, return 128MB
435  QFile memFile( QStringLiteral("/proc/meminfo") );
436  if ( !memFile.open( QIODevice::ReadOnly ) )
437  return (cachedValue = 134217728);
438 
439  QTextStream readStream( &memFile );
440  while ( true )
441  {
442  QString entry = readStream.readLine();
443  if ( entry.isNull() ) break;
444  if ( entry.startsWith( QLatin1String("MemTotal:") ) )
445  return (cachedValue = (Q_UINT64_C(1024) * entry.section( QLatin1Char ( ' ' ), -2, -2 ).toULongLong()));
446  }
447 #elif defined(Q_OS_FREEBSD)
448  qulonglong physmem;
449  int mib[] = {CTL_HW, HW_PHYSMEM};
450  size_t len = sizeof( physmem );
451  if ( sysctl( mib, 2, &physmem, &len, NULL, 0 ) == 0 )
452  return (cachedValue = physmem);
453 #elif defined(Q_OS_WIN)
454  MEMORYSTATUSEX stat;
455  stat.dwLength = sizeof(stat);
456  GlobalMemoryStatusEx (&stat);
457 
458  return ( cachedValue = stat.ullTotalPhys );
459 #endif
460  return (cachedValue = 134217728);
461 }
462 
463 qulonglong DocumentPrivate::getFreeMemory( qulonglong *freeSwap )
464 {
465  static QTime lastUpdate = QTime::currentTime().addSecs(-3);
466  static qulonglong cachedValue = 0;
467  static qulonglong cachedFreeSwap = 0;
468 
469  if ( qAbs( lastUpdate.msecsTo( QTime::currentTime() ) ) <= kMemCheckTime - 100 )
470  {
471  if (freeSwap)
472  *freeSwap = cachedFreeSwap;
473  return cachedValue;
474  }
475 
476  /* Initialize the returned free swap value to 0. It is overwritten if the
477  * actual value is available */
478  if (freeSwap)
479  *freeSwap = 0;
480 
481 #if defined(Q_OS_LINUX)
482  // if /proc/meminfo doesn't exist, return MEMORY FULL
483  QFile memFile( QStringLiteral("/proc/meminfo") );
484  if ( !memFile.open( QIODevice::ReadOnly ) )
485  return 0;
486 
487  // read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
488  // and 'Cached' fields. consider swapped memory as used memory.
489  qulonglong memoryFree = 0;
490  QString entry;
491  QTextStream readStream( &memFile );
492  static const int nElems = 5;
493  QString names[nElems] = { QStringLiteral("MemFree:"), QStringLiteral("Buffers:"), QStringLiteral("Cached:"), QStringLiteral("SwapFree:"), QStringLiteral("SwapTotal:") };
494  qulonglong values[nElems] = { 0, 0, 0, 0, 0 };
495  bool foundValues[nElems] = { false, false, false, false, false };
496  while ( true )
497  {
498  entry = readStream.readLine();
499  if ( entry.isNull() ) break;
500  for ( int i = 0; i < nElems; ++i )
501  {
502  if ( entry.startsWith( names[i] ) )
503  {
504  values[i] = entry.section( QLatin1Char ( ' ' ), -2, -2 ).toULongLong( &foundValues[i] );
505  }
506  }
507  }
508  memFile.close();
509  bool found = true;
510  for ( int i = 0; found && i < nElems; ++i )
511  found = found && foundValues[i];
512  if ( found )
513  {
514  /* MemFree + Buffers + Cached - SwapUsed =
515  * = MemFree + Buffers + Cached - (SwapTotal - SwapFree) =
516  * = MemFree + Buffers + Cached + SwapFree - SwapTotal */
517  memoryFree = values[0] + values[1] + values[2] + values[3];
518  if ( values[4] > memoryFree )
519  memoryFree = 0;
520  else
521  memoryFree -= values[4];
522  }
523  else
524  {
525  return 0;
526  }
527 
528  lastUpdate = QTime::currentTime();
529 
530  if (freeSwap)
531  *freeSwap = ( cachedFreeSwap = (Q_UINT64_C(1024) * values[3]) );
532  return ( cachedValue = (Q_UINT64_C(1024) * memoryFree) );
533 #elif defined(Q_OS_FREEBSD)
534  qulonglong cache, inact, free, psize;
535  size_t cachelen, inactlen, freelen, psizelen;
536  cachelen = sizeof( cache );
537  inactlen = sizeof( inact );
538  freelen = sizeof( free );
539  psizelen = sizeof( psize );
540  // sum up inactive, cached and free memory
541  if ( sysctlbyname( "vm.stats.vm.v_cache_count", &cache, &cachelen, NULL, 0 ) == 0 &&
542  sysctlbyname( "vm.stats.vm.v_inactive_count", &inact, &inactlen, NULL, 0 ) == 0 &&
543  sysctlbyname( "vm.stats.vm.v_free_count", &free, &freelen, NULL, 0 ) == 0 &&
544  sysctlbyname( "vm.stats.vm.v_page_size", &psize, &psizelen, NULL, 0 ) == 0 )
545  {
546  lastUpdate = QTime::currentTime();
547  return (cachedValue = (cache + inact + free) * psize);
548  }
549  else
550  {
551  return 0;
552  }
553 #elif defined(Q_OS_WIN)
554  MEMORYSTATUSEX stat;
555  stat.dwLength = sizeof(stat);
556  GlobalMemoryStatusEx (&stat);
557 
558  lastUpdate = QTime::currentTime();
559 
560  if (freeSwap)
561  *freeSwap = ( cachedFreeSwap = stat.ullAvailPageFile );
562  return ( cachedValue = stat.ullAvailPhys );
563 #else
564  // tell the memory is full.. will act as in LOW profile
565  return 0;
566 #endif
567 }
568 
569 bool DocumentPrivate::loadDocumentInfo( LoadDocumentInfoFlags loadWhat )
570 // note: load data and stores it internally (document or pages). observers
571 // are still uninitialized at this point so don't access them
572 {
573  //qCDebug(OkularCoreDebug).nospace() << "Using '" << d->m_xmlFileName << "' as document info file.";
574  if ( m_xmlFileName.isEmpty() )
575  return false;
576 
577  QFile infoFile( m_xmlFileName );
578  return loadDocumentInfo( infoFile, loadWhat );
579 }
580 
581 bool DocumentPrivate::loadDocumentInfo( QFile &infoFile, LoadDocumentInfoFlags loadWhat )
582 {
583  if ( !infoFile.exists() || !infoFile.open( QIODevice::ReadOnly ) )
584  return false;
585 
586  // Load DOM from XML file
587  QDomDocument doc( QStringLiteral("documentInfo") );
588  if ( !doc.setContent( &infoFile ) )
589  {
590  qCDebug(OkularCoreDebug) << "Can't load XML pair! Check for broken xml.";
591  infoFile.close();
592  return false;
593  }
594  infoFile.close();
595 
596  QDomElement root = doc.documentElement();
597 
598  if ( root.tagName() != QLatin1String("documentInfo") )
599  return false;
600 
601  bool loadedAnything = false; // set if something gets actually loaded
602 
603  // Parse the DOM tree
604  QDomNode topLevelNode = root.firstChild();
605  while ( topLevelNode.isElement() )
606  {
607  QString catName = topLevelNode.toElement().tagName();
608 
609  // Restore page attributes (bookmark, annotations, ...) from the DOM
610  if ( catName == QLatin1String("pageList") && ( loadWhat & LoadPageInfo ) )
611  {
612  QDomNode pageNode = topLevelNode.firstChild();
613  while ( pageNode.isElement() )
614  {
615  QDomElement pageElement = pageNode.toElement();
616  if ( pageElement.hasAttribute( QStringLiteral("number") ) )
617  {
618  // get page number (node's attribute)
619  bool ok;
620  int pageNumber = pageElement.attribute( QStringLiteral("number") ).toInt( &ok );
621 
622  // pass the domElement to the right page, to read config data from
623  if ( ok && pageNumber >= 0 && pageNumber < (int)m_pagesVector.count() )
624  {
625  if ( m_pagesVector[ pageNumber ]->d->restoreLocalContents( pageElement ) )
626  loadedAnything = true;
627  }
628  }
629  pageNode = pageNode.nextSibling();
630  }
631  }
632 
633  // Restore 'general info' from the DOM
634  else if ( catName == QLatin1String("generalInfo") && ( loadWhat & LoadGeneralInfo ) )
635  {
636  QDomNode infoNode = topLevelNode.firstChild();
637  while ( infoNode.isElement() )
638  {
639  QDomElement infoElement = infoNode.toElement();
640 
641  // restore viewports history
642  if ( infoElement.tagName() == QLatin1String("history") )
643  {
644  // clear history
645  m_viewportHistory.clear();
646  // append old viewports
647  QDomNode historyNode = infoNode.firstChild();
648  while ( historyNode.isElement() )
649  {
650  QDomElement historyElement = historyNode.toElement();
651  if ( historyElement.hasAttribute( QStringLiteral("viewport") ) )
652  {
653  QString vpString = historyElement.attribute( QStringLiteral("viewport") );
654  m_viewportIterator = m_viewportHistory.insert( m_viewportHistory.end(),
655  DocumentViewport( vpString ) );
656  loadedAnything = true;
657  }
658  historyNode = historyNode.nextSibling();
659  }
660  // consistency check
661  if ( m_viewportHistory.isEmpty() )
662  m_viewportIterator = m_viewportHistory.insert( m_viewportHistory.end(), DocumentViewport() );
663  }
664  else if ( infoElement.tagName() == QLatin1String("rotation") )
665  {
666  QString str = infoElement.text();
667  bool ok = true;
668  int newrotation = !str.isEmpty() ? ( str.toInt( &ok ) % 4 ) : 0;
669  if ( ok && newrotation != 0 )
670  {
671  setRotationInternal( newrotation, false );
672  loadedAnything = true;
673  }
674  }
675  else if ( infoElement.tagName() == QLatin1String("views") )
676  {
677  QDomNode viewNode = infoNode.firstChild();
678  while ( viewNode.isElement() )
679  {
680  QDomElement viewElement = viewNode.toElement();
681  if ( viewElement.tagName() == QLatin1String("view") )
682  {
683  const QString viewName = viewElement.attribute( QStringLiteral("name") );
684  for ( View *view : qAsConst(m_views) )
685  {
686  if ( view->name() == viewName )
687  {
688  loadViewsInfo( view, viewElement );
689  loadedAnything = true;
690  break;
691  }
692  }
693  }
694  viewNode = viewNode.nextSibling();
695  }
696  }
697  infoNode = infoNode.nextSibling();
698  }
699  }
700 
701  topLevelNode = topLevelNode.nextSibling();
702  } // </documentInfo>
703 
704  return loadedAnything;
705 }
706 
707 void DocumentPrivate::loadViewsInfo( View *view, const QDomElement &e )
708 {
709  QDomNode viewNode = e.firstChild();
710  while ( viewNode.isElement() )
711  {
712  QDomElement viewElement = viewNode.toElement();
713 
714  if ( viewElement.tagName() == QLatin1String("zoom") )
715  {
716  const QString valueString = viewElement.attribute( QStringLiteral("value") );
717  bool newzoom_ok = true;
718  const double newzoom = !valueString.isEmpty() ? valueString.toDouble( &newzoom_ok ) : 1.0;
719  if ( newzoom_ok && newzoom != 0
720  && view->supportsCapability( View::Zoom )
722  {
723  view->setCapability( View::Zoom, newzoom );
724  }
725  const QString modeString = viewElement.attribute( QStringLiteral("mode") );
726  bool newmode_ok = true;
727  const int newmode = !modeString.isEmpty() ? modeString.toInt( &newmode_ok ) : 2;
728  if ( newmode_ok
731  {
732  view->setCapability( View::ZoomModality, newmode );
733  }
734  }
735  else if ( viewElement.tagName() == QLatin1String("viewMode") )
736  {
737  const QString modeString = viewElement.attribute( QStringLiteral("mode") );
738  bool newmode_ok = true;
739  const int newmode = !modeString.isEmpty() ? modeString.toInt( &newmode_ok ) : 2;
740  if ( newmode_ok
744  {
745  view->setCapability( View::ViewModeModality, newmode );
746  }
747  }
748  else if ( viewElement.tagName() == QLatin1String("continuous") )
749  {
750  const QString modeString = viewElement.attribute( QStringLiteral("mode") );
751  bool newmode_ok = true;
752  const int newmode = !modeString.isEmpty() ? modeString.toInt( &newmode_ok ) : 2;
753  if ( newmode_ok
755  && ( view->capabilityFlags( View::Continuous )
757  {
758  view->setCapability( View::Continuous, newmode );
759  }
760  }
761  else if ( viewElement.tagName() == QLatin1String("trimMargins") )
762  {
763  const QString valueString = viewElement.attribute( QStringLiteral("value") );
764  bool newmode_ok = true;
765  const int newmode = !valueString.isEmpty() ? valueString.toInt( &newmode_ok ) : 2;
766  if ( newmode_ok
768  && ( view->capabilityFlags( View::TrimMargins )
770  {
771  view->setCapability( View::TrimMargins, newmode );
772  }
773  }
774 
775  viewNode = viewNode.nextSibling();
776  }
777 }
778 
779 void DocumentPrivate::saveViewsInfo( View *view, QDomElement &e ) const
780 {
781  if ( view->supportsCapability( View::Zoom )
785  {
786  QDomElement zoomEl = e.ownerDocument().createElement( QStringLiteral("zoom") );
787  e.appendChild( zoomEl );
788  bool ok = true;
789  const double zoom = view->capability( View::Zoom ).toDouble( &ok );
790  if ( ok && zoom != 0 )
791  {
792  zoomEl.setAttribute( QStringLiteral("value"), QString::number(zoom) );
793  }
794  const int mode = view->capability( View::ZoomModality ).toInt( &ok );
795  if ( ok )
796  {
797  zoomEl.setAttribute( QStringLiteral("mode"), mode );
798  }
799  }
801  && ( view->capabilityFlags( View::Continuous )
803  {
804  QDomElement contEl = e.ownerDocument().createElement( QStringLiteral("continuous") );
805  e.appendChild( contEl );
806  const bool mode = view->capability( View::Continuous ).toBool();
807  contEl.setAttribute( QStringLiteral("mode"), mode );
808  }
812  {
813  QDomElement viewEl = e.ownerDocument().createElement( QStringLiteral("viewMode") );
814  e.appendChild( viewEl );
815  bool ok = true;
816  const int mode = view->capability( View::ViewModeModality ).toInt( &ok );
817  if ( ok )
818  {
819  viewEl.setAttribute( QStringLiteral("mode"), mode );
820  }
821  }
823  && ( view->capabilityFlags( View::TrimMargins )
825  {
826  QDomElement contEl = e.ownerDocument().createElement( QStringLiteral("trimMargins") );
827  e.appendChild( contEl );
828  const bool value = view->capability( View::TrimMargins ).toBool();
829  contEl.setAttribute( QStringLiteral("value"), value );
830  }
831 }
832 
833 QUrl DocumentPrivate::giveAbsoluteUrl( const QString & fileName ) const
834 {
835  if ( !QDir::isRelativePath( fileName ) )
836  return QUrl::fromLocalFile(fileName);
837 
838  if ( !m_url.isValid() )
839  return QUrl();
840 
841  return QUrl(KIO::upUrl(m_url).toString() + fileName);
842 }
843 
844 bool DocumentPrivate::openRelativeFile( const QString & fileName )
845 {
846  QUrl url = giveAbsoluteUrl( fileName );
847  if ( url.isEmpty() )
848  return false;
849 
850  qCDebug(OkularCoreDebug).nospace() << "openRelativeFile: '" << url << "'";
851 
852  emit m_parent->openUrl( url );
853  return true;
854 }
855 
856 Generator * DocumentPrivate::loadGeneratorLibrary( const KPluginMetaData &service )
857 {
858  KPluginLoader loader( service.fileName() );
859  qCDebug(OkularCoreDebug) << service.fileName();
860  KPluginFactory *factory = loader.factory();
861  if ( !factory )
862  {
863  qCWarning(OkularCoreDebug).nospace() << "Invalid plugin factory for " << service.fileName() << ":" << loader.errorString();
864  return nullptr;
865  }
866 
867  Generator * plugin = factory->create<Okular::Generator>();
868 
869  GeneratorInfo info( plugin, service );
870  m_loadedGenerators.insert( service.pluginId(), info );
871  return plugin;
872 }
873 
874 void DocumentPrivate::loadAllGeneratorLibraries()
875 {
876  if ( m_generatorsLoaded )
877  return;
878 
879  loadServiceList( availableGenerators() );
880 
881  m_generatorsLoaded = true;
882 }
883 
884 void DocumentPrivate::loadServiceList( const QVector<KPluginMetaData>& offers )
885 {
886  int count = offers.count();
887  if ( count <= 0 )
888  return;
889 
890  for ( int i = 0; i < count; ++i )
891  {
892  QString id = offers.at(i).pluginId();
893  // don't load already loaded generators
894  QHash< QString, GeneratorInfo >::const_iterator genIt = m_loadedGenerators.constFind( id );
895  if ( !m_loadedGenerators.isEmpty() && genIt != m_loadedGenerators.constEnd() )
896  continue;
897 
898  Generator * g = loadGeneratorLibrary( offers.at(i) );
899  (void)g;
900  }
901 }
902 
903 void DocumentPrivate::unloadGenerator( const GeneratorInfo& info )
904 {
905  delete info.generator;
906 }
907 
908 void DocumentPrivate::cacheExportFormats()
909 {
910  if ( m_exportCached )
911  return;
912 
913  const ExportFormat::List formats = m_generator->exportFormats();
914  for ( int i = 0; i < formats.count(); ++i )
915  {
916  if ( formats.at( i ).mimeType().name() == QLatin1String( "text/plain" ) )
917  m_exportToText = formats.at( i );
918  else
919  m_exportFormats.append( formats.at( i ) );
920  }
921 
922  m_exportCached = true;
923 }
924 
925 ConfigInterface* DocumentPrivate::generatorConfig( GeneratorInfo& info )
926 {
927  if ( info.configChecked )
928  return info.config;
929 
930  info.config = qobject_cast< Okular::ConfigInterface * >( info.generator );
931  info.configChecked = true;
932  return info.config;
933 }
934 
935 SaveInterface* DocumentPrivate::generatorSave( GeneratorInfo& info )
936 {
937  if ( info.saveChecked )
938  return info.save;
939 
940  info.save = qobject_cast< Okular::SaveInterface * >( info.generator );
941  info.saveChecked = true;
942  return info.save;
943 }
944 
945 Document::OpenResult DocumentPrivate::openDocumentInternal( const KPluginMetaData& offer, bool isstdin, const QString& docFile, const QByteArray& filedata, const QString& password )
946 {
947  QString propName = offer.pluginId();
948  QHash< QString, GeneratorInfo >::const_iterator genIt = m_loadedGenerators.constFind( propName );
949  m_walletGenerator = nullptr;
950  if ( genIt != m_loadedGenerators.constEnd() )
951  {
952  m_generator = genIt.value().generator;
953  }
954  else
955  {
956  m_generator = loadGeneratorLibrary( offer );
957  if ( !m_generator )
958  return Document::OpenError;
959  genIt = m_loadedGenerators.constFind( propName );
960  Q_ASSERT( genIt != m_loadedGenerators.constEnd() );
961  }
962  Q_ASSERT_X( m_generator, "Document::load()", "null generator?!" );
963 
964  m_generator->d_func()->m_document = this;
965 
966  // connect error reporting signals
967  m_openError.clear();
968  QMetaObject::Connection errorToOpenErrorConnection = QObject::connect( m_generator, &Generator::error, m_parent, [this](const QString &message) { m_openError = message; } );
969  QObject::connect( m_generator, &Generator::warning, m_parent, &Document::warning );
970  QObject::connect( m_generator, &Generator::notice, m_parent, &Document::notice );
971 
972  QApplication::setOverrideCursor( Qt::WaitCursor );
973 
974  const QSizeF dpi = Utils::realDpi(m_widget);
975  qCDebug(OkularCoreDebug) << "Output DPI:" << dpi;
976  m_generator->setDPI(dpi);
977 
978  Document::OpenResult openResult = Document::OpenError;
979  if ( !isstdin )
980  {
981  openResult = m_generator->loadDocumentWithPassword( docFile, m_pagesVector, password );
982  }
983  else if ( !filedata.isEmpty() )
984  {
985  if ( m_generator->hasFeature( Generator::ReadRawData ) )
986  {
987  openResult = m_generator->loadDocumentFromDataWithPassword( filedata, m_pagesVector, password );
988  }
989  else
990  {
991  m_tempFile = new QTemporaryFile();
992  if ( !m_tempFile->open() )
993  {
994  delete m_tempFile;
995  m_tempFile = nullptr;
996  }
997  else
998  {
999  m_tempFile->write( filedata );
1000  QString tmpFileName = m_tempFile->fileName();
1001  m_tempFile->close();
1002  openResult = m_generator->loadDocumentWithPassword( tmpFileName, m_pagesVector, password );
1003  }
1004  }
1005  }
1006 
1008  if ( openResult != Document::OpenSuccess || m_pagesVector.size() <= 0 )
1009  {
1010  m_generator->d_func()->m_document = nullptr;
1011  QObject::disconnect( m_generator, nullptr, m_parent, nullptr );
1012 
1013  // TODO this is a bit of a hack, since basically means that
1014  // you can only call walletDataForFile after calling openDocument
1015  // but since in reality it's what happens I've decided not to refactor/break API
1016  // One solution is just kill walletDataForFile and make OpenResult be an object
1017  // where the wallet data is also returned when OpenNeedsPassword
1018  m_walletGenerator = m_generator;
1019  m_generator = nullptr;
1020 
1021  qDeleteAll( m_pagesVector );
1022  m_pagesVector.clear();
1023  delete m_tempFile;
1024  m_tempFile = nullptr;
1025 
1026  // TODO: emit a message telling the document is empty
1027  if ( openResult == Document::OpenSuccess )
1028  openResult = Document::OpenError;
1029  } else {
1030  /*
1031  * Now that the documen is opened, the tab (if using tabs) is visible, which mean that
1032  * we can now connect the error reporting signal directly to the parent
1033  */
1034 
1035  QObject::disconnect(errorToOpenErrorConnection);
1036  QObject::connect( m_generator, &Generator::error, m_parent, &Document::error );
1037  }
1038 
1039  return openResult;
1040 }
1041 
1042 bool DocumentPrivate::savePageDocumentInfo( QTemporaryFile *infoFile, int what ) const
1043 {
1044  if ( infoFile->open() )
1045  {
1046  // 1. Create DOM
1047  QDomDocument doc( QStringLiteral("documentInfo") );
1048  QDomProcessingInstruction xmlPi = doc.createProcessingInstruction(
1049  QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"utf-8\"" ) );
1050  doc.appendChild( xmlPi );
1051  QDomElement root = doc.createElement( QStringLiteral("documentInfo") );
1052  doc.appendChild( root );
1053 
1054  // 2.1. Save page attributes (bookmark state, annotations, ... ) to DOM
1055  QDomElement pageList = doc.createElement( QStringLiteral("pageList") );
1056  root.appendChild( pageList );
1057  // <page list><page number='x'>.... </page> save pages that hold data
1058  QVector< Page * >::const_iterator pIt = m_pagesVector.constBegin(), pEnd = m_pagesVector.constEnd();
1059  for ( ; pIt != pEnd; ++pIt )
1060  (*pIt)->d->saveLocalContents( pageList, doc, PageItems( what ) );
1061 
1062  // 3. Save DOM to XML file
1063  QString xml = doc.toString();
1064  QTextStream os( infoFile );
1065  os.setCodec( "UTF-8" );
1066  os << xml;
1067  return true;
1068  }
1069  return false;
1070 }
1071 
1072 DocumentViewport DocumentPrivate::nextDocumentViewport() const
1073 {
1074  DocumentViewport ret = m_nextDocumentViewport;
1075  if ( !m_nextDocumentDestination.isEmpty() && m_generator )
1076  {
1077  DocumentViewport vp( m_parent->metaData( QStringLiteral("NamedViewport"), m_nextDocumentDestination ).toString() );
1078  if ( vp.isValid() )
1079  {
1080  ret = vp;
1081  }
1082  }
1083  return ret;
1084 }
1085 
1086 void DocumentPrivate::performAddPageAnnotation( int page, Annotation * annotation )
1087 {
1088  Okular::SaveInterface * iface = qobject_cast< Okular::SaveInterface * >( m_generator );
1089  AnnotationProxy *proxy = iface ? iface->annotationProxy() : nullptr;
1090 
1091  // find out the page to attach annotation
1092  Page * kp = m_pagesVector[ page ];
1093  if ( !m_generator || !kp )
1094  return;
1095 
1096  // the annotation belongs already to a page
1097  if ( annotation->d_ptr->m_page )
1098  return;
1099 
1100  // add annotation to the page
1101  kp->addAnnotation( annotation );
1102 
1103  // tell the annotation proxy
1104  if ( proxy && proxy->supports(AnnotationProxy::Addition) )
1105  proxy->notifyAddition( annotation, page );
1106 
1107  // notify observers about the change
1108  notifyAnnotationChanges( page );
1109 
1110  if ( annotation->flags() & Annotation::ExternallyDrawn )
1111  {
1112  // Redraw everything, including ExternallyDrawn annotations
1113  refreshPixmaps( page );
1114  }
1115 }
1116 
1117 void DocumentPrivate::performRemovePageAnnotation( int page, Annotation * annotation )
1118 {
1119  Okular::SaveInterface * iface = qobject_cast< Okular::SaveInterface * >( m_generator );
1120  AnnotationProxy *proxy = iface ? iface->annotationProxy() : nullptr;
1121  bool isExternallyDrawn;
1122 
1123  // find out the page
1124  Page * kp = m_pagesVector[ page ];
1125  if ( !m_generator || !kp )
1126  return;
1127 
1128  if ( annotation->flags() & Annotation::ExternallyDrawn )
1129  isExternallyDrawn = true;
1130  else
1131  isExternallyDrawn = false;
1132 
1133  // try to remove the annotation
1134  if ( m_parent->canRemovePageAnnotation( annotation ) )
1135  {
1136  // tell the annotation proxy
1137  if ( proxy && proxy->supports(AnnotationProxy::Removal) )
1138  proxy->notifyRemoval( annotation, page );
1139 
1140  kp->removeAnnotation( annotation ); // Also destroys the object
1141 
1142  // in case of success, notify observers about the change
1143  notifyAnnotationChanges( page );
1144 
1145  if ( isExternallyDrawn )
1146  {
1147  // Redraw everything, including ExternallyDrawn annotations
1148  refreshPixmaps( page );
1149  }
1150  }
1151 }
1152 
1153 void DocumentPrivate::performModifyPageAnnotation( int page, Annotation * annotation, bool appearanceChanged )
1154 {
1155  Okular::SaveInterface * iface = qobject_cast< Okular::SaveInterface * >( m_generator );
1156  AnnotationProxy *proxy = iface ? iface->annotationProxy() : nullptr;
1157 
1158  // find out the page
1159  Page * kp = m_pagesVector[ page ];
1160  if ( !m_generator || !kp )
1161  return;
1162 
1163  // tell the annotation proxy
1164  if ( proxy && proxy->supports(AnnotationProxy::Modification) )
1165  {
1166  proxy->notifyModification( annotation, page, appearanceChanged );
1167  }
1168 
1169  // notify observers about the change
1170  notifyAnnotationChanges( page );
1171  if ( appearanceChanged && (annotation->flags() & Annotation::ExternallyDrawn) )
1172  {
1173  /* When an annotation is being moved, the generator will not render it.
1174  * Therefore there's no need to refresh pixmaps after the first time */
1175  if ( annotation->flags() & (Annotation::BeingMoved | Annotation::BeingResized) )
1176  {
1177  if ( m_annotationBeingModified )
1178  return;
1179  else // First time: take note
1180  m_annotationBeingModified = true;
1181  }
1182  else
1183  {
1184  m_annotationBeingModified = false;
1185  }
1186 
1187  // Redraw everything, including ExternallyDrawn annotations
1188  qCDebug(OkularCoreDebug) << "Refreshing Pixmaps";
1189  refreshPixmaps( page );
1190  }
1191 }
1192 
1193 void DocumentPrivate::performSetAnnotationContents( const QString & newContents, Annotation *annot, int pageNumber )
1194 {
1195  bool appearanceChanged = false;
1196 
1197  // Check if appearanceChanged should be true
1198  switch ( annot->subType() )
1199  {
1200  // If it's an in-place TextAnnotation, set the inplace text
1202  {
1203  Okular::TextAnnotation * txtann = static_cast< Okular::TextAnnotation * >( annot );
1204  if ( txtann->textType() == Okular::TextAnnotation::InPlace )
1205  {
1206  appearanceChanged = true;
1207  }
1208  break;
1209  }
1210  // If it's a LineAnnotation, check if caption text is visible
1212  {
1213  Okular::LineAnnotation * lineann = static_cast< Okular::LineAnnotation * >( annot );
1214  if ( lineann->showCaption() )
1215  appearanceChanged = true;
1216  break;
1217  }
1218  default:
1219  break;
1220  }
1221 
1222  // Set contents
1223  annot->setContents( newContents );
1224 
1225  // Tell the document the annotation has been modified
1226  performModifyPageAnnotation( pageNumber, annot, appearanceChanged );
1227 }
1228 
1229 void DocumentPrivate::recalculateForms()
1230 {
1231  const QVariant fco = m_parent->metaData(QStringLiteral("FormCalculateOrder"));
1232  const QVector<int> formCalculateOrder = fco.value<QVector<int>>();
1233  foreach(int formId, formCalculateOrder) {
1234  for ( uint pageIdx = 0; pageIdx < m_parent->pages(); pageIdx++ )
1235  {
1236  const Page *p = m_parent->page( pageIdx );
1237  if (p)
1238  {
1239  bool pageNeedsRefresh = false;
1240  foreach( FormField *form, p->formFields() )
1241  {
1242  if ( form->id() == formId ) {
1244  if (action)
1245  {
1246  FormFieldText *fft = dynamic_cast< FormFieldText * >( form );
1247  std::shared_ptr<Event> event;
1248  QString oldVal;
1249  if ( fft )
1250  {
1251  // Prepare text calculate event
1252  event = Event::createFormCalculateEvent( fft, m_pagesVector[pageIdx] );
1253  if ( !m_scripter )
1254  m_scripter = new Scripter( this );
1255  m_scripter->setEvent( event.get() );
1256  // The value maybe changed in javascript so save it first.
1257  oldVal = fft->text();
1258  }
1259 
1260  m_parent->processAction( action );
1261  if ( event && fft )
1262  {
1263  // Update text field from calculate
1264  m_scripter->setEvent( nullptr );
1265  const QString newVal = event->value().toString();
1266  if ( newVal != oldVal )
1267  {
1268  fft->setText( newVal );
1269  fft->setAppearanceText( newVal );
1270  if ( const Okular::Action *action = fft->additionalAction( Okular::FormField::FormatField ) )
1271  {
1272  // The format action handles the refresh.
1273  m_parent->processFormatAction( action, fft );
1274  }
1275  else
1276  {
1277  emit m_parent->refreshFormWidget( fft );
1278  pageNeedsRefresh = true;
1279  }
1280  }
1281  }
1282  }
1283  else
1284  {
1285  qWarning() << "Form that is part of calculate order doesn't have a calculate action";
1286  }
1287  }
1288  }
1289  if ( pageNeedsRefresh )
1290  {
1291  refreshPixmaps( p->number() );
1292  }
1293  }
1294  }
1295  }
1296 }
1297 
1298 void DocumentPrivate::saveDocumentInfo() const
1299 {
1300  if ( m_xmlFileName.isEmpty() )
1301  return;
1302 
1303  QFile infoFile( m_xmlFileName );
1304  qCDebug(OkularCoreDebug) << "About to save document info to" << m_xmlFileName;
1305  if (!infoFile.open( QIODevice::WriteOnly | QIODevice::Truncate))
1306  {
1307  qCWarning(OkularCoreDebug) << "Failed to open docdata file" << m_xmlFileName;
1308  return;
1309  }
1310  // 1. Create DOM
1311  QDomDocument doc( QStringLiteral("documentInfo") );
1312  QDomProcessingInstruction xmlPi = doc.createProcessingInstruction(
1313  QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"utf-8\"" ) );
1314  doc.appendChild( xmlPi );
1315  QDomElement root = doc.createElement( QStringLiteral("documentInfo") );
1316  root.setAttribute( QStringLiteral("url"), m_url.toDisplayString(QUrl::PreferLocalFile) );
1317  doc.appendChild( root );
1318 
1319  // 2.1. Save page attributes (bookmark state, annotations, ... ) to DOM
1320  // -> do this if there are not-yet-migrated annots or forms in docdata/
1321  if ( m_docdataMigrationNeeded )
1322  {
1323  QDomElement pageList = doc.createElement( QStringLiteral("pageList") );
1324  root.appendChild( pageList );
1325  // OriginalAnnotationPageItems and OriginalFormFieldPageItems tell to
1326  // store the same unmodified annotation list and form contents that we
1327  // read when we opened the file and ignore any change made by the user.
1328  // Since we don't store annotations and forms in docdata/ any more, this is
1329  // necessary to preserve annotations/forms that previous Okular version
1330  // had stored there.
1331  const PageItems saveWhat = AllPageItems | OriginalAnnotationPageItems | OriginalFormFieldPageItems;
1332  // <page list><page number='x'>.... </page> save pages that hold data
1333  QVector< Page * >::const_iterator pIt = m_pagesVector.constBegin(), pEnd = m_pagesVector.constEnd();
1334  for ( ; pIt != pEnd; ++pIt )
1335  (*pIt)->d->saveLocalContents( pageList, doc, saveWhat );
1336  }
1337 
1338  // 2.2. Save document info (current viewport, history, ... ) to DOM
1339  QDomElement generalInfo = doc.createElement( QStringLiteral("generalInfo") );
1340  root.appendChild( generalInfo );
1341  // create rotation node
1342  if ( m_rotation != Rotation0 )
1343  {
1344  QDomElement rotationNode = doc.createElement( QStringLiteral("rotation") );
1345  generalInfo.appendChild( rotationNode );
1346  rotationNode.appendChild( doc.createTextNode( QString::number( (int)m_rotation ) ) );
1347  }
1348  // <general info><history> ... </history> save history up to OKULAR_HISTORY_SAVEDSTEPS viewports
1349  const auto currentViewportIterator = QLinkedList< DocumentViewport >::const_iterator(m_viewportIterator);
1350  QLinkedList< DocumentViewport >::const_iterator backIterator = currentViewportIterator;
1351  if ( backIterator != m_viewportHistory.constEnd() )
1352  {
1353  // go back up to OKULAR_HISTORY_SAVEDSTEPS steps from the current viewportIterator
1354  int backSteps = OKULAR_HISTORY_SAVEDSTEPS;
1355  while ( backSteps-- && backIterator != m_viewportHistory.constBegin() )
1356  --backIterator;
1357 
1358  // create history root node
1359  QDomElement historyNode = doc.createElement( QStringLiteral("history") );
1360  generalInfo.appendChild( historyNode );
1361 
1362  // add old[backIterator] and present[viewportIterator] items
1363  QLinkedList< DocumentViewport >::const_iterator endIt = currentViewportIterator;
1364  ++endIt;
1365  while ( backIterator != endIt )
1366  {
1367  QString name = (backIterator == currentViewportIterator) ? QStringLiteral ("current") : QStringLiteral ("oldPage");
1368  QDomElement historyEntry = doc.createElement( name );
1369  historyEntry.setAttribute( QStringLiteral("viewport"), (*backIterator).toString() );
1370  historyNode.appendChild( historyEntry );
1371  ++backIterator;
1372  }
1373  }
1374  // create views root node
1375  QDomElement viewsNode = doc.createElement( QStringLiteral("views") );
1376  generalInfo.appendChild( viewsNode );
1377  for ( View *view : qAsConst(m_views) )
1378  {
1379  QDomElement viewEntry = doc.createElement( QStringLiteral("view") );
1380  viewEntry.setAttribute( QStringLiteral("name"), view->name() );
1381  viewsNode.appendChild( viewEntry );
1382  saveViewsInfo( view, viewEntry );
1383  }
1384 
1385  // 3. Save DOM to XML file
1386  QString xml = doc.toString();
1387  QTextStream os( &infoFile );
1388  os.setCodec( "UTF-8" );
1389  os << xml;
1390  infoFile.close();
1391 }
1392 
1393 void DocumentPrivate::slotTimedMemoryCheck()
1394 {
1395  // [MEM] clean memory (for 'free mem dependent' profiles only)
1396  if ( SettingsCore::memoryLevel() != SettingsCore::EnumMemoryLevel::Low &&
1397  m_allocatedPixmapsTotalMemory > 1024*1024 )
1398  cleanupPixmapMemory();
1399 }
1400 
1401 void DocumentPrivate::sendGeneratorPixmapRequest()
1402 {
1403  /* If the pixmap cache will have to be cleaned in order to make room for the
1404  * next request, get the distance from the current viewport of the page
1405  * whose pixmap will be removed. We will ignore preload requests for pages
1406  * that are at the same distance or farther */
1407  const qulonglong memoryToFree = calculateMemoryToFree();
1408  const int currentViewportPage = (*m_viewportIterator).pageNumber;
1409  int maxDistance = INT_MAX; // Default: No maximum
1410  if ( memoryToFree )
1411  {
1412  AllocatedPixmap *pixmapToReplace = searchLowestPriorityPixmap( true );
1413  if ( pixmapToReplace )
1414  maxDistance = qAbs( pixmapToReplace->page - currentViewportPage );
1415  }
1416 
1417  // find a request
1418  PixmapRequest * request = nullptr;
1419  m_pixmapRequestsMutex.lock();
1420  while ( !m_pixmapRequestsStack.isEmpty() && !request )
1421  {
1422  PixmapRequest * r = m_pixmapRequestsStack.last();
1423  if (!r)
1424  {
1425  m_pixmapRequestsStack.pop_back();
1426  continue;
1427  }
1428 
1429  QRect requestRect = r->isTile() ? r->normalizedRect().geometry( r->width(), r->height() ) : QRect( 0, 0, r->width(), r->height() );
1430  TilesManager *tilesManager = r->d->tilesManager();
1431  const double normalizedArea = r->normalizedRect().width() * r->normalizedRect().height();
1432  const QScreen *screen = nullptr;
1433  if (m_widget)
1434  {
1435  const QWindow *window = m_widget->window()->windowHandle();
1436  if (window)
1437  screen = window->screen();
1438  }
1439  if (!screen)
1440  screen = QGuiApplication::primaryScreen();
1441  const long screenSize = screen->devicePixelRatio() * screen->size().width()
1442  * screen->devicePixelRatio() * screen->size().height();
1443 
1444  // If it's a preload but the generator is not threaded no point in trying to preload
1445  if ( r->preload() && !m_generator->hasFeature( Generator::Threaded ) )
1446  {
1447  m_pixmapRequestsStack.pop_back();
1448  delete r;
1449  }
1450  // request only if page isn't already present and request has valid id
1451  else if ( ( !r->d->mForce && r->page()->hasPixmap( r->observer(), r->width(), r->height(), r->normalizedRect() ) ) || !m_observers.contains(r->observer()) )
1452  {
1453  m_pixmapRequestsStack.pop_back();
1454  delete r;
1455  }
1456  else if ( !r->d->mForce && r->preload() && qAbs( r->pageNumber() - currentViewportPage ) >= maxDistance )
1457  {
1458  m_pixmapRequestsStack.pop_back();
1459  //qCDebug(OkularCoreDebug) << "Ignoring request that doesn't fit in cache";
1460  delete r;
1461  }
1462  // Ignore requests for pixmaps that are already being generated
1463  else if ( tilesManager && tilesManager->isRequesting( r->normalizedRect(), r->width(), r->height() ) )
1464  {
1465  m_pixmapRequestsStack.pop_back();
1466  delete r;
1467  }
1468  // If the requested area is above 4*screenSize pixels, and we're not rendering most of the page, switch on the tile manager
1469  else if ( !tilesManager && m_generator->hasFeature( Generator::TiledRendering ) &&
1470  (long)r->width() * (long)r->height() > 4L*screenSize &&
1471  normalizedArea < 0.75 && normalizedArea != 0 )
1472  {
1473  // if the image is too big. start using tiles
1474  qCDebug(OkularCoreDebug).nospace() << "Start using tiles on page " << r->pageNumber()
1475  << " (" << r->width() << "x" << r->height() << " px);";
1476 
1477  // fill the tiles manager with the last rendered pixmap
1478  const QPixmap *pixmap = r->page()->_o_nearestPixmap( r->observer(), r->width(), r->height() );
1479  if ( pixmap )
1480  {
1481  tilesManager = new TilesManager( r->pageNumber(), pixmap->width(), pixmap->height(), r->page()->rotation() );
1482  tilesManager->setPixmap( pixmap, NormalizedRect( 0, 0, 1, 1 ), true /*isPartialPixmap*/ );
1483  tilesManager->setSize( r->width(), r->height() );
1484  }
1485  else
1486  {
1487  // create new tiles manager
1488  tilesManager = new TilesManager( r->pageNumber(), r->width(), r->height(), r->page()->rotation() );
1489  }
1490  tilesManager->setRequest( r->normalizedRect(), r->width(), r->height() );
1491  r->page()->deletePixmap( r->observer() );
1492  r->page()->d->setTilesManager( r->observer(), tilesManager );
1493  r->setTile( true );
1494 
1495  // Change normalizedRect to the smallest rect that contains all
1496  // visible tiles.
1497  if ( !r->normalizedRect().isNull() )
1498  {
1499  NormalizedRect tilesRect;
1500  const QList<Tile> tiles = tilesManager->tilesAt( r->normalizedRect(), TilesManager::TerminalTile );
1501  QList<Tile>::const_iterator tIt = tiles.constBegin(), tEnd = tiles.constEnd();
1502  while ( tIt != tEnd )
1503  {
1504  Tile tile = *tIt;
1505  if ( tilesRect.isNull() )
1506  tilesRect = tile.rect();
1507  else
1508  tilesRect |= tile.rect();
1509 
1510  ++tIt;
1511  }
1512 
1513  r->setNormalizedRect( tilesRect );
1514  request = r;
1515  }
1516  else
1517  {
1518  // Discard request if normalizedRect is null. This happens in
1519  // preload requests issued by PageView if the requested page is
1520  // not visible and the user has just switched from a non-tiled
1521  // zoom level to a tiled one
1522  m_pixmapRequestsStack.pop_back();
1523  delete r;
1524  }
1525  }
1526  // If the requested area is below 3*screenSize pixels, switch off the tile manager
1527  else if ( tilesManager && (long)r->width() * (long)r->height() < 3L*screenSize )
1528  {
1529  qCDebug(OkularCoreDebug).nospace() << "Stop using tiles on page " << r->pageNumber()
1530  << " (" << r->width() << "x" << r->height() << " px);";
1531 
1532  // page is too small. stop using tiles.
1533  r->page()->deletePixmap( r->observer() );
1534  r->setTile( false );
1535 
1536  request = r;
1537  }
1538  else if ( (long)requestRect.width() * (long)requestRect.height() > 100L*screenSize && (SettingsCore::memoryLevel() != SettingsCore::EnumMemoryLevel::Greedy ) )
1539  {
1540  m_pixmapRequestsStack.pop_back();
1541  if ( !m_warnedOutOfMemory )
1542  {
1543  qCWarning(OkularCoreDebug).nospace() << "Running out of memory on page " << r->pageNumber()
1544  << " (" << r->width() << "x" << r->height() << " px);";
1545  qCWarning(OkularCoreDebug) << "this message will be reported only once.";
1546  m_warnedOutOfMemory = true;
1547  }
1548  delete r;
1549  }
1550  else
1551  {
1552  request = r;
1553  }
1554  }
1555 
1556  // if no request found (or already generated), return
1557  if ( !request )
1558  {
1559  m_pixmapRequestsMutex.unlock();
1560  return;
1561  }
1562 
1563  // [MEM] preventive memory freeing
1564  qulonglong pixmapBytes = 0;
1565  TilesManager * tm = request->d->tilesManager();
1566  if ( tm )
1567  pixmapBytes = tm->totalMemory();
1568  else
1569  pixmapBytes = 4 * request->width() * request->height();
1570 
1571  if ( pixmapBytes > (1024 * 1024) )
1572  cleanupPixmapMemory( memoryToFree /* previously calculated value */ );
1573 
1574  // submit the request to the generator
1575  if ( m_generator->canGeneratePixmap() )
1576  {
1577  QRect requestRect = !request->isTile() ? QRect(0, 0, request->width(), request->height() ) : request->normalizedRect().geometry( request->width(), request->height() );
1578  qCDebug(OkularCoreDebug).nospace() << "sending request observer=" << request->observer() << " " <<requestRect.width() << "x" << requestRect.height() << "@" << request->pageNumber() << " async == " << request->asynchronous() << " isTile == " << request->isTile();
1579  m_pixmapRequestsStack.removeAll ( request );
1580 
1581  if ( tm )
1582  tm->setRequest( request->normalizedRect(), request->width(), request->height() );
1583 
1584  if ( (int)m_rotation % 2 )
1585  request->d->swap();
1586 
1587  if ( m_rotation != Rotation0 && !request->normalizedRect().isNull() )
1588  request->setNormalizedRect( TilesManager::fromRotatedRect(
1589  request->normalizedRect(), m_rotation ) );
1590 
1591  // If set elsewhere we already know we want it to be partial
1592  if ( !request->partialUpdatesWanted() )
1593  {
1594  request->setPartialUpdatesWanted( request->asynchronous() && !request->page()->hasPixmap( request->observer() ) );
1595  }
1596 
1597  // we always have to unlock _before_ the generatePixmap() because
1598  // a sync generation would end with requestDone() -> deadlock, and
1599  // we can not really know if the generator can do async requests
1600  m_executingPixmapRequests.push_back( request );
1601  m_pixmapRequestsMutex.unlock();
1602  m_generator->generatePixmap( request );
1603  }
1604  else
1605  {
1606  m_pixmapRequestsMutex.unlock();
1607  // pino (7/4/2006): set the polling interval from 10 to 30
1608  QTimer::singleShot( 30, m_parent, [this] { sendGeneratorPixmapRequest(); } );
1609  }
1610 }
1611 
1612 void DocumentPrivate::rotationFinished( int page, Okular::Page *okularPage )
1613 {
1614  Okular::Page *wantedPage = m_pagesVector.value( page, 0 );
1615  if ( !wantedPage || wantedPage != okularPage )
1616  return;
1617 
1618  foreach(DocumentObserver *o, m_observers)
1620 }
1621 
1622 void DocumentPrivate::slotFontReadingProgress( int page )
1623 {
1624  emit m_parent->fontReadingProgress( page );
1625 
1626  if ( page >= (int)m_parent->pages() - 1 )
1627  {
1628  emit m_parent->fontReadingEnded();
1629  m_fontThread = nullptr;
1630  m_fontsCached = true;
1631  }
1632 }
1633 
1634 void DocumentPrivate::fontReadingGotFont( const Okular::FontInfo& font )
1635 {
1636  // Try to avoid duplicate fonts
1637  if (m_fontsCache.indexOf(font) == -1) {
1638  m_fontsCache.append( font );
1639 
1640  emit m_parent->gotFont( font );
1641  }
1642 }
1643 
1644 void DocumentPrivate::slotGeneratorConfigChanged()
1645 {
1646  if ( !m_generator )
1647  return;
1648 
1649  // reparse generator config and if something changed clear Pages
1650  bool configchanged = false;
1651  QHash< QString, GeneratorInfo >::iterator it = m_loadedGenerators.begin(), itEnd = m_loadedGenerators.end();
1652  for ( ; it != itEnd; ++it )
1653  {
1654  Okular::ConfigInterface * iface = generatorConfig( it.value() );
1655  if ( iface )
1656  {
1657  bool it_changed = iface->reparseConfig();
1658  if ( it_changed && ( m_generator == it.value().generator ) )
1659  configchanged = true;
1660  }
1661  }
1662  if ( configchanged )
1663  {
1664  // invalidate pixmaps
1665  QVector<Page*>::const_iterator it = m_pagesVector.constBegin(), end = m_pagesVector.constEnd();
1666  for ( ; it != end; ++it ) {
1667  (*it)->deletePixmaps();
1668  }
1669 
1670  // [MEM] remove allocation descriptors
1671  qDeleteAll( m_allocatedPixmaps );
1672  m_allocatedPixmaps.clear();
1673  m_allocatedPixmapsTotalMemory = 0;
1674 
1675  // send reload signals to observers
1676  foreachObserverD( notifyContentsCleared( DocumentObserver::Pixmap ) );
1677  }
1678 
1679  // free memory if in 'low' profile
1680  if ( SettingsCore::memoryLevel() == SettingsCore::EnumMemoryLevel::Low &&
1681  !m_allocatedPixmaps.isEmpty() && !m_pagesVector.isEmpty() )
1682  cleanupPixmapMemory();
1683 }
1684 
1685 void DocumentPrivate::refreshPixmaps( int pageNumber )
1686 {
1687  Page* page = m_pagesVector.value( pageNumber, 0 );
1688  if ( !page )
1689  return;
1690 
1691  QMap< DocumentObserver*, PagePrivate::PixmapObject >::ConstIterator it = page->d->m_pixmaps.constBegin(), itEnd = page->d->m_pixmaps.constEnd();
1692  QVector< Okular::PixmapRequest * > pixmapsToRequest;
1693  for ( ; it != itEnd; ++it )
1694  {
1695  const QSize size = (*it).m_pixmap->size();
1696  PixmapRequest * p = new PixmapRequest( it.key(), pageNumber, size.width() / qApp->devicePixelRatio(), size.height() / qApp->devicePixelRatio(), 1, PixmapRequest::Asynchronous );
1697  p->d->mForce = true;
1698  pixmapsToRequest << p;
1699  }
1700 
1701  // Need to do this ↑↓ in two steps since requestPixmaps can end up calling cancelRenderingBecauseOf
1702  // which changes m_pixmaps and thus breaks the loop above
1703  for ( PixmapRequest *pr : qAsConst( pixmapsToRequest ) )
1704  {
1705  QLinkedList< Okular::PixmapRequest * > requestedPixmaps;
1706  requestedPixmaps.push_back( pr );
1707  m_parent->requestPixmaps( requestedPixmaps, Okular::Document::NoOption );
1708  }
1709 
1710  for (DocumentObserver *observer : qAsConst(m_observers))
1711  {
1712  QLinkedList< Okular::PixmapRequest * > requestedPixmaps;
1713 
1714  TilesManager *tilesManager = page->d->tilesManager( observer );
1715  if ( tilesManager )
1716  {
1717  tilesManager->markDirty();
1718 
1719  PixmapRequest * p = new PixmapRequest( observer, pageNumber, tilesManager->width() / qApp->devicePixelRatio(), tilesManager->height() / qApp->devicePixelRatio(), 1, PixmapRequest::Asynchronous );
1720 
1721  // Get the visible page rect
1722  NormalizedRect visibleRect;
1723  QVector< Okular::VisiblePageRect * >::const_iterator vIt = m_pageRects.constBegin(), vEnd = m_pageRects.constEnd();
1724  for ( ; vIt != vEnd; ++vIt )
1725  {
1726  if ( (*vIt)->pageNumber == pageNumber )
1727  {
1728  visibleRect = (*vIt)->rect;
1729  break;
1730  }
1731  }
1732 
1733  if ( !visibleRect.isNull() )
1734  {
1735  p->setNormalizedRect( visibleRect );
1736  p->setTile( true );
1737  p->d->mForce = true;
1738  requestedPixmaps.push_back( p );
1739  }
1740  else
1741  {
1742  delete p;
1743  }
1744  }
1745 
1746  m_parent->requestPixmaps( requestedPixmaps, Okular::Document::NoOption );
1747  }
1748 
1749 }
1750 
1751 void DocumentPrivate::_o_configChanged()
1752 {
1753  // free text pages if needed
1754  calculateMaxTextPages();
1755  while (m_allocatedTextPagesFifo.count() > m_maxAllocatedTextPages)
1756  {
1757  int pageToKick = m_allocatedTextPagesFifo.takeFirst();
1758  m_pagesVector.at(pageToKick)->setTextPage( nullptr ); // deletes the textpage
1759  }
1760 }
1761 
1762 void DocumentPrivate::doContinueDirectionMatchSearch(void *doContinueDirectionMatchSearchStruct)
1763 {
1764  DoContinueDirectionMatchSearchStruct *searchStruct = static_cast<DoContinueDirectionMatchSearchStruct *>(doContinueDirectionMatchSearchStruct);
1765  RunningSearch *search = m_searches.value(searchStruct->searchID);
1766 
1767  if ((m_searchCancelled && !searchStruct->match) || !search)
1768  {
1769  // if the user cancelled but he just got a match, give him the match!
1771 
1772  if (search) search->isCurrentlySearching = false;
1773 
1774  emit m_parent->searchFinished( searchStruct->searchID, Document::SearchCancelled );
1775  delete searchStruct->pagesToNotify;
1776  delete searchStruct;
1777  return;
1778  }
1779 
1780  const bool forward = search->cachedType == Document::NextMatch;
1781  bool doContinue = false;
1782  // if no match found, loop through the whole doc, starting from currentPage
1783  if ( !searchStruct->match )
1784  {
1785  const int pageCount = m_pagesVector.count();
1786  if (search->pagesDone < pageCount)
1787  {
1788  doContinue = true;
1789  if ( searchStruct->currentPage >= pageCount )
1790  {
1791  searchStruct->currentPage = 0;
1792  emit m_parent->notice(i18n("Continuing search from beginning"), 3000);
1793  }
1794  else if ( searchStruct->currentPage < 0 )
1795  {
1796  searchStruct->currentPage = pageCount - 1;
1797  emit m_parent->notice(i18n("Continuing search from bottom"), 3000);
1798  }
1799  }
1800  }
1801 
1802  if (doContinue)
1803  {
1804  // get page
1805  Page * page = m_pagesVector[ searchStruct->currentPage ];
1806  // request search page if needed
1807  if ( !page->hasTextPage() )
1808  m_parent->requestTextPage( page->number() );
1809 
1810  // if found a match on the current page, end the loop
1811  searchStruct->match = page->findText( searchStruct->searchID, search->cachedString, forward ? FromTop : FromBottom, search->cachedCaseSensitivity );
1812  if ( !searchStruct->match )
1813  {
1814  if (forward) searchStruct->currentPage++;
1815  else searchStruct->currentPage--;
1816  search->pagesDone++;
1817  }
1818  else
1819  {
1820  search->pagesDone = 1;
1821  }
1822 
1823  // Both of the previous if branches need to call doContinueDirectionMatchSearch
1824  QTimer::singleShot(0, m_parent, [this, searchStruct] { doContinueDirectionMatchSearch(searchStruct); });
1825  }
1826  else
1827  {
1828  doProcessSearchMatch( searchStruct->match, search, searchStruct->pagesToNotify, searchStruct->currentPage, searchStruct->searchID, search->cachedViewportMove, search->cachedColor );
1829  delete searchStruct;
1830  }
1831 }
1832 
1833 void DocumentPrivate::doProcessSearchMatch( RegularAreaRect *match, RunningSearch *search, QSet< int > *pagesToNotify, int currentPage, int searchID, bool moveViewport, const QColor & color )
1834 {
1835  // reset cursor to previous shape
1837 
1838  bool foundAMatch = false;
1839 
1840  search->isCurrentlySearching = false;
1841 
1842  // if a match has been found..
1843  if ( match )
1844  {
1845  // update the RunningSearch structure adding this match..
1846  foundAMatch = true;
1847  search->continueOnPage = currentPage;
1848  search->continueOnMatch = *match;
1849  search->highlightedPages.insert( currentPage );
1850  // ..add highlight to the page..
1851  m_pagesVector[ currentPage ]->d->setHighlight( searchID, match, color );
1852 
1853  // ..queue page for notifying changes..
1854  pagesToNotify->insert( currentPage );
1855 
1856  // Create a normalized rectangle around the search match that includes a 5% buffer on all sides.
1857  const Okular::NormalizedRect matchRectWithBuffer = Okular::NormalizedRect( match->first().left - 0.05,
1858  match->first().top - 0.05,
1859  match->first().right + 0.05,
1860  match->first().bottom + 0.05 );
1861 
1862  const bool matchRectFullyVisible = isNormalizedRectangleFullyVisible( matchRectWithBuffer, currentPage );
1863 
1864  // ..move the viewport to show the first of the searched word sequence centered
1865  if ( moveViewport && !matchRectFullyVisible )
1866  {
1867  DocumentViewport searchViewport( currentPage );
1868  searchViewport.rePos.enabled = true;
1869  searchViewport.rePos.normalizedX = (match->first().left + match->first().right) / 2.0;
1870  searchViewport.rePos.normalizedY = (match->first().top + match->first().bottom) / 2.0;
1871  m_parent->setViewport( searchViewport, nullptr, true );
1872  }
1873  delete match;
1874  }
1875 
1876  // notify observers about highlights changes
1877  foreach(int pageNumber, *pagesToNotify)
1878  foreach(DocumentObserver *observer, m_observers)
1879  observer->notifyPageChanged( pageNumber, DocumentObserver::Highlights );
1880 
1881  if (foundAMatch) emit m_parent->searchFinished( searchID, Document::MatchFound );
1882  else emit m_parent->searchFinished( searchID, Document::NoMatchFound );
1883 
1884  delete pagesToNotify;
1885 }
1886 
1887 void DocumentPrivate::doContinueAllDocumentSearch(void *pagesToNotifySet, void *pageMatchesMap, int currentPage, int searchID)
1888 {
1889  QMap< Page *, QVector<RegularAreaRect *> > *pageMatches = static_cast< QMap< Page *, QVector<RegularAreaRect *> > * >(pageMatchesMap);
1890  QSet< int > *pagesToNotify = static_cast< QSet< int > * >( pagesToNotifySet );
1891  RunningSearch *search = m_searches.value(searchID);
1892 
1893  if (m_searchCancelled || !search)
1894  {
1895  typedef QVector<RegularAreaRect *> MatchesVector;
1896 
1898 
1899  if (search) search->isCurrentlySearching = false;
1900 
1901  emit m_parent->searchFinished( searchID, Document::SearchCancelled );
1902  foreach(const MatchesVector &mv, *pageMatches) qDeleteAll(mv);
1903  delete pageMatches;
1904  delete pagesToNotify;
1905  return;
1906  }
1907 
1908  if (currentPage < m_pagesVector.count())
1909  {
1910  // get page (from the first to the last)
1911  Page *page = m_pagesVector.at(currentPage);
1912  int pageNumber = page->number(); // redundant? is it == currentPage ?
1913 
1914  // request search page if needed
1915  if ( !page->hasTextPage() )
1916  m_parent->requestTextPage( pageNumber );
1917 
1918  // loop on a page adding highlights for all found items
1919  RegularAreaRect * lastMatch = nullptr;
1920  while ( true )
1921  {
1922  if ( lastMatch )
1923  lastMatch = page->findText( searchID, search->cachedString, NextResult, search->cachedCaseSensitivity, lastMatch );
1924  else
1925  lastMatch = page->findText( searchID, search->cachedString, FromTop, search->cachedCaseSensitivity );
1926 
1927  if ( !lastMatch )
1928  break;
1929 
1930  // add highlight rect to the matches map
1931  (*pageMatches)[page].append(lastMatch);
1932  }
1933  delete lastMatch;
1934 
1935  QTimer::singleShot(0, m_parent, [this, pagesToNotifySet, pageMatches, currentPage, searchID] { doContinueAllDocumentSearch(pagesToNotifySet, pageMatches, currentPage + 1, searchID); });
1936  }
1937  else
1938  {
1939  // reset cursor to previous shape
1941 
1942  search->isCurrentlySearching = false;
1943  bool foundAMatch = pageMatches->count() != 0;
1944  QMap< Page *, QVector<RegularAreaRect *> >::const_iterator it, itEnd;
1945  it = pageMatches->constBegin();
1946  itEnd = pageMatches->constEnd();
1947  for ( ; it != itEnd; ++it)
1948  {
1949  foreach(RegularAreaRect *match, it.value())
1950  {
1951  it.key()->d->setHighlight( searchID, match, search->cachedColor );
1952  delete match;
1953  }
1954  search->highlightedPages.insert( it.key()->number() );
1955  pagesToNotify->insert( it.key()->number() );
1956  }
1957 
1958  foreach(DocumentObserver *observer, m_observers)
1959  observer->notifySetup( m_pagesVector, 0 );
1960 
1961  // notify observers about highlights changes
1962  foreach(int pageNumber, *pagesToNotify)
1963  foreach(DocumentObserver *observer, m_observers)
1964  observer->notifyPageChanged( pageNumber, DocumentObserver::Highlights );
1965 
1966  if (foundAMatch) emit m_parent->searchFinished(searchID, Document::MatchFound );
1967  else emit m_parent->searchFinished( searchID, Document::NoMatchFound );
1968 
1969  delete pageMatches;
1970  delete pagesToNotify;
1971  }
1972 }
1973 
1974 void DocumentPrivate::doContinueGooglesDocumentSearch(void *pagesToNotifySet, void *pageMatchesMap, int currentPage, int searchID, const QStringList & words)
1975 {
1976  typedef QPair<RegularAreaRect *, QColor> MatchColor;
1977  QMap< Page *, QVector<MatchColor> > *pageMatches = static_cast< QMap< Page *, QVector<MatchColor> > * >(pageMatchesMap);
1978  QSet< int > *pagesToNotify = static_cast< QSet< int > * >( pagesToNotifySet );
1979  RunningSearch *search = m_searches.value(searchID);
1980 
1981  if (m_searchCancelled || !search)
1982  {
1983  typedef QVector<MatchColor> MatchesVector;
1984 
1986 
1987  if (search) search->isCurrentlySearching = false;
1988 
1989  emit m_parent->searchFinished( searchID, Document::SearchCancelled );
1990 
1991  foreach(const MatchesVector &mv, *pageMatches)
1992  {
1993  foreach(const MatchColor &mc, mv) delete mc.first;
1994  }
1995  delete pageMatches;
1996  delete pagesToNotify;
1997  return;
1998  }
1999 
2000  const int wordCount = words.count();
2001  const int hueStep = (wordCount > 1) ? (60 / (wordCount - 1)) : 60;
2002  int baseHue, baseSat, baseVal;
2003  search->cachedColor.getHsv( &baseHue, &baseSat, &baseVal );
2004 
2005  if (currentPage < m_pagesVector.count())
2006  {
2007  // get page (from the first to the last)
2008  Page *page = m_pagesVector.at(currentPage);
2009  int pageNumber = page->number(); // redundant? is it == currentPage ?
2010 
2011  // request search page if needed
2012  if ( !page->hasTextPage() )
2013  m_parent->requestTextPage( pageNumber );
2014 
2015  // loop on a page adding highlights for all found items
2016  bool allMatched = wordCount > 0,
2017  anyMatched = false;
2018  for ( int w = 0; w < wordCount; w++ )
2019  {
2020  const QString &word = words[ w ];
2021  int newHue = baseHue - w * hueStep;
2022  if ( newHue < 0 )
2023  newHue += 360;
2024  QColor wordColor = QColor::fromHsv( newHue, baseSat, baseVal );
2025  RegularAreaRect * lastMatch = nullptr;
2026  // add all highlights for current word
2027  bool wordMatched = false;
2028  while ( true )
2029  {
2030  if ( lastMatch )
2031  lastMatch = page->findText( searchID, word, NextResult, search->cachedCaseSensitivity, lastMatch );
2032  else
2033  lastMatch = page->findText( searchID, word, FromTop, search->cachedCaseSensitivity);
2034 
2035  if ( !lastMatch )
2036  break;
2037 
2038  // add highligh rect to the matches map
2039  (*pageMatches)[page].append(MatchColor(lastMatch, wordColor));
2040  wordMatched = true;
2041  }
2042  allMatched = allMatched && wordMatched;
2043  anyMatched = anyMatched || wordMatched;
2044  }
2045 
2046  // if not all words are present in page, remove partial highlights
2047  const bool matchAll = search->cachedType == Document::GoogleAll;
2048  if ( !allMatched && matchAll )
2049  {
2050  QVector<MatchColor> &matches = (*pageMatches)[page];
2051  foreach(const MatchColor &mc, matches) delete mc.first;
2052  pageMatches->remove(page);
2053  }
2054 
2055  QTimer::singleShot(0, m_parent, [this, pagesToNotifySet, pageMatches, currentPage, searchID, words] { doContinueGooglesDocumentSearch(pagesToNotifySet, pageMatches, currentPage + 1, searchID, words); });
2056  }
2057  else
2058  {
2059  // reset cursor to previous shape
2061 
2062  search->isCurrentlySearching = false;
2063  bool foundAMatch = pageMatches->count() != 0;
2064  QMap< Page *, QVector<MatchColor> >::const_iterator it, itEnd;
2065  it = pageMatches->constBegin();
2066  itEnd = pageMatches->constEnd();
2067  for ( ; it != itEnd; ++it)
2068  {
2069  foreach(const MatchColor &mc, it.value())
2070  {
2071  it.key()->d->setHighlight( searchID, mc.first, mc.second );
2072  delete mc.first;
2073  }
2074  search->highlightedPages.insert( it.key()->number() );
2075  pagesToNotify->insert( it.key()->number() );
2076  }
2077 
2078  // send page lists to update observers (since some filter on bookmarks)
2079  foreach(DocumentObserver *observer, m_observers)
2080  observer->notifySetup( m_pagesVector, 0 );
2081 
2082  // notify observers about highlights changes
2083  foreach(int pageNumber, *pagesToNotify)
2084  foreach(DocumentObserver *observer, m_observers)
2085  observer->notifyPageChanged( pageNumber, DocumentObserver::Highlights );
2086 
2087  if (foundAMatch) emit m_parent->searchFinished( searchID, Document::MatchFound );
2088  else emit m_parent->searchFinished( searchID, Document::NoMatchFound );
2089 
2090  delete pageMatches;
2091  delete pagesToNotify;
2092  }
2093 }
2094 
2095 QVariant DocumentPrivate::documentMetaData( const Generator::DocumentMetaDataKey key, const QVariant &option ) const
2096 {
2097  switch ( key )
2098  {
2100  {
2101  bool giveDefault = option.toBool();
2102  QColor color;
2103  if ( ( SettingsCore::renderMode() == SettingsCore::EnumRenderMode::Paper )
2104  && SettingsCore::changeColors() )
2105  {
2106  color = SettingsCore::paperColor();
2107  }
2108  else if ( giveDefault )
2109  {
2110  color = Qt::white;
2111  }
2112  return color;
2113  }
2114  break;
2115 
2117  switch ( SettingsCore::textAntialias() )
2118  {
2119  case SettingsCore::EnumTextAntialias::Enabled:
2120  return true;
2121  break;
2122 #if 0
2123  case Settings::EnumTextAntialias::UseKDESettings:
2124  // TODO: read the KDE configuration
2125  return true;
2126  break;
2127 #endif
2128  case SettingsCore::EnumTextAntialias::Disabled:
2129  return false;
2130  break;
2131  }
2132  break;
2133 
2135  switch ( SettingsCore::graphicsAntialias() )
2136  {
2137  case SettingsCore::EnumGraphicsAntialias::Enabled:
2138  return true;
2139  break;
2140  case SettingsCore::EnumGraphicsAntialias::Disabled:
2141  return false;
2142  break;
2143  }
2144  break;
2145 
2147  switch ( SettingsCore::textHinting() )
2148  {
2149  case SettingsCore::EnumTextHinting::Enabled:
2150  return true;
2151  break;
2152  case SettingsCore::EnumTextHinting::Disabled:
2153  return false;
2154  break;
2155  }
2156  break;
2157  }
2158  return QVariant();
2159 }
2160 
2161 bool DocumentPrivate::isNormalizedRectangleFullyVisible( const Okular::NormalizedRect & rectOfInterest, int rectPage )
2162 {
2163  bool rectFullyVisible = false;
2164  const QVector<Okular::VisiblePageRect *> & visibleRects = m_parent->visiblePageRects();
2167 
2168  for ( ; ( vIt != vEnd ) && !rectFullyVisible; ++vIt )
2169  {
2170  if ( (*vIt)->pageNumber == rectPage &&
2171  (*vIt)->rect.contains( rectOfInterest.left, rectOfInterest.top ) &&
2172  (*vIt)->rect.contains( rectOfInterest.right, rectOfInterest.bottom ) )
2173  {
2174  rectFullyVisible = true;
2175  }
2176  }
2177  return rectFullyVisible;
2178 }
2179 
2180 struct pdfsyncpoint
2181 {
2182  QString file;
2183  qlonglong x;
2184  qlonglong y;
2185  int row;
2186  int column;
2187  int page;
2188 };
2189 
2190 void DocumentPrivate::loadSyncFile( const QString & filePath )
2191 {
2192  QFile f( filePath + QLatin1String( "sync" ) );
2193  if ( !f.open( QIODevice::ReadOnly ) )
2194  return;
2195 
2196  QTextStream ts( &f );
2197  // first row: core name of the pdf output
2198  const QString coreName = ts.readLine();
2199  // second row: version string, in the form 'Version %u'
2200  const QString versionstr = ts.readLine();
2201  // anchor the pattern with \A and \z to match the entire subject string
2202  // TODO: with Qt 5.12 QRegularExpression::anchoredPattern() can be used instead
2203  QRegularExpression versionre( QStringLiteral("\\AVersion \\d+\\z")
2204  , QRegularExpression::CaseInsensitiveOption );
2205  QRegularExpressionMatch match = versionre.match( versionstr );
2206  if ( !match.hasMatch() ) {
2207  return;
2208  }
2209 
2210  QHash<int, pdfsyncpoint> points;
2211  QStack<QString> fileStack;
2212  int currentpage = -1;
2213  const QLatin1String texStr( ".tex" );
2214  const QChar spaceChar = QChar::fromLatin1( ' ' );
2215 
2216  fileStack.push( coreName + texStr );
2217 
2218  const QSizeF dpi = m_generator->dpi();
2219 
2220  QString line;
2221  while ( !ts.atEnd() )
2222  {
2223  line = ts.readLine();
2224  const QStringList tokens = line.split( spaceChar, QString::SkipEmptyParts );
2225  const int tokenSize = tokens.count();
2226  if ( tokenSize < 1 )
2227  continue;
2228  if ( tokens.first() == QLatin1String( "l" ) && tokenSize >= 3 )
2229  {
2230  int id = tokens.at( 1 ).toInt();
2232  if ( it == points.constEnd() )
2233  {
2234  pdfsyncpoint pt;
2235  pt.x = 0;
2236  pt.y = 0;
2237  pt.row = tokens.at( 2 ).toInt();
2238  pt.column = 0; // TODO
2239  pt.page = -1;
2240  pt.file = fileStack.top();
2241  points[ id ] = pt;
2242  }
2243  }
2244  else if ( tokens.first() == QLatin1String( "s" ) && tokenSize >= 2 )
2245  {
2246  currentpage = tokens.at( 1 ).toInt() - 1;
2247  }
2248  else if ( tokens.first() == QLatin1String( "p*" ) && tokenSize >= 4 )
2249  {
2250  // TODO
2251  qCDebug(OkularCoreDebug) << "PdfSync: 'p*' line ignored";
2252  }
2253  else if ( tokens.first() == QLatin1String( "p" ) && tokenSize >= 4 )
2254  {
2255  int id = tokens.at( 1 ).toInt();
2256  QHash<int, pdfsyncpoint>::iterator it = points.find( id );
2257  if ( it != points.end() )
2258  {
2259  it->x = tokens.at( 2 ).toInt();
2260  it->y = tokens.at( 3 ).toInt();
2261  it->page = currentpage;
2262  }
2263  }
2264  else if ( line.startsWith( QLatin1Char( '(' ) ) && tokenSize == 1 )
2265  {
2266  QString newfile = line;
2267  // chop the leading '('
2268  newfile.remove( 0, 1 );
2269  if ( !newfile.endsWith( texStr ) )
2270  {
2271  newfile += texStr;
2272  }
2273  fileStack.push( newfile );
2274  }
2275  else if ( line == QLatin1String( ")" ) )
2276  {
2277  if ( !fileStack.isEmpty() )
2278  {
2279  fileStack.pop();
2280  }
2281  else
2282  qCDebug(OkularCoreDebug) << "PdfSync: going one level down too much";
2283  }
2284  else
2285  qCDebug(OkularCoreDebug).nospace() << "PdfSync: unknown line format: '" << line << "'";
2286 
2287  }
2288 
2289  QVector< QLinkedList< Okular::SourceRefObjectRect * > > refRects( m_pagesVector.size() );
2290  for ( const pdfsyncpoint &pt : qAsConst(points) )
2291  {
2292  // drop pdfsync points not completely valid
2293  if ( pt.page < 0 || pt.page >= m_pagesVector.size() )
2294  continue;
2295 
2296  // magic numbers for TeX's RSU's (Ridiculously Small Units) conversion to pixels
2298  ( pt.x * dpi.width() ) / ( 72.27 * 65536.0 * m_pagesVector[pt.page]->width() ),
2299  ( pt.y * dpi.height() ) / ( 72.27 * 65536.0 * m_pagesVector[pt.page]->height() )
2300  );
2301  QString file = pt.file;
2302  Okular::SourceReference * sourceRef = new Okular::SourceReference( file, pt.row, pt.column );
2303  refRects[ pt.page ].append( new Okular::SourceRefObjectRect( p, sourceRef ) );
2304  }
2305  for ( int i = 0; i < refRects.size(); ++i )
2306  if ( !refRects.at(i).isEmpty() )
2307  m_pagesVector[i]->setSourceReferences( refRects.at(i) );
2308 }
2309 
2310 void DocumentPrivate::clearAndWaitForRequests()
2311 {
2312  m_pixmapRequestsMutex.lock();
2313  QLinkedList< PixmapRequest * >::const_iterator sIt = m_pixmapRequestsStack.constBegin();
2314  QLinkedList< PixmapRequest * >::const_iterator sEnd = m_pixmapRequestsStack.constEnd();
2315  for ( ; sIt != sEnd; ++sIt )
2316  delete *sIt;
2317  m_pixmapRequestsStack.clear();
2318  m_pixmapRequestsMutex.unlock();
2319 
2320  QEventLoop loop;
2321  bool startEventLoop = false;
2322  do
2323  {
2324  m_pixmapRequestsMutex.lock();
2325  startEventLoop = !m_executingPixmapRequests.isEmpty();
2326 
2327  if ( m_generator->hasFeature( Generator::SupportsCancelling ) )
2328  {
2329  for ( PixmapRequest *executingRequest : qAsConst( m_executingPixmapRequests ) )
2330  executingRequest->d->mShouldAbortRender = 1;
2331 
2332  if ( m_generator->d_ptr->mTextPageGenerationThread )
2333  m_generator->d_ptr->mTextPageGenerationThread->abortExtraction();
2334  }
2335 
2336  m_pixmapRequestsMutex.unlock();
2337  if ( startEventLoop )
2338  {
2339  m_closingLoop = &loop;
2340  loop.exec();
2341  m_closingLoop = nullptr;
2342  }
2343  }
2344  while ( startEventLoop );
2345 }
2346 
2347 int DocumentPrivate::findFieldPageNumber( Okular::FormField *field )
2348 {
2349  // Lookup the page of the FormField
2350  int foundPage = -1;
2351  for ( uint pageIdx = 0, nPages = m_parent->pages(); pageIdx < nPages; pageIdx++ )
2352  {
2353  const Page *p = m_parent->page( pageIdx );
2354  if ( p && p->formFields().contains( field ) )
2355  {
2356  foundPage = static_cast< int >( pageIdx );
2357  break;
2358  }
2359  }
2360  return foundPage;
2361 }
2362 
2363 void DocumentPrivate::executeScriptEvent( const std::shared_ptr< Event > &event, const Okular::ScriptAction * linkscript )
2364 {
2365  if ( !m_scripter )
2366  {
2367  m_scripter = new Scripter( this );
2368  }
2369  m_scripter->setEvent( event.get() );
2370  m_scripter->execute( linkscript->scriptType(), linkscript->script() );
2371 
2372  // Clear out the event after execution
2373  m_scripter->setEvent( nullptr );
2374 }
2375 
2376 
2378  : QObject( nullptr ), d( new DocumentPrivate( this ) )
2379 {
2380  d->m_widget = widget;
2381  d->m_bookmarkManager = new BookmarkManager( d );
2382  d->m_viewportIterator = d->m_viewportHistory.insert( d->m_viewportHistory.end(), DocumentViewport() );
2383  d->m_undoStack = new QUndoStack(this);
2384 
2385  connect( SettingsCore::self(), &SettingsCore::configChanged, this, [this] { d->_o_configChanged(); } );
2389 
2390  qRegisterMetaType<Okular::FontInfo>();
2391 }
2392 
2394 {
2395  // delete generator, pages, and related stuff
2396  closeDocument();
2397 
2398  QSet< View * >::const_iterator viewIt = d->m_views.constBegin(), viewEnd = d->m_views.constEnd();
2399  for ( ; viewIt != viewEnd; ++viewIt )
2400  {
2401  View *v = *viewIt;
2402  v->d_func()->document = nullptr;
2403  }
2404 
2405  // delete the bookmark manager
2406  delete d->m_bookmarkManager;
2407 
2408  // delete the loaded generators
2409  QHash< QString, GeneratorInfo >::const_iterator it = d->m_loadedGenerators.constBegin(), itEnd = d->m_loadedGenerators.constEnd();
2410  for ( ; it != itEnd; ++it )
2411  d->unloadGenerator( it.value() );
2412  d->m_loadedGenerators.clear();
2413 
2414  // delete the private structure
2415  delete d;
2416 }
2417 
2418 QString DocumentPrivate::docDataFileName(const QUrl &url, qint64 document_size)
2419 {
2420 
2421  QString fn = url.fileName();
2422  fn = QString::number( document_size ) + QLatin1Char('.') + fn + QStringLiteral(".xml");
2423  QString docdataDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
2424  + QStringLiteral("/okular/docdata");
2425  // make sure that the okular/docdata/ directory exists (probably this used to be handled by KStandardDirs)
2426  if (!QFileInfo::exists(docdataDir))
2427  {
2428  qCDebug(OkularCoreDebug) << "creating docdata folder" << docdataDir;
2429  QDir().mkpath(docdataDir);
2430  }
2431  QString newokularfile = docdataDir + QLatin1Char('/') + fn;
2432  // we don't want to accidentally migrate old files when running unit tests
2433  if (!QFile::exists( newokularfile ) && !QStandardPaths::isTestModeEnabled())
2434  {
2435  // see if an KDE4 file still exists
2436  static Kdelibs4Migration k4migration;
2437  QString oldfile = k4migration.locateLocal("data", QStringLiteral("okular/docdata/") + fn);
2438  if (oldfile.isEmpty())
2439  {
2440  oldfile = k4migration.locateLocal("data", QStringLiteral("kpdf/") + fn);
2441  }
2442  if ( !oldfile.isEmpty() && QFile::exists( oldfile ) )
2443  {
2444  // ### copy or move?
2445  if ( !QFile::copy( oldfile, newokularfile ) )
2446  return QString();
2447  }
2448  }
2449  return newokularfile;
2450 }
2451 
2452 QVector<KPluginMetaData> DocumentPrivate::availableGenerators()
2453 {
2454  static QVector<KPluginMetaData> result;
2455  if (result.isEmpty())
2456  {
2457  result = KPluginLoader::findPlugins( QStringLiteral ( "okular/generators" ) );
2458  }
2459  return result;
2460 }
2461 
2462 KPluginMetaData DocumentPrivate::generatorForMimeType(const QMimeType& type, QWidget* widget, const QVector<KPluginMetaData> &triedOffers)
2463 {
2464  // First try to find an exact match, and then look for more general ones (e. g. the plain text one)
2465  // Ideally we would rank these by "closeness", but that might be overdoing it
2466 
2467  const QVector<KPluginMetaData> available = availableGenerators();
2468  QVector<KPluginMetaData> offers;
2469  QVector<KPluginMetaData> exactMatches;
2470 
2471  QMimeDatabase mimeDatabase;
2472 
2473  for (const KPluginMetaData& md : available)
2474  {
2475  if (triedOffers.contains(md))
2476  continue;
2477 
2478  const QStringList mimetypes = md.mimeTypes();
2479  for (const QString &supported : mimetypes)
2480  {
2481  QMimeType mimeType = mimeDatabase.mimeTypeForName(supported);
2482  if (mimeType == type && !exactMatches.contains(md)) {
2483  exactMatches << md;
2484  }
2485 
2486  if (type.inherits(supported) && !offers.contains(md))
2487  {
2488  offers << md;
2489  }
2490  }
2491  }
2492 
2493  if (!exactMatches.isEmpty()) {
2494  offers = exactMatches;
2495  }
2496 
2497  if (offers.isEmpty())
2498  {
2499  return KPluginMetaData();
2500  }
2501  int hRank=0;
2502  // best ranked offer search
2503  int offercount = offers.size();
2504  if (offercount > 1)
2505  {
2506  // sort the offers: the offers with an higher priority come before
2507  auto cmp = [](const KPluginMetaData& s1, const KPluginMetaData& s2)
2508  {
2509  const QString property = QStringLiteral("X-KDE-Priority");
2510  return s1.rawData()[property].toInt() > s2.rawData()[property].toInt();
2511  };
2512  std::stable_sort(offers.begin(), offers.end(), cmp);
2513 
2514  if (SettingsCore::chooseGenerators())
2515  {
2516  QStringList list;
2517  for (int i = 0; i < offercount; ++i)
2518  {
2519  list << offers.at(i).pluginId();
2520  }
2521  ChooseEngineDialog choose(list, type, widget);
2522 
2523  if (choose.exec() == QDialog::Rejected)
2524  return KPluginMetaData();
2525 
2526  hRank = choose.selectedGenerator();
2527  }
2528  }
2529  Q_ASSERT(hRank < offers.size());
2530  return offers.at(hRank);
2531 }
2532 
2533 Document::OpenResult Document::openDocument(const QString & docFile, const QUrl &url, const QMimeType &_mime, const QString & password )
2534 {
2535  QMimeDatabase db;
2536  QMimeType mime = _mime;
2537  QByteArray filedata;
2538  int fd = -1;
2539  if (url.scheme() == QLatin1String("fd"))
2540  {
2541  bool ok;
2542  fd = url.path().midRef(1).toInt(&ok);
2543  if (!ok)
2544  {
2545  return OpenError;
2546  }
2547  }
2548  else if (url.fileName() == QLatin1String( "-" ))
2549  {
2550  fd = 0;
2551  }
2552  bool triedMimeFromFileContent = false;
2553  if ( fd < 0 )
2554  {
2555  if ( !mime.isValid() )
2556  return OpenError;
2557 
2558  d->m_url = url;
2559  d->m_docFileName = docFile;
2560 
2561  if ( !d->updateMetadataXmlNameAndDocSize() )
2562  return OpenError;
2563  }
2564  else
2565  {
2566  QFile qstdin;
2567  const bool ret = qstdin.open( fd, QIODevice::ReadOnly, QFileDevice::AutoCloseHandle );
2568  if (!ret) {
2569  qWarning() << "failed to read" << url << filedata;
2570  return OpenError;
2571  }
2572 
2573  filedata = qstdin.readAll();
2574  mime = db.mimeTypeForData( filedata );
2575  if ( !mime.isValid() || mime.isDefault() )
2576  return OpenError;
2577  d->m_docSize = filedata.size();
2578  triedMimeFromFileContent = true;
2579  }
2580 
2581  const bool fromFileDescriptor = fd >= 0;
2582 
2583  // 0. load Generator
2584  // request only valid non-disabled plugins suitable for the mimetype
2585  KPluginMetaData offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget);
2586  if ( !offer.isValid() && !triedMimeFromFileContent )
2587  {
2588  QMimeType newmime = db.mimeTypeForFile(docFile, QMimeDatabase::MatchContent);
2589  triedMimeFromFileContent = true;
2590  if ( newmime != mime )
2591  {
2592  mime = newmime;
2593  offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget);
2594  }
2595  if ( !offer.isValid() )
2596  {
2597  // There's still no offers, do a final mime search based on the filename
2598  // We need this because sometimes (e.g. when downloading from a webserver) the mimetype we
2599  // use is the one fed by the server, that may be wrong
2600  newmime = db.mimeTypeForUrl( url );
2601 
2602  if ( !newmime.isDefault() && newmime != mime )
2603  {
2604  mime = newmime;
2605  offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget);
2606  }
2607  }
2608  }
2609  if (!offer.isValid())
2610  {
2611  d->m_openError = i18n( "Can not find a plugin which is able to handle the document being passed." );
2612  emit error( d->m_openError, -1 );
2613  qCWarning(OkularCoreDebug).nospace() << "No plugin for mimetype '" << mime.name() << "'.";
2614  return OpenError;
2615  }
2616 
2617  // 1. load Document
2618  OpenResult openResult = d->openDocumentInternal( offer, fromFileDescriptor, docFile, filedata, password );
2619  if ( openResult == OpenError )
2620  {
2621  QVector<KPluginMetaData> triedOffers;
2622  triedOffers << offer;
2623  offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget, triedOffers);
2624 
2625  while ( offer.isValid() )
2626  {
2627  openResult = d->openDocumentInternal( offer, fromFileDescriptor, docFile, filedata, password );
2628 
2629  if ( openResult == OpenError )
2630  {
2631  triedOffers << offer;
2632  offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget, triedOffers);
2633  }
2634  else break;
2635  }
2636 
2637  if (openResult == OpenError && !triedMimeFromFileContent )
2638  {
2639  QMimeType newmime = db.mimeTypeForFile(docFile, QMimeDatabase::MatchContent);
2640  triedMimeFromFileContent = true;
2641  if ( newmime != mime )
2642  {
2643  mime = newmime;
2644  offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget, triedOffers);
2645  while ( offer.isValid() )
2646  {
2647  openResult = d->openDocumentInternal( offer, fromFileDescriptor, docFile, filedata, password );
2648 
2649  if ( openResult == OpenError )
2650  {
2651  triedOffers << offer;
2652  offer = DocumentPrivate::generatorForMimeType(mime, d->m_widget, triedOffers);
2653  }
2654  else break;
2655  }
2656  }
2657  }
2658 
2659  if ( openResult == OpenSuccess )
2660  {
2661  // Clear errors, since we're trying various generators, maybe one of them errored out
2662  // but we finally succeeded
2663  // TODO one can still see the error message animating out but since this is a very rare
2664  // condition we can leave this for future work
2665  emit error( QString(), -1 );
2666  }
2667  }
2668  if ( openResult != OpenSuccess )
2669  {
2670  return openResult;
2671  }
2672 
2673  // no need to check for the existence of a synctex file, no parser will be
2674  // created if none exists
2675  d->m_synctex_scanner = synctex_scanner_new_with_output_file( QFile::encodeName( docFile ).constData(), nullptr, 1);
2676  if ( !d->m_synctex_scanner && QFile::exists(docFile + QLatin1String( "sync" ) ) )
2677  {
2678  d->loadSyncFile(docFile);
2679  }
2680 
2681  d->m_generatorName = offer.pluginId();
2682  d->m_pageController = new PageController();
2683  connect( d->m_pageController, &PageController::rotationFinished,
2684  this, [this](int p, Okular::Page *op) { d->rotationFinished(p, op); } );
2685 
2686  for ( Page *p : qAsConst(d->m_pagesVector) )
2687  p->d->m_doc = d;
2688 
2689  d->m_metadataLoadingCompleted = false;
2690  d->m_docdataMigrationNeeded = false;
2691 
2692  // 2. load Additional Data (bookmarks, local annotations and metadata) about the document
2693  if ( d->m_archiveData )
2694  {
2695  // QTemporaryFile is weird and will return false in exists if fileName wasn't called before
2696  d->m_archiveData->metadataFile.fileName();
2697  d->loadDocumentInfo( d->m_archiveData->metadataFile, LoadPageInfo );
2698  d->loadDocumentInfo( LoadGeneralInfo );
2699  }
2700  else
2701  {
2702  if ( d->loadDocumentInfo( LoadPageInfo ) )
2703  d->m_docdataMigrationNeeded = true;
2704  d->loadDocumentInfo( LoadGeneralInfo );
2705  }
2706 
2707  d->m_metadataLoadingCompleted = true;
2708  d->m_bookmarkManager->setUrl( d->m_url );
2709 
2710  // 3. setup observers internal lists and data
2711  foreachObserver( notifySetup( d->m_pagesVector, DocumentObserver::DocumentChanged | DocumentObserver::UrlChanged ) );
2712 
2713  // 4. set initial page (restoring the page saved in xml if loaded)
2714  DocumentViewport loadedViewport = (*d->m_viewportIterator);
2715  if ( loadedViewport.isValid() )
2716  {
2717  (*d->m_viewportIterator) = DocumentViewport();
2718  if ( loadedViewport.pageNumber >= (int)d->m_pagesVector.size() )
2719  loadedViewport.pageNumber = d->m_pagesVector.size() - 1;
2720  }
2721  else
2722  loadedViewport.pageNumber = 0;
2723  setViewport( loadedViewport );
2724 
2725  // start bookmark saver timer
2726  if ( !d->m_saveBookmarksTimer )
2727  {
2728  d->m_saveBookmarksTimer = new QTimer( this );
2729  connect( d->m_saveBookmarksTimer, &QTimer::timeout, this, [this] { d->saveDocumentInfo(); } );
2730  }
2731  d->m_saveBookmarksTimer->start( 5 * 60 * 1000 );
2732 
2733  // start memory check timer
2734  if ( !d->m_memCheckTimer )
2735  {
2736  d->m_memCheckTimer = new QTimer( this );
2737  connect( d->m_memCheckTimer, &QTimer::timeout, this, [this] { d->slotTimedMemoryCheck(); } );
2738  }
2739  d->m_memCheckTimer->start( kMemCheckTime );
2740 
2741  const DocumentViewport nextViewport = d->nextDocumentViewport();
2742  if ( nextViewport.isValid() )
2743  {
2744  setViewport( nextViewport );
2745  d->m_nextDocumentViewport = DocumentViewport();
2746  d->m_nextDocumentDestination = QString();
2747  }
2748 
2749  AudioPlayer::instance()->d->m_currentDocument = fromFileDescriptor ? QUrl() : d->m_url;
2750 
2751  const QStringList docScripts = d->m_generator->metaData( QStringLiteral("DocumentScripts"), QStringLiteral ( "JavaScript" ) ).toStringList();
2752  if ( !docScripts.isEmpty() )
2753  {
2754  d->m_scripter = new Scripter( d );
2755  for ( const QString &docscript : docScripts )
2756  {
2757  d->m_scripter->execute( JavaScript, docscript );
2758  }
2759  }
2760 
2761  return OpenSuccess;
2762 }
2763 
2764 bool DocumentPrivate::updateMetadataXmlNameAndDocSize()
2765 {
2766  // m_docFileName is always local so we can use QFileInfo on it
2767  QFileInfo fileReadTest( m_docFileName );
2768  if ( !fileReadTest.isFile() && !fileReadTest.isReadable() )
2769  return false;
2770 
2771  m_docSize = fileReadTest.size();
2772 
2773  // determine the related "xml document-info" filename
2774  if ( m_url.isLocalFile() )
2775  {
2776  const QString filePath = docDataFileName( m_url, m_docSize );
2777  qCDebug(OkularCoreDebug) << "Metadata file is now:" << filePath;
2778  m_xmlFileName = filePath;
2779  }
2780  else
2781  {
2782  qCDebug(OkularCoreDebug) << "Metadata file: disabled";
2783  m_xmlFileName = QString();
2784  }
2785 
2786  return true;
2787 }
2788 
2789 
2791 {
2792  if ( d->m_generator )
2793  {
2794  Okular::GuiInterface * iface = qobject_cast< Okular::GuiInterface * >( d->m_generator );
2795  if ( iface )
2796  return iface->guiClient();
2797  }
2798  return nullptr;
2799 }
2800 
2802 {
2803  // check if there's anything to close...
2804  if ( !d->m_generator )
2805  return;
2806 
2807  emit aboutToClose();
2808 
2809  delete d->m_pageController;
2810  d->m_pageController = nullptr;
2811 
2812  delete d->m_scripter;
2813  d->m_scripter = nullptr;
2814 
2815  // remove requests left in queue
2816  d->clearAndWaitForRequests();
2817 
2818  if ( d->m_fontThread )
2819  {
2820  disconnect( d->m_fontThread, nullptr, this, nullptr );
2821  d->m_fontThread->stopExtraction();
2822  d->m_fontThread->wait();
2823  d->m_fontThread = nullptr;
2824  }
2825 
2826  // stop any audio playback
2828 
2829  // close the current document and save document info if a document is still opened
2830  if ( d->m_generator && d->m_pagesVector.size() > 0 )
2831  {
2832  d->saveDocumentInfo();
2833  d->m_generator->closeDocument();
2834  }
2835 
2836  if ( d->m_synctex_scanner )
2837  {
2838  synctex_scanner_free( d->m_synctex_scanner );
2839  d->m_synctex_scanner = nullptr;
2840  }
2841 
2842  // stop timers
2843  if ( d->m_memCheckTimer )
2844  d->m_memCheckTimer->stop();
2845  if ( d->m_saveBookmarksTimer )
2846  d->m_saveBookmarksTimer->stop();
2847 
2848  if ( d->m_generator )
2849  {
2850  // disconnect the generator from this document ...
2851  d->m_generator->d_func()->m_document = nullptr;
2852  // .. and this document from the generator signals
2853  disconnect( d->m_generator, nullptr, this, nullptr );
2854 
2855  QHash< QString, GeneratorInfo >::const_iterator genIt = d->m_loadedGenerators.constFind( d->m_generatorName );
2856  Q_ASSERT( genIt != d->m_loadedGenerators.constEnd() );
2857  }
2858  d->m_generator = nullptr;
2859  d->m_generatorName = QString();
2860  d->m_url = QUrl();
2861  d->m_walletGenerator = nullptr;
2862  d->m_docFileName = QString();
2863  d->m_xmlFileName = QString();
2864  delete d->m_tempFile;
2865  d->m_tempFile = nullptr;
2866  delete d->m_archiveData;
2867  d->m_archiveData = nullptr;
2868  d->m_docSize = -1;
2869  d->m_exportCached = false;
2870  d->m_exportFormats.clear();
2871  d->m_exportToText = ExportFormat();
2872  d->m_fontsCached = false;
2873  d->m_fontsCache.clear();
2874  d->m_rotation = Rotation0;
2875 
2876  // send an empty list to observers (to free their data)
2878 
2879  // delete pages and clear 'd->m_pagesVector' container
2880  QVector< Page * >::const_iterator pIt = d->m_pagesVector.constBegin();
2881  QVector< Page * >::const_iterator pEnd = d->m_pagesVector.constEnd();
2882  for ( ; pIt != pEnd; ++pIt )
2883  delete *pIt;
2884  d->m_pagesVector.clear();
2885 
2886  // clear 'memory allocation' descriptors
2887  qDeleteAll( d->m_allocatedPixmaps );
2888  d->m_allocatedPixmaps.clear();
2889 
2890  // clear 'running searches' descriptors
2891  QMap< int, RunningSearch * >::const_iterator rIt = d->m_searches.constBegin();
2892  QMap< int, RunningSearch * >::const_iterator rEnd = d->m_searches.constEnd();
2893  for ( ; rIt != rEnd; ++rIt )
2894  delete *rIt;
2895  d->m_searches.clear();
2896 
2897  // clear the visible areas and notify the observers
2898  QVector< VisiblePageRect * >::const_iterator vIt = d->m_pageRects.constBegin();
2899  QVector< VisiblePageRect * >::const_iterator vEnd = d->m_pageRects.constEnd();
2900  for ( ; vIt != vEnd; ++vIt )
2901  delete *vIt;
2902  d->m_pageRects.clear();
2903  foreachObserver( notifyVisibleRectsChanged() );
2904 
2905  // reset internal variables
2906 
2907  d->m_viewportHistory.clear();
2908  d->m_viewportHistory.append( DocumentViewport() );
2909  d->m_viewportIterator = d->m_viewportHistory.begin();
2910  d->m_allocatedPixmapsTotalMemory = 0;
2911  d->m_allocatedTextPagesFifo.clear();
2912  d->m_pageSize = PageSize();
2913  d->m_pageSizes.clear();
2914 
2915  d->m_documentInfo = DocumentInfo();
2916  d->m_documentInfoAskedKeys.clear();
2917 
2918  AudioPlayer::instance()->d->m_currentDocument = QUrl();
2919 
2920  d->m_undoStack->clear();
2921  d->m_docdataMigrationNeeded = false;
2922 
2923 #if HAVE_MALLOC_TRIM
2924  // trim unused memory, glibc should do this but it seems it does not
2925  // this can greatly decrease the [perceived] memory consumption of okular
2926  // see: https://sourceware.org/bugzilla/show_bug.cgi?id=14827
2927  malloc_trim(0);
2928 #endif
2929 }
2930 
2932 {
2933  Q_ASSERT( !d->m_observers.contains( pObserver ) );
2934  d->m_observers << pObserver;
2935 
2936  // if the observer is added while a document is already opened, tell it
2937  if ( !d->m_pagesVector.isEmpty() )
2938  {
2940  pObserver->notifyViewportChanged( false /*disables smoothMove*/ );
2941  }
2942 }
2943 
2945 {
2946  // remove observer from the set. it won't receive notifications anymore
2947  if ( d->m_observers.contains( pObserver ) )
2948  {
2949  // free observer's pixmap data
2950  QVector<Page*>::const_iterator it = d->m_pagesVector.constBegin(), end = d->m_pagesVector.constEnd();
2951  for ( ; it != end; ++it )
2952  (*it)->deletePixmap( pObserver );
2953 
2954  // [MEM] free observer's allocation descriptors
2955  QLinkedList< AllocatedPixmap * >::iterator aIt = d->m_allocatedPixmaps.begin();
2956  QLinkedList< AllocatedPixmap * >::iterator aEnd = d->m_allocatedPixmaps.end();
2957  while ( aIt != aEnd )
2958  {
2959  AllocatedPixmap * p = *aIt;
2960  if ( p->observer == pObserver )
2961  {
2962  aIt = d->m_allocatedPixmaps.erase( aIt );
2963  delete p;
2964  }
2965  else
2966  ++aIt;
2967  }
2968 
2969  for ( PixmapRequest *executingRequest : qAsConst( d->m_executingPixmapRequests ) )
2970  {
2971  if ( executingRequest->observer() == pObserver ) {
2972  d->cancelRenderingBecauseOf( executingRequest, nullptr );
2973  }
2974  }
2975 
2976  // remove observer entry from the set
2977  d->m_observers.remove( pObserver );
2978  }
2979 }
2980 
2982 {
2983  // reparse generator config and if something changed clear Pages
2984  bool configchanged = false;
2985  if ( d->m_generator )
2986  {
2987  Okular::ConfigInterface * iface = qobject_cast< Okular::ConfigInterface * >( d->m_generator );
2988  if ( iface )
2989  configchanged = iface->reparseConfig();
2990  }
2991  if ( configchanged )
2992  {
2993  // invalidate pixmaps
2994  QVector<Page*>::const_iterator it = d->m_pagesVector.constBegin(), end = d->m_pagesVector.constEnd();
2995  for ( ; it != end; ++it ) {
2996  (*it)->deletePixmaps();
2997  }
2998 
2999  // [MEM] remove allocation descriptors
3000  qDeleteAll( d->m_allocatedPixmaps );
3001  d->m_allocatedPixmaps.clear();
3002  d->m_allocatedPixmapsTotalMemory = 0;
3003 
3004  // send reload signals to observers
3005  foreachObserver( notifyContentsCleared( DocumentObserver::Pixmap ) );
3006  }
3007 
3008  // free memory if in 'low' profile
3009  if ( SettingsCore::memoryLevel() == SettingsCore::EnumMemoryLevel::Low &&
3010  !d->m_allocatedPixmaps.isEmpty() && !d->m_pagesVector.isEmpty() )
3011  d->cleanupPixmapMemory();
3012 }
3013 
3014 
3016 {
3017  return d->m_generator;
3018 }
3019 
3021 {
3022  if ( d->m_generator )
3023  {
3024  Okular::PrintInterface * iface = qobject_cast< Okular::PrintInterface * >( d->m_generator );
3025  return iface ? true : false;
3026  }
3027  else
3028  return false;
3029 }
3030 
3032 {
3036  ks = Okular::DocumentInfo::Key( ks+1 ) )
3037  {
3038  keys << ks;
3039  }
3040 
3041  return documentInfo( keys );
3042 }
3043 
3045 {
3046  DocumentInfo result = d->m_documentInfo;
3047  const QSet<DocumentInfo::Key> missingKeys = keys - d->m_documentInfoAskedKeys;
3048 
3049  if ( d->m_generator && !missingKeys.isEmpty() )
3050  {
3051  DocumentInfo info = d->m_generator->generateDocumentInfo( missingKeys );
3052 
3053  if ( missingKeys.contains( DocumentInfo::FilePath ) )
3054  {
3055  info.set( DocumentInfo::FilePath, currentDocument().toDisplayString() );
3056  }
3057 
3058  if ( d->m_docSize != -1 && missingKeys.contains( DocumentInfo::DocumentSize ) )
3059  {
3060  const QString sizeString = KFormat().formatByteSize( d->m_docSize );
3061  info.set( DocumentInfo::DocumentSize, sizeString );
3062  }
3063  if ( missingKeys.contains( DocumentInfo::PagesSize ) )
3064  {
3065  const QString pagesSize = d->pagesSizeString();
3066  if ( !pagesSize.isEmpty() )
3067  {
3068  info.set( DocumentInfo::PagesSize, pagesSize );
3069  }
3070  }
3071 
3072  if ( missingKeys.contains( DocumentInfo::Pages ) && info.get( DocumentInfo::Pages ).isEmpty() ) {
3073  info.set( DocumentInfo::Pages, QString::number( this->pages() ) );
3074  }
3075 
3076  d->m_documentInfo.d->values.unite(info.d->values);
3077  d->m_documentInfo.d->titles.unite(info.d->titles);
3078  result.d->values.unite(info.d->values);
3079  result.d->titles.unite(info.d->titles);
3080  }
3081  d->m_documentInfoAskedKeys += keys;
3082 
3083  return result;
3084 }
3085 
3087 {
3088  return d->m_generator ? d->m_generator->generateDocumentSynopsis() : nullptr;
3089 }
3090 
3092 {
3093  if ( !d->m_generator || !d->m_generator->hasFeature( Generator::FontInfo ) || d->m_fontThread )
3094  return;
3095 
3096  if ( d->m_fontsCached )
3097  {
3098  // in case we have cached fonts, simulate a reading
3099  // this way the API is the same, and users no need to care about the
3100  // internal caching
3101  for ( int i = 0; i < d->m_fontsCache.count(); ++i )
3102  {
3103  emit gotFont( d->m_fontsCache.at( i ) );
3104  emit fontReadingProgress( i / pages() );
3105  }
3106  emit fontReadingEnded();
3107  return;
3108  }
3109 
3110  d->m_fontThread = new FontExtractionThread( d->m_generator, pages() );
3111  connect( d->m_fontThread, &FontExtractionThread::gotFont, this, [this](const Okular::FontInfo &f) { d->fontReadingGotFont(f); } );
3112  connect( d->m_fontThread.data(), &FontExtractionThread::progress, this, [this](int p) { d->slotFontReadingProgress(p); } );
3113 
3114  d->m_fontThread->startExtraction( /*d->m_generator->hasFeature( Generator::Threaded )*/true );
3115 }
3116 
3118 {
3119  if ( !d->m_fontThread )
3120  return;
3121 
3122  disconnect( d->m_fontThread, nullptr, this, nullptr );
3123  d->m_fontThread->stopExtraction();
3124  d->m_fontThread = nullptr;
3125  d->m_fontsCache.clear();
3126 }
3127 
3129 {
3130  return d->m_generator ? d->m_generator->hasFeature( Generator::FontInfo ) : false;
3131 }
3132 
3134 {
3135  return d->m_generator ? d->m_generator->embeddedFiles() : nullptr;
3136 }
3137 
3138 const Page * Document::page( int n ) const
3139 {
3140  return ( n >= 0 && n < d->m_pagesVector.count() ) ? d->m_pagesVector.at(n) : nullptr;
3141 }
3142 
3144 {
3145  return (*d->m_viewportIterator);
3146 }
3147 
3149 {
3150  return d->m_pageRects;
3151 }
3152 
3154 {
3155  QVector< VisiblePageRect * >::const_iterator vIt = d->m_pageRects.constBegin();
3156  QVector< VisiblePageRect * >::const_iterator vEnd = d->m_pageRects.constEnd();
3157  for ( ; vIt != vEnd; ++vIt )
3158  delete *vIt;
3159  d->m_pageRects = visiblePageRects;
3160  // notify change to all other (different from id) observers
3161  foreach(DocumentObserver *o, d->m_observers)
3162  if ( o != excludeObserver )
3164 }
3165 
3167 {
3168  return (*d->m_viewportIterator).pageNumber;
3169 }
3170 
3171 uint Document::pages() const
3172 {
3173  return d->m_pagesVector.size();
3174 }
3175 
3177 {
3178  return d->m_url;
3179 }
3180 
3181 bool Document::isAllowed( Permission action ) const
3182 {
3183  if ( action == Okular::AllowNotes && ( d->m_docdataMigrationNeeded || !d->m_annotationEditingEnabled ) )
3184  return false;
3185  if ( action == Okular::AllowFillForms && d->m_docdataMigrationNeeded )
3186  return false;
3187 
3188 #if !OKULAR_FORCE_DRM
3189  if ( KAuthorized::authorize( QStringLiteral("skip_drm") ) && !SettingsCore::obeyDRM() )
3190  return true;
3191 #endif
3192 
3193  return d->m_generator ? d->m_generator->isAllowed( action ) : false;
3194 }
3195 
3197 {
3198  return d->m_generator ? d->m_generator->hasFeature( Generator::TextExtraction ) : false;
3199 }
3200 
3202 {
3203  return d->m_generator ? d->m_generator->hasFeature( Generator::PageSizes ) : false;
3204 }
3205 
3207 {
3208  return d->m_generator ? d->m_generator->hasFeature( Generator::TiledRendering ) : false;
3209 }
3210 
3212 {
3213  if ( d->m_generator )
3214  {
3215  if ( d->m_pageSizes.isEmpty() )
3216  d->m_pageSizes = d->m_generator->pageSizes();
3217  return d->m_pageSizes;
3218  }
3219  return PageSize::List();
3220 }
3221 
3223 {
3224  if ( !d->m_generator )
3225  return false;
3226 
3227  d->cacheExportFormats();
3228  return !d->m_exportToText.isNull();
3229 }
3230 
3231 bool Document::exportToText( const QString& fileName ) const
3232 {
3233  if ( !d->m_generator )
3234  return false;
3235 
3236  d->cacheExportFormats();
3237  if ( d->m_exportToText.isNull() )
3238  return false;
3239 
3240  return d->m_generator->exportTo( fileName, d->m_exportToText );
3241 }
3242 
3244 {
3245  if ( !d->m_generator )
3246  return ExportFormat::List();
3247 
3248  d->cacheExportFormats();
3249  return d->m_exportFormats;
3250 }
3251 
3252 bool Document::exportTo( const QString& fileName, const ExportFormat& format ) const
3253 {
3254  return d->m_generator ? d->m_generator->exportTo( fileName, format ) : false;
3255 }
3256 
3258 {
3259  return d->m_viewportIterator == d->m_viewportHistory.begin();
3260 }
3261 
3263 {
3264  return d->m_viewportIterator == --(d->m_viewportHistory.end());
3265 }
3266 
3267 QVariant Document::metaData( const QString & key, const QVariant & option ) const
3268 {
3269  // if option starts with "src:" assume that we are handling a
3270  // source reference
3271  if ( key == QLatin1String("NamedViewport")
3272  && option.toString().startsWith( QLatin1String("src:"), Qt::CaseInsensitive )
3273  && d->m_synctex_scanner)
3274  {
3275  const QString reference = option.toString();
3276 
3277  // The reference is of form "src:1111Filename", where "1111"
3278  // points to line number 1111 in the file "Filename".
3279  // Extract the file name and the numeral part from the reference string.
3280  // This will fail if Filename starts with a digit.
3281  QString name, lineString;
3282  // Remove "src:". Presence of substring has been checked before this
3283  // function is called.
3284  name = reference.mid( 4 );
3285  // split
3286  int nameLength = name.length();
3287  int i = 0;
3288  for( i = 0; i < nameLength; ++i )
3289  {
3290  if ( !name[i].isDigit() ) break;
3291  }
3292  lineString = name.left( i );
3293  name = name.mid( i );
3294  // Remove spaces.
3295  name = name.trimmed();
3296  lineString = lineString.trimmed();
3297  // Convert line to integer.
3298  bool ok;
3299  int line = lineString.toInt( &ok );
3300  if (!ok) line = -1;
3301 
3302  // Use column == -1 for now.
3303  if( synctex_display_query( d->m_synctex_scanner, QFile::encodeName(name).constData(), line, -1, 0 ) > 0 )
3304  {
3305  synctex_node_p node;
3306  // For now use the first hit. Could possibly be made smarter
3307  // in case there are multiple hits.
3308  while( ( node = synctex_scanner_next_result( d->m_synctex_scanner ) ) )
3309  {
3311 
3312  // TeX pages start at 1.
3313  viewport.pageNumber = synctex_node_page( node ) - 1;
3314 
3315  if ( viewport.pageNumber >= 0 )
3316  {
3317  const QSizeF dpi = d->m_generator->dpi();
3318 
3319  // TeX small points ...
3320  double px = (synctex_node_visible_h( node ) * dpi.width()) / 72.27;
3321  double py = (synctex_node_visible_v( node ) * dpi.height()) / 72.27;
3322  viewport.rePos.normalizedX = px / page(viewport.pageNumber)->width();
3323  viewport.rePos.normalizedY = ( py + 0.5 ) / page(viewport.pageNumber)->height();
3324  viewport.rePos.enabled = true;
3325  viewport.rePos.pos = Okular::DocumentViewport::Center;
3326 
3327  return viewport.toString();
3328  }
3329  }
3330  }
3331  }
3332  return d->m_generator ? d->m_generator->metaData( key, option ) : QVariant();
3333 }
3334 
3336 {
3337  return d->m_rotation;
3338 }
3339 
3341 {
3342  bool allPagesSameSize = true;
3343  QSizeF size;
3344  for (int i = 0; allPagesSameSize && i < d->m_pagesVector.count(); ++i)
3345  {
3346  const Page *p = d->m_pagesVector.at(i);
3347  if (i == 0) size = QSizeF(p->width(), p->height());
3348  else
3349  {
3350  allPagesSameSize = (size == QSizeF(p->width(), p->height()));
3351  }
3352  }
3353  if (allPagesSameSize) return size;
3354  else return QSizeF();
3355 }
3356 
3358 {
3359  if (d->m_generator)
3360  {
3361  if (d->m_generator->pagesSizeMetric() != Generator::None)
3362  {
3363  const Page *p = d->m_pagesVector.at( page );
3364  return d->localizedSize(QSizeF(p->width(), p->height()));
3365  }
3366  }
3367  return QString();
3368 }
3369 
3370 static bool shouldCancelRenderingBecauseOf( const PixmapRequest & executingRequest, const PixmapRequest & otherRequest )
3371 {
3372  // New request has higher priority -> cancel
3373  if ( executingRequest.priority() > otherRequest.priority() )
3374  return true;
3375 
3376  // New request has lower priority -> don't cancel
3377  if ( executingRequest.priority() < otherRequest.priority() )
3378  return false;
3379 
3380  // New request has same priority and is from a different observer -> don't cancel
3381  // AFAIK this never happens since all observers have different priorities
3382  if ( executingRequest.observer() != otherRequest.observer() )
3383  return false;
3384 
3385  // Same priority and observer, different page number -> don't cancel
3386  // may still end up cancelled later in the parent caller if none of the requests
3387  // is of the executingRequest page and RemoveAllPrevious is specified
3388  if ( executingRequest.pageNumber() != otherRequest.pageNumber() )
3389  return false;
3390 
3391  // Same priority, observer, page, different size -> cancel
3392  if ( executingRequest.width() != otherRequest.width() )
3393  return true;
3394 
3395  // Same priority, observer, page, different size -> cancel
3396  if ( executingRequest.height() != otherRequest.height() )
3397  return true;
3398 
3399  // Same priority, observer, page, different tiling -> cancel
3400  if ( executingRequest.isTile() != otherRequest.isTile() )
3401  return true;
3402 
3403  // Same priority, observer, page, different tiling -> cancel
3404  if ( executingRequest.isTile() )
3405  {
3406  const NormalizedRect bothRequestsRect = executingRequest.normalizedRect() | otherRequest.normalizedRect();
3407  if ( !( bothRequestsRect == executingRequest.normalizedRect() ) )
3408  return true;
3409  }
3410 
3411  return false;
3412 }
3413 
3414 bool DocumentPrivate::cancelRenderingBecauseOf( PixmapRequest *executingRequest, PixmapRequest *newRequest )
3415 {
3416  // No point in aborting the rendering already finished, let it go through
3417  if ( !executingRequest->d->mResultImage.isNull() )
3418  return false;
3419 
3420  if ( newRequest && newRequest->asynchronous() && executingRequest->partialUpdatesWanted() ) {
3421  newRequest->setPartialUpdatesWanted( true );
3422  }
3423 
3424  TilesManager *tm = executingRequest->d->tilesManager();
3425  if ( tm )
3426  {
3427  tm->setPixmap( nullptr, executingRequest->normalizedRect(), true /*isPartialPixmap*/ );
3428  tm->setRequest( NormalizedRect(), 0, 0 );
3429  }
3430  PagePrivate::PixmapObject object = executingRequest->page()->d->m_pixmaps.take( executingRequest->observer() );
3431  delete object.m_pixmap;
3432 
3433  if ( executingRequest->d->mShouldAbortRender != 0)
3434  return false;
3435 
3436  executingRequest->d->mShouldAbortRender = 1;
3437 
3438  if ( m_generator->d_ptr->mTextPageGenerationThread && m_generator->d_ptr->mTextPageGenerationThread->page() == executingRequest->page() )
3439  {
3440  m_generator->d_ptr->mTextPageGenerationThread->abortExtraction();
3441  }
3442 
3443  return true;
3444 }
3445 
3447 {
3448  requestPixmaps( requests, RemoveAllPrevious );
3449 }
3450 
3452 {
3453  if ( requests.isEmpty() )
3454  return;
3455 
3456  if ( !d->m_pageController )
3457  {
3458  // delete requests..
3459  QLinkedList< PixmapRequest * >::const_iterator rIt = requests.constBegin(), rEnd = requests.constEnd();
3460  for ( ; rIt != rEnd; ++rIt )
3461  delete *rIt;
3462  // ..and return
3463  return;
3464  }
3465 
3466  QSet< DocumentObserver * > observersPixmapCleared;
3467 
3468  // 1. [CLEAN STACK] remove previous requests of requesterID
3469  DocumentObserver *requesterObserver = requests.first()->observer();
3470  QSet< int > requestedPages;
3471  {
3472  QLinkedList< PixmapRequest * >::const_iterator rIt = requests.constBegin(), rEnd = requests.constEnd();
3473  for ( ; rIt != rEnd; ++rIt )
3474  {
3475  Q_ASSERT( (*rIt)->observer() == requesterObserver );
3476  requestedPages.insert( (*rIt)->pageNumber() );
3477  }
3478  }
3479  const bool removeAllPrevious = reqOptions & RemoveAllPrevious;
3480  d->m_pixmapRequestsMutex.lock();
3481  QLinkedList< PixmapRequest * >::iterator sIt = d->m_pixmapRequestsStack.begin(), sEnd = d->m_pixmapRequestsStack.end();
3482  while ( sIt != sEnd )
3483  {
3484  if ( (*sIt)->observer() == requesterObserver
3485  && ( removeAllPrevious || requestedPages.contains( (*sIt)->pageNumber() ) ) )
3486  {
3487  // delete request and remove it from stack
3488  delete *sIt;
3489  sIt = d->m_pixmapRequestsStack.erase( sIt );
3490  }
3491  else
3492  ++sIt;
3493  }
3494 
3495  // 1.B [PREPROCESS REQUESTS] tweak some values of the requests
3496  for ( PixmapRequest *request : requests )
3497  {
3498  // set the 'page field' (see PixmapRequest) and check if it is valid
3499  qCDebug(OkularCoreDebug).nospace() << "request observer=" << request->observer() << " " <<request->width() << "x" << request->height() << "@" << request->pageNumber();
3500  if ( d->m_pagesVector.value( request->pageNumber() ) == 0 )
3501  {
3502  // skip requests referencing an invalid page (must not happen)
3503  delete request;
3504  continue;
3505  }
3506 
3507  request->d->mPage = d->m_pagesVector.value( request->pageNumber() );
3508 
3509  if ( request->isTile() )
3510  {
3511  // Change the current request rect so that only invalid tiles are
3512  // requested. Also make sure the rect is tile-aligned.
3513  NormalizedRect tilesRect;
3514  const QList<Tile> tiles = request->d->tilesManager()->tilesAt( request->normalizedRect(), TilesManager::TerminalTile );
3515  QList<Tile>::const_iterator tIt = tiles.constBegin(), tEnd = tiles.constEnd();
3516  while ( tIt != tEnd )
3517  {
3518  const Tile &tile = *tIt;
3519  if ( !tile.isValid() )
3520  {
3521  if ( tilesRect.isNull() )
3522  tilesRect = tile.rect();
3523  else
3524  tilesRect |= tile.rect();
3525  }
3526 
3527  tIt++;
3528  }
3529 
3530  request->setNormalizedRect( tilesRect );
3531  }
3532 
3533  if ( !request->asynchronous() )
3534  request->d->mPriority = 0;
3535  }
3536 
3537  // 1.C [CANCEL REQUESTS] cancel those requests that are running and should be cancelled because of the new requests coming in
3538  if ( d->m_generator->hasFeature( Generator::SupportsCancelling ) )
3539  {
3540  for ( PixmapRequest *executingRequest : qAsConst( d->m_executingPixmapRequests ) )
3541  {
3542  bool newRequestsContainExecutingRequestPage = false;
3543  bool requestCancelled = false;
3544  for ( PixmapRequest *newRequest : requests )
3545  {
3546  if ( newRequest->pageNumber() == executingRequest->pageNumber() && requesterObserver == executingRequest->observer())
3547  {
3548  newRequestsContainExecutingRequestPage = true;
3549  }
3550 
3551  if ( shouldCancelRenderingBecauseOf( *executingRequest, *newRequest ) )
3552  {
3553  requestCancelled = d->cancelRenderingBecauseOf( executingRequest, newRequest );
3554  }
3555  }
3556 
3557  // If we were told to remove all the previous requests and the executing request page is not part of the new requests, cancel it
3558  if ( !requestCancelled && removeAllPrevious && requesterObserver == executingRequest->observer() && !newRequestsContainExecutingRequestPage )
3559  {
3560  requestCancelled = d->cancelRenderingBecauseOf( executingRequest, nullptr );
3561  }
3562 
3563  if ( requestCancelled )
3564  {
3565  observersPixmapCleared << executingRequest->observer();
3566  }
3567  }
3568  }
3569 
3570  // 2. [ADD TO STACK] add requests to stack
3571  for ( PixmapRequest *request : requests )
3572  {
3573  // add request to the 'stack' at the right place
3574  if ( !request->priority() )
3575  // add priority zero requests to the top of the stack
3576  d->m_pixmapRequestsStack.append( request );
3577  else
3578  {
3579  // insert in stack sorted by priority
3580  sIt = d->m_pixmapRequestsStack.begin();
3581  sEnd = d->m_pixmapRequestsStack.end();
3582  while ( sIt != sEnd && (*sIt)->priority() > request->priority() )
3583  ++sIt;
3584  d->m_pixmapRequestsStack.insert( sIt, request );
3585  }
3586  }
3587  d->m_pixmapRequestsMutex.unlock();
3588 
3589  // 3. [START FIRST GENERATION] if <NO>generator is ready, start a new generation,
3590  // or else (if gen is running) it will be started when the new contents will
3591  //come from generator (in requestDone())</NO>
3592  // all handling of requests put into sendGeneratorPixmapRequest
3593  // if ( generator->canRequestPixmap() )
3594  d->sendGeneratorPixmapRequest();
3595 
3596  for ( DocumentObserver *o : qAsConst( observersPixmapCleared ) )
3598 }
3599 
3600 void Document::requestTextPage( uint pageNumber )
3601 {
3602  Page * kp = d->m_pagesVector[ pageNumber ];
3603  if ( !d->m_generator || !kp )
3604  return;
3605 
3606  // Memory management for TextPages
3607 
3608  d->m_generator->generateTextPage( kp );
3609 }
3610 
3611 void DocumentPrivate::notifyAnnotationChanges( int page )
3612 {
3613  foreachObserverD( notifyPageChanged( page, DocumentObserver::Annotations ) );
3614 }
3615 
3616 void DocumentPrivate::notifyFormChanges( int /*page*/ )
3617 {
3618  recalculateForms();
3619 }
3620 
3621 void Document::addPageAnnotation( int page, Annotation * annotation )
3622 {
3623  // Transform annotation's base boundary rectangle into unrotated coordinates
3624  Page *p = d->m_pagesVector[page];
3625  QTransform t = p->d->rotationMatrix();
3626  annotation->d_ptr->baseTransform(t.inverted());
3627  QUndoCommand *uc = new AddAnnotationCommand(this->d, annotation, page);
3628  d->m_undoStack->push(uc);
3629 }
3630 
3631 bool Document::canModifyPageAnnotation( const Annotation * annotation ) const
3632 {
3633  if ( !annotation || ( annotation->flags() & Annotation::DenyWrite ) )
3634  return false;
3635 
3636  if ( !isAllowed(Okular::AllowNotes) )
3637  return false;
3638 
3639  if ( ( annotation->flags() & Annotation::External ) && !d->canModifyExternalAnnotations() )
3640  return false;
3641 
3642  switch ( annotation->subType() )
3643  {
3644  case Annotation::AText:
3645  case Annotation::ALine:
3646  case Annotation::AGeom:
3648  case Annotation::AStamp:
3649  case Annotation::AInk:
3650  return true;
3651  default:
3652  return false;
3653  }
3654 }
3655 
3657 {
3658  Q_ASSERT(d->m_prevPropsOfAnnotBeingModified.isNull());
3659  if (!d->m_prevPropsOfAnnotBeingModified.isNull())
3660  {
3661  qCCritical(OkularCoreDebug) << "Error: Document::prepareToModifyAnnotationProperties has already been called since last call to Document::modifyPageAnnotationProperties";
3662  return;
3663  }
3664  d->m_prevPropsOfAnnotBeingModified = annotation->getAnnotationPropertiesDomNode();
3665 }
3666 
3668 {
3669  Q_ASSERT(!d->m_prevPropsOfAnnotBeingModified.isNull());
3670  if (d->m_prevPropsOfAnnotBeingModified.isNull())
3671  {
3672  qCCritical(OkularCoreDebug) << "Error: Document::prepareToModifyAnnotationProperties must be called before Annotation is modified";
3673  return;
3674  }
3675  QDomNode prevProps = d->m_prevPropsOfAnnotBeingModified;
3676  QUndoCommand *uc = new Okular::ModifyAnnotationPropertiesCommand( d,
3677  annotation,
3678  page,
3679  prevProps,
3680  annotation->getAnnotationPropertiesDomNode() );
3681  d->m_undoStack->push( uc );
3682  d->m_prevPropsOfAnnotBeingModified.clear();
3683 }
3684 
3685 void Document::translatePageAnnotation(int page, Annotation* annotation, const NormalizedPoint & delta )
3686 {
3687  int complete = (annotation->flags() & Okular::Annotation::BeingMoved) == 0;
3688  QUndoCommand *uc = new Okular::TranslateAnnotationCommand( d, annotation, page, delta, complete );
3689  d->m_undoStack->push(uc);
3690 }
3691 
3692 void Document::adjustPageAnnotation( int page, Annotation *annotation, const Okular::NormalizedPoint & delta1, const Okular::NormalizedPoint & delta2 )
3693 {
3694  const bool complete = (annotation->flags() & Okular::Annotation::BeingResized) == 0;
3695  QUndoCommand *uc = new Okular::AdjustAnnotationCommand( d, annotation, page, delta1, delta2, complete );
3696  d->m_undoStack->push(uc);
3697 }
3698 
3700  const QString & newContents,
3701  int newCursorPos,
3702  int prevCursorPos,
3703  int prevAnchorPos
3704  )
3705 {
3706  QString prevContents = annotation->contents();
3707  QUndoCommand *uc = new EditAnnotationContentsCommand( d, annotation, page, newContents, newCursorPos,
3708  prevContents, prevCursorPos, prevAnchorPos );
3709  d->m_undoStack->push( uc );
3710 }
3711 
3712 bool Document::canRemovePageAnnotation( const Annotation * annotation ) const
3713 {
3714  if ( !annotation || ( annotation->flags() & Annotation::DenyDelete ) )
3715  return false;
3716 
3717  if ( ( annotation->flags() & Annotation::External ) && !d->canRemoveExternalAnnotations() )
3718  return false;
3719 
3720  switch ( annotation->subType() )
3721  {
3722  case Annotation::AText:
3723  case Annotation::ALine:
3724  case Annotation::AGeom:
3726  case Annotation::AStamp:
3727  case Annotation::AInk:
3728  case Annotation::ACaret:
3729  return true;
3730  default:
3731  return false;
3732  }
3733 }
3734 
3735 void Document::removePageAnnotation( int page, Annotation * annotation )
3736 {
3737  QUndoCommand *uc = new RemoveAnnotationCommand(this->d, annotation, page);
3738  d->m_undoStack->push(uc);
3739 }
3740 
3741 void Document::removePageAnnotations( int page, const QList<Annotation*> &annotations )
3742 {
3743  d->m_undoStack->beginMacro(i18nc("remove a collection of annotations from the page", "remove annotations"));
3744  foreach(Annotation* annotation, annotations)
3745  {
3746  QUndoCommand *uc = new RemoveAnnotationCommand(this->d, annotation, page);
3747  d->m_undoStack->push(uc);
3748  }
3749  d->m_undoStack->endMacro();
3750 }
3751 
3752 bool DocumentPrivate::canAddAnnotationsNatively() const
3753 {
3754  Okular::SaveInterface * iface = qobject_cast< Okular::SaveInterface * >( m_generator );
3755 
3756  if ( iface && iface->supportsOption(Okular::SaveInterface::SaveChanges) &&
3758  return true;
3759 
3760  return false;
3761 }
3762 
3763 bool DocumentPrivate::canModifyExternalAnnotations() const
3764 {
3765  Okular::SaveInterface * iface = qobject_cast< Okular::SaveInterface * >( m_generator );
3766 
3767  if ( iface && iface->supportsOption(Okular::SaveInterface::SaveChanges) &&
3769  return true;
3770 
3771  return false;
3772 }
3773 
3774 bool DocumentPrivate::canRemoveExternalAnnotations() const
3775 {
3776  Okular::SaveInterface * iface = qobject_cast< Okular::SaveInterface * >( m_generator );
3777 
3778  if ( iface && iface->supportsOption(Okular::SaveInterface::SaveChanges) &&
3780  return true;
3781 
3782  return false;
3783 }
3784 
3785 void Document::setPageTextSelection( int page, RegularAreaRect * rect, const QColor & color )
3786 {
3787  Page * kp = d->m_pagesVector[ page ];
3788  if ( !d->m_generator || !kp )
3789  return;
3790 
3791  // add or remove the selection basing whether rect is null or not
3792  if ( rect )
3793  kp->d->setTextSelections( rect, color );
3794  else
3795  kp->d->deleteTextSelections();
3796 
3797  // notify observers about the change
3798  foreachObserver( notifyPageChanged( page, DocumentObserver::TextSelection ) );
3799 }
3800 
3801 bool Document::canUndo() const
3802 {
3803  return d->m_undoStack->canUndo();
3804 }
3805 
3806 bool Document::canRedo() const
3807 {
3808  return d->m_undoStack->canRedo();
3809 }
3810 
3811 /* REFERENCE IMPLEMENTATION: better calling setViewport from other code
3812 void Document::setNextPage()
3813 {
3814  // advance page and set viewport on observers
3815  if ( (*d->m_viewportIterator).pageNumber < (int)d->m_pagesVector.count() - 1 )
3816  setViewport( DocumentViewport( (*d->m_viewportIterator).pageNumber + 1 ) );
3817 }
3818 
3819 void Document::setPrevPage()
3820 {
3821  // go to previous page and set viewport on observers
3822  if ( (*d->m_viewportIterator).pageNumber > 0 )
3823  setViewport( DocumentViewport( (*d->m_viewportIterator).pageNumber - 1 ) );
3824 }
3825 */
3826 void Document::setViewportPage( int page, DocumentObserver *excludeObserver, bool smoothMove )
3827 {
3828  // clamp page in range [0 ... numPages-1]
3829  if ( page < 0 )
3830  page = 0;
3831  else if ( page > (int)d->m_pagesVector.count() )
3832  page = d->m_pagesVector.count() - 1;
3833 
3834  // make a viewport from the page and broadcast it
3835  setViewport( DocumentViewport( page ), excludeObserver, smoothMove );
3836 }
3837 
3838 void Document::setViewport( const DocumentViewport & viewport, DocumentObserver *excludeObserver, bool smoothMove )
3839 {
3840  if ( !viewport.isValid() )
3841  {
3842  qCDebug(OkularCoreDebug) << "invalid viewport:" << viewport.toString();
3843  return;
3844  }
3845  if ( viewport.pageNumber >= int(d->m_pagesVector.count()) )
3846  {
3847  //qCDebug(OkularCoreDebug) << "viewport out of document:" << viewport.toString();
3848  return;
3849  }
3850 
3851  // if already broadcasted, don't redo it
3852  DocumentViewport & oldViewport = *d->m_viewportIterator;
3853  // disabled by enrico on 2005-03-18 (less debug output)
3854  //if ( viewport == oldViewport )
3855  // qCDebug(OkularCoreDebug) << "setViewport with the same viewport.";
3856 
3857  const int oldPageNumber = oldViewport.pageNumber;
3858 
3859  // set internal viewport taking care of history
3860  if ( oldViewport.pageNumber == viewport.pageNumber || !oldViewport.isValid() )
3861  {
3862  // if page is unchanged save the viewport at current position in queue
3863  oldViewport = viewport;
3864  }
3865  else
3866  {
3867  // remove elements after viewportIterator in queue
3868  d->m_viewportHistory.erase( ++d->m_viewportIterator, d->m_viewportHistory.end() );
3869 
3870  // keep the list to a reasonable size by removing head when needed
3871  if ( d->m_viewportHistory.count() >= OKULAR_HISTORY_MAXSTEPS )
3872  d->m_viewportHistory.pop_front();
3873 
3874  // add the item at the end of the queue
3875  d->m_viewportIterator = d->m_viewportHistory.insert( d->m_viewportHistory.end(), viewport );
3876  }
3877 
3878  const int currentViewportPage = (*d->m_viewportIterator).pageNumber;
3879 
3880  const bool currentPageChanged = (oldPageNumber != currentViewportPage);
3881 
3882  // notify change to all other (different from id) observers
3883  for (DocumentObserver *o : qAsConst(d->m_observers))
3884  {
3885  if ( o != excludeObserver )
3886  o->notifyViewportChanged( smoothMove );
3887 
3888  if ( currentPageChanged )
3889  o->notifyCurrentPageChanged( oldPageNumber, currentViewportPage );
3890  }
3891 }
3892 
3893 void Document::setZoom(int factor, DocumentObserver *excludeObserver)
3894 {
3895  // notify change to all other (different from id) observers
3896  for (DocumentObserver *o : qAsConst(d->m_observers))
3897  if (o != excludeObserver)
3898  o->notifyZoom( factor );
3899 }
3900 
3902 // restore viewport from the history
3903 {
3904  if ( d->m_viewportIterator != d->m_viewportHistory.begin() )
3905  {
3906  const int oldViewportPage = (*d->m_viewportIterator).pageNumber;
3907 
3908  // restore previous viewport and notify it to observers
3909  --d->m_viewportIterator;
3910  foreachObserver( notifyViewportChanged( true ) );
3911 
3912  const int currentViewportPage = (*d->m_viewportIterator).pageNumber;
3913  if (oldViewportPage != currentViewportPage)
3914  foreachObserver( notifyCurrentPageChanged( oldViewportPage, currentViewportPage ) );
3915  }
3916 }
3917 
3919 // restore next viewport from the history
3920 {
3921  auto nextIterator = QLinkedList< DocumentViewport >::const_iterator(d->m_viewportIterator);
3922  ++nextIterator;
3923  if ( nextIterator != d->m_viewportHistory.constEnd() )
3924  {
3925  const int oldViewportPage = (*d->m_viewportIterator).pageNumber;
3926 
3927  // restore next viewport and notify it to observers
3928  ++d->m_viewportIterator;
3929  foreachObserver( notifyViewportChanged( true ) );
3930 
3931  const int currentViewportPage = (*d->m_viewportIterator).pageNumber;
3932  if (oldViewportPage != currentViewportPage)
3933  foreachObserver( notifyCurrentPageChanged( oldViewportPage, currentViewportPage ) );
3934  }
3935 }
3936 
3938 {
3939  d->m_nextDocumentViewport = viewport;
3940 }
3941 
3942 void Document::setNextDocumentDestination( const QString &namedDestination )
3943 {
3944  d->m_nextDocumentDestination = namedDestination;
3945 }
3946 
3947 void Document::searchText( int searchID, const QString & text, bool fromStart, Qt::CaseSensitivity caseSensitivity,
3948  SearchType type, bool moveViewport, const QColor & color )
3949 {
3950  d->m_searchCancelled = false;
3951 
3952  // safety checks: don't perform searches on empty or unsearchable docs
3953  if ( !d->m_generator || !d->m_generator->hasFeature( Generator::TextExtraction ) || d->m_pagesVector.isEmpty() )
3954  {
3955  emit searchFinished( searchID, NoMatchFound );
3956  return;
3957  }
3958 
3959  // if searchID search not recorded, create new descriptor and init params
3960  QMap< int, RunningSearch * >::iterator searchIt = d->m_searches.find( searchID );
3961  if ( searchIt == d->m_searches.end() )
3962  {
3963  RunningSearch * search = new RunningSearch();
3964  search->continueOnPage = -1;
3965  searchIt = d->m_searches.insert( searchID, search );
3966  }
3967  RunningSearch * s = *searchIt;
3968 
3969  // update search structure
3970  bool newText = text != s->cachedString;
3971  s->cachedString = text;
3972  s->cachedType = type;
3973  s->cachedCaseSensitivity = caseSensitivity;
3974  s->cachedViewportMove = moveViewport;
3975  s->cachedColor = color;
3976  s->isCurrentlySearching = true;
3977 
3978  // global data for search
3979  QSet< int > *pagesToNotify = new QSet< int >;
3980 
3981  // remove highlights from pages and queue them for notifying changes
3982  *pagesToNotify += s->highlightedPages;
3983  for (const int pageNumber : qAsConst(s->highlightedPages)) {
3984  d->m_pagesVector.at(pageNumber)->d->deleteHighlights( searchID );
3985  }
3986  s->highlightedPages.clear();
3987 
3988  // set hourglass cursor
3989  QApplication::setOverrideCursor( Qt::WaitCursor );
3990 
3991  // 1. ALLDOC - process all document marking pages
3992  if ( type == AllDocument )
3993  {
3995 
3996  // search and highlight 'text' (as a solid phrase) on all pages
3997  QTimer::singleShot(0, this, [this, pagesToNotify, pageMatches, searchID] { d->doContinueAllDocumentSearch(pagesToNotify, pageMatches, 0, searchID); });
3998  }
3999  // 2. NEXTMATCH - find next matching item (or start from top)
4000  // 3. PREVMATCH - find previous matching item (or start from bottom)
4001  else if ( type == NextMatch || type == PreviousMatch )
4002  {
4003  // find out from where to start/resume search from
4004  const bool forward = type == NextMatch;
4005  const int viewportPage = (*d->m_viewportIterator).pageNumber;
4006  const int fromStartSearchPage = forward ? 0 : d->m_pagesVector.count() - 1;
4007  int currentPage = fromStart ? fromStartSearchPage : ((s->continueOnPage != -1) ? s->continueOnPage : viewportPage);
4008  Page * lastPage = fromStart ? 0 : d->m_pagesVector[ currentPage ];
4009  int pagesDone = 0;
4010 
4011  // continue checking last TextPage first (if it is the current page)
4012  RegularAreaRect * match = nullptr;
4013  if ( lastPage && lastPage->number() == s->continueOnPage )
4014  {
4015  if ( newText )
4016  match = lastPage->findText( searchID, text, forward ? FromTop : FromBottom, caseSensitivity );
4017  else
4018  match = lastPage->findText( searchID, text, forward ? NextResult : PreviousResult, caseSensitivity, &s->continueOnMatch );
4019  if ( !match )
4020  {
4021  if (forward) currentPage++;
4022  else currentPage--;
4023  pagesDone++;
4024  }
4025  }
4026 
4027  s->pagesDone = pagesDone;
4028 
4029  DoContinueDirectionMatchSearchStruct *searchStruct = new DoContinueDirectionMatchSearchStruct();
4030  searchStruct->pagesToNotify = pagesToNotify;
4031  searchStruct->match = match;
4032  searchStruct->currentPage = currentPage;
4033  searchStruct->searchID = searchID;
4034 
4035  QTimer::singleShot(0, this, [this, searchStruct] { d->doContinueDirectionMatchSearch(searchStruct); });
4036  }
4037  // 4. GOOGLE* - process all document marking pages
4038  else if ( type == GoogleAll || type == GoogleAny )
4039  {
4041  const QStringList words = text.split( QLatin1Char ( ' ' ), QString::SkipEmptyParts );
4042 
4043  // search and highlight every word in 'text' on all pages
4044  QTimer::singleShot(0, this, [this, pagesToNotify, pageMatches, searchID, words] { d->doContinueGooglesDocumentSearch(pagesToNotify, pageMatches, 0, searchID, words); });
4045  }
4046 }
4047 
4048 void Document::continueSearch( int searchID )
4049 {
4050  // check if searchID is present in runningSearches
4051  QMap< int, RunningSearch * >::const_iterator it = d->m_searches.constFind( searchID );
4052  if ( it == d->m_searches.constEnd() )
4053  {
4054  emit searchFinished( searchID, NoMatchFound );
4055  return;
4056  }
4057 
4058  // start search with cached parameters from last search by searchID
4059  RunningSearch * p = *it;
4060  if ( !p->isCurrentlySearching )
4061  searchText( searchID, p->cachedString, false, p->cachedCaseSensitivity,
4062  p->cachedType, p->cachedViewportMove, p->cachedColor );
4063 }
4064 
4065 void Document::continueSearch( int searchID, SearchType type )
4066 {
4067  // check if searchID is present in runningSearches
4068  QMap< int, RunningSearch * >::const_iterator it = d->m_searches.constFind( searchID );
4069  if ( it == d->m_searches.constEnd() )
4070  {
4071  emit searchFinished( searchID, NoMatchFound );
4072  return;
4073  }
4074 
4075  // start search with cached parameters from last search by searchID
4076  RunningSearch * p = *it;
4077  if ( !p->isCurrentlySearching )
4078  searchText( searchID, p->cachedString, false, p->cachedCaseSensitivity,
4079  type, p->cachedViewportMove, p->cachedColor );
4080 }
4081 
4082 void Document::resetSearch( int searchID )
4083 {
4084  // if we are closing down, don't bother doing anything
4085  if ( !d->m_generator )
4086  return;
4087 
4088  // check if searchID is present in runningSearches
4089  QMap< int, RunningSearch * >::iterator searchIt = d->m_searches.find( searchID );
4090  if ( searchIt == d->m_searches.end() )
4091  return;
4092 
4093  // get previous parameters for search
4094  RunningSearch * s = *searchIt;
4095 
4096  // unhighlight pages and inform observers about that
4097  for (const int pageNumber : qAsConst(s->highlightedPages))
4098  {
4099  d->m_pagesVector.at(pageNumber)->d->deleteHighlights( searchID );
4100  foreachObserver( notifyPageChanged( pageNumber, DocumentObserver::Highlights ) );
4101  }
4102 
4103  // send the setup signal too (to update views that filter on matches)
4104  foreachObserver( notifySetup( d->m_pagesVector, 0 ) );
4105 
4106  // remove search from the runningSearches list and delete it
4107  d->m_searches.erase( searchIt );
4108  delete s;
4109 }
4110 
4112 {
4113  d->m_searchCancelled = true;
4114 }
4115 
4117 {
4118  d->m_undoStack->undo();
4119 }
4120 
4122 {
4123  d->m_undoStack->redo();
4124 }
4125 
4126 void Document::editFormText( int pageNumber,
4127  Okular::FormFieldText* form,
4128  const QString & newContents,
4129  int newCursorPos,
4130  int prevCursorPos,
4131  int prevAnchorPos )
4132 {
4133  QUndoCommand *uc = new EditFormTextCommand( this->d, form, pageNumber, newContents, newCursorPos, form->text(), prevCursorPos, prevAnchorPos );
4134  d->m_undoStack->push( uc );
4135 }
4136 
4137 void Document::editFormList( int pageNumber,
4138  FormFieldChoice* form,
4139  const QList< int > & newChoices )
4140 {
4141  const QList< int > prevChoices = form->currentChoices();
4142  QUndoCommand *uc = new EditFormListCommand( this->d, form, pageNumber, newChoices, prevChoices );
4143  d->m_undoStack->push( uc );
4144 }
4145 
4146 void Document::editFormCombo( int pageNumber,
4147  FormFieldChoice* form,
4148  const QString & newText,
4149  int newCursorPos,
4150  int prevCursorPos,
4151  int prevAnchorPos )
4152 {
4153 
4154  QString prevText;
4155  if ( form->currentChoices().isEmpty() )
4156  {
4157  prevText = form->editChoice();
4158  }
4159  else
4160  {
4161  prevText = form->choices().at(form->currentChoices().constFirst());
4162  }
4163 
4164  QUndoCommand *uc = new EditFormComboCommand( this->d, form, pageNumber, newText, newCursorPos, prevText, prevCursorPos, prevAnchorPos );
4165  d->m_undoStack->push( uc );
4166 }
4167 
4168 void Document::editFormButtons( int pageNumber, const QList< FormFieldButton* >& formButtons, const QList< bool >& newButtonStates )
4169 {
4170  QUndoCommand *uc = new EditFormButtonsCommand( this->d, pageNumber, formButtons, newButtonStates );
4171  d->m_undoStack->push( uc );
4172 }
4173 
4175 {
4176  const int numOfPages = pages();
4177  for( int i = currentPage(); i >= 0; i -- )
4178  d->refreshPixmaps( i );
4179  for( int i = currentPage() + 1; i < numOfPages; i ++ )
4180  d->refreshPixmaps( i );
4181 }
4182 
4184 {
4185  return d->m_bookmarkManager;
4186 }
4187 
4189 {
4190  QList<int> list;
4191  uint docPages = pages();
4192 
4193  //pages are 0-indexed internally, but 1-indexed externally
4194  for ( uint i = 0; i < docPages; i++ )
4195  {
4196  if ( bookmarkManager()->isBookmarked( i ) )
4197  {
4198  list << i + 1;
4199  }
4200  }
4201  return list;
4202 }
4203 
4205 {
4206  // Code formerly in Part::slotPrint()
4207  // range detecting
4208  QString range;
4209  uint docPages = pages();
4210  int startId = -1;
4211  int endId = -1;
4212 
4213  for ( uint i = 0; i < docPages; ++i )
4214  {
4215  if ( bookmarkManager()->isBookmarked( i ) )
4216  {
4217  if ( startId < 0 )
4218  startId = i;
4219  if ( endId < 0 )
4220  endId = startId;
4221  else
4222  ++endId;
4223  }
4224  else if ( startId >= 0 && endId >= 0 )
4225  {
4226  if ( !range.isEmpty() )
4227  range += QLatin1Char ( ',' );
4228 
4229  if ( endId - startId > 0 )
4230  range += QStringLiteral( "%1-%2" ).arg( startId + 1 ).arg( endId + 1 );
4231  else
4232  range += QString::number( startId + 1 );
4233  startId = -1;
4234  endId = -1;
4235  }
4236  }
4237  if ( startId >= 0 && endId >= 0 )
4238  {
4239  if ( !range.isEmpty() )
4240  range += QLatin1Char ( ',' );
4241 
4242  if ( endId - startId > 0 )
4243  range += QStringLiteral( "%1-%2" ).arg( startId + 1 ).arg( endId + 1 );
4244  else
4245  range += QString::number( startId + 1 );
4246  }
4247  return range;
4248 }
4249 
4250 void Document::processAction( const Action * action )
4251 {
4252  if ( !action )
4253  return;
4254 
4255  // Don't execute next actions if the action itself caused the closing of the document
4256  bool executeNextActions = true;
4257  QObject disconnectHelper; // guarantees the connect below will be disconnected on finishing the function
4258  connect( this, &Document::aboutToClose, &disconnectHelper, [&executeNextActions] { executeNextActions = false; } );
4259 
4260  switch( action->actionType() )
4261  {
4262  case Action::Goto: {
4263  const GotoAction * go = static_cast< const GotoAction * >( action );
4264  d->m_nextDocumentViewport = go->destViewport();
4265  d->m_nextDocumentDestination = go->destinationName();
4266 
4267  // Explanation of why d->m_nextDocumentViewport is needed:
4268  // all openRelativeFile does is launch a signal telling we
4269  // want to open another URL, the problem is that when the file is
4270  // non local, the loading is done asynchronously so you can't
4271  // do a setViewport after the if as it was because you are doing the setViewport
4272  // on the old file and when the new arrives there is no setViewport for it and
4273  // it does not show anything
4274 
4275  // first open filename if link is pointing outside this document
4276  if ( go->isExternal() && !d->openRelativeFile( go->fileName() ) )
4277  {
4278  qCWarning(OkularCoreDebug).nospace() << "Action: Error opening '" << go->fileName() << "'.";
4279  break;
4280  }
4281  else
4282  {
4283  const DocumentViewport nextViewport = d->nextDocumentViewport();
4284  // skip local links that point to nowhere (broken ones)
4285  if ( !nextViewport.isValid() )
4286  break;
4287 
4288  setViewport( nextViewport, nullptr, true );
4289  d->m_nextDocumentViewport = DocumentViewport();
4290  d->m_nextDocumentDestination = QString();
4291  }
4292 
4293  } break;
4294 
4295  case Action::Execute: {
4296  const ExecuteAction * exe = static_cast< const ExecuteAction * >( action );
4297  const QString fileName = exe->fileName();
4298  if ( fileName.endsWith( QLatin1String(".pdf"), Qt::CaseInsensitive ) )
4299  {
4300  d->openRelativeFile( fileName );
4301  break;
4302  }
4303 
4304  // Albert: the only pdf i have that has that kind of link don't define
4305  // an application and use the fileName as the file to open
4306  QUrl url = d->giveAbsoluteUrl( fileName );
4307  QMimeDatabase db;
4308  QMimeType mime = db.mimeTypeForUrl( url );
4309  // Check executables
4310  if ( KRun::isExecutableFile( url, mime.name() ) )
4311  {
4312  // Don't have any pdf that uses this code path, just a guess on how it should work
4313  if ( !exe->parameters().isEmpty() )
4314  {
4315  url = d->giveAbsoluteUrl( exe->parameters() );
4316  mime = db.mimeTypeForUrl( url );
4317 
4318  if ( KRun::isExecutableFile( url, mime.name() ) )
4319  {
4320  // this case is a link pointing to an executable with a parameter
4321  // that also is an executable, possibly a hand-crafted pdf
4322  KMessageBox::information( d->m_widget, i18n("The document is trying to execute an external application and, for your safety, Okular does not allow that.") );
4323  break;
4324  }
4325  }
4326  else
4327  {
4328  // this case is a link pointing to an executable with no parameters
4329  // core developers find unacceptable executing it even after asking the user
4330  KMessageBox::information( d->m_widget, i18n("The document is trying to execute an external application and, for your safety, Okular does not allow that.") );
4331  break;
4332  }
4333  }
4334 
4335  KService::Ptr ptr = KMimeTypeTrader::self()->preferredService( mime.name(), QStringLiteral("Application") );
4336  if ( ptr )
4337  {
4338  QList<QUrl> lst;
4339  lst.append( url );
4340  KRun::runService( *ptr, lst, nullptr );
4341  }
4342  else
4343  KMessageBox::information( d->m_widget, i18n( "No application found for opening file of mimetype %1.", mime.name() ) );
4344  } break;
4345 
4346  case Action::DocAction: {
4347  const DocumentAction * docaction = static_cast< const DocumentAction * >( action );
4348  switch( docaction->documentActionType() )
4349  {
4351  setViewportPage( 0 );
4352  break;
4354  if ( (*d->m_viewportIterator).pageNumber > 0 )
4355  setViewportPage( (*d->m_viewportIterator).pageNumber - 1 );
4356  break;
4358  if ( (*d->m_viewportIterator).pageNumber < (int)d->m_pagesVector.count() - 1 )
4359  setViewportPage( (*d->m_viewportIterator).pageNumber + 1 );
4360  break;
4362  setViewportPage( d->m_pagesVector.count() - 1 );
4363  break;
4365  setPrevViewport();
4366  break;
4368  setNextViewport();
4369  break;
4370  case DocumentAction::Quit:
4371  emit quit();
4372  break;
4374  emit linkPresentation();
4375  break;
4377  emit linkEndPresentation();
4378  break;
4379  case DocumentAction::Find:
4380  emit linkFind();
4381  break;
4383  emit linkGoToPage();
4384  break;
4385  case DocumentAction::Close:
4386  emit close();
4387  break;
4388  }
4389  } break;
4390 
4391  case Action::Browse: {
4392  const BrowseAction * browse = static_cast< const BrowseAction * >( action );
4393  QString lilySource;
4394  int lilyRow = 0, lilyCol = 0;
4395  // if the url is a mailto one, invoke mailer
4396  if ( browse->url().scheme() == QLatin1String("mailto") )
4397  {
4398  QDesktopServices::openUrl( browse->url() );
4399  }
4400  else if ( extractLilyPondSourceReference( browse->url(), &lilySource, &lilyRow, &lilyCol ) )
4401  {
4402  const SourceReference ref( lilySource, lilyRow, lilyCol );
4403  processSourceReference( &ref );
4404  }
4405  else
4406  {
4407  const QUrl url = browse->url();
4408 
4409  // fix for #100366, documents with relative links that are the form of http:foo.pdf
4410  if ((url.scheme() == QLatin1String("http")) && url.host().isEmpty() && url.fileName().endsWith(QLatin1String("pdf")))
4411  {
4412  d->openRelativeFile(url.fileName());
4413  break;
4414  }
4415 
4416  // handle documents with relative path
4417  if ( d->m_url.isValid() )
4418  {
4419  const QUrl realUrl = KIO::upUrl(d->m_url).resolved(url);
4420  // KRun autodeletes
4421  KRun *r = new KRun( realUrl, d->m_widget );
4422  r->setRunExecutables(false);
4423  }
4424  }
4425  } break;
4426 
4427  case Action::Sound: {
4428  const SoundAction * linksound = static_cast< const SoundAction * >( action );
4429  AudioPlayer::instance()->playSound( linksound->sound(), linksound );
4430  } break;
4431 
4432  case Action::Script: {
4433  const ScriptAction * linkscript = static_cast< const ScriptAction * >( action );
4434  if ( !d->m_scripter )
4435  d->m_scripter = new Scripter( d );
4436  d->m_scripter->execute( linkscript->scriptType(), linkscript->script() );
4437  } break;
4438 
4439  case Action::Movie:
4440  emit processMovieAction( static_cast< const MovieAction * >( action ) );
4441  break;
4442  case Action::Rendition: {
4443  const RenditionAction * linkrendition = static_cast< const RenditionAction * >( action );
4444  if ( !linkrendition->script().isEmpty() )
4445  {
4446  if ( !d->m_scripter )
4447  d->m_scripter = new Scripter( d );
4448  d->m_scripter->execute( linkrendition->scriptType(), linkrendition->script() );
4449  }
4450 
4451  emit processRenditionAction( static_cast< const RenditionAction * >( action ) );
4452  } break;
4453  case Action::BackendOpaque: {
4454  d->m_generator->opaqueAction( static_cast< const BackendOpaqueAction * >( action ) );
4455  } break;
4456 
4457  }
4458 
4459  if ( executeNextActions )
4460  {
4461  const QVector<Action*> nextActions = action->nextActions();
4462  for ( const Action *a : nextActions )
4463  {
4464  processAction( a );
4465  }
4466  }
4467 }
4468 
4470 {
4471  if ( action->actionType() != Action::Script )
4472  {
4473  qCDebug( OkularCoreDebug ) << "Unsupported action type" << action->actionType() << "for formatting.";
4474  return;
4475  }
4476 
4477  // Lookup the page of the FormFieldText
4478  int foundPage = d->findFieldPageNumber( fft );
4479 
4480  if ( foundPage == -1 )
4481  {
4482  qCDebug( OkularCoreDebug ) << "Could not find page for formfield!";
4483  return;
4484  }
4485 
4486  const QString unformattedText = fft->text();
4487 
4488  std::shared_ptr< Event > event = Event::createFormatEvent( fft, d->m_pagesVector[foundPage] );
4489 
4490  const ScriptAction * linkscript = static_cast< const ScriptAction * >( action );
4491 
4492  d->executeScriptEvent( event, linkscript );
4493 
4494  const QString formattedText = event->value().toString();
4495  if ( formattedText != unformattedText )
4496  {
4497  // We set the formattedText, because when we call refreshFormWidget
4498  // It will set the QLineEdit to this formattedText
4499  fft->setText( formattedText );
4500  fft->setAppearanceText( formattedText );
4501  emit refreshFormWidget( fft );
4502  d->refreshPixmaps( foundPage );
4503  // Then we make the form have the unformatted text, to use
4504  // in calculations and other things.
4505  fft->setText( unformattedText );
4506  }
4507  else if ( fft->additionalAction( FormField::CalculateField ) )
4508  {
4509  // When the field was calculated we need to refresh even
4510  // if the format script changed nothing. e.g. on error.
4511  // This is because the recalculateForms function delegated
4512  // the responsiblity for the refresh to us.
4513  emit refreshFormWidget( fft );
4514  d->refreshPixmaps( foundPage );
4515  }
4516 }
4517 
4518 void Document::processKeystrokeAction( const Action * action, Okular::FormFieldText *fft, bool &returnCode )
4519 {
4520  if ( action->actionType() != Action::Script )
4521  {
4522  qCDebug( OkularCoreDebug ) << "Unsupported action type" << action->actionType() << "for keystroke.";
4523  return;
4524  }
4525  // Lookup the page of the FormFieldText
4526  int foundPage = d->findFieldPageNumber( fft );
4527 
4528  if ( foundPage == -1 )
4529  {
4530  qCDebug( OkularCoreDebug ) << "Could not find page for formfield!";
4531  return;
4532  }
4533 
4534  std::shared_ptr< Event > event = Event::createKeystrokeEvent( fft, d->m_pagesVector[foundPage] );
4535 
4536  const ScriptAction * linkscript = static_cast< const ScriptAction * >( action );
4537 
4538  d->executeScriptEvent( event, linkscript );
4539 
4540  returnCode = event->returnCode();
4541 }
4542 
4544 {
4545  if ( !action || action->actionType() != Action::Script )
4546  return;
4547 
4548  // Lookup the page of the FormFieldText
4549  int foundPage = d->findFieldPageNumber( field );
4550 
4551  if ( foundPage == -1 )
4552  {
4553  qCDebug( OkularCoreDebug ) << "Could not find page for formfield!";
4554  return;
4555  }
4556 
4557  std::shared_ptr< Event > event = Event::createFormFocusEvent( field, d->m_pagesVector[foundPage] );
4558 
4559  const ScriptAction * linkscript = static_cast< const ScriptAction * >( action );
4560 
4561  d->executeScriptEvent( event, linkscript );
4562 }
4563 
4564 void Document::processValidateAction( const Action * action, Okular::FormFieldText *fft, bool &returnCode )
4565 {
4566  if ( !action || action->actionType() != Action::Script )
4567  return;
4568 
4569  // Lookup the page of the FormFieldText
4570  int foundPage = d->findFieldPageNumber( fft );
4571 
4572  if ( foundPage == -1 )
4573  {
4574  qCDebug( OkularCoreDebug ) << "Could not find page for formfield!";
4575  return;
4576  }
4577 
4578  std::shared_ptr< Event > event = Event::createFormValidateEvent( fft, d->m_pagesVector[foundPage] );
4579 
4580  const ScriptAction * linkscript = static_cast< const ScriptAction * >( action );
4581 
4582  d->executeScriptEvent( event, linkscript );
4583  returnCode = event->returnCode();
4584 }
4585 
4587 {
4588  if ( !ref )
4589  return;
4590 
4591  const QUrl url = d->giveAbsoluteUrl( ref->fileName() );
4592  if ( !url.isLocalFile() )
4593  {
4594  qCDebug(OkularCoreDebug) << url.url() << "is not a local file.";
4595  return;
4596  }
4597 
4598  const QString absFileName = url.toLocalFile();
4599  if ( !QFile::exists( absFileName ) )
4600  {
4601  qCDebug(OkularCoreDebug) << "No such file:" << absFileName;
4602  return;
4603  }
4604 
4605  bool handled = false;
4606  emit sourceReferenceActivated(absFileName, ref->row(), ref->column(), &handled);
4607  if(handled) {
4608  return;
4609  }
4610 
4611  static QHash< int, QString > editors;
4612  // init the editors table if empty (on first run, usually)
4613  if ( editors.isEmpty() )
4614  {
4615  editors = buildEditorsMap();
4616  }
4617 
4618  QHash< int, QString >::const_iterator it = editors.constFind( SettingsCore::externalEditor() );
4619  QString p;
4620  if ( it != editors.constEnd() )
4621  p = *it;
4622  else
4623  p = SettingsCore::externalEditorCommand();
4624  // custom editor not yet configured
4625  if ( p.isEmpty() )
4626  return;
4627 
4628  // manually append the %f placeholder if not specified
4629  if ( p.indexOf( QLatin1String( "%f" ) ) == -1 )
4630  p.append( QLatin1String( " %f" ) );
4631 
4632  // replacing the placeholders
4634  map.insert( QLatin1Char ( 'f' ), absFileName );
4635  map.insert( QLatin1Char ( 'c' ), QString::number( ref->column() ) );
4636  map.insert( QLatin1Char ( 'l' ), QString::number( ref->row() ) );
4637  const QString cmd = KMacroExpander::expandMacrosShellQuote( p, map );
4638  if ( cmd.isEmpty() )
4639  return;
4640  const QStringList args = KShell::splitArgs( cmd );
4641  if ( args.isEmpty() )
4642  return;
4643 
4644  KProcess::startDetached( args );
4645 }
4646 
4647 const SourceReference * Document::dynamicSourceReference( int pageNr, double absX, double absY )
4648 {
4649  if ( !d->m_synctex_scanner )
4650  return nullptr;
4651 
4652  const QSizeF dpi = d->m_generator->dpi();
4653 
4654  if (synctex_edit_query(d->m_synctex_scanner, pageNr + 1, absX * 72. / dpi.width(), absY * 72. / dpi.height()) > 0)
4655  {
4656  synctex_node_p node;
4657  // TODO what should we do if there is really more than one node?
4658  while (( node = synctex_scanner_next_result( d->m_synctex_scanner ) ))
4659  {
4660  int line = synctex_node_line(node);
4661  int col = synctex_node_column(node);
4662  // column extraction does not seem to be implemented in synctex so far. set the SourceReference default value.
4663  if ( col == -1 )
4664  {
4665  col = 0;
4666  }
4667  const char *name = synctex_scanner_get_name( d->m_synctex_scanner, synctex_node_tag( node ) );
4668 
4669  return new Okular::SourceReference( QFile::decodeName( name ), line, col );
4670  }
4671  }
4672  return nullptr;
4673 }
4674 
4676 {
4677  if ( d->m_generator )
4678  {
4679 
4680  if ( d->m_generator->hasFeature( Generator::PrintNative ) )
4681  {
4682  return NativePrinting;
4683  }
4684 
4685 #ifndef Q_OS_WIN
4686  if ( d->m_generator->hasFeature( Generator::PrintPostscript ) )
4687  {
4688  return PostscriptPrinting;
4689  }
4690 #endif
4691 
4692  }
4693 
4694  return NoPrinting;
4695 }
4696 
4698 {
4699  return d->m_generator ? d->m_generator->hasFeature( Generator::PrintToFile ) : false;
4700 }
4701 
4702 bool Document::print( QPrinter &printer )
4703 {
4704  return d->m_generator ? d->m_generator->print( printer ) : false;
4705 }
4706 
4708 {
4709  Okular::Generator::PrintError err = Generator::UnknownPrintError;
4710  if ( d->m_generator )
4711  {
4712  QMetaObject::invokeMethod( d->m_generator, "printError", Qt::DirectConnection, Q_RETURN_ARG(Okular::Generator::PrintError, err) );
4713  }
4714  Q_ASSERT( err != Generator::NoPrintError );
4715  switch ( err )
4716  {
4717  case Generator::TemporaryFileOpenPrintError:
4718  return i18n( "Could not open a temporary file" );
4719  case Generator::FileConversionPrintError:
4720  return i18n( "Print conversion failed" );
4721  case Generator::PrintingProcessCrashPrintError:
4722  return i18n( "Printing process crashed" );
4723  case Generator::PrintingProcessStartPrintError:
4724  return i18n( "Printing process could not start" );
4725  case Generator::PrintToFilePrintError:
4726  return i18n( "Printing to file failed" );
4727  case Generator::InvalidPrinterStatePrintError:
4728  return i18n( "Printer was in invalid state" );
4729  case Generator::UnableToFindFilePrintError:
4730  return i18n( "Unable to find file to print" );
4731  case Generator::NoFileToPrintError:
4732  return i18n( "There was no file to print" );
4733  case Generator::NoBinaryToPrintError:
4734  return i18n( "Could not find a suitable binary for printing. Make sure CUPS lpr binary is available" );
4736  return i18n( "The page print size is invalid" );
4738  return QString();
4739  case Generator::UnknownPrintError:
4740  return QString();
4741  }
4742 
4743  return QString();
4744 }
4745 
4747 {
4748  if ( d->m_generator )
4749  {
4750  PrintInterface * iface = qobject_cast< Okular::PrintInterface * >( d->m_generator );
4751  return iface ? iface->printConfigurationWidget() : nullptr;
4752  }
4753  else
4754  return nullptr;
4755 }
4756 
4758 {
4759  if ( !dialog )
4760  return;
4761 
4762  // We know it's a BackendConfigDialog, but check anyway
4763  BackendConfigDialog *bcd = dynamic_cast<BackendConfigDialog*>( dialog );
4764  if ( !bcd )
4765  return;
4766 
4767  // ensure that we have all the generators with settings loaded
4768  QVector<KPluginMetaData> offers = DocumentPrivate::configurableGenerators();
4769  d->loadServiceList( offers );
4770 
4771  // We want the generators to be sorted by name so let's fill in a QMap
4772  // this sorts by internal id which is not awesome, but at least the sorting
4773  // is stable between runs that before it wasn't
4774  QMap<QString, GeneratorInfo> sortedGenerators;
4775  QHash< QString, GeneratorInfo >::iterator it = d->m_loadedGenerators.begin();
4776  QHash< QString, GeneratorInfo >::iterator itEnd = d->m_loadedGenerators.end();
4777  for ( ; it != itEnd; ++it )
4778  {
4779  sortedGenerators.insert(it.key(), it.value());
4780  }
4781 
4782  bool pagesAdded = false;
4783  QMap< QString, GeneratorInfo >::iterator sit = sortedGenerators.begin();
4784  QMap< QString, GeneratorInfo >::iterator sitEnd = sortedGenerators.end();
4785  for ( ; sit != sitEnd; ++sit )
4786  {
4787  Okular::ConfigInterface * iface = d->generatorConfig( sit.value() );
4788  if ( iface )
4789  {
4790  iface->addPages( dialog );
4791  pagesAdded = true;
4792 
4793  if ( sit.value().generator == d->m_generator )
4794  {
4795  const int rowCount = bcd->thePageWidget()->model()->rowCount();
4796  KPageView *view = bcd->thePageWidget();
4797  view->setCurrentPage( view->model()->index( rowCount - 1, 0 ) );
4798  }
4799  }
4800  }
4801  if ( pagesAdded )
4802  {
4804  this, [this] { d->slotGeneratorConfigChanged(); } );
4805  }
4806 }
4807 
4808 
4809 QVector<KPluginMetaData> DocumentPrivate::configurableGenerators()
4810 {
4811  const QVector<KPluginMetaData> available = availableGenerators();
4812  QVector<KPluginMetaData> result;
4813  for (const KPluginMetaData& md : available)
4814  {
4815  if (md.rawData()[QStringLiteral("X-KDE-okularHasInternalSettings")].toBool())
4816  {
4817  result << md;
4818  }
4819  }
4820  return result;
4821 }
4822 
4824 {
4825  if (!d->m_generator)
4826  return KPluginMetaData();
4827 
4828  auto genIt = d->m_loadedGenerators.constFind(d->m_generatorName);
4829  Q_ASSERT(genIt != d->m_loadedGenerators.constEnd());
4830  return genIt.value().metadata;
4831 }
4832 
4834 {
4835  return DocumentPrivate::configurableGenerators().size();
4836 }
4837 
4839 {
4840  // TODO: make it a static member of DocumentPrivate?
4841  QStringList result = d->m_supportedMimeTypes;
4842  if (result.isEmpty())
4843  {
4844  const QVector<KPluginMetaData> available = DocumentPrivate::availableGenerators();
4845  for (const KPluginMetaData& md : available)
4846  {
4847  result << md.mimeTypes();
4848  }
4849 
4850  // Remove duplicate mimetypes represented by different names
4851  QMimeDatabase mimeDatabase;
4852  QSet<QMimeType> uniqueMimetypes;
4853  for (const QString &mimeName : qAsConst(result)) {
4854  uniqueMimetypes.insert(mimeDatabase.mimeTypeForName(mimeName));
4855  }
4856  result.clear();
4857  for (const QMimeType &mimeType : uniqueMimetypes) {
4858  result.append(mimeType.name());
4859  }
4860 
4861  // Add the Okular archive mimetype
4862  result << QStringLiteral("application/vnd.kde.okular-archive");
4863 
4864  // Sorting by mimetype name doesn't make a ton of sense,
4865  // but ensures that the list is ordered the same way every time
4866  std::sort(result.begin(), result.end());
4867 
4868  d->m_supportedMimeTypes = result;
4869  }
4870  return result;
4871 }
4872 
4874 {
4875  if ( !d->m_generator )
4876  return false;
4877 
4878  return d->m_generator->hasFeature( Generator::SwapBackingFile );
4879 }
4880 
4881 bool Document::swapBackingFile( const QString &newFileName, const QUrl &url )
4882 {
4883  if ( !d->m_generator )
4884  return false;
4885 
4886  if ( !d->m_generator->hasFeature( Generator::SwapBackingFile ) )
4887  return false;
4888 
4889  // Save metadata about the file we're about to close
4890  d->saveDocumentInfo();
4891 
4892  d->clearAndWaitForRequests();
4893 
4894  qCDebug(OkularCoreDebug) << "Swapping backing file to" << newFileName;
4895  QVector< Page * > newPagesVector;
4896  Generator::SwapBackingFileResult result = d->m_generator->swapBackingFile( newFileName, newPagesVector );
4897  if (result != Generator::SwapBackingFileError)
4898  {
4899  QLinkedList< ObjectRect* > rectsToDelete;
4900  QLinkedList< Annotation* > annotationsToDelete;
4901  QSet< PagePrivate* > pagePrivatesToDelete;
4902 
4903  if (result == Generator::SwapBackingFileReloadInternalData)
4904  {
4905  // Here we need to replace everything that the old generator
4906  // had created with what the new one has without making it look like
4907  // we have actually closed and opened the file again
4908 
4909  // Simple sanity check
4910  if (newPagesVector.count() != d->m_pagesVector.count())
4911  return false;
4912 
4913  // Update the undo stack contents
4914  for (int i = 0; i < d->m_undoStack->count(); ++i)
4915  {
4916  // Trust me on the const_cast ^_^
4917  QUndoCommand *uc = const_cast<QUndoCommand *>( d->m_undoStack->command( i ) );
4918  if (OkularUndoCommand *ouc = dynamic_cast<OkularUndoCommand*>( uc ))
4919  {
4920  const bool success = ouc->refreshInternalPageReferences( newPagesVector );
4921  if ( !success )
4922  {
4923  qWarning() << "Document::swapBackingFile: refreshInternalPageReferences failed" << ouc;
4924  return false;
4925  }
4926  }
4927  else
4928  {
4929  qWarning() << "Document::swapBackingFile: Unhandled undo command" << uc;
4930  return false;
4931  }
4932  }
4933 
4934  for (int i = 0; i < d->m_pagesVector.count(); ++i)
4935  {
4936  // switch the PagePrivate* from newPage to oldPage
4937  // this way everyone still holding Page* doesn't get
4938  // disturbed by it
4939  Page *oldPage = d->m_pagesVector[i];
4940  Page *newPage = newPagesVector[i];
4941  newPage->d->adoptGeneratedContents(oldPage->d);
4942 
4943  pagePrivatesToDelete << oldPage->d;
4944  oldPage->d = newPage->d;
4945  oldPage->d->m_page = oldPage;
4946  oldPage->d->m_doc = d;
4947  newPage->d = nullptr;
4948 
4949  annotationsToDelete << oldPage->m_annotations;
4950  rectsToDelete << oldPage->m_rects;
4951  oldPage->m_annotations = newPage->m_annotations;
4952  oldPage->m_rects = newPage->m_rects;
4953  }
4954  qDeleteAll( newPagesVector );
4955  }
4956 
4957  d->m_url = url;
4958  d->m_docFileName = newFileName;
4959  d->updateMetadataXmlNameAndDocSize();
4960  d->m_bookmarkManager->setUrl( d->m_url );
4961  d->m_documentInfo = DocumentInfo();
4962  d->m_documentInfoAskedKeys.clear();
4963 
4964  if ( d->m_synctex_scanner )
4965  {
4966  synctex_scanner_free( d->m_synctex_scanner );
4967  d->m_synctex_scanner = synctex_scanner_new_with_output_file( QFile::encodeName( newFileName ).constData(), nullptr, 1);
4968  if ( !d->m_synctex_scanner && QFile::exists(newFileName + QLatin1String( "sync" ) ) )
4969  {
4970  d->loadSyncFile(newFileName);
4971  }
4972  }
4973 
4974  foreachObserver( notifySetup( d->m_pagesVector, DocumentObserver::UrlChanged ) );
4975 
4976  qDeleteAll( annotationsToDelete );
4977  qDeleteAll( rectsToDelete );
4978  qDeleteAll( pagePrivatesToDelete );
4979 
4980  return true;
4981  }
4982  else
4983  {
4984  return false;
4985  }
4986 }
4987 
4988 bool Document::swapBackingFileArchive( const QString &newFileName, const QUrl &url )
4989 {
4990  qCDebug(OkularCoreDebug) << "Swapping backing archive to" << newFileName;
4991 
4992  ArchiveData *newArchive = DocumentPrivate::unpackDocumentArchive( newFileName );
4993  if ( !newArchive )
4994  return false;
4995 
4996  const QString tempFileName = newArchive->document.fileName();
4997 
4998  const bool success = swapBackingFile( tempFileName, url );
4999 
5000  if ( success )
5001  {
5002  delete d->m_archiveData;
5003  d->m_archiveData = newArchive;
5004  }
5005 
5006  return success;
5007 }
5008 
5009 void Document::setHistoryClean( bool clean )
5010 {
5011  if ( clean )
5012  d->m_undoStack->setClean();
5013  else
5014  d->m_undoStack->resetClean();
5015 }
5016 
5018 {
5019  if ( !d->m_generator )
5020  return false;
5021  Q_ASSERT( !d->m_generatorName.isEmpty() );
5022 
5023  QHash< QString, GeneratorInfo >::iterator genIt = d->m_loadedGenerators.find( d->m_generatorName );
5024  Q_ASSERT( genIt != d->m_loadedGenerators.end() );
5025  SaveInterface* saveIface = d->generatorSave( genIt.value() );
5026  if ( !saveIface )
5027  return false;
5028 
5029  return saveIface->supportsOption( SaveInterface::SaveChanges );
5030 }
5031 
5033 {
5034  switch ( cap )
5035  {
5036  case SaveFormsCapability:
5037  /* Assume that if the generator supports saving, forms can be saved.
5038  * We have no means to actually query the generator at the moment
5039  * TODO: Add some method to query the generator in SaveInterface */
5040  return canSaveChanges();
5041 
5043  return d->canAddAnnotationsNatively();
5044  }
5045 
5046  return false;
5047 }
5048 
5049 bool Document::saveChanges( const QString &fileName )
5050 {
5051  QString errorText;
5052  return saveChanges( fileName, &errorText );
5053 }
5054 
5055 bool Document::saveChanges( const QString &fileName, QString *errorText )
5056 {
5057  if ( !d->m_generator || fileName.isEmpty() )
5058  return false;
5059  Q_ASSERT( !d->m_generatorName.isEmpty() );
5060 
5061  QHash< QString, GeneratorInfo >::iterator genIt = d->m_loadedGenerators.find( d->m_generatorName );
5062  Q_ASSERT( genIt != d->m_loadedGenerators.end() );
5063  SaveInterface* saveIface = d->generatorSave( genIt.value() );
5064  if ( !saveIface || !saveIface->supportsOption( SaveInterface::SaveChanges ) )
5065  return false;
5066 
5067  return saveIface->save( fileName, SaveInterface::SaveChanges, errorText );
5068 }
5069 
5071 {
5072  if ( !view )
5073  return;
5074 
5075  Document *viewDoc = view->viewDocument();
5076  if ( viewDoc )
5077  {
5078  // check if already registered for this document
5079  if ( viewDoc == this )
5080  return;
5081 
5082  viewDoc->unregisterView( view );
5083  }
5084 
5085  d->m_views.insert( view );
5086  view->d_func()->document = d;
5087 }
5088 
5090 {
5091  if ( !view )
5092  return;
5093 
5094  Document *viewDoc = view->viewDocument();
5095  if ( !viewDoc || viewDoc != this )
5096  return;
5097 
5098  view->d_func()->document = nullptr;
5099  d->m_views.remove( view );
5100 }
5101 
5103 {
5104  QByteArray result;
5105 
5106  if (d->m_generator)
5107  {
5108  QMetaObject::invokeMethod(d->m_generator, "requestFontData", Qt::DirectConnection, Q_ARG(Okular::FontInfo, font), Q_ARG(QByteArray*, &result));
5109  }
5110 
5111  return result;
5112 }
5113 
5114 ArchiveData *DocumentPrivate::unpackDocumentArchive( const QString &archivePath )
5115 {
5116  QMimeDatabase db;
5117  const QMimeType mime = db.mimeTypeForFile( archivePath, QMimeDatabase::MatchExtension );
5118  if ( !mime.inherits( QStringLiteral("application/vnd.kde.okular-archive") ) )
5119  return nullptr;
5120 
5121  KZip okularArchive( archivePath );
5122  if ( !okularArchive.open( QIODevice::ReadOnly ) )
5123  return nullptr;
5124 
5125  const KArchiveDirectory * mainDir = okularArchive.directory();
5126 
5127  // Check the archive doesn't have folders, we don't create them when saving the archive
5128  // and folders mean paths and paths mean path traversal issues
5129  const QStringList mainDirEntries = mainDir->entries();
5130  for ( const QString &entry : mainDirEntries )
5131  {
5132  if ( mainDir->entry( entry )->isDirectory() )
5133  {
5134  qWarning() << "Warning: Found a directory inside" << archivePath << " - Okular does not create files like that so it is most probably forged.";
5135  return nullptr;
5136  }
5137  }
5138 
5139  const KArchiveEntry * mainEntry = mainDir->entry( QStringLiteral("content.xml") );
5140  if ( !mainEntry || !mainEntry->isFile() )
5141  return nullptr;
5142 
5143  std::unique_ptr< QIODevice > mainEntryDevice( static_cast< const KZipFileEntry * >( mainEntry )->createDevice() );
5144  QDomDocument doc;
5145  if ( !doc.setContent( mainEntryDevice.get() ) )
5146  return nullptr;
5147  mainEntryDevice.reset();
5148 
5149  QDomElement root = doc.documentElement();
5150  if ( root.tagName() != QLatin1String("OkularArchive") )
5151  return nullptr;
5152 
5153  QString documentFileName;
5154  QString metadataFileName;
5155  QDomElement el = root.firstChild().toElement();
5156  for ( ; !el.isNull(); el = el.nextSibling().toElement() )
5157  {
5158  if ( el.tagName() == QLatin1String("Files") )
5159  {
5160  QDomElement fileEl = el.firstChild().toElement();
5161  for ( ; !fileEl.isNull(); fileEl = fileEl.nextSibling().toElement() )
5162  {
5163  if ( fileEl.tagName() == QLatin1String("DocumentFileName") )
5164  documentFileName = fileEl.text();
5165  else if ( fileEl.tagName() == QLatin1String("MetadataFileName") )
5166  metadataFileName = fileEl.text();
5167  }
5168  }
5169  }
5170  if ( documentFileName.isEmpty() )
5171  return nullptr;
5172 
5173  const KArchiveEntry * docEntry = mainDir->entry( documentFileName );
5174  if ( !docEntry || !docEntry->isFile() )
5175  return nullptr;
5176 
5177  std::unique_ptr< ArchiveData > archiveData( new ArchiveData() );
5178  const int dotPos = documentFileName.indexOf( QLatin1Char('.') );
5179  if ( dotPos != -1 )
5180  archiveData->document.setFileTemplate(QDir::tempPath() + QLatin1String("/okular_XXXXXX") + documentFileName.mid(dotPos));
5181  if ( !archiveData->document.open() )
5182  return nullptr;
5183 
5184  archiveData->originalFileName = documentFileName;
5185 
5186  {
5187  std::unique_ptr< QIODevice > docEntryDevice( static_cast< const KZipFileEntry * >( docEntry )->createDevice() );
5188  copyQIODevice( docEntryDevice.get(), &archiveData->document );
5189  archiveData->document.close();
5190  }
5191 
5192  const KArchiveEntry * metadataEntry = mainDir->entry( metadataFileName );
5193  if ( metadataEntry && metadataEntry->isFile() )
5194  {
5195  std::unique_ptr< QIODevice > metadataEntryDevice( static_cast< const KZipFileEntry * >( metadataEntry )->createDevice() );
5196  archiveData->metadataFile.setFileTemplate(QDir::tempPath() + QLatin1String("/okular_XXXXXX.xml"));
5197  if ( archiveData->metadataFile.open() )
5198  {
5199  copyQIODevice( metadataEntryDevice.get(), &archiveData->metadataFile );
5200  archiveData->metadataFile.close();
5201  }
5202  }
5203 
5204  return archiveData.release();
5205 }
5206 
5207 Document::OpenResult Document::openDocumentArchive( const QString & docFile, const QUrl & url, const QString & password )
5208 {
5209  d->m_archiveData = DocumentPrivate::unpackDocumentArchive( docFile );
5210  if ( !d->m_archiveData )
5211  return OpenError;
5212 
5213  const QString tempFileName = d->m_archiveData->document.fileName();
5214  QMimeDatabase db;
5215  const QMimeType docMime = db.mimeTypeForFile( tempFileName, QMimeDatabase::MatchExtension );
5216  const OpenResult ret = openDocument( tempFileName, url, docMime, password );
5217 
5218  if ( ret != OpenSuccess )
5219  {
5220  delete d->m_archiveData;
5221  d->m_archiveData = nullptr;
5222  }
5223 
5224  return ret;
5225 }
5226 
5228 {
5229  if ( !d->m_generator )
5230  return false;
5231 
5232  /* If we opened an archive, use the name of original file (eg foo.pdf)
5233  * instead of the archive's one (eg foo.okular) */
5234  QString docFileName = d->m_archiveData ? d->m_archiveData->originalFileName : d->m_url.fileName();
5235  if ( docFileName == QLatin1String( "-" ) )
5236  return false;
5237 
5238  QString docPath = d->m_docFileName;
5239  const QFileInfo fi( docPath );
5240  if ( fi.isSymLink() )
5241  docPath = fi.symLinkTarget();
5242 
5243  KZip okularArchive( fileName );
5244  if ( !okularArchive.open( QIODevice::WriteOnly ) )
5245  return false;
5246 
5247  const KUser user;
5248 #ifndef Q_OS_WIN
5249  const KUserGroup userGroup( user.groupId() );
5250 #else
5251  const KUserGroup userGroup( QString( "" ) );
5252 #endif
5253 
5254  QDomDocument contentDoc( QStringLiteral("OkularArchive") );
5256  QStringLiteral( "xml" ), QStringLiteral( "version=\"1.0\" encoding=\"utf-8\"" ) );
5257  contentDoc.appendChild( xmlPi );
5258  QDomElement root = contentDoc.createElement( QStringLiteral("OkularArchive") );
5259  contentDoc.appendChild( root );
5260 
5261  QDomElement filesNode = contentDoc.createElement( QStringLiteral("Files") );
5262  root.appendChild( filesNode );
5263 
5264  QDomElement fileNameNode = contentDoc.createElement( QStringLiteral("DocumentFileName") );
5265  filesNode.appendChild( fileNameNode );
5266  fileNameNode.appendChild( contentDoc.createTextNode( docFileName ) );
5267 
5268  QDomElement metadataFileNameNode = contentDoc.createElement( QStringLiteral("MetadataFileName") );
5269  filesNode.appendChild( metadataFileNameNode );
5270  metadataFileNameNode.appendChild( contentDoc.createTextNode( QStringLiteral("metadata.xml") ) );
5271 
5272  // If the generator can save annotations natively, do it
5273  QTemporaryFile modifiedFile;
5274  bool annotationsSavedNatively = false;
5275  bool formsSavedNatively = false;
5276  if ( d->canAddAnnotationsNatively() || canSaveChanges( SaveFormsCapability ) )
5277  {
5278  if ( !modifiedFile.open() )
5279  return false;
5280 
5281  const QString modifiedFileName = modifiedFile.fileName();
5282 
5283  modifiedFile.close(); // We're only interested in the file name
5284 
5285  QString errorText;
5286  if ( saveChanges( modifiedFileName, &errorText ) )
5287  {
5288  docPath = modifiedFileName; // Save this instead of the original file
5289  annotationsSavedNatively = d->canAddAnnotationsNatively();
5290  formsSavedNatively = canSaveChanges( SaveFormsCapability );
5291  }
5292  else
5293  {
5294  qCWarning(OkularCoreDebug) << "saveChanges failed: " << errorText;
5295  qCDebug(OkularCoreDebug) << "Falling back to saving a copy of the original file";
5296  }
5297  }
5298 
5299  PageItems saveWhat = None;
5300  if ( !annotationsSavedNatively )
5301  saveWhat |= AnnotationPageItems;
5302  if ( !formsSavedNatively )
5303  saveWhat |= FormFieldPageItems;
5304 
5305  QTemporaryFile metadataFile;
5306  if ( !d->savePageDocumentInfo( &metadataFile, saveWhat ) )
5307  return false;
5308 
5309  const QByteArray contentDocXml = contentDoc.toByteArray();
5310  const mode_t perm = 0100644;
5311  okularArchive.writeFile( QStringLiteral("content.xml"), contentDocXml, perm,
5312  user.loginName(), userGroup.name() );
5313 
5314 
5315  okularArchive.addLocalFile( docPath, docFileName );
5316  okularArchive.addLocalFile( metadataFile.fileName(), QStringLiteral("metadata.xml") );
5317 
5318  if ( !okularArchive.close() )
5319  return false;
5320 
5321  return true;
5322 }
5323 
5324 bool Document::extractArchivedFile( const QString &destFileName )
5325 {
5326  if ( !d->m_archiveData )
5327  return false;
5328 
5329  // Remove existing file, if present (QFile::copy doesn't overwrite by itself)
5330  QFile::remove( destFileName );
5331 
5332  return d->m_archiveData->document.copy( destFileName );
5333 }
5334 
5335 QPrinter::Orientation Document::orientation() const
5336 {
5337  double width, height;
5338  int landscape, portrait;
5339  const Okular::Page *currentPage;
5340 
5341  // if some pages are landscape and others are not, the most common wins, as
5342  // QPrinter does not accept a per-page setting
5343  landscape = 0;
5344  portrait = 0;
5345  for (uint i = 0; i < pages(); i++)
5346  {
5347  currentPage = page(i);
5348  width = currentPage->width();
5349  height = currentPage->height();
5350  if (currentPage->orientation() == Okular::Rotation90 || currentPage->orientation() ==