|
| 1 | +/********************************************************************************** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Antoine Beauchamp |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + *********************************************************************************/ |
| 24 | + |
| 25 | +#include "WindowsIconResolutionService.h" |
| 26 | +#include "PropertyManager.h" |
| 27 | +#include "SelectionContext.h" |
| 28 | +#include "LoggerHelper.h" |
| 29 | +#include "App.h" |
| 30 | +#include "SaUtils.h" |
| 31 | +#include "Win32Registry.h" |
| 32 | + |
| 33 | +#include "rapidassist/strings.h" |
| 34 | +#include "rapidassist/environment.h" |
| 35 | +#include "rapidassist/filesystem_utf8.h" |
| 36 | +#include "rapidassist/unicode.h" |
| 37 | + |
| 38 | +#include <Windows.h> |
| 39 | + |
| 40 | +namespace shellanything |
| 41 | +{ |
| 42 | + WindowsIconResolutionService::WindowsIconResolutionService() |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + WindowsIconResolutionService::~WindowsIconResolutionService() |
| 47 | + { |
| 48 | + } |
| 49 | + |
| 50 | + bool WindowsIconResolutionService::ResolveFileExtensionIcon(Icon& icon) |
| 51 | + { |
| 52 | + // Is this icon have a file extension ? |
| 53 | + std::string original_file_extension = icon.GetFileExtension(); |
| 54 | + if (original_file_extension.empty()) |
| 55 | + return false; //nothing to resolve |
| 56 | + |
| 57 | + shellanything::PropertyManager& pmgr = shellanything::PropertyManager::GetInstance(); |
| 58 | + std::string file_extension = pmgr.Expand(original_file_extension); |
| 59 | + if (file_extension.empty()) |
| 60 | + return false; //nothing to resolve |
| 61 | + |
| 62 | + // Check for multiple values. Keep the first value, forget about other selected file extensions. |
| 63 | + const std::string separator = pmgr.GetProperty(SelectionContext::MULTI_SELECTION_SEPARATOR_PROPERTY_NAME); |
| 64 | + if (file_extension.find(separator) != std::string::npos) |
| 65 | + { |
| 66 | + //multiple values detected. |
| 67 | + ra::strings::StringVector extension_list = ra::strings::Split(file_extension, separator.c_str()); |
| 68 | + if (!extension_list.empty()) |
| 69 | + file_extension = extension_list[0]; |
| 70 | + } |
| 71 | + |
| 72 | + // Search within previous resolutions |
| 73 | + IconResolutionInfoMap::const_iterator mapIt = mResolutions.find(file_extension); |
| 74 | + bool found = (mapIt != mResolutions.end()); |
| 75 | + if (found) |
| 76 | + { |
| 77 | + const ICON_RESOLUTION_INFO& resolution = mapIt->second; |
| 78 | + icon.SetPath(resolution.path); |
| 79 | + icon.SetIndex(resolution.index); |
| 80 | + icon.SetFileExtension(""); |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + // Generate an empty file with the required file extension |
| 85 | + IRandomService* random_service = App::GetInstance().GetRandomService(); |
| 86 | + if (random_service == NULL) |
| 87 | + { |
| 88 | + SA_LOG(ERROR) << "No Random service configured for resolving file extensions to icon."; |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + SA_LOG(INFO) << "Resolving icon for file extension '" << file_extension << "'."; |
| 93 | + |
| 94 | + // Generate a random filename in %TEMP% directory |
| 95 | + std::string temp_file_path; |
| 96 | + temp_file_path += ra::filesystem::GetTemporaryDirectoryUtf8(); |
| 97 | + temp_file_path += ra::filesystem::GetPathSeparatorStr(); |
| 98 | + temp_file_path += "icon_resolution"; // file prefix |
| 99 | + temp_file_path += ra::strings::ToString(random_service->GetRandomValue(10000,99999)); |
| 100 | + temp_file_path += "."; // file postfix |
| 101 | + temp_file_path += file_extension; |
| 102 | + |
| 103 | + // Create a blank file |
| 104 | + bool file_created = ra::filesystem::WriteFileUtf8(temp_file_path, file_extension); |
| 105 | + if (!file_created) |
| 106 | + { |
| 107 | + SA_LOG(ERROR) << "Failed to create sample file '" << temp_file_path << "' to resolve icon for file extension '" << file_extension << "'."; |
| 108 | + mFailures.insert(file_extension); |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + std::wstring temp_file_path_wide = ra::unicode::Utf8ToUnicode(temp_file_path); |
| 113 | + |
| 114 | + // Try to get an actual icon |
| 115 | + HINSTANCE hInstance = GetModuleHandle(NULL); |
| 116 | + wchar_t buffer[2 * MAX_PATH]; |
| 117 | + wsprintfW(buffer, L"%s", temp_file_path_wide.c_str()); |
| 118 | + WORD official_index = 0; |
| 119 | + WORD official_id = 0; |
| 120 | + HICON hIcon = ExtractAssociatedIconExW(hInstance, buffer, &official_index, &official_id); |
| 121 | + std::wstring official_file_path_wide = buffer; |
| 122 | + |
| 123 | + // Clean up |
| 124 | + ra::filesystem::DeleteFileUtf8(temp_file_path.c_str()); |
| 125 | + if (hIcon) |
| 126 | + DestroyIcon(hIcon); |
| 127 | + |
| 128 | + // Did we failed resolution? |
| 129 | + if (hIcon == NULL || official_file_path_wide == temp_file_path_wide) |
| 130 | + { |
| 131 | + DWORD dwLastError = GetLastError(); |
| 132 | + Win32Registry::REGISTRY_ICON unknown_file_icon = Win32Registry::GetUnknownFileTypeIcon(); |
| 133 | + SA_LOG(ERROR) << |
| 134 | + "Failed to find icon for file extension '" << file_extension << "' with ExtractAssociatedIconExW(). GetLastError(): " << ToHexString(dwLastError) << ". " |
| 135 | + "Resolving icon with default icon for unknown file type '" << unknown_file_icon.path << "' with index '" << unknown_file_icon.index << "'"; |
| 136 | + |
| 137 | + // Remember this result |
| 138 | + ICON_RESOLUTION_INFO info; |
| 139 | + info.path = unknown_file_icon.path; |
| 140 | + info.index = unknown_file_icon.index; |
| 141 | + info.id = -1; |
| 142 | + ResolveWith(icon, file_extension, info); |
| 143 | + |
| 144 | + // But also remember that its not a true resolution |
| 145 | + mFailures.insert(file_extension); |
| 146 | + |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + // Remember this result |
| 151 | + ICON_RESOLUTION_INFO info; |
| 152 | + info.path = ra::unicode::UnicodeToUtf8(official_file_path_wide); |
| 153 | + info.index = official_index; |
| 154 | + info.id = official_id; |
| 155 | + ResolveWith(icon, file_extension, info); |
| 156 | + |
| 157 | + SA_LOG(INFO) << "Resolved icon for file extension '" << file_extension << "' to file '" << info.path << "' with index '" << info.index << "'"; |
| 158 | + |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + bool WindowsIconResolutionService::HaveResolvedBefore(const std::string& file_extension) const |
| 163 | + { |
| 164 | + IconResolutionInfoMap::const_iterator mapIt = mResolutions.find(file_extension); |
| 165 | + bool found = (mapIt != mResolutions.end()); |
| 166 | + if (found) |
| 167 | + return true; |
| 168 | + |
| 169 | + // or did we resolved as a failure ? |
| 170 | + found = HaveFailedBefore(file_extension); |
| 171 | + return found; |
| 172 | + } |
| 173 | + |
| 174 | + bool WindowsIconResolutionService::HaveFailedBefore(const std::string& file_extension) const |
| 175 | + { |
| 176 | + bool have_failed_before = mFailures.find(file_extension) != mFailures.end(); |
| 177 | + return have_failed_before; |
| 178 | + } |
| 179 | + |
| 180 | + void WindowsIconResolutionService::ResolveWith(Icon& icon, const std::string& file_extension, const ICON_RESOLUTION_INFO& info) |
| 181 | + { |
| 182 | + icon.SetPath(info.path); |
| 183 | + icon.SetIndex(info.index); |
| 184 | + icon.SetFileExtension(""); |
| 185 | + |
| 186 | + // Remember this result |
| 187 | + mResolutions[file_extension] = info; |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | +} //namespace shellanything |
0 commit comments