2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3 * Copyright (C) 2009 University of Szeged
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include "urlloader.h"
35 UrlLoader::UrlLoader(QWebFrame* frame, const QString& inputFileName, int timeoutSeconds, int extraTimeSeconds)
39 , m_numFramesLoading(0)
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()));
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()));
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()));
60 connect(this, SIGNAL(pageLoadFinished()), this, SLOT(loadNext()));
61 loadUrlList(inputFileName);
64 void UrlLoader::loadNext()
66 m_timeoutTimer.stop();
67 m_extraTimeTimer.stop();
68 m_checkIfFinishedTimer.stop();
69 m_numFramesLoading = 0;
72 QUrl url(qstr, QUrl::StrictMode);
74 m_stdOut << "Loading " << qstr << " ......" << ++m_loaded << endl;
79 disconnect(m_frame, 0, this, 0);
82 void UrlLoader::checkIfFinished()
84 if (!m_numFramesLoading)
85 emit pageLoadFinished();
88 void UrlLoader::frameLoadStarted()
91 m_checkIfFinishedTimer.stop();
94 void UrlLoader::frameLoadFinished()
96 Q_ASSERT(m_numFramesLoading > 0);
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();
103 void UrlLoader::loadUrlList(const QString& inputFileName)
105 QFile inputFile(inputFileName);
106 if (inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
107 QTextStream stream(&inputFile);
110 line = stream.readLine();
116 qDebug() << "Can't open list file";
123 bool UrlLoader::getUrl(QString& qstr)
125 if (m_index == m_urls.size())
128 qstr = m_urls[m_index++];