vdr-plugin-skindesigner/extensions/skinrepo.c

376 lines
11 KiB
C
Raw Normal View History

2015-05-30 16:43:59 +02:00
#include <iostream>
#include <fstream>
2015-05-22 13:35:01 +02:00
#include "skinrepo.h"
2016-01-26 18:32:38 +01:00
#include "helpers.h"
2015-05-22 13:35:01 +02:00
using namespace std;
// --- cSkinRepo -------------------------------------------------------------
cSkinRepo::cSkinRepo(void) {
name = "";
repoType = rtUndefined;
2015-05-30 16:43:59 +02:00
action = eaUndefined;
2015-05-22 13:35:01 +02:00
url = "";
2015-05-30 16:43:59 +02:00
author = "unknown";
minSDVersion = "0.0.1";
2015-05-22 13:35:01 +02:00
command = "";
2015-05-30 16:43:59 +02:00
command2 = "";
2015-05-22 13:35:01 +02:00
tempfile = "";
result = -1;
2015-05-30 16:43:59 +02:00
skinPath = "";
themesPath = "";
2015-05-22 13:35:01 +02:00
}
cSkinRepo::~cSkinRepo() {
}
bool cSkinRepo::Valid(void) {
if (!name.size())
return false;
if (repoType == rtUndefined)
return false;
if (!url.size())
return false;
return true;
}
2015-05-30 16:43:59 +02:00
void cSkinRepo::Install(string path, string themesPath) {
2015-05-22 13:35:01 +02:00
if (Running())
return;
2015-05-30 16:43:59 +02:00
action = eaInstall;
this->skinPath = path + name;
this->themesPath = themesPath;
2015-05-22 13:35:01 +02:00
if (repoType == rtGit) {
command = *cString::sprintf("git clone --depth=1 --progress %s %s", url.c_str(), skinPath.c_str());
2015-05-22 13:35:01 +02:00
tempfile = *cString::sprintf("gitclone_%s_%ld.out", name.c_str(), time(0));
dsyslog("skindesigner: installing skin from Git, command: %s, logfile: %s", command.c_str(), tempfile.c_str());
2015-05-22 13:35:01 +02:00
Start();
2015-05-30 16:43:59 +02:00
} else if (repoType == rtZipUrl) {
size_t hit = url.find_last_of('/');
if (hit == string::npos)
return;
string filename = url.substr(hit+1);
command = *cString::sprintf("wget -P /tmp/ %s", url.c_str());
command2 = *cString::sprintf("unzip /tmp/%s -d %s", filename.c_str(), path.c_str());
dsyslog("skindesigner: installing skin from Zip, command: %s, %s", command.c_str(), command2.c_str());
2015-05-30 16:43:59 +02:00
Start();
}
}
void cSkinRepo::Update(string path) {
if (Running())
return;
action = eaUpdate;
this->skinPath = path + name;
if (repoType == rtGit) {
command = *cString::sprintf("cd %s; git pull", skinPath.c_str());
tempfile = *cString::sprintf("gitpull_%s_%ld.out", name.c_str(), time(0));
dsyslog("skindesigner: updating skin from Git, command: %s, logfile: /tmp/%s", command.c_str(), tempfile.c_str());
2015-05-30 16:43:59 +02:00
Start();
2015-05-22 13:35:01 +02:00
} else if (repoType == rtZipUrl) {
//TODO
}
}
void cSkinRepo::Action(void) {
if (command.size() < 1)
return;
if (tempfile.size() > 0) {
command = *cString::sprintf("%s > /tmp/%s 2>&1", command.c_str(), tempfile.c_str());
}
2015-05-30 16:43:59 +02:00
2015-05-22 13:35:01 +02:00
result = system (command.c_str());
2015-05-30 16:43:59 +02:00
if (result == 0 && command2.size() > 0) {
result = system (command2.c_str());
}
if (result == 0) {
if (action == eaInstall)
CreateThemeFiles();
dsyslog("skindesigner: %s successfully executed", command.c_str());
} else {
esyslog("skindesigner: ERROR executing %s", command.c_str());
2015-05-30 16:43:59 +02:00
}
}
void cSkinRepo::CreateThemeFiles(void) {
string availableThemesPath = skinPath + "/themes/";
DIR *folder = NULL;
struct dirent *dirEntry;
folder = opendir(availableThemesPath.c_str());
if (!folder) {
return;
}
vector<string> skinThemes;
while (dirEntry = readdir(folder)) {
string dirEntryName = dirEntry->d_name;
int dirEntryType = dirEntry->d_type;
if (!dirEntryName.compare(".") || !dirEntryName.compare("..") || dirEntryType != DT_DIR)
continue;
skinThemes.push_back(dirEntryName);
}
for (vector<string>::iterator it = skinThemes.begin(); it != skinThemes.end(); it++) {
string themeName = *it;
string themeFileName = themesPath;
themeFileName += name + "-" + themeName + ".theme";
if (FileExists(themeFileName)) {
continue;
}
ofstream themeFile (themeFileName.c_str());
if (themeFile.is_open()) {
themeFile << "Description = ";
themeFile << themeName << "\n";
themeFile.close();
}
}
}
bool cSkinRepo::SuccessfullyUpdated(void) {
string logfilePath = "/tmp/" + tempfile;
bool updated = true;
string line;
ifstream logfile(logfilePath.c_str());
if (logfile.is_open()) {
while ( getline (logfile, line) ) {
if (line.find("up-to-date") != string::npos) {
updated = false;
break;
}
}
logfile.close();
}
return updated;
2015-05-22 13:35:01 +02:00
}
void cSkinRepo::Debug() {
string strRepoType = "Undefined";
if (repoType == rtGit)
strRepoType = "Git";
else if (repoType == rtZipUrl)
strRepoType = "ZipUrl";
dsyslog("skindesigner: --- skinrepo %s, Type %s ---", name.c_str(), strRepoType.c_str());
dsyslog("skindesigner: url %s", url.c_str());
2015-05-30 16:43:59 +02:00
dsyslog("skindesigner: author %s", author.c_str());
dsyslog("skindesigner: minimum Skindesigner Version required %s", minSDVersion.c_str());
2015-05-22 13:35:01 +02:00
if (specialFonts.size() > 0) {
for (vector<string>::iterator it = specialFonts.begin(); it != specialFonts.end(); it++) {
dsyslog("skindesigner: special font %s", (*it).c_str());
}
}
if (supportedPlugins.size() > 0) {
for (vector<string>::iterator it = supportedPlugins.begin(); it != supportedPlugins.end(); it++) {
dsyslog("skindesigner: supported plugin %s", (*it).c_str());
}
}
if (screenshots.size() > 0) {
for (vector<pair<string,string> >::iterator it = screenshots.begin(); it != screenshots.end(); it++) {
string desc = (it->first).c_str();
string url = (it->second).c_str();
dsyslog("skindesigner: screenshot \"%s\", url %s", desc.c_str(), url.c_str());
}
}
}
// --- cSkinRepos -------------------------------------------------------------
cSkinRepos::cSkinRepos(void) {
skinRepoUrl = "https://github.com/louisbraun/skinrepository.git";
repoFolder = "skinrepositories/";
2015-05-22 13:35:01 +02:00
}
cSkinRepos::~cSkinRepos() {
for (vector<cSkinRepo*>::iterator it = repos.begin(); it != repos.end(); it++) {
delete (*it);
}
}
void cSkinRepos::Init(string path) {
string repoPath = path + repoFolder;
if (FolderExists(repoPath)) {
PullRepoGit(repoPath);
} else {
InitRepoGit(repoPath);
2015-05-22 13:35:01 +02:00
}
}
2015-05-22 13:35:01 +02:00
void cSkinRepos::Read(string path) {
string repoPath = path + repoFolder;
DIR *folder = NULL;
struct dirent *dirEntry;
folder = opendir(repoPath.c_str());
if (!folder) {
esyslog("skindesigner: no skinrepository folder available in %s", repoPath.c_str());
2015-05-22 13:35:01 +02:00
return;
}
while (dirEntry = readdir(folder)) {
string fileName = dirEntry->d_name;
if (!fileName.compare(".") || !fileName.compare("..") || !fileName.compare(".git"))
2015-05-22 13:35:01 +02:00
continue;
string filePath = repoPath + fileName;
if (! ReadXMLFile(filePath.c_str(), false) ) {
esyslog("skindesigner: error reading skinrepo %s", filePath.c_str());
2015-05-22 13:35:01 +02:00
continue;
}
if (! SetDocument() )
continue;
if (!ParseRepository())
esyslog("skindesigner: error parsing skinrepository %s", filePath.c_str());
DeleteDocument();
2015-05-22 13:35:01 +02:00
}
}
cSkinRepo *cSkinRepos::GetRepo(string name) {
for (vector<cSkinRepo*>::iterator it = repos.begin(); it != repos.end(); it++) {
cSkinRepo *repo = (*it);
if (!name.compare(repo->Name()))
return repo;
}
return NULL;
}
2015-05-30 16:43:59 +02:00
cSkinRepo *cSkinRepos::GetNextRepo(void) {
if (repoIt == repos.end())
return NULL;
cSkinRepo *repo = *repoIt;
repoIt++;
return repo;
}
2015-05-22 13:35:01 +02:00
void cSkinRepos::Debug(void) {
for (vector<cSkinRepo*>::iterator it = repos.begin(); it != repos.end(); it++) {
(*it)->Debug();
}
}
bool cSkinRepos::ParseRepository(void) {
if (!LevelDown())
return false;
2015-05-22 13:35:01 +02:00
cSkinRepo *repo = new cSkinRepo();
do {
string value = "";
if (CheckNodeName("name")) {
if (GetNodeValue(value)) {
repo->SetName(value);
}
} else if (CheckNodeName("type")) {
if (GetNodeValue(value)) {
2015-05-22 13:35:01 +02:00
eRepoType repoType = rtUndefined;
if (!value.compare("git"))
2015-05-22 13:35:01 +02:00
repoType = rtGit;
else if (!value.compare("zip"))
2015-05-22 13:35:01 +02:00
repoType = rtZipUrl;
repo->SetRepoType(repoType);
}
} else if (CheckNodeName("url")) {
if (GetNodeValue(value)) {
repo->SetUrl(value);
}
} else if (CheckNodeName("author")) {
if (GetNodeValue(value)) {
repo->SetAuthor(value);
2015-05-22 13:35:01 +02:00
}
} else if (CheckNodeName("minimumskindesignerversion")) {
if (GetNodeValue(value)) {
repo->SetMinSDVersion(value);
}
} else if (CheckNodeName("specialfonts")) {
if (!LevelDown())
continue;
do {
if (CheckNodeName("font")) {
if (GetNodeValue(value)) {
repo->SetSpecialFont(value);
}
2015-05-22 13:35:01 +02:00
}
} while (NextNode());
LevelUp();
} else if (CheckNodeName("supportedplugins")) {
if (!LevelDown())
continue;
do {
if (CheckNodeName("plugin")) {
if (GetNodeValue(value)) {
repo->SetSupportedPlugin(value);
2015-05-22 13:35:01 +02:00
}
}
} while (NextNode());
LevelUp();
} else if (CheckNodeName("screenshots")) {
if (!LevelDown()) {
continue;
2015-05-22 13:35:01 +02:00
}
do {
if (CheckNodeName("screenshot")) {
if (!LevelDown()) {
continue;
}
2015-05-22 13:35:01 +02:00
string desc = "";
string url = "";
do {
if (CheckNodeName("description")) {
GetNodeValue(desc);
} else if (CheckNodeName("url")) {
GetNodeValue(url);
2015-05-22 13:35:01 +02:00
}
} while (NextNode());
LevelUp();
if (desc.size() && url.size())
repo->SetScreenshot(desc, url);
2015-05-22 13:35:01 +02:00
}
} while (NextNode());
LevelUp();
2015-05-22 13:35:01 +02:00
}
} while (NextNode());
LevelUp();
if (repo->Valid()) {
repos.push_back(repo);
return true;
}
return false;
}
2015-05-22 13:35:01 +02:00
void cSkinRepos::InitRepoGit(string path) {
dsyslog("skindesigner: initiating skin repository %s", path.c_str());
CreateFolder(path);
cString command = cString::sprintf("git clone --depth=1 %s %s", skinRepoUrl.c_str(), path.c_str());
int result = system (*command);
if (result == 0) {
dsyslog("skindesigner: skinrepository successfully initiated");
} else {
esyslog("skindesigner: ERROR initiating skinrepository. Command: %s", *command);
2015-05-22 13:35:01 +02:00
}
}
void cSkinRepos::PullRepoGit(string path) {
dsyslog("skindesigner: updating skin repository %s", path.c_str());
cString command = *cString::sprintf("cd %s; git pull", path.c_str());
int result = system (*command);
if (result == 0) {
dsyslog("skindesigner: skinrepository successfully updated");
} else {
esyslog("skindesigner: ERROR updating skinrepository. Command: %s", *command);
}
}