application.cpp 20.1 KB
Newer Older
adjam's avatar
adjam committed
1
2
3
4
/* ============================================================
*
* This file is a part of the rekonq project
*
5
* Copyright (C) 2008-2011 by Andrea Diamantini <adjam7 at gmail dot com>
adjam's avatar
adjam committed
6
* Copyright (C) 2009 by Paweł Prażak <pawelprazak at gmail dot com>
7
* Copyright (C) 2009-2011 by Lionel Chauvin <megabigbug@yahoo.fr>
adjam's avatar
adjam committed
8
9
*
*
Andrea Diamantini's avatar
Andrea Diamantini committed
10
11
12
13
14
* 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 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
Andrea Diamantini's avatar
Andrea Diamantini committed
15
* by the membership of KDE e.V.), which shall act as a proxy
Andrea Diamantini's avatar
Andrea Diamantini committed
16
* defined in Section 14 of version 3 of the license.
Andrea Diamantini's avatar
Andrea Diamantini committed
17
*
adjam's avatar
adjam committed
18
19
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
Andrea Diamantini's avatar
Andrea Diamantini committed
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
adjam's avatar
adjam committed
21
22
* GNU General Public License for more details.
*
Andrea Diamantini's avatar
Andrea Diamantini committed
23
24
25
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
adjam's avatar
adjam committed
26
27
28
29
30
* ============================================================ */


// Self Includes
#include "application.h"
31
#include "application.moc"
adjam's avatar
adjam committed
32
33
34
35
36

// Auto Includes
#include "rekonq.h"

// Local Includes
37
#include "adblockmanager.h"
38
#include "bookmarkprovider.h"
39
40
41
#include "filterurljob.h"
#include "historymanager.h"
#include "iconmanager.h"
adjam's avatar
adjam committed
42
#include "mainview.h"
43
#include "mainwindow.h"
44
#include "messagebar.h"
megabigbug's avatar
megabigbug committed
45
#include "opensearchmanager.h"
46
47
#include "sessionmanager.h"
#include "stackedurlbar.h"
Andrea Diamantini's avatar
Andrea Diamantini committed
48
#include "tabbar.h"
49
50
#include "urlbar.h"
#include "webtab.h"
adjam's avatar
adjam committed
51
52
53

// KDE Includes
#include <KCmdLineArgs>
54
#include <KIcon>
55
#include <KMessageBox>
56
#include <KStandardDirs>
57
#include <KStartupInfo>
Andrea Diamantini's avatar
Andrea Diamantini committed
58
#include <ThreadWeaver/Weaver>
59
#include <KAction>
60
#include <KWindowSystem>
61
62
#include <KGlobal>
#include <KCharsets>
adjam's avatar
adjam committed
63

64
65
// Qt Includes
#include <QVBoxLayout>
adjam's avatar
adjam committed
66

67

68
using namespace ThreadWeaver;
adjam's avatar
adjam committed
69

70

adjam's avatar
adjam committed
71
Application::Application()
Andrea Diamantini's avatar
Andrea Diamantini committed
72
        : KUniqueApplication()
73
        , _privateBrowsingAction(0)
adjam's avatar
adjam committed
74
{
Andrea Diamantini's avatar
Andrea Diamantini committed
75
76
    connect(Weaver::instance(), SIGNAL(jobDone(ThreadWeaver::Job*)),
            this, SLOT(loadResolvedUrl(ThreadWeaver::Job*)));
77
78
79
80

    _privateBrowsingAction = new KAction(KIcon("view-media-artist"), i18n("Private &Browsing"), this);
    _privateBrowsingAction->setCheckable(true);
    connect(_privateBrowsingAction, SIGNAL(triggered(bool)), this, SLOT(setPrivateBrowsingMode(bool)));
adjam's avatar
adjam committed
81
82
83
84
85
}


Application::~Application()
{
Andrea Diamantini's avatar
Andrea Diamantini committed
86
87
    // ok, we are closing well.
    // Don't recover on next load..
88
    ReKonfig::setRecoverOnCrash(0);
Andrea Diamantini's avatar
Andrea Diamantini committed
89
    saveConfiguration();
Andrea Diamantini's avatar
Andrea Diamantini committed
90

91
    Q_FOREACH(QWeakPointer<MainWindow> window, m_mainWindows)
92
93
94
95
    {
        delete window.data();
        window.clear();
    }
96
97
    
    delete bookmarkProvider();
adjam's avatar
adjam committed
98
99
100
101
102
}


int Application::newInstance()
{
Andrea Diamantini's avatar
Andrea Diamantini committed
103
    KCmdLineArgs::setCwd(QDir::currentPath().toUtf8());
adjam's avatar
adjam committed
104
    KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
105

106
    // not that easy, indeed
107
    // We have to consider 3 variables here:
108
109
110
111
    // 1) Is first load?
    // 2) Are there arguments?
    // 3) Is rekonq recovering from crash?
    // so, we have 8 possible cases...
112
    bool isFirstLoad = m_mainWindows.isEmpty();
113
    bool areThereArguments = (args->count() > 0);
114
    bool isRekonqCrashed = ReKonfig::recoverOnCrash();
115

116
117
118
    kDebug() << "is first load? " << isFirstLoad;
    kDebug() << "are there arguments? " << areThereArguments;
    kDebug() << "is rekonq crashed? " << isRekonqCrashed;
119

120
    int exitValue = 1 * isFirstLoad + 2 * areThereArguments + 4 * isRekonqCrashed;
121

Andrea Diamantini's avatar
Andrea Diamantini committed
122
123
124
125
126
127
128
129
130
131
132
    if (isRekonqCrashed && isFirstLoad)
    {
        loadUrl(KUrl("about:closedTabs"));
        MessageBar *msgBar = new MessageBar(i18n("It seems rekonq was not closed properly. Do you want "
                                            "to restore the last saved session?")
                                            , mainWindow()->currentTab()
                                            , QMessageBox::Warning
                                            , MessageBar::Yes | MessageBar::No);

        connect(msgBar, SIGNAL(accepted()), sessionManager(), SLOT(restoreSession()));
        mainWindow()->currentTab()->insertBar(msgBar);
133
    }
134

Andrea Diamantini's avatar
Andrea Diamantini committed
135
136
    if (areThereArguments)
    {
137
        KUrl::List urlList;
Andrea Diamantini's avatar
Andrea Diamantini committed
138
        for (int i = 0; i < args->count(); ++i)
139
140
141
142
143
        {
            const KUrl u = args->url(i);
            if (u.isLocalFile() && QFile::exists(u.toLocalFile())) // "rekonq somefile.html" case
                urlList += u;
            else
Andrea Diamantini's avatar
Andrea Diamantini committed
144
                urlList += KUrl(args->arg(i));   // "rekonq kde.org" || "rekonq kde:kdialog" case
145
        }
146

Andrea Diamantini's avatar
Andrea Diamantini committed
147
148
        if (isFirstLoad && !isRekonqCrashed)
        {
149
150
151
152
153
154
            // No windows in the current desktop? No windows at all?
            // Create a new one and load there sites...
            loadUrl(urlList.at(0), Rekonq::CurrentTab);
        }
        else
        {
Andrea Diamantini's avatar
Andrea Diamantini committed
155
156
            if (ReKonfig::openTabNoWindow())
            {
157
                loadUrl(urlList.at(0), Rekonq::NewTab);
Andrea Diamantini's avatar
Andrea Diamantini committed
158
                if (!mainWindow()->isActiveWindow())
159
160
                    KWindowSystem::demandAttention(mainWindow()->winId(), true);
            }
161
162
            else
                loadUrl(urlList.at(0), Rekonq::NewWindow);
163
        }
Andrea Diamantini's avatar
Andrea Diamantini committed
164

165
        for (int i = 1; i < urlList.count(); ++i)
Andrea Diamantini's avatar
Andrea Diamantini committed
166
            loadUrl(urlList.at(i), Rekonq::NewTab);
167

168
169
        KStartupInfo::appStarted();

Andrea Diamantini's avatar
Andrea Diamantini committed
170
171
172
    }
    else if (!isRekonqCrashed)
    {
173

Andrea Diamantini's avatar
Andrea Diamantini committed
174
        if (isFirstLoad)  // we are starting rekonq, for the first time with no args: use startup behaviour
Andrea Diamantini's avatar
Andrea Diamantini committed
175
        {
Andrea Diamantini's avatar
Andrea Diamantini committed
176
            switch (ReKonfig::startupBehaviour())
Andrea Diamantini's avatar
Andrea Diamantini committed
177
            {
Andrea Diamantini's avatar
Andrea Diamantini committed
178
179
180
181
182
183
184
            case 0: // open home page
                mainWindow()->homePage();
                break;
            case 1: // open new tab page
                loadUrl(KUrl("about:home"));
                break;
            case 2: // restore session
185
186
                sessionManager()->restoreSession();
                kDebug() << "session restored following settings";
187
                break;
Andrea Diamantini's avatar
Andrea Diamantini committed
188
189
190
191
            default:
                mainWindow()->homePage();
                break;
            }
Andrea Diamantini's avatar
Andrea Diamantini committed
192
193
194
        }
        else    // rekonq has just been started. Just open a new window
        {
195
196
197
198
199
200
201
202
203
204
205
            switch (ReKonfig::newTabsBehaviour())
            {
            case 0: // new tab page
                loadUrl(KUrl("about:home") , Rekonq::NewWindow);
                break;
            case 1: // blank page
                loadUrl(KUrl("about:blank") , Rekonq::NewWindow);
                break;
            case 2: // homepage
                loadUrl(KUrl(ReKonfig::homePage()) , Rekonq::NewWindow);
                break;
Andrea Diamantini's avatar
Andrea Diamantini committed
206
            default:
207
208
                loadUrl(KUrl("about:blank") , Rekonq::NewWindow);
                break;
Andrea Diamantini's avatar
Andrea Diamantini committed
209
            }
210

211
        }
Andrea Diamantini's avatar
Andrea Diamantini committed
212
    }
213

Andrea Diamantini's avatar
Andrea Diamantini committed
214
    if (isFirstLoad)
215
    {
216
217
        // give me some time to do the other things..
        QTimer::singleShot(100, this, SLOT(postLaunch()));
adjam's avatar
adjam committed
218
    }
219

220
    return exitValue;
adjam's avatar
adjam committed
221
222
223
224
225
}


Application *Application::instance()
{
Andrea Diamantini's avatar
Andrea Diamantini committed
226
    return (qobject_cast<Application *>(QCoreApplication::instance()));
adjam's avatar
adjam committed
227
228
229
230
}


void Application::postLaunch()
Andrea Diamantini's avatar
Andrea Diamantini committed
231
{
Andrea Diamantini's avatar
Andrea Diamantini committed
232
233
234
    // updating rekonq configuration
    updateConfiguration();

235
    setWindowIcon(KIcon("rekonq"));
Andrea Diamantini's avatar
Andrea Diamantini committed
236

237
238
    historyManager();
    sessionManager()->setSessionManagementEnabled(true);
Andrea Diamantini's avatar
Andrea Diamantini committed
239

240
    // bookmarks loading
241
242
    connect(bookmarkProvider(), SIGNAL(openUrl(const KUrl&, const Rekonq::OpenType&)),
            instance(), SLOT(loadUrl(const KUrl&, const Rekonq::OpenType&)));
Andrea Diamantini's avatar
Andrea Diamantini committed
243

Andrea Diamantini's avatar
Andrea Diamantini committed
244
    // crash recovering
245
    ReKonfig::setRecoverOnCrash(ReKonfig::recoverOnCrash() + 1);
Andrea Diamantini's avatar
Andrea Diamantini committed
246
    saveConfiguration();
adjam's avatar
adjam committed
247
248
249
}


Andrea Diamantini's avatar
Andrea Diamantini committed
250
void Application::saveConfiguration() const
adjam's avatar
adjam committed
251
252
253
254
255
256
257
{
    ReKonfig::self()->writeConfig();
}


MainWindow *Application::mainWindow()
{
Andrea Diamantini's avatar
Andrea Diamantini committed
258
    if (m_mainWindows.isEmpty())
259
        return newMainWindow();
Andrea Diamantini's avatar
Andrea Diamantini committed
260

261
    MainWindow *active = qobject_cast<MainWindow*>(QApplication::activeWindow());
Andrea Diamantini's avatar
Andrea Diamantini committed
262
263

    if (!active)
264
    {
265
        return m_mainWindows.at(0).data();
266
267
    }
    return active;
adjam's avatar
adjam committed
268
269
270
271
272
}


HistoryManager *Application::historyManager()
{
273
    if (m_historyManager.isNull())
adjam's avatar
adjam committed
274
    {
275
        m_historyManager = new HistoryManager(instance());
adjam's avatar
adjam committed
276
    }
277
    return m_historyManager.data();
adjam's avatar
adjam committed
278
279
280
281
282
}


BookmarkProvider *Application::bookmarkProvider()
{
283
    if (m_bookmarkProvider.isNull())
adjam's avatar
adjam committed
284
    {
285
        m_bookmarkProvider = new BookmarkProvider(instance());
adjam's avatar
adjam committed
286
    }
287
    return m_bookmarkProvider.data();
adjam's avatar
adjam committed
288
289
290
}


291
292
SessionManager *Application::sessionManager()
{
293
    if (m_sessionManager.isNull())
294
    {
295
        m_sessionManager = new SessionManager(instance());
296
    }
297
    return m_sessionManager.data();
298
299
300
}


megabigbug's avatar
megabigbug committed
301
302
OpenSearchManager *Application::opensearchManager()
{
303
    if (m_opensearchManager.isNull())
megabigbug's avatar
megabigbug committed
304
    {
305
        m_opensearchManager = new OpenSearchManager(instance());
megabigbug's avatar
megabigbug committed
306
    }
307
    return m_opensearchManager.data();
megabigbug's avatar
megabigbug committed
308
309
310
}


311
IconManager *Application::iconManager()
adjam's avatar
adjam committed
312
{
313
    if (m_iconManager.isNull())
adjam's avatar
adjam committed
314
    {
315
        m_iconManager = new IconManager(instance());
adjam's avatar
adjam committed
316
    }
317
    return m_iconManager.data();
adjam's avatar
adjam committed
318
}
319
320


321
322
323
324
325
void Application::loadUrl(const KUrl& url, const Rekonq::OpenType& type)
{
    if (url.isEmpty())
        return;

Andrea Diamantini's avatar
Andrea Diamantini committed
326
    if (!url.isValid())
327
    {
328
        KMessageBox::error(0, i18n("Malformed URL:\n%1", url.url(KUrl::RemoveTrailingSlash)));
329
330
        return;
    }
331

332
333
334
    // first, create the webview(s) to not let hangs UI..
    WebTab *tab = 0;
    MainWindow *w = 0;
Andrea Diamantini's avatar
Andrea Diamantini committed
335
    w = (type == Rekonq::NewWindow)
336
337
        ? newMainWindow()
        : mainWindow();
Andrea Diamantini's avatar
Andrea Diamantini committed
338
339

    switch (type)
340
    {
Andrea Diamantini's avatar
Andrea Diamantini committed
341
    case Rekonq::NewTab:
Andrea Diamantini's avatar
Andrea Diamantini committed
342
343
        if (ReKonfig::openTabNoWindow())
            tab = w->mainView()->newWebTab(!ReKonfig::openTabsBack());
344
345
346
347
348
        else
        {
            w = newMainWindow();
            tab = w->mainView()->currentWebTab();
        }
349
        break;
Andrea Diamantini's avatar
Andrea Diamantini committed
350
    case Rekonq::NewFocusedTab:
351
352
353
        tab = w->mainView()->newWebTab(true);
        break;
    case Rekonq::NewBackTab:
354
        tab = w->mainView()->newWebTab(false);
355
356
357
358
359
360
        break;
    case Rekonq::NewWindow:
    case Rekonq::CurrentTab:
        tab = w->mainView()->currentWebTab();
        break;
    };
Andrea Diamantini's avatar
Andrea Diamantini committed
361

Andrea Diamantini's avatar
Andrea Diamantini committed
362
363

    // rapidly show first loading url..
364
    int tabIndex = w->mainView()->indexOf(tab);
Andrea Diamantini's avatar
Andrea Diamantini committed
365
    Q_ASSERT(tabIndex != -1);
366
    UrlBar *barForTab = qobject_cast<UrlBar *>(w->mainView()->widgetBar()->widget(tabIndex));
367
    barForTab->activateSuggestions(false);
368
    barForTab->setQUrl(url);
369

370
    WebView *view = tab->view();
Andrea Diamantini's avatar
Andrea Diamantini committed
371

372
373
374
375
376
    if (view)
    {
        FilterUrlJob *job = new FilterUrlJob(view, url.pathOrUrl(), this);
        Weaver::instance()->enqueue(job);
    }
377
}
378
379


380
MainWindow *Application::newMainWindow(bool withTab)
381
382
{
    MainWindow *w = new MainWindow();
Andrea Diamantini's avatar
Andrea Diamantini committed
383
384

    if (withTab)
385
        w->mainView()->newWebTab();    // remember using newWebTab and NOT newTab here!!
Andrea Diamantini's avatar
Andrea Diamantini committed
386

387
388
    m_mainWindows.prepend(w);
    w->show();
Andrea Diamantini's avatar
Andrea Diamantini committed
389

390
391
392
    return w;
}

393

394
395
void Application::removeMainWindow(MainWindow *window)
{
396
    m_mainWindows.removeOne(window);
397
398
399
}


400
401
402
403
MainWindowList Application::mainWindowList()
{
    return m_mainWindows;
}
Andrea Diamantini's avatar
Andrea Diamantini committed
404
405
406
407


AdBlockManager *Application::adblockManager()
{
408
    if (m_adblockManager.isNull())
Andrea Diamantini's avatar
Andrea Diamantini committed
409
    {
410
        m_adblockManager = new AdBlockManager(instance());
Andrea Diamantini's avatar
Andrea Diamantini committed
411
    }
412
    return m_adblockManager.data();
413
}
Andrea Diamantini's avatar
STEP 1:    
Andrea Diamantini committed
414
415


Andrea Diamantini's avatar
Andrea Diamantini committed
416
void Application::loadResolvedUrl(ThreadWeaver::Job *job)
Andrea Diamantini's avatar
STEP 1:    
Andrea Diamantini committed
417
{
Andrea Diamantini's avatar
Andrea Diamantini committed
418
419
420
    FilterUrlJob *threadedJob = static_cast<FilterUrlJob *>(job);
    KUrl url = threadedJob->url();
    WebView *view = threadedJob->view();
Andrea Diamantini's avatar
Andrea Diamantini committed
421

Andrea Diamantini's avatar
Andrea Diamantini committed
422
423
    if (view)
    {
Andrea Diamantini's avatar
Andrea Diamantini committed
424
        view->load(url);
Andrea Diamantini's avatar
Andrea Diamantini committed
425
    }
426

427
428
    // Bye and thanks :)
    delete threadedJob;
Andrea Diamantini's avatar
STEP 1:    
Andrea Diamantini committed
429
}
430
431
432
433


void Application::newWindow()
{
Andrea Diamantini's avatar
Andrea Diamantini committed
434
    loadUrl(KUrl("about:home"), Rekonq::NewWindow);
Andrea Diamantini's avatar
Andrea Diamantini committed
435
    mainWindow()->mainView()->currentUrlBar()->setFocus();
436
}
Andrea Diamantini's avatar
Andrea Diamantini committed
437
438
439
440
441


void Application::updateConfiguration()
{
    // ============== Tabs ==================
Andrea Diamantini's avatar
Andrea Diamantini committed
442
443
444
445
446
    bool b = ReKonfig::closeTabSelectPrevious();
    Q_FOREACH(const QWeakPointer<MainWindow> &w, m_mainWindows)
    {
        MainView *mv = w.data()->mainView();
        mv->updateTabBar();
447

Andrea Diamantini's avatar
Andrea Diamantini committed
448
449
450
451
452
        if (b)
            mv->tabBar()->setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
        else
            mv->tabBar()->setSelectionBehaviorOnRemove(QTabBar::SelectRightTab);
    }
Andrea Diamantini's avatar
Andrea Diamantini committed
453

454
455
456
    QWebSettings *defaultSettings = QWebSettings::globalSettings();

    // =========== Fonts ==============
Andrea Diamantini's avatar
Andrea Diamantini committed
457
458
459
460
    defaultSettings->setFontFamily(QWebSettings::StandardFont, ReKonfig::standardFontFamily());
    defaultSettings->setFontFamily(QWebSettings::FixedFont, ReKonfig::fixedFontFamily());
    defaultSettings->setFontFamily(QWebSettings::SerifFont, ReKonfig::serifFontFamily());
    defaultSettings->setFontFamily(QWebSettings::SansSerifFont, ReKonfig::sansSerifFontFamily());
Andrea Diamantini's avatar
Andrea Diamantini committed
461
462
463
    defaultSettings->setFontFamily(QWebSettings::CursiveFont, ReKonfig::cursiveFontFamily());
    defaultSettings->setFontFamily(QWebSettings::FantasyFont, ReKonfig::fantasyFontFamily());

464
    // compute font size
Andrea Diamantini's avatar
Andrea Diamantini committed
465
466
    // (I have to admit I know nothing about these DPI questions..: copied from kwebkitpart, as someone suggested)
    // font size in pixels =  font size in inches × screen dpi
467
    int defaultFontSize = ReKonfig::defaultFontSize();
Andrea Diamantini's avatar
Andrea Diamantini committed
468
    int minimumFontSize = ReKonfig::minFontSize();
469

470
471
    int logDpiY = mainWindow()->currentTab()->view()->logicalDpiY();
    kDebug() << "Logical Dot per Inch Y: " << logDpiY;
472

473
    float toPix = (logDpiY < 96.0)
Andrea Diamantini's avatar
Andrea Diamantini committed
474
475
                  ? 96.0 / 72.0
                  : logDpiY / 72.0 ;
megabigbug's avatar
megabigbug committed
476

Andrea Diamantini's avatar
Andrea Diamantini committed
477
478
    defaultSettings->setFontSize(QWebSettings::DefaultFontSize, qRound(defaultFontSize * toPix));
    defaultSettings->setFontSize(QWebSettings::MinimumFontSize, qRound(minimumFontSize * toPix));
Andrea Diamantini's avatar
Andrea Diamantini committed
479

480
481
482
    // encodings
    QString enc = ReKonfig::defaultEncoding();
    defaultSettings->setDefaultTextEncoding(enc);
483

Andrea Diamantini's avatar
Andrea Diamantini committed
484
485
486
487
488
489
490
491
492
493
    // ================ WebKit ============================
    defaultSettings->setAttribute(QWebSettings::AutoLoadImages, ReKonfig::autoLoadImages());
    defaultSettings->setAttribute(QWebSettings::DnsPrefetchEnabled, ReKonfig::dnsPrefetch());
    defaultSettings->setAttribute(QWebSettings::JavascriptEnabled, ReKonfig::javascriptEnabled());
    defaultSettings->setAttribute(QWebSettings::JavaEnabled, ReKonfig::javaEnabled());
    defaultSettings->setAttribute(QWebSettings::JavascriptCanOpenWindows, ReKonfig::javascriptCanOpenWindows());
    defaultSettings->setAttribute(QWebSettings::JavascriptCanAccessClipboard, ReKonfig::javascriptCanAccessClipboard());
    defaultSettings->setAttribute(QWebSettings::LinksIncludedInFocusChain, ReKonfig::linksIncludedInFocusChain());
    defaultSettings->setAttribute(QWebSettings::ZoomTextOnly, ReKonfig::zoomTextOnly());
    defaultSettings->setAttribute(QWebSettings::PrintElementBackgrounds, ReKonfig::printElementBackgrounds());
Andrea Diamantini's avatar
Andrea Diamantini committed
494
495

    if (ReKonfig::pluginsEnabled() == 2)
Andrea Diamantini's avatar
Andrea Diamantini committed
496
497
498
499
        defaultSettings->setAttribute(QWebSettings::PluginsEnabled, false);
    else
        defaultSettings->setAttribute(QWebSettings::PluginsEnabled, true);

500
501
    // Enabling WebKit "Page Cache" feature: http://webkit.org/blog/427/webkit-page-cache-i-the-basics/
    defaultSettings->setMaximumPagesInCache(3);
502

Andrea Diamantini's avatar
Andrea Diamantini committed
503
504
505
506
    // ===== HTML 5 features WebKit support ======
    defaultSettings->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, ReKonfig::offlineStorageDatabaseEnabled());
    defaultSettings->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, ReKonfig::offlineWebApplicationCacheEnabled());
    defaultSettings->setAttribute(QWebSettings::LocalStorageEnabled, ReKonfig::localStorageEnabled());
Andrea Diamantini's avatar
Andrea Diamantini committed
507
    if (ReKonfig::localStorageEnabled())
Andrea Diamantini's avatar
Andrea Diamantini committed
508
509
510
511
512
513
514
515
516
    {
        QString path = KStandardDirs::locateLocal("cache", QString("WebkitLocalStorage/rekonq"), true);
        path.remove("rekonq");
        QWebSettings::setOfflineStoragePath(path);
        QWebSettings::setOfflineStorageDefaultQuota(50000);
    }

    // Applies user defined CSS to all open webpages. If there no longer is a
    // user defined CSS removes it from all open webpages.
517
    if (!ReKonfig::userCSS().isEmpty())
Andrea Diamantini's avatar
Andrea Diamantini committed
518
519
520
        defaultSettings->setUserStyleSheetUrl(ReKonfig::userCSS());

    // ====== load Settings on main classes
521
522
    historyManager()->loadSettings();
    adblockManager()->loadSettings();
Andrea Diamantini's avatar
Andrea Diamantini committed
523
    if (!ReKonfig::useFavicon())
524
525
526
        mainWindow()->setWindowIcon(KIcon("rekonq"));
    else
        mainWindow()->changeWindowIcon(mainWindow()->mainView()->currentIndex());
Andrea Diamantini's avatar
Andrea Diamantini committed
527

528
    // hovering unfocused tabs options
Andrea Diamantini's avatar
Andrea Diamantini committed
529
    switch (ReKonfig::hoveringTabOption())
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
    {
    case 0: // tab previews
    case 3: // nothing
        for (int i = 0; i < mainWindow()->mainView()->tabBar()->count(); i++)
        {
            mainWindow()->mainView()->tabBar()->setTabToolTip(i, "");
        }
        break;

    case 1: // title previews
        for (int i = 0; i < mainWindow()->mainView()->tabBar()->count(); i++)
        {
            mainWindow()->mainView()->tabBar()->setTabToolTip(i, mainWindow()->mainView()->tabText(i).remove('&'));
        }
        break;

    case 2: // url previews
        for (int i = 0; i < mainWindow()->mainView()->tabBar()->count(); i++)
        {
            mainWindow()->mainView()->tabBar()->setTabToolTip(i, mainWindow()->mainView()->webTab(i)->url().toMimeDataString());
        }
        break;

    default: // non extant case
        break;
    }

Andrea Diamantini's avatar
Andrea Diamantini committed
557
558
    defaultSettings = 0;
}
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614


void Application::addDownload(const QString &srcUrl, const QString &destUrl)
{
    QWebSettings *globalSettings = QWebSettings::globalSettings();
    if (globalSettings->testAttribute(QWebSettings::PrivateBrowsingEnabled))
        return;
    QString downloadFilePath = KStandardDirs::locateLocal("appdata" , "downloads");
    QFile downloadFile(downloadFilePath);
    if (!downloadFile.open(QFile::WriteOnly | QFile::Append))
    {
        kDebug() << "Unable to open download file (WRITE mode)..";
        return;
    }
    QDataStream out(&downloadFile);
    out << srcUrl;
    out << destUrl;
    out << QDateTime::currentDateTime();
    downloadFile.close();
}


DownloadList Application::downloads()
{
    DownloadList list;

    QString downloadFilePath = KStandardDirs::locateLocal("appdata" , "downloads");
    QFile downloadFile(downloadFilePath);
    if (!downloadFile.open(QFile::ReadOnly))
    {
        kDebug() << "Unable to open download file (READ mode)..";
        return list;
    }

    QDataStream in(&downloadFile);
    while (!in.atEnd())
    {
        QString srcUrl;
        in >> srcUrl;
        QString destUrl;
        in >> destUrl;
        QDateTime dt;
        in >> dt;
        DownloadItem item(srcUrl, destUrl, dt);
        list << item;
    }
    return list;
}


bool Application::clearDownloadsHistory()
{
    QString downloadFilePath = KStandardDirs::locateLocal("appdata" , "downloads");
    QFile downloadFile(downloadFilePath);
    return downloadFile.remove();
}
615
616
617
618
619
620
621


void Application::setPrivateBrowsingMode(bool b)
{
// NOTE
// to let work nicely Private Browsing, we need the following:
// - enable WebKit Private Browsing mode :)
Andrea Diamantini's avatar
Andrea Diamantini committed
622
// - treat all cookies as session cookies
623
624
625
626
627
628
629
//  (so that they do not get saved to a persistent storage). Available from KDE SC 4.5.72, see BUG: 250122
// - favicons (fixed in rekonq 0.5.87)
// - save actual session (to restore it when Private Mode is closed) and stop storing it
// - disable history saving

    QWebSettings *settings = QWebSettings::globalSettings();
    bool isJustEnabled = settings->testAttribute(QWebSettings::PrivateBrowsingEnabled);
Andrea Diamantini's avatar
Andrea Diamantini committed
630
    if (isJustEnabled == b)
631
        return;     // uhm... something goes wrong...
632

633
634
635
636
    if (b)
    {
        QString caption = i18n("Are you sure you want to turn on private browsing?");
        QString text = i18n("<b>%1</b>"
Andrius Štikonas's avatar
Andrius Štikonas committed
637
                            "<p>rekonq will save your current tabs for when you'll stop private browsing the net.</p>", caption);
638

Andrea Diamantini's avatar
Andrea Diamantini committed
639
        int button = KMessageBox::warningContinueCancel(mainWindow(), text, caption, KStandardGuiItem::cont(), KStandardGuiItem::cancel(), i18n("don't ask again"));
640
641
        if (button != KMessageBox::Continue)
            return;
642

643
644
        settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, true);
        _privateBrowsingAction->setChecked(true);
645

646
647
648
649
        Q_FOREACH(const QWeakPointer<MainWindow> &w, m_mainWindows)
        {
            w.data()->close();
        }
Andrea Diamantini's avatar
Andrea Diamantini committed
650
        loadUrl(KUrl("about:home"), Rekonq::NewWindow);
651
652
653
654
655
656
657
658
659
660
    }
    else
    {
        Q_FOREACH(const QWeakPointer<MainWindow> &w, m_mainWindows)
        {
            w.data()->close();
        }

        settings->setAttribute(QWebSettings::PrivateBrowsingEnabled, false);
        _privateBrowsingAction->setChecked(false);
661

Andrea Diamantini's avatar
Andrea Diamantini committed
662
663
664
        loadUrl(KUrl("about:blank"), Rekonq::NewWindow);
        if (!sessionManager()->restoreSession())
            loadUrl(KUrl("about:home"), Rekonq::NewWindow);
665
666
    }
}