1 /************************************************************************** 2 ** ** 3 ** Copyright (C) 2018 Lukas Spies ** 4 ** Contact: http://photoqt.org ** 5 ** ** 6 ** This file is part of PhotoQt. ** 7 ** ** 8 ** PhotoQt is free software: you can redistribute it and/or modify ** 9 ** it under the terms of the GNU General Public License as published by ** 10 ** the Free Software Foundation, either version 2 of the License, or ** 11 ** (at your option) any later version. ** 12 ** ** 13 ** PhotoQt is distributed in the hope that it will be useful, ** 14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 16 ** GNU General Public License for more details. ** 17 ** ** 18 ** You should have received a copy of the GNU General Public License ** 19 ** along with PhotoQt. If not, see <http://www.gnu.org/licenses/>. ** 20 ** ** 21 **************************************************************************/ 22 23 #ifndef REPLYTIMEOUT_H 24 #define REPLYTIMEOUT_H 25 26 #include <QObject> 27 28 class ReplyTimeout : public QObject { 29 Q_OBJECT 30 QBasicTimer m_timer; 31 public: ReplyTimeout(QNetworkReply * reply,const int timeout)32 ReplyTimeout(QNetworkReply* reply, const int timeout) : QObject(reply) { 33 Q_ASSERT(reply); 34 if (reply && reply->isRunning()) 35 m_timer.start(timeout, this); 36 } set(QNetworkReply * reply,const int timeout)37 static void set(QNetworkReply* reply, const int timeout) { 38 new ReplyTimeout(reply, timeout); 39 } 40 protected: timerEvent(QTimerEvent * ev)41 void timerEvent(QTimerEvent * ev) { 42 if (!m_timer.isActive() || ev->timerId() != m_timer.timerId()) 43 return; 44 auto reply = static_cast<QNetworkReply*>(parent()); 45 if (reply->isRunning()) 46 reply->close(); 47 m_timer.stop(); 48 } 49 }; 50 51 #endif // REPLYTIMEOUT_H 52