libyui  3.9.3
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 #include <sys/stat.h>
27 #include <sstream>
28 
29 #define YUILogComponent "ui"
30 #include "YUILog.h"
31 
32 #include "YIconLoader.h"
33 
34 #define FALLBACK_ICON_PATH "/usr/share/icons/hicolor/"
35 
36 
37 using std::string;
38 
39 
40 YIconLoader::YIconLoader()
41 {
42  addIconSearchPath( FALLBACK_ICON_PATH );
43 }
44 
45 
46 YIconLoader::~YIconLoader()
47 {
48 }
49 
50 
51 void YIconLoader::setIconBasePath( string path )
52 {
53  _iconBasePath = path;
54 }
55 
56 
57 string YIconLoader::iconBasePath() const
58 {
59  return _iconBasePath;
60 }
61 
62 
63 void YIconLoader::addIconSearchPath( string path )
64 {
65  icon_dirs.push_front( path );
66 }
67 
68 
69 string YIconLoader::findIcon( string name )
70 {
71  // No extension -> add some
72  string::size_type loc = name.find(".png");
73  if ( loc == string::npos )
74  name += ".png";
75 
76  // Absolute path -> return it
77  if (name[0] == '/')
78  return name;
79 
80  string fullPath;
81 
82  // Look in global search path
83  if ( !_iconBasePath.empty () )
84  {
85  fullPath = _iconBasePath + name;
86  if ( fileExists ( fullPath ) )
87  {
88  yuiMilestone() << "Found " << name << " in global search path" << endl;
89  return fullPath;
90  }
91  }
92 
93  // Now search the fallback dirs
94  std::list<string>::iterator listIt = icon_dirs.begin();
95 
96  while( listIt != icon_dirs.end() )
97  {
98  // Something like relative path
99  if ( name.find('/') != string::npos )
100  fullPath = *listIt + name;
101  // No '/' chars, just the name -> use '22x22/apps' fallback
102  else
103  fullPath = *listIt + "22x22/apps/" + name;
104 
105  if ( fileExists( fullPath ) )
106  {
107  yuiMilestone() << "Found " << name << " in " << *listIt << " search path" << endl;
108  return fullPath;
109  }
110 
111  yuiMilestone() << name << " not found in " << *listIt << " search path, skipping" << endl;
112  listIt++;
113  }
114 
115  return "";
116 }
117 
118 
119 bool YIconLoader::fileExists( string fname )
120 {
121  struct stat fileInfo;
122  int ret = stat (fname.c_str(), &fileInfo);
123 
124  return ( ret == 0 );
125 }