Root/src/surfacecollection.cpp

Source at commit 73ceffa51d3450d2d20ce933e7fc6da187e09fc8 created 7 years 5 months ago.
By Maarten ter Huurne, Fix bug in section directory creation method
1/***************************************************************************
2 * Copyright (C) 2006 by Massimiliano Torromeo *
3 * massimiliano.torromeo@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include "surfacecollection.h"
22#include "surface.h"
23#include "utilities.h"
24#include "debug.h"
25#include "gmenu2x.h"
26
27#include <iostream>
28
29using std::endl;
30using std::string;
31
32SurfaceCollection::SurfaceCollection()
33    : skin("default")
34{
35}
36
37SurfaceCollection::~SurfaceCollection() {}
38
39void SurfaceCollection::setSkin(const string &skin) {
40    this->skin = skin;
41}
42
43/* Returns the location of a skin directory,
44 * from its name given as a parameter. */
45string SurfaceCollection::getSkinPath(const string &skin)
46{
47    string path = GMenu2X::getHome() + "/skins/" + skin;
48    if (fileExists(path))
49      return path;
50
51    path = GMENU2X_SYSTEM_DIR "/skins/" + skin;
52    if (fileExists(path))
53      return path;
54
55    return "";
56}
57
58string SurfaceCollection::getSkinFilePath(const string &file, bool useDefault)
59{
60    /* We first search the skin file on the user-specific directory. */
61    string path = GMenu2X::getHome() + "/skins/" + skin + "/" + file;
62    if (fileExists(path))
63      return path;
64
65    /* If not found, we search that skin file on the system directory. */
66    path = GMENU2X_SYSTEM_DIR "/skins/" + skin + "/" + file;
67    if (fileExists(path))
68      return path;
69
70    /* If it is nowhere to be found, as a last resort we check the
71     * "Default" skin for a corresponding (but probably not similar) file. */
72    if (useDefault) {
73        path = GMenu2X::getHome() + "/skins/Default/" + file;
74        if (fileExists(path))
75          return path;
76
77        path = GMENU2X_SYSTEM_DIR "/skins/Default/" + file;
78        if (fileExists(path))
79          return path;
80    }
81
82    return "";
83}
84
85void SurfaceCollection::debug() {
86    SurfaceHash::iterator end = surfaces.end();
87    for(SurfaceHash::iterator curr = surfaces.begin(); curr != end; curr++){
88        DEBUG("key: %s\n", curr->first.c_str());
89    }
90}
91
92bool SurfaceCollection::exists(const string &path) {
93    return surfaces.find(path) != surfaces.end();
94}
95
96OffscreenSurface *SurfaceCollection::add(const string &path) {
97    if (path.empty()) return NULL;
98    if (exists(path)) del(path);
99    string filePath = path;
100
101    if (filePath.substr(0,5)=="skin:") {
102        filePath = getSkinFilePath(filePath.substr(5,filePath.length()));
103        if (filePath.empty())
104            return NULL;
105    } else if ((filePath.find('#') == filePath.npos) && (!fileExists(filePath))) {
106        WARNING("Unable to add image %s\n", path.c_str());
107        return NULL;
108    }
109
110    DEBUG("Adding surface: '%s'\n", path.c_str());
111    // TODO: Be safe.
112    auto s = OffscreenSurface::loadImage(filePath).release();
113    if (s) {
114        surfaces[path] = s;
115    }
116    return s;
117}
118
119OffscreenSurface *SurfaceCollection::addSkinRes(const string &path, bool useDefault) {
120    if (path.empty()) return NULL;
121    if (exists(path)) del(path);
122
123    string skinpath = getSkinFilePath(path, useDefault);
124    if (skinpath.empty())
125        return NULL;
126
127    DEBUG("Adding skin surface: '%s'\n", path.c_str());
128    // TODO: Be safe.
129    auto s = OffscreenSurface::loadImage(skinpath).release();
130    if (s) {
131        surfaces[path] = s;
132    }
133    return s;
134}
135
136void SurfaceCollection::del(const string &path) {
137    SurfaceHash::iterator i = surfaces.find(path);
138    if (i != surfaces.end()) {
139        delete i->second;
140        surfaces.erase(i);
141    }
142
143    DEBUG("Unloading skin surface: '%s'\n", path.c_str());
144}
145
146void SurfaceCollection::clear() {
147    surfaces.clear();
148}
149
150void SurfaceCollection::move(const string &from, const string &to) {
151    del(to);
152    surfaces[to] = surfaces[from];
153    surfaces.erase(from);
154}
155
156OffscreenSurface *SurfaceCollection::operator[](const string &key) {
157    SurfaceHash::iterator i = surfaces.find(key);
158    if (i == surfaces.end())
159        return add(key);
160    else
161        return i->second;
162}
163
164OffscreenSurface *SurfaceCollection::skinRes(const string &key, bool useDefault) {
165    if (key.empty()) return NULL;
166
167    SurfaceHash::iterator i = surfaces.find(key);
168    if (i == surfaces.end())
169        return addSkinRes(key, useDefault);
170    else
171        return i->second;
172}
173

Archive Download this file



interactive