Root/src/surfacecollection.cpp

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)
59{
60    return SurfaceCollection::getSkinFilePath(skin, file);
61}
62
63string SurfaceCollection::getSkinFilePath(const string &skin, const string &file)
64{
65    /* We first search the skin file on the user-specific directory. */
66    string path = GMenu2X::getHome() + "/skins/" + skin + "/" + file;
67    if (fileExists(path))
68      return path;
69
70    /* If not found, we search that skin file on the system directory. */
71    path = GMENU2X_SYSTEM_DIR "/skins/" + skin + "/" + file;
72    if (fileExists(path))
73      return path;
74
75    return "";
76}
77
78void SurfaceCollection::debug() {
79    SurfaceHash::iterator end = surfaces.end();
80    for(SurfaceHash::iterator curr = surfaces.begin(); curr != end; curr++){
81        DEBUG("key: %s\n", curr->first.c_str());
82    }
83}
84
85bool SurfaceCollection::exists(const string &path) {
86    return surfaces.find(path) != surfaces.end();
87}
88
89Surface *SurfaceCollection::add(Surface *s, const string &path) {
90    if (exists(path)) del(path);
91    surfaces[path] = s;
92    return s;
93}
94
95Surface *SurfaceCollection::add(const string &path) {
96    DEBUG("Adding surface: '%s'\n", path.c_str());
97
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 (!fileExists(filePath)) return NULL;
106
107    Surface *s = new Surface(filePath);
108    surfaces[path] = s;
109    return s;
110}
111
112Surface *SurfaceCollection::addSkinRes(const string &path) {
113    DEBUG("Adding skin surface: '%s'\n", path.c_str());
114
115    if (path.empty()) return NULL;
116    if (exists(path)) del(path);
117
118    string skinpath = getSkinFilePath(path);
119    if (skinpath.empty())
120        return NULL;
121    Surface *s = new Surface(skinpath);
122    if (s != NULL)
123        surfaces[path] = s;
124    return s;
125}
126
127void SurfaceCollection::del(const string &path) {
128    SurfaceHash::iterator i = surfaces.find(path);
129    if (i != surfaces.end()) {
130        free(i->second);
131        surfaces.erase(i);
132    }
133}
134
135void SurfaceCollection::clear() {
136    while (surfaces.size()>0) {
137        surfaces.begin()->second->free();
138        free(surfaces.begin()->second);
139        surfaces.erase(surfaces.begin());
140    }
141}
142
143void SurfaceCollection::move(const string &from, const string &to) {
144    del(to);
145    surfaces[to] = surfaces[from];
146    surfaces.erase(from);
147}
148
149Surface *SurfaceCollection::operator[](const string &key) {
150    SurfaceHash::iterator i = surfaces.find(key);
151    if (i == surfaces.end())
152        return add(key);
153    else
154        return i->second;
155}
156
157Surface *SurfaceCollection::skinRes(const string &key) {
158    if (key.empty()) return NULL;
159
160    SurfaceHash::iterator i = surfaces.find(key);
161    if (i == surfaces.end())
162        return addSkinRes(key);
163    else
164        return i->second;
165}
166

Archive Download this file



interactive