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"
22 #include "konqfactory.h"
23 #include "konqmainwindow.h"
24 #include "konqsessionmanager.h"
26 #include "konqsettingsxt.h"
28 #include <ktemporaryfile.h>
30 #include <kstandarddirs.h>
32 #include <kcmdlineargs.h>
33 #include <QtCore/QFile>
34 #include <QtGui/QApplication>
35 #include <QtGui/QWidget>
41 #include <QtDBus/QtDBus>
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");
83 KonqMainWindow::s_crashlog_file
= &crashlog_file
;
85 if ( app
.isSessionRestored() )
87 KonqSessionManager::self()->askUserToRestoreAutosavedAbandonedSessions();
90 while ( KonqMainWindow::canBeRestored( n
) )
92 QString className
= KXmlGuiWindow::classNameOfToplevel( n
);
93 if( className
== QLatin1String( "KonqMainWindow" ))
94 (new KonqMainWindow() )->restore( n
);
96 kWarning() << "Unknown class " << className
<< " in session saved data!" ;
102 if (args
->isSet("profiles"))
104 QStringList profiles
= KGlobal::dirs()->findAllResources("data", "konqueror/profiles/*", KStandardDirs::NoDuplicates
);
106 for(QStringList::ConstIterator it
= profiles
.constBegin();
107 it
!= profiles
.constEnd(); ++it
)
110 file
= file
.mid(file
.lastIndexOf('/')+1);
111 printf("%s\n", QFile::encodeName(file
).data());
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();
129 QStringList filesToSelect
;
130 if (args
->count() == 1)
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
);
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
);
153 QFileInfo
fileInfo(it
.next());
154 printf("%s\n", QFile::encodeName(fileInfo
.baseName()).data());
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
);
169 kError() << "session " << session
<< " not found or empty";
173 KonqSessionManager::self()->restoreSessions(sessionPath
);
177 if (args
->count() == 0)
179 if (args
->isSet("preload"))
182 if( KonqSettings::maxPreloadCount() > 0 )
184 QDBusInterface
ref( "org.kde.kded", "/modules/konqy_preloader", "org.kde.konqueror.Preloader", QDBusConnection::sessionBus() );
186 QDBusReply
<bool> retVal
= ref
.call( QDBus::Block
, "registerPreloadedKonqy", QDBusConnection::sessionBus().baseService(), info
.screen());
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();
197 return 0; // no preloading
200 return 0; // no preloading
203 else if(!args
->isSet("silent")) {
204 const QString profile
= KonqMisc::defaultProfileName();
205 KonqMisc::createBrowserWindowFromProfile( KonqMisc::defaultProfilePath(), profile
);
207 kDebug(1202) << "main() -> no args";
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
);
223 QStringList filesToSelect
;
225 if (url
.isLocalFile() && QFile::exists(url
.path())) // "konqueror index.html"
228 urlToOpen
= KUrl( KonqMisc::konqFilteredURL(0L, args
->arg(i
)) ); // "konqueror slashdot.org"
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
);
242 urlToOpen
.setFileName("");
245 const bool tempFile
= KCmdLineArgs::isTempFileSet();
246 mainwin
= KonqMisc::createNewWindow( urlToOpen
, urlargs
, KParts::BrowserArguments(), false, filesToSelect
, tempFile
);
248 urlList
+= urlToOpen
;
251 mainwin
->openMultiURL( urlList
);
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();