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.
5 #include "base/mime_util.h"
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"
28 class MimeUtilConstants
{
31 // In seconds, specified by icon theme specs.
32 const int kUpdateInterval
;
34 // Store icon directories and their mtimes.
35 std::map
<FilePath
, int>* icon_dirs_
;
37 // Store icon formats.
38 std::vector
<std::string
> icon_formats_
;
40 // Store loaded icon_theme.
41 std::map
<std::string
, IconTheme
*>* icon_themes_
;
43 static const size_t kDefaultThemeNum
= 4;
46 IconTheme
* default_themes_
[kDefaultThemeNum
];
48 time_t last_check_time_
;
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_
;
61 icon_formats_
.push_back(".png");
62 icon_formats_
.push_back(".svg");
63 icon_formats_
.push_back(".xpm");
65 for (size_t i
= 0; i
< kDefaultThemeNum
; ++i
)
66 default_themes_
[i
] = NULL
;
70 friend struct DefaultSingletonTraits
<MimeUtilConstants
>;
72 DISALLOW_COPY_AND_ASSIGN(MimeUtilConstants
);
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'.
80 // A theme consists of multiple sub-directories, like '32x32' and 'scalable'.
83 // See spec for details.
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.
103 explicit IconTheme(const std::string
& name
);
106 delete[] info_array_
;
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
);
114 // Load a theme with the name |theme_name| into memory. Returns null if theme
116 static IconTheme
* LoadTheme(const std::string
& theme_name
);
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
);
123 // Whether the theme loaded properly.
125 return index_theme_loaded_
;
128 // Read and parse |file| which is usually named 'index.theme' per theme spec.
129 bool LoadIndexTheme(const FilePath
& file
);
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
);
135 // Yet another function to read a line.
136 std::string
ReadLine(FILE* fp
);
138 // Set directories to search for icons to the comma-separated list |dirs|.
139 bool SetDirectories(const std::string
& dirs
);
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_
;
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.
151 IconTheme::IconTheme(const std::string
& name
)
152 : index_theme_loaded_(false),
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
;
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
))
164 FilePath theme_index
= theme_path
.Append("index.theme");
165 if (!index_theme_loaded_
&& file_util::PathExists(theme_index
)) {
166 if (!LoadIndexTheme(theme_index
))
168 index_theme_loaded_
= true;
170 dirs_
.push_back(theme_path
);
174 FilePath
IconTheme::GetIconPath(const std::string
& icon_name
, int size
,
176 std::map
<std::string
, int>::iterator subdir_iter
;
179 for (subdir_iter
= subdirs_
.begin();
180 subdir_iter
!= subdirs_
.end();
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())
189 // Now looking for the mostly matched.
190 int min_delta_seen
= 9999;
192 for (subdir_iter
= subdirs_
.begin();
193 subdir_iter
!= subdirs_
.end();
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
);
200 min_delta_seen
= delta
;
206 if (!icon_path
.empty() || !inherits
|| inherits_
== "")
209 IconTheme
* theme
= LoadTheme(inherits_
);
210 // Inheriting from itself means the theme is buggy but we shouldn't crash.
211 if (theme
&& theme
!= this)
212 return theme
->GetIconPath(icon_name
, size
, inherits
);
217 IconTheme
* IconTheme::LoadTheme(const std::string
& theme_name
) {
218 scoped_ptr
<IconTheme
> theme
;
219 std::map
<std::string
, IconTheme
*>* icon_themes
=
220 Singleton
<MimeUtilConstants
>::get()->icon_themes_
;
221 if (icon_themes
->find(theme_name
) != icon_themes
->end()) {
222 theme
.reset((*icon_themes
)[theme_name
]);
224 theme
.reset(new IconTheme(theme_name
));
225 if (!theme
->IsValid())
227 (*icon_themes
)[theme_name
] = theme
.get();
229 return theme
.release();
232 FilePath
IconTheme::GetIconPathUnderSubdir(const std::string
& icon_name
,
233 const std::string
& subdir
) {
235 std::list
<FilePath
>::iterator dir_iter
;
236 std::vector
<std::string
>* icon_formats
=
237 &Singleton
<MimeUtilConstants
>::get()->icon_formats_
;
238 for (dir_iter
= dirs_
.begin(); dir_iter
!= dirs_
.end(); ++dir_iter
) {
239 for (size_t i
= 0; i
< icon_formats
->size(); ++i
) {
240 icon_path
= dir_iter
->Append(subdir
);
241 icon_path
= icon_path
.Append(icon_name
+ (*icon_formats
)[i
]);
242 if (file_util::PathExists(icon_path
))
249 bool IconTheme::LoadIndexTheme(const FilePath
& file
) {
250 FILE* fp
= file_util::OpenFile(file
, "r");
251 SubDirInfo
* current_info
= NULL
;
256 while (!feof(fp
) && !ferror(fp
)) {
257 std::string buf
= ReadLine(fp
);
262 TrimWhitespaceASCII(buf
, TRIM_ALL
, &entry
);
263 if (entry
.length() == 0 || entry
[0] == '#') {
264 // Blank line or Comment.
266 } else if (entry
[0] == '[' && info_array_
) {
268 std::string subdir
= entry
.substr(1, entry
.length() - 2);
269 if (subdirs_
.find(subdir
) != subdirs_
.end())
270 current_info
= &info_array_
[subdirs_
[subdir
]];
273 std::string key
, value
;
274 std::vector
<std::string
> r
;
275 SplitStringDontTrim(entry
, '=', &r
);
279 TrimWhitespaceASCII(r
[0], TRIM_ALL
, &key
);
280 for (size_t i
= 1; i
< r
.size(); i
++)
282 TrimWhitespaceASCII(value
, TRIM_ALL
, &value
);
286 current_info
->size
= atoi(value
.c_str());
287 } else if (key
== "Type") {
288 if (value
== "Fixed")
289 current_info
->type
= SubDirInfo::Fixed
;
290 else if (value
== "Scalable")
291 current_info
->type
= SubDirInfo::Scalable
;
292 else if (value
== "Threshold")
293 current_info
->type
= SubDirInfo::Threshold
;
294 } else if (key
== "MaxSize") {
295 current_info
->max_size
= atoi(value
.c_str());
296 } else if (key
== "MinSize") {
297 current_info
->min_size
= atoi(value
.c_str());
298 } else if (key
== "Threshold") {
299 current_info
->threshold
= atoi(value
.c_str());
302 if (key
.compare("Directories") == 0 && !info_array_
) {
303 if (!SetDirectories(value
)) break;
304 } else if (key
.compare("Inherits") == 0) {
305 if (value
!= "hicolor")
311 file_util::CloseFile(fp
);
312 return info_array_
!= NULL
;
315 size_t IconTheme::MatchesSize(SubDirInfo
* info
, size_t size
) {
316 if (info
->type
== SubDirInfo::Fixed
) {
317 return size
- info
->size
;
318 } else if (info
->type
== SubDirInfo::Scalable
) {
319 if (size
>= info
->min_size
&& size
<= info
->max_size
) {
322 return abs(size
- info
->min_size
) < abs(size
- info
->max_size
) ?
323 (size
- info
->min_size
) : (size
- info
->max_size
);
326 if (size
>= info
->size
- info
->threshold
&&
327 size
<= info
->size
+ info
->threshold
) {
330 return abs(size
- info
->size
- info
->threshold
) <
331 abs(size
- info
->size
+ info
->threshold
)
332 ? size
- info
->size
- info
->threshold
333 : size
- info
->size
+ info
->threshold
;
338 std::string
IconTheme::ReadLine(FILE* fp
) {
342 std::string result
= "";
343 const size_t kBufferSize
= 100;
344 char buffer
[kBufferSize
];
345 while ((fgets(buffer
, kBufferSize
- 1, fp
)) != NULL
) {
347 size_t len
= result
.length();
350 char end
= result
[len
- 1];
351 if (end
== '\n' || end
== '\0')
358 bool IconTheme::SetDirectories(const std::string
& dirs
) {
360 std::string::size_type pos
= 0, epos
;
362 while ((epos
= dirs
.find(',', pos
)) != std::string::npos
) {
363 TrimWhitespaceASCII(dirs
.substr(pos
, epos
- pos
), TRIM_ALL
, &dir
);
364 if (dir
.length() == 0) {
365 LOG(WARNING
) << "Invalid index.theme: blank subdir";
368 subdirs_
[dir
] = num
++;
371 TrimWhitespaceASCII(dirs
.substr(pos
), TRIM_ALL
, &dir
);
372 if (dir
.length() == 0) {
373 LOG(WARNING
) << "Invalid index.theme: blank subdir";
376 subdirs_
[dir
] = num
++;
377 info_array_
= new SubDirInfo
[num
];
381 // Make sure |dir| exists and add it to the list of icon directories.
382 void TryAddIconDir(const FilePath
& dir
) {
383 if (!file_util::DirectoryExists(dir
))
385 (*Singleton
<MimeUtilConstants
>::get()->icon_dirs_
)[dir
] = 0;
388 // For a xdg directory |dir|, add the appropriate icon sub-directories.
389 void AddXDGDataDir(const FilePath
& dir
) {
390 if (!file_util::DirectoryExists(dir
))
392 TryAddIconDir(dir
.Append("icons"));
393 TryAddIconDir(dir
.Append("pixmaps"));
396 // Add all the xdg icon directories.
398 Singleton
<MimeUtilConstants
>::get()->icon_dirs_
->clear();
399 const char* home
= getenv("HOME");
401 FilePath
legacy_data_dir(home
);
402 legacy_data_dir
= legacy_data_dir
.AppendASCII(".icons");
403 if (file_util::DirectoryExists(legacy_data_dir
))
404 TryAddIconDir(legacy_data_dir
);
406 const char* env
= getenv("XDG_DATA_HOME");
408 AddXDGDataDir(FilePath(env
));
410 FilePath
local_data_dir(home
);
411 local_data_dir
= local_data_dir
.AppendASCII(".local");
412 local_data_dir
= local_data_dir
.AppendASCII("share");
413 AddXDGDataDir(local_data_dir
);
416 env
= getenv("XDG_DATA_DIRS");
418 AddXDGDataDir(FilePath("/usr/local/share"));
419 AddXDGDataDir(FilePath("/usr/share"));
421 std::string xdg_data_dirs
= env
;
422 std::string::size_type pos
= 0, epos
;
423 while ((epos
= xdg_data_dirs
.find(':', pos
)) != std::string::npos
) {
424 AddXDGDataDir(FilePath(xdg_data_dirs
.substr(pos
, epos
- pos
)));
427 AddXDGDataDir(FilePath(xdg_data_dirs
.substr(pos
)));
431 // Per xdg theme spec, we should check the icon directories every so often for
432 // newly added icons. This isn't quite right.
433 void EnsureUpdated() {
435 gettimeofday(&t
, NULL
);
436 time_t now
= t
.tv_sec
;
437 MimeUtilConstants
* constants
= Singleton
<MimeUtilConstants
>::get();
439 if (constants
->last_check_time_
== 0) {
440 constants
->icon_dirs_
= new std::map
<FilePath
, int>;
441 constants
->icon_themes_
= new std::map
<std::string
, IconTheme
*>;
443 constants
->last_check_time_
= now
;
445 // TODO(thestig): something changed. start over. Upstream fix to Google
446 // Gadgets for Linux.
447 if (now
> constants
->last_check_time_
+ constants
->kUpdateInterval
) {
452 // Find a fallback icon if we cannot find it in the default theme.
453 FilePath
LookupFallbackIcon(const std::string
& icon_name
) {
455 MimeUtilConstants
* constants
= Singleton
<MimeUtilConstants
>::get();
456 std::map
<FilePath
, int>::iterator iter
;
457 std::map
<FilePath
, int>* icon_dirs
= constants
->icon_dirs_
;
458 std::vector
<std::string
>* icon_formats
= &constants
->icon_formats_
;
459 for (iter
= icon_dirs
->begin(); iter
!= icon_dirs
->end(); ++iter
) {
460 for (size_t i
= 0; i
< icon_formats
->size(); ++i
) {
461 icon
= iter
->first
.Append(icon_name
+ (*icon_formats
)[i
]);
462 if (file_util::PathExists(icon
))
469 // Initialize the list of default themes.
470 void InitDefaultThemes() {
471 IconTheme
** default_themes
=
472 Singleton
<MimeUtilConstants
>::get()->default_themes_
;
474 char* env
= getenv("KDE_FULL_SESSION");
477 std::string kde_default_theme
;
478 std::string kde_fallback_theme
;
480 // TODO(thestig): Figure out how to get the current icon theme on KDE.
481 // Setting stored in ~/.kde/share/config/kdeglobals under Icons -> Theme.
482 default_themes
[0] = NULL
;
484 // Try some reasonable defaults for KDE.
485 env
= getenv("KDE_SESSION_VERSION");
486 if (!env
|| env
[0] != '4') {
488 kde_default_theme
= "default.kde";
489 kde_fallback_theme
= "crystalsvg";
492 kde_default_theme
= "default.kde4";
493 kde_fallback_theme
= "oxygen";
495 default_themes
[1] = IconTheme::LoadTheme(kde_default_theme
);
496 default_themes
[2] = IconTheme::LoadTheme(kde_fallback_theme
);
498 // Assume it's Gnome and use GTK to figure out the theme.
499 default_themes
[1] = IconTheme::LoadTheme(
500 Singleton
<MimeUtilConstants
>::get()->gtk_theme_name_
);
501 default_themes
[2] = IconTheme::LoadTheme("gnome");
503 // hicolor needs to be last per icon theme spec.
504 default_themes
[3] = IconTheme::LoadTheme("hicolor");
506 for (size_t i
= 0; i
< MimeUtilConstants::kDefaultThemeNum
; i
++) {
507 if (default_themes
[i
] == NULL
)
509 // NULL out duplicate pointers.
510 for (size_t j
= i
+ 1; j
< MimeUtilConstants::kDefaultThemeNum
; j
++) {
511 if (default_themes
[j
] == default_themes
[i
])
512 default_themes
[j
] = NULL
;
517 // Try to find an icon with the name |icon_name| that's |size| pixels.
518 FilePath
LookupIconInDefaultTheme(const std::string
& icon_name
, int size
) {
520 MimeUtilConstants
* constants
= Singleton
<MimeUtilConstants
>::get();
521 std::map
<std::string
, IconTheme
*>* icon_themes
= constants
->icon_themes_
;
522 if (icon_themes
->size() == 0)
526 IconTheme
** default_themes
= constants
->default_themes_
;
527 for (size_t i
= 0; i
< MimeUtilConstants::kDefaultThemeNum
; i
++) {
528 if (default_themes
[i
]) {
529 icon_path
= default_themes
[i
]->GetIconPath(icon_name
, size
, true);
530 if (!icon_path
.empty())
534 return LookupFallbackIcon(icon_name
);
537 MimeUtilConstants::~MimeUtilConstants() {
540 for (size_t i
= 0; i
< kDefaultThemeNum
; i
++)
541 delete default_themes_
[i
];
546 namespace mime_util
{
548 std::string
GetFileMimeType(const FilePath
& filepath
) {
549 return xdg_mime_get_mime_type_from_file_name(filepath
.value().c_str());
552 std::string
GetDataMimeType(const std::string
& data
) {
553 return xdg_mime_get_mime_type_for_data(data
.data(), data
.length(), NULL
);
556 void DetectGtkTheme() {
557 // If the theme name is already loaded, do nothing. Chrome doesn't respond
558 // to changes in the system theme, so we never need to set this more than
560 if (!Singleton
<MimeUtilConstants
>::get()->gtk_theme_name_
.empty())
563 // We should only be called on the UI thread.
564 DCHECK_EQ(MessageLoop::TYPE_UI
, MessageLoop::current()->type());
566 gchar
* gtk_theme_name
;
567 g_object_get(gtk_settings_get_default(),
568 "gtk-icon-theme-name",
569 >k_theme_name
, NULL
);
570 Singleton
<MimeUtilConstants
>::get()->gtk_theme_name_
.assign(gtk_theme_name
);
571 g_free(gtk_theme_name
);
574 FilePath
GetMimeIcon(const std::string
& mime_type
, size_t size
) {
575 std::vector
<std::string
> icon_names
;
576 std::string icon_name
;
579 const char* icon
= xdg_mime_get_icon(mime_type
.c_str());
580 icon_name
= std::string(icon
? icon
: "");
581 if (icon_name
.length())
582 icon_names
.push_back(icon_name
);
584 // For text/plain, try text-plain.
585 icon_name
= mime_type
;
586 for (size_t i
= icon_name
.find('/', 0); i
!= std::string::npos
;
587 i
= icon_name
.find('/', i
+ 1)) {
590 icon_names
.push_back(icon_name
);
591 // Also try gnome-mime-text-plain.
592 icon_names
.push_back("gnome-mime-" + icon_name
);
594 // Try "deb" for "application/x-deb" in KDE 3.
595 icon_name
= mime_type
.substr(mime_type
.find("/x-") + 3);
596 icon_names
.push_back(icon_name
);
598 // Try generic name like text-x-generic.
599 icon_name
= mime_type
.substr(0, mime_type
.find('/')) + "-x-generic";
600 icon_names
.push_back(icon_name
);
603 icon_names
.push_back("unknown");
605 for (size_t i
= 0; i
< icon_names
.size(); i
++) {
606 if (icon_names
[i
][0] == '/') {
607 icon_file
= FilePath(icon_names
[i
]);
608 if (file_util::PathExists(icon_file
))
611 icon_file
= LookupIconInDefaultTheme(icon_names
[i
], size
);
612 if (!icon_file
.empty())
619 } // namespace mime_util