Akonadi

asapcat/session.cpp
1 /***************************************************************************
2  * Copyright (C) 2013 by Volker Krause <[email protected]> *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU Library General Public License as *
6  * published by the Free Software Foundation; either version 2 of the *
7  * License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Library General Public *
15  * License along with this program; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18  ***************************************************************************/
19 
20 #include "session.h"
21 
22 #include <private/standarddirs_p.h>
23 
24 #include <QCoreApplication>
25 #include <QFile>
26 #include <QSocketNotifier>
27 #include <QSettings>
28 #include <QLocalSocket>
29 
30 #include <iostream>
31 #include <fcntl.h>
32 #include <unistd.h>
33 
34 Session::Session(const QString &input, QObject *parent)
35  : QObject(parent)
36 {
37  QFile *file = new QFile(this);
38  if (input != QLatin1String("-")) {
39  file->setFileName(input);
40  if (!file->open(QFile::ReadOnly)) {
41  qFatal("Failed to open %s", qPrintable(input));
42  }
43  } else {
44  // ### does that work on Windows?
45  const int flags = fcntl(0, F_GETFL);
46  fcntl(0, F_SETFL, flags | O_NONBLOCK);
47 
48  if (!file->open(stdin, QFile::ReadOnly | QFile::Unbuffered)) {
49  qFatal("Failed to open stdin!");
50  }
51  m_notifier = new QSocketNotifier(0, QSocketNotifier::Read, this);
52  connect(m_notifier, &QSocketNotifier::activated, this, &Session::inputAvailable);
53  }
54  m_input = file;
55 }
56 
58 {
59 }
60 
61 void Session::connectToHost()
62 {
63  const QSettings connectionSettings(Akonadi::StandardDirs::connectionConfigFile(), QSettings::IniFormat);
64 
65  QString serverAddress;
66 #ifdef Q_OS_WIN
67  serverAddress = connectionSettings.value(QStringLiteral("Data/NamedPipe"), QString()).toString();
68 #else
69  serverAddress = connectionSettings.value(QStringLiteral("Data/UnixPath"), QString()).toString();
70 #endif
71  if (serverAddress.isEmpty()) {
72  qFatal("Unable to determine server address.");
73  }
74 
75  QLocalSocket *socket = new QLocalSocket(this);
76 #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
77  connect(socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error), this, &Session::serverError);
78 #else
79  connect(socket, &QLocalSocket::errorOccurred, this, &Session::serverError);
80 #endif
81  connect(socket, &QLocalSocket::disconnected, this, &Session::serverDisconnected);
82  connect(socket, &QIODevice::readyRead, this, &Session::serverRead);
83  connect(socket, &QLocalSocket::connected, this, &Session::inputAvailable);
84 
85  m_session = socket;
86  socket->connectToServer(serverAddress);
87 
88  m_connectionTime.start();
89 }
90 
91 void Session::inputAvailable()
92 {
93  if (!m_session->isOpen()) {
94  return;
95  }
96 
97  if (m_notifier) {
98  m_notifier->setEnabled(false);
99  }
100 
101  if (m_input->atEnd()) {
102  return;
103  }
104 
105  QByteArray buffer(1024, Qt::Uninitialized);
106  qint64 readSize = 0;
107 
108  while ((readSize = m_input->read(buffer.data(), buffer.size())) > 0) {
109  m_session->write(buffer.constData(), readSize);
110  m_sentBytes += readSize;
111  }
112 
113  if (m_notifier) {
114  m_notifier->setEnabled(true);
115  }
116 }
117 
118 void Session::serverDisconnected()
119 {
121 }
122 
123 void Session::serverError(QLocalSocket::LocalSocketError socketError)
124 {
125  if (socketError == QLocalSocket::PeerClosedError) {
127  return;
128  }
129 
130  std::cerr << qPrintable(m_session->errorString());
132 }
133 
134 void Session::serverRead()
135 {
136  QByteArray buffer(1024, Qt::Uninitialized);
137  qint64 readSize = 0;
138 
139  while ((readSize = m_session->read(buffer.data(), buffer.size())) > 0) {
140  write(1, buffer.data(), readSize);
141  m_receivedBytes += readSize;
142  }
143 }
144 
145 void Session::printStats() const
146 {
147  std::cerr << "Connection time: " << m_connectionTime.elapsed() << " ms" << std::endl;
148  std::cerr << "Sent: " << m_sentBytes << " bytes" << std::endl;
149  std::cerr << "Received: " << m_receivedBytes << " bytes" << std::endl;
150 }
void activated(int socket)
void connected()
void setFileName(const QString &name)
~Session()
Destroys the session.
void exit(int returnCode)
bool isEmpty() const const
virtual bool open(QIODevice::OpenMode mode) override
void disconnected()
void connectToServer(QIODevice::OpenMode openMode)
void readyRead()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLocalSocket::LocalSocketError error() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.