delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / konqueror / src / konqmain.cpp
blob0a2ca3492e3ffcbf4728f4d891be3f9d597a9e95
1 /* This file is part of the KDE project
2 Copyright (C) 1998, 1999 Simon Hausmann <hausmann@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "konqapplication.h"
21 #include "konqmisc.h"
22 #include "konqfactory.h"
23 #include "konqmainwindow.h"
24 #include "konqsessionmanager.h"
25 #include "konqview.h"
26 #include "konqsettingsxt.h"
28 #include <ktemporaryfile.h>
29 #include <klocale.h>
30 #include <kstandarddirs.h>
31 #include <kdebug.h>
32 #include <kcmdlineargs.h>
33 #include <QtCore/QFile>
34 #include <QtGui/QApplication>
35 #include <QtGui/QWidget>
37 #ifdef Q_WS_X11
38 #include <QX11Info>
39 #endif
41 #include <QtDBus/QtDBus>
42 #include <QDir>
44 extern "C" KDE_EXPORT int kdemain( int argc, char **argv )
46 KCmdLineArgs::init( argc, argv, KonqFactory::aboutData() );
49 KCmdLineOptions options;
51 options.add("silent", ki18n("Start without a default window"));
53 options.add("preload", ki18n("Preload for later use"));
55 options.add("profile <profile>", ki18n("Profile to open"));
57 options.add("profiles", ki18n("List available profiles"));
59 options.add("sessions", ki18n("List available sessions"));
61 options.add("open-session <session>", ki18n("Session to open"));
63 options.add("mimetype <mimetype>", ki18n("Mimetype to use for this URL (e.g. text/html or inode/directory)"));
65 options.add("select", ki18n("For URLs that point to files, opens the directory and selects the file, instead of opening the actual file"));
67 options.add("+[URL]", ki18n("Location to open"));
69 KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
70 KCmdLineArgs::addTempFileOption();
72 KonquerorApplication app;
73 app.setQuitOnLastWindowClosed(false);
75 KGlobal::locale()->insertCatalog("libkonq"); // needed for apps using libkonq
77 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
79 KTemporaryFile crashlog_file;
80 crashlog_file.setPrefix("konqueror-crash-");
81 crashlog_file.setSuffix(".log");
82 crashlog_file.open();
83 KonqMainWindow::s_crashlog_file = &crashlog_file;
85 if ( app.isSessionRestored() )
87 KonqSessionManager::self()->askUserToRestoreAutosavedAbandonedSessions();
89 int n = 1;
90 while ( KonqMainWindow::canBeRestored( n ) )
92 QString className = KXmlGuiWindow::classNameOfToplevel( n );
93 if( className == QLatin1String( "KonqMainWindow" ))
94 (new KonqMainWindow() )->restore( n );
95 else
96 kWarning() << "Unknown class " << className << " in session saved data!" ;
97 n++;
100 else
102 if (args->isSet("profiles"))
104 QStringList profiles = KGlobal::dirs()->findAllResources("data", "konqueror/profiles/*", KStandardDirs::NoDuplicates);
105 profiles.sort();
106 for(QStringList::ConstIterator it = profiles.constBegin();
107 it != profiles.constEnd(); ++it)
109 QString file = *it;
110 file = file.mid(file.lastIndexOf('/')+1);
111 printf("%s\n", QFile::encodeName(file).data());
114 return 0;
116 else if (args->isSet("profile"))
118 QString profile = args->getOption("profile");
119 QString profilePath = profile;
120 if (profile[0] != '/') {
121 profilePath = KStandardDirs::locate( "data", QLatin1String("konqueror/profiles/")+profile );
122 if (profilePath.isEmpty()) {
123 profile = KonqMisc::defaultProfileName();
124 profilePath = KonqMisc::defaultProfilePath();
128 QString url;
129 QStringList filesToSelect;
130 if (args->count() == 1)
131 url = args->arg(0);
132 KUrl kurl(url);
133 KParts::OpenUrlArguments urlargs;
134 if (args->isSet("mimetype"))
135 urlargs.setMimeType( args->getOption("mimetype") );
136 if (args->isSet("select")) {
137 QString fn = kurl.fileName(KUrl::ObeyTrailingSlash);
138 if( !fn.isEmpty() ){
139 filesToSelect += fn;
140 kurl.setFileName("");
143 kDebug(1202) << "main() -> createBrowserWindowFromProfile mimeType=" << urlargs.mimeType();
144 KonqMisc::createBrowserWindowFromProfile( profilePath, profile, kurl, urlargs, KParts::BrowserArguments(), false, filesToSelect );
146 else if (args->isSet("sessions"))
148 QString dir = KStandardDirs::locateLocal("appdata", "sessions/");
149 QDirIterator it(dir, QDir::Readable|QDir::NoDotAndDotDot|QDir::Dirs);
151 while (it.hasNext())
153 QFileInfo fileInfo(it.next());
154 printf("%s\n", QFile::encodeName(fileInfo.baseName()).data());
157 return 0;
159 else if (args->isSet("open-session"))
161 QString session = args->getOption("open-session");
162 QString sessionPath = session;
163 if (session[0] != '/') {
164 sessionPath = KStandardDirs::locateLocal("appdata", "sessions/" + session);
167 QDirIterator it(sessionPath, QDir::Readable|QDir::Files);
168 if (!it.hasNext()) {
169 kError() << "session " << session << " not found or empty";
170 return -1;
173 KonqSessionManager::self()->restoreSessions(sessionPath);
175 else
177 if (args->count() == 0)
179 if (args->isSet("preload"))
181 #ifdef Q_WS_X11
182 if( KonqSettings::maxPreloadCount() > 0 )
184 QDBusInterface ref( "org.kde.kded", "/modules/konqy_preloader", "org.kde.konqueror.Preloader", QDBusConnection::sessionBus() );
185 QX11Info info;
186 QDBusReply<bool> retVal = ref.call( QDBus::Block, "registerPreloadedKonqy", QDBusConnection::sessionBus().baseService(), info.screen());
187 if( !retVal )
188 return 0; // too many preloaded or failed
189 KonqMainWindow* win = new KonqMainWindow; // prepare an empty window too
190 // KonqMainWindow ctor sets always the preloaded flag to false, so create the window before this
191 KonqMainWindow::setPreloadedFlag( true );
192 KonqMainWindow::setPreloadedWindow( win );
193 kDebug(1202) << "Konqy preloaded :" << QDBusConnection::sessionBus().baseService();
195 else
197 return 0; // no preloading
199 #else
200 return 0; // no preloading
201 #endif
203 else if(!args->isSet("silent")) {
204 const QString profile = KonqMisc::defaultProfileName();
205 KonqMisc::createBrowserWindowFromProfile( KonqMisc::defaultProfilePath(), profile );
207 kDebug(1202) << "main() -> no args";
209 else
211 KUrl::List urlList;
212 KonqMainWindow * mainwin = 0L;
214 QList<KonqMainWindow*> *mainWindowList = KonqMainWindow::mainWindowList();
215 if(mainWindowList && !mainWindowList->isEmpty())
216 mainwin = mainWindowList->first();
218 for ( int i = 0; i < args->count(); i++ )
220 // KonqMisc::konqFilteredURL doesn't cope with local files... A bit of hackery below
221 KUrl url = args->url(i);
222 KUrl urlToOpen;
223 QStringList filesToSelect;
225 if (url.isLocalFile() && QFile::exists(url.path())) // "konqueror index.html"
226 urlToOpen = url;
227 else
228 urlToOpen = KUrl( KonqMisc::konqFilteredURL(0L, args->arg(i)) ); // "konqueror slashdot.org"
230 if ( !mainwin ) {
231 KParts::OpenUrlArguments urlargs;
232 if (args->isSet("mimetype"))
234 urlargs.setMimeType( args->getOption("mimetype") );
235 kDebug(1202) << "main() : setting mimeType to " << urlargs.mimeType();
237 if (args->isSet("select"))
239 QString fn = urlToOpen.fileName(KUrl::ObeyTrailingSlash);
240 if( !fn.isEmpty() ){
241 filesToSelect += fn;
242 urlToOpen.setFileName("");
245 const bool tempFile = KCmdLineArgs::isTempFileSet();
246 mainwin = KonqMisc::createNewWindow( urlToOpen, urlargs, KParts::BrowserArguments(), false, filesToSelect, tempFile );
247 } else
248 urlList += urlToOpen;
250 if ( mainwin )
251 mainwin->openMultiURL( urlList );
255 args->clear();
257 app.exec();
259 // Delete all KonqMainWindows, so that we don't have
260 // any parts loaded when KLibLoader::cleanUp is called.
261 // Their deletion was postponed in their event()
262 // (and Qt doesn't delete WDestructiveClose widgets on exit anyway :( )
263 while( KonqMainWindow::mainWindowList() != NULL )
264 { // the list will be deleted by last KonqMainWindow
265 delete KonqMainWindow::mainWindowList()->first();
268 // Notify the session manager that the instance was closed without errors, and normally.
269 KonqSessionManager::self()->disableAutosave();
270 KonqSessionManager::self()->deleteOwnedSessions();
272 return 0;