bring order back
[kdegraphics.git] / okular / shell / shell.cpp
blob066c3c9999b44c84c41d2b3278f3f0a1442f00a2
1 /***************************************************************************
2 * Copyright (C) 2002 by Wilco Greven <greven@kde.org> *
3 * Copyright (C) 2002 by Chris Cheney <ccheney@cheney.cx> *
4 * Copyright (C) 2003 by Benjamin Meyer <benjamin@csh.rit.edu> *
5 * Copyright (C) 2003-2004 by Christophe Devriese *
6 * <Christophe.Devriese@student.kuleuven.ac.be> *
7 * Copyright (C) 2003 by Laurent Montel <montel@kde.org> *
8 * Copyright (C) 2003-2004 by Albert Astals Cid <tsdgeos@terra.es> *
9 * Copyright (C) 2003 by Luboš Luňák <l.lunak@kde.org> *
10 * Copyright (C) 2003 by Malcolm Hunter <malcolm.hunter@gmx.co.uk> *
11 * Copyright (C) 2004 by Dominique Devriese <devriese@kde.org> *
12 * Copyright (C) 2004 by Dirk Mueller <mueller@kde.org> *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 ***************************************************************************/
20 #include "shell.h"
22 // qt/kde includes
23 #include <qtimer.h>
24 #include <QtDBus/qdbusconnection.h>
25 #include <kaction.h>
26 #include <kapplication.h>
27 #include <kcmdlineargs.h>
28 #include <kedittoolbar.h>
29 #include <kfiledialog.h>
30 #include <kpluginloader.h>
31 #include <kmessagebox.h>
32 #include <kmimetype.h>
33 #include <kstandardaction.h>
34 #include <ktoolbar.h>
35 #include <kurl.h>
36 #include <kdebug.h>
37 #include <klocale.h>
38 #include <kmenubar.h>
39 #include <kio/netaccess.h>
40 #include <krecentfilesaction.h>
41 #include <kservicetypetrader.h>
42 #include <ktoggleaction.h>
43 #include <ktogglefullscreenaction.h>
44 #include <kactioncollection.h>
46 // local includes
47 #include "kdocumentviewer.h"
49 Shell::Shell(KCmdLineArgs* args, const KUrl &url)
50 : KParts::MainWindow(), m_args(args), m_menuBarWasShown(true), m_toolBarWasShown(true)
52 m_openUrl = url;
53 init();
56 void Shell::init()
58 setObjectName( QLatin1String( "okular::Shell" ) );
59 setContextMenuPolicy( Qt::NoContextMenu );
60 // set the shell's ui resource file
61 setXMLFile("shell.rc");
62 m_doc=0L;
63 m_fileformatsscanned = false;
64 // this routine will find and load our Part. it finds the Part by
65 // name which is a bad idea usually.. but it's alright in this
66 // case since our Part is made for this Shell
67 KPluginFactory *factory = KPluginLoader("okularpart").factory();
68 if (!factory)
70 // if we couldn't find our Part, we exit since the Shell by
71 // itself can't do anything useful
72 KMessageBox::error(this, i18n("Unable to find the Okular component."));
73 m_part = 0;
74 return;
77 // now that the Part is loaded, we cast it to a Part to get
78 // our hands on it
79 m_part = factory->create< KParts::ReadOnlyPart >( this );
80 if (m_part)
82 // then, setup our actions
83 setupActions();
84 // tell the KParts::MainWindow that this is indeed the main widget
85 setCentralWidget(m_part->widget());
86 // and integrate the part's GUI with the shell's
87 setupGUI(Keys | Save);
88 createGUI(m_part);
89 m_showToolBarAction = static_cast<KToggleAction*>(toolBarMenuAction());
90 m_doc = qobject_cast<KDocumentViewer*>(m_part);
93 connect( this, SIGNAL( restoreDocument(const KConfigGroup&) ),m_part, SLOT( restoreDocument(const KConfigGroup&)));
94 connect( this, SIGNAL( saveDocumentRestoreInfo(KConfigGroup&) ), m_part, SLOT( saveDocumentRestoreInfo(KConfigGroup&)));
95 connect( m_part, SIGNAL( enablePrintAction(bool) ), m_printAction, SLOT( setEnabled(bool)));
97 readSettings();
99 if (!KGlobal::config()->hasGroup("MainWindow"))
101 showMaximized();
104 if (m_args && m_args->isSet("unique") && m_args->count() == 1)
106 QDBusConnection::sessionBus().registerService("org.kde.okular");
109 if (m_openUrl.isValid()) QTimer::singleShot(0, this, SLOT(delayedOpen()));
112 void Shell::delayedOpen()
114 uint page = 0;
115 if (m_args && m_doc)
117 QString pageopt = m_args->getOption("page");
118 page = pageopt.toUInt();
120 openUrl(m_openUrl, page);
123 Shell::~Shell()
125 if ( m_part ) writeSettings();
126 delete m_part;
127 if ( m_args )
128 m_args->clear();
131 void Shell::openUrl( const KUrl & url, uint page )
133 if ( m_part )
135 if ( m_doc && m_args && m_args->isSet( "presentation" ) )
136 m_doc->startPresentation();
137 bool openOk = page > 0 && m_doc ? m_doc->openDocument( url, page ) : m_part->openUrl( url );
138 if ( openOk ) m_recent->addUrl( url );
139 else m_recent->removeUrl( url );
144 void Shell::readSettings()
146 m_recent->loadEntries( KGlobal::config()->group( "Recent Files" ) );
147 m_recent->setEnabled( true ); // force enabling
149 const KConfigGroup group = KGlobal::config()->group( "Desktop Entry" );
150 bool fullScreen = group.readEntry( "FullScreen", false );
151 setFullScreen( fullScreen );
154 void Shell::writeSettings()
156 m_recent->saveEntries( KGlobal::config()->group( "Recent Files" ) );
157 KConfigGroup group = KGlobal::config()->group( "Desktop Entry" );
158 group.writeEntry( "FullScreen", m_fullScreenAction->isChecked() );
159 KGlobal::config()->sync();
162 void Shell::setupActions()
164 KStandardAction::open(this, SLOT(fileOpen()), actionCollection());
165 m_recent = KStandardAction::openRecent( this, SLOT( openUrl( const KUrl& ) ), actionCollection() );
166 m_recent->setToolBarMode( KRecentFilesAction::MenuMode );
167 m_recent->setToolButtonPopupMode( QToolButton::DelayedPopup );
168 connect( m_recent, SIGNAL( triggered() ), this, SLOT( fileOpen() ) );
169 m_recent->setToolTip( i18n("Click to open a file\nClick and hold to open a recent file") );
170 m_recent->setWhatsThis( i18n( "<b>Click</b> to open a file or <b>Click and hold</b> to select a recent file" ) );
171 m_printAction = KStandardAction::print( m_part, SLOT( slotPrint() ), actionCollection() );
172 m_printAction->setEnabled( false );
173 KStandardAction::quit(this, SLOT(slotQuit()), actionCollection());
175 setStandardToolBarMenuEnabled(true);
177 m_showMenuBarAction = KStandardAction::showMenubar( this, SLOT( slotShowMenubar() ), actionCollection());
178 KStandardAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
179 m_fullScreenAction = KStandardAction::fullScreen( this, SLOT( slotUpdateFullScreen() ), this,actionCollection() );
182 void Shell::saveProperties(KConfigGroup &group)
184 // the 'config' object points to the session managed
185 // config file. anything you write here will be available
186 // later when this app is restored
187 emit saveDocumentRestoreInfo(group);
190 void Shell::readProperties(const KConfigGroup &group)
192 // the 'config' object points to the session managed
193 // config file. this function is automatically called whenever
194 // the app is being restored. read in here whatever you wrote
195 // in 'saveProperties'
196 if(m_part)
198 emit restoreDocument(group);
202 QStringList Shell::fileFormats() const
204 QStringList supportedPatterns;
206 QString constraint( "(Library == 'okularpart')" );
207 QLatin1String basePartService( "KParts/ReadOnlyPart" );
208 KService::List offers = KServiceTypeTrader::self()->query( basePartService, constraint );
209 KService::List::ConstIterator it = offers.begin(), itEnd = offers.end();
210 for ( ; it != itEnd; ++it )
212 KService::Ptr service = *it;
213 QStringList mimeTypes = service->serviceTypes();
214 foreach ( const QString& mimeType, mimeTypes )
215 if ( mimeType != basePartService )
216 supportedPatterns.append( mimeType );
219 return supportedPatterns;
222 void Shell::fileOpen()
224 // this slot is called whenever the File->Open menu is selected,
225 // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
226 // button is clicked
227 if ( !m_fileformatsscanned )
229 if ( m_doc )
230 m_fileformats = m_doc->supportedMimeTypes();
232 if ( m_fileformats.isEmpty() )
233 m_fileformats = fileFormats();
235 m_fileformatsscanned = true;
238 QString startDir;
239 if ( m_part->url().isLocalFile() )
240 startDir = m_part->url().path();
241 KFileDialog dlg( startDir, QString(), this );
242 dlg.setOperationMode( KFileDialog::Opening );
243 if ( m_fileformatsscanned && m_fileformats.isEmpty() )
244 dlg.setFilter( i18n( "*|All Files" ) );
245 else
246 dlg.setMimeFilter( m_fileformats );
247 dlg.setCaption( i18n( "Open Document" ) );
248 if ( !dlg.exec() )
249 return;
250 KUrl url = dlg.selectedUrl();
251 if ( !url.isEmpty() )
252 openUrl( url );
255 void
256 Shell::optionsConfigureToolbars()
258 KEditToolBar dlg(factory(), this);
259 connect(&dlg, SIGNAL(newToolBarConfig()), this, SLOT(applyNewToolbarConfig()));
260 dlg.exec();
263 void
264 Shell::applyNewToolbarConfig()
266 applyMainWindowSettings(KGlobal::config()->group("MainWindow"));
269 void Shell::slotQuit()
271 kapp->closeAllWindows();
274 // only called when starting the program
275 void Shell::setFullScreen( bool useFullScreen )
277 if( useFullScreen )
278 setWindowState( windowState() | Qt::WindowFullScreen ); // set
279 else
280 setWindowState( windowState() & ~Qt::WindowFullScreen ); // reset
283 void Shell::slotUpdateFullScreen()
285 if(m_fullScreenAction->isChecked())
287 m_menuBarWasShown = m_showMenuBarAction->isChecked();
288 m_showMenuBarAction->setChecked(false);
289 menuBar()->hide();
291 m_toolBarWasShown = m_showToolBarAction->isChecked();
292 m_showToolBarAction->setChecked(false);
293 toolBar()->hide();
295 KToggleFullScreenAction::setFullScreen(this, true);
297 else
299 if (m_menuBarWasShown)
301 m_showMenuBarAction->setChecked(true);
302 menuBar()->show();
304 if (m_toolBarWasShown)
306 m_showToolBarAction->setChecked(true);
307 toolBar()->show();
309 KToggleFullScreenAction::setFullScreen(this, false);
313 void Shell::slotShowMenubar()
315 if ( m_showMenuBarAction->isChecked() )
316 menuBar()->show();
317 else
318 menuBar()->hide();
321 #include "shell.moc"