~rohangarg/grub-customizer/use-pkexec

« back to all changes in this revision

Viewing changes to src/Model/ThemeManager.cpp

  • Committer: daniel
  • Date: 2014-12-05 15:59:27 UTC
  • Revision ID: svn-v4:be7c28b6-f33a-4d9b-8ecf-65c8357c33c1:trunk:736
headerless: combined headers with cpp files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2010-2011 Daniel Richter <danielrichter2007@web.de>
3
 
 *
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.
8
 
 *
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.
13
 
 *
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
17
 
 */
18
 
 
19
 
#include "ThemeManager.h"
20
 
 
21
 
Model_ThemeManager::Model_ThemeManager()
22
 
        : gotSaveErrors(false)
23
 
{}
24
 
 
25
 
void Model_ThemeManager::load() {
26
 
        this->themes.clear();
27
 
        std::string path = this->env->output_config_dir + "/" + "themes";
28
 
 
29
 
        DIR* dir = opendir(path.c_str());
30
 
        if (dir) {
31
 
                struct dirent *entry;
32
 
                struct stat fileProperties;
33
 
                while ((entry = readdir(dir))) {
34
 
                        if (std::string(entry->d_name) == "." || std::string(entry->d_name) == "..") {
35
 
                                continue;
36
 
                        }
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));
40
 
                }
41
 
                closedir(dir);
42
 
        } else {
43
 
                throw FileReadException("cannot read the theme directory: " + path);
44
 
        }
45
 
}
46
 
 
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) {
50
 
                        return *themeIter;
51
 
                }
52
 
        }
53
 
        throw ItemNotFoundException("getTheme: Theme not found: " + name, __FILE__, __LINE__);
54
 
}
55
 
 
56
 
bool Model_ThemeManager::themeExists(std::string const& name) {
57
 
        try {
58
 
                this->getTheme(name);
59
 
                return true;
60
 
        } catch (ItemNotFoundException const& e) {
61
 
        }
62
 
        return false;
63
 
}
64
 
 
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__);
69
 
        }
70
 
        int slashPos = indexFile.find('/', themePath.size() + 1);
71
 
        if (slashPos == -1) {
72
 
                throw InvalidStringFormatException("theme index file path incomplete", __FILE__, __LINE__);
73
 
        }
74
 
 
75
 
        int themeNameSize = slashPos - themePath.size() - 1;
76
 
 
77
 
        return indexFile.substr(themePath.size() + 1, themeNameSize);
78
 
}
79
 
 
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)) {
86
 
                name += "-";
87
 
        }
88
 
        this->themes.push_back(Model_Theme("", fileName, name));
89
 
        return name;
90
 
}
91
 
 
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);
97
 
                        }
98
 
                        this->themes.erase(themeIter);
99
 
                        break;
100
 
                }
101
 
        }
102
 
}
103
 
 
104
 
void Model_ThemeManager::save() {
105
 
        this->saveErrors = "";
106
 
        this->gotSaveErrors = false;
107
 
 
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);
112
 
        }
113
 
        this->removedThemes.clear();
114
 
 
115
 
        bool themesSaved = false;
116
 
        for (std::list<Model_Theme>::iterator themeIter = this->themes.begin(); themeIter != this->themes.end(); themeIter++) {
117
 
                if (themeIter->isModified) {
118
 
                        try {
119
 
                                themeIter->save(dirName);
120
 
                        } catch (Exception const& e) {
121
 
                                this->saveErrors += e + "\n";
122
 
                                this->gotSaveErrors = true;
123
 
                        }
124
 
                        themesSaved = true;
125
 
                }
126
 
        }
127
 
        if (themesSaved) {
128
 
                this->load();
129
 
        }
130
 
}
131
 
 
132
 
std::string Model_ThemeManager::getThemePath() {
133
 
        return this->env->output_config_dir + "/themes";
134
 
}
135
 
 
136
 
bool Model_ThemeManager::hasSaveErrors() {
137
 
        return this->gotSaveErrors;
138
 
}
139
 
 
140
 
std::string Model_ThemeManager::getSaveErrors() {
141
 
        return this->saveErrors;
142
 
}