Exemple #1
0
// determines the icon data; this could be either a path on disk (if we have
// a suitable icon locally), base64-encoded icon data (if the icon is embedded
// in an R package), or nothing (if we cannot determine an icon at all)
std::string iconData(const std::string& iconGroup, 
      const std::string& iconName,
      const std::string& iconPath)
{
   if (iconPath.empty())
   {
      // convert the icon name into the format of our shipped icons, which is
      // all lowercase with no whitespace (e.g. "SQL Server" => "sqlserver.png")
      std::string iconFilename(string_utils::toLower(iconName));
      iconFilename = boost::regex_replace(iconFilename,
            boost::regex("\\s"), "") + ".png";

      // the package did not supply an icon; see if there's one baked in
      FilePath path = options().rResourcesPath().childPath("connections")
         .childPath(iconGroup)
         .childPath(iconFilename);
      if (path.exists())
         return std::string("connections/") + iconGroup + "/" + iconFilename;

      if (iconGroup == "drivers")
         return std::string("connections/drivers/odbc.png");

      // didn't find anything
      return std::string();
   }

   // expand the path 
   FilePath icon = module_context::resolveAliasedPath(iconPath);
   std::string iconData;

   // ensure that the icon file exists and is a small GIF, JPG, or PNG image
   if (icon.exists() && icon.size() < kMaxIconSize &&
       (icon.hasExtensionLowerCase(".gif") ||
        icon.hasExtensionLowerCase(".png") ||
        icon.hasExtensionLowerCase(".jpg") ||
        icon.hasExtensionLowerCase(".jpeg")))
   {
      Error error = base64::encode(icon, &iconData);
      if (error)
         LOG_ERROR(error);
      else
      {
         iconData = "data:" + icon.mimeContentType("image/png") + 
                    ";base64," + iconData;
      }
   }
   return iconData;
}
File   APP::iconFile()        { return homeDir().getChildFile  (iconsPath() + iconFilename()   ) ; }
File   APP::logoFile()        { return binFile().getSiblingFile(iconFilename()) ;                  }