LCOV - code coverage report
Current view: top level - vcl/source/app - IconThemeScanner.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 75 86 87.2 %
Date: 2014-04-11 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #include <vcl/IconThemeScanner.hxx>
      11             : 
      12             : #include <config_folders.h>
      13             : #include <osl/file.hxx>
      14             : #include <rtl/bootstrap.hxx>
      15             : 
      16             : #include <vcl/svapp.hxx>
      17             : #include <vcl/IconThemeInfo.hxx>
      18             : 
      19             : namespace vcl {
      20             : 
      21             : namespace {
      22             : 
      23             : bool
      24         290 : search_path_is_valid(const OUString& dir)
      25             : {
      26         290 :     osl::DirectoryItem dirItem;
      27         290 :     osl::FileBase::RC retvalGet = osl::DirectoryItem::get(dir, dirItem);
      28         290 :     if (retvalGet != osl::FileBase::E_None) {
      29           0 :         return false;
      30             :     }
      31         580 :     osl::FileStatus fileStatus(osl_FileStatus_Mask_Type);
      32         290 :     osl::FileBase::RC retvalStatus = dirItem.getFileStatus(fileStatus);
      33         290 :     if (retvalStatus != osl::FileBase::E_None) {
      34           0 :         return false;
      35             :     }
      36             : 
      37         290 :     if (!fileStatus.isDirectory()) {
      38           0 :         return false;
      39             :     }
      40         580 :     return true;
      41             : }
      42             : 
      43             : }
      44             : 
      45         293 : IconThemeScanner::IconThemeScanner()
      46         293 : {;}
      47             : 
      48             : bool
      49         290 : IconThemeScanner::ScanDirectoryForIconThemes(const OUString& path)
      50             : {
      51         290 :     bool pathIsValid = search_path_is_valid(path);
      52         290 :     if (!pathIsValid) {
      53           0 :         return false;
      54             :     }
      55         290 :     std::vector<OUString> iconThemePaths = ReadIconThemesFromPath(path);
      56         290 :     if (iconThemePaths.empty()) {
      57           0 :         return false;
      58             :     }
      59         290 :     mFoundIconThemes.clear();
      60        2030 :     for (std::vector<OUString>::iterator aI = iconThemePaths.begin(); aI != iconThemePaths.end(); ++aI)
      61             :     {
      62        1740 :         AddIconThemeByPath(*aI);
      63             :     }
      64         290 :     return true;
      65             : }
      66             : 
      67             : bool
      68        1743 : IconThemeScanner::AddIconThemeByPath(const OUString &url)
      69             : {
      70        1743 :     if (!IconThemeInfo::UrlCanBeParsed(url)) {
      71           0 :         return false;
      72             :     }
      73        1743 :     IconThemeInfo newTheme(url);
      74        1743 :     mFoundIconThemes.push_back(newTheme);
      75        1743 :     return true;
      76             : }
      77             : 
      78             : /*static*/ std::vector<OUString>
      79         290 : IconThemeScanner::ReadIconThemesFromPath(const OUString& dir)
      80             : {
      81         290 :     std::vector<OUString> found;
      82             : 
      83         580 :     osl::Directory dirToScan(dir);
      84         290 :     osl::FileBase::RC retvalOpen = dirToScan.open();
      85         290 :     if (retvalOpen != osl::FileBase::E_None) {
      86           0 :         return found;
      87             :     }
      88             : 
      89         580 :     osl::DirectoryItem directoryItem;
      90        3480 :     while (dirToScan.getNextItem(directoryItem) == osl::FileBase::E_None) {
      91        2900 :         osl::FileStatus status(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileURL | osl_FileStatus_Mask_FileName);
      92        2900 :         osl::FileBase::RC retvalStatus = directoryItem.getFileStatus(status);
      93        2900 :         if (retvalStatus != osl::FileBase::E_None) {
      94           0 :             continue;
      95             :         }
      96        2900 :         if (!status.isRegular()) {
      97         870 :             continue;
      98             :         }
      99        2030 :         if (!FileIsValidIconTheme(status.getFileURL())) {
     100         290 :             continue;
     101             :         }
     102        3480 :         OUString entry;
     103        1740 :         entry = status.getFileURL();
     104        1740 :         found.push_back(entry);
     105        1740 :     }
     106         290 :     return found;
     107             : }
     108             : 
     109             : /*static*/ bool
     110        2030 : IconThemeScanner::FileIsValidIconTheme(const OUString& filename)
     111             : {
     112             :     // check whether we can construct a IconThemeInfo from it
     113        2030 :     if (!IconThemeInfo::UrlCanBeParsed(filename)) {
     114         290 :         return false;
     115             :     }
     116             : 
     117             :     // check whether the file is a regular file
     118        1740 :     osl::DirectoryItem dirItem;
     119        1740 :     osl::FileBase::RC retvalGet = osl::DirectoryItem::get(filename, dirItem);
     120        1740 :     if (retvalGet != osl::FileBase::E_None) {
     121           0 :         return false;
     122             :     }
     123        3480 :     osl::FileStatus fileStatus(osl_FileStatus_Mask_Type);
     124        1740 :     osl::FileBase::RC retvalStatus = dirItem.getFileStatus(fileStatus);
     125        1740 :     if (retvalStatus != osl::FileBase::E_None) {
     126           0 :         return false;
     127             :     }
     128        1740 :     if (!fileStatus.isRegular()) {
     129           0 :         return false;
     130             :     }
     131        3480 :     return true;
     132             : }
     133             : 
     134             : bool
     135           1 : IconThemeScanner::IconThemeIsInstalled(const OUString& themeId) const
     136             : {
     137           1 :     return IconThemeInfo::IconThemeIsInVector(mFoundIconThemes, themeId);
     138             : }
     139             : 
     140             : /*static*/ boost::shared_ptr<IconThemeScanner>
     141         290 : IconThemeScanner::Create(const OUString &path)
     142             : {
     143         290 :     boost::shared_ptr<IconThemeScanner> retval(new IconThemeScanner);
     144         290 :     retval->ScanDirectoryForIconThemes(path);
     145         290 :     return retval;
     146             : }
     147             : 
     148             : /*static*/ OUString
     149         290 : IconThemeScanner::GetStandardIconThemePath()
     150             : {
     151         290 :     OUString url( "$BRAND_BASE_DIR/" LIBO_SHARE_FOLDER "/config/" );
     152         290 :     rtl::Bootstrap::expandMacros(url);
     153         290 :     return url;
     154             : }
     155             : 
     156       29404 : IconThemeScanner::~IconThemeScanner()
     157       29404 : {;}
     158             : 
     159             : namespace
     160             : {
     161             :     class SameTheme :
     162             :         public std::unary_function<const vcl::IconThemeInfo &, bool>
     163             :     {
     164             :     private:
     165             :         const OUString& m_rThemeId;
     166             :     public:
     167           2 :         SameTheme(const OUString &rThemeId) : m_rThemeId(rThemeId) {}
     168           2 :         bool operator()(const vcl::IconThemeInfo &rInfo)
     169             :         {
     170           2 :             return m_rThemeId == rInfo.GetThemeId();
     171             :         }
     172             :     };
     173             : }
     174             : 
     175             : const vcl::IconThemeInfo&
     176           2 : IconThemeScanner::GetIconThemeInfo(const OUString& themeId)
     177             : {
     178             :     std::vector<IconThemeInfo>::iterator info = std::find_if(mFoundIconThemes.begin(), mFoundIconThemes.end(),
     179           2 :         SameTheme(themeId));
     180           2 :     if (info == mFoundIconThemes.end()) {
     181           1 :         throw std::runtime_error("Requested information on not-installed icon theme");
     182             :     }
     183           1 :     return *info;
     184             : }
     185             : 
     186             : } // end namespace vcl
     187             : 
     188             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10