Home | History | Annotate | Download | only in qt
      1 /*
      2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
      3  * Copyright (C) 2009 University of Szeged
      4  *
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "UrlLoader.h"
     30 
     31 #include <QDebug>
     32 #include <QFile>
     33 
     34 UrlLoader::UrlLoader(BrowserWindow* browserWindow, const QString& inputFileName, int timeoutSeconds, int extraTimeSeconds)
     35     : m_browserWindow(browserWindow)
     36     , m_stdOut(stdout)
     37     , m_loaded(0)
     38     , m_numFramesLoading(0)
     39 {
     40     m_checkIfFinishedTimer.setInterval(200);
     41     m_checkIfFinishedTimer.setSingleShot(true);
     42     connect(&m_checkIfFinishedTimer, SIGNAL(timeout()), this, SLOT(checkIfFinished()));
     43     // loadStarted and loadFinished on QWebPage is emitted for each frame/sub-frame
     44     connect(m_browserWindow->page(), SIGNAL(loadStarted()), this, SLOT(frameLoadStarted()));
     45     connect(m_browserWindow->page(), SIGNAL(loadFinished(bool)), this, SLOT(frameLoadFinished()));
     46 
     47     if (timeoutSeconds) {
     48         m_timeoutTimer.setInterval(timeoutSeconds * 1000);
     49         m_timeoutTimer.setSingleShot(true);
     50         connect(m_browserWindow->page(), SIGNAL(loadStarted()), &m_timeoutTimer, SLOT(start()));
     51         connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(loadNext()));
     52     }
     53     if (extraTimeSeconds) {
     54         m_extraTimeTimer.setInterval(extraTimeSeconds * 1000);
     55         m_extraTimeTimer.setSingleShot(true);
     56         connect(this, SIGNAL(pageLoadFinished()), &m_extraTimeTimer, SLOT(start()));
     57         connect(&m_extraTimeTimer, SIGNAL(timeout()), this, SLOT(loadNext()));
     58     } else
     59         connect(this, SIGNAL(pageLoadFinished()), this, SLOT(loadNext()));
     60     loadUrlList(inputFileName);
     61 }
     62 
     63 void UrlLoader::loadNext()
     64 {
     65     m_timeoutTimer.stop();
     66     m_extraTimeTimer.stop();
     67     m_checkIfFinishedTimer.stop();
     68     m_numFramesLoading = 0;
     69     QString qstr;
     70     if (getUrl(qstr)) {
     71         QUrl url(qstr, QUrl::StrictMode);
     72         if (url.isValid()) {
     73             m_stdOut << "Loading " << qstr << " ......" << ++m_loaded << endl;
     74             m_browserWindow->load(url.toString());
     75         } else
     76             loadNext();
     77     } else
     78         disconnect(m_browserWindow, 0, this, 0);
     79 }
     80 
     81 void UrlLoader::checkIfFinished()
     82 {
     83     if (!m_numFramesLoading)
     84         emit pageLoadFinished();
     85 }
     86 
     87 void UrlLoader::frameLoadStarted()
     88 {
     89     ++m_numFramesLoading;
     90     m_checkIfFinishedTimer.stop();
     91 }
     92 
     93 void UrlLoader::frameLoadFinished()
     94 {
     95     Q_ASSERT(m_numFramesLoading > 0);
     96     --m_numFramesLoading;
     97     // Once our frame has finished loading, wait a moment to call loadNext for cases
     98     // where a sub-frame starts loading or another frame is loaded through JavaScript.
     99     m_checkIfFinishedTimer.start();
    100 }
    101 
    102 void UrlLoader::loadUrlList(const QString& inputFileName)
    103 {
    104     QFile inputFile(inputFileName);
    105     if (inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
    106         QTextStream stream(&inputFile);
    107         QString line;
    108         while (true) {
    109             line = stream.readLine();
    110             if (line.isNull())
    111                 break;
    112             m_urls.append(line);
    113         }
    114     } else {
    115         qDebug() << "Can't open list file";
    116         exit(0);
    117     }
    118     m_index = 0;
    119     inputFile.close();
    120 }
    121 
    122 bool UrlLoader::getUrl(QString& qstr)
    123 {
    124     if (m_index == m_urls.size())
    125         return false;
    126 
    127     qstr = m_urls[m_index++];
    128     return true;
    129 }
    130