From 3e8f8a8069e2972aa2a2c24de147782417f3758f Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sun, 30 Oct 2016 14:34:55 +0200 Subject: [PATCH] Resolved issue #582. Issue with standard path to shared data on Linux. --HG-- branch : develop --- ChangeLog.txt | 1 + src/libs/ifc/xml/vabstractconverter.cpp | 23 ++++++ src/libs/ifc/xml/vabstractconverter.h | 1 + src/libs/vmisc/vabstractapplication.cpp | 63 ++++++++++++++++ src/libs/vmisc/vabstractapplication.h | 2 + src/libs/vmisc/vcommonsettings.cpp | 99 +++++++++++-------------- src/libs/vmisc/vcommonsettings.h | 12 ++- 7 files changed, 143 insertions(+), 58 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index b3e69e059..63b889358 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -44,6 +44,7 @@ - [#577] 10 new colors added for the lines - [#570] Tiled PDF : Margin values correctly taken into account - [#580] Extend the list of heights. +- [#582] Issue with standard path to shared data on Linux. # Version 0.4.5 October 15, 2016 - [#435] Valentina doesn't change the cursor. diff --git a/src/libs/ifc/xml/vabstractconverter.cpp b/src/libs/ifc/xml/vabstractconverter.cpp index a1692b18a..d2d220cb0 100644 --- a/src/libs/ifc/xml/vabstractconverter.cpp +++ b/src/libs/ifc/xml/vabstractconverter.cpp @@ -51,6 +51,12 @@ VAbstractConverter::VAbstractConverter(const QString &fileName) :VDomDocument(), ver(0x0), fileName(fileName) { + QFileInfo info(fileName); + if (info.isSymLink() && not info.isWritable()) + { + ReplaceSymLink(); + } + this->setXMLContent(fileName); const QString version = GetVersionStr(); ver = GetVersion(version); @@ -187,6 +193,23 @@ void VAbstractConverter::ReserveFile() const } } +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractConverter::ReplaceSymLink() const +{ + // See issue #582. Issue with standard path to shared data on Linux + // https://bitbucket.org/dismine/valentina/issues/582/issue-with-standard-path-to-shared-data-on + QFileInfo info(fileName); + if (info.isSymLink() && not info.isWritable()) + { + QString error; + if (not SafeCopy(info.symLinkTarget(), fileName, error)) + { + const QString errorMsg(tr("Error replacing a synlink by real file: %1.").arg(error)); + throw VException(errorMsg); + } + } +} + //--------------------------------------------------------------------------------------------------------------------- void VAbstractConverter::Replace(QString &formula, const QString &newName, int position, const QString &token, int &bias) const diff --git a/src/libs/ifc/xml/vabstractconverter.h b/src/libs/ifc/xml/vabstractconverter.h index 700fcaa10..9e5b82821 100644 --- a/src/libs/ifc/xml/vabstractconverter.h +++ b/src/libs/ifc/xml/vabstractconverter.h @@ -88,6 +88,7 @@ private: static void ValidateVersion(const QString &version); void ReserveFile() const; + void ReplaceSymLink() const; }; #endif // VABSTRACTCONVERTER_H diff --git a/src/libs/vmisc/vabstractapplication.cpp b/src/libs/vmisc/vabstractapplication.cpp index d6513d8ca..1592325ed 100644 --- a/src/libs/vmisc/vabstractapplication.cpp +++ b/src/libs/vmisc/vabstractapplication.cpp @@ -113,6 +113,18 @@ VAbstractApplication::VAbstractApplication(int &argc, char **argv) // Connect this slot with VApplication::aboutToQuit. Settings()->sync(); }); + +#if !defined(Q_OS_WIN) + QDir standardPath(VCommonSettings::unixStandardSharePath); + const QDir localdata (QDir::homePath() + QDir::separator() + VCommonSettings::valentinaUnixHomeFolder); + if (standardPath.exists() && not localdata.exists()) + { + if (localdata.mkdir(localdata.absolutePath())) + { + SymlinkCopyDirRecursive(standardPath.absolutePath(), localdata.absolutePath(), true); + } + } +#endif // !defined(Q_OS_WIN) } //--------------------------------------------------------------------------------------------------------------------- @@ -382,3 +394,54 @@ void VAbstractApplication::ClearTranslation() delete pmsTranslator; } } + +//--------------------------------------------------------------------------------------------------------------------- +bool VAbstractApplication::SymlinkCopyDirRecursive(const QString &fromDir, const QString toDir, bool replaceOnConflit) +{ + QDir dir; + dir.setPath(fromDir); + + foreach (QString copyFile, dir.entryList(QDir::Files)) + { + const QString from = fromDir + QDir::separator() + copyFile; + const QString to = toDir + QDir::separator() + copyFile; + + if (QFile::exists(to)) + { + if (replaceOnConflit) + { + if (QFile::remove(to) == false) + { + return false; + } + } + else + { + continue; + } + } + + if (QFile::link(from, to) == false) + { + return false; + } + } + + foreach (QString copyDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) + { + const QString from = fromDir + QDir::separator() + copyDir; + const QString to = toDir + QDir::separator() + copyDir; + + if (dir.mkpath(to) == false) + { + return false; + } + + if (SymlinkCopyDirRecursive(from, to, replaceOnConflit) == false) + { + return false; + } + } + + return true; +} diff --git a/src/libs/vmisc/vabstractapplication.h b/src/libs/vmisc/vabstractapplication.h index e311d7615..82f25162c 100644 --- a/src/libs/vmisc/vabstractapplication.h +++ b/src/libs/vmisc/vabstractapplication.h @@ -149,6 +149,8 @@ private: bool openingPattern; void ClearTranslation(); + + static bool SymlinkCopyDirRecursive(const QString &fromDir, const QString toDir, bool replaceOnConflit); }; diff --git a/src/libs/vmisc/vcommonsettings.cpp b/src/libs/vmisc/vcommonsettings.cpp index af84473bb..803b9f063 100644 --- a/src/libs/vmisc/vcommonsettings.cpp +++ b/src/libs/vmisc/vcommonsettings.cpp @@ -75,6 +75,11 @@ const QString VCommonSettings::SettingUserDefinedMaterials = QStringLitera static const QString commonIniFilename = QStringLiteral("common"); +#if !defined(Q_OS_WIN) +const QString VCommonSettings::unixStandardSharePath = QStringLiteral("/usr/share/valentina"); +const QString VCommonSettings::valentinaUnixHomeFolder = QStringLiteral(".valentina"); +#endif + //--------------------------------------------------------------------------------------------------------------------- VCommonSettings::VCommonSettings(Format format, Scope scope, const QString &organization, const QString &application, QObject *parent) @@ -82,13 +87,12 @@ VCommonSettings::VCommonSettings(Format format, Scope scope, const QString &orga {} //--------------------------------------------------------------------------------------------------------------------- -QString VCommonSettings::StandardTablesPath() const +QString VCommonSettings::SharePath(const QString &shareItem) { - const QString stPath = QStringLiteral("/tables/standard"); #ifdef Q_OS_WIN - return QApplication::applicationDirPath() + stPath; + return QApplication::applicationDirPath() + shareItem; #elif defined(Q_OS_MAC) - QDir dirBundle(QApplication::applicationDirPath() + QStringLiteral("/../Resources") + stPath); + QDir dirBundle(QApplication::applicationDirPath() + QStringLiteral("/../Resources") + shareItem); if (dirBundle.exists()) { return dirBundle.absolutePath(); @@ -99,31 +103,59 @@ QString VCommonSettings::StandardTablesPath() const appDir.cdUp(); appDir.cdUp(); appDir.cdUp(); - QDir dir(appDir.absolutePath() + stPath); + QDir dir(appDir.absolutePath() + shareItem); if (dir.exists()) { return dir.absolutePath(); } else { - return QStringLiteral("/usr/share/valentina/tables/standard"); + QDir dir(QDir::homePath() + QDir::separator() + VCommonSettings::valentinaUnixHomeFolder + shareItem); + if (dir.exists()) + { + return dir.absolutePath(); + } + else + { + return VCommonSettings::unixStandardSharePath + shareItem; + } } } #else // Unix - #ifdef QT_DEBUG - return QApplication::applicationDirPath() + stPath; - #else - QDir dir(QApplication::applicationDirPath() + stPath); +#ifdef QT_DEBUG + return QApplication::applicationDirPath() + shareItem; +#else + QDir dir(QApplication::applicationDirPath() + shareItem); + if (dir.exists()) + { + return dir.absolutePath(); + } + else + { + QDir dir(QDir::homePath() + QDir::separator() + VCommonSettings::valentinaUnixHomeFolder + shareItem); if (dir.exists()) { return dir.absolutePath(); } else { - return QStringLiteral("/usr/share/valentina/tables/standard"); + return VCommonSettings::unixStandardSharePath + shareItem; } - #endif + } #endif +#endif +} + +//--------------------------------------------------------------------------------------------------------------------- +QString VCommonSettings::StandardTablesPath() +{ + return SharePath(QStringLiteral("/tables/standard")); +} + +//--------------------------------------------------------------------------------------------------------------------- +QString VCommonSettings::TemplatesPath() +{ + return SharePath(QStringLiteral("/tables/templates")); } //--------------------------------------------------------------------------------------------------------------------- @@ -172,49 +204,6 @@ void VCommonSettings::SetPathTemplate(const QString &value) settings.sync(); } -//--------------------------------------------------------------------------------------------------------------------- -QString VCommonSettings::TemplatesPath() const -{ - const QString stPath = QStringLiteral("/tables/templates"); - const QString unixFullPath = QStringLiteral("/usr/share/valentina/tables/templates"); -#ifdef Q_OS_WIN - return QApplication::applicationDirPath() + stPath; -#elif defined(Q_OS_MAC) - QDir dirBundle(QApplication::applicationDirPath() + QStringLiteral("/../Resources") + stPath); - if (dirBundle.exists()) - { - return dirBundle.absolutePath(); - } - else - { - QDir dir(QApplication::applicationDirPath() + stPath); - if (dir.exists()) - { - return dir.absolutePath(); - } - else - { - return unixFullPath; - } - } -#else // Unix - #ifdef QT_DEBUG - Q_UNUSED(unixFullPath); - return QApplication::applicationDirPath() + stPath; - #else - QDir dir(QApplication::applicationDirPath() + stPath); - if (dir.exists()) - { - return dir.absolutePath(); - } - else - { - return unixFullPath; - } - #endif -#endif -} - //--------------------------------------------------------------------------------------------------------------------- bool VCommonSettings::GetOsSeparator() const { diff --git a/src/libs/vmisc/vcommonsettings.h b/src/libs/vmisc/vcommonsettings.h index 7ce7433c9..2959ab838 100644 --- a/src/libs/vmisc/vcommonsettings.h +++ b/src/libs/vmisc/vcommonsettings.h @@ -47,10 +47,11 @@ class VCommonSettings : public QSettings Q_OBJECT public: VCommonSettings(Format format, Scope scope, const QString &organization, const QString &application = QString(), - QObject *parent = 0); + QObject *parent = nullptr); - QString StandardTablesPath() const; - QString TemplatesPath() const; + static QString SharePath(const QString &shareItem); + static QString StandardTablesPath(); + static QString TemplatesPath(); QString GetPathIndividualMeasurements() const; void SetPathIndividualMeasurements(const QString &value); @@ -125,6 +126,11 @@ public: bool GetForbidWorkpieceFlipping() const; void SetForbidWorkpieceFlipping(bool value); +#if !defined(Q_OS_WIN) + static const QString unixStandardSharePath; + static const QString valentinaUnixHomeFolder; +#endif + private: Q_DISABLE_COPY(VCommonSettings)