add more spacing
[personal-kdebase.git] / apps / konqueror / src / konqmisc.cpp
blobb273ded623106afd502c71b682681e31d6a39004
1 /* This file is part of the KDE project
2 Copyright (C) 1998, 1999 David Faure <faure@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.
19 #include "konqmisc.h"
20 #include "konqsessionmanager.h"
21 #include "konqsettingsxt.h"
22 #include "konqmainwindow.h"
23 #include "konqviewmanager.h"
24 #include "konqview.h"
26 #include <kapplication.h>
27 #include <kdebug.h>
28 #include <kmessagebox.h>
29 #include <kurifilter.h>
30 #include <klocale.h>
31 #include <kstandarddirs.h>
32 #include <kwindowsystem.h>
33 #include <kprotocolmanager.h>
34 #include <kstartupinfo.h>
35 #include <kiconloader.h>
36 #include <kconfiggroup.h>
37 #include <QList>
39 /**********************************************
41 * KonqMisc
43 **********************************************/
45 // Terminates fullscreen-mode for any full-screen window on the current desktop
46 void KonqMisc::abortFullScreenMode()
48 #ifdef Q_WS_X11
49 QList<KonqMainWindow*> *mainWindows = KonqMainWindow::mainWindowList();
50 if ( mainWindows )
52 foreach ( KonqMainWindow* window, *mainWindows )
54 if ( window->fullScreenMode() )
56 KWindowInfo info = KWindowSystem::windowInfo( window->winId(), NET::WMDesktop );
57 if ( info.valid() && info.isOnCurrentDesktop() )
58 window->setWindowState( window->windowState() & ~Qt::WindowFullScreen );
62 #endif
65 KonqMainWindow * KonqMisc::createSimpleWindow( const KUrl & url, const KParts::OpenUrlArguments &args,
66 const KParts::BrowserArguments& browserArgs,
67 bool tempFile )
69 abortFullScreenMode();
71 KonqOpenURLRequest req;
72 req.args = args;
73 req.browserArgs = browserArgs;
74 req.tempFile = tempFile;
75 KonqMainWindow *win = new KonqMainWindow;
76 win->openUrl( 0L, url, QString(), req );
77 win->show();
79 return win;
82 KonqMainWindow * KonqMisc::createNewWindow( const KUrl &url, const KParts::OpenUrlArguments &args,
83 const KParts::BrowserArguments& browserArgs,
84 bool forbidUseHTML, const QStringList &filesToSelect, bool tempFile, bool openUrl )
86 //kDebug() << "url=" << url;
87 // For HTTP or html files, use the web browsing profile, otherwise use filemanager profile
88 const QString profileName = url.isEmpty() || // e.g. in window.open
89 (!(KProtocolManager::supportsListing(url)) || // e.g. any HTTP url
90 KMimeType::findByUrl(url)->name() == "text/html")
91 ? "webbrowsing" : "filemanagement";
93 QString profile = KStandardDirs::locate( "data", QLatin1String("konqueror/profiles/") + profileName );
94 return createBrowserWindowFromProfile(profile, profileName,
95 url, args, browserArgs,
96 forbidUseHTML, filesToSelect, tempFile, openUrl );
99 KonqMainWindow * KonqMisc::createBrowserWindowFromProfile( const QString& _path, const QString &filename, const KUrl &url,
100 const KParts::OpenUrlArguments &args,
101 const KParts::BrowserArguments& browserArgs,
102 bool forbidUseHTML, const QStringList& filesToSelect, bool tempFile, bool openUrl )
104 QString path(_path);
105 kDebug(1202) << "path=" << path << ", filename=" << filename << ", url=" << url;
106 Q_ASSERT(!path.isEmpty());
107 // Well the path can be empty when misusing DBUS calls....
108 if (path.isEmpty())
109 path = defaultProfilePath();
111 abortFullScreenMode();
113 KonqOpenURLRequest req;
114 req.args = args;
115 req.browserArgs = browserArgs;
116 req.filesToSelect = filesToSelect;
117 req.tempFile = tempFile;
119 KonqMainWindow * mainWindow;
120 // Ask the user to recover session if appliable
121 if(KonqSessionManager::self()->askUserToRestoreAutosavedAbandonedSessions())
123 QList<KonqMainWindow*> *mainWindowList = KonqMainWindow::mainWindowList();
124 if(mainWindowList && !mainWindowList->isEmpty())
125 mainWindow = mainWindowList->first();
126 else // This should never happen but just to be sure
127 mainWindow = new KonqMainWindow;
129 if(!url.isEmpty())
130 mainWindow->openUrl( 0, url, QString(), req );
132 else if( KonqMainWindow::isPreloaded() && KonqMainWindow::preloadedWindow() != NULL )
134 mainWindow = KonqMainWindow::preloadedWindow();
135 #ifdef Q_WS_X11
136 KStartupInfo::setWindowStartupId( mainWindow->winId(), kapp->startupId());
137 #endif
138 KonqMainWindow::setPreloadedWindow( NULL );
139 KonqMainWindow::setPreloadedFlag( false );
140 mainWindow->resetWindow();
141 mainWindow->reparseConfiguration();
142 mainWindow->viewManager()->loadViewProfileFromFile(path, filename, url, req, true, openUrl);
144 else
146 KSharedConfigPtr cfg = KSharedConfig::openConfig(path, KConfig::SimpleConfig);
147 const KConfigGroup profileGroup(cfg, "Profile");
148 const QString xmluiFile = profileGroup.readPathEntry("XMLUIFile","konqueror.rc");
150 mainWindow = new KonqMainWindow(KUrl(), xmluiFile);
151 mainWindow->viewManager()->loadViewProfileFromConfig(cfg, path, filename, url, req, false, openUrl);
153 if ( forbidUseHTML )
154 mainWindow->setShowHTML( false );
155 mainWindow->setInitialFrameName( browserArgs.frameName );
156 mainWindow->show();
157 return mainWindow;
160 KonqMainWindow * KonqMisc::newWindowFromHistory( KonqView* view, int steps )
162 int oldPos = view->historyIndex();
163 int newPos = oldPos + steps;
165 const HistoryEntry * he = view->historyAt(newPos);
166 if(!he)
167 return 0L;
169 KonqMainWindow* mainwindow = createNewWindow(he->url, KParts::OpenUrlArguments(),
170 KParts::BrowserArguments(),
171 false, QStringList(), false, /*openUrl*/false);
172 if(!mainwindow)
173 return 0L;
174 KonqView* newView = mainwindow->currentView();
176 if(!newView)
177 return 0L;
179 newView->copyHistory(view);
180 newView->setHistoryIndex(newPos);
181 newView->restoreHistory();
182 return mainwindow;
185 QString KonqMisc::konqFilteredURL( QWidget* parent, const QString& _url, const QString& _path )
187 if ( !_url.startsWith( "about:" ) ) // Don't filter "about:" URLs
189 KUriFilterData data(_url);
191 if( !_path.isEmpty() )
192 data.setAbsolutePath(_path);
194 // We do not want to the filter to check for executables
195 // from the location bar.
196 data.setCheckForExecutables (false);
198 if( KUriFilter::self()->filterUri( data ) )
200 if( data.uriType() == KUriFilterData::Error && !data.errorMsg().isEmpty() )
202 KMessageBox::sorry( parent, i18n( data.errorMsg().toUtf8() ) );
203 return QString();
205 else
206 return data.uri().url();
209 else if (_url != "about:blank" && _url != "about:plugins" && !_url.startsWith("about:konqueror")) {
210 return "about:";
212 return _url; // return the original url if it cannot be filtered.
215 QString KonqMisc::defaultProfileName()
217 // By default try to open in webbrowser mode. People can use "konqueror ." to get a filemanager.
218 return "webbrowsing";
221 QString KonqMisc::defaultProfilePath()
223 return KStandardDirs::locate("data", QLatin1String("konqueror/profiles/")+ defaultProfileName());
226 QString KonqMisc::encodeFilename(QString filename)
228 return filename.replace(':', '_');
231 QString KonqMisc::decodeFilename(QString filename)
233 return filename.replace('_', ':');