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 <QFile>
32#include <QDebug>
33#include <QWebPage>
34
35UrlLoader::UrlLoader(QWebFrame* frame, const QString& inputFileName, int timeoutSeconds, int extraTimeSeconds)
36    : m_frame(frame)
37    , m_stdOut(stdout)
38    , m_loaded(0)
39    , m_numFramesLoading(0)
40{
41    m_checkIfFinishedTimer.setInterval(200);
42    m_checkIfFinishedTimer.setSingleShot(true);
43    connect(&m_checkIfFinishedTimer, SIGNAL(timeout()), this, SLOT(checkIfFinished()));
44    // loadStarted and loadFinished on QWebPage is emitted for each frame/sub-frame
45    connect(m_frame->page(), SIGNAL(loadStarted()), this, SLOT(frameLoadStarted()));
46    connect(m_frame->page(), SIGNAL(loadFinished(bool)), this, SLOT(frameLoadFinished()));
47
48    if (timeoutSeconds) {
49        m_timeoutTimer.setInterval(timeoutSeconds * 1000);
50        m_timeoutTimer.setSingleShot(true);
51        connect(frame, SIGNAL(loadStarted()), &m_timeoutTimer, SLOT(start()));
52        connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(loadNext()));
53    }
54    if (extraTimeSeconds) {
55        m_extraTimeTimer.setInterval(extraTimeSeconds * 1000);
56        m_extraTimeTimer.setSingleShot(true);
57        connect(this, SIGNAL(pageLoadFinished()), &m_extraTimeTimer, SLOT(start()));
58        connect(&m_extraTimeTimer, SIGNAL(timeout()), this, SLOT(loadNext()));
59    } else
60        connect(this, SIGNAL(pageLoadFinished()), this, SLOT(loadNext()));
61    loadUrlList(inputFileName);
62}
63
64void UrlLoader::loadNext()
65{
66    m_timeoutTimer.stop();
67    m_extraTimeTimer.stop();
68    m_checkIfFinishedTimer.stop();
69    m_numFramesLoading = 0;
70    QString qstr;
71    if (getUrl(qstr)) {
72        QUrl url(qstr, QUrl::StrictMode);
73        if (url.isValid()) {
74            m_stdOut << "Loading " << qstr << " ......" << ++m_loaded << endl;
75            m_frame->load(url);
76        } else
77            loadNext();
78    } else
79        disconnect(m_frame, 0, this, 0);
80}
81
82void UrlLoader::checkIfFinished()
83{
84    if (!m_numFramesLoading)
85        emit pageLoadFinished();
86}
87
88void UrlLoader::frameLoadStarted()
89{
90    ++m_numFramesLoading;
91    m_checkIfFinishedTimer.stop();
92}
93
94void UrlLoader::frameLoadFinished()
95{
96    Q_ASSERT(m_numFramesLoading > 0);
97    --m_numFramesLoading;
98    // Once our frame has finished loading, wait a moment to call loadNext for cases
99    // where a sub-frame starts loading or another frame is loaded through JavaScript.
100    m_checkIfFinishedTimer.start();
101}
102
103void UrlLoader::loadUrlList(const QString& inputFileName)
104{
105    QFile inputFile(inputFileName);
106    if (inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
107        QTextStream stream(&inputFile);
108        QString line;
109        while (true) {
110            line = stream.readLine();
111            if (line.isNull())
112                break;
113            m_urls.append(line);
114        }
115    } else {
116        qDebug() << "Can't open list file";
117        exit(0);
118    }
119    m_index = 0;
120    inputFile.close();
121}
122
123bool UrlLoader::getUrl(QString& qstr)
124{
125    if (m_index == m_urls.size())
126        return false;
127
128    qstr = m_urls[m_index++];
129    return true;
130}
131