• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/mime_util.h"
6 
7 #include <gtk/gtk.h>
8 #include <sys/time.h>
9 #include <time.h>
10 
11 #include <cstdlib>
12 #include <list>
13 #include <map>
14 #include <vector>
15 
16 #include "base/file_util.h"
17 #include "base/logging.h"
18 #include "base/message_loop.h"
19 #include "base/scoped_ptr.h"
20 #include "base/singleton.h"
21 #include "base/string_util.h"
22 #include "base/third_party/xdg_mime/xdgmime.h"
23 
24 namespace {
25 
26 class IconTheme;
27 
28 class MimeUtilConstants {
29  public:
30 
31   // In seconds, specified by icon theme specs.
32   const int kUpdateInterval;
33 
34   // Store icon directories and their mtimes.
35   std::map<FilePath, int>* icon_dirs_;
36 
37   // Store icon formats.
38   std::vector<std::string> icon_formats_;
39 
40   // Store loaded icon_theme.
41   std::map<std::string, IconTheme*>* icon_themes_;
42 
43   static const size_t kDefaultThemeNum = 4;
44 
45   // The default theme.
46   IconTheme* default_themes_[kDefaultThemeNum];
47 
48   time_t last_check_time_;
49 
50   // This is set by DetectGtkTheme(). We cache it so that we can access the
51   // theme name from threads that aren't allowed to call
52   // gtk_settings_get_default().
53   std::string gtk_theme_name_;
54 
55  private:
MimeUtilConstants()56   MimeUtilConstants()
57       : kUpdateInterval(5),
58         icon_dirs_(NULL),
59         icon_themes_(NULL),
60         last_check_time_(0) {
61     icon_formats_.push_back(".png");
62     icon_formats_.push_back(".svg");
63     icon_formats_.push_back(".xpm");
64 
65     for (size_t i = 0; i < kDefaultThemeNum; ++i)
66       default_themes_[i] = NULL;
67   }
68   ~MimeUtilConstants();
69 
70   friend struct DefaultSingletonTraits<MimeUtilConstants>;
71 
72   DISALLOW_COPY_AND_ASSIGN(MimeUtilConstants);
73 };
74 
75 // IconTheme represents an icon theme as defined by the xdg icon theme spec.
76 // Example themes on GNOME include 'Human' and 'Mist'.
77 // Example themes on KDE include 'crystalsvg' and 'kdeclassic'.
78 class IconTheme {
79  public:
80   // A theme consists of multiple sub-directories, like '32x32' and 'scalable'.
81   class SubDirInfo {
82    public:
83     // See spec for details.
84     enum Type {
85       Fixed,
86       Scalable,
87       Threshold
88     };
SubDirInfo()89     SubDirInfo()
90         : size(0),
91           type(Threshold),
92           max_size(0),
93           min_size(0),
94           threshold(2) {
95     }
96     size_t size;  // Nominal size of the icons in this directory.
97     Type type;  // Type of the icon size.
98     size_t max_size;  // Maximum size that the icons can be scaled to.
99     size_t min_size;  // Minimum size that the icons can be scaled to.
100     size_t threshold;  // Maximum difference from desired size. 2 by default.
101   };
102 
103   explicit IconTheme(const std::string& name);
104 
~IconTheme()105   ~IconTheme() {
106     delete[] info_array_;
107   }
108 
109   // Returns the path to an icon with the name |icon_name| and a size of |size|
110   // pixels. If the icon does not exist, but |inherits| is true, then look for
111   // the icon in the parent theme.
112   FilePath GetIconPath(const std::string& icon_name, int size, bool inherits);
113 
114   // Load a theme with the name |theme_name| into memory. Returns null if theme
115   // is invalid.
116   static IconTheme* LoadTheme(const std::string& theme_name);
117 
118  private:
119   // Returns the path to an icon with the name |icon_name| in |subdir|.
120   FilePath GetIconPathUnderSubdir(const std::string& icon_name,
121                                   const std::string& subdir);
122 
123   // Whether the theme loaded properly.
IsValid()124   bool IsValid() {
125     return index_theme_loaded_;
126   }
127 
128   // Read and parse |file| which is usually named 'index.theme' per theme spec.
129   bool LoadIndexTheme(const FilePath& file);
130 
131   // Checks to see if the icons in |info| matches |size| (in pixels). Returns
132   // 0 if they match, or the size difference in pixels.
133   size_t MatchesSize(SubDirInfo* info, size_t size);
134 
135   // Yet another function to read a line.
136   std::string ReadLine(FILE* fp);
137 
138   // Set directories to search for icons to the comma-separated list |dirs|.
139   bool SetDirectories(const std::string& dirs);
140 
141   bool index_theme_loaded_;  // True if an instance is properly loaded.
142   // store the scattered directories of this theme.
143   std::list<FilePath> dirs_;
144 
145   // store the subdirs of this theme and array index of |info_array_|.
146   std::map<std::string, int> subdirs_;
147   SubDirInfo* info_array_;  // List of sub-directories.
148   std::string inherits_;  // Name of the theme this one inherits from.
149 };
150 
IconTheme(const std::string & name)151 IconTheme::IconTheme(const std::string& name)
152   : index_theme_loaded_(false),
153     info_array_(NULL) {
154   // Iterate on all icon directories to find directories of the specified
155   // theme and load the first encountered index.theme.
156   std::map<FilePath, int>::iterator iter;
157   FilePath theme_path;
158   std::map<FilePath, int>* icon_dirs =
159       Singleton<MimeUtilConstants>::get()->icon_dirs_;
160   for (iter = icon_dirs->begin(); iter != icon_dirs->end(); ++iter) {
161     theme_path = iter->first.Append(name);
162     if (!file_util::DirectoryExists(theme_path))
163       continue;
164     FilePath theme_index = theme_path.Append("index.theme");
165     if (!index_theme_loaded_ && file_util::PathExists(theme_index)) {
166       if (!LoadIndexTheme(theme_index))
167         return;
168       index_theme_loaded_ = true;
169     }
170     dirs_.push_back(theme_path);
171   }
172 }
173 
GetIconPath(const std::string & icon_name,int size,bool inherits)174 FilePath IconTheme::GetIconPath(const std::string& icon_name, int size,
175                                 bool inherits) {
176   std::map<std::string, int>::iterator subdir_iter;
177   FilePath icon_path;
178 
179   for (subdir_iter = subdirs_.begin();
180        subdir_iter != subdirs_.end();
181        ++subdir_iter) {
182     SubDirInfo* info = &info_array_[subdir_iter->second];
183     if (MatchesSize(info, size) == 0) {
184       icon_path = GetIconPathUnderSubdir(icon_name, subdir_iter->first);
185       if (!icon_path.empty())
186         return icon_path;
187     }
188   }
189   // Now looking for the mostly matched.
190   int min_delta_seen = 9999;
191 
192   for (subdir_iter = subdirs_.begin();
193        subdir_iter != subdirs_.end();
194        ++subdir_iter) {
195     SubDirInfo* info = &info_array_[subdir_iter->second];
196     int delta = abs(MatchesSize(info, size));
197     if (delta < min_delta_seen) {
198       FilePath path = GetIconPathUnderSubdir(icon_name, subdir_iter->first);
199       if (!path.empty()) {
200         min_delta_seen = delta;
201         icon_path = path;
202       }
203     }
204   }
205 
206   if (!icon_path.empty() || !inherits || inherits_ == "")
207     return icon_path;
208 
209   IconTheme* theme = LoadTheme(inherits_);
210   if (theme)
211     return theme->GetIconPath(icon_name, size, inherits);
212   else
213     return FilePath();
214 }
215 
LoadTheme(const std::string & theme_name)216 IconTheme* IconTheme::LoadTheme(const std::string& theme_name) {
217   scoped_ptr<IconTheme> theme;
218   std::map<std::string, IconTheme*>* icon_themes =
219       Singleton<MimeUtilConstants>::get()->icon_themes_;
220   if (icon_themes->find(theme_name) != icon_themes->end()) {
221     theme.reset((*icon_themes)[theme_name]);
222   } else {
223     theme.reset(new IconTheme(theme_name));
224     if (!theme->IsValid())
225       theme.reset();
226     (*icon_themes)[theme_name] = theme.get();
227   }
228   return theme.release();
229 }
230 
GetIconPathUnderSubdir(const std::string & icon_name,const std::string & subdir)231 FilePath IconTheme::GetIconPathUnderSubdir(const std::string& icon_name,
232                                            const std::string& subdir) {
233   FilePath icon_path;
234   std::list<FilePath>::iterator dir_iter;
235   std::vector<std::string>* icon_formats =
236       &Singleton<MimeUtilConstants>::get()->icon_formats_;
237   for (dir_iter = dirs_.begin(); dir_iter != dirs_.end(); ++dir_iter) {
238     for (size_t i = 0; i < icon_formats->size(); ++i) {
239       icon_path = dir_iter->Append(subdir);
240       icon_path = icon_path.Append(icon_name + (*icon_formats)[i]);
241       if (file_util::PathExists(icon_path))
242         return icon_path;
243     }
244   }
245   return FilePath();
246 }
247 
LoadIndexTheme(const FilePath & file)248 bool IconTheme::LoadIndexTheme(const FilePath& file) {
249   FILE* fp = file_util::OpenFile(file, "r");
250   SubDirInfo* current_info = NULL;
251   if (!fp)
252     return false;
253 
254   // Read entries.
255   while (!feof(fp) && !ferror(fp)) {
256     std::string buf = ReadLine(fp);
257     if (buf == "")
258       break;
259 
260     std::string entry;
261     TrimWhitespaceASCII(buf, TRIM_ALL, &entry);
262     if (entry.length() == 0 || entry[0] == '#') {
263       // Blank line or Comment.
264       continue;
265     } else if (entry[0] == '[' && info_array_) {
266       current_info = NULL;
267       std::string subdir = entry.substr(1, entry.length() - 2);
268       if (subdirs_.find(subdir) != subdirs_.end())
269         current_info = &info_array_[subdirs_[subdir]];
270     }
271 
272     std::string key, value;
273     std::vector<std::string> r;
274     SplitStringDontTrim(entry, '=', &r);
275     if (r.size() < 2)
276       continue;
277 
278     TrimWhitespaceASCII(r[0], TRIM_ALL, &key);
279     for (size_t i = 1; i < r.size(); i++)
280       value.append(r[i]);
281     TrimWhitespaceASCII(value, TRIM_ALL, &value);
282 
283     if (current_info) {
284       if (key == "Size") {
285         current_info->size = atoi(value.c_str());
286       } else if (key == "Type") {
287         if (value == "Fixed")
288           current_info->type = SubDirInfo::Fixed;
289         else if (value == "Scalable")
290           current_info->type = SubDirInfo::Scalable;
291         else if (value == "Threshold")
292           current_info->type = SubDirInfo::Threshold;
293       } else if (key == "MaxSize") {
294         current_info->max_size = atoi(value.c_str());
295       } else if (key == "MinSize") {
296         current_info->min_size = atoi(value.c_str());
297       } else if (key == "Threshold") {
298         current_info->threshold = atoi(value.c_str());
299       }
300     } else {
301       if (key.compare("Directories") == 0 && !info_array_) {
302         if (!SetDirectories(value)) break;
303       } else if (key.compare("Inherits") == 0) {
304         if (value != "hicolor")
305           inherits_ = value;
306       }
307     }
308   }
309 
310   file_util::CloseFile(fp);
311   return info_array_ != NULL;
312 }
313 
MatchesSize(SubDirInfo * info,size_t size)314 size_t IconTheme::MatchesSize(SubDirInfo* info, size_t size) {
315   if (info->type == SubDirInfo::Fixed) {
316     return size - info->size;
317   } else if (info->type == SubDirInfo::Scalable) {
318     if (size >= info->min_size && size <= info->max_size) {
319       return 0;
320     } else {
321       return abs(size - info->min_size) < abs(size - info->max_size) ?
322           (size - info->min_size) : (size - info->max_size);
323     }
324   } else {
325     if (size >= info->size - info->threshold &&
326         size <= info->size + info->threshold) {
327       return 0;
328     } else {
329       return abs(size - info->size - info->threshold) <
330           abs(size - info->size + info->threshold)
331           ? size - info->size - info->threshold
332           : size - info->size + info->threshold;
333     }
334   }
335 }
336 
ReadLine(FILE * fp)337 std::string IconTheme::ReadLine(FILE* fp) {
338   if (!fp)
339     return "";
340 
341   std::string result = "";
342   const size_t kBufferSize = 100;
343   char buffer[kBufferSize];
344   while ((fgets(buffer, kBufferSize - 1, fp)) != NULL) {
345     result += buffer;
346     size_t len = result.length();
347     if (len == 0)
348       break;
349     char end = result[len - 1];
350     if (end == '\n' || end == '\0')
351       break;
352   }
353 
354   return result;
355 }
356 
SetDirectories(const std::string & dirs)357 bool IconTheme::SetDirectories(const std::string& dirs) {
358   int num = 0;
359   std::string::size_type pos = 0, epos;
360   std::string dir;
361   while ((epos = dirs.find(',', pos)) != std::string::npos) {
362     TrimWhitespaceASCII(dirs.substr(pos, epos - pos), TRIM_ALL, &dir);
363     if (dir.length() == 0) {
364       LOG(WARNING) << "Invalid index.theme: blank subdir";
365       return false;
366     }
367     subdirs_[dir] = num++;
368     pos = epos + 1;
369   }
370   TrimWhitespaceASCII(dirs.substr(pos), TRIM_ALL, &dir);
371   if (dir.length() == 0) {
372     LOG(WARNING) << "Invalid index.theme: blank subdir";
373     return false;
374   }
375   subdirs_[dir] = num++;
376   info_array_ = new SubDirInfo[num];
377   return true;
378 }
379 
380 // Make sure |dir| exists and add it to the list of icon directories.
TryAddIconDir(const FilePath & dir)381 void TryAddIconDir(const FilePath& dir) {
382   if (!file_util::DirectoryExists(dir))
383     return;
384   (*Singleton<MimeUtilConstants>::get()->icon_dirs_)[dir] = 0;
385 }
386 
387 // For a xdg directory |dir|, add the appropriate icon sub-directories.
AddXDGDataDir(const FilePath & dir)388 void AddXDGDataDir(const FilePath& dir) {
389   if (!file_util::DirectoryExists(dir))
390     return;
391   TryAddIconDir(dir.Append("icons"));
392   TryAddIconDir(dir.Append("pixmaps"));
393 }
394 
395 // Add all the xdg icon directories.
InitIconDir()396 void InitIconDir() {
397   Singleton<MimeUtilConstants>::get()->icon_dirs_->clear();
398   const char* home = getenv("HOME");
399   if (home) {
400       FilePath legacy_data_dir(home);
401       legacy_data_dir = legacy_data_dir.AppendASCII(".icons");
402       if (file_util::DirectoryExists(legacy_data_dir))
403         TryAddIconDir(legacy_data_dir);
404   }
405   const char* env = getenv("XDG_DATA_HOME");
406   if (env) {
407     AddXDGDataDir(FilePath(env));
408   } else if (home) {
409     FilePath local_data_dir(home);
410     local_data_dir = local_data_dir.AppendASCII(".local");
411     local_data_dir = local_data_dir.AppendASCII("share");
412     AddXDGDataDir(local_data_dir);
413   }
414 
415   env = getenv("XDG_DATA_DIRS");
416   if (!env) {
417     AddXDGDataDir(FilePath("/usr/local/share"));
418     AddXDGDataDir(FilePath("/usr/share"));
419   } else {
420     std::string xdg_data_dirs = env;
421     std::string::size_type pos = 0, epos;
422     while ((epos = xdg_data_dirs.find(':', pos)) != std::string::npos) {
423       AddXDGDataDir(FilePath(xdg_data_dirs.substr(pos, epos - pos)));
424       pos = epos + 1;
425     }
426     AddXDGDataDir(FilePath(xdg_data_dirs.substr(pos)));
427   }
428 }
429 
430 // Per xdg theme spec, we should check the icon directories every so often for
431 // newly added icons. This isn't quite right.
EnsureUpdated()432 void EnsureUpdated() {
433   struct timeval t;
434   gettimeofday(&t, NULL);
435   time_t now = t.tv_sec;
436   MimeUtilConstants* constants = Singleton<MimeUtilConstants>::get();
437 
438   if (constants->last_check_time_ == 0) {
439     constants->icon_dirs_ = new std::map<FilePath, int>;
440     constants->icon_themes_ = new std::map<std::string, IconTheme*>;
441     InitIconDir();
442     constants->last_check_time_ = now;
443   } else {
444     // TODO(thestig): something changed. start over. Upstream fix to Google
445     // Gadgets for Linux.
446     if (now > constants->last_check_time_ + constants->kUpdateInterval) {
447     }
448   }
449 }
450 
451 // Find a fallback icon if we cannot find it in the default theme.
LookupFallbackIcon(const std::string & icon_name)452 FilePath LookupFallbackIcon(const std::string& icon_name) {
453   FilePath icon;
454   MimeUtilConstants* constants = Singleton<MimeUtilConstants>::get();
455   std::map<FilePath, int>::iterator iter;
456   std::map<FilePath, int>* icon_dirs = constants->icon_dirs_;
457   std::vector<std::string>* icon_formats = &constants->icon_formats_;
458   for (iter = icon_dirs->begin(); iter != icon_dirs->end(); ++iter) {
459     for (size_t i = 0; i < icon_formats->size(); ++i) {
460       icon = iter->first.Append(icon_name + (*icon_formats)[i]);
461       if (file_util::PathExists(icon))
462         return icon;
463     }
464   }
465   return FilePath();
466 }
467 
468 // Initialize the list of default themes.
InitDefaultThemes()469 void InitDefaultThemes() {
470   IconTheme** default_themes =
471       Singleton<MimeUtilConstants>::get()->default_themes_;
472 
473   char* env = getenv("KDE_FULL_SESSION");
474   if (env) {
475     // KDE
476     std::string kde_default_theme;
477     std::string kde_fallback_theme;
478 
479     // TODO(thestig): Figure out how to get the current icon theme on KDE.
480     // Setting stored in ~/.kde/share/config/kdeglobals under Icons -> Theme.
481     default_themes[0] = NULL;
482 
483     // Try some reasonable defaults for KDE.
484     env = getenv("KDE_SESSION_VERSION");
485     if (!env || env[0] != '4') {
486       // KDE 3
487       kde_default_theme = "default.kde";
488       kde_fallback_theme = "crystalsvg";
489     } else {
490       // KDE 4
491       kde_default_theme = "default.kde4";
492       kde_fallback_theme = "oxygen";
493     }
494     default_themes[1] = IconTheme::LoadTheme(kde_default_theme);
495     default_themes[2] = IconTheme::LoadTheme(kde_fallback_theme);
496   } else {
497     // Assume it's Gnome and use GTK to figure out the theme.
498     default_themes[1] = IconTheme::LoadTheme(
499         Singleton<MimeUtilConstants>::get()->gtk_theme_name_);
500     default_themes[2] = IconTheme::LoadTheme("gnome");
501   }
502   // hicolor needs to be last per icon theme spec.
503   default_themes[3] = IconTheme::LoadTheme("hicolor");
504 
505   for (size_t i = 0; i < MimeUtilConstants::kDefaultThemeNum; i++) {
506     if (default_themes[i] == NULL)
507       continue;
508     // NULL out duplicate pointers.
509     for (size_t j = i + 1; j < MimeUtilConstants::kDefaultThemeNum; j++) {
510       if (default_themes[j] == default_themes[i])
511         default_themes[j] = NULL;
512     }
513   }
514 }
515 
516 // Try to find an icon with the name |icon_name| that's |size| pixels.
LookupIconInDefaultTheme(const std::string & icon_name,int size)517 FilePath LookupIconInDefaultTheme(const std::string& icon_name, int size) {
518   EnsureUpdated();
519   MimeUtilConstants* constants = Singleton<MimeUtilConstants>::get();
520   std::map<std::string, IconTheme*>* icon_themes = constants->icon_themes_;
521   if (icon_themes->size() == 0)
522     InitDefaultThemes();
523 
524   FilePath icon_path;
525   IconTheme** default_themes = constants->default_themes_;
526   for (size_t i = 0; i < MimeUtilConstants::kDefaultThemeNum; i++) {
527     if (default_themes[i]) {
528       icon_path = default_themes[i]->GetIconPath(icon_name, size, true);
529       if (!icon_path.empty())
530         return icon_path;
531     }
532   }
533   return LookupFallbackIcon(icon_name);
534 }
535 
~MimeUtilConstants()536 MimeUtilConstants::~MimeUtilConstants() {
537   delete icon_dirs_;
538   delete icon_themes_;
539   for (size_t i = 0; i < kDefaultThemeNum; i++)
540     delete default_themes_[i];
541 }
542 
543 }  // namespace
544 
545 namespace mime_util {
546 
GetFileMimeType(const FilePath & filepath)547 std::string GetFileMimeType(const FilePath& filepath) {
548   return xdg_mime_get_mime_type_from_file_name(filepath.value().c_str());
549 }
550 
GetDataMimeType(const std::string & data)551 std::string GetDataMimeType(const std::string& data) {
552   return xdg_mime_get_mime_type_for_data(data.data(), data.length(), NULL);
553 }
554 
DetectGtkTheme()555 void DetectGtkTheme() {
556   // If the theme name is already loaded, do nothing. Chrome doesn't respond
557   // to changes in the system theme, so we never need to set this more than
558   // once.
559   if (!Singleton<MimeUtilConstants>::get()->gtk_theme_name_.empty())
560     return;
561 
562   // We should only be called on the UI thread.
563   DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
564 
565   gchar* gtk_theme_name;
566   g_object_get(gtk_settings_get_default(),
567                "gtk-icon-theme-name",
568                &gtk_theme_name, NULL);
569   Singleton<MimeUtilConstants>::get()->gtk_theme_name_.assign(gtk_theme_name);
570   g_free(gtk_theme_name);
571 }
572 
GetMimeIcon(const std::string & mime_type,size_t size)573 FilePath GetMimeIcon(const std::string& mime_type, size_t size) {
574   std::vector<std::string> icon_names;
575   std::string icon_name;
576   FilePath icon_file;
577 
578   const char* icon = xdg_mime_get_icon(mime_type.c_str());
579   icon_name = std::string(icon ? icon : "");
580   if (icon_name.length())
581     icon_names.push_back(icon_name);
582 
583   // For text/plain, try text-plain.
584   icon_name = mime_type;
585   for (size_t i = icon_name.find('/', 0); i != std::string::npos;
586        i = icon_name.find('/', i + 1)) {
587     icon_name[i] = '-';
588   }
589   icon_names.push_back(icon_name);
590   // Also try gnome-mime-text-plain.
591   icon_names.push_back("gnome-mime-" + icon_name);
592 
593   // Try "deb" for "application/x-deb" in KDE 3.
594   icon_name = mime_type.substr(mime_type.find("/x-") + 3);
595   icon_names.push_back(icon_name);
596 
597   // Try generic name like text-x-generic.
598   icon_name = mime_type.substr(0, mime_type.find('/')) + "-x-generic";
599   icon_names.push_back(icon_name);
600 
601   // Last resort
602   icon_names.push_back("unknown");
603 
604   for (size_t i = 0; i < icon_names.size(); i++) {
605     if (icon_names[i][0] == '/') {
606       icon_file = FilePath(icon_names[i]);
607       if (file_util::PathExists(icon_file))
608         return icon_file;
609     } else {
610       icon_file = LookupIconInDefaultTheme(icon_names[i], size);
611       if (!icon_file.empty())
612         return icon_file;
613     }
614   }
615   return FilePath();
616 }
617 
618 }  // namespace mime_util
619