00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "part.h"
00025
00026
00027 #include <qapplication.h>
00028 #include <qfile.h>
00029 #include <qlayout.h>
00030 #include <qlabel.h>
00031 #include <qtimer.h>
00032 #include <QtGui/QPrinter>
00033 #include <QtGui/QPrintDialog>
00034
00035 #include <kvbox.h>
00036 #include <kaboutapplicationdialog.h>
00037 #include <kaction.h>
00038 #include <kactioncollection.h>
00039 #include <kdirwatch.h>
00040 #include <kstandardaction.h>
00041 #include <kparts/genericfactory.h>
00042 #include <kfiledialog.h>
00043 #include <kmessagebox.h>
00044 #include <knuminput.h>
00045 #include <kio/netaccess.h>
00046 #include <kmenu.h>
00047 #include <kxmlguiclient.h>
00048 #include <kxmlguifactory.h>
00049 #include <kservicetypetrader.h>
00050 #include <kstandarddirs.h>
00051 #include <kstandardshortcut.h>
00052 #include <ktemporaryfile.h>
00053 #include <ktoggleaction.h>
00054 #include <ktogglefullscreenaction.h>
00055 #include <kio/job.h>
00056 #include <kicon.h>
00057 #include <kfilterdev.h>
00058 #include <knewstuff2/engine.h>
00059 #include <kdeprintdialog.h>
00060 #include <kprintpreview.h>
00061
00062
00063 #include "aboutdata.h"
00064 #include "ui/pageview.h"
00065 #include "ui/toc.h"
00066 #include "ui/searchwidget.h"
00067 #include "ui/thumbnaillist.h"
00068 #include "ui/side_reviews.h"
00069 #include "ui/minibar.h"
00070 #include "ui/embeddedfilesdialog.h"
00071 #include "ui/propertiesdialog.h"
00072 #include "ui/presentationwidget.h"
00073 #include "ui/pagesizelabel.h"
00074 #include "ui/bookmarklist.h"
00075 #include "ui/findbar.h"
00076 #include "ui/sidebar.h"
00077 #include "ui/fileprinterpreview.h"
00078 #include "ui/guiutils.h"
00079 #include "conf/preferencesdialog.h"
00080 #include "settings.h"
00081 #include "core/bookmarkmanager.h"
00082 #include "core/document.h"
00083 #include "core/generator.h"
00084 #include "core/page.h"
00085 #include "core/fileprinter.h"
00086
00087 K_PLUGIN_FACTORY( okularPartFactory, registerPlugin< Part >(); )
00088 K_EXPORT_PLUGIN( okularPartFactory( okularAboutData( "okular", I18N_NOOP( "Okular" ) ) ) )
00089
00090 static QAction* actionForExportFormat( const Okular::ExportFormat& format, QObject *parent = 0 )
00091 {
00092 QAction *act = new QAction( format.description(), parent );
00093 if ( !format.icon().isNull() )
00094 {
00095 act->setIcon( format.icon() );
00096 }
00097 return act;
00098 }
00099
00100 static QString compressedMimeFor( const QString& mime_to_check )
00101 {
00102 static QHash< QString, QString > compressedMimeMap;
00103 if ( compressedMimeMap.isEmpty() )
00104 {
00105 compressedMimeMap[ QString::fromLatin1( "application/x-gzip" ) ] =
00106 QString::fromLatin1( "application/x-gzip" );
00107 compressedMimeMap[ QString::fromLatin1( "application/x-bzip" ) ] =
00108 QString::fromLatin1( "application/x-bzip" );
00109 compressedMimeMap[ QString::fromLatin1( "application/x-bzpdf" ) ] =
00110 QString::fromLatin1( "application/x-bzip" );
00111 compressedMimeMap[ QString::fromLatin1( "application/x-bzpostscript" ) ] =
00112 QString::fromLatin1( "application/x-bzip" );
00113 compressedMimeMap[ QString::fromLatin1( "application/x-bzdvi" ) ] =
00114 QString::fromLatin1( "application/x-bzip" );
00115 }
00116 QHash< QString, QString >::const_iterator it = compressedMimeMap.find( mime_to_check );
00117 if ( it != compressedMimeMap.end() )
00118 return it.value();
00119
00120 return QString();
00121 }
00122
00123 Part::Part(QWidget *parentWidget,
00124 QObject *parent,
00125 const QVariantList &args )
00126 : KParts::ReadOnlyPart(parent),
00127 m_tempfile( 0 ), m_showMenuBarAction( 0 ), m_showFullScreenAction( 0 ), m_actionsSearched( false ),
00128 m_cliPresentation(false), m_generatorGuiClient(0)
00129 {
00130
00131 QString newokularconffile = KStandardDirs::locateLocal( "config", "okularpartrc" );
00132 if ( !QFile::exists( newokularconffile ) )
00133 {
00134 QString oldkpdfconffile = KStandardDirs::locateLocal( "config", "kpdfpartrc" );
00135 if ( QFile::exists( oldkpdfconffile ) )
00136 QFile::copy( oldkpdfconffile, newokularconffile );
00137 }
00138
00139 QDBusConnection::sessionBus().registerObject("/okular", this, QDBusConnection::ExportScriptableSlots);
00140
00141
00142 connect(this, SIGNAL(started(KIO::Job *)), this, SLOT(setMimeTypes(KIO::Job *)));
00143
00144
00145 connect(this, SIGNAL(completed()), this, SLOT(setWindowTitleFromDocument()));
00146 connect(this, SIGNAL(canceled(const QString &)), this, SLOT(loadCancelled(const QString &)));
00147
00148
00149 m_bExtension = new BrowserExtension(this);
00150
00151
00152 setComponentData(okularPartFactory::componentData());
00153
00154 GuiUtils::setIconLoader( iconLoader() );
00155
00156 m_sidebar = new Sidebar( parentWidget );
00157 setWidget( m_sidebar );
00158
00159
00160 m_document = new Okular::Document(widget());
00161 connect( m_document, SIGNAL( linkFind() ), this, SLOT( slotFind() ) );
00162 connect( m_document, SIGNAL( linkGoToPage() ), this, SLOT( slotGoToPage() ) );
00163 connect( m_document, SIGNAL( linkPresentation() ), this, SLOT( slotShowPresentation() ) );
00164 connect( m_document, SIGNAL( linkEndPresentation() ), this, SLOT( slotHidePresentation() ) );
00165 connect( m_document, SIGNAL( openUrl(const KUrl &) ), this, SLOT( openUrlFromDocument(const KUrl &) ) );
00166 connect( m_document->bookmarkManager(), SIGNAL( openUrl(const KUrl &) ), this, SLOT( openUrlFromBookmarks(const KUrl &) ) );
00167 connect( m_document, SIGNAL( close() ), this, SLOT( close() ) );
00168
00169 if ( parent && parent->metaObject()->indexOfSlot( QMetaObject::normalizedSignature( "slotQuit()" ) ) != -1 )
00170 connect( m_document, SIGNAL( quit() ), parent, SLOT( slotQuit() ) );
00171 else
00172 connect( m_document, SIGNAL( quit() ), this, SLOT( cannotQuit() ) );
00173
00174
00175
00176
00177
00178
00179
00180
00181 int tbIndex;
00182
00183 m_toc = new TOC( 0, m_document );
00184 connect( m_toc, SIGNAL( hasTOC( bool ) ), this, SLOT( enableTOC( bool ) ) );
00185 tbIndex = m_sidebar->addItem( m_toc, KIcon(QApplication::isLeftToRight() ? "format-justify-left" : "format-justify-right"), i18n("Contents") );
00186 enableTOC( false );
00187
00188
00189 KVBox * thumbsBox = new ThumbnailsBox( 0 );
00190 thumbsBox->setSpacing( 6 );
00191 m_searchWidget = new SearchWidget( thumbsBox, m_document );
00192 m_thumbnailList = new ThumbnailList( thumbsBox, m_document );
00193
00194 connect( m_thumbnailList, SIGNAL( urlDropped( const KUrl& ) ), SLOT( openUrlFromDocument( const KUrl & )) );
00195 connect( m_thumbnailList, SIGNAL( rightClick(const Okular::Page *, const QPoint &) ), this, SLOT( slotShowMenu(const Okular::Page *, const QPoint &) ) );
00196 tbIndex = m_sidebar->addItem( thumbsBox, KIcon( "view-preview" ), i18n("Thumbnails") );
00197 m_sidebar->setCurrentIndex( tbIndex );
00198
00199
00200 Reviews * reviewsWidget = new Reviews( 0, m_document );
00201 m_sidebar->addItem( reviewsWidget, KIcon("draw-freehand"), i18n("Reviews") );
00202 m_sidebar->setItemEnabled( 2, false );
00203
00204
00205 BookmarkList * bookmarkList = new BookmarkList( m_document, 0 );
00206 m_sidebar->addItem( bookmarkList, KIcon("bookmarks"), i18n("Bookmarks") );
00207 m_sidebar->setItemEnabled( 3, false );
00208
00209
00210 #ifdef OKULAR_ENABLE_MINIBAR
00211 QWidget * miniBarContainer = new QWidget( 0 );
00212 m_sidebar->setBottomWidget( miniBarContainer );
00213 QVBoxLayout * miniBarLayout = new QVBoxLayout( miniBarContainer );
00214 miniBarLayout->setMargin( 0 );
00215
00216 miniBarLayout->addItem( new QSpacerItem( 6, 6, QSizePolicy::Fixed, QSizePolicy::Fixed ) );
00217
00218 QFrame * bevelContainer = new QFrame( miniBarContainer );
00219 bevelContainer->setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
00220 QVBoxLayout * bevelContainerLayout = new QVBoxLayout( bevelContainer );
00221 bevelContainerLayout->setMargin( 4 );
00222 m_progressWidget = new ProgressWidget( bevelContainer, m_document );
00223 bevelContainerLayout->addWidget( m_progressWidget );
00224 miniBarLayout->addWidget( bevelContainer );
00225 miniBarLayout->addItem( new QSpacerItem( 6, 6, QSizePolicy::Fixed, QSizePolicy::Fixed ) );
00226 #endif
00227
00228
00229 QWidget * rightContainer = new QWidget( 0 );
00230 m_sidebar->setMainWidget( rightContainer );
00231 QVBoxLayout * rightLayout = new QVBoxLayout( rightContainer );
00232 rightLayout->setMargin( 0 );
00233 rightLayout->setSpacing( 0 );
00234
00235
00236 m_topMessage = new PageViewTopMessage( rightContainer );
00237 m_topMessage->setup( i18n( "This document has embedded files. <a href=\"okular:/embeddedfiles\">Click here to see them</a> or go to File -> Embedded Files." ), KIcon( "mail-attachment" ) );
00238 connect( m_topMessage, SIGNAL( action() ), this, SLOT( slotShowEmbeddedFiles() ) );
00239 rightLayout->addWidget( m_topMessage );
00240 m_formsMessage = new PageViewTopMessage( rightContainer );
00241 m_formsMessage->setup( i18n( "This document has forms. Click on the button to interact with them, or use View -> Show Forms." ) );
00242 rightLayout->addWidget( m_formsMessage );
00243 m_pageView = new PageView( rightContainer, m_document );
00244 m_pageView->setFocus();
00245
00246 connect( m_pageView, SIGNAL( urlDropped( const KUrl& ) ), SLOT( openUrlFromDocument( const KUrl & )));
00247 connect( m_pageView, SIGNAL( rightClick(const Okular::Page *, const QPoint &) ), this, SLOT( slotShowMenu(const Okular::Page *, const QPoint &) ) );
00248 connect( m_document, SIGNAL( error( const QString&, int ) ), m_pageView, SLOT( errorMessage( const QString&, int ) ) );
00249 connect( m_document, SIGNAL( warning( const QString&, int ) ), m_pageView, SLOT( warningMessage( const QString&, int ) ) );
00250 connect( m_document, SIGNAL( notice( const QString&, int ) ), m_pageView, SLOT( noticeMessage( const QString&, int ) ) );
00251 rightLayout->addWidget( m_pageView );
00252 m_findBar = new FindBar( m_document, rightContainer );
00253 rightLayout->addWidget( m_findBar );
00254 QWidget * bottomBar = new QWidget( rightContainer );
00255 QHBoxLayout * bottomBarLayout = new QHBoxLayout( bottomBar );
00256 m_pageSizeLabel = new PageSizeLabel( bottomBar, m_document );
00257 bottomBarLayout->setMargin( 0 );
00258 bottomBarLayout->setSpacing( 0 );
00259 bottomBarLayout->addWidget( m_pageSizeLabel->antiWidget() );
00260 bottomBarLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
00261 m_miniBar = new MiniBar( bottomBar, m_document );
00262 bottomBarLayout->addWidget( m_miniBar );
00263 bottomBarLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
00264 bottomBarLayout->addWidget( m_pageSizeLabel );
00265 rightLayout->addWidget( bottomBar );
00266
00267 connect( reviewsWidget, SIGNAL( setAnnotationWindow( Okular::Annotation* ) ),
00268 m_pageView, SLOT( setAnnotationWindow( Okular::Annotation* ) ) );
00269 connect( reviewsWidget, SIGNAL( removeAnnotationWindow( Okular::Annotation* ) ),
00270 m_pageView, SLOT( removeAnnotationWindow( Okular::Annotation* ) ) );
00271
00272
00273 m_document->addObserver( this );
00274 m_document->addObserver( m_thumbnailList );
00275 m_document->addObserver( m_pageView );
00276 m_document->addObserver( m_toc );
00277 m_document->addObserver( m_miniBar );
00278 #ifdef OKULAR_ENABLE_MINIBAR
00279 m_document->addObserver( m_progressWidget );
00280 #endif
00281 m_document->addObserver( reviewsWidget );
00282 m_document->addObserver( m_pageSizeLabel );
00283 m_document->addObserver( bookmarkList );
00284
00285 connect( m_document->bookmarkManager(), SIGNAL( saved() ),
00286 this, SLOT( slotRebuildBookmarkMenu() ) );
00287
00288
00289 KActionCollection * ac = actionCollection();
00290
00291
00292 m_gotoPage = KStandardAction::gotoPage( this, SLOT( slotGoToPage() ), ac );
00293 m_gotoPage->setShortcut( QKeySequence(Qt::CTRL + Qt::Key_G) );
00294
00295 connect( m_miniBar, SIGNAL( gotoPage() ), m_gotoPage, SLOT( trigger() ) );
00296
00297 m_prevPage = KStandardAction::prior(this, SLOT(slotPreviousPage()), ac);
00298 m_prevPage->setIconText( i18nc( "Previous page", "Previous" ) );
00299 m_prevPage->setToolTip( i18n( "Go back to the Previous Page" ) );
00300 m_prevPage->setWhatsThis( i18n( "Moves to the previous page of the document" ) );
00301 m_prevPage->setShortcut( 0 );
00302
00303 connect( m_miniBar, SIGNAL( prevPage() ), m_prevPage, SLOT( trigger() ) );
00304 #ifdef OKULAR_ENABLE_MINIBAR
00305 connect( m_progressWidget, SIGNAL( prevPage() ), m_prevPage, SLOT( trigger() ) );
00306 #endif
00307
00308 m_nextPage = KStandardAction::next(this, SLOT(slotNextPage()), ac );
00309 m_nextPage->setIconText( i18nc( "Next page", "Next" ) );
00310 m_nextPage->setToolTip( i18n( "Advance to the Next Page" ) );
00311 m_nextPage->setWhatsThis( i18n( "Moves to the next page of the document" ) );
00312 m_nextPage->setShortcut( 0 );
00313
00314 connect( m_miniBar, SIGNAL( nextPage() ), m_nextPage, SLOT( trigger() ) );
00315 #ifdef OKULAR_ENABLE_MINIBAR
00316 connect( m_progressWidget, SIGNAL( nextPage() ), m_nextPage, SLOT( trigger() ) );
00317 #endif
00318
00319 m_firstPage = KStandardAction::firstPage( this, SLOT( slotGotoFirst() ), ac );
00320 ac->addAction("first_page", m_firstPage);
00321 m_firstPage->setWhatsThis( i18n( "Moves to the first page of the document" ) );
00322
00323 m_lastPage = KStandardAction::lastPage( this, SLOT( slotGotoLast() ), ac );
00324 ac->addAction("last_page",m_lastPage);
00325 m_lastPage->setWhatsThis( i18n( "Moves to the last page of the document" ) );
00326
00327
00328 m_historyBack = 0;
00329 m_historyNext = 0;
00330
00331 m_addBookmark = KStandardAction::addBookmark( this, SLOT( slotAddBookmark() ), ac );
00332 m_addBookmarkText = m_addBookmark->text();
00333 m_addBookmarkIcon = m_addBookmark->icon();
00334
00335 m_prevBookmark = ac->addAction("previous_bookmark");
00336 m_prevBookmark->setText(i18n( "Previous Bookmark" ));
00337 m_prevBookmark->setIcon(KIcon( "go-up-search" ));
00338 m_prevBookmark->setWhatsThis( i18n( "Go to the previous bookmarked page" ) );
00339 connect( m_prevBookmark, SIGNAL( triggered() ), this, SLOT( slotPreviousBookmark() ) );
00340
00341 m_nextBookmark = ac->addAction("next_bookmark");
00342 m_nextBookmark->setText(i18n( "Next Bookmark" ));
00343 m_nextBookmark->setIcon(KIcon( "go-down-search" ));
00344 m_nextBookmark->setWhatsThis( i18n( "Go to the next bookmarked page" ) );
00345 connect( m_nextBookmark, SIGNAL( triggered() ), this, SLOT( slotNextBookmark() ) );
00346
00347 m_copy = KStandardAction::create( KStandardAction::Copy, m_pageView, SLOT( copyTextSelection() ), ac );
00348
00349 m_selectAll = KStandardAction::selectAll( m_pageView, SLOT( selectAll() ), ac );
00350
00351
00352 m_find = KStandardAction::find( this, SLOT( slotShowFindBar() ), ac );
00353 QList<QKeySequence> s = m_find->shortcuts();
00354 s.append( QKeySequence( Qt::Key_Slash ) );
00355 m_find->setShortcuts( s );
00356 m_find->setEnabled( false );
00357
00358 m_findNext = KStandardAction::findNext( this, SLOT( slotFindNext() ), ac);
00359 m_findNext->setEnabled( false );
00360
00361 m_saveAs = KStandardAction::saveAs( this, SLOT( slotSaveFileAs() ), ac );
00362 ac->addAction("save",m_saveAs);
00363 m_saveAs->setEnabled( false );
00364
00365 QAction * prefs = KStandardAction::preferences( this, SLOT( slotPreferences() ), ac);
00366 if ( parent && ( parent->objectName() == QLatin1String( "okular::Shell" ) ) )
00367 {
00368 prefs->setText( i18n( "Configure okular..." ) );
00369 }
00370 else
00371 {
00372
00373 prefs->setText( i18n( "Configure Viewer..." ) );
00374 }
00375
00376 KAction * genPrefs = new KAction( ac );
00377 ac->addAction("options_configure_generators", genPrefs);
00378 genPrefs->setText( i18n( "Configure Backends..." ) );
00379 genPrefs->setIcon( KIcon( "configure" ) );
00380 genPrefs->setEnabled( m_document->configurableGenerators() > 0 );
00381 connect( genPrefs, SIGNAL( triggered( bool ) ), this, SLOT( slotGeneratorPreferences() ) );
00382
00383 m_printPreview = KStandardAction::printPreview( this, SLOT( slotPrintPreview() ), ac );
00384 m_printPreview->setEnabled( false );
00385
00386 m_showLeftPanel = ac->add<KToggleAction>("show_leftpanel");
00387 m_showLeftPanel->setText(i18n( "Show &Navigation Panel"));
00388 m_showLeftPanel->setIcon(KIcon( "view-sidetree" ));
00389 connect( m_showLeftPanel, SIGNAL( toggled( bool ) ), this, SLOT( slotShowLeftPanel() ) );
00390 m_showLeftPanel->setShortcut( Qt::Key_F7 );
00391 m_showLeftPanel->setChecked( Okular::Settings::showLeftPanel() );
00392 slotShowLeftPanel();
00393
00394 QAction * importPS = ac->addAction("import_ps");
00395 importPS->setText(i18n("&Import Postscript as PDF..."));
00396 importPS->setIcon(KIcon("document-import"));
00397 connect(importPS, SIGNAL(triggered()), this, SLOT(slotImportPSFile()));
00398 #if 0
00399 QAction * ghns = ac->addAction("get_new_stuff");
00400 ghns->setText(i18n("&Get Books From Internet..."));
00401 ghns->setIcon(KIcon("get-hot-new-stuff"));
00402 connect(ghns, SIGNAL(triggered()), this, SLOT(slotGetNewStuff()));
00403
00404 ghns->setShortcut( Qt::Key_G );
00405 #endif
00406
00407 m_showProperties = ac->addAction("properties");
00408 m_showProperties->setText(i18n("&Properties"));
00409 m_showProperties->setIcon(KIcon("document-properties"));
00410 connect(m_showProperties, SIGNAL(triggered()), this, SLOT(slotShowProperties()));
00411 m_showProperties->setEnabled( false );
00412
00413 m_showEmbeddedFiles = ac->addAction("embedded_files");
00414 m_showEmbeddedFiles->setText(i18n("&Embedded Files"));
00415 m_showEmbeddedFiles->setIcon( KIcon( "mail-attachment" ) );
00416 connect(m_showEmbeddedFiles, SIGNAL(triggered()), this, SLOT(slotShowEmbeddedFiles()));
00417 m_showEmbeddedFiles->setEnabled( false );
00418
00419 m_showPresentation = ac->addAction("presentation");
00420 m_showPresentation->setText(i18n("P&resentation"));
00421 m_showPresentation->setIcon( KIcon( "view-presentation" ) );
00422 connect(m_showPresentation, SIGNAL(triggered()), this, SLOT(slotShowPresentation()));
00423 m_showPresentation->setShortcut( QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_P ) );
00424 m_showPresentation->setEnabled( false );
00425
00426 m_exportAs = ac->addAction("file_export_as");
00427 m_exportAs->setText(i18n("E&xport As"));
00428 m_exportAs->setIcon( KIcon( "document-export" ) );
00429 QMenu *menu = new QMenu(widget());
00430 connect(menu, SIGNAL(triggered(QAction *)), this, SLOT(slotExportAs(QAction *)));
00431 m_exportAs->setMenu( menu );
00432 m_exportAsText = actionForExportFormat( Okular::ExportFormat::standardFormat( Okular::ExportFormat::PlainText ), menu );
00433 menu->addAction( m_exportAsText );
00434 m_exportAs->setEnabled( false );
00435 m_exportAsText->setEnabled( false );
00436
00437 m_aboutBackend = ac->addAction("help_about_backend");
00438 m_aboutBackend->setText(i18n("About backend..."));
00439 m_aboutBackend->setEnabled( false );
00440 connect(m_aboutBackend, SIGNAL(triggered()), this, SLOT(slotAboutBackend()));
00441
00442 KAction *reload = ac->add<KAction>( "file_reload" );
00443 reload->setText( i18n( "Reloa&d" ) );
00444 reload->setIcon( KIcon( "view-refresh" ) );
00445 reload->setWhatsThis( i18n( "Reload the current document from disk." ) );
00446 connect( reload, SIGNAL(triggered()), this, SLOT(slotReload()) );
00447 reload->setShortcut( KStandardShortcut::reload() );
00448 m_reload = reload;
00449
00450 KAction *closeFindBar = new KAction( i18n( "Close &Find Bar" ), ac );
00451 ac->addAction("close_find_bar", closeFindBar);
00452 connect(closeFindBar, SIGNAL(triggered()), this, SLOT(slotHideFindBar()));
00453 closeFindBar->setShortcut( QKeySequence( Qt::Key_Escape ) );
00454 widget()->addAction(closeFindBar);
00455
00456
00457 m_watcher = new KDirWatch( this );
00458 connect( m_watcher, SIGNAL( dirty( const QString& ) ), this, SLOT( slotFileDirty( const QString& ) ) );
00459 m_dirtyHandler = new QTimer( this );
00460 m_dirtyHandler->setSingleShot( true );
00461 connect( m_dirtyHandler, SIGNAL( timeout() ),this, SLOT( slotDoFileDirty() ) );
00462
00463 slotNewConfig();
00464
00465
00466 KService::List offers = KServiceTypeTrader::self()->query("DBUS/Text-to-Speech", "Name == 'KTTSD'");
00467 Okular::Settings::setUseKTTSD( !offers.isEmpty() );
00468 Okular::Settings::self()->writeConfig();
00469
00470 rebuildBookmarkMenu( false );
00471
00472
00473 setXMLFile("part.rc");
00474
00475 m_pageView->setupBaseActions( actionCollection() );
00476
00477
00478 updateViewActions();
00479
00480 m_dummyMode = true;
00481 m_sidebar->setSidebarVisibility( false );
00482 if ( !args.contains( QVariant( "Print/Preview" ) ) ) unsetDummyMode();
00483 }
00484
00485
00486 Part::~Part()
00487 {
00488 if ( m_document->isOpened() )
00489 Part::closeUrl();
00490
00491 delete m_toc;
00492 delete m_pageView;
00493 delete m_thumbnailList;
00494 delete m_miniBar;
00495 #ifdef OKULAR_ENABLE_MINIBAR
00496 delete m_progressWidget;
00497 #endif
00498 delete m_pageSizeLabel;
00499
00500 delete m_document;
00501
00502 delete m_tempfile;
00503
00504 qDeleteAll( m_bookmarkActions );
00505 }
00506
00507
00508 bool Part::openDocument(const KUrl& url, uint page)
00509 {
00510 Okular::DocumentViewport vp( page - 1 );
00511 vp.rePos.enabled = true;
00512 vp.rePos.normalizedX = 0;
00513 vp.rePos.normalizedY = 0;
00514 vp.rePos.pos = Okular::DocumentViewport::TopLeft;
00515 if ( vp.isValid() )
00516 m_document->setNextDocumentViewport( vp );
00517 return openUrl( url );
00518 }
00519
00520
00521 void Part::startPresentation()
00522 {
00523 m_cliPresentation = true;
00524 }
00525
00526
00527 QStringList Part::supportedMimeTypes() const
00528 {
00529 return m_document->supportedMimeTypes();
00530 }
00531
00532
00533 KUrl Part::realUrl() const
00534 {
00535 if ( !m_realUrl.isEmpty() )
00536 return m_realUrl;
00537
00538 return url();
00539 }
00540
00541
00542 void Part::openUrlFromDocument(const KUrl &url)
00543 {
00544 if (m_dummyMode) return;
00545
00546 m_bExtension->openUrlNotify();
00547 m_bExtension->setLocationBarUrl(url.prettyUrl());
00548 openUrl(url);
00549 }
00550
00551 void Part::openUrlFromBookmarks(const KUrl &_url)
00552 {
00553 KUrl url = _url;
00554 Okular::DocumentViewport vp( _url.htmlRef() );
00555 if ( vp.isValid() )
00556 m_document->setNextDocumentViewport( vp );
00557 url.setHTMLRef( QString() );
00558 if ( m_document->currentDocument() == url )
00559 {
00560 if ( vp.isValid() )
00561 m_document->setViewport( vp );
00562 }
00563 else
00564 openUrl( url );
00565 }
00566
00567 void Part::setMimeTypes(KIO::Job *job)
00568 {
00569 if (job)
00570 {
00571 QStringList supportedMimeTypes = m_document->supportedMimeTypes();
00572 job->addMetaData("accept", supportedMimeTypes.join(", ") + ", */*;q=0.5");
00573 }
00574 }
00575
00576 void Part::loadCancelled(const QString &reason)
00577 {
00578 emit setWindowCaption( QString() );
00579
00580
00581
00582
00583 if (m_viewportDirty.pageNumber == -1)
00584 {
00585 if (!reason.isEmpty())
00586 {
00587 KMessageBox::error( widget(), i18n("Could not open %1. Reason: %2", url().prettyUrl(), reason ) );
00588 }
00589 else
00590 {
00591 KMessageBox::error( widget(), i18n("Could not open %1", url().prettyUrl() ) );
00592 }
00593 }
00594 }
00595
00596 void Part::setWindowTitleFromDocument()
00597 {
00598
00599 QString title = m_document->metaData( "DocumentTitle" ).toString();
00600 if ( !title.isEmpty() && !title.trimmed().isEmpty() )
00601 {
00602 emit setWindowCaption( title );
00603 }
00604 else
00605 {
00606 emit setWindowCaption( realUrl().fileName() );
00607 }
00608 }
00609
00610 void Part::slotGeneratorPreferences( )
00611 {
00612
00613
00614 if ( KConfigDialog::showDialog( "generator_prefs" ) )
00615 return;
00616
00617
00618 KConfigDialog * dialog = new KConfigDialog( m_pageView, "generator_prefs", Okular::Settings::self() );
00619 dialog->setCaption( i18n( "Configure Backends" ) );
00620
00621 m_document->fillConfigDialog( dialog );
00622
00623
00624 connect( dialog, SIGNAL( settingsChanged( const QString& ) ), this, SLOT( slotNewGeneratorConfig() ) );
00625 dialog->show();
00626 }
00627
00628
00629 void Part::notifySetup( const QVector< Okular::Page * > & , int setupFlags )
00630 {
00631 if ( !( setupFlags & Okular::DocumentObserver::DocumentChanged ) )
00632 return;
00633
00634 rebuildBookmarkMenu();
00635 updateAboutBackendAction();
00636 m_searchWidget->setEnabled( m_document->supportsSearching() );
00637 }
00638
00639 void Part::notifyViewportChanged( bool )
00640 {
00641
00642 static int lastPage = -1;
00643 int viewportPage = m_document->viewport().pageNumber;
00644 if ( viewportPage != lastPage )
00645 {
00646 updateViewActions();
00647 lastPage = viewportPage;
00648 }
00649 }
00650
00651 void Part::notifyPageChanged( int page, int flags )
00652 {
00653 if ( !(flags & Okular::DocumentObserver::Bookmark ) )
00654 return;
00655
00656 rebuildBookmarkMenu();
00657 if ( page == m_document->viewport().pageNumber )
00658 updateBookmarksActions();
00659 }
00660
00661
00662 void Part::goToPage(uint i)
00663 {
00664 if ( i <= m_document->pages() )
00665 m_document->setViewportPage( i - 1 );
00666 }
00667
00668
00669 void Part::openDocument( const QString &doc )
00670 {
00671 openUrl( KUrl( doc ) );
00672 }
00673
00674
00675 uint Part::pages()
00676 {
00677 return m_document->pages();
00678 }
00679
00680
00681 uint Part::currentPage()
00682 {
00683 return m_document->pages() ? m_document->currentPage() + 1 : 0;
00684 }
00685
00686
00687 QString Part::currentDocument()
00688 {
00689 return m_document->currentDocument().pathOrUrl();
00690 }
00691
00692
00693 bool Part::slotImportPSFile()
00694 {
00695 QString app = KStandardDirs::findExe( "ps2pdf" );
00696 if ( app.isEmpty() )
00697 {
00698
00699 KMessageBox::error( widget(), i18n( "The program \"ps2pdf\" was not found, so okular can not import PS files using it." ), i18n("ps2pdf not found") );
00700 return false;
00701 }
00702
00703 KUrl url = KFileDialog::getOpenUrl( KUrl(), "application/postscript", this->widget() );
00704 if ( url.isLocalFile() )
00705 {
00706 KTemporaryFile tf;
00707 tf.setSuffix( ".pdf" );
00708 tf.setAutoRemove( false );
00709 if ( !tf.open() )
00710 return false;
00711 m_temporaryLocalFile = tf.fileName();
00712 tf.close();
00713
00714 setLocalFilePath( url.path() );
00715 QStringList args;
00716 QProcess *p = new QProcess();
00717 args << url.toLocalFile() << m_temporaryLocalFile;
00718 m_pageView->displayMessage(i18n("Importing PS file as PDF (this may take a while)..."));
00719 connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(psTransformEnded(int, QProcess::ExitStatus)));
00720 p->start(app, args);
00721 return true;
00722 }
00723
00724 m_temporaryLocalFile.clear();
00725 return false;
00726 }
00727
00728
00729 bool Part::openFile()
00730 {
00731 KMimeType::Ptr mime;
00732 if ( !arguments().mimeType().isEmpty() )
00733 {
00734 mime = KMimeType::mimeType( arguments().mimeType() );
00735 }
00736 if ( !mime )
00737 {
00738 mime = KMimeType::findByPath( localFilePath() );
00739 }
00740 bool isCompressedFile = false;
00741 bool uncompressOk = true;
00742 QString fileNameToOpen = localFilePath();
00743 QString compressedMime = compressedMimeFor( mime->name() );
00744 if ( compressedMime.isEmpty() )
00745 compressedMime = compressedMimeFor( mime->parentMimeType() );
00746 if ( !compressedMime.isEmpty() )
00747 {
00748 isCompressedFile = true;
00749 uncompressOk = handleCompressed( fileNameToOpen, localFilePath(), compressedMime );
00750 mime = KMimeType::findByPath( fileNameToOpen );
00751 }
00752 bool ok = false;
00753 if ( uncompressOk )
00754 {
00755 ok = m_document->openDocument( fileNameToOpen, url(), mime );
00756 }
00757 bool canSearch = m_document->supportsSearching();
00758
00759
00760 m_find->setEnabled( ok && canSearch );
00761 m_findNext->setEnabled( ok && canSearch );
00762 m_saveAs->setEnabled( ok );
00763 m_printPreview->setEnabled( ok && m_document->printingSupport() != Okular::Document::NoPrinting );
00764 m_showProperties->setEnabled( ok );
00765 bool hasEmbeddedFiles = ok && m_document->embeddedFiles() && m_document->embeddedFiles()->count() > 0;
00766 m_showEmbeddedFiles->setEnabled( hasEmbeddedFiles );
00767 m_topMessage->setVisible( hasEmbeddedFiles );
00768
00769 m_formsMessage->setVisible( ok && m_pageView->toggleFormsAction() && m_pageView->toggleFormsAction()->isEnabled() );
00770 m_showPresentation->setEnabled( ok );
00771 if ( ok )
00772 {
00773 m_exportFormats = m_document->exportFormats();
00774 QList<Okular::ExportFormat>::ConstIterator it = m_exportFormats.constBegin();
00775 QList<Okular::ExportFormat>::ConstIterator itEnd = m_exportFormats.constEnd();
00776 QMenu *menu = m_exportAs->menu();
00777 for ( ; it != itEnd; ++it )
00778 {
00779 menu->addAction( actionForExportFormat( *it ) );
00780 }
00781 if ( isCompressedFile )
00782 {
00783 m_realUrl = url();
00784 }
00785 }
00786 m_exportAsText->setEnabled( ok && m_document->canExportToText() );
00787 m_exportAs->setEnabled( ok );
00788
00789
00790 updateViewActions();
00791
00792 if ( !ok )
00793 {
00794
00795 m_pageView->widget()->update();
00796 m_thumbnailList->update();
00797 return false;
00798 }
00799
00800
00801 if ( url().isLocalFile() && !m_watcher->contains( localFilePath() ) )
00802 m_watcher->addFile(localFilePath());
00803
00804
00805 if ( m_document->metaData( "OpenTOC" ).toBool() && m_sidebar->isItemEnabled( 0 ) )
00806 {
00807 m_sidebar->setCurrentIndex( 0 );
00808 }
00809
00810
00811 if ( m_document->metaData( "StartFullScreen" ).toBool() || m_cliPresentation )
00812 {
00813 if ( !m_cliPresentation )
00814 KMessageBox::information( m_presentationWidget, i18n("The document is going to be launched on presentation mode because the file requested it."), QString(), "autoPresentationWarning" );
00815 m_cliPresentation = false;
00816 QMetaObject::invokeMethod(this, "slotShowPresentation", Qt::QueuedConnection);
00817 }
00818 m_generatorGuiClient = factory() ? m_document->guiClient() : 0;
00819 if ( m_generatorGuiClient )
00820 factory()->addClient( m_generatorGuiClient );
00821 return true;
00822 }
00823
00824 bool Part::openUrl(const KUrl &url)
00825 {
00826
00827 bool openOk = KParts::ReadOnlyPart::openUrl( url );
00828
00829 if ( openOk )
00830 {
00831 m_viewportDirty.pageNumber = -1;
00832
00833 setWindowTitleFromDocument();
00834 }
00835
00836 emit enablePrintAction(openOk && m_document->printingSupport() != Okular::Document::NoPrinting);
00837 return openOk;
00838 }
00839
00840
00841 bool Part::closeUrl()
00842 {
00843 if (!m_temporaryLocalFile.isNull() && m_temporaryLocalFile != localFilePath())
00844 {
00845 QFile::remove( m_temporaryLocalFile );
00846 m_temporaryLocalFile.clear();
00847 }
00848
00849 slotHidePresentation();
00850 m_find->setEnabled( false );
00851 m_findNext->setEnabled( false );
00852 m_saveAs->setEnabled( false );
00853 m_printPreview->setEnabled( false );
00854 m_showProperties->setEnabled( false );
00855 m_showEmbeddedFiles->setEnabled( false );
00856 m_exportAs->setEnabled( false );
00857 m_exportAsText->setEnabled( false );
00858 m_exportFormats.clear();
00859 QMenu *menu = m_exportAs->menu();
00860 QList<QAction*> acts = menu->actions();
00861 int num = acts.count();
00862 for ( int i = 1; i < num; ++i )
00863 {
00864 menu->removeAction( acts.at(i) );
00865 delete acts.at(i);
00866 }
00867 m_showPresentation->setEnabled( false );
00868 emit setWindowCaption("");
00869 emit enablePrintAction(false);
00870 m_realUrl = KUrl();
00871 if ( url().isLocalFile() )
00872 m_watcher->removeFile( localFilePath() );
00873 if ( m_generatorGuiClient )
00874 factory()->removeClient( m_generatorGuiClient );
00875 m_generatorGuiClient = 0;
00876 m_document->closeDocument();
00877 updateViewActions();
00878 m_searchWidget->clearText();
00879 delete m_tempfile;
00880 m_tempfile = 0;
00881 return KParts::ReadOnlyPart::closeUrl();
00882 }
00883
00884
00885 void Part::close()
00886 {
00887
00888 if (parent() && (parent()->objectName() == QLatin1String("okular/okular__Shell")))
00889 {
00890 closeUrl();
00891 }
00892 else KMessageBox::information( widget(), i18n( "This link points to a close document action that does not work when using the embedded viewer." ), QString(), "warnNoCloseIfNotInOkular" );
00893 }
00894
00895
00896 void Part::cannotQuit()
00897 {
00898 KMessageBox::information( widget(), i18n( "This link points to a quit application action that does not work when using the embedded viewer." ), QString(), "warnNoQuitIfNotInOkular" );
00899 }
00900
00901
00902 void Part::slotShowLeftPanel()
00903 {
00904 bool showLeft = m_showLeftPanel->isChecked();
00905 Okular::Settings::setShowLeftPanel( showLeft );
00906 Okular::Settings::self()->writeConfig();
00907
00908 m_sidebar->setSidebarVisibility( showLeft );
00909 }
00910
00911
00912 void Part::slotFileDirty( const QString& fileName )
00913 {
00914
00915
00916
00917
00918
00919 if ( fileName == localFilePath() )
00920 {
00921 m_dirtyHandler->start( 750 );
00922 }
00923 }
00924
00925
00926 void Part::slotDoFileDirty()
00927 {
00928
00929 if ( m_viewportDirty.pageNumber == -1 )
00930 {
00931
00932 m_viewportDirty = m_document->viewport();
00933
00934
00935 m_dirtyToolboxIndex = m_sidebar->currentIndex();
00936 m_wasSidebarVisible = m_sidebar->isSidebarVisible();
00937
00938
00939 m_wasPresentationOpen = ((PresentationWidget*)m_presentationWidget != 0);
00940
00941
00942 m_pageView->displayMessage( i18n("Reloading the document...") );
00943 }
00944
00945
00946 if ( KParts::ReadOnlyPart::openUrl( url() ) )
00947 {
00948
00949 if ( m_viewportDirty.pageNumber >= (int) m_document->pages() )
00950 m_viewportDirty.pageNumber = (int) m_document->pages() - 1;
00951 m_document->setViewport( m_viewportDirty );
00952 m_viewportDirty.pageNumber = -1;
00953 if ( m_sidebar->currentIndex() != m_dirtyToolboxIndex && m_sidebar->isItemEnabled( m_dirtyToolboxIndex ) )
00954 {
00955 m_sidebar->setCurrentIndex( m_dirtyToolboxIndex );
00956 }
00957 if ( m_sidebar->isSidebarVisible() != m_wasSidebarVisible )
00958 {
00959 m_sidebar->setCurrentIndex( m_sidebar->currentIndex() );
00960 }
00961 if (m_wasPresentationOpen) slotShowPresentation();
00962 emit enablePrintAction(true && m_document->printingSupport() != Okular::Document::NoPrinting);
00963 }
00964 else
00965 {
00966
00967 m_watcher->addFile(localFilePath());
00968 m_dirtyHandler->start( 750 );
00969 }
00970 }
00971
00972
00973 void Part::updateViewActions()
00974 {
00975 bool opened = m_document->pages() > 0;
00976 if ( opened )
00977 {
00978 bool atBegin = m_document->currentPage() < 1;
00979 bool atEnd = m_document->currentPage() >= (m_document->pages() - 1);
00980 m_gotoPage->setEnabled( m_document->pages() > 1 );
00981 m_firstPage->setEnabled( !atBegin );
00982 m_prevPage->setEnabled( !atBegin );
00983 m_lastPage->setEnabled( !atEnd );
00984 m_nextPage->setEnabled( !atEnd );
00985 if (m_historyBack) m_historyBack->setEnabled( !m_document->historyAtBegin() );
00986 if (m_historyNext) m_historyNext->setEnabled( !m_document->historyAtEnd() );
00987 m_reload->setEnabled( true );
00988 m_copy->setEnabled( true );
00989 m_selectAll->setEnabled( true );
00990 }
00991 else
00992 {
00993 m_gotoPage->setEnabled( false );
00994 m_firstPage->setEnabled( false );
00995 m_lastPage->setEnabled( false );
00996 m_prevPage->setEnabled( false );
00997 m_nextPage->setEnabled( false );
00998 if (m_historyBack) m_historyBack->setEnabled( false );
00999 if (m_historyNext) m_historyNext->setEnabled( false );
01000 m_reload->setEnabled( false );
01001 m_copy->setEnabled( false );
01002 m_selectAll->setEnabled( false );
01003 }
01004 updateBookmarksActions();
01005 }
01006
01007
01008 void Part::updateBookmarksActions()
01009 {
01010 bool opened = m_document->pages() > 0;
01011 if ( opened )
01012 {
01013 m_addBookmark->setEnabled( true );
01014 if ( m_document->bookmarkManager()->isBookmarked( m_document->currentPage() ) )
01015 {
01016 m_addBookmark->setText( i18n( "Remove Bookmark" ) );
01017 m_addBookmark->setIcon( KIcon( "edit-delete-bookmark" ) );
01018 }
01019 else
01020 {
01021 m_addBookmark->setText( m_addBookmarkText );
01022 m_addBookmark->setIcon( m_addBookmarkIcon );
01023 }
01024 }
01025 else
01026 {
01027 m_addBookmark->setEnabled( false );
01028 m_addBookmark->setText( m_addBookmarkText );
01029 m_addBookmark->setIcon( m_addBookmarkIcon );
01030 }
01031 }
01032
01033
01034 void Part::enableTOC(bool enable)
01035 {
01036 m_sidebar->setItemEnabled(0, enable);
01037 }
01038
01039 void Part::slotRebuildBookmarkMenu()
01040 {
01041 rebuildBookmarkMenu();
01042 }
01043
01044 void Part::slotShowFindBar()
01045 {
01046 m_findBar->show();
01047 m_findBar->focusAndSetCursor();
01048 }
01049
01050 void Part::slotHideFindBar()
01051 {
01052 m_findBar->hide();
01053 m_pageView->setFocus();
01054 }
01055
01056
01057 class GotoPageDialog : public KDialog
01058 {
01059 public:
01060 GotoPageDialog(QWidget *p, int current, int max) : KDialog(p)
01061 {
01062 setCaption(i18n("Go to Page"));
01063 setButtons(Ok | Cancel);
01064 setDefaultButton(Ok);
01065
01066 QWidget *w = new QWidget(this);
01067 setMainWidget(w);
01068
01069 QVBoxLayout *topLayout = new QVBoxLayout(w);
01070 topLayout->setMargin(0);
01071 topLayout->setSpacing(spacingHint());
01072 e1 = new KIntNumInput(current, w);
01073 e1->setRange(1, max);
01074 e1->setEditFocus(true);
01075 e1->setSliderEnabled(true);
01076
01077 QLabel *label = new QLabel(i18n("&Page:"), w);
01078 label->setBuddy(e1);
01079 topLayout->addWidget(label);
01080 topLayout->addWidget(e1);
01081
01082 topLayout->addSpacing(spacingHint());
01083 topLayout->addStretch(10);
01084 e1->setFocus();
01085 }
01086
01087 int getPage() const
01088 {
01089 return e1->value();
01090 }
01091
01092 protected:
01093 KIntNumInput *e1;
01094 };
01095
01096
01097 void Part::slotGoToPage()
01098 {
01099 GotoPageDialog pageDialog( m_pageView, m_document->currentPage() + 1, m_document->pages() );
01100 if ( pageDialog.exec() == QDialog::Accepted )
01101 m_document->setViewportPage( pageDialog.getPage() - 1 );
01102 }
01103
01104
01105 void Part::slotPreviousPage()
01106 {
01107 if ( m_document->isOpened() && !(m_document->currentPage() < 1) )
01108 m_document->setViewportPage( m_document->currentPage() - 1 );
01109 }
01110
01111
01112 void Part::slotNextPage()
01113 {
01114 if ( m_document->isOpened() && m_document->currentPage() < (m_document->pages() - 1) )
01115 m_document->setViewportPage( m_document->currentPage() + 1 );
01116 }
01117
01118
01119 void Part::slotGotoFirst()
01120 {
01121 if ( m_document->isOpened() )
01122 m_document->setViewportPage( 0 );
01123 }
01124
01125
01126 void Part::slotGotoLast()
01127 {
01128 if ( m_document->isOpened() )
01129 m_document->setViewportPage( m_document->pages() - 1 );
01130 }
01131
01132
01133 void Part::slotHistoryBack()
01134 {
01135 m_document->setPrevViewport();
01136 }
01137
01138
01139 void Part::slotHistoryNext()
01140 {
01141 m_document->setNextViewport();
01142 }
01143
01144
01145 void Part::slotAddBookmark()
01146 {
01147 uint current = m_document->currentPage();
01148 if ( m_document->bookmarkManager()->isBookmarked( current ) )
01149 {
01150 m_document->bookmarkManager()->removeBookmark( current );
01151 }
01152 else
01153 {
01154 m_document->bookmarkManager()->addBookmark( current );
01155 }
01156 }
01157
01158
01159 void Part::slotPreviousBookmark()
01160 {
01161 uint current = m_document->currentPage();
01162
01163 if ( current == 0 )
01164 return;
01165
01166 for ( int i = current - 1; i >= 0; --i )
01167 {
01168 if ( m_document->bookmarkManager()->isBookmarked( i ) )
01169 {
01170 m_document->setViewportPage( i );
01171 break;
01172 }
01173 }
01174 }
01175
01176
01177 void Part::slotNextBookmark()
01178 {
01179 uint current = m_document->currentPage();
01180 uint pages = m_document->pages();
01181
01182 if ( current == pages )
01183 return;
01184
01185 for ( uint i = current + 1; i < pages; ++i )
01186 {
01187 if ( m_document->bookmarkManager()->isBookmarked( i ) )
01188 {
01189 m_document->setViewportPage( i );
01190 break;
01191 }
01192 }
01193 }
01194
01195
01196 void Part::slotFind()
01197 {
01198
01199
01200 if ( (PresentationWidget*)m_presentationWidget != 0 )
01201 {
01202 m_presentationWidget->slotFind();
01203 }
01204 else
01205 {
01206 slotShowFindBar();
01207 }
01208 }
01209
01210
01211 void Part::slotFindNext()
01212 {
01213 if (m_findBar->isHidden())
01214 slotShowFindBar();
01215 else
01216 m_findBar->findNext();
01217 }
01218
01219
01220 void Part::slotSaveFileAs()
01221 {
01222 if (m_dummyMode) return;
01223
01224 KUrl saveUrl = KFileDialog::getSaveUrl( url().isLocalFile() ? url().url() : url().fileName(), QString(), widget() );
01225 if ( saveUrl.isValid() && !saveUrl.isEmpty() )
01226 {
01227 if ( KIO::NetAccess::exists( saveUrl, KIO::NetAccess::DestinationSide, widget() ) )
01228 {
01229 if (KMessageBox::warningContinueCancel( widget(), i18n("A file named \"%1\" already exists. Are you sure you want to overwrite it?", saveUrl.fileName()), QString(), KGuiItem(i18n("Overwrite"))) != KMessageBox::Continue)
01230 return;
01231 }
01232
01233 KIO::Job *copyJob = KIO::file_copy( localFilePath(), saveUrl, -1, KIO::Overwrite );
01234 if ( !KIO::NetAccess::synchronousRun( copyJob, widget() ) )
01235 KMessageBox::information( widget(), i18n("File could not be saved in '%1'. Try to save it to another location.", saveUrl.prettyUrl() ) );
01236 }
01237 }
01238
01239
01240 void Part::slotGetNewStuff()
01241 {
01242 KNS::Engine engine(widget());
01243 engine.init( "okular.knsrc" );
01244
01245 KNS::Entry::List entries = engine.downloadDialogModal( m_pageView );
01246 Q_UNUSED( entries )
01247 }
01248
01249
01250 void Part::slotPreferences()
01251 {
01252
01253
01254 if ( PreferencesDialog::showDialog( "preferences" ) )
01255 return;
01256
01257
01258 PreferencesDialog * dialog = new PreferencesDialog( m_pageView, Okular::Settings::self() );
01259
01260 connect( dialog, SIGNAL( settingsChanged( const QString & ) ), this, SLOT( slotNewConfig() ) );
01261
01262 dialog->show();
01263 }
01264
01265
01266 void Part::slotNewConfig()
01267 {
01268
01269
01270
01271
01272 bool watchFile = Okular::Settings::watchFile();
01273 if ( watchFile && m_watcher->isStopped() )
01274 m_watcher->startScan();
01275 if ( !watchFile && !m_watcher->isStopped() )
01276 {
01277 m_dirtyHandler->stop();
01278 m_watcher->stopScan();
01279 }
01280
01281
01282 m_pageView->reparseConfig();
01283
01284
01285 m_document->reparseConfig();
01286
01287
01288 if ( m_sidebar->isItemEnabled(0) )
01289 m_toc->reparseConfig();
01290
01291
01292 if ( Okular::Settings::showLeftPanel() && !m_thumbnailList->isHidden() )
01293 m_thumbnailList->updateWidgets();
01294 }
01295
01296
01297 void Part::slotNewGeneratorConfig()
01298 {
01299
01300
01301
01302
01303
01304
01305
01306 m_pageView->reparseConfig();
01307
01308
01309 if ( m_sidebar->isItemEnabled(0) )
01310 m_toc->reparseConfig();
01311
01312
01313 if ( Okular::Settings::showLeftPanel() && !m_thumbnailList->isHidden() )
01314 m_thumbnailList->updateWidgets();
01315 }
01316
01317
01318 void Part::slotPrintPreview()
01319 {
01320 if (m_document->pages() == 0) return;
01321
01322 QPrinter printer;
01323
01324
01325 if ( m_document->printingSupport() == Okular::Document::NativePrinting )
01326 {
01327 KPrintPreview previewdlg( &printer, widget() );
01328 setupPrint( printer );
01329 doPrint( printer );
01330 previewdlg.exec();
01331 }
01332 else
01333 {
01334
01335 KTemporaryFile tf;
01336 tf.setAutoRemove( true );
01337 tf.setSuffix( ".ps" );
01338 tf.open();
01339 printer.setOutputFileName( tf.fileName() );
01340 tf.close();
01341 setupPrint( printer );
01342 doPrint( printer );
01343 if ( QFile::exists( printer.outputFileName() ) )
01344 {
01345 Okular::FilePrinterPreview previewdlg( printer.outputFileName(), widget() );
01346 previewdlg.exec();
01347 }
01348 }
01349 }
01350
01351
01352 void Part::slotShowMenu(const Okular::Page *page, const QPoint &point)
01353 {
01354 if (m_dummyMode) return;
01355
01356 bool reallyShow = false;
01357 if (!m_actionsSearched)
01358 {
01359
01360 KActionCollection *ac;
01361 QAction *act;
01362
01363 if (factory())
01364 {
01365 QList<KXMLGUIClient*> clients(factory()->clients());
01366 for(int i = 0 ; (!m_showMenuBarAction || !m_showFullScreenAction) && i < clients.size(); ++i)
01367 {
01368 ac = clients.at(i)->actionCollection();
01369
01370 act = ac->action("options_show_menubar");
01371 if (act && qobject_cast<KToggleAction*>(act))
01372 m_showMenuBarAction = qobject_cast<KToggleAction*>(act);
01373
01374 act = ac->action("fullscreen");
01375 if (act && qobject_cast<KToggleFullScreenAction*>(act))
01376 m_showFullScreenAction = qobject_cast<KToggleFullScreenAction*>(act);
01377 }
01378 }
01379 m_actionsSearched = true;
01380 }
01381
01382 KMenu *popup = new KMenu( widget() );
01383 QAction *addBookmark = 0;
01384 QAction *removeBookmark = 0;
01385 QAction *fitPageWidth = 0;
01386 if (page)
01387 {
01388 popup->addTitle( i18n( "Page %1", page->number() + 1 ) );
01389 if ( m_document->bookmarkManager()->isBookmarked( page->number() ) )
01390 removeBookmark = popup->addAction( KIcon("edit-delete-bookmark"), i18n("Remove Bookmark") );
01391 else
01392 addBookmark = popup->addAction( KIcon("bookmark-new"), i18n("Add Bookmark") );
01393 if ( m_pageView->canFitPageWidth() )
01394 fitPageWidth = popup->addAction( KIcon("zoom-fit-best"), i18n("Fit Width") );
01395 popup->addAction( m_prevBookmark );
01396 popup->addAction( m_nextBookmark );
01397 reallyShow = true;
01398 }
01399
01400
01401
01402
01403
01404
01405
01406
01407 if ((m_showMenuBarAction && !m_showMenuBarAction->isChecked()) || (m_showFullScreenAction && m_showFullScreenAction->isChecked()))
01408 {
01409 popup->addTitle( i18n( "Tools" ) );
01410 if (m_showMenuBarAction && !m_showMenuBarAction->isChecked()) popup->addAction(m_showMenuBarAction);
01411 if (m_showFullScreenAction && m_showFullScreenAction->isChecked()) popup->addAction(m_showFullScreenAction);
01412 reallyShow = true;
01413
01414 }
01415
01416 if (reallyShow)
01417 {
01418 QAction *res = popup->exec(point);
01419 if (res)
01420 {
01421 if (res == addBookmark) m_document->bookmarkManager()->addBookmark( page->number() );
01422 else if (res == removeBookmark) m_document->bookmarkManager()->removeBookmark( page->number() );
01423 else if (res == fitPageWidth) m_pageView->fitPageWidth( page->number() );
01424 }
01425 }
01426 delete popup;
01427 }
01428
01429
01430 void Part::slotShowProperties()
01431 {
01432 PropertiesDialog *d = new PropertiesDialog(widget(), m_document);
01433 d->exec();
01434 delete d;
01435 }
01436
01437
01438 void Part::slotShowEmbeddedFiles()
01439 {
01440 EmbeddedFilesDialog *d = new EmbeddedFilesDialog(widget(), m_document);
01441 d->exec();
01442 delete d;
01443 }
01444
01445
01446 void Part::slotShowPresentation()
01447 {
01448 if ( !m_presentationWidget )
01449 {
01450 m_presentationWidget = new PresentationWidget( widget(), m_document );
01451 m_presentationWidget->setupActions( actionCollection() );
01452 }
01453 }
01454
01455
01456 void Part::slotHidePresentation()
01457 {
01458 if ( m_presentationWidget )
01459 delete (PresentationWidget*) m_presentationWidget;
01460 }
01461
01462
01463 void Part::slotTogglePresentation()
01464 {
01465 if ( m_document->isOpened() )
01466 {
01467 if ( !m_presentationWidget )
01468 m_presentationWidget = new PresentationWidget( widget(), m_document );
01469 else delete (PresentationWidget*) m_presentationWidget;
01470 }
01471 }
01472
01473
01474 void Part::slotAboutBackend()
01475 {
01476 const KComponentData *data = m_document->componentData();
01477 if ( !data )
01478 return;
01479
01480 KAboutApplicationDialog dlg( data->aboutData(), widget() );
01481 dlg.exec();
01482 }
01483
01484
01485 void Part::slotExportAs(QAction * act)
01486 {
01487 QList<QAction*> acts = m_exportAs->menu() ? m_exportAs->menu()->actions() : QList<QAction*>();
01488 int id = acts.indexOf( act );
01489 if ( ( id < 0 ) || ( id >= acts.count() ) )
01490 return;
01491
01492 QString filter = id == 0 ? "text/plain" : m_exportFormats.at( id - 1 ).mimeType()->name();
01493 QString fileName = KFileDialog::getSaveFileName( url().isLocalFile() ? url().directory() : QString(), filter, widget() );
01494 if ( !fileName.isEmpty() )
01495 {
01496 bool saved = id == 0 ? m_document->exportToText( fileName ) : m_document->exportTo( fileName, m_exportFormats.at( id - 1 ) );
01497 if ( !saved )
01498 KMessageBox::information( widget(), i18n("File could not be saved in '%1'. Try to save it to another location.", fileName ) );
01499 }
01500 }
01501
01502
01503 void Part::slotReload()
01504 {
01505
01506
01507 m_dirtyHandler->stop();
01508
01509 slotDoFileDirty();
01510 }
01511
01512
01513 void Part::slotPrint()
01514 {
01515 if (m_document->pages() == 0) return;
01516
01517 QPrinter printer;
01518 QPrintDialog *printDialog = 0;
01519 QWidget *printConfigWidget = 0;
01520
01521
01522 setupPrint( printer );
01523
01524
01525 if ( m_document->canConfigurePrinter() )
01526 {
01527 printConfigWidget = m_document->printConfigurationWidget();
01528 }
01529 if ( printConfigWidget )
01530 {
01531 printDialog = KdePrint::createPrintDialog( &printer, QList<QWidget*>() << printConfigWidget, widget() );
01532 }
01533 else
01534 {
01535 printDialog = KdePrint::createPrintDialog( &printer, widget() );
01536 }
01537
01538 if ( printDialog )
01539 {
01540
01541
01542 printDialog->setMinMax( 1, m_document->pages() );
01543 printDialog->setFromTo( 1, m_document->pages() );
01544
01545
01546 if ( !m_document->bookmarkedPageRange().isEmpty() )
01547 {
01548 printDialog->addEnabledOption( QAbstractPrintDialog::PrintSelection );
01549 }
01550
01551
01552 if ( printDialog->isOptionEnabled( QAbstractPrintDialog::PrintToFile ) &&
01553 !m_document->supportsPrintToFile() )
01554 {
01555 printDialog->setEnabledOptions( printDialog->enabledOptions() ^ QAbstractPrintDialog::PrintToFile );
01556 }
01557
01558 if ( printDialog->exec() )
01559 doPrint( printer );
01560
01561 }
01562 }
01563
01564
01565 void Part::setupPrint( QPrinter &printer )
01566 {
01567 double width, height;
01568 int landscape, portrait;
01569 const Okular::Page *page;
01570
01571
01572
01573 landscape = 0;
01574 portrait = 0;
01575 for (uint i = 0; i < m_document->pages(); i++)
01576 {
01577 page = m_document->page(i);
01578 width = page->width();
01579 height = page->height();
01580 if (page->orientation() == Okular::Rotation90 || page->orientation() == Okular::Rotation270) qSwap(width, height);
01581 if (width > height) landscape++;
01582 else portrait++;
01583 }
01584 if (landscape > portrait) printer.setOrientation(QPrinter::Landscape);
01585
01586
01587 QString title = m_document->metaData( "DocumentTitle" ).toString();
01588 if ( title.isEmpty() )
01589 {
01590 title = m_document->currentDocument().fileName();
01591 }
01592 if ( !title.isEmpty() )
01593 {
01594 printer.setDocName( title );
01595 }
01596 }
01597
01598
01599 void Part::doPrint(QPrinter &printer)
01600 {
01601 if (!m_document->isAllowed(Okular::AllowPrint))
01602 {
01603 KMessageBox::error(widget(), i18n("Printing this document is not allowed."));
01604 return;
01605 }
01606
01607 if (!m_document->print(printer))
01608 {
01609 KMessageBox::error(widget(), i18n("Could not print the document. Please report to bugs.kde.org"));
01610 }
01611 }
01612
01613
01614 void Part::restoreDocument(const KConfigGroup &group)
01615 {
01616 KUrl url ( group.readPathEntry( "URL", QString() ) );
01617 if ( url.isValid() )
01618 {
01619 QString viewport = group.readEntry( "Viewport" );
01620 if (!viewport.isEmpty()) m_document->setNextDocumentViewport( Okular::DocumentViewport( viewport ) );
01621 openUrl( url );
01622 }
01623 }
01624
01625
01626 void Part::saveDocumentRestoreInfo(KConfigGroup &group)
01627 {
01628 group.writePathEntry( "URL", url().url() );
01629 group.writeEntry( "Viewport", m_document->viewport().toString() );
01630 }
01631
01632
01633 void Part::psTransformEnded(int exit, QProcess::ExitStatus status)
01634 {
01635 Q_UNUSED( exit )
01636 if ( status != QProcess::NormalExit )
01637 return;
01638
01639 QProcess *senderobj = sender() ? qobject_cast< QProcess * >( sender() ) : 0;
01640 if ( senderobj )
01641 {
01642 senderobj->close();
01643 senderobj->deleteLater();
01644 }
01645
01646 setLocalFilePath( m_temporaryLocalFile );
01647 openUrl( m_temporaryLocalFile );
01648 m_temporaryLocalFile.clear();
01649 }
01650
01651
01652 void Part::unsetDummyMode()
01653 {
01654 if (!m_dummyMode) return;
01655
01656 m_dummyMode = false;
01657
01658 m_sidebar->setItemEnabled( 2, true );
01659 m_sidebar->setItemEnabled( 3, true );
01660 m_sidebar->setSidebarVisibility( Okular::Settings::showLeftPanel() );
01661
01662
01663 m_historyBack = KStandardAction::documentBack( this, SLOT( slotHistoryBack() ), actionCollection() );
01664 m_historyBack->setWhatsThis( i18n( "Go to the place you were before" ) );
01665
01666 m_historyNext = KStandardAction::documentForward( this, SLOT( slotHistoryNext() ), actionCollection());
01667 m_historyNext->setWhatsThis( i18n( "Go to the place you were after" ) );
01668
01669 m_pageView->setupActions( actionCollection() );
01670
01671
01672 m_formsMessage->setActionButton( m_pageView->toggleFormsAction() );
01673
01674
01675 updateViewActions();
01676 }
01677
01678
01679 bool Part::handleCompressed( QString &destpath, const QString &path, const QString &compressedMimetype )
01680 {
01681 m_tempfile = 0;
01682
01683
01684
01685 KTemporaryFile *newtempfile = new KTemporaryFile();
01686 newtempfile->setAutoRemove(true);
01687
01688 if ( !newtempfile->open() )
01689 {
01690 KMessageBox::error( widget(),
01691 i18n("<qt><strong>File Error!</strong> Could not create temporary file "
01692 "<nobr><strong>%1</strong></nobr>.</qt>",
01693 strerror(newtempfile->error())));
01694 delete newtempfile;
01695 return false;
01696 }
01697
01698
01699 QIODevice* filterDev = KFilterDev::deviceForFile( path, compressedMimetype );
01700 if (!filterDev)
01701 {
01702 delete newtempfile;
01703 return false;
01704 }
01705
01706 if ( !filterDev->open(QIODevice::ReadOnly) )
01707 {
01708 KMessageBox::detailedError( widget(),
01709 i18n("<qt><strong>File Error!</strong> Could not open the file "
01710 "<nobr><strong>%1</strong></nobr> for uncompression. "
01711 "The file will not be loaded.</qt>", path),
01712 i18n("<qt>This error typically occurs if you do "
01713 "not have enough permissions to read the file. "
01714 "You can check ownership and permissions if you "
01715 "right-click on the file in the Konqueror "
01716 "file manager and then choose the 'Properties' menu.</qt>"));
01717
01718 delete filterDev;
01719 delete newtempfile;
01720 return false;
01721 }
01722
01723 QByteArray buf(1024, '\0');
01724 int read = 0, wrtn = 0;
01725
01726 while ((read = filterDev->read(buf.data(), buf.size())) > 0)
01727 {
01728 wrtn = newtempfile->write(buf.data(), read);
01729 if ( read != wrtn )
01730 break;
01731 }
01732 delete filterDev;
01733 if ((read != 0) || (newtempfile->size() == 0))
01734 {
01735 KMessageBox::detailedError(widget(),
01736 i18n("<qt><strong>File Error!</strong> Could not uncompress "
01737 "the file <nobr><strong>%1</strong></nobr>. "
01738 "The file will not be loaded.</qt>", path ),
01739 i18n("<qt>This error typically occurs if the file is corrupt. "
01740 "If you want to be sure, try to decompress the file manually "
01741 "using command-line tools.</qt>"));
01742 delete newtempfile;
01743 return false;
01744 }
01745 m_tempfile = newtempfile;
01746 destpath = m_tempfile->fileName();
01747 return true;
01748 }
01749
01750 void Part::rebuildBookmarkMenu( bool unplugActions )
01751 {
01752 if ( unplugActions )
01753 {
01754 unplugActionList( "bookmarks_currentdocument" );
01755 qDeleteAll( m_bookmarkActions );
01756 m_bookmarkActions.clear();
01757 }
01758 KUrl u = m_document->currentDocument();
01759 if ( u.isValid() )
01760 {
01761 m_bookmarkActions = m_document->bookmarkManager()->actionsForUrl( u );
01762 }
01763 bool havebookmarks = true;
01764 if ( m_bookmarkActions.isEmpty() )
01765 {
01766 havebookmarks = false;
01767 QAction * a = new QAction( 0 );
01768 a->setText( i18n( "No Bookmarks" ) );
01769 a->setEnabled( false );
01770 m_bookmarkActions.append( a );
01771 }
01772 for ( int i = 0; i < m_bookmarkActions.count(); ++i )
01773 {
01774 actionCollection()->addAction( QString( "bookmark_action_%1" ).arg( i ), m_bookmarkActions.at(i) );
01775 }
01776 plugActionList( "bookmarks_currentdocument", m_bookmarkActions );
01777
01778 m_prevBookmark->setEnabled( havebookmarks );
01779 m_nextBookmark->setEnabled( havebookmarks );
01780 }
01781
01782 void Part::updateAboutBackendAction()
01783 {
01784 const KComponentData *data = m_document->componentData();
01785 if ( data )
01786 {
01787 m_aboutBackend->setEnabled( true );
01788 }
01789 else
01790 {
01791 m_aboutBackend->setEnabled( false );
01792 }
01793 }
01794
01795
01796
01797
01798
01799 BrowserExtension::BrowserExtension(Part* parent)
01800 : KParts::BrowserExtension( parent ), m_part( parent )
01801 {
01802 emit enableAction("print", true);
01803 setURLDropHandlingEnabled(true);
01804 }
01805
01806
01807 void BrowserExtension::print()
01808 {
01809 m_part->slotPrint();
01810 }
01811
01812
01813 #include "part.moc"