-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathprintsubmissionserver.cpp
More file actions
180 lines (136 loc) · 4.06 KB
/
Copy pathprintsubmissionserver.cpp
File metadata and controls
180 lines (136 loc) · 4.06 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright 2026 Ian Walls <ian@bywatersolutions.com>
*
* This file is part of Libki.
*
* Libki 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 3 of the License, or
* (at your option) any later version.
*
* Libki 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 Libki. If not, see <http://www.gnu.org/licenses/>.
*/
#include "printsubmissionserver.h"
#include <QDataStream>
#include <QDebug>
#include <QLocalSocket>
#include "printprotocol.h"
PrintSubmissionServer::PrintSubmissionServer(QObject *parent) : QObject(parent) {
server = new QLocalServer(this);
connect(server,
SIGNAL(newConnection()),
this,
SLOT(newConnection()));
}
bool PrintSubmissionServer::start() {
QLocalServer::removeServer(LIBKI_PRINT_SERVER_NAME);
if (!server->listen(LIBKI_PRINT_SERVER_NAME)) {
qWarning() << "Unable to start PrintSubmissionServer:"
<< server->errorString();
return false;
}
qDebug() << "PrintSubmissionServer listening on"
<< LIBKI_PRINT_SERVER_NAME;
return true;
}
void PrintSubmissionServer::newConnection()
{
while (server->hasPendingConnections()) {
QLocalSocket *socket =
server->nextPendingConnection();
connect(socket,
SIGNAL(readyRead()),
this,
SLOT(socketReadyRead()));
connect(socket,
SIGNAL(disconnected()),
socket,
SLOT(deleteLater()));
}
}
void PrintSubmissionServer::socketReadyRead()
{
QLocalSocket *socket =
qobject_cast<QLocalSocket *>(sender());
if (!socket)
return;
QString error;
bool finished =
processSocket(socket, error);
if (!error.isEmpty())
qWarning() << error;
if (finished &&
socket->state() == QLocalSocket::ConnectedState)
{
socket->disconnectFromServer();
}
}
bool PrintSubmissionServer::processSocket(QLocalSocket *socket, QString &error) {
if (!socket)
return true;
if (socket->bytesAvailable() < sizeof(quint32) * 2)
return false;
QDataStream stream(socket);
stream.setVersion(QDataStream::Qt_5_5);
quint32 version;
quint32 message;
stream >> version;
stream >> message;
if (version != LIBKI_PRINT_PROTOCOL_VERSION) {
error = tr("Protocol version mismatch.");
return true;
}
switch (message) {
case PrintMessage_SubmitPrintRequest:
{
SubmitPrintRequest request;
stream >> request;
emit submitPrintRequested(request);
QDataStream out(socket);
out.setVersion(QDataStream::Qt_5_5);
out << (quint32)LIBKI_PRINT_PROTOCOL_VERSION;
out << (quint32)PrintStatus_Ok;
out << QString("OK");
socket->flush();
socket->waitForBytesWritten(1000);
return true;
}
case PrintMessage_GetPrintInfoRequest:
{
PrintInfoRequest request;
stream >> request;
emit printInfoRequested(request, socket);
return false;
}
default:
qWarning()
<< "Unknown IPC message";
return true;
}
QDataStream out(socket);
out.setVersion(QDataStream::Qt_5_5);
out << (quint32)LIBKI_PRINT_PROTOCOL_VERSION;
out << (quint32)PrintStatus_Ok;
out << QString("OK");
socket->flush();
socket->waitForBytesWritten(1000);
socket->disconnectFromServer();
return true;
}
void PrintSubmissionServer::sendPrintInfoReply(QLocalSocket *socket, const PrintInfoReply &reply)
{
if (!socket)
return;
QDataStream stream(socket);
stream << (quint32)LIBKI_PRINT_PROTOCOL_VERSION;
stream << (quint32)PrintMessage_GetPrintInfoReply;
stream << reply;
socket->flush();
socket->disconnectFromServer();
}