Refactoring. Move the code that gets path to exe file to separate module. DRY.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2019-11-29 14:40:00 +02:00
parent 18284289a0
commit 037ca99280
7 changed files with 109 additions and 156 deletions

View file

@ -40,42 +40,16 @@
#else
# include <QScopeGuard>
#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<char *> (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);

View file

@ -40,10 +40,7 @@
#else
# include <QScopeGuard>
#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<char *> (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)

View file

@ -0,0 +1,63 @@
/************************************************************************
**
** @file appimage.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @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
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "appimage.h"
#include <stdlib.h>
#include <unicode/putil.h>
#include <cstring>
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<char *> (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;
}

33
src/libs/vmisc/appimage.h Normal file
View file

@ -0,0 +1,33 @@
/************************************************************************
**
** @file appimage.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @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
** <https://bitbucket.org/dismine/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef APPIMAGE_H
#define APPIMAGE_H
char* IcuDataPath(const char* correction);
#endif // APPIMAGE_H

View file

@ -49,77 +49,6 @@
# include <unistd.h>
#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/<pid>/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()

View file

@ -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;

View file

@ -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