part.h 15.3 KB
Newer Older
1 2 3 4 5 6 7
/***************************************************************************
 *   Copyright (C) 2002 by Wilco Greven <greven@kde.org>                   *
 *   Copyright (C) 2003-2004 by Christophe Devriese                        *
 *                         <Christophe.Devriese@student.kuleuven.ac.be>    *
 *   Copyright (C) 2003 by Andy Goossens <andygoossens@telenet.be>         *
 *   Copyright (C) 2003 by Laurent Montel <montel@kde.org>                 *
 *   Copyright (C) 2004 by Dominique Devriese <devriese@kde.org>           *
8
 *   Copyright (C) 2004-2007 by Albert Astals Cid <aacid@kde.org>          *
9 10 11
 *   Copyright (C) 2017    Klarälvdalens Datakonsult AB, a KDAB Group      *
 *                         company, info@kdab.com. Work sponsored by the   *
 *                         LiMux project of the city of Munich             *
12 13 14 15 16 17 18
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

19 20
#ifndef _PART_H_
#define _PART_H_
21

22 23 24 25
#include <QIcon>
#include <QList>
#include <QPointer>
#include <QProcess>
Yuri Chornoivan's avatar
Yuri Chornoivan committed
26
#include <QtDBus> // krazy:exclude=includes
Lukáš Tinkl's avatar
Lukáš Tinkl committed
27
#include <QUrl>
28 29 30 31 32 33 34

#include <KCompressionDevice>
#include <KIO/Job>
#include <KMessageWidget>
#include <KParts/ReadWritePart>
#include <KPluginFactory>

35
#include "core/observer.h"
36
#include "core/document.h"
37
#include "kdocumentviewer.h"
38
#include "interfaces/viewerinterface.h"
39

40
#include "okularpart_export.h"
41

Kevin Ottens's avatar
Kevin Ottens committed
42 43
#include <config-okular.h>

44
class QAction;
45
class QWidget;
46
class QPrinter;
47
class QMenu;
48

49
class KConfigDialog;
Albert Astals Cid's avatar
Albert Astals Cid committed
50
class KDirWatch;
51
class KToggleAction;
52
class KToggleFullScreenAction;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
53
class QTemporaryFile;
Vishesh Handa's avatar
Vishesh Handa committed
54
class QAction;
Kevin Ottens's avatar
Kevin Ottens committed
55
class QJsonObject;
56
namespace KParts { class GUIActivateEvent; }
57

58
class FindBar;
Albert Astals Cid's avatar
Albert Astals Cid committed
59
class ThumbnailList;
Albert Astals Cid's avatar
Albert Astals Cid committed
60
class PageSizeLabel;
61
class PageView;
62
class PresentationWidget;
63
class ProgressWidget;
64
class SearchWidget;
Pino Toscano's avatar
Pino Toscano committed
65
class Sidebar;
Albert Astals Cid's avatar
Albert Astals Cid committed
66 67
class TOC;
class MiniBar;
Albert Astals Cid's avatar
Albert Astals Cid committed
68
class MiniBarLogic;
69
class FileKeeper;
70 71
class Reviews;
class BookmarkList;
72
class DrawingToolActions;
73
class Layers;
74
class SignaturePanel;
75

Kevin Ottens's avatar
Kevin Ottens committed
76 77 78 79
#if PURPOSE_FOUND
namespace Purpose { class Menu; }
#endif

80 81
namespace Okular
{
82 83

class BrowserExtension;
84
class ExportFormat;
85

86 87 88 89 90 91 92 93 94 95 96
/**
 * Describes the possible embedding modes of the part
 *
 * @since 0.14 (KDE 4.8)
 */
enum EmbedMode
{
    UnknownEmbedMode,
    NativeShellMode,         // embedded in the native Okular' shell
    PrintPreviewMode,        // embedded to show the print preview of a document
    KHTMLPartMode,           // embedded in KHTML
Laurent Montel's avatar
Laurent Montel committed
97
    ViewerWidgetMode         // the part acts as a widget that can display all kinds of documents
98 99
};

100 101 102 103 104 105 106 107
/**
 * This is a "Part".  It that does all the real work in a KPart
 * application.
 *
 * @short Main Part
 * @author Wilco Greven <greven@kde.org>
 * @version 0.2
 */
108
class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::DocumentObserver, public KDocumentViewer, public Okular::ViewerInterface
109
{
110
    Q_OBJECT
Pino Toscano's avatar
Pino Toscano committed
111 112
    Q_CLASSINFO("D-Bus Interface", "org.kde.okular")
    Q_INTERFACES(KDocumentViewer)
113
    Q_INTERFACES(Okular::ViewerInterface)
114

115
    friend class PartTest;
116

Pino Toscano's avatar
Pino Toscano committed
117
    public:
118
        // Default constructor
119 120 121 122 123 124
        /**
         * If one element of 'args' contains one of the strings "Print/Preview" or "ViewerWidget",
         * the part will be set up in the corresponding mode. Additionally, it is possible to specify
         * which config file should be used by adding a string containing "ConfigFileName=<file name>"
         * to 'args'.
         **/
125
        Part(QWidget* parentWidget, QObject* parent, const QVariantList& args);
126 127 128 129 130

        // Destructor
        ~Part();

        // inherited from DocumentObserver
131 132 133
        void notifySetup( const QVector< Okular::Page * > &pages, int setupFlags ) override;
        void notifyViewportChanged( bool smoothMove ) override;
        void notifyPageChanged( int page, int flags ) override;
134

135 136 137
        bool openDocument(const QUrl &url, uint page) override;
        void startPresentation() override;
        QStringList supportedMimeTypes() const override;
138

Lukáš Tinkl's avatar
Lukáš Tinkl committed
139
        QUrl realUrl() const;
140

141 142 143 144 145 146 147
        void showSourceLocation(const QString& fileName, int line, int column, bool showGraphically = true) override;
        void clearLastShownSourceLocation() override;
        bool isWatchFileModeEnabled() const override;
        void setWatchFileModeEnabled(bool enable) override;
        bool areSourceLocationsShownGraphically() const override;
        void setShowSourceLocationsGraphically(bool show) override;
        bool openNewFilesInTabs() const override;
148

149
    public Q_SLOTS:                // dbus
150
        Q_SCRIPTABLE Q_NOREPLY void goToPage(uint page) override;
151
        Q_SCRIPTABLE Q_NOREPLY void openDocument( const QString &doc );
152 153
        Q_SCRIPTABLE uint pages();
        Q_SCRIPTABLE uint currentPage();
154
        Q_SCRIPTABLE QString currentDocument();
155
        Q_SCRIPTABLE QString documentMetaData( const QString &metaData ) const;
156 157 158 159 160 161 162
        Q_SCRIPTABLE void slotPreferences();
        Q_SCRIPTABLE void slotFind();
        Q_SCRIPTABLE void slotPrintPreview();
        Q_SCRIPTABLE void slotPreviousPage();
        Q_SCRIPTABLE void slotNextPage();
        Q_SCRIPTABLE void slotGotoFirst();
        Q_SCRIPTABLE void slotGotoLast();
163
        Q_SCRIPTABLE void slotTogglePresentation();
164 165
        Q_SCRIPTABLE void slotToggleChangeColors();
        Q_SCRIPTABLE void slotSetChangeColors(bool active);
166
        Q_SCRIPTABLE Q_NOREPLY void reload();
167
        Q_SCRIPTABLE Q_NOREPLY void enableStartWithPrint();
168
        Q_SCRIPTABLE Q_NOREPLY void enableExitAfterPrint();
169
        Q_SCRIPTABLE Q_NOREPLY void enableStartWithFind(const QString &text);
170
        Q_SCRIPTABLE void slotOpenContainingFolder();
171

172
    Q_SIGNALS:
173
        void enablePrintAction(bool enable);
174
        void openSourceReference(const QString& absFileName, int line, int column);
175
        void viewerMenuStateChange(bool enabled);
176
        void enableCloseAction(bool enable);
Frederik Gladhorn's avatar
Frederik Gladhorn committed
177
        void mimeTypeChanged(QMimeType mimeType);
Vishesh Handa's avatar
Vishesh Handa committed
178
        void urlsDropped( const QList<QUrl>& urls );
179
        void fitWindowToPage( const QSize& pageViewPortSize, const QSize& pageSize );
180 181

    protected:
182
        // reimplemented from KParts::ReadWritePart
183 184 185
        bool openFile() override;
        bool openUrl(const QUrl &url) override;
        void guiActivateEvent(KParts::GUIActivateEvent *event) override;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
186
        void displayInfoMessage( const QString &message, KMessageWidget::MessageType messageType = KMessageWidget::Information, int duration = -1 );
187
    public:
188 189 190 191 192
        bool queryClose() override;
        bool closeUrl() override;
        bool closeUrl(bool promptToSave) override;
        void setReadWrite(bool readwrite) override;
        bool saveAs(const QUrl & saveUrl) override;
193

194
    protected Q_SLOTS:
195
        // connected to actions
Lukáš Tinkl's avatar
Lukáš Tinkl committed
196 197
        void openUrlFromDocument(const QUrl &url);
        void openUrlFromBookmarks(const QUrl &url);
Vishesh Handa's avatar
Vishesh Handa committed
198
        void handleDroppedUrls( const QList<QUrl>& urls );
199 200 201
        void slotGoToPage();
        void slotHistoryBack();
        void slotHistoryNext();
202
        void slotAddBookmark();
203
        void slotRenameBookmarkFromMenu();
204
        void slotRemoveBookmarkFromMenu();
205
        void slotRenameCurrentViewportBookmark();
206 207 208
        void slotPreviousBookmark();
        void slotNextBookmark();
        void slotFindNext();
Pino Toscano's avatar
Pino Toscano committed
209
        void slotFindPrev();
210
        bool slotSaveFileAs(bool showOkularArchiveAsDefaultFormat = false);
211 212 213
        void slotGetNewStuff();
        void slotNewConfig();
        void slotShowMenu(const Okular::Page *page, const QPoint &point);
Albert Astals Cid's avatar
Albert Astals Cid committed
214
        void slotShowTOCMenu(const Okular::DocumentViewport &vp, const QPoint &point, const QString &title);
215 216 217
        void slotShowProperties();
        void slotShowEmbeddedFiles();
        void slotShowLeftPanel();
218
        void slotShowBottomBar();
219 220 221 222
        void slotShowPresentation();
        void slotHidePresentation();
        void slotExportAs(QAction *);
        bool slotImportPSFile();
223
        void slotAboutBackend();
224
        void slotReload();
225 226
        void close();
        void cannotQuit();
227 228
        void slotShowFindBar();
        void slotHideFindBar();
229
        void slotJobStarted(KIO::Job *job);
230
        void slotJobFinished(KJob *job);
231 232
        void loadCancelled(const QString &reason);
        void setWindowTitleFromDocument();
233 234
        // can be connected to widget elements
        void updateViewActions();
235
        void updateBookmarksActions();
236
        void enableTOC(bool enable);
237
        void slotRebuildBookmarkMenu();
238
        void enableLayers( bool enable );
239
        void showSidebarSignaturesItem( bool show );
240

241
    public Q_SLOTS:
242
        bool saveFile() override;
243 244 245
        // connected to Shell action (and browserExtension), not local one
        void slotPrint();
        void slotFileDirty( const QString& );
246
        bool slotAttemptReload( bool oneShot = false, const QUrl &newUrl = QUrl() );
247
        void psTransformEnded(int, QProcess::ExitStatus);
248
        KConfigDialog * slotGeneratorPreferences();
249

250 251 252 253
        void errorMessage( const QString &message, int duration = 0 );
        void warningMessage( const QString &message, int duration = -1 );
        void noticeMessage( const QString &message, int duration = -1 );

254 255
        void moveSplitter( const int sideWidgetSize );

256
    private:
257
        bool aboutToShowContextMenu(QMenu *menu, QAction *action, QMenu *contextMenu);
Peter Wu's avatar
Peter Wu committed
258
        void showMenu(const Okular::Page *page, const QPoint &point, const QString &bookmarkTitle = QString(), const Okular::DocumentViewport &vp = DocumentViewport(), bool showTOCActions = false);
259
        bool eventFilter(QObject * watched, QEvent * event) override;
Frederik Gladhorn's avatar
Frederik Gladhorn committed
260
        Document::OpenResult doOpenFile(const QMimeType &mime, const QString &fileNameToOpen, bool *isCompressedFile);
261
        bool openUrl( const QUrl &url, bool swapInsteadOfOpening );
262

263
        void setupViewerActions();
264
        void setViewerShortcuts();
265 266
        void setupActions();

John Layt's avatar
John Layt committed
267
        void setupPrint( QPrinter &printer );
268
        bool doPrint( QPrinter &printer );
269
        bool handleCompressed(QString &destpath, const QString &path, KCompressionDevice::CompressionType compressionType );
270
        void rebuildBookmarkMenu( bool unplugActions = true );
271
        void updateAboutBackendAction();
Albert Astals Cid's avatar
Albert Astals Cid committed
272
        void unsetDummyMode();
273
        void slotRenameBookmark( const DocumentViewport &viewport );
274
        void slotRemoveBookmark( const DocumentViewport &viewport );
275
        void resetStartArguments();
276
        void checkNativeSaveDataLoss(bool *out_wontSaveForms, bool *out_wontSaveAnnotations) const;
277 278 279 280

        enum SaveAsFlag
        {
            NoSaveAsFlags = 0,         ///< No options
281
            SaveAsOkularArchive = 1    ///< Save as Okular Archive (.okular) instead of document's native format
282 283 284
        };
        Q_DECLARE_FLAGS( SaveAsFlags, SaveAsFlag )

285
        bool saveAs( const QUrl & saveUrl, SaveAsFlags flags );
286

287 288 289
        void setFileToWatch( const QString &filePath );
        void unsetFileToWatch();

Kevin Ottens's avatar
Kevin Ottens committed
290 291 292 293
#if PURPOSE_FOUND
        void slotShareActionFinished(const QJsonObject &output, int error, const QString &message);
#endif

294
        static int numberOfParts;
Albert Astals Cid's avatar
Albert Astals Cid committed
295

Frederik Gladhorn's avatar
Frederik Gladhorn committed
296
        QTemporaryFile *m_tempfile;
297 298 299

        // the document
        Okular::Document * m_document;
300
        QDateTime m_fileLastModified;
301
        QString m_temporaryLocalFile;
302
        bool isDocumentArchive;
303
        bool m_documentOpenWithPassword;
304
        bool m_swapInsteadOfOpening; // if set, the next open operation will replace the backing file (used when reloading just saved files)
305 306

        // main widgets
Pino Toscano's avatar
Pino Toscano committed
307
        Sidebar *m_sidebar;
308
        SearchWidget *m_searchWidget;
309
        FindBar * m_findBar;
310
        KMessageWidget * m_migrationMessage;
311 312
        KMessageWidget * m_topMessage;
        KMessageWidget * m_formsMessage;
313
        KMessageWidget * m_infoMessage;
314
        KMessageWidget * m_signatureMessage;
315 316 317
        QPointer<ThumbnailList> m_thumbnailList;
        QPointer<PageView> m_pageView;
        QPointer<TOC> m_toc;
Albert Astals Cid's avatar
Albert Astals Cid committed
318
        QPointer<MiniBarLogic> m_miniBarLogic;
319
        QPointer<MiniBar> m_miniBar;
320
        QPointer<MiniBar> m_pageNumberTool;
321
        QPointer<QWidget> m_bottomBar;
322 323 324
        QPointer<PresentationWidget> m_presentationWidget;
        QPointer<ProgressWidget> m_progressWidget;
        QPointer<PageSizeLabel> m_pageSizeLabel;
325 326
        QPointer<Reviews> m_reviewsWidget;
        QPointer<BookmarkList> m_bookmarkList;
327
        QPointer<Layers> m_layers;
328
        QPointer<SignaturePanel> m_signaturePanel;
329 330 331

        // document watcher (and reloader) variables
        KDirWatch *m_watcher;
332
        QString m_watchedFilePath, m_watchedFileSymlinkTarget;
333
        QTimer *m_dirtyHandler;
Lukáš Tinkl's avatar
Lukáš Tinkl committed
334
        QUrl m_oldUrl;
335
        Okular::DocumentViewport m_viewportDirty;
336
        bool m_isReloading;
337
        bool m_wasPresentationOpen;
338
        QWidget *m_dirtyToolboxItem;
339
        bool m_wasSidebarVisible;
340
        bool m_wasSidebarCollapsed;
341
        bool m_fileWasRemoved;
342
        Rotation m_dirtyPageRotation;
343 344 345 346 347

        // Remember the search history
        QStringList m_searchHistory;

        // actions
348 349 350 351 352 353 354 355 356 357 358 359 360
        QAction *m_gotoPage;
        QAction *m_prevPage;
        QAction *m_nextPage;
        QAction *m_beginningOfDocument;
        QAction *m_endOfDocument;
        QAction *m_historyBack;
        QAction *m_historyNext;
        QAction *m_addBookmark;
        QAction *m_renameBookmark;
        QAction *m_prevBookmark;
        QAction *m_nextBookmark;
        QAction *m_copy;
        QAction *m_selectAll;
361
        QAction *m_selectCurrentPage;
362 363 364
        QAction *m_find;
        QAction *m_findNext;
        QAction *m_findPrev;
365
        QAction *m_save;
366 367 368 369 370 371
        QAction *m_saveAs;
        QAction *m_saveCopyAs;
        QAction *m_printPreview;
        QAction *m_showProperties;
        QAction *m_showEmbeddedFiles;
        QAction *m_exportAs;
372
        QAction *m_exportAsText;
373
        QAction *m_exportAsDocArchive;
Kevin Ottens's avatar
Kevin Ottens committed
374 375 376
#if PURPOSE_FOUND
        QAction *m_share;
#endif
377
        QAction *m_showPresentation;
378
        QAction *m_openContainingFolder;
379 380
        KToggleAction* m_showMenuBarAction;
        KToggleAction* m_showLeftPanel;
381
        KToggleAction* m_showBottomBar;
382
        QAction* m_showSignaturePanel;
383
        KToggleFullScreenAction* m_showFullScreenAction;
384 385
        QAction *m_aboutBackend;
        QAction *m_reload;
386
        QMenu *m_exportAsMenu;
Kevin Ottens's avatar
Kevin Ottens committed
387 388 389
#if PURPOSE_FOUND
        Purpose::Menu *m_shareMenu;
#endif
390
        QAction *m_closeFindBar;
391
        DrawingToolActions *m_presentationDrawingActions;
392 393 394 395 396

        bool m_actionsSearched;
        BrowserExtension *m_bExtension;

        QList<Okular::ExportFormat> m_exportFormats;
397
        QList<QAction*> m_bookmarkActions;
398
        bool m_cliPresentation;
399
        bool m_cliPrint;
400
        bool m_cliPrintAndExit;
401
        QString m_addBookmarkText;
402
        QIcon m_addBookmarkIcon;
403

404
        EmbedMode m_embedMode;
405

Lukáš Tinkl's avatar
Lukáš Tinkl committed
406
        QUrl m_realUrl;
407

408
        KXMLGUIClient *m_generatorGuiClient;
409
        FileKeeper *m_keeper;
410

411 412 413
        // Timer for m_infoMessage
        QTimer *m_infoTimer;

Albert Astals Cid's avatar
Albert Astals Cid committed
414 415
        QString m_registerDbusName;

416 417 418
        // String to search in document startup
        QString m_textToFindOnOpen;

419
    private Q_SLOTS:
420
        void slotAnnotationPreferences();
Michel Ludwig's avatar
Michel Ludwig committed
421
        void slotHandleActivatedSourceReference(const QString& absFileName, int line, int col, bool *handled);
422
};
Wilco Greven's avatar
Wilco Greven committed
423

424 425
}

426 427
#endif

428
/* kate: replace-tabs on; indent-width 4; */