Merge pull request #42 from QuasarApp/platformutils

added support snap platform
This commit is contained in:
Andrei Yankovich 2024-01-21 11:03:51 +01:00 committed by GitHub
commit 10d0361ae1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 153 additions and 0 deletions

53
qaplatformutils.h Normal file
View File

@ -0,0 +1,53 @@
/*
* Copyright (C) 2024-2024 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#ifndef PLATFORMUTILS_H
#define PLATFORMUTILS_H
#include <QString>
#include "quasarapp_global.h"
namespace QuasarAppUtils {
/**
* @brief The iPlatformUtils class bas interface for get access platform dependet functions and constants.
*/
class QUASARAPPSHARED_EXPORT PlatformUtils
{
public:
PlatformUtils();
/**
* @brief isSnap This method check destribution type. true if the application packed as a snap.
* @return true if the application packaged as a snap else false.
*/
static bool isSnap();
/**
* @brief snapRootFS returns root file system of main OS (available read only)
* @return path ro main os root.
*/
static QString snapRootFS();
/**
* @brief transportPathToSnapRoot change path to snap avaialble.
* @param path source path of main OS.
* @return readable path location for snap pacakge.
* @note this method is not a fast, so not invoke it too offten.
*/
static QString transportPathToSnapRoot(const QString &path);
/**
* @brief checkSystemBakupSnapInterface This method check access to bakcup permision of snap pacakge.
* This permision required to get access to rootfs of main OS.
* @return true if the app have this accesss.
*/
static bool checkSystemBakupSnapInterface();
};
}
#endif // IPLATFORMUTILS_H

99
qaplatformutils_linux.cpp Normal file
View File

@ -0,0 +1,99 @@
/*
* Copyright (C) 2024-2024 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "qaplatformutils.h"
#if defined(Q_OS_LINUX)
#include <QDir>
#include <QFileInfo>
#include <QProcessEnvironment>
#elif defined(Q_OS_ANDROID)
#elif defined(Q_OS_WIN32)
#elif defined(Q_OS_MACOS)
#elif defined(Q_OS_IOS)
#endif
namespace QuasarAppUtils {
#if defined(Q_OS_LINUX)
bool PlatformUtils::isSnap() {
return QProcessEnvironment::systemEnvironment().value("SNAP").size();
}
QString PlatformUtils::snapRootFS() {
return "/var/lib/snapd/hostfs";
}
QString PlatformUtils::transportPathToSnapRoot(const QString &path) {
if (isSnap() && checkSystemBakupSnapInterface()) {
if(QFileInfo(path).isWritable()) {
return path;
}
if (path.size() && path[0] != QString("/")) {
auto absalutPath = QProcessEnvironment::systemEnvironment().value("PWD") + "/" + path;
if (!absalutPath.contains(snapRootFS())) {
return snapRootFS() + "/" + absalutPath;
}
}
if (!path.contains(snapRootFS())) {
return snapRootFS() + "/" + path;
}
}
return path;
}
bool PlatformUtils::checkSystemBakupSnapInterface() {
return QDir(snapRootFS()).entryList(QDir::AllEntries | QDir::NoDotAndDotDot).size();
}
#elif defined(Q_OS_ANDROID)
bool PlatformUtils::isSnap() { return false;}
QString PlatformUtils::snapRootFS() { return "";}
QString PlatformUtils::transportPathToSnapRoot(const QString &path) { return path; }
bool PlatformUtils::checkSystemBakupSnapInterface() { return false; }
#elif defined(Q_OS_WIN32)
bool PlatformUtils::isSnap() { return false;}
QString PlatformUtils::snapRootFS() { return "";}
QString PlatformUtils::transportPathToSnapRoot(const QString &path) { return path; }
bool PlatformUtils::checkSystemBakupSnapInterface() { return false; }
#elif defined(Q_OS_MACOS)
bool PlatformUtils::isSnap() { return false;}
QString PlatformUtils::snapRootFS() { return "";}
QString PlatformUtils::transportPathToSnapRoot(const QString &path) { return path; }
bool PlatformUtils::checkSystemBakupSnapInterface() { return false; }
#elif defined(Q_OS_IOS)
bool PlatformUtils::isSnap() { return false;}
QString PlatformUtils::snapRootFS() { return "";}
QString PlatformUtils::transportPathToSnapRoot(const QString &path) { return path; }
bool PlatformUtils::checkSystemBakupSnapInterface() { return false; }
#endif
}

View File

@ -11,6 +11,7 @@
#include "params.h"
#include "locales.h"
#include "settings.h"
#include "qaplatformutils.h"
/**
* @brief The QuasaraAppUtils class