2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2014-01-11 16:11:42 +01:00
* Copyright ( C ) 2010 - 2014 David Rosca < nowrep @ gmail . com >
2011-03-03 18:29:20 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
2011-03-02 16:57:41 +01:00
# include "mainapplication.h"
2014-03-10 00:47:07 +01:00
# include "qtwin.h"
# include "history.h"
# include "qztools.h"
# include "updater.h"
# include "autofill.h"
# include "settings.h"
# include "qzregexp.h"
# include "autosaver.h"
2014-03-09 21:51:42 +01:00
# include "datapaths.h"
2011-03-02 16:57:41 +01:00
# include "tabwidget.h"
# include "cookiejar.h"
2014-03-10 00:47:07 +01:00
# include "bookmarks.h"
# include "qzsettings.h"
2011-03-02 16:57:41 +01:00
# include "rssmanager.h"
2014-03-10 00:47:07 +01:00
# include "proxystyle.h"
2011-03-02 16:57:41 +01:00
# include "pluginproxy.h"
2011-04-25 20:56:45 +02:00
# include "iconprovider.h"
2014-03-10 00:47:07 +01:00
# include "browserwindow.h"
# include "networkmanager.h"
# include "checkboxdialog.h"
2014-03-09 21:51:42 +01:00
# include "profilemanager.h"
2014-03-10 00:47:07 +01:00
# include "adblockmanager.h"
# include "restoremanager.h"
# include "browsinglibrary.h"
# include "downloadmanager.h"
2012-03-23 17:29:12 +01:00
# include "clearprivatedata.h"
2012-08-16 23:15:31 +02:00
# include "useragentmanager.h"
2014-03-10 00:47:07 +01:00
# include "commandlineoptions.h"
# include "webhistoryinterface.h"
# include "searchenginesmanager.h"
# include "desktopnotificationsfactory.h"
2013-01-19 17:28:12 +01:00
# include "html5permissions/html5permissionsmanager.h"
2013-02-05 20:43:53 +01:00
2012-02-29 18:33:50 +01:00
# include <QNetworkDiskCache>
# include <QDesktopServices>
2014-05-04 20:21:41 +02:00
# include <QFontDatabase>
2014-03-07 18:03:42 +01:00
# include <QSqlDatabase>
2014-03-27 11:51:31 +01:00
# include <QLibraryInfo>
2014-10-05 16:12:29 +02:00
# include <QMessageBox>
2012-06-26 11:49:39 +02:00
# include <QTranslator>
2014-03-16 11:21:24 +01:00
# include <QThreadPool>
2012-02-29 18:33:50 +01:00
# include <QSettings>
2012-06-26 11:49:39 +02:00
# include <QProcess>
2012-02-29 18:33:50 +01:00
# include <QTimer>
2012-06-26 11:49:39 +02:00
# include <QDir>
2012-02-29 18:33:50 +01:00
2013-01-30 12:18:07 +01:00
# if QT_VERSION < 0x050000
# include "qwebkitversion.h"
2014-02-11 09:40:47 +01:00
# else
# include <QStandardPaths>
2013-01-30 12:18:07 +01:00
# endif
2014-03-19 12:14:01 +01:00
# if defined(Q_OS_WIN) && !defined(Q_OS_OS2)
# include "registerqappassociation.h"
# endif
2012-03-24 22:08:17 +01:00
MainApplication : : MainApplication ( int & argc , char * * argv )
2012-06-26 11:49:39 +02:00
: QtSingleApplication ( argc , argv )
2014-03-10 00:47:07 +01:00
, m_isPrivate ( false )
, m_isPortable ( false )
, m_isClosing ( false )
, m_isRestoring ( false )
, m_isStartingAfterCrash ( false )
2014-03-06 16:12:36 +01:00
, m_history ( 0 )
2014-02-05 15:26:51 +01:00
, m_bookmarks ( 0 )
2014-03-10 00:47:07 +01:00
, m_autoFill ( 0 )
, m_cookieJar ( 0 )
, m_plugins ( 0 )
2012-06-15 17:43:19 +02:00
, m_networkCache ( 0 )
2014-03-10 00:47:07 +01:00
, m_browsingLibrary ( 0 )
, m_rssManager ( 0 )
, m_networkManager ( 0 )
2012-08-21 20:28:38 +02:00
, m_restoreManager ( 0 )
2014-03-10 00:47:07 +01:00
, m_downloadManager ( 0 )
, m_userAgentManager ( 0 )
, m_searchEnginesManager ( 0 )
, m_html5PermissionsManager ( 0 )
, m_desktopNotifications ( 0 )
, m_autoSaver ( 0 )
2012-09-03 13:24:29 +02:00
, m_proxyStyle ( 0 )
2014-01-01 23:15:50 +01:00
# if defined(Q_OS_WIN) && !defined(Q_OS_OS2)
2012-12-01 07:50:31 +01:00
, m_registerQAppAssociation ( 0 )
2013-01-29 20:34:14 +01:00
# endif
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
setApplicationName ( QLatin1String ( " QupZilla " ) ) ;
2014-02-19 22:07:21 +01:00
setApplicationVersion ( Qz : : VERSION ) ;
2014-03-10 00:47:07 +01:00
setOrganizationDomain ( QLatin1String ( " qupzilla " ) ) ;
2014-04-16 09:17:57 +02:00
setWindowIcon ( QIcon : : fromTheme ( QSL ( " qupzilla " ) , QIcon ( QSL ( " :icons/exeicons/qupzilla-window.png " ) ) ) ) ;
2014-03-09 21:51:42 +01:00
2014-04-07 09:30:42 +02:00
// Set fallback icon theme (eg. on Windows/Mac)
if ( QIcon : : fromTheme ( QSL ( " view-refresh " ) ) . isNull ( ) ) {
QIcon : : setThemeSearchPaths ( QStringList ( ) < < QL1S ( " :/oxygen-fallback " ) ) ;
QIcon : : setThemeName ( QSL ( " oxygen-fallback " ) ) ;
}
2014-10-05 16:12:29 +02:00
// QSQLITE database plugin is required
if ( ! QSqlDatabase : : isDriverAvailable ( QSL ( " QSQLITE " ) ) ) {
QMessageBox : : critical ( 0 , QSL ( " Error " ) , QSL ( " Qt SQLite database plugin is not available. "
" Please install it and restart the application. " ) ) ;
m_isClosing = true ;
return ;
}
2014-05-04 20:21:41 +02:00
# ifdef Q_OS_WIN
// Set default app font (needed for N'ko)
int fontId = QFontDatabase : : addApplicationFont ( QSL ( " font.ttf " ) ) ;
if ( fontId ! = - 1 ) {
const QStringList families = QFontDatabase : : applicationFontFamilies ( fontId ) ;
if ( ! families . empty ( ) )
setFont ( QFont ( families . first ( ) ) ) ;
}
# endif
2012-03-13 15:10:18 +01:00
QUrl startUrl ;
2011-04-03 21:50:44 +02:00
QString startProfile ;
2014-03-10 00:47:07 +01:00
QStringList messages ;
bool noAddons = false ;
bool newInstance = false ;
2011-05-10 21:25:31 +02:00
2011-03-02 16:57:41 +01:00
if ( argc > 1 ) {
2012-03-24 22:08:17 +01:00
CommandLineOptions cmd ( argc ) ;
2013-03-06 09:05:41 +01:00
foreach ( const CommandLineOptions : : ActionPair & pair , cmd . getActions ( ) ) {
2011-05-18 22:58:49 +02:00
switch ( pair . action ) {
2012-01-21 23:19:38 +01:00
case Qz : : CL_StartWithoutAddons :
2011-04-03 21:50:44 +02:00
noAddons = true ;
break ;
2012-01-21 23:19:38 +01:00
case Qz : : CL_StartWithProfile :
2011-05-18 22:58:49 +02:00
startProfile = pair . text ;
break ;
2014-01-23 13:02:07 +01:00
case Qz : : CL_StartPortable :
m_isPortable = true ;
break ;
2012-01-21 23:19:38 +01:00
case Qz : : CL_NewTab :
2012-09-04 12:42:45 +02:00
messages . append ( QLatin1String ( " ACTION:NewTab " ) ) ;
2011-12-30 16:54:37 +01:00
m_postLaunchActions . append ( OpenNewTab ) ;
2011-05-18 22:58:49 +02:00
break ;
2012-01-21 23:19:38 +01:00
case Qz : : CL_NewWindow :
2012-09-04 12:42:45 +02:00
messages . append ( QLatin1String ( " ACTION:NewWindow " ) ) ;
2011-05-18 22:58:49 +02:00
break ;
2014-01-22 17:34:25 +01:00
case Qz : : CL_ToggleFullScreen :
messages . append ( QLatin1String ( " ACTION:ToggleFullScreen " ) ) ;
m_postLaunchActions . append ( ToggleFullScreen ) ;
break ;
2012-01-21 23:19:38 +01:00
case Qz : : CL_ShowDownloadManager :
2012-09-04 12:42:45 +02:00
messages . append ( QLatin1String ( " ACTION:ShowDownloadManager " ) ) ;
2011-12-30 16:54:37 +01:00
m_postLaunchActions . append ( OpenDownloadManager ) ;
2011-12-26 20:23:54 +01:00
break ;
2012-01-21 23:19:38 +01:00
case Qz : : CL_StartPrivateBrowsing :
2014-03-10 00:47:07 +01:00
m_isPrivate = true ;
2011-05-18 22:58:49 +02:00
break ;
2012-09-01 15:59:27 +02:00
case Qz : : CL_StartNewInstance :
newInstance = true ;
break ;
2012-04-03 20:41:07 +02:00
case Qz : : CL_OpenUrlInCurrentTab :
startUrl = QUrl : : fromUserInput ( pair . text ) ;
messages . append ( " ACTION:OpenUrlInCurrentTab " + pair . text ) ;
2012-07-10 00:36:05 +02:00
break ;
case Qz : : CL_OpenUrlInNewWindow :
startUrl = QUrl : : fromUserInput ( pair . text ) ;
messages . append ( " ACTION:OpenUrlInNewWindow " + pair . text ) ;
2012-04-03 20:41:07 +02:00
break ;
2012-01-21 23:19:38 +01:00
case Qz : : CL_OpenUrl :
2012-03-24 22:08:17 +01:00
startUrl = QUrl : : fromUserInput ( pair . text ) ;
2012-03-16 23:28:23 +01:00
messages . append ( " URL: " + pair . text ) ;
2011-04-03 21:50:44 +02:00
break ;
2012-03-24 22:08:17 +01:00
case Qz : : CL_ExitAction :
m_isClosing = true ;
return ;
2011-04-03 21:50:44 +02:00
default :
break ;
}
2011-03-02 16:57:41 +01:00
}
}
2014-01-23 13:02:07 +01:00
if ( isPortable ( ) ) {
std : : cout < < " QupZilla: Running in Portable Mode. " < < std : : endl ;
2014-03-09 21:51:42 +01:00
DataPaths : : setPortableVersion ( ) ;
2014-01-23 13:02:07 +01:00
}
2012-06-26 11:49:39 +02:00
// Don't start single application in private browsing
2014-03-10 00:47:07 +01:00
if ( ! isPrivate ( ) ) {
QString appId = QLatin1String ( " QupZillaWebBrowser " ) ;
if ( isPortable ( ) ) {
appId . append ( QLatin1String ( " Portable " ) ) ;
}
2012-09-01 15:59:27 +02:00
2014-03-10 00:47:07 +01:00
// TODO: This should generate some random string for appId
2012-09-01 15:59:27 +02:00
if ( newInstance ) {
2012-09-04 12:42:45 +02:00
if ( startProfile . isEmpty ( ) | | startProfile = = QLatin1String ( " default " ) ) {
2012-09-01 15:59:27 +02:00
std : : cout < < " New instance cannot be started with default profile! " < < std : : endl ;
}
else {
appId . append ( startProfile ) ;
}
}
setAppId ( appId ) ;
2012-06-26 11:49:39 +02:00
}
2014-03-10 00:47:07 +01:00
// If there is nothing to tell other instance, we need to at least wake it
2011-12-27 12:27:39 +01:00
if ( messages . isEmpty ( ) ) {
2012-09-04 12:42:45 +02:00
messages . append ( QLatin1String ( " " ) ) ;
2011-12-27 12:27:39 +01:00
}
2011-03-02 16:57:41 +01:00
if ( isRunning ( ) ) {
2014-03-10 00:47:07 +01:00
m_isClosing = true ;
2013-03-06 09:05:41 +01:00
foreach ( const QString & message , messages ) {
2011-12-26 20:23:54 +01:00
sendMessage ( message ) ;
}
2011-03-02 16:57:41 +01:00
return ;
}
2012-09-03 22:48:52 +02:00
# ifdef Q_OS_MAC
2012-01-06 15:03:48 +01:00
setQuitOnLastWindowClosed ( false ) ;
# else
2011-03-02 16:57:41 +01:00
setQuitOnLastWindowClosed ( true ) ;
2012-01-06 15:03:48 +01:00
# endif
2014-03-09 21:51:42 +01:00
QSettings : : setDefaultFormat ( QSettings : : IniFormat ) ;
2012-01-06 15:03:48 +01:00
QDesktopServices : : setUrlHandler ( " http " , this , " addNewTab " ) ;
2013-01-28 10:52:55 +01:00
QDesktopServices : : setUrlHandler ( " ftp " , this , " addNewTab " ) ;
2011-03-02 16:57:41 +01:00
2014-03-09 21:51:42 +01:00
ProfileManager profileManager ;
profileManager . initConfigDir ( ) ;
profileManager . initCurrentProfile ( startProfile ) ;
2011-10-18 14:30:17 +02:00
2014-03-09 22:17:13 +01:00
Settings : : createSettings ( DataPaths : : currentProfilePath ( ) + QLatin1String ( " /settings.ini " ) ) ;
2012-01-11 21:58:25 +01:00
2014-03-06 16:12:36 +01:00
m_autoSaver = new AutoSaver ( this ) ;
2014-03-10 00:47:07 +01:00
connect ( m_autoSaver , SIGNAL ( save ( ) ) , this , SLOT ( saveSession ( ) ) ) ;
2014-03-06 16:12:36 +01:00
2011-03-02 16:57:41 +01:00
translateApp ( ) ;
loadSettings ( ) ;
2012-03-13 15:10:18 +01:00
2012-04-04 21:12:52 +02:00
m_plugins = new PluginProxy ;
2012-03-13 15:10:18 +01:00
if ( ! noAddons ) {
2012-04-04 21:12:52 +02:00
m_plugins - > loadPlugins ( ) ;
2012-03-13 15:10:18 +01:00
}
2011-03-29 19:55:21 +02:00
2014-09-25 17:37:20 +02:00
BrowserWindow * window = createWindow ( Qz : : BW_FirstAppWindow , startUrl ) ;
connect ( window , SIGNAL ( startingCompleted ( ) ) , this , SLOT ( restoreOverrideCursor ( ) ) ) ;
2014-03-10 00:47:07 +01:00
if ( ! isPrivate ( ) ) {
2012-06-26 11:49:39 +02:00
Settings settings ;
2014-03-09 21:51:42 +01:00
m_isStartingAfterCrash = settings . value ( " SessionRestore/isRunning " , false ) . toBool ( ) ;
2014-02-11 15:44:47 +01:00
int afterLaunch = settings . value ( " Web-URL-Settings/afterLaunch " , 3 ) . toInt ( ) ;
2012-06-26 11:49:39 +02:00
settings . setValue ( " SessionRestore/isRunning " , true ) ;
2014-02-02 16:21:06 +01:00
# ifndef DISABLE_UPDATES_CHECK
2014-02-22 15:23:46 +01:00
bool checkUpdates = settings . value ( " Web-Browser-Settings/CheckUpdates " , DEFAULT_CHECK_UPDATES ) . toBool ( ) ;
2012-06-26 11:49:39 +02:00
if ( checkUpdates ) {
2014-03-10 00:47:07 +01:00
new Updater ( window ) ;
2012-06-26 11:49:39 +02:00
}
2014-02-02 16:21:06 +01:00
# endif
2013-12-06 21:26:07 +01:00
backupSavedSessions ( ) ;
2014-03-09 21:51:42 +01:00
if ( m_isStartingAfterCrash | | afterLaunch = = 3 ) {
m_restoreManager = new RestoreManager ( ) ;
2012-12-25 16:56:23 +01:00
if ( ! m_restoreManager - > isValid ( ) ) {
destroyRestoreManager ( ) ;
}
2012-08-21 20:28:38 +02:00
}
2012-03-14 14:04:20 +01:00
}
2011-12-30 16:54:37 +01:00
QTimer : : singleShot ( 0 , this , SLOT ( postLaunch ( ) ) ) ;
}
2014-03-09 21:51:42 +01:00
MainApplication : : ~ MainApplication ( )
{
2014-03-16 11:21:24 +01:00
IconProvider : : instance ( ) - > saveIconsToDatabase ( ) ;
// Wait for all QtConcurrent jobs to finish
QThreadPool : : globalInstance ( ) - > waitForDone ( ) ;
2014-03-09 21:51:42 +01:00
// Delete all classes that are saving data in destructor
delete m_bookmarks ;
2014-03-10 09:38:16 +01:00
delete m_cookieJar ;
2014-09-29 18:23:42 +02:00
delete m_plugins ;
Settings : : syncSettings ( ) ;
2014-03-09 21:51:42 +01:00
}
2014-03-10 00:47:07 +01:00
bool MainApplication : : isClosing ( ) const
2011-12-30 16:54:37 +01:00
{
2014-03-10 00:47:07 +01:00
return m_isClosing ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
bool MainApplication : : isRestoring ( ) const
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
return m_isRestoring ;
}
2012-12-21 16:38:44 +01:00
2014-03-10 00:47:07 +01:00
bool MainApplication : : isPrivate ( ) const
{
return m_isPrivate ;
}
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
bool MainApplication : : isPortable ( ) const
{
# ifdef PORTABLE_BUILD
return true ;
# else
return m_isPortable ;
2011-11-05 22:02:23 +01:00
# endif
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
bool MainApplication : : isStartingAfterCrash ( ) const
2012-01-12 21:32:27 +01:00
{
2014-03-10 00:47:07 +01:00
return m_isStartingAfterCrash ;
2012-01-12 21:32:27 +01:00
}
2014-03-10 00:47:07 +01:00
int MainApplication : : windowCount ( ) const
2011-10-22 22:29:33 +02:00
{
2014-03-10 00:47:07 +01:00
return m_windows . count ( ) ;
2011-10-22 22:29:33 +02:00
}
2014-03-10 00:47:07 +01:00
QList < BrowserWindow * > MainApplication : : windows ( ) const
2011-05-06 20:05:49 +02:00
{
2014-03-10 00:47:07 +01:00
return m_windows ;
2011-05-06 20:05:49 +02:00
}
2014-03-10 00:47:07 +01:00
BrowserWindow * MainApplication : : getWindow ( ) const
2011-03-02 16:57:41 +01:00
{
2014-02-19 22:07:21 +01:00
BrowserWindow * activeW = qobject_cast < BrowserWindow * > ( activeWindow ( ) ) ;
2014-03-10 00:47:07 +01:00
2012-08-23 15:52:07 +02:00
if ( activeW ) {
return activeW ;
}
2014-03-10 00:47:07 +01:00
return m_windows . isEmpty ( ) ? 0 : m_windows . first ( ) ;
}
2012-08-23 15:52:07 +02:00
2014-03-10 00:47:07 +01:00
BrowserWindow * MainApplication : : createWindow ( Qz : : BrowserWindowType type , const QUrl & startUrl )
{
if ( windowCount ( ) = = 0 & & type ! = Qz : : BW_MacFirstWindow ) {
type = Qz : : BW_FirstAppWindow ;
2011-03-02 16:57:41 +01:00
}
2012-08-23 15:52:07 +02:00
2014-03-10 00:47:07 +01:00
BrowserWindow * window = new BrowserWindow ( type , startUrl ) ;
connect ( window , SIGNAL ( destroyed ( QObject * ) ) , this , SLOT ( windowDestroyed ( QObject * ) ) ) ;
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
m_windows . prepend ( window ) ;
return window ;
2011-07-28 12:12:00 +02:00
}
2014-03-10 00:47:07 +01:00
bool MainApplication : : restoreSession ( BrowserWindow * window , RestoreData restoreData )
2012-04-01 16:36:38 +02:00
{
2014-03-10 00:47:07 +01:00
if ( m_isPrivate | | restoreData . isEmpty ( ) ) {
return false ;
}
2012-04-01 16:36:38 +02:00
2014-03-10 00:47:07 +01:00
m_isRestoring = true ;
2014-03-25 16:55:00 +01:00
setOverrideCursor ( Qt : : BusyCursor ) ;
2014-03-10 00:47:07 +01:00
2014-04-08 15:47:48 +02:00
window - > setUpdatesEnabled ( false ) ;
2014-03-10 00:47:07 +01:00
window - > tabWidget ( ) - > closeRecoveryTab ( ) ;
if ( window - > tabWidget ( ) - > normalTabsCount ( ) > 1 ) {
// This can only happen when recovering crashed session!
//
// Don't restore tabs in current window as user already opened
// some new tabs.
// Instead create new one and restore pinned tabs there
BrowserWindow * newWin = createWindow ( Qz : : BW_OtherRestoredWindow ) ;
2014-04-08 15:47:48 +02:00
newWin - > setUpdatesEnabled ( false ) ;
2014-03-10 00:47:07 +01:00
newWin - > restoreWindowState ( restoreData . first ( ) ) ;
2014-04-08 15:47:48 +02:00
newWin - > setUpdatesEnabled ( true ) ;
2014-03-10 00:47:07 +01:00
restoreData . remove ( 0 ) ;
}
else {
// QTabWidget::count() - count of tabs is not updated after closing
// recovery tab ...
// update: it seems with ComboTabBar QTabWidget::count() is updated,
// we add pinnedTabCounts to currentTab!
int tabCount = window - > tabWidget ( ) - > pinnedTabsCount ( ) ;
RestoreManager : : WindowData data = restoreData . first ( ) ;
data . currentTab + = tabCount ;
restoreData . remove ( 0 ) ;
window - > restoreWindowState ( data ) ;
2012-04-01 16:36:38 +02:00
}
2014-04-08 15:47:48 +02:00
window - > setUpdatesEnabled ( true ) ;
2014-04-01 16:34:08 +02:00
processEvents ( ) ;
2014-03-10 00:47:07 +01:00
foreach ( const RestoreManager : : WindowData & data , restoreData ) {
BrowserWindow * window = createWindow ( Qz : : BW_OtherRestoredWindow ) ;
2014-04-08 15:47:48 +02:00
window - > setUpdatesEnabled ( false ) ;
2014-03-10 00:47:07 +01:00
window - > restoreWindowState ( data ) ;
2014-04-08 15:47:48 +02:00
window - > setUpdatesEnabled ( true ) ;
2014-04-01 16:34:08 +02:00
processEvents ( ) ;
2014-03-10 00:47:07 +01:00
}
2012-04-01 16:36:38 +02:00
2014-03-10 00:47:07 +01:00
destroyRestoreManager ( ) ;
2014-03-25 16:55:00 +01:00
restoreOverrideCursor ( ) ;
2014-03-10 00:47:07 +01:00
m_isRestoring = false ;
return true ;
2013-02-05 20:43:53 +01:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : destroyRestoreManager ( )
2013-02-11 16:45:10 +01:00
{
2014-03-10 00:47:07 +01:00
delete m_restoreManager ;
m_restoreManager = 0 ;
2013-02-11 16:45:10 +01:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : reloadSettings ( )
2013-02-05 20:43:53 +01:00
{
2014-03-10 00:47:07 +01:00
loadSettings ( ) ;
emit settingsReloaded ( ) ;
2013-02-05 20:43:53 +01:00
}
2014-03-10 00:47:07 +01:00
ProxyStyle * MainApplication : : proxyStyle ( ) const
2014-01-23 13:02:07 +01:00
{
2014-03-10 00:47:07 +01:00
return m_proxyStyle ;
2014-01-23 13:02:07 +01:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : setProxyStyle ( ProxyStyle * style )
2013-02-05 15:41:10 +01:00
{
2014-03-10 00:47:07 +01:00
m_proxyStyle = style ;
QApplication : : setStyle ( style ) ;
2013-02-05 20:43:53 +01:00
}
2014-03-10 00:47:07 +01:00
QString MainApplication : : styleName ( ) const
2013-02-05 20:43:53 +01:00
{
2014-03-10 00:47:07 +01:00
return m_proxyStyle ? m_proxyStyle - > baseStyle ( ) - > objectName ( ) : QString ( ) ;
2013-02-05 20:43:53 +01:00
}
QString MainApplication : : currentLanguageFile ( ) const
{
2014-03-10 00:47:07 +01:00
return m_languageFile ;
2013-02-05 20:43:53 +01:00
}
QString MainApplication : : currentLanguage ( ) const
{
2014-03-10 00:47:07 +01:00
QString lang = m_languageFile ;
2013-02-05 20:43:53 +01:00
if ( lang . isEmpty ( ) ) {
return " en_US " ;
}
return lang . left ( lang . length ( ) - 3 ) ;
}
2014-03-10 00:47:07 +01:00
History * MainApplication : : history ( )
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
if ( ! m_history ) {
m_history = new History ( this ) ;
}
return m_history ;
}
2012-04-03 20:41:07 +02:00
2014-03-10 00:47:07 +01:00
Bookmarks * MainApplication : : bookmarks ( )
{
if ( ! m_bookmarks ) {
m_bookmarks = new Bookmarks ( this ) ;
2011-11-06 17:01:23 +01:00
}
2014-03-10 00:47:07 +01:00
return m_bookmarks ;
}
AutoFill * MainApplication : : autoFill ( )
{
if ( ! m_autoFill ) {
m_autoFill = new AutoFill ( this ) ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
return m_autoFill ;
}
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
CookieJar * MainApplication : : cookieJar ( )
{
if ( ! m_cookieJar ) {
m_cookieJar = new CookieJar ( this ) ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
return m_cookieJar ;
}
2011-12-27 12:27:39 +01:00
2014-03-10 00:47:07 +01:00
PluginProxy * MainApplication : : plugins ( )
{
return m_plugins ;
}
2012-04-03 20:41:07 +02:00
2014-03-10 00:47:07 +01:00
QNetworkDiskCache * MainApplication : : networkCache ( )
{
if ( ! m_networkCache ) {
Settings settings ;
2014-03-13 12:53:19 +01:00
const QString defaultBasePath = QString ( " %1/networkcache/ " ) . arg ( DataPaths : : currentProfilePath ( ) ) ;
2014-03-10 00:47:07 +01:00
const QString basePath = settings . value ( " Web-Browser-Settings/CachePath " , defaultBasePath ) . toString ( ) ;
const QString cachePath = QString ( " %1/%2-Qt%3/ " ) . arg ( basePath , qWebKitVersion ( ) , qVersion ( ) ) ;
2014-01-22 17:34:25 +01:00
2014-03-10 00:47:07 +01:00
m_networkCache = new QNetworkDiskCache ( this ) ;
m_networkCache - > setCacheDirectory ( cachePath ) ;
2012-04-03 20:41:07 +02:00
}
2014-03-10 00:47:07 +01:00
return m_networkCache ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
BrowsingLibrary * MainApplication : : browsingLibrary ( )
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
if ( ! m_browsingLibrary ) {
m_browsingLibrary = new BrowsingLibrary ( getWindow ( ) ) ;
2011-11-06 17:01:23 +01:00
}
2014-03-10 00:47:07 +01:00
return m_browsingLibrary ;
}
2013-01-25 23:49:46 +01:00
2014-03-10 00:47:07 +01:00
RSSManager * MainApplication : : rssManager ( )
{
if ( ! m_rssManager ) {
m_rssManager = new RSSManager ( getWindow ( ) ) ;
}
return m_rssManager ;
}
2012-01-18 18:36:10 +01:00
2014-03-10 00:47:07 +01:00
NetworkManager * MainApplication : : networkManager ( )
{
if ( ! m_networkManager ) {
m_networkManager = new NetworkManager ( this ) ;
}
return m_networkManager ;
}
2011-12-27 12:27:39 +01:00
2014-03-10 00:47:07 +01:00
RestoreManager * MainApplication : : restoreManager ( )
{
return m_restoreManager ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
DownloadManager * MainApplication : : downloadManager ( )
{
if ( ! m_downloadManager ) {
m_downloadManager = new DownloadManager ( ) ;
}
return m_downloadManager ;
}
2013-03-16 14:57:08 +01:00
2014-03-10 00:47:07 +01:00
UserAgentManager * MainApplication : : userAgentManager ( )
2013-03-16 14:57:08 +01:00
{
2014-03-10 00:47:07 +01:00
if ( ! m_userAgentManager ) {
m_userAgentManager = new UserAgentManager ( this ) ;
2013-03-16 14:57:08 +01:00
}
2014-03-10 00:47:07 +01:00
return m_userAgentManager ;
2013-03-16 14:57:08 +01:00
}
2014-03-10 00:47:07 +01:00
SearchEnginesManager * MainApplication : : searchEnginesManager ( )
2013-02-07 14:01:01 +01:00
{
2014-03-10 00:47:07 +01:00
if ( ! m_searchEnginesManager ) {
m_searchEnginesManager = new SearchEnginesManager ( this ) ;
2013-02-07 14:01:01 +01:00
}
2014-03-10 00:47:07 +01:00
return m_searchEnginesManager ;
2013-02-07 14:01:01 +01:00
}
2014-03-10 00:47:07 +01:00
HTML5PermissionsManager * MainApplication : : html5PermissionsManager ( )
2012-01-12 20:10:51 +01:00
{
2014-03-10 00:47:07 +01:00
if ( ! m_html5PermissionsManager ) {
m_html5PermissionsManager = new HTML5PermissionsManager ( this ) ;
2012-01-12 20:10:51 +01:00
}
2014-03-10 00:47:07 +01:00
return m_html5PermissionsManager ;
}
2012-01-12 20:10:51 +01:00
2014-03-10 00:47:07 +01:00
DesktopNotificationsFactory * MainApplication : : desktopNotifications ( )
{
if ( ! m_desktopNotifications ) {
m_desktopNotifications = new DesktopNotificationsFactory ( this ) ;
2012-01-12 20:10:51 +01:00
}
2014-03-10 00:47:07 +01:00
return m_desktopNotifications ;
}
2012-01-12 20:10:51 +01:00
2014-03-10 00:47:07 +01:00
// static
MainApplication * MainApplication : : instance ( )
{
return static_cast < MainApplication * > ( QCoreApplication : : instance ( ) ) ;
2012-01-12 20:10:51 +01:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : addNewTab ( const QUrl & url )
2013-06-07 12:59:22 +02:00
{
2014-03-10 00:47:07 +01:00
BrowserWindow * window = getWindow ( ) ;
2013-06-07 12:59:22 +02:00
2014-03-10 00:47:07 +01:00
if ( window ) {
window - > tabWidget ( ) - > addView ( url , url . isEmpty ( ) ? Qz : : NT_SelectedNewEmptyTab : Qz : : NT_SelectedTabAtTheEnd ) ;
2013-06-07 12:59:22 +02:00
}
2014-03-10 00:47:07 +01:00
}
2013-06-07 12:59:22 +02:00
2014-03-10 00:47:07 +01:00
void MainApplication : : startPrivateBrowsing ( const QUrl & startUrl )
{
QUrl url = startUrl ;
if ( QAction * act = qobject_cast < QAction * > ( sender ( ) ) ) {
url = act - > data ( ) . toUrl ( ) ;
2013-06-07 12:59:22 +02:00
}
2014-03-10 00:47:07 +01:00
QStringList args ;
foreach ( const QString & arg , arguments ( ) ) {
if ( arg . startsWith ( QLatin1Char ( ' - ' ) ) & & arg ! = QLatin1String ( " --private-browsing " ) & & arg ! = QLatin1String ( " -pb " ) ) {
args . append ( arg ) ;
}
2013-06-07 12:59:22 +02:00
}
2014-03-10 00:47:07 +01:00
args . append ( QLatin1String ( " --private-browsing " ) ) ;
2013-06-07 12:59:22 +02:00
2014-03-10 00:47:07 +01:00
if ( ! url . isEmpty ( ) ) {
args < < url . toEncoded ( ) ;
2013-06-07 12:59:22 +02:00
}
2014-03-10 00:47:07 +01:00
if ( ! QProcess : : startDetached ( applicationFilePath ( ) , args ) ) {
qWarning ( ) < < " MainApplication: Cannot start new browser process for private browsing! " < < applicationFilePath ( ) < < args ;
2013-06-07 12:59:22 +02:00
}
2014-03-10 00:47:07 +01:00
}
2013-06-07 12:59:22 +02:00
2014-03-10 00:47:07 +01:00
void MainApplication : : reloadUserStyleSheet ( )
{
const QUrl userCss = userStyleSheet ( Settings ( ) . value ( " Web-Browser-Settings/userStyleSheet " , QString ( ) ) . toString ( ) ) ;
QWebSettings : : globalSettings ( ) - > setUserStyleSheetUrl ( userCss ) ;
}
2013-06-07 12:59:22 +02:00
2014-03-10 00:47:07 +01:00
void MainApplication : : restoreOverrideCursor ( )
{
QApplication : : restoreOverrideCursor ( ) ;
2013-06-07 12:59:22 +02:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : changeOcurred ( )
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
m_autoSaver - > changeOcurred ( ) ;
}
2013-02-05 20:43:53 +01:00
2014-03-10 00:47:07 +01:00
void MainApplication : : quitApplication ( )
{
if ( m_downloadManager & & ! m_downloadManager - > canClose ( ) ) {
m_downloadManager - > show ( ) ;
return ;
2013-02-05 20:43:53 +01:00
}
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
if ( m_windows . count ( ) > 0 ) {
m_autoSaver - > saveIfNecessary ( ) ;
2014-01-17 21:39:02 +01:00
}
2014-03-10 00:47:07 +01:00
m_isClosing = true ;
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
// Saving settings in saveSettings() slot called from quit() so
// everything gets saved also when quitting application in other
// way than clicking Quit action in File menu or closing last window
// eg. on Mac (#157)
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
if ( ! isPrivate ( ) ) {
removeLockFile ( ) ;
}
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
quit ( ) ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : postLaunch ( )
2013-12-06 21:26:07 +01:00
{
2014-03-10 00:47:07 +01:00
if ( m_postLaunchActions . contains ( OpenDownloadManager ) ) {
downloadManager ( ) - > show ( ) ;
}
2013-12-06 21:26:07 +01:00
2014-03-10 00:47:07 +01:00
if ( m_postLaunchActions . contains ( OpenNewTab ) ) {
getWindow ( ) - > tabWidget ( ) - > addView ( QUrl ( ) , Qz : : NT_SelectedNewEmptyTab ) ;
2013-12-06 21:26:07 +01:00
}
2014-03-10 00:47:07 +01:00
if ( m_postLaunchActions . contains ( ToggleFullScreen ) ) {
getWindow ( ) - > toggleFullScreen ( ) ;
2013-12-06 21:26:07 +01:00
}
2014-03-10 00:47:07 +01:00
QSettings : : setPath ( QSettings : : IniFormat , QSettings : : UserScope , DataPaths : : currentProfilePath ( ) ) ;
QWebHistoryInterface : : setDefaultInterface ( new WebHistoryInterface ( this ) ) ;
connect ( this , SIGNAL ( messageReceived ( QString ) ) , this , SLOT ( messageReceived ( QString ) ) ) ;
connect ( this , SIGNAL ( aboutToQuit ( ) ) , this , SLOT ( saveSettings ( ) ) ) ;
checkDefaultWebBrowser ( ) ;
QtWin : : createJumpList ( ) ;
2013-12-06 21:26:07 +01:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : saveSession ( )
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
if ( m_isPrivate | | m_isRestoring | | m_windows . count ( ) = = 0 | | m_restoreManager ) {
2011-03-02 16:57:41 +01:00
return ;
}
2012-01-04 14:09:27 +01:00
2014-03-10 00:47:07 +01:00
QByteArray data ;
QDataStream stream ( & data , QIODevice : : WriteOnly ) ;
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
stream < < Qz : : sessionVersion ;
stream < < m_windows . count ( ) ;
2014-03-06 16:12:36 +01:00
2014-03-10 00:47:07 +01:00
foreach ( BrowserWindow * w , m_windows ) {
stream < < w - > tabWidget ( ) - > saveState ( ) ;
if ( w - > isFullScreen ( ) ) {
stream < < QByteArray ( ) ;
}
else {
stream < < w - > saveState ( ) ;
}
}
2012-01-12 20:10:51 +01:00
2014-03-10 00:47:07 +01:00
BrowserWindow * qupzilla_ = getWindow ( ) ;
if ( qupzilla_ & & m_windows . count ( ) = = 1 ) {
qupzilla_ - > tabWidget ( ) - > savePinnedTabs ( ) ;
2013-04-16 10:00:18 +02:00
}
2014-03-10 00:47:07 +01:00
QFile file ( DataPaths : : currentProfilePath ( ) + QLatin1String ( " /session.dat " ) ) ;
file . open ( QIODevice : : WriteOnly ) ;
file . write ( data ) ;
file . close ( ) ;
2012-01-12 20:10:51 +01:00
}
void MainApplication : : saveSettings ( )
{
2014-03-10 00:47:07 +01:00
if ( isPrivate ( ) ) {
2012-06-26 11:49:39 +02:00
return ;
}
2012-01-12 20:10:51 +01:00
m_isClosing = true ;
2012-01-11 21:58:25 +01:00
Settings settings ;
2011-03-02 16:57:41 +01:00
settings . beginGroup ( " SessionRestore " ) ;
2011-11-06 17:01:23 +01:00
settings . setValue ( " isRunning " , false ) ;
2011-03-02 16:57:41 +01:00
settings . endGroup ( ) ;
2012-01-26 18:23:35 +01:00
settings . beginGroup ( " Web-Browser-Settings " ) ;
bool deleteHistory = settings . value ( " deleteHistoryOnClose " , false ) . toBool ( ) ;
bool deleteHtml5Storage = settings . value ( " deleteHTML5StorageOnClose " , false ) . toBool ( ) ;
settings . endGroup ( ) ;
2011-03-02 16:57:41 +01:00
2011-11-06 17:01:23 +01:00
if ( deleteHistory ) {
2014-03-06 16:12:36 +01:00
m_history - > clearHistory ( ) ;
2011-11-06 17:01:23 +01:00
}
2012-01-26 18:23:35 +01:00
if ( deleteHtml5Storage ) {
2012-03-23 17:29:12 +01:00
ClearPrivateData : : clearLocalStorage ( ) ;
2012-01-26 18:23:35 +01:00
}
2011-03-02 16:57:41 +01:00
2011-10-21 23:26:34 +02:00
m_searchEnginesManager - > saveSettings ( ) ;
2014-03-10 00:47:07 +01:00
m_networkManager - > saveSettings ( ) ;
2012-07-08 14:03:50 +02:00
m_plugins - > shutdown ( ) ;
2014-03-09 21:51:42 +01:00
DataPaths : : clearTempData ( ) ;
2011-03-02 16:57:41 +01:00
2013-02-09 15:44:17 +01:00
qzSettings - > saveSettings ( ) ;
2012-01-12 20:10:51 +01:00
AdBlockManager : : instance ( ) - > save ( ) ;
2014-03-09 22:17:13 +01:00
QFile : : remove ( DataPaths : : currentProfilePath ( ) + QLatin1String ( " /WebpageIcons.db " ) ) ;
2011-03-02 16:57:41 +01:00
}
2014-05-01 14:08:06 +02:00
void MainApplication : : messageReceived ( const QString & message )
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
QWidget * actWin = getWindow ( ) ;
QUrl actUrl ;
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
if ( message . startsWith ( QLatin1String ( " URL: " ) ) ) {
const QUrl url = QUrl : : fromUserInput ( message . mid ( 4 ) ) ;
addNewTab ( url ) ;
actWin = getWindow ( ) ;
2011-11-06 17:01:23 +01:00
}
2014-03-10 00:47:07 +01:00
else if ( message . startsWith ( QLatin1String ( " ACTION: " ) ) ) {
const QString text = message . mid ( 7 ) ;
if ( text = = QLatin1String ( " NewTab " ) ) {
addNewTab ( ) ;
}
else if ( text = = QLatin1String ( " NewWindow " ) ) {
actWin = createWindow ( Qz : : BW_NewWindow ) ;
}
else if ( text = = QLatin1String ( " ShowDownloadManager " ) ) {
downloadManager ( ) - > show ( ) ;
actWin = downloadManager ( ) ;
}
else if ( text = = QLatin1String ( " ToggleFullScreen " ) & & actWin ) {
BrowserWindow * qz = static_cast < BrowserWindow * > ( actWin ) ;
qz - > toggleFullScreen ( ) ;
}
else if ( text . startsWith ( QLatin1String ( " OpenUrlInCurrentTab " ) ) ) {
actUrl = QUrl : : fromUserInput ( text . mid ( 19 ) ) ;
}
else if ( text . startsWith ( QLatin1String ( " OpenUrlInNewWindow " ) ) ) {
createWindow ( Qz : : BW_NewWindow , QUrl : : fromUserInput ( text . mid ( 18 ) ) ) ;
return ;
}
2011-11-06 17:01:23 +01:00
}
2014-05-01 14:08:06 +02:00
else {
// User attempted to start another instance, let's open a new window
actWin = createWindow ( Qz : : BW_NewWindow ) ;
}
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
if ( ! actWin ) {
if ( ! isClosing ( ) ) {
// It can only occur if download manager window was still opened
createWindow ( Qz : : BW_NewWindow , actUrl ) ;
}
return ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
actWin - > setWindowState ( actWin - > windowState ( ) & ~ Qt : : WindowMinimized ) ;
actWin - > raise ( ) ;
actWin - > activateWindow ( ) ;
actWin - > setFocus ( ) ;
BrowserWindow * win = qobject_cast < BrowserWindow * > ( actWin ) ;
if ( win & & ! actUrl . isEmpty ( ) ) {
win - > loadAddress ( actUrl ) ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
}
2014-03-10 19:41:44 +01:00
void MainApplication : : windowDestroyed ( QObject * window )
2012-04-04 21:12:52 +02:00
{
2014-03-10 16:55:11 +01:00
// qobject_cast doesn't work because QObject::destroyed is emitted from destructor
Q_ASSERT ( static_cast < BrowserWindow * > ( window ) ) ;
Q_ASSERT ( m_windows . contains ( static_cast < BrowserWindow * > ( window ) ) ) ;
2014-03-10 00:47:07 +01:00
2014-03-10 16:55:11 +01:00
m_windows . removeOne ( static_cast < BrowserWindow * > ( window ) ) ;
2012-04-04 21:12:52 +02:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : loadSettings ( )
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
Settings settings ;
settings . beginGroup ( " Themes " ) ;
QString activeTheme = settings . value ( " activeTheme " , DEFAULT_THEME_NAME ) . toString ( ) ;
settings . endGroup ( ) ;
loadTheme ( activeTheme ) ;
QWebSettings * webSettings = QWebSettings : : globalSettings ( ) ;
// Web browsing settings
settings . beginGroup ( " Web-Browser-Settings " ) ;
if ( ! m_isPrivate ) {
webSettings - > enablePersistentStorage ( DataPaths : : currentProfilePath ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : LocalStorageEnabled , settings . value ( " HTML5StorageEnabled " , true ) . toBool ( ) ) ;
}
webSettings - > setAttribute ( QWebSettings : : DeveloperExtrasEnabled , true ) ;
webSettings - > setAttribute ( QWebSettings : : PluginsEnabled , settings . value ( " allowFlash " , true ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : JavascriptEnabled , settings . value ( " allowJavaScript " , true ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : JavascriptCanOpenWindows , settings . value ( " allowJavaScriptOpenWindow " , false ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : JavaEnabled , settings . value ( " allowJava " , true ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : DnsPrefetchEnabled , settings . value ( " DNS-Prefetch " , false ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : JavascriptCanAccessClipboard , settings . value ( " allowJavaScriptAccessClipboard " , true ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : LinksIncludedInFocusChain , settings . value ( " IncludeLinkInFocusChain " , false ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : ZoomTextOnly , settings . value ( " zoomTextOnly " , false ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : PrintElementBackgrounds , settings . value ( " PrintElementBackground " , true ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : XSSAuditingEnabled , settings . value ( " XSSAuditing " , false ) . toBool ( ) ) ;
webSettings - > setMaximumPagesInCache ( settings . value ( " maximumCachedPages " , 3 ) . toInt ( ) ) ;
webSettings - > setDefaultTextEncoding ( settings . value ( " DefaultEncoding " , webSettings - > defaultTextEncoding ( ) ) . toString ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : SpatialNavigationEnabled , settings . value ( " SpatialNavigation " , false ) . toBool ( ) ) ;
# if QTWEBKIT_FROM_2_3
webSettings - > setAttribute ( QWebSettings : : CaretBrowsingEnabled , settings . value ( " CaretBrowsing " , false ) . toBool ( ) ) ;
webSettings - > setAttribute ( QWebSettings : : ScrollAnimatorEnabled , settings . value ( " AnimateScrolling " , true ) . toBool ( ) ) ;
# endif
# ifdef USE_WEBGL
2014-03-11 09:12:37 +01:00
webSettings - > setAttribute ( QWebSettings : : WebGLEnabled , true ) ;
webSettings - > setAttribute ( QWebSettings : : AcceleratedCompositingEnabled , true ) ;
2014-03-10 00:47:07 +01:00
# endif
# if QTWEBKIT_FROM_2_2
webSettings - > setAttribute ( QWebSettings : : HyperlinkAuditingEnabled , true ) ;
webSettings - > setAttribute ( QWebSettings : : JavascriptCanCloseWindows , settings . value ( " allowJavaScriptCloseWindow " , false ) . toBool ( ) ) ;
# endif
setWheelScrollLines ( settings . value ( " wheelScrollLines " , wheelScrollLines ( ) ) . toInt ( ) ) ;
2014-09-25 17:37:20 +02:00
const QString userCss = settings . value ( " userStyleSheet " , QString ( ) ) . toString ( ) ;
2014-03-10 00:47:07 +01:00
settings . endGroup ( ) ;
2014-09-25 17:37:20 +02:00
webSettings - > setUserStyleSheetUrl ( userStyleSheet ( userCss ) ) ;
2014-03-10 00:47:07 +01:00
settings . beginGroup ( " Browser-Fonts " ) ;
webSettings - > setFontFamily ( QWebSettings : : StandardFont , settings . value ( " StandardFont " , webSettings - > fontFamily ( QWebSettings : : StandardFont ) ) . toString ( ) ) ;
webSettings - > setFontFamily ( QWebSettings : : CursiveFont , settings . value ( " CursiveFont " , webSettings - > fontFamily ( QWebSettings : : CursiveFont ) ) . toString ( ) ) ;
webSettings - > setFontFamily ( QWebSettings : : FantasyFont , settings . value ( " FantasyFont " , webSettings - > fontFamily ( QWebSettings : : FantasyFont ) ) . toString ( ) ) ;
webSettings - > setFontFamily ( QWebSettings : : FixedFont , settings . value ( " FixedFont " , webSettings - > fontFamily ( QWebSettings : : FixedFont ) ) . toString ( ) ) ;
webSettings - > setFontFamily ( QWebSettings : : SansSerifFont , settings . value ( " SansSerifFont " , webSettings - > fontFamily ( QWebSettings : : SansSerifFont ) ) . toString ( ) ) ;
webSettings - > setFontFamily ( QWebSettings : : SerifFont , settings . value ( " SerifFont " , webSettings - > fontFamily ( QWebSettings : : SerifFont ) ) . toString ( ) ) ;
2014-10-08 22:12:02 +02:00
webSettings - > setFontSize ( QWebSettings : : DefaultFontSize , settings . value ( " DefaultFontSize " , 14 ) . toInt ( ) ) ;
webSettings - > setFontSize ( QWebSettings : : DefaultFixedFontSize , settings . value ( " FixedFontSize " , 12 ) . toInt ( ) ) ;
webSettings - > setFontSize ( QWebSettings : : MinimumFontSize , settings . value ( " MinimumFontSize " , 3 ) . toInt ( ) ) ;
webSettings - > setFontSize ( QWebSettings : : MinimumLogicalFontSize , settings . value ( " MinimumLogicalFontSize " , 5 ) . toInt ( ) ) ;
2014-03-10 00:47:07 +01:00
settings . endGroup ( ) ;
webSettings - > setWebGraphic ( QWebSettings : : DefaultFrameIconGraphic , IconProvider : : emptyWebIcon ( ) . pixmap ( 16 , 16 ) ) ;
webSettings - > setWebGraphic ( QWebSettings : : MissingImageGraphic , QPixmap ( ) ) ;
if ( isPrivate ( ) ) {
webSettings - > setAttribute ( QWebSettings : : PrivateBrowsingEnabled , true ) ;
history ( ) - > setSaving ( false ) ;
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
if ( m_downloadManager ) {
m_downloadManager - > loadSettings ( ) ;
2011-11-06 17:01:23 +01:00
}
2014-03-10 00:47:07 +01:00
qzSettings - > loadSettings ( ) ;
userAgentManager ( ) - > loadSettings ( ) ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : loadTheme ( const QString & name )
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
QString activeThemePath ;
const QStringList themePaths = DataPaths : : allPaths ( DataPaths : : Themes ) ;
foreach ( const QString & path , themePaths ) {
const QString theme = QString ( " %1/%2 " ) . arg ( path , name ) ;
if ( QFile : : exists ( theme + QLatin1String ( " /main.css " ) ) ) {
activeThemePath = theme ;
break ;
}
2011-11-06 17:01:23 +01:00
}
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
if ( activeThemePath . isEmpty ( ) ) {
qWarning ( ) < < " Cannot load theme " < < name ;
activeThemePath = QString ( " %1/%2 " ) . arg ( DataPaths : : path ( DataPaths : : Themes ) , DEFAULT_THEME_NAME ) ;
2011-11-06 17:01:23 +01:00
}
2011-10-21 23:26:34 +02:00
2014-03-10 00:47:07 +01:00
QString qss = QzTools : : readAllFileContents ( activeThemePath + QLatin1String ( " /main.css " ) ) ;
2014-03-09 21:51:42 +01:00
2014-03-10 00:47:07 +01:00
# if defined(Q_OS_MAC)
qss . append ( QzTools : : readAllFileContents ( activeThemePath + QLatin1String ( " /mac.css " ) ) ) ;
2014-04-20 12:28:33 +02:00
# elif defined(Q_OS_UNIX)
qss . append ( QzTools : : readAllFileContents ( activeThemePath + QLatin1String ( " /linux.css " ) ) ) ;
2014-03-10 00:47:07 +01:00
# elif defined(Q_OS_WIN) || defined(Q_OS_OS2)
qss . append ( QzTools : : readAllFileContents ( activeThemePath + QLatin1String ( " /windows.css " ) ) ) ;
# endif
2012-06-15 17:43:19 +02:00
2014-03-10 00:47:07 +01:00
if ( isRightToLeft ( ) ) {
qss . append ( QzTools : : readAllFileContents ( activeThemePath + QLatin1String ( " /rtl.css " ) ) ) ;
2011-11-06 17:01:23 +01:00
}
2014-03-10 00:47:07 +01:00
QString relativePath = QDir : : current ( ) . relativeFilePath ( activeThemePath ) ;
2014-04-19 11:23:25 +02:00
qss . replace ( QzRegExp ( QSL ( " url \\ s* \\ ( \\ s*([^ \\ *: \\ );]+) \\ s* \\ ) " ) , Qt : : CaseSensitive ) , QString ( " url(%1/ \\ 1) " ) . arg ( relativePath ) ) ;
2014-03-10 00:47:07 +01:00
setStyleSheet ( qss ) ;
2011-04-24 22:40:35 +02:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : translateApp ( )
2013-01-19 17:28:12 +01:00
{
2014-09-25 17:37:20 +02:00
QString file = Settings ( ) . value ( QSL ( " Language/language " ) , QLocale : : system ( ) . name ( ) ) . toString ( ) ;
2014-03-10 00:47:07 +01:00
2014-03-27 11:51:31 +01:00
if ( ! file . isEmpty ( ) & & ! file . endsWith ( QL1S ( " .qm " ) ) ) {
file . append ( QL1S ( " .qm " ) ) ;
2013-01-19 17:28:12 +01:00
}
2014-03-27 11:51:31 +01:00
// Either we load default language (with empty file), or we attempt to load xx.qm (xx_yy.qm)
Q_ASSERT ( file . isEmpty ( ) | | file . size ( ) > = 5 ) ;
2014-03-10 19:41:44 +01:00
QString translationPath = DataPaths : : path ( DataPaths : : Translations ) ;
2013-02-05 15:41:10 +01:00
2014-03-10 00:47:07 +01:00
if ( ! file . isEmpty ( ) ) {
const QStringList translationsPaths = DataPaths : : allPaths ( DataPaths : : Translations ) ;
2014-02-22 14:57:22 +01:00
2014-03-10 00:47:07 +01:00
foreach ( const QString & path , translationsPaths ) {
// If "xx_yy" translation doesn't exists, try to use "xx*" translation
2014-03-27 11:51:31 +01:00
// It can only happen when language is chosen from system locale
2014-03-10 00:47:07 +01:00
2014-03-10 19:41:44 +01:00
if ( ! QFile ( QString ( " %1/%2 " ) . arg ( path , file ) ) . exists ( ) ) {
2014-03-10 00:47:07 +01:00
QDir dir ( path ) ;
2014-03-27 11:51:31 +01:00
QString lang = file . left ( 2 ) + QL1S ( " *.qm " ) ;
2014-03-10 00:47:07 +01:00
const QStringList translations = dir . entryList ( QStringList ( lang ) ) ;
// If no translation can be found, default English will be used
file = translations . isEmpty ( ) ? QString ( ) : translations . first ( ) ;
}
2014-03-10 19:41:44 +01:00
if ( ! file . isEmpty ( ) & & QFile ( QString ( " %1/%2 " ) . arg ( path , file ) ) . exists ( ) ) {
translationPath = path ;
2014-03-10 00:47:07 +01:00
break ;
}
2012-06-26 11:49:39 +02:00
}
}
2014-03-27 11:51:31 +01:00
// Load application translation
2014-03-10 00:47:07 +01:00
QTranslator * app = new QTranslator ( this ) ;
2014-03-10 19:41:44 +01:00
app - > load ( file , translationPath ) ;
2012-06-26 11:49:39 +02:00
2014-03-27 11:51:31 +01:00
// Load Qt translation (first try to load from Qt path)
2014-03-10 00:47:07 +01:00
QTranslator * sys = new QTranslator ( this ) ;
2014-03-27 11:51:31 +01:00
sys - > load ( QL1S ( " qt_ " ) + file , QLibraryInfo : : location ( QLibraryInfo : : TranslationsPath ) ) ;
// If there is no translation in Qt path for specified language, try to load it from our path
if ( sys - > isEmpty ( ) ) {
sys - > load ( QL1S ( " qt_ " ) + file , translationPath ) ;
}
2014-02-22 14:57:22 +01:00
2014-03-10 00:47:07 +01:00
m_languageFile = file ;
installTranslator ( app ) ;
installTranslator ( sys ) ;
2012-06-26 11:49:39 +02:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : backupSavedSessions ( )
2012-07-04 17:53:49 +02:00
{
2014-03-10 00:47:07 +01:00
// session.dat - current
// session.dat.old - first backup
// session.dat.old1 - second backup
const QString sessionFile = DataPaths : : currentProfilePath ( ) + QLatin1String ( " /session.dat " ) ;
if ( ! QFile : : exists ( sessionFile ) ) {
return ;
}
if ( QFile : : exists ( sessionFile + QLatin1String ( " .old " ) ) ) {
QFile : : remove ( sessionFile + QLatin1String ( " .old1 " ) ) ;
QFile : : copy ( sessionFile + QLatin1String ( " .old " ) , sessionFile + QLatin1String ( " .old1 " ) ) ;
}
QFile : : remove ( sessionFile + QLatin1String ( " .old " ) ) ;
QFile : : copy ( sessionFile , sessionFile + QLatin1String ( " .old " ) ) ;
2012-07-04 17:53:49 +02:00
}
2014-03-10 00:47:07 +01:00
void MainApplication : : checkDefaultWebBrowser ( )
2012-12-01 07:50:31 +01:00
{
2014-03-10 00:47:07 +01:00
if ( isPortable ( ) ) {
return ;
}
2014-01-01 23:15:50 +01:00
# if defined(Q_OS_WIN) && !defined(Q_OS_OS2)
2014-03-10 00:47:07 +01:00
Settings settings ;
bool checkNow = settings . value ( " Web-Browser-Settings/CheckDefaultBrowser " , DEFAULT_CHECK_DEFAULTBROWSER ) . toBool ( ) ;
if ( ! checkNow ) {
return ;
}
bool checkAgain = true ;
2012-12-01 07:50:31 +01:00
if ( ! associationManager ( ) - > isDefaultForAllCapabilities ( ) ) {
2013-03-16 15:44:06 +01:00
CheckBoxDialog dialog ( QDialogButtonBox : : Yes | QDialogButtonBox : : No , getWindow ( ) ) ;
2013-01-29 20:34:14 +01:00
dialog . setText ( tr ( " QupZilla is not currently your default browser. Would you like to make it your default browser? " ) ) ;
dialog . setCheckBoxText ( tr ( " Always perform this check when starting QupZilla. " ) ) ;
2013-03-16 15:44:06 +01:00
dialog . setDefaultCheckState ( Qt : : Checked ) ;
2013-01-29 20:34:14 +01:00
dialog . setWindowTitle ( tr ( " Default Browser " ) ) ;
2014-03-07 22:54:50 +01:00
dialog . setIcon ( IconProvider : : standardIcon ( QStyle : : SP_MessageBoxWarning ) ) ;
2012-12-01 07:50:31 +01:00
2013-01-29 20:34:14 +01:00
if ( dialog . exec ( ) = = QDialog : : Accepted ) {
2012-12-01 07:50:31 +01:00
associationManager ( ) - > registerAllAssociation ( ) ;
}
2013-01-29 20:34:14 +01:00
2014-03-10 00:47:07 +01:00
checkAgain = dialog . isChecked ( ) ;
2012-12-01 07:50:31 +01:00
}
2014-03-10 00:47:07 +01:00
settings . setValue ( " Web-Browser-Settings/CheckDefaultBrowser " , checkAgain ) ;
2013-01-29 20:34:14 +01:00
# endif
2012-12-01 07:50:31 +01:00
}
2013-01-31 11:53:48 +01:00
QUrl MainApplication : : userStyleSheet ( const QString & filePath ) const
2012-06-26 11:49:39 +02:00
{
2014-03-10 00:47:07 +01:00
QString userCss ;
2013-02-22 11:59:52 +01:00
2014-03-10 00:47:07 +01:00
# if !defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
2013-02-22 11:59:52 +01:00
// Don't grey out selection on losing focus (to prevent graying out found text)
2013-02-24 13:07:14 +01:00
QString highlightColor ;
QString highlightedTextColor ;
# ifdef Q_OS_MAC
highlightColor = QLatin1String ( " #b6d6fc " ) ;
highlightedTextColor = QLatin1String ( " #000 " ) ;
# else
2013-02-22 11:59:52 +01:00
QPalette pal = style ( ) - > standardPalette ( ) ;
2013-02-24 13:07:14 +01:00
highlightColor = pal . color ( QPalette : : Highlight ) . name ( ) ;
highlightedTextColor = pal . color ( QPalette : : HighlightedText ) . name ( ) ;
# endif
2014-03-10 00:47:07 +01:00
userCss + = QString ( " ::selection {background: %1; color: %2;} " ) . arg ( highlightColor , highlightedTextColor ) ;
2013-02-22 11:59:52 +01:00
# endif
2014-03-10 00:47:07 +01:00
userCss + = AdBlockManager : : instance ( ) - > elementHidingRules ( ) ;
userCss + = QzTools : : readAllFileContents ( filePath ) . remove ( QLatin1Char ( ' \n ' ) ) ;
2012-06-26 11:49:39 +02:00
2014-03-10 00:47:07 +01:00
const QString encodedStyle = userCss . toLatin1 ( ) . toBase64 ( ) ;
2013-12-30 13:43:48 +01:00
const QString dataString = QString ( " data:text/css;charset=utf-8;base64,%1 " ) . arg ( encodedStyle ) ;
2012-06-26 11:49:39 +02:00
return QUrl ( dataString ) ;
}
2014-03-10 00:47:07 +01:00
# if defined(Q_OS_WIN) && !defined(Q_OS_OS2)
RegisterQAppAssociation * MainApplication : : associationManager ( )
2011-03-02 16:57:41 +01:00
{
2014-03-10 00:47:07 +01:00
if ( ! m_registerQAppAssociation ) {
QString desc = tr ( " QupZilla is a new, fast and secure open-source WWW browser. QupZilla is licensed under GPL version 3 or (at your option) any later version. It is based on WebKit core and Qt Framework. " ) ;
QString fileIconPath = QApplication : : applicationFilePath ( ) + " ,1 " ;
QString appIconPath = QApplication : : applicationFilePath ( ) + " ,0 " ;
m_registerQAppAssociation = new RegisterQAppAssociation ( " QupZilla " , QApplication : : applicationFilePath ( ) , appIconPath , desc , this ) ;
m_registerQAppAssociation - > addCapability ( " .html " , " QupZilla.HTML " , " HTML File " , fileIconPath , RegisterQAppAssociation : : FileAssociation ) ;
m_registerQAppAssociation - > addCapability ( " .htm " , " QupZilla.HTM " , " HTM File " , fileIconPath , RegisterQAppAssociation : : FileAssociation ) ;
m_registerQAppAssociation - > addCapability ( " http " , " QupZilla.HTTP " , " URL:HyperText Transfer Protocol " , appIconPath , RegisterQAppAssociation : : UrlAssociation ) ;
m_registerQAppAssociation - > addCapability ( " https " , " QupZilla.HTTPS " , " URL:HyperText Transfer Protocol with Privacy " , appIconPath , RegisterQAppAssociation : : UrlAssociation ) ;
m_registerQAppAssociation - > addCapability ( " ftp " , " QupZilla.FTP " , " URL:File Transfer Protocol " , appIconPath , RegisterQAppAssociation : : UrlAssociation ) ;
2011-11-06 17:01:23 +01:00
}
2014-03-10 00:47:07 +01:00
return m_registerQAppAssociation ;
2011-03-02 16:57:41 +01:00
}
2014-03-10 00:47:07 +01:00
# endif
2011-03-02 16:57:41 +01:00
2014-03-10 00:47:07 +01:00
# ifdef Q_OS_MAC
# include <QFileOpenEvent>
2013-03-03 20:18:34 +01:00
2014-03-10 00:47:07 +01:00
bool MainApplication : : event ( QEvent * e )
2012-09-03 13:24:29 +02:00
{
2014-03-10 00:47:07 +01:00
switch ( e - > type ( ) ) {
case QEvent : : FileOpen : {
QString fileName = static_cast < QFileOpenEvent * > ( e ) - > file ( ) ;
addNewTab ( QUrl : : fromLocalFile ( fileName ) ) ;
return true ;
}
break ;
2012-09-03 13:24:29 +02:00
2014-03-10 00:47:07 +01:00
default :
break ;
}
2012-09-03 13:24:29 +02:00
2014-03-10 00:47:07 +01:00
return QtSingleApplication : : event ( e ) ;
2012-09-03 13:24:29 +02:00
}
2014-03-10 00:47:07 +01:00
# endif