+class Worker : public QObject
+{
+ Q_OBJECT
+
+public:
+ Worker(UserId userId, bool restoreState, bool strictIdentEnabled)
+ : _userId{userId}
+ , _restoreState{restoreState}
+ , _strictIdentEnabled{strictIdentEnabled}
+ {}
+
+public slots:
+ void initialize()
+ {
+ _session = new CoreSession{_userId, _restoreState, _strictIdentEnabled, this};
+ connect(_session, &QObject::destroyed, QThread::currentThread(), &QThread::quit);
+ connect(_session, &CoreSession::sessionStateReceived, Core::instance(), &Core::sessionStateReceived);
+ emit initialized();
+ }
+
+ void shutdown()
+ {
+ if (_session) {
+ _session->shutdown();
+ }
+ }
+
+ void addClient(Peer* peer)
+ {
+ if (!_session) {
+ qWarning() << "Session not initialized!";
+ return;
+ }
+
+ auto remotePeer = qobject_cast<RemotePeer*>(peer);
+ if (remotePeer) {
+ _session->addClient(remotePeer);
+ return;
+ }
+ auto internalPeer = qobject_cast<InternalPeer*>(peer);
+ if (internalPeer) {
+ _session->addClient(internalPeer);
+ return;
+ }
+
+ qWarning() << "SessionThread::addClient() received invalid peer!" << peer;
+ }
+
+signals:
+ void initialized();
+
+private:
+ UserId _userId;
+ bool _restoreState;
+ bool _strictIdentEnabled; ///< Whether or not strict ident mode is enabled, locking users' idents to Quassel username
+ QPointer<CoreSession> _session;
+};
+
+} // namespace
+
+SessionThread::SessionThread(UserId uid, bool restoreState, bool strictIdentEnabled, QObject* parent)
+ : QObject(parent)
+{
+ auto worker = new Worker(uid, restoreState, strictIdentEnabled);
+ worker->moveToThread(&_sessionThread);
+ connect(&_sessionThread, &QThread::started, worker, &Worker::initialize);
+ connect(&_sessionThread, &QThread::finished, worker, &QObject::deleteLater);
+ connect(worker, &Worker::initialized, this, &SessionThread::onSessionInitialized);
+ connect(worker, &QObject::destroyed, this, &SessionThread::onSessionDestroyed);
+
+ connect(this, &SessionThread::addClientToWorker, worker, &Worker::addClient);
+ connect(this, &SessionThread::shutdownSession, worker, &Worker::shutdown);
+
+ // Defer thread start through the event loop, so the SessionThread instance is fully constructed before
+ QTimer::singleShot(0, &_sessionThread, SLOT(start()));