2
* Copyright (C) 2010-2011 Daniel Richter <danielrichter2007@web.de>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 3 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19
#include "ThemeManager.h"
21
Model_ThemeManager::Model_ThemeManager()
22
: gotSaveErrors(false)
25
void Model_ThemeManager::load() {
27
std::string path = this->env->output_config_dir + "/" + "themes";
29
DIR* dir = opendir(path.c_str());
32
struct stat fileProperties;
33
while ((entry = readdir(dir))) {
34
if (std::string(entry->d_name) == "." || std::string(entry->d_name) == "..") {
37
std::string currentFileName = path + "/" + entry->d_name;
38
stat(currentFileName.c_str(), &fileProperties);
39
this->themes.push_back(Model_Theme(currentFileName, "", entry->d_name));
43
throw FileReadException("cannot read the theme directory: " + path);
47
Model_Theme& Model_ThemeManager::getTheme(std::string const& name) {
48
for (std::list<Model_Theme>::iterator themeIter = this->themes.begin(); themeIter != this->themes.end(); themeIter++) {
49
if (themeIter->name == name) {
53
throw ItemNotFoundException("getTheme: Theme not found: " + name, __FILE__, __LINE__);
56
bool Model_ThemeManager::themeExists(std::string const& name) {
60
} catch (ItemNotFoundException const& e) {
65
std::string Model_ThemeManager::extractThemeName(std::string const& indexFile) {
66
std::string themePath = this->env->output_config_dir + "/themes";
67
if (indexFile.substr(0, themePath.size()) != themePath) {
68
throw InvalidStringFormatException("theme index file path must contain '" + themePath + "' given path: '" + indexFile + "'", __FILE__, __LINE__);
70
int slashPos = indexFile.find('/', themePath.size() + 1);
72
throw InvalidStringFormatException("theme index file path incomplete", __FILE__, __LINE__);
75
int themeNameSize = slashPos - themePath.size() - 1;
77
return indexFile.substr(themePath.size() + 1, themeNameSize);
80
std::string Model_ThemeManager::addThemePackage(std::string const& fileName) {
81
int lastSlashPos = fileName.find_last_of('/');
82
std::string name = lastSlashPos != -1 ? fileName.substr(lastSlashPos + 1) : fileName;
83
int firstDotPos = name.find_first_of('.');
84
name = firstDotPos != -1 ? name.substr(0, firstDotPos) : name;
85
while (this->themeExists(name)) {
88
this->themes.push_back(Model_Theme("", fileName, name));
92
void Model_ThemeManager::removeTheme(Model_Theme const& theme) {
93
for (std::list<Model_Theme>::iterator themeIter = this->themes.begin(); themeIter != this->themes.end(); themeIter++) {
94
if (&*themeIter == &theme) {
95
if (themeIter->directory != "") {
96
this->removedThemes.push_back(*themeIter);
98
this->themes.erase(themeIter);
104
void Model_ThemeManager::save() {
105
this->saveErrors = "";
106
this->gotSaveErrors = false;
108
std::string dirName = this->env->output_config_dir + "/themes";
109
mkdir(dirName.c_str(), 0755);
110
for (std::list<Model_Theme>::iterator themeIter = this->removedThemes.begin(); themeIter != this->removedThemes.end(); themeIter++) {
111
themeIter->deleteThemeFiles(dirName);
113
this->removedThemes.clear();
115
bool themesSaved = false;
116
for (std::list<Model_Theme>::iterator themeIter = this->themes.begin(); themeIter != this->themes.end(); themeIter++) {
117
if (themeIter->isModified) {
119
themeIter->save(dirName);
120
} catch (Exception const& e) {
121
this->saveErrors += e + "\n";
122
this->gotSaveErrors = true;
132
std::string Model_ThemeManager::getThemePath() {
133
return this->env->output_config_dir + "/themes";
136
bool Model_ThemeManager::hasSaveErrors() {
137
return this->gotSaveErrors;
140
std::string Model_ThemeManager::getSaveErrors() {
141
return this->saveErrors;