Better wording
[kdepim.git] / kleopatra / uiserver / uiserver_win.cpp
blob464d234e7dffd907fbd41cdaf80318b51fe7eff4
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 uiserver/uiserver_win.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2007 Klarälvdalens Datakonsult AB
7 Kleopatra is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Kleopatra is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
33 #include <config-kleopatra.h>
35 #include "uiserver_p.h"
37 #include "utils/gnupg-helper.h"
39 #include <KLocalizedString>
41 #include <QFile>
42 #include <QDir>
43 #include <QTextStream>
44 #include <qendian.h>
46 #include <stdexcept>
47 #include <cassert>
49 #include <windows.h>
50 #include <io.h>
51 #include <winsock2.h>
53 #include <cstring>
54 #include <cstdlib>
56 using namespace Kleo;
57 using namespace boost;
60 QString UiServer::Private::systemErrorString() {
61 #ifndef _WIN32_WCE
62 return QString::fromLocal8Bit( strerror(errno) );
63 #else
64 return QString();
65 #endif
68 void UiServer::Private::doMakeListeningSocket( const QByteArray & encodedFileName ) {
69 // Create a Unix Domain Socket:
70 const assuan_fd_t sock = assuan_sock_new( AF_UNIX, SOCK_STREAM, 0 );
71 if ( sock == ASSUAN_INVALID_FD )
72 throw_<std::runtime_error>( i18n( "Could not create socket: %1", systemErrorString() ) );
74 try {
75 // Bind
76 struct sockaddr_un sa;
77 std::memset( &sa, 0, sizeof(sa) );
78 sa.sun_family = AF_UNIX;
79 std::strncpy( sa.sun_path, encodedFileName.constData(), sizeof( sa.sun_path ) - 1 );
80 if ( assuan_sock_bind( sock, (struct sockaddr*)&sa, sizeof( sa ) ) )
81 throw_<std::runtime_error>( i18n( "Could not bind to socket: %1", systemErrorString() ) );
83 if ( assuan_sock_get_nonce( (struct sockaddr*)&sa, sizeof( sa ), &nonce ) )
84 throw_<std::runtime_error>( i18n("Could not get socket nonce: %1", systemErrorString() ) );
86 // Listen
87 if ( ::listen( (SOCKET)sock, SOMAXCONN ) )
88 throw_<std::runtime_error>( i18n( "Could not listen to socket: %1", systemErrorString() ) );
90 if ( !setSocketDescriptor( (intptr_t)sock ) )
91 throw_<std::runtime_error>( i18n( "Could not pass socket to Qt: %1. This should not happen, please report this bug.", errorString() ) );
93 } catch ( ... ) {
94 assuan_sock_close( sock );
95 throw;