libyui  3.3.2
YIconLoader.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YIconLoader.cc
20 
21  Author: Katarína Machálková <kmachalkova@novell.com>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ui"
27 #include "YUILog.h"
28 
29 #include <sys/stat.h>
30 #include <sstream>
31 
32 #include "YIconLoader.h"
33 
34 using std::endl;
35 
36 #define FALLBACK_ICON_PATH "/usr/share/icons/hicolor/"
37 
38 YIconLoader::YIconLoader()
39 {
40  addIconSearchPath(FALLBACK_ICON_PATH);
41 }
42 
43 YIconLoader::~YIconLoader()
44 {
45 }
46 
47 void YIconLoader::setIconBasePath( std::string path )
48 {
49  _iconBasePath = path;
50 }
51 
52 std::string YIconLoader::iconBasePath() const
53 {
54  return _iconBasePath;
55 }
56 
57 void YIconLoader::addIconSearchPath( std::string path )
58 {
59  icon_dirs.push_front( path );
60 }
61 
62 std::string YIconLoader::findIcon( std::string name )
63 {
64  // No extension -> add some
65  std::string::size_type loc = name.find(".png");
66  if ( loc == std::string::npos )
67  name += ".png";
68 
69  // Absolute path -> return it
70  if (name[0] == '/')
71  return name;
72 
73  std::string fullPath;
74 
75  // Look in global search path
76  if ( !_iconBasePath.empty () )
77  {
78  fullPath = _iconBasePath + name;
79  if ( fileExists ( fullPath ) )
80  {
81  yuiMilestone() << "Found " << name << " in global search path" << endl;
82  return fullPath;
83  }
84  }
85 
86  // Now search the fallback dirs
87  std::list<std::string>::iterator listIt = icon_dirs.begin();
88 
89  while( listIt != icon_dirs.end() )
90  {
91  // Something like relative path
92  if ( name.find('/') != std::string::npos )
93  fullPath = *listIt + name;
94  // No '/' chars, just the name -> use '22x22/apps' fallback
95  else
96  fullPath = *listIt + "22x22/apps/" + name;
97 
98  if ( fileExists( fullPath ) )
99  {
100  yuiMilestone() << "Found " << name << " in " << *listIt << " search path" << endl;
101  return fullPath;
102  }
103 
104  yuiMilestone() << name << " not found in " << *listIt << " search path, skipping" << endl;
105  listIt++;
106  }
107 
108  return "";
109 }
110 
111 bool YIconLoader::fileExists( std::string fname )
112 {
113  struct stat fileInfo;
114  int ret = stat (fname.c_str(), &fileInfo);
115 
116  return ( ret == 0 );
117 }