File indexing completed on 2024-05-12 17:15:12

0001 /*
0002    Copyright (C) 2013 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #include "eavesdropperthread.h"
0025 
0026 #include "arguments.h"
0027 #include "connectaddress.h"
0028 #include "eavesdroppermodel.h"
0029 #include "error.h"
0030 #include "eventdispatcher.h"
0031 #include "localsocket.h"
0032 #include "message.h"
0033 #include "pendingreply.h"
0034 #include "connection.h"
0035 
0036 #include "../setupeavesdropping.h"
0037 
0038 EavesdropperThread::EavesdropperThread(EavesdropperModel *model)
0039 {
0040     // do not parent this to the model; it doesn't work across threads
0041     moveToThread(&m_thread);
0042     connect(this, SIGNAL(messageReceived(Message *, qint64)),
0043             model, SLOT(addMessage(Message *, qint64)), Qt::QueuedConnection);
0044     connect(&m_thread, SIGNAL(started()), SLOT(run()));
0045     m_thread.start();
0046 }
0047 
0048 EavesdropperThread::~EavesdropperThread()
0049 {
0050     m_dispatcher->interrupt();
0051     m_thread.wait();
0052     delete m_connection;
0053     delete m_dispatcher;
0054 }
0055 
0056 void EavesdropperThread::run()
0057 {
0058     m_timer.start();
0059     m_dispatcher = new EventDispatcher;
0060     m_connection = new Connection(m_dispatcher, ConnectAddress::StandardBus::Session);
0061 
0062     setupEavesdropping(m_connection);
0063     m_connection->setSpontaneousMessageReceiver(this);
0064 
0065     while (m_dispatcher->poll()) {
0066     }
0067 
0068     m_thread.quit();
0069 }
0070 
0071 void EavesdropperThread::handleSpontaneousMessageReceived(Message message, Connection *)
0072 {
0073     emit messageReceived(new Message(std::move(message)), m_timer.nsecsElapsed());
0074 }