File indexing completed on 2024-11-10 04:40:11
0001 /* 0002 SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "fakesession.h" 0008 #include "job.h" 0009 #include "private/protocol_p.h" 0010 #include "session_p.h" 0011 #include <QCoreApplication> 0012 #include <QRandomGenerator> 0013 #include <QTimer> 0014 using namespace std::chrono_literals; 0015 class FakeSessionPrivate : public SessionPrivate 0016 { 0017 public: 0018 FakeSessionPrivate(FakeSession *parent, FakeSession::Mode mode) 0019 : SessionPrivate(parent) 0020 , q_ptr(parent) 0021 , m_mode(mode) 0022 { 0023 protocolVersion = Protocol::version(); 0024 } 0025 0026 /* reimp */ 0027 void init(const QByteArray &id) override 0028 { 0029 // trimmed down version of the real SessionPrivate::init(), without any server access 0030 if (!id.isEmpty()) { 0031 sessionId = id; 0032 } else { 0033 sessionId = QCoreApplication::instance()->applicationName().toUtf8() + '-' + QByteArray::number(QRandomGenerator::global()->bounded(1, RAND_MAX)); 0034 } 0035 0036 connected = false; 0037 theNextTag = 1; 0038 jobRunning = false; 0039 0040 reconnect(); 0041 } 0042 0043 /* reimp */ 0044 void reconnect() override 0045 { 0046 if (m_mode == FakeSession::EndJobsImmediately) { 0047 return; 0048 } 0049 0050 // Like Session does: delay the actual disconnect+reconnect 0051 QTimer::singleShot(10ms, q_ptr, [&]() { 0052 socketDisconnected(); 0053 Q_EMIT q_ptr->reconnected(); 0054 connected = true; 0055 startNext(); 0056 }); 0057 } 0058 0059 /* reimp */ 0060 void addJob(Job *job) override 0061 { 0062 Q_EMIT q_ptr->jobAdded(job); 0063 // Return immediately so that no actual communication happens with the server and 0064 // the started jobs are completed. 0065 if (m_mode == FakeSession::EndJobsImmediately) { 0066 endJob(job); 0067 } else { 0068 SessionPrivate::addJob(job); 0069 } 0070 } 0071 0072 FakeSession *q_ptr; 0073 FakeSession::Mode m_mode; 0074 }; 0075 0076 FakeSession::FakeSession(const QByteArray &sessionId, FakeSession::Mode mode, QObject *parent) 0077 : Session(new FakeSessionPrivate(this, mode), sessionId, parent) 0078 { 0079 } 0080 0081 void FakeSession::setAsDefaultSession() 0082 { 0083 d->setDefaultSession(this); 0084 } 0085 0086 #include "moc_fakesession.cpp"