Skip to content

Commit

Permalink
Merge pull request #5 from merelabs/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
bmin authored Feb 19, 2021
2 parents ac9d648 + 823de4f commit 2d258e4
Show file tree
Hide file tree
Showing 18 changed files with 1,295 additions and 0 deletions.
61 changes: 61 additions & 0 deletions mere-xdg-lib.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
QT = core

CONFIG += c++11
CONFIG += shared

TARGET = mere-xdg
VERSION= 0.0.1
TEMPLATE = lib

DEFINES += LIB_CODE=\\\"xdg\\\"
DEFINES += LIB_NAME=\\\"$$TARGET\\\"
DEFINES += LIB_VERSION=\\\"$$VERSION\\\"
DEFINES += MERE_XDG_LIB

SOURCES += \
src/autostartdirectoryspec.cpp \
src/basedirectoryspec.cpp \
src/desktopentry.cpp \
src/desktopentrydirectoryspec.cpp \
src/desktopentryspec.cpp \
src/icontheme.cpp \
src/iconthemedirectoryspec.cpp \
src/iconthemespec.cpp

HEADERS += \
src/autostartdirectoryspec.h \
src/basedirectoryspec.h \
src/desktopentry.h \
src/desktopentrydirectoryspec.h \
src/desktopentryspec.h \
src/global.h \
src/global.h \
src/icontheme.h \
src/iconthemedirectoryspec.h \
src/iconthemespec.h

INCLUDEPATH += /usr/local/include

LIBS += -L/usr/local/lib

LIBS += -lmere-utils

#
# Install
#
unix {
target.path = /usr/local/lib
INSTALLS += target

INSTALL_PREFIX = /usr/local/include/mere/xdg
for(header, HEADERS) {
sdir = $${dirname(header)}
sdir = $$replace(sdir, "src", "")
path = $${INSTALL_PREFIX}$${sdir}

eval(headers_$${path}.files += $$header)
eval(headers_$${path}.path = $$path)
eval(INSTALLS *= headers_$${path})
}
}

70 changes: 70 additions & 0 deletions src/autostartdirectoryspec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "autostartdirectoryspec.h"
#include "basedirectoryspec.h"
#include "desktopentryspec.h"

#include "mere/utils/envutils.h"
#include "mere/utils/stringutils.h"

#include <QDir>
#include <QDirIterator>

std::vector<std::string> Mere::XDG::AutostartDirectorySpec::directories()
{
std::vector<std::string> autostartDirectories;

std::string userConfigHome = BaseDirectorySpec::configHome();
Mere::Utils::EnvUtils::expandEnvVar(userConfigHome);

if (Mere::Utils::StringUtils::isNotBlank(userConfigHome))
autostartDirectories.push_back( Mere::XDG::AutostartDirectorySpec::directory(userConfigHome) );

const std::vector<std::string> directories = BaseDirectorySpec::configDirectories();
for (std::string directory : directories)
{
Mere::Utils::EnvUtils::expandEnvVar(directory);

if (Mere::Utils::StringUtils::isNotBlank(directory))
autostartDirectories.push_back( Mere::XDG::AutostartDirectorySpec::directory(directory) );
}

return autostartDirectories;
}

std::vector<Mere::XDG::DesktopEntry> Mere::XDG::AutostartDirectorySpec::applications()
{
std::vector<DesktopEntry> desktopEntries ;

std::vector<std::string> directories = AutostartDirectorySpec::directories();

for(const std::string &directory : directories)
{
QDir dir(directory.c_str());

QFileInfoList fileInfoList = dir.entryInfoList(QDir::AllEntries);
QListIterator<QFileInfo> i(fileInfoList);
while (i.hasNext())
{
QFileInfo fileInfo = i.next();

if (fileInfo.isFile() && fileInfo.suffix() == "desktop")
{
DesktopEntry desktopEntry = DesktopEntrySpec::parse(fileInfo);

if (Mere::XDG::DesktopEntrySpec::valid(desktopEntry))
desktopEntries.push_back(desktopEntry);
}
}
}

return desktopEntries;
}

std::string Mere::XDG::AutostartDirectorySpec::directory(const std::string &path)
{
std::string autostartDirectory(path);

if (autostartDirectory[autostartDirectory.length() - 1] != '/')
autostartDirectory.append("/");

return autostartDirectory.append(XDG::AUTOSTART_DIRECTORY);
}
36 changes: 36 additions & 0 deletions src/autostartdirectoryspec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef MERE_XDG_AUTOSTARTDIRECTORYSPEC_H
#define MERE_XDG_AUTOSTARTDIRECTORYSPEC_H

#include "global.h"
#include "desktopentry.h"

namespace Mere
{
namespace XDG
{

static const char* AUTOSTART_DIRECTORY = "autostart";

class MERE_XDG_LIB_SPEC AutostartDirectorySpec
{
AutostartDirectorySpec() = default;
public:
/**
* @brief directories
*
* Directories where system will search for 'Desktop Entry' files, and will be
* automatically launched during startup of the user's desktop environment after the user has logged in.
*
* @return
*/
static std::vector<std::string> directories();
static std::vector<DesktopEntry> applications();

private:
static std::string directory(const std::string &path);
};

}
}

#endif // MERE_XDG_AUTOSTARTDIRECTORYSPEC_H
205 changes: 205 additions & 0 deletions src/basedirectoryspec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#include "basedirectoryspec.h"

#include "mere/utils/envutils.h"
#include "mere/utils/stringutils.h"

#include <sstream>
#include <fstream>
#include <iostream>

#include <QDir>
#include <QDebug>

Mere::XDG::BaseDirectorySpec::BaseDirectorySpec()
{

}

unsigned int Mere::XDG::BaseDirectorySpec::setupEnv()
{
int result = 0;

result += setupDesktopEnv();
result += setupDataHomeEnv();
result += setupConfigHomeEnv();
result += setupIconHomeEnv();
result += setupDataDirsEnv();
result += setupConfigDirsEnv();
result += setupCacheHomeEnv();
result += setupRuntimeDirEnv();

return result;
}

unsigned int Mere::XDG::BaseDirectorySpec::setupEnvVar(const char* env, const char* value, unsigned int err)
{
std::string str(value);
Mere::Utils::EnvUtils::expandEnvVar(str);

int result = setenv(env, str.c_str(), 1);
if (result != 0) return err;

if (!std::ifstream(str).good())
QDir().mkpath(str.c_str());

return 0;
}

unsigned int Mere::XDG::BaseDirectorySpec::setupDesktopEnv()
{
int result = setenv(XDG::SESSION_DESKTOP, XDG::BaseDirectory::SESSION_DESKTOP, 1);
if (result != 0) return XDG::ErrorMask::SESSION_DESKTOP;

result = setenv(XDG::CURRENT_DESKTOP, XDG::BaseDirectory::CURRENT_DESKTOP, 1);
if (result != 0) return XDG::ErrorMask::CURRENT_DESKTOP;

return 0;
}

unsigned int Mere::XDG::BaseDirectorySpec::setupDataHomeEnv()
{
return setupEnvVar(XDG::DATA_HOME, XDG::BaseDirectory::DATA_HOME, XDG::ErrorMask::DATA_HOME);
}

unsigned int Mere::XDG::BaseDirectorySpec::setupConfigHomeEnv()
{
return setupEnvVar(XDG::CONFIG_HOME, XDG::BaseDirectory::CONFIG_HOME, XDG::ErrorMask::CONFIG_HOME);
}

unsigned int Mere::XDG::BaseDirectorySpec::setupIconHomeEnv()
{
return setupEnvVar(XDG::ICON_HOME, XDG::BaseDirectory::ICON_HOME, XDG::ErrorMask::ICON_HOME);
}

unsigned int Mere::XDG::BaseDirectorySpec::setupDataDirsEnv()
{
int result = setenv(XDG::DATA_DIRS, XDG::BaseDirectory::DATA_DIRS, XDG::ErrorMask::DATA_DIRS);
if (result != 0) return 4;

std::string dataDirs(getenv(XDG::DATA_DIRS));

if (!std::ifstream(dataDirs).good())
QDir().mkpath(dataDirs.c_str());

return 0;
}

unsigned int Mere::XDG::BaseDirectorySpec::setupConfigDirsEnv()
{
int result = setenv(XDG::CONFIG_DIRS, XDG::BaseDirectory::CONFIG_DIRS, XDG::ErrorMask::CONFIG_DIRS);
if (result != 0) return 8;

QString configDirs(getenv(XDG::CONFIG_DIRS));

return 0;
}

unsigned int Mere::XDG::BaseDirectorySpec::setupCacheHomeEnv()
{
return BaseDirectorySpec::setupEnvVar(XDG::CACHE_HOME, XDG::BaseDirectory::CACHE_HOME, XDG::ErrorMask::CACHE_HOME);
}

unsigned int Mere::XDG::BaseDirectorySpec::setupRuntimeDirEnv()
{
return BaseDirectorySpec::setupEnvVar(XDG::RUNTIME_DIR, XDG::BaseDirectory::RUNTIME_DIR, XDG::ErrorMask::RUNTIME_DIR);
}

std::string Mere::XDG::BaseDirectorySpec::dataHome()
{
const char *home = getenv(XDG::DATA_HOME);

std::string dataHome(home ? home : XDG::BaseDirectory::DATA_HOME);

Mere::Utils::EnvUtils::expandEnvVar(dataHome);

if (!std::ifstream(dataHome).good())
QDir().mkpath(dataHome.c_str());

return dataHome;
}

std::string Mere::XDG::BaseDirectorySpec::configHome()
{
const char *home = getenv(XDG::CONFIG_HOME);

std::string configHome(home ? home : XDG::BaseDirectory::CONFIG_HOME);

Mere::Utils::EnvUtils::expandEnvVar(configHome);

if (!std::ifstream(configHome).good())
QDir().mkpath(configHome.c_str());

return configHome;
}

std::string Mere::XDG::BaseDirectorySpec::cacheHome()
{
const char *home = getenv(XDG::CACHE_HOME);

std::string cacheHome( home ? home : XDG::BaseDirectory::CACHE_HOME);

Mere::Utils::EnvUtils::expandEnvVar(cacheHome);

if (!std::ifstream(cacheHome).good())
QDir().mkpath(cacheHome.c_str());

return cacheHome;
}

std::string Mere::XDG::BaseDirectorySpec::iconHome()
{
const char *home = getenv(XDG::ICON_HOME);

std::string iconHome( home ? home : XDG::BaseDirectory::ICON_HOME);

Mere::Utils::EnvUtils::expandEnvVar(iconHome);

if (!std::ifstream(iconHome).good())
QDir().mkpath(iconHome.c_str());

return iconHome;
}

std::vector<std::string> Mere::XDG::BaseDirectorySpec::dataDirectories()
{
std::vector<std::string> dirs;

const char *datadirs = getenv(XDG::DATA_DIRS);

std::string dataDirectories(datadirs ? datadirs : XDG::BaseDirectory::DATA_DIRS);

std::string dir;
std::istringstream iss(dataDirectories);
while (std::getline(iss, dir, ':'))
{
Mere::Utils::EnvUtils::expandEnvVar(dir);
if (!std::ifstream(dir).good())
QDir().mkpath(dir.c_str());

dirs.push_back(dir);
}

return dirs;
}

std::vector<std::string> Mere::XDG::BaseDirectorySpec::configDirectories()
{
std::vector<std::string> dirs;

const char *configdirs = getenv(XDG::CONFIG_DIRS);

std::string configDirectories(configdirs ? configdirs : XDG::BaseDirectory::CONFIG_DIRS);

std::string dir;
std::istringstream iss(configDirectories);
while (std::getline(iss, dir, ':'))
{
Mere::Utils::EnvUtils::expandEnvVar(dir);
if (!std::ifstream(dir).good())
QDir().mkpath(dir.c_str());

dirs.push_back(dir);
}

return dirs;
}
Loading

0 comments on commit 2d258e4

Please sign in to comment.