Commit 53a95b3a authored by Rafał Malinowski's avatar Rafał Malinowski

win: use RAII for WSA

Signed-off-by: Rafał Malinowski's avatarRafał Malinowski <rafal.przemyslaw.malinowski@gmail.com>
parent afa65391
......@@ -49,8 +49,6 @@
#include <sys/types.h>
#include <pwd.h>
#include <unistd.h>
#else // !Q_OS_WIN32
#include <winsock2.h>
#endif // !Q_OS_WIN32
#include "configuration/configuration-file.h"
......@@ -58,6 +56,8 @@
#include "core/core.h"
#include "gui/windows/message-dialog.h"
#include "os/qtsingleapplication/qtlocalpeer.h"
#include "os/win/wsa-exception.h"
#include "os/win/wsa-handler.h"
#include "protocols/protocols-manager.h"
#include "icons/icons-manager.h"
......@@ -164,8 +164,10 @@ static void printKaduOptions()
" (overwrites CONFIG_DIR variable)\n");
}
int main(int argc, char *argv[])
int main(int argc, char *argv[]) try
{
WSAHandler wsaHandler;
bool ok;
long msec;
time_t sec;
......@@ -188,10 +190,6 @@ int main(int argc, char *argv[])
qputenv("LC_COLLATE", "en_US");
else if (lcAllEnv.isEmpty())
qputenv("LC_COLLATE", langEnv);
#else // !Q_OS_WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
return 2;
#endif // !Q_OS_WIN32
kdebugm(KDEBUG_INFO, "before creation of new KaduApplication\n");
......@@ -209,9 +207,6 @@ int main(int argc, char *argv[])
{
printVersion();
delete qApp;
#ifdef Q_OS_WIN32
WSACleanup();
#endif
return 0;
}
else if (*it == QLatin1String("--help"))
......@@ -219,9 +214,6 @@ int main(int argc, char *argv[])
printUsage();
printKaduOptions();
delete qApp;
#ifdef Q_OS_WIN32
WSACleanup();
#endif
return 0;
}
else if (*it == QLatin1String("--debug") && ++it != arguments.constEnd())
......@@ -294,10 +286,6 @@ int main(int argc, char *argv[])
delete config_file_ptr;
delete xml_config_file;
delete qApp;
#ifdef Q_OS_WIN32
WSACleanup();
#endif
return 1;
}
......@@ -341,10 +329,6 @@ int main(int argc, char *argv[])
delete qApp;
#endif
#ifdef Q_OS_WIN32
WSACleanup();
#endif
if (measureTime)
{
getTime(&sec, &msec);
......@@ -361,3 +345,13 @@ int main(int argc, char *argv[])
return ret;
}
#if defined(Q_OS_WIN32)
catch (WSAException &)
{
return 2;
}
#endif
catch (...)
{
throw;
}
......@@ -5,6 +5,7 @@ set (os_SRCS
generic/system-info.cpp
generic/url-opener.cpp
generic/window-geometry-manager.cpp
win/wsa-handler.cpp
)
set (os_MOC_SRCS
......
/*
* %kadu copyright begin%
* Copyright 2014 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <exception>
class WSAException : public std::exception
{
virtual const char *what() const noexcept { return "WSA initialization failed"; }
};
/*
* %kadu copyright begin%
* Copyright 2014 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "wsa-handler.h"
#include "os/win/wsa-exception.h"
#ifdef Q_OS_WIN32
#include <winsock2.h>
#endif
WSAHandler::WSAHandler()
{
#ifdef Q_OS_WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
throw WSAException();
#endif
}
WSAHandler::~WSAHandler()
{
#ifdef Q_OS_WIN32
WSACleanup();
#endif
}
/*
* %kadu copyright begin%
* Copyright 2014 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
* %kadu copyright end%
*
* 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) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "exports.h"
class KADUAPI WSAHandler final
{
public:
WSAHandler();
~WSAHandler();
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment