WBBrowserWindow.cpp 16.9 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25
 */
26 27


Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "WBBrowserWindow.h"

#include <QtGui>
#include <QtWebKit>
75
#include <QDesktopWidget>
Claudio Valerio's avatar
Claudio Valerio committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

#include "core/UBSettings.h"
#include "core/UBSetting.h"
#include "core/UBApplication.h"

#include "network/UBNetworkAccessManager.h"
#include "network/UBAutoSaver.h"

#include "gui/UBMainWindow.h"

#include "pdf/UBWebPluginPDFWidget.h"


#include "WBChaseWidget.h"
#include "WBDownloadManager.h"
#include "WBHistory.h"
#include "WBTabWidget.h"
#include "WBToolBarSearch.h"
#include "ui_passworddialog.h"
#include "WBWebView.h"

97
#include "core/memcheck.h"
Claudio Valerio's avatar
Claudio Valerio committed
98 99 100 101 102

WBDownloadManager *WBBrowserWindow::sDownloadManager = 0;
WBHistoryManager *WBBrowserWindow::sHistoryManager = 0;


103
WBBrowserWindow::WBBrowserWindow(QWidget *parent, Ui::MainWindow* uniboardMainWindow)
Claudio Valerio's avatar
Claudio Valerio committed
104
        : QWidget(parent)
105
        , mWebToolBar(0)
Claudio Valerio's avatar
Claudio Valerio committed
106 107 108 109 110 111 112 113 114 115
        , mSearchToolBar(0)
        , mTabWidget(new WBTabWidget(this))
        , mSearchAction(0)
        , mUniboardMainWindow(uniboardMainWindow)
{
    QWebSettings *defaultSettings = QWebSettings::globalSettings();
    defaultSettings->setAttribute(QWebSettings::JavascriptEnabled, true);
    defaultSettings->setAttribute(QWebSettings::PluginsEnabled, true);

    setupMenu();
116 117
    setupToolBar();

Claudio Valerio's avatar
Claudio Valerio committed
118 119 120 121 122 123 124 125 126

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setSpacing(0);
    layout->setMargin(0);

    layout->addWidget(mTabWidget);

    this->setLayout(layout);

127
    connect(mTabWidget, SIGNAL(loadPage(const QString &)), this, SLOT(loadPage(const QString &)));
Claudio Valerio's avatar
Claudio Valerio committed
128 129


130
    connect(mTabWidget, SIGNAL(setCurrentTitle(const QString &)), this, SLOT(slotUpdateWindowTitle(const QString &)));
Claudio Valerio's avatar
Claudio Valerio committed
131

132 133
    connect(mTabWidget, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));

Claudio Valerio's avatar
Claudio Valerio committed
134 135 136

    connect(mTabWidget, SIGNAL(loadFinished(bool)), this, SIGNAL(activeViewPageChanged()));

137
    connect(mTabWidget, SIGNAL(geometryChangeRequested(const QRect &)), this, SLOT(geometryChangeRequested(const QRect &)));
Claudio Valerio's avatar
Claudio Valerio committed
138 139 140 141 142 143 144 145 146 147 148 149 150

    slotUpdateWindowTitle();


    mTabWidget->newTab();

    connect(mTabWidget, SIGNAL(currentChanged(int)), this, SLOT(tabCurrentChanged(int)));

}


WBBrowserWindow::~WBBrowserWindow()
{
151 152 153 154 155
    if(mTabWidget){
        delete mTabWidget;
        mTabWidget = NULL;
    }

156
    //Explanation mSearchToolBar has a parent so it's automatically freed
Claudio Valerio's avatar
Claudio Valerio committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}


UBCookieJar *WBBrowserWindow::cookieJar()
{
    return (UBCookieJar*)UBNetworkAccessManager::defaultAccessManager()->cookieJar();
}


WBDownloadManager *WBBrowserWindow::downloadManager()
{
    if (!sDownloadManager) {
        sDownloadManager = new WBDownloadManager(UBApplication::mainWindow);
    }
    return sDownloadManager;
}


WBHistoryManager *WBBrowserWindow::historyManager()
{
    if (!sHistoryManager) {
        sHistoryManager = new WBHistoryManager();
        QWebHistoryInterface::setDefaultInterface(sHistoryManager);
    }
    return sHistoryManager;
}


QSize WBBrowserWindow::sizeHint() const
{
187
    QRect desktopRect = QApplication::desktop()->screenGeometry(UBApplication::controlScreenIndex());
Claudio Valerio's avatar
Claudio Valerio committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    QSize size = desktopRect.size() * qreal(0.9);
    return size;
}


void WBBrowserWindow::setupMenu()
{
    new QShortcut(QKeySequence(Qt::Key_F6), this, SLOT(slotSwapFocus()));
}


void WBBrowserWindow::setupToolBar()
{
    mWebToolBar = mUniboardMainWindow->webToolBar;

    mTabWidget->addWebAction(mUniboardMainWindow->actionWebBack, QWebPage::Back);
    mTabWidget->addWebAction(mUniboardMainWindow->actionWebForward, QWebPage::Forward);
    mTabWidget->addWebAction(mUniboardMainWindow->actionWebReload, QWebPage::Reload);
    mTabWidget->addWebAction(mUniboardMainWindow->actionStopLoading, QWebPage::Stop);

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    mHistoryBackMenu = new QMenu(this);
    connect(mHistoryBackMenu, SIGNAL(aboutToShow()),this, SLOT(aboutToShowBackMenu()));
    connect(mHistoryBackMenu, SIGNAL(triggered(QAction *)), this, SLOT(openActionUrl(QAction *)));

    foreach (QWidget* menuWidget,  mUniboardMainWindow->actionWebBack->associatedWidgets())
    {
        QToolButton *tb = qobject_cast<QToolButton*>(menuWidget);

        if (tb && !tb->menu())
        {
            tb->setMenu(mHistoryBackMenu);
            tb->setStyleSheet("QToolButton::menu-indicator { subcontrol-position: bottom left; }");
        }
    }

    mHistoryForwardMenu = new QMenu(this);
    connect(mHistoryForwardMenu, SIGNAL(aboutToShow()), this, SLOT(aboutToShowForwardMenu()));
    connect(mHistoryForwardMenu, SIGNAL(triggered(QAction *)), this, SLOT(openActionUrl(QAction *)));
226

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
227 228 229 230 231 232 233 234 235 236 237
    foreach (QWidget* menuWidget,  mUniboardMainWindow->actionWebForward->associatedWidgets())
    {
        QToolButton *tb = qobject_cast<QToolButton*>(menuWidget);

        if (tb && !tb->menu())
        {
            tb->setMenu(mHistoryForwardMenu);
            tb->setStyleSheet("QToolButton { padding-right: 8px; }");
        }
    }

Claudio Valerio's avatar
Claudio Valerio committed
238 239 240 241 242 243 244 245 246
    mWebToolBar->insertWidget(mUniboardMainWindow->actionWebBigger, mTabWidget->lineEditStack());

    mSearchToolBar = new WBToolbarSearch(mWebToolBar);

    mSearchAction = mWebToolBar->insertWidget(mUniboardMainWindow->actionWebBigger, mSearchToolBar);

    connect(mSearchToolBar, SIGNAL(search(const QUrl&)), SLOT(loadUrl(const QUrl&)));

    mChaseWidget = new WBChaseWidget(this);
Ivan Ilin's avatar
Ivan Ilin committed
247
    mWebToolBar->insertWidget(mUniboardMainWindow->actionWebBigger, mChaseWidget);
Claudio Valerio's avatar
Claudio Valerio committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    mWebToolBar->insertSeparator(mUniboardMainWindow->actionWebBigger);

    connect(mUniboardMainWindow->actionHome, SIGNAL(triggered()), this , SLOT(slotHome()));

    connect(mUniboardMainWindow->actionBookmarks, SIGNAL(triggered()), this , SLOT(bookmarks()));
    connect(mUniboardMainWindow->actionAddBookmark, SIGNAL(triggered()), this , SLOT(addBookmark()));
    connect(mUniboardMainWindow->actionWebBigger, SIGNAL(triggered()), this , SLOT(slotViewZoomIn()));
    connect(mUniboardMainWindow->actionWebSmaller, SIGNAL(triggered()), this , SLOT(slotViewZoomOut()));

    mWebToolBar->show();
}


void WBBrowserWindow::adaptToolBar(bool wideRes)
{
    if (mSearchAction)
    {
        mSearchAction->setVisible(wideRes);
    }
}


QUrl WBBrowserWindow::guessUrlFromString(const QString &string)
{
    QString urlStr = string.trimmed();
    QRegExp test(QLatin1String("^[a-zA-Z]+\\:.*"));

    // Check if it looks like a qualified URL. Try parsing it and see.
    bool hasSchema = test.exactMatch(urlStr);
    if (hasSchema)
    {
        int dotCount = urlStr.count(".");

        if (dotCount == 0 && !urlStr.contains(".com"))
        {
            urlStr += ".com";
        }

        QUrl url = QUrl::fromEncoded(urlStr.toUtf8(), QUrl::TolerantMode);
        if (url.isValid())
        {
            return url;
        }
    }

    // Might be a file.
    if (QFile::exists(urlStr))
    {
        QFileInfo info(urlStr);
        return QUrl::fromLocalFile(info.absoluteFilePath());
    }

    // Might be a shorturl - try to detect the schema.
    if (!hasSchema)
    {
        QString schema = "http";

        QString guessed = schema + "://" + urlStr;

        int dotCount = guessed.count(".");

        if (dotCount == 0 && !urlStr.contains(".com"))
        {
            guessed += ".com";
        }

        QUrl url = QUrl::fromEncoded(guessed.toUtf8(), QUrl::TolerantMode);

        if (url.isValid())
            return url;
    }

    // Fall back to QUrl's own tolerant parser.
    QUrl url = QUrl::fromEncoded(string.toUtf8(), QUrl::TolerantMode);

    // finally for cases where the user just types in a hostname add http
    if (url.scheme().isEmpty())
        url = QUrl::fromEncoded("http://" + string.toUtf8(), QUrl::TolerantMode);

    return url;
}


void WBBrowserWindow::loadUrl(const QUrl &url)
{
    if (!currentTabWebView() || !url.isValid())
        return;

336
    mTabWidget->currentLineEdit()->setText(url.toString());
Claudio Valerio's avatar
Claudio Valerio committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    mTabWidget->loadUrlInCurrentTab(url);
}


void WBBrowserWindow::loadUrlInNewTab(const QUrl &url)
{
    if (!url.isValid())
        return;

    mTabWidget->newTab();
    loadUrl(url);
}


WBWebView* WBBrowserWindow::createNewTab()
{
    return mTabWidget->newTab();
}


void WBBrowserWindow::slotSelectLineEdit()
{
    mTabWidget->currentLineEdit()->selectAll();
    mTabWidget->currentLineEdit()->setFocus();
}


void WBBrowserWindow::slotFileSaveAs()
{
    downloadManager()->download(currentTabWebView()->url(), true);
}


void WBBrowserWindow::slotUpdateStatusbar(const QString &string)
{
    Q_UNUSED(string);
}


void WBBrowserWindow::slotUpdateWindowTitle(const QString &title)
{
    Q_UNUSED(title);
}


void WBBrowserWindow::closeEvent(QCloseEvent *event)
{
    event->accept();
    deleteLater();
}


void WBBrowserWindow::slotViewZoomIn()
{
    WBWebView *currentWebView = currentTabWebView();

    if (!currentWebView)
        return;

    QWidget *view = currentWebView->page()->view();

    UBWebPluginPDFWidget *pdfPluginWidget = view ? view->findChild<UBWebPluginPDFWidget *>("PDFWebPluginWidget") : 0;
    if (pdfPluginWidget)
    {
        pdfPluginWidget->zoomIn();
    }
    else
    {
        currentWebView->setZoomFactor(currentWebView->zoomFactor() + 0.1);
    }
}


void WBBrowserWindow::slotViewZoomOut()
{
    WBWebView *currentWebView = currentTabWebView();

    if (!currentWebView)
        return;

    QWidget *view = currentWebView->page()->view();

    UBWebPluginPDFWidget *pdfPluginWidget = view ? view->findChild<UBWebPluginPDFWidget *>("PDFWebPluginWidget") : 0;

    if (pdfPluginWidget)
    {
        pdfPluginWidget->zoomOut();
    }
    else
    {
        currentWebView->setZoomFactor(currentWebView->zoomFactor() - 0.1);
    }
}


void WBBrowserWindow::slotViewResetZoom()
{
    if (!currentTabWebView())
        return;
    currentTabWebView()->setZoomFactor(1.0);
}


void WBBrowserWindow::slotViewZoomTextOnly(bool enable)
{
    if (!currentTabWebView())
        return;
    currentTabWebView()->page()->settings()->setAttribute(QWebSettings::ZoomTextOnly, enable);
}


void WBBrowserWindow::slotHome()
{
    loadPage(UBSettings::settings()->webHomePage->get().toString());
}


void WBBrowserWindow::slotWebSearch()
{
    mSearchToolBar->lineEdit()->selectAll();
    mSearchToolBar->lineEdit()->setFocus();
}


void WBBrowserWindow::slotToggleInspector(bool enable)
{
    Q_UNUSED(enable);
}


void WBBrowserWindow::slotSwapFocus()
{
    if (currentTabWebView()->hasFocus())
        mTabWidget->currentLineEdit()->setFocus();
    else
        currentTabWebView()->setFocus();
}


void WBBrowserWindow::loadPage(const QString &page)
{
    QUrl url = guessUrlFromString(page);
    loadUrl(url);
}


WBTabWidget *WBBrowserWindow::tabWidget() const
{
    return mTabWidget;
}


WBWebView *WBBrowserWindow::currentTabWebView() const
{
    return mTabWidget->currentWebView();
}


void WBBrowserWindow::slotLoadProgress(int progress)
{
    if (mChaseWidget)
        mChaseWidget->setAnimated(progress > 0 && progress < 100);
}


void WBBrowserWindow::geometryChangeRequested(const QRect &geometry)
{
    setGeometry(geometry);
}


void WBBrowserWindow::tabCurrentChanged(int index)
{
    QWidget* current = mTabWidget->widget(index);

    emit activeViewChange(current);
    emit activeViewPageChanged();
}


void WBBrowserWindow::bookmarks()
{
    loadPage(UBSettings::settings()->webBookmarksPage->get().toString());
}


void WBBrowserWindow::addBookmark()
{
    loadPage(UBSettings::settings()->webAddBookmarkUrl->get().toString() + currentTabWebView()->url().toString());
}


WBWebView* WBBrowserWindow::paintWidget()
{
    return mTabWidget->currentWebView();
}


void WBBrowserWindow::showTabAtTop(bool attop)
{
    if (attop)
        mTabWidget->setTabPosition(QTabWidget::North);
    else
        mTabWidget->setTabPosition(QTabWidget::South);
}

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
543 544 545 546 547 548 549 550 551 552 553 554
void WBBrowserWindow::aboutToShowBackMenu()
{
    mHistoryBackMenu->clear();
    if (!currentTabWebView())
        return;
    QWebHistory *history = currentTabWebView()->history();

    int historyCount = history->count();
    int historyLimit = history->backItems(historyCount).count() - UBSettings::settings()->historyLimit->get().toReal();
    if (historyLimit < 0)
        historyLimit = 0;

555
    for (int i = history->backItems(historyCount).count() - 1; i >= historyLimit; --i)
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
556 557 558 559 560 561 562 563
    {
        QWebHistoryItem item = history->backItems(historyCount).at(i);

        QAction *action = new QAction(this);
        action->setData(-1*(historyCount-i-1));

        if (!QWebSettings::iconForUrl(item.originalUrl()).isNull())
        action->setIcon(item.icon());
Claudio Valerio's avatar
Claudio Valerio committed
564
        action->setText(item.title().isEmpty() ? item.url().toString() : item.title());
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
        mHistoryBackMenu->addAction(action);
    }

    mHistoryBackMenu->addSeparator();

    QAction *action = new QAction(this);
    action->setData("clear");
    action->setText("Clear history");
    mHistoryBackMenu->addAction(action);

}

void WBBrowserWindow::aboutToShowForwardMenu()
{
    mHistoryForwardMenu->clear();
    if (!currentTabWebView())
        return;
    QWebHistory *history = currentTabWebView()->history();
    int historyCount = history->count();

    int historyLimit = history->forwardItems(historyCount).count();
    if (historyLimit > UBSettings::settings()->historyLimit->get().toReal())
        historyLimit = UBSettings::settings()->historyLimit->get().toReal();

589
    for (int i = 0; i < historyLimit; ++i)
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
    {
        QWebHistoryItem item = history->forwardItems(historyCount).at(i);

        QAction *action = new QAction(this);
        action->setData(historyCount-i);

        if (!QWebSettings::iconForUrl(item.originalUrl()).isNull())
        action->setIcon(item.icon());
        action->setText(item.title().isEmpty() ? item.url().toString() : item.title());
        mHistoryForwardMenu->addAction(action);
    }

    mHistoryForwardMenu->addSeparator();

    QAction *action = new QAction(this);
    action->setData("clear");
    action->setText("Clear history");
    mHistoryForwardMenu->addAction(action);
}

void WBBrowserWindow::openActionUrl(QAction *action)
{
612
    QWebHistory *history = currentTabWebView()->history();
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
613 614 615 616 617 618

    if (action->data() == "clear")
    {
        history->clear();
        return;
    }
Claudio Valerio's avatar
Claudio Valerio committed
619

Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
620 621
    int offset = action->data().toInt();
    if (offset < 0)
622
        history->goToItem(history->backItems(-1*offset).first());
Anatoly Mihalchenko's avatar
Anatoly Mihalchenko committed
623
    else if (offset > 0)
624
        history->goToItem(history->forwardItems(history->count() - offset + 1).back());
625
 }