~ubuntu-branches/ubuntu/jaunty/quassel/jaunty-backports

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/***************************************************************************
 *   Copyright (C) 2005-09 by the Quassel Project                          *
 *   devel@quassel-irc.org                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) version 3.                                           *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QMutexLocker>

#include "sessionthread.h"
#include "signalproxy.h"
#include "coresession.h"
#include "core.h"

SessionThread::SessionThread(UserId uid, bool restoreState, QObject *parent)
  : QThread(parent),
    _session(0),
    _user(uid),
    _sessionInitialized(false),
    _restoreState(restoreState)
{
  connect(this, SIGNAL(initialized()), this, SLOT(setSessionInitialized()));
}

SessionThread::~SessionThread() {
  // shut down thread gracefully
  quit();
  wait();
}

CoreSession *SessionThread::session() {
  return _session;
}

UserId SessionThread::user() {
  return _user;
}

bool SessionThread::isSessionInitialized() {
  return _sessionInitialized;
}

void SessionThread::setSessionInitialized() {
  _sessionInitialized = true;
  foreach(QObject *peer, clientQueue) {
    addClientToSession(peer);
  }
  clientQueue.clear();
}

void SessionThread::addClient(QObject *peer) {
  if(isSessionInitialized()) {
    addClientToSession(peer);
  } else {
    clientQueue.append(peer);
  }
}

void SessionThread::addClientToSession(QObject *peer) {
  QIODevice *socket = qobject_cast<QIODevice *>(peer);
  if(socket) {
    addRemoteClientToSession(socket);
    return;
  }

  SignalProxy *proxy = qobject_cast<SignalProxy *>(peer);
  if(proxy) {
    addInternalClientToSession(proxy);
    return;
  }

  qWarning() << "SessionThread::addClient() received neither QIODevice nor SignalProxy as peer!" << peer;
}

void SessionThread::addRemoteClientToSession(QIODevice *socket) {
  socket->setParent(0);
  socket->moveToThread(session()->thread());
  emit addRemoteClient(socket);
}

void SessionThread::addInternalClientToSession(SignalProxy *proxy) {
  emit addInternalClient(proxy);
}

void SessionThread::run() {
  _session = new CoreSession(user(), _restoreState);
  connect(this, SIGNAL(addRemoteClient(QIODevice *)), _session, SLOT(addClient(QIODevice *)));
  connect(this, SIGNAL(addInternalClient(SignalProxy *)), _session, SLOT(addClient(SignalProxy *)));
  connect(_session, SIGNAL(sessionState(const QVariant &)), Core::instance(), SIGNAL(sessionState(const QVariant &)));
  emit initialized();
  exec();
  delete _session;
}