Skip to content

Commit 70a2c22

Browse files
committed
Moved legacy implementation of Icon::ResolveFileExtensionIcon() to LegacyIconResolutionService::ResolveFileExtensionIcon(Icon& icon).
1 parent bf00d99 commit 70a2c22

4 files changed

Lines changed: 76 additions & 59 deletions

File tree

src/core/Icon.cpp

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,16 @@
2323
*********************************************************************************/
2424

2525
#include "Icon.h"
26-
#include "SelectionContext.h"
27-
#include "PropertyManager.h"
28-
#include "Win32Registry.h"
26+
#include "App.h"
2927
#include "LoggerHelper.h"
28+
#include "PropertyManager.h"
29+
#include "SelectionContext.h"
3030
#include "SaUtils.h"
3131

3232
#include "rapidassist/strings.h"
33-
#include "rapidassist/environment.h"
3433

3534
namespace shellanything
3635
{
37-
Icon::FileExtensionSet Icon::mUnresolvedFileExtensions;
38-
3936
Icon::Icon() :
4037
mIndex(0) // As per documentation, "If the index is not specified, the value 0 is used." See Issue #17, #150 and #155.
4138
{
@@ -89,57 +86,6 @@ namespace shellanything
8986

9087
void Icon::ResolveFileExtensionIcon()
9188
{
92-
//is this menu have a file extension ?
93-
shellanything::PropertyManager& pmgr = shellanything::PropertyManager::GetInstance();
94-
std::string file_extension = pmgr.Expand(mFileExtension);
95-
if (!file_extension.empty())
96-
{
97-
//check for multiple values. keep the first value, forget about other selected file extensions.
98-
const std::string separator = pmgr.GetProperty(SelectionContext::MULTI_SELECTION_SEPARATOR_PROPERTY_NAME);
99-
if (file_extension.find(separator) != std::string::npos)
100-
{
101-
//multiple values detected.
102-
ra::strings::StringVector extension_list = ra::strings::Split(file_extension, separator.c_str());
103-
if (!extension_list.empty())
104-
file_extension = extension_list[0];
105-
}
106-
107-
//try to find the path to the icon module for the given file extension.
108-
Win32Registry::REGISTRY_ICON resolved_icon = Win32Registry::GetFileTypeIcon(file_extension.c_str());
109-
110-
//An icon with a negative index is valid from the registry.
111-
//Only the special case index = -1 should be considered invalid (Issue #17).
112-
//And ShellAnything accept positive (index) and negative index (resource id). (Issue #155, Issue #164).
113-
//See issue #17, 155, 164.
114-
if (Win32Registry::IsValid(resolved_icon))
115-
{
116-
//found the icon for the file extension
117-
//replace this menu's icon with the new information
118-
SA_LOG(INFO) << "Resolving icon for file extension '" << file_extension << "' to file '" << resolved_icon.path << "' with index '" << resolved_icon.index << "'";
119-
mPath = resolved_icon.path;
120-
mIndex = resolved_icon.index;
121-
mFileExtension = "";
122-
}
123-
else
124-
{
125-
//failed to find a valid icon.
126-
//using the default "unknown" icon
127-
Win32Registry::REGISTRY_ICON unknown_file_icon = Win32Registry::GetUnknownFileTypeIcon();
128-
mPath = unknown_file_icon.path;
129-
mIndex = unknown_file_icon.index;
130-
mFileExtension = "";
131-
132-
//show the message only once in logs
133-
const bool is_already_in_log = mUnresolvedFileExtensions.find(file_extension) != mUnresolvedFileExtensions.end();
134-
if (!is_already_in_log)
135-
{
136-
SA_LOG(WARNING) << "Failed to find icon for file extension '" << file_extension << "'. Resolving icon with default icon for unknown file type '" << unknown_file_icon.path << "' with index '" << unknown_file_icon.index << "'";
137-
138-
//remember this failure.
139-
mUnresolvedFileExtensions.insert(file_extension);
140-
}
141-
}
142-
}
14389
}
14490

14591
const std::string& Icon::GetFileExtension() const

src/core/Icon.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ namespace shellanything
119119
virtual void ToLongString(std::string& str, int indent) const;
120120

121121
private:
122-
typedef std::set<std::string /*file extension*/> FileExtensionSet;
123-
static FileExtensionSet mUnresolvedFileExtensions;
124122
std::string mFileExtension;
125123
std::string mPath;
126124
int mIndex;

src/windows/LegacyIconResolutionService.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,19 @@
2323
*********************************************************************************/
2424

2525
#include "LegacyIconResolutionService.h"
26+
#include "PropertyManager.h"
27+
#include "SelectionContext.h"
28+
#include "Win32Registry.h"
29+
#include "LoggerHelper.h"
30+
#include "SaUtils.h"
31+
32+
#include "rapidassist/strings.h"
33+
#include "rapidassist/environment.h"
2634

2735
namespace shellanything
2836
{
37+
LegacyIconResolutionService::FileExtensionSet LegacyIconResolutionService::mUnresolvedFileExtensions;
38+
2939
LegacyIconResolutionService::LegacyIconResolutionService()
3040
{
3141
}
@@ -36,6 +46,65 @@ namespace shellanything
3646

3747
bool LegacyIconResolutionService::ResolveFileExtensionIcon(Icon& icon)
3848
{
49+
//is this icon have a file extension ?
50+
std::string original_file_extension = icon.GetFileExtension();
51+
if (original_file_extension.empty())
52+
return false; //nothing to resolve
53+
54+
shellanything::PropertyManager& pmgr = shellanything::PropertyManager::GetInstance();
55+
std::string file_extension = pmgr.Expand(original_file_extension);
56+
if (!file_extension.empty())
57+
{
58+
//check for multiple values. keep the first value, forget about other selected file extensions.
59+
const std::string separator = pmgr.GetProperty(SelectionContext::MULTI_SELECTION_SEPARATOR_PROPERTY_NAME);
60+
if (file_extension.find(separator) != std::string::npos)
61+
{
62+
//multiple values detected.
63+
ra::strings::StringVector extension_list = ra::strings::Split(file_extension, separator.c_str());
64+
if (!extension_list.empty())
65+
file_extension = extension_list[0];
66+
}
67+
68+
//try to find the path to the icon module for the given file extension.
69+
Win32Registry::REGISTRY_ICON resolved_icon = Win32Registry::GetFileTypeIcon(file_extension.c_str());
70+
71+
//An icon with a negative index is valid from the registry.
72+
//Only the special case index = -1 should be considered invalid (Issue #17).
73+
//And ShellAnything accept positive (index) and negative index (resource id). (Issue #155, Issue #164).
74+
//See issue #17, 155, 164.
75+
if (Win32Registry::IsValid(resolved_icon))
76+
{
77+
//found the icon for the file extension
78+
//replace this menu's icon with the new information
79+
SA_LOG(INFO) << "Resolving icon for file extension '" << file_extension << "' to file '" << resolved_icon.path << "' with index '" << resolved_icon.index << "'";
80+
icon.SetPath(resolved_icon.path);
81+
icon.SetIndex(resolved_icon.index);
82+
icon.SetFileExtension("");
83+
84+
return true;
85+
}
86+
else
87+
{
88+
//failed to find a valid icon.
89+
//using the default "unknown" icon
90+
Win32Registry::REGISTRY_ICON unknown_file_icon = Win32Registry::GetUnknownFileTypeIcon();
91+
icon.SetPath(unknown_file_icon.path);
92+
icon.SetIndex(unknown_file_icon.index);
93+
icon.SetFileExtension("");
94+
95+
const bool is_already_in_log = mUnresolvedFileExtensions.find(file_extension) != mUnresolvedFileExtensions.end();
96+
if (!is_already_in_log)
97+
{
98+
SA_LOG(WARNING) << "Failed to find icon for file extension '" << file_extension << "'. Resolving icon with default icon for unknown file type '" << unknown_file_icon.path << "' with index '" << unknown_file_icon.index << "'";
99+
100+
//remember this failure.
101+
mUnresolvedFileExtensions.insert(file_extension);
102+
}
103+
104+
return true;
105+
}
106+
}
107+
39108
return false;
40109
}
41110

src/windows/LegacyIconResolutionService.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ namespace shellanything
5252
/// <returns>Returns true if the operation is successful. Returns false otherwise.</returns>
5353
virtual bool ResolveFileExtensionIcon(Icon& icon);
5454

55+
private:
56+
typedef std::set<std::string /*file extension*/> FileExtensionSet;
57+
static FileExtensionSet mUnresolvedFileExtensions;
58+
5559
};
5660

5761
} //namespace shellanything

0 commit comments

Comments
 (0)