Skip to content

Commit e20dc58

Browse files
committed
Modified IIconResolutionService: added new method HaveResolvedBefore() and HaveFailedBefore(). #164
1 parent 70a2c22 commit e20dc58

5 files changed

Lines changed: 78 additions & 7 deletions

File tree

src/core/IIconResolutionService.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ namespace shellanything
5656
/// <returns>Returns true if the operation is successful. Returns false otherwise.</returns>
5757
virtual bool ResolveFileExtensionIcon(Icon & icon) = 0;
5858

59+
/// <summary>
60+
/// Check if the given file extension have resolved before.
61+
/// </summary>
62+
/// <param name="file_entension">The file extension to check.</param>
63+
/// <returns>Returns true if the file extension has previously resolve to a valid icon. Returns false otherwise.</returns>
64+
virtual bool HaveResolvedBefore(const std::string& file_extension) const = 0;
65+
66+
/// <summary>
67+
/// Check if the given file extension have previously failed to resolve.
68+
/// </summary>
69+
/// <param name="file_entension">The file extension to check.</param>
70+
/// <returns>Returns true if the file extension has previously failed to resolve. Returns false otherwise.</returns>
71+
virtual bool HaveFailedBefore(const std::string& file_extension) const = 0;
72+
5973
};
6074

6175
} //namespace shellanything

src/core/Icon.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ namespace shellanything
8686

8787
void Icon::ResolveFileExtensionIcon()
8888
{
89+
IIconResolutionService* icon_resolution_service = App::GetInstance().GetIconResolutionService();
90+
if (icon_resolution_service == NULL)
91+
{
92+
SA_LOG(ERROR) << "No Icon Resolution service configured for resolving file extensions to icon.";
93+
return;
94+
}
95+
96+
shellanything::PropertyManager& pmgr = shellanything::PropertyManager::GetInstance();
97+
std::string file_extension = pmgr.Expand(mFileExtension);
98+
99+
const bool have_resolved_before = icon_resolution_service->HaveResolvedBefore(file_extension);
100+
const bool have_failed_before = icon_resolution_service->HaveFailedBefore(file_extension);
101+
102+
//Do the actual resolution
103+
bool success = icon_resolution_service->ResolveFileExtensionIcon(*this);
104+
105+
// Print a success/failure message once. Issue #98.
106+
if (success && !have_resolved_before)
107+
{
108+
SA_LOG(INFO) << "Resolving icon for file extension '" << file_extension << "' to file '" << mPath << "' with index '" << mIndex << "'";
109+
}
110+
else if (!success && !have_failed_before)
111+
{
112+
SA_LOG(WARNING) << "Failed to resolve icon for file extension '" << file_extension << "'.";
113+
}
89114
}
90115

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

src/tests/TestIcon.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ namespace shellanything
240240
ASSERT_EQ(UNKNOWN_ICON_INDEX, icon.GetIndex());
241241

242242
//act (issue #98)
243-
for (int i = 0; i < 500; i++)
243+
for (int i = 0; i < 100; i++)
244244
{
245245
//this should create multiple log entries if the feature is not properly implemented.
246246
Icon tmp_icon;
@@ -263,7 +263,8 @@ namespace shellanything
263263
ASSERT_TRUE(read) << "Failed to read log file: " << path;
264264

265265
int count = CountString(content, UNKNOWN_FILE_EXTENSION);
266-
ASSERT_LE(count, 1) << "Log file '" << path << "' contains " << count << " references to '" << UNKNOWN_FILE_EXTENSION << "'.";
266+
static const int MAX_COUNT = 2; // Issue 167. A maximum of 2 reference is allowed. One from LegacyIconResolutionService class and another from Icon::ResolveFileExtensionIcon().
267+
ASSERT_LE(count, MAX_COUNT) << "Log file '" << path << "' contains " << count << " references to '" << UNKNOWN_FILE_EXTENSION << "'.";
267268
}
268269
}
269270
}

src/windows/LegacyIconResolutionService.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434

3535
namespace shellanything
3636
{
37-
LegacyIconResolutionService::FileExtensionSet LegacyIconResolutionService::mUnresolvedFileExtensions;
38-
3937
LegacyIconResolutionService::LegacyIconResolutionService()
4038
{
4139
}
@@ -81,6 +79,8 @@ namespace shellanything
8179
icon.SetIndex(resolved_icon.index);
8280
icon.SetFileExtension("");
8381

82+
mResolvedFileExtensions.insert(file_extension);
83+
8484
return true;
8585
}
8686
else
@@ -92,8 +92,7 @@ namespace shellanything
9292
icon.SetIndex(unknown_file_icon.index);
9393
icon.SetFileExtension("");
9494

95-
const bool is_already_in_log = mUnresolvedFileExtensions.find(file_extension) != mUnresolvedFileExtensions.end();
96-
if (!is_already_in_log)
95+
if (!HaveFailedBefore(file_extension))
9796
{
9897
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 << "'";
9998

@@ -108,4 +107,21 @@ namespace shellanything
108107
return false;
109108
}
110109

110+
bool LegacyIconResolutionService::HaveResolvedBefore(const std::string& file_extension) const
111+
{
112+
const bool has_resolved_before = mResolvedFileExtensions.find(file_extension) != mResolvedFileExtensions.end();
113+
if (has_resolved_before)
114+
return true;
115+
116+
// or did we resolved as a failure ?
117+
const bool has_already_failed = HaveFailedBefore(file_extension);
118+
return has_already_failed;
119+
}
120+
121+
bool LegacyIconResolutionService::HaveFailedBefore(const std::string& file_extension) const
122+
{
123+
const bool has_already_failed = mUnresolvedFileExtensions.find(file_extension) != mUnresolvedFileExtensions.end();
124+
return has_already_failed;
125+
}
126+
111127
} //namespace shellanything

src/windows/LegacyIconResolutionService.h

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

55+
/// <summary>
56+
/// Check if the given file extension have resolved before.
57+
/// </summary>
58+
/// <param name="file_entension">The file extension to check.</param>
59+
/// <returns>Returns true if the file extension has previously resolve to a valid icon. Returns false otherwise.</returns>
60+
virtual bool HaveResolvedBefore(const std::string& file_extension) const;
61+
62+
/// <summary>
63+
/// Check if the given file extension have previously failed to resolve.
64+
/// </summary>
65+
/// <param name="file_extension">The file extension to check.</param>
66+
/// <returns>Returns true if the file extension has previously failed to resolve. Returns false otherwise.</returns>
67+
virtual bool HaveFailedBefore(const std::string& file_extension) const;
68+
5569
private:
5670
typedef std::set<std::string /*file extension*/> FileExtensionSet;
57-
static FileExtensionSet mUnresolvedFileExtensions;
71+
FileExtensionSet mResolvedFileExtensions;
72+
FileExtensionSet mUnresolvedFileExtensions;
5873

5974
};
6075

0 commit comments

Comments
 (0)