libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YIconLoader.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YIconLoader.cc
00020 
00021   Author:       Katarína Machálková <kmachalkova@novell.com>
00022 
00023 /-*/
00024 
00025 
00026 #define YUILogComponent "ui"
00027 #include "YUILog.h"
00028 
00029 #include <sys/stat.h>
00030 #include <sstream>
00031 
00032 #include "YIconLoader.h"
00033 
00034 using std::endl;
00035 
00036 #define FALLBACK_ICON_PATH "/usr/share/icons/hicolor/"
00037 
00038 YIconLoader::YIconLoader()
00039 {
00040     addIconSearchPath(FALLBACK_ICON_PATH);
00041 }
00042 
00043 YIconLoader::~YIconLoader()
00044 {
00045 }
00046 
00047 void YIconLoader::setIconBasePath( std::string path )
00048 {
00049     _iconBasePath = path;
00050 }
00051 
00052 std::string YIconLoader::iconBasePath() const
00053 {
00054     return _iconBasePath;
00055 }
00056 
00057 void YIconLoader::addIconSearchPath( std::string path )
00058 {
00059     icon_dirs.push_front( path );
00060 }
00061 
00062 std::string YIconLoader::findIcon( std::string name )
00063 {
00064     // No extension -> add some
00065     std::string::size_type loc = name.find(".png");
00066     if ( loc == std::string::npos )
00067         name += ".png";
00068 
00069     // Absolute path -> return it
00070     if (name[0] == '/')
00071         return name;
00072 
00073     std::string fullPath;
00074 
00075     // Look in global search path
00076     if ( !_iconBasePath.empty () )
00077     {
00078         fullPath = _iconBasePath + name;
00079         if ( fileExists ( fullPath ) )
00080         {
00081             yuiMilestone() << "Found " << name << " in global search path" << endl;
00082             return fullPath;
00083         }
00084     }
00085 
00086     // Now search the fallback dirs
00087     std::list<std::string>::iterator listIt = icon_dirs.begin();
00088 
00089     while( listIt != icon_dirs.end() )
00090     {
00091         // Something like relative path
00092         if ( name.find('/') != std::string::npos )
00093             fullPath = *listIt +  name;
00094         // No '/' chars, just the name -> use '22x22/apps' fallback
00095         else
00096            fullPath = *listIt + "22x22/apps/" + name;
00097 
00098         if ( fileExists( fullPath ) )
00099         {
00100             yuiMilestone() << "Found " << name << " in " <<  *listIt << " search path" << endl;
00101             return fullPath;
00102         }
00103 
00104         yuiMilestone() <<  name << " not found in " <<  *listIt << " search path, skipping" << endl;
00105         listIt++;
00106     }
00107 
00108     return "";
00109 }
00110 
00111 bool YIconLoader::fileExists( std::string fname )
00112 {
00113     struct stat fileInfo;
00114     int ret = stat (fname.c_str(), &fileInfo);
00115 
00116     return ( ret == 0 );
00117 }
 All Classes Functions Variables Enumerations Friends