Root/
| 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 | |
| 29 | using std::endl; |
| 30 | using std::string; |
| 31 | |
| 32 | SurfaceCollection::SurfaceCollection() |
| 33 | : skin("default") |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | SurfaceCollection::~SurfaceCollection() {} |
| 38 | |
| 39 | void 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. */ |
| 45 | string 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 | |
| 58 | string SurfaceCollection::getSkinFilePath(const string &file, bool useDefault) |
| 59 | { |
| 60 | return SurfaceCollection::getSkinFilePath(skin, file, useDefault); |
| 61 | } |
| 62 | |
| 63 | string SurfaceCollection::getSkinFilePath(const string &skin, const string &file, bool useDefault) |
| 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 | /* If it is nowhere to be found, as a last resort we check the |
| 76 | * "Default" skin for a corresponding (but probably not similar) file. */ |
| 77 | if (useDefault) { |
| 78 | path = GMenu2X::getHome() + "/skins/Default/" + file; |
| 79 | if (fileExists(path)) |
| 80 | return path; |
| 81 | |
| 82 | path = GMENU2X_SYSTEM_DIR "/skins/Default/" + file; |
| 83 | if (fileExists(path)) |
| 84 | return path; |
| 85 | } |
| 86 | |
| 87 | return ""; |
| 88 | } |
| 89 | |
| 90 | void SurfaceCollection::debug() { |
| 91 | SurfaceHash::iterator end = surfaces.end(); |
| 92 | for(SurfaceHash::iterator curr = surfaces.begin(); curr != end; curr++){ |
| 93 | DEBUG("key: %s\n", curr->first.c_str()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | bool SurfaceCollection::exists(const string &path) { |
| 98 | return surfaces.find(path) != surfaces.end(); |
| 99 | } |
| 100 | |
| 101 | Surface *SurfaceCollection::add(Surface *s, const string &path) { |
| 102 | if (exists(path)) del(path); |
| 103 | surfaces[path] = s; |
| 104 | return s; |
| 105 | } |
| 106 | |
| 107 | Surface *SurfaceCollection::add(const string &path) { |
| 108 | if (path.empty()) return NULL; |
| 109 | if (exists(path)) del(path); |
| 110 | string filePath = path; |
| 111 | |
| 112 | if (filePath.substr(0,5)=="skin:") { |
| 113 | filePath = getSkinFilePath(filePath.substr(5,filePath.length())); |
| 114 | if (filePath.empty()) |
| 115 | return NULL; |
| 116 | } else if ((filePath.find('#') == filePath.npos) && (!fileExists(filePath))) { |
| 117 | WARNING("Unable to add image %s\n", path.c_str()); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | DEBUG("Adding surface: '%s'\n", path.c_str()); |
| 122 | Surface *s = Surface::loadImage(filePath); |
| 123 | if (s != NULL) { |
| 124 | surfaces[path] = s; |
| 125 | } |
| 126 | return s; |
| 127 | } |
| 128 | |
| 129 | Surface *SurfaceCollection::addSkinRes(const string &path, bool useDefault) { |
| 130 | if (path.empty()) return NULL; |
| 131 | if (exists(path)) del(path); |
| 132 | |
| 133 | string skinpath = getSkinFilePath(path, useDefault); |
| 134 | if (skinpath.empty()) |
| 135 | return NULL; |
| 136 | |
| 137 | DEBUG("Adding skin surface: '%s'\n", path.c_str()); |
| 138 | Surface *s = Surface::loadImage(skinpath); |
| 139 | if (s != NULL) { |
| 140 | surfaces[path] = s; |
| 141 | } |
| 142 | return s; |
| 143 | } |
| 144 | |
| 145 | void SurfaceCollection::del(const string &path) { |
| 146 | SurfaceHash::iterator i = surfaces.find(path); |
| 147 | if (i != surfaces.end()) { |
| 148 | delete i->second; |
| 149 | surfaces.erase(i); |
| 150 | } |
| 151 | |
| 152 | DEBUG("Unloading skin surface: '%s'\n", path.c_str()); |
| 153 | } |
| 154 | |
| 155 | void SurfaceCollection::clear() { |
| 156 | surfaces.clear(); |
| 157 | } |
| 158 | |
| 159 | void SurfaceCollection::move(const string &from, const string &to) { |
| 160 | del(to); |
| 161 | surfaces[to] = surfaces[from]; |
| 162 | surfaces.erase(from); |
| 163 | } |
| 164 | |
| 165 | Surface *SurfaceCollection::operator[](const string &key) { |
| 166 | SurfaceHash::iterator i = surfaces.find(key); |
| 167 | if (i == surfaces.end()) |
| 168 | return add(key); |
| 169 | else |
| 170 | return i->second; |
| 171 | } |
| 172 | |
| 173 | Surface *SurfaceCollection::skinRes(const string &key, bool useDefault) { |
| 174 | if (key.empty()) return NULL; |
| 175 | |
| 176 | SurfaceHash::iterator i = surfaces.find(key); |
| 177 | if (i == surfaces.end()) |
| 178 | return addSkinRes(key, useDefault); |
| 179 | else |
| 180 | return i->second; |
| 181 | } |
| 182 |
Branches:
install_locations
master
opkrun
packages
