qtui: Set proper icon for "About Quassel" menu option
[quassel.git] / src / core / sessionthread.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "core.h"
22 #include "coresession.h"
23 #include "internalpeer.h"
24 #include "remotepeer.h"
25 #include "sessionthread.h"
26 #include "signalproxy.h"
27
28 SessionThread::SessionThread(UserId uid, bool restoreState, bool strictIdentEnabled, QObject *parent)
29     : QThread(parent),
30     _session(0),
31     _user(uid),
32     _sessionInitialized(false),
33     _restoreState(restoreState),
34     _strictIdentEnabled(strictIdentEnabled)
35 {
36     connect(this, SIGNAL(initialized()), this, SLOT(setSessionInitialized()));
37 }
38
39
40 SessionThread::~SessionThread()
41 {
42     // shut down thread gracefully
43     quit();
44     wait();
45 }
46
47
48 CoreSession *SessionThread::session()
49 {
50     return _session;
51 }
52
53
54 UserId SessionThread::user()
55 {
56     return _user;
57 }
58
59
60 bool SessionThread::isSessionInitialized()
61 {
62     return _sessionInitialized;
63 }
64
65
66 void SessionThread::setSessionInitialized()
67 {
68     _sessionInitialized = true;
69     foreach(QObject *peer, clientQueue) {
70         addClientToSession(peer);
71     }
72     clientQueue.clear();
73 }
74
75
76 // this and the following related methods are executed in the Core thread!
77 void SessionThread::addClient(QObject *peer)
78 {
79     if (isSessionInitialized()) {
80         addClientToSession(peer);
81     }
82     else {
83         clientQueue.append(peer);
84     }
85 }
86
87
88 void SessionThread::addClientToSession(QObject *peer)
89 {
90     RemotePeer *remote = qobject_cast<RemotePeer *>(peer);
91     if (remote) {
92         addRemoteClientToSession(remote);
93         return;
94     }
95
96     InternalPeer *internal = qobject_cast<InternalPeer *>(peer);
97     if (internal) {
98         addInternalClientToSession(internal);
99         return;
100     }
101
102     qWarning() << "SessionThread::addClient() received invalid peer!" << peer;
103 }
104
105
106 void SessionThread::addRemoteClientToSession(RemotePeer *remotePeer)
107 {
108     remotePeer->setParent(0);
109     remotePeer->moveToThread(session()->thread());
110     emit addRemoteClient(remotePeer);
111 }
112
113
114 void SessionThread::addInternalClientToSession(InternalPeer *internalPeer)
115 {
116     internalPeer->setParent(0);
117     internalPeer->moveToThread(session()->thread());
118     emit addInternalClient(internalPeer);
119 }
120
121
122 void SessionThread::run()
123 {
124     _session = new CoreSession(user(), _restoreState, _strictIdentEnabled);
125     connect(this, SIGNAL(addRemoteClient(RemotePeer*)), _session, SLOT(addClient(RemotePeer*)));
126     connect(this, SIGNAL(addInternalClient(InternalPeer*)), _session, SLOT(addClient(InternalPeer*)));
127     connect(_session, SIGNAL(sessionState(Protocol::SessionState)), Core::instance(), SIGNAL(sessionState(Protocol::SessionState)));
128     emit initialized();
129     exec();
130     delete _session;
131 }