From 037ca992801a192cfce6060fbc5900663293b85a Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 29 Nov 2019 14:40:00 +0200 Subject: [PATCH] Refactoring. Move the code that gets path to exe file to separate module. DRY. --HG-- branch : develop --- src/app/tape/main.cpp | 34 ++------- src/app/valentina/main.cpp | 32 +-------- src/libs/vmisc/appimage.cpp | 63 +++++++++++++++++ src/libs/vmisc/appimage.h | 33 +++++++++ src/libs/vmisc/vabstractapplication.cpp | 91 ------------------------- src/libs/vmisc/vabstractapplication.h | 4 -- src/libs/vmisc/vmisc.pri | 8 ++- 7 files changed, 109 insertions(+), 156 deletions(-) create mode 100644 src/libs/vmisc/appimage.cpp create mode 100644 src/libs/vmisc/appimage.h diff --git a/src/app/tape/main.cpp b/src/app/tape/main.cpp index 9d724774c..16ecd4333 100644 --- a/src/app/tape/main.cpp +++ b/src/app/tape/main.cpp @@ -40,42 +40,16 @@ #else # include #endif -# include "unicode/putil.h" -extern "C" { -# include "../vmisc/binreloc.h" -} +# include "../vmisc/appimage.h" #endif // defined(APPIMAGE) && defined(Q_OS_LINUX) int main(int argc, char *argv[]) { #if defined(APPIMAGE) && defined(Q_OS_LINUX) - /* When deploying with AppImage based on OpenSuse, the ICU library has a hardcoded path to the icudt*.dat file. - * This prevents the library from using shared in memory data. There are few ways to resolve this issue. According - * to documentation we can either use ICU_DATA environment variable or the function u_setDataDirectory(). - */ -// VAbstractApplication::SetICUData(argc, argv); - char *exe_dir = nullptr; - - BrInitError error; - if (br_init (&error)) - { - char *path = br_find_exe_dir(nullptr); - if (path) - { - const char* correction = "/../share/icu"; - exe_dir = static_cast (malloc(strlen(path)+strlen(correction)+1)); - if(exe_dir) - { - strcpy(exe_dir, path); - strcat(exe_dir, correction); - u_setDataDirectory(exe_dir); - } - free(path); - } - } - + /* Fix path to ICU_DATA when run AppImage.*/ + char *exe_dir = IcuDataPath("/../share/icu"); auto FreeMemory = qScopeGuard([exe_dir] {free(exe_dir);}); -#endif +#endif // defined(APPIMAGE) && defined(Q_OS_LINUX) Q_INIT_RESOURCE(tapeicon); Q_INIT_RESOURCE(theme); diff --git a/src/app/valentina/main.cpp b/src/app/valentina/main.cpp index 153dd2b34..806c941ef 100644 --- a/src/app/valentina/main.cpp +++ b/src/app/valentina/main.cpp @@ -40,10 +40,7 @@ #else # include #endif -# include "unicode/putil.h" -extern "C" { -# include "../vmisc/binreloc.h" -} +# include "../vmisc/appimage.h" #endif // defined(APPIMAGE) && defined(Q_OS_LINUX) //--------------------------------------------------------------------------------------------------------------------- @@ -51,31 +48,8 @@ extern "C" { int main(int argc, char *argv[]) { #if defined(APPIMAGE) && defined(Q_OS_LINUX) - /* When deploying with AppImage based on OpenSuse, the ICU library has a hardcoded path to the icudt*.dat file. - * This prevents the library from using shared in memory data. There are few ways to resolve this issue. According - * to documentation we can either use ICU_DATA environment variable or the function u_setDataDirectory(). - */ -// VAbstractApplication::SetICUData(argc, argv); - char *exe_dir = nullptr; - - BrInitError error; - if (br_init (&error)) - { - char *path = br_find_exe_dir(nullptr); - if (path) - { - const char* correction = "/../share/icu"; - exe_dir = static_cast (malloc(strlen(path)+strlen(correction)+1)); - if(exe_dir) - { - strcpy(exe_dir, path); - strcat(exe_dir, correction); - u_setDataDirectory(exe_dir); - } - free(path); - } - } - + /* Fix path to ICU_DATA when run AppImage.*/ + char *exe_dir = IcuDataPath("/../share/icu"); auto FreeMemory = qScopeGuard([exe_dir] {free(exe_dir);}); #endif // defined(APPIMAGE) && defined(Q_OS_LINUX) diff --git a/src/libs/vmisc/appimage.cpp b/src/libs/vmisc/appimage.cpp new file mode 100644 index 000000000..534b4de34 --- /dev/null +++ b/src/libs/vmisc/appimage.cpp @@ -0,0 +1,63 @@ +/************************************************************************ + ** + ** @file appimage.cpp + ** @author Roman Telezhynskyi + ** @date 29 11, 2019 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2019 Valentina project + ** All Rights Reserved. + ** + ** Valentina 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. + ** + ** Valentina 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 Valentina. If not, see . + ** + *************************************************************************/ +#include "appimage.h" + +#include +#include +#include +extern "C" { +#include "binreloc.h" +} + + +/* When deploying with AppImage based on OpenSuse, the ICU library has a hardcoded path to the icudt*.dat file. + * This prevents the library from using shared in memory data. There are few ways to resolve this issue. According + * to documentation we can either use ICU_DATA environment variable or the function u_setDataDirectory(). + */ +char* IcuDataPath(const char* correction) +{ + char * data_path = nullptr; + BrInitError error; + if (br_init (&error)) + { + char *path = br_find_exe_dir(nullptr); + if (path) + { + data_path = static_cast (malloc(strlen(path) + strlen(correction) + 1)); + if(data_path) + { + strcpy(data_path, path); + strcat(data_path, correction); + u_setDataDirectory(data_path); + } + free(path); + } + } + + return data_path; +} diff --git a/src/libs/vmisc/appimage.h b/src/libs/vmisc/appimage.h new file mode 100644 index 000000000..3f88263b2 --- /dev/null +++ b/src/libs/vmisc/appimage.h @@ -0,0 +1,33 @@ +/************************************************************************ + ** + ** @file appimage.h + ** @author Roman Telezhynskyi + ** @date 29 11, 2019 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2019 Valentina project + ** All Rights Reserved. + ** + ** Valentina 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. + ** + ** Valentina 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 Valentina. If not, see . + ** + *************************************************************************/ +#ifndef APPIMAGE_H +#define APPIMAGE_H + +char* IcuDataPath(const char* correction); + +#endif // APPIMAGE_H diff --git a/src/libs/vmisc/vabstractapplication.cpp b/src/libs/vmisc/vabstractapplication.cpp index 598492cbb..f4b9abfa0 100644 --- a/src/libs/vmisc/vabstractapplication.cpp +++ b/src/libs/vmisc/vabstractapplication.cpp @@ -49,77 +49,6 @@ # include #endif -#if defined(APPIMAGE) && defined(Q_OS_LINUX) -# include "unicode/putil.h" -extern "C" { -# include "../vmisc/binreloc.h" -} -#endif - -namespace -{ -#if defined(APPIMAGE) && defined(Q_OS_LINUX) -QString ApplicationFilePath(const int &argc, char **argv) -{ - if (argc) - { - static QByteArray procName = QByteArray(argv[0]); - if (procName != argv[0]) - { - procName = QByteArray(argv[0]); - } - } -#if defined( Q_OS_UNIX ) -# if defined(Q_OS_LINUX) && (!defined(Q_OS_ANDROID) || defined(Q_OS_ANDROID_EMBEDDED)) - // Try looking for a /proc//exe symlink first which points to - // the absolute path of the executable - QFileInfo pfi(QStringLiteral("/proc/%1/exe").arg(getpid())); - if (pfi.exists() && pfi.isSymLink()) - { - return pfi.canonicalFilePath(); - } -# endif - if (argc > 0) - { - QString argv0 = QFile::decodeName(argv[0]); - QString absPath; - if (not argv0.isEmpty() && argv0.at(0) == QLatin1Char('/')) - { - /* - If argv0 starts with a slash, it is already an absolute - file path. - */ - absPath = argv0; - } - else if (argv0.contains(QLatin1Char('/'))) - { - /* - If argv0 contains one or more slashes, it is a file path - relative to the current directory. - */ - absPath = QDir::current().absoluteFilePath(argv0); - } - else - { - /* - Otherwise, the file path has to be determined using the - PATH environment variable. - */ - absPath = QStandardPaths::findExecutable(argv0); - } - absPath = QDir::cleanPath(absPath); - QFileInfo fi(absPath); - if (fi.exists()) - { - return fi.canonicalFilePath(); - } - } -#endif - return QString(); -} -#endif // defined(APPIMAGE) && defined(Q_OS_LINUX) -} - const QString VAbstractApplication::patternMessageSignature = QStringLiteral("[PATTERN MESSAGE]"); //--------------------------------------------------------------------------------------------------------------------- @@ -349,26 +278,6 @@ bool VAbstractApplication::IsPatternMessage(const QString &message) const return VAbstractApplication::ClearMessage(message).startsWith(patternMessageSignature); } -//#if defined(APPIMAGE) && defined(Q_OS_LINUX) -//--------------------------------------------------------------------------------------------------------------------- -//void VAbstractApplication::SetICUData(int &argc, char **argv) -//{ -// /* When deploying with AppImage based on OpenSuse, the ICU library has a hardcoded path to the icudt*.dat file. -// * This prevents the library from using shared in memory data. There are few ways to resolve this issue. According -// * to documentation we can either use ICU_DATA environment variable or the function u_setDataDirectory(). -// */ -//// const QString appDirPath = QFileInfo(ApplicationFilePath(argc, argv)).path(); -//// u_setDataDirectory(QString(appDirPath + QStringLiteral("/../share/icu")).toUtf8().constData()); -// BrInitError error; -// if (br_init (&error)) -// { -// exe_dir = br_find_exe_dir (nullptr); -// u_setDataDirectory(exe_dir); -// } - -//} -//#endif // defined(Q_OS_LINUX) - //--------------------------------------------------------------------------------------------------------------------- #if defined(Q_OS_WIN) void VAbstractApplication::WinAttachConsole() diff --git a/src/libs/vmisc/vabstractapplication.h b/src/libs/vmisc/vabstractapplication.h index 0c7decd76..e09ee2db3 100644 --- a/src/libs/vmisc/vabstractapplication.h +++ b/src/libs/vmisc/vabstractapplication.h @@ -129,10 +129,6 @@ public: static const QString patternMessageSignature; bool IsPatternMessage(const QString &message) const; -//#if defined(APPIMAGE) && defined(Q_OS_LINUX) -// static void SetICUData(int &argc, char ** argv); -//#endif - protected: QUndoStack *undoStack; diff --git a/src/libs/vmisc/vmisc.pri b/src/libs/vmisc/vmisc.pri index 82ed45e3f..72704a513 100644 --- a/src/libs/vmisc/vmisc.pri +++ b/src/libs/vmisc/vmisc.pri @@ -18,7 +18,9 @@ SOURCES += \ *msvc*:SOURCES += $$PWD/stable.cpp contains(DEFINES, APPIMAGE) { - SOURCES += $$PWD/binreloc.c + SOURCES += \ + $$PWD/binreloc.c \ + $$PWD/appimage.cpp } HEADERS += \ @@ -50,7 +52,9 @@ HEADERS += \ $$PWD/backport/qscopeguard.h contains(DEFINES, APPIMAGE) { - SOURCES += $$PWD/binreloc.h + SOURCES += \ + $$PWD/binreloc.h \ + $$PWD/appimage.h } # Qt's versions