diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,6 +85,10 @@ include (CheckIncludeFile) check_include_file("sys/types.h" HAVE_SYS_TYPES_H) check_include_file("unistd.h" HAVE_UNISTD_H) +check_include_file("pwd.h" HAVE_PWD_H) +check_include_file("windows.h" HAVE_WINDOWS_H) +check_include_file("lmcons.h" HAVE_LMCONS_H) +check_include_file("process.h" HAVE_PROCESS_H) # include check for members in structs include (CheckStructHasMember) diff --git a/kmymoney/CMakeLists.txt b/kmymoney/CMakeLists.txt --- a/kmymoney/CMakeLists.txt +++ b/kmymoney/CMakeLists.txt @@ -74,6 +74,7 @@ kmm_widgets kmm_mymoney kmm_utils_webconnect + kmm_utils_platformtools ) # must build kmymoney/transactionsortoption.h diff --git a/kmymoney/dialogs/CMakeLists.txt b/kmymoney/dialogs/CMakeLists.txt --- a/kmymoney/dialogs/CMakeLists.txt +++ b/kmymoney/dialogs/CMakeLists.txt @@ -94,6 +94,7 @@ kmm_mymoney onlinetask_interfaces kmm_widgets + kmm_utils_platformtools ) target_link_libraries(dialogs LINK_PUBLIC diff --git a/kmymoney/dialogs/kgeneratesqldlg.cpp b/kmymoney/dialogs/kgeneratesqldlg.cpp --- a/kmymoney/dialogs/kgeneratesqldlg.cpp +++ b/kmymoney/dialogs/kgeneratesqldlg.cpp @@ -19,9 +19,7 @@ // ---------------------------------------------------------------------------- // System includes -#include #include -#include // ---------------------------------------------------------------------------- // QT Includes @@ -49,6 +47,7 @@ #include "storage/mymoneystoragesql.h" #include "storage/mymoneyseqaccessmgr.h" #include "kguiutils.h" +#include "misc/platformtools.h" KGenerateSqlDlg::KGenerateSqlDlg(QWidget *) { @@ -267,9 +266,7 @@ m_widget->textDbName->setText("KMyMoney"); m_widget->textHostName->setText("localhost"); m_widget->textUserName->setText(""); - struct passwd * pwd = getpwuid(geteuid()); - if (pwd != nullptr) - m_widget->textUserName->setText(QString(pwd->pw_name)); + m_widget->textUserName->setText(platformTools::getOSUsername()); m_widget->textPassword->setText(""); } diff --git a/kmymoney/dialogs/kselectdatabasedlg.cpp b/kmymoney/dialogs/kselectdatabasedlg.cpp --- a/kmymoney/dialogs/kselectdatabasedlg.cpp +++ b/kmymoney/dialogs/kselectdatabasedlg.cpp @@ -17,9 +17,7 @@ #include "kselectdatabasedlg.h" -#include #include -#include // ---------------------------------------------------------------------------- // QT Includes @@ -38,6 +36,7 @@ #include "ui_kselectdatabasedlg.h" #include "kguiutils.h" #include "storage/mymoneystoragesql.h" +#include "misc/platformtools.h" KSelectDatabaseDlg::KSelectDatabaseDlg(int openMode, QUrl openURL, QWidget *) : m_widget(new Ui::KSelectDatabaseDlg()) @@ -118,9 +117,7 @@ m_widget->textDbName->setText(QLatin1String("KMyMoney")); m_widget->textHostName->setText(QLatin1String("localhost")); m_widget->textUserName->setText(QString()); - struct passwd * pwd = getpwuid(geteuid()); - if (pwd != 0) - m_widget->textUserName->setText(QString(pwd->pw_name)); + m_widget->textUserName->setText(platformTools::getOSUsername()); m_widget->textPassword->setText(QString()); connect(m_widget->databaseTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(slotDriverSelected(int))); m_widget->checkPreLoad->setChecked(false); diff --git a/kmymoney/kmymoney.cpp b/kmymoney/kmymoney.cpp --- a/kmymoney/kmymoney.cpp +++ b/kmymoney/kmymoney.cpp @@ -20,18 +20,6 @@ #include "kmymoney.h" -// for _getpid -#ifdef Q_OS_WIN32 //krazy:exclude=cpp -#include -#else -#if HAVE_SYS_TYPES_H -#include -#endif -#if HAVE_UNISTD_H -#include -#endif -#endif - // ---------------------------------------------------------------------------- // Std C++ / STL Includes @@ -168,6 +156,8 @@ #include "storageenums.h" #include "mymoneyenums.h" +#include "misc/platformtools.h" + using namespace Icons; static constexpr char recoveryKeyId[] = "59B0F826D2B08440"; @@ -6837,11 +6827,7 @@ // please change this method of creating a list of 'all the other kmymoney instances that are running on the system' // since assuming that D-Bus creates service names with org.kde.kmymoney-PID is an observation I don't think that it's documented somwhere if ((*it).indexOf("org.kde.kmymoney-") == 0) { -#ifdef _MSC_VER - uint thisProcPid = _getpid(); -#else - uint thisProcPid = getpid(); -#endif + uint thisProcPid = platformTools::getProcessId(); if ((*it).indexOf(QString("org.kde.kmymoney-%1").arg(thisProcPid)) != 0) list += (*it); } diff --git a/kmymoney/misc/CMakeLists.txt b/kmymoney/misc/CMakeLists.txt --- a/kmymoney/misc/CMakeLists.txt +++ b/kmymoney/misc/CMakeLists.txt @@ -10,6 +10,16 @@ webconnect.cpp ) +if( ${HAVE_UNISTD_H} AND ${HAVE_PWD_H} ) + set( kmm_utils_platformtools_SRCS + platformtools_gnu.cpp + ) +elseif( ${HAVE_WINDOWS_H} AND ${HAVE_LMCONS_H} AND ${HAVE_PROCESS_H} ) + set( kmm_utils_platformtools_SRCS + platformtools_nognu.cpp + ) +endif() + set( kmm_utils_validators_HEADER charvalidator.h validators.h @@ -19,8 +29,13 @@ webconnect.h ) +set( kmm_utils_platformtools_HEADER + platformtools.h +) + add_library(kmm_utils_validators STATIC ${kmm_utils_validators_SRCS}) add_library(kmm_utils_webconnect STATIC ${kmm_utils_webconnect_SRCS}) +add_library(kmm_utils_platformtools STATIC ${kmm_utils_platformtools_SRCS}) target_link_libraries( kmm_utils_validators PUBLIC @@ -31,3 +46,8 @@ PUBLIC Qt5::Core ) + +target_link_libraries( kmm_utils_platformtools + PUBLIC + Qt5::Core +) diff --git a/kmymoney/misc/platformtools.h b/kmymoney/misc/platformtools.h new file mode 100644 --- /dev/null +++ b/kmymoney/misc/platformtools.h @@ -0,0 +1,42 @@ +/* + * This file is part of KMyMoney, A Personal Finance Manager by KDE + * Copyright (C) 2017 Marc Hübner + * + * 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 . + */ + +#ifndef PLATFORMTOOLS_H +#define PLATFORMTOOLS_H + +#include + +class QString; + + +namespace platformTools +{ + + /** + * This function returns the os username of the user account + * under which the application is being run. + */ + const QString getOSUsername(); + + /** + * This function returns the PID associated with the current process. + */ + uint getProcessId(); +}; + +#endif // PLATFORMTOOLS_H diff --git a/kmymoney/misc/platformtools_gnu.cpp b/kmymoney/misc/platformtools_gnu.cpp new file mode 100644 --- /dev/null +++ b/kmymoney/misc/platformtools_gnu.cpp @@ -0,0 +1,39 @@ +/* + * This file is part of KMyMoney, A Personal Finance Manager by KDE + * Copyright (C) 2017 Marc Hübner + * + * 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 . + */ + +#include "platformtools.h" + +#include +#include + +#include + +const QString platformTools::getOSUsername() +{ + QString name; + struct passwd* pwd = getpwuid(geteuid()); + if( pwd != nullptr){ + name = QString::fromLatin1(pwd->pw_name); + } + return name; +} + +uint platformTools::getProcessId() +{ + return getpid(); +} diff --git a/kmymoney/misc/platformtools_nognu.cpp b/kmymoney/misc/platformtools_nognu.cpp new file mode 100644 --- /dev/null +++ b/kmymoney/misc/platformtools_nognu.cpp @@ -0,0 +1,41 @@ +/* + * This file is part of KMyMoney, A Personal Finance Manager by KDE + * Copyright (C) 2017 Marc Hübner + * + * 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 . + */ + +#include "platformtools.h" + +#include +#include +#include + +#include + +const QString platformTools::getOSUsername() +{ + QString name; + DWORD size = UNLEN+1; + wchar_t wcname[UNLEN+1]; + if(GetUserNameW((LPWSTR) wcname, &size)){ + name = QString::fromWCharArray(wcname); + } + return name; +} + +uint platformTools::getProcessId() +{ + return _getpid(); +} diff --git a/kmymoney/mymoney/storage/tests/CMakeLists.txt b/kmymoney/mymoney/storage/tests/CMakeLists.txt --- a/kmymoney/mymoney/storage/tests/CMakeLists.txt +++ b/kmymoney/mymoney/storage/tests/CMakeLists.txt @@ -17,4 +17,5 @@ kmm_testutils reports_testcommon onlinetask_unavailabletask + kmm_utils_platformtools ) diff --git a/kmymoney/mymoney/storage/tests/mymoneydatabasemgr-test.cpp b/kmymoney/mymoney/storage/tests/mymoneydatabasemgr-test.cpp --- a/kmymoney/mymoney/storage/tests/mymoneydatabasemgr-test.cpp +++ b/kmymoney/mymoney/storage/tests/mymoneydatabasemgr-test.cpp @@ -15,8 +15,6 @@ ***************************************************************************/ #include "mymoneydatabasemgr-test.h" -#include -#include #include #include @@ -25,6 +23,7 @@ #include "mymoneyfile.h" #include "onlinetasks/dummy/tasks/dummytask.h" +#include "misc/platformtools.h" #include "mymoneyenums.h" @@ -108,11 +107,7 @@ void MyMoneyDatabaseMgrTest::setupUrl(const QString& fname) { - struct passwd * pwd = getpwuid(geteuid()); - QString m_userName; - if (pwd != 0) { - m_userName = QString(pwd->pw_name); - } + QString m_userName = platformTools::getOSUsername(); QString m_mode = //"QPSQL&mode=single";