2010-02-04 13:33:47 +02:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2006 by Massimiliano Torromeo *
|
|
|
|
* massimiliano.torromeo@gmail.com *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "surfacecollection.h"
|
2010-07-28 04:08:26 +03:00
|
|
|
#include "surface.h"
|
|
|
|
#include "utilities.h"
|
2010-09-17 23:31:09 +03:00
|
|
|
#include "debug.h"
|
2011-03-29 21:39:41 +03:00
|
|
|
#include "gmenu2x.h"
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2011-03-30 04:21:31 +03:00
|
|
|
#include <iostream>
|
|
|
|
|
2010-07-28 04:08:26 +03:00
|
|
|
using std::endl;
|
|
|
|
using std::string;
|
2010-02-04 13:33:47 +02:00
|
|
|
|
2011-06-02 06:52:47 +03:00
|
|
|
SurfaceCollection::SurfaceCollection()
|
|
|
|
: skin("default")
|
|
|
|
{
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceCollection::~SurfaceCollection() {}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
void SurfaceCollection::setSkin(const string &skin) {
|
2010-02-04 13:33:47 +02:00
|
|
|
this->skin = skin;
|
|
|
|
}
|
|
|
|
|
2011-03-29 22:22:32 +03:00
|
|
|
/* Returns the location of a skin directory,
|
|
|
|
* from its name given as a parameter. */
|
|
|
|
string SurfaceCollection::getSkinPath(const string &skin)
|
|
|
|
{
|
|
|
|
string path = GMenu2X::getHome() + "/skins/" + skin;
|
|
|
|
if (fileExists(path))
|
|
|
|
return path;
|
|
|
|
|
|
|
|
path = GMENU2X_SYSTEM_DIR "/skins/" + skin;
|
|
|
|
if (fileExists(path))
|
|
|
|
return path;
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2011-07-11 14:34:52 +03:00
|
|
|
string SurfaceCollection::getSkinFilePath(const string &file, bool useDefault)
|
2011-03-29 22:10:13 +03:00
|
|
|
{
|
2011-07-11 14:34:52 +03:00
|
|
|
return SurfaceCollection::getSkinFilePath(skin, file, useDefault);
|
2011-03-29 22:10:13 +03:00
|
|
|
}
|
|
|
|
|
2011-07-11 14:34:52 +03:00
|
|
|
string SurfaceCollection::getSkinFilePath(const string &skin, const string &file, bool useDefault)
|
2011-03-29 21:39:41 +03:00
|
|
|
{
|
|
|
|
/* We first search the skin file on the user-specific directory. */
|
|
|
|
string path = GMenu2X::getHome() + "/skins/" + skin + "/" + file;
|
|
|
|
if (fileExists(path))
|
|
|
|
return path;
|
|
|
|
|
|
|
|
/* If not found, we search that skin file on the system directory. */
|
|
|
|
path = GMENU2X_SYSTEM_DIR "/skins/" + skin + "/" + file;
|
|
|
|
if (fileExists(path))
|
2011-07-11 19:14:27 +03:00
|
|
|
return path;
|
|
|
|
|
|
|
|
/* If it is nowhere to be found, as a last resort we check the
|
|
|
|
* "Default" skin on the system directory for a corresponding
|
|
|
|
* (but probably not similar) file. */
|
2011-07-11 14:34:52 +03:00
|
|
|
if (useDefault) {
|
|
|
|
path = GMENU2X_SYSTEM_DIR "/skins/Default/" + file;
|
|
|
|
if (fileExists(path))
|
|
|
|
return path;
|
|
|
|
}
|
2011-03-29 21:39:41 +03:00
|
|
|
|
|
|
|
return "";
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceCollection::debug() {
|
|
|
|
SurfaceHash::iterator end = surfaces.end();
|
|
|
|
for(SurfaceHash::iterator curr = surfaces.begin(); curr != end; curr++){
|
2011-05-11 03:31:41 +03:00
|
|
|
DEBUG("key: %s\n", curr->first.c_str());
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
bool SurfaceCollection::exists(const string &path) {
|
2010-02-04 13:33:47 +02:00
|
|
|
return surfaces.find(path) != surfaces.end();
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
Surface *SurfaceCollection::add(Surface *s, const string &path) {
|
2010-02-04 13:33:47 +02:00
|
|
|
if (exists(path)) del(path);
|
|
|
|
surfaces[path] = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-06-02 06:52:47 +03:00
|
|
|
Surface *SurfaceCollection::add(const string &path) {
|
2011-07-11 14:34:52 +03:00
|
|
|
if (path.empty()) return NULL;
|
2010-02-04 13:33:47 +02:00
|
|
|
if (exists(path)) del(path);
|
|
|
|
string filePath = path;
|
|
|
|
|
|
|
|
if (filePath.substr(0,5)=="skin:") {
|
|
|
|
filePath = getSkinFilePath(filePath.substr(5,filePath.length()));
|
|
|
|
if (filePath.empty())
|
|
|
|
return NULL;
|
|
|
|
} else if (!fileExists(filePath)) return NULL;
|
|
|
|
|
2011-07-11 14:34:52 +03:00
|
|
|
DEBUG("Adding surface: '%s'\n", path.c_str());
|
2011-06-02 23:09:03 +03:00
|
|
|
Surface *s = Surface::loadImage(filePath);
|
|
|
|
if (s != NULL) {
|
|
|
|
surfaces[path] = s;
|
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-07-11 14:34:52 +03:00
|
|
|
Surface *SurfaceCollection::addSkinRes(const string &path, bool useDefault) {
|
2010-02-04 13:33:47 +02:00
|
|
|
if (path.empty()) return NULL;
|
|
|
|
if (exists(path)) del(path);
|
|
|
|
|
2011-07-11 14:34:52 +03:00
|
|
|
string skinpath = getSkinFilePath(path, useDefault);
|
2010-02-04 13:33:47 +02:00
|
|
|
if (skinpath.empty())
|
|
|
|
return NULL;
|
2011-07-11 14:34:52 +03:00
|
|
|
|
|
|
|
DEBUG("Adding skin surface: '%s'\n", path.c_str());
|
2011-06-02 23:09:03 +03:00
|
|
|
Surface *s = Surface::loadImage(skinpath);
|
|
|
|
if (s != NULL) {
|
2010-02-04 13:33:47 +02:00
|
|
|
surfaces[path] = s;
|
2011-06-02 23:09:03 +03:00
|
|
|
}
|
2010-02-04 13:33:47 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
void SurfaceCollection::del(const string &path) {
|
2010-02-04 13:33:47 +02:00
|
|
|
SurfaceHash::iterator i = surfaces.find(path);
|
|
|
|
if (i != surfaces.end()) {
|
2011-07-20 14:43:25 +03:00
|
|
|
delete i->second;
|
2010-02-04 13:33:47 +02:00
|
|
|
surfaces.erase(i);
|
|
|
|
}
|
2011-09-18 14:21:03 +03:00
|
|
|
|
|
|
|
DEBUG("Unloading skin surface: '%s'\n", path.c_str());
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceCollection::clear() {
|
2011-06-02 19:15:15 +03:00
|
|
|
surfaces.clear();
|
2010-02-04 13:33:47 +02:00
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
void SurfaceCollection::move(const string &from, const string &to) {
|
2010-02-04 13:33:47 +02:00
|
|
|
del(to);
|
|
|
|
surfaces[to] = surfaces[from];
|
|
|
|
surfaces.erase(from);
|
|
|
|
}
|
|
|
|
|
2010-05-02 15:29:09 +03:00
|
|
|
Surface *SurfaceCollection::operator[](const string &key) {
|
2010-02-04 13:33:47 +02:00
|
|
|
SurfaceHash::iterator i = surfaces.find(key);
|
|
|
|
if (i == surfaces.end())
|
2011-06-02 06:52:47 +03:00
|
|
|
return add(key);
|
2010-02-04 13:33:47 +02:00
|
|
|
else
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
2011-07-11 14:34:52 +03:00
|
|
|
Surface *SurfaceCollection::skinRes(const string &key, bool useDefault) {
|
2010-02-04 13:33:47 +02:00
|
|
|
if (key.empty()) return NULL;
|
|
|
|
|
|
|
|
SurfaceHash::iterator i = surfaces.find(key);
|
|
|
|
if (i == surfaces.end())
|
2011-07-11 14:34:52 +03:00
|
|
|
return addSkinRes(key, useDefault);
|
2010-02-04 13:33:47 +02:00
|
|
|
else
|
|
|
|
return i->second;
|
|
|
|
}
|