Skip to content

Commit

Permalink
#2 : Global option to copy paths recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
clechasseur committed Jul 27, 2020
1 parent 4dbe7af commit a0647ad
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions PathCopyCopy/prihdr/PathCopyCopyContextMenuExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class ATL_NO_VTABLE CPathCopyCopyContextMenuExt :
HWND p_hWnd);
void AddQuotes(std::wstring& p_rName,
bool p_Optional) const;
PCC::FilesV GetFilesToActOn(bool p_Recursively) const;

void RemoveFromExtToMenu();
void CheckForUpdates();
Expand Down
1 change: 1 addition & 0 deletions PathCopyCopy/prihdr/PathCopyCopySettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace PCC
bool GetUsePreviewModeInMainMenu() const;
bool GetDropRedundantWords() const;
bool GetAlwaysShowSubmenu() const;
bool GetCopyPathsRecursively() const;
std::wstring GetPathsSeparator() const;
bool GetTrueLnkPaths() const;
std::wstring GetWSLPathPrefix() const;
Expand Down
48 changes: 46 additions & 2 deletions PathCopyCopy/src/PathCopyCopyContextMenuExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ HRESULT CPathCopyCopyContextMenuExt::ActOnFiles(const PCC::PluginSP& p_spPlugin,
const bool areQuotesOptional = GetSettings().GetAreQuotesOptional();
const bool makeEmailLinks = GetSettings().GetMakePathsIntoEmailLinks();
const StringUtils::EncodeParam encodeParam = GetSettings().GetEncodeParam();
const bool recursively = GetSettings().GetCopyPathsRecursively();
std::wstring pathsSeparator = p_spPlugin->PathsSeparator();
if (pathsSeparator.empty()) {
pathsSeparator = GetSettings().GetPathsSeparator();
Expand All @@ -1058,9 +1059,9 @@ HRESULT CPathCopyCopyContextMenuExt::ActOnFiles(const PCC::PluginSP& p_spPlugin,
}
}
std::wstring newFiles;
for (auto it = m_vFiles.cbegin(); it != m_vFiles.cend(); ++it) {
auto vFiles = GetFilesToActOn(recursively);
for (const auto& oldName : vFiles) {
// Ask plugin to compute filename using its scheme and save it.
const std::wstring& oldName = *it;
if (!newFiles.empty()) {
newFiles += pathsSeparator;
}
Expand Down Expand Up @@ -1113,6 +1114,49 @@ void CPathCopyCopyContextMenuExt::AddQuotes(std::wstring& p_rName,
}
}

//
// Returns the files to act on. If instructed, will be fetched recursively.
//
// @param p_Recursively Whether to fetch filenames recursively.
// @return List of files to act on.
//
PCC::FilesV CPathCopyCopyContextMenuExt::GetFilesToActOn(const bool p_Recursively) const
{
auto vFiles = m_vFiles;

if (p_Recursively) {
auto vNewFiles(vFiles);
while (!vNewFiles.empty()) {
PCC::FilesV vFilesToScan;
vFilesToScan.swap(vNewFiles);
for (const auto& fileToScan : vFilesToScan) {
auto attributes = ::GetFileAttributesW(fileToScan.c_str());
if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
WIN32_FIND_DATAW findData;
HANDLE hFind = ::FindFirstFileW((fileToScan + L"\\*").c_str(), &findData);
if (hFind != INVALID_HANDLE_VALUE) {
try {
do {
const std::wstring fileName = findData.cFileName;
if (fileName != L"." && fileName != L"..") {
vNewFiles.emplace_back(fileToScan + L"\\" + fileName);
}
} while (::FindNextFileW(hFind, &findData));
::FindClose(hFind);
} catch (...) {
::FindClose(hFind);
throw;
}
}
}
}
vFiles.insert(vFiles.end(), vNewFiles.begin(), vNewFiles.end());
}
}

return vFiles;
}

//
// Scans the vector keeping tracks of instances modifying menus
// and removes any reference to this instance from it.
Expand Down
21 changes: 21 additions & 0 deletions PathCopyCopy/src/PathCopyCopySettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace
const wchar_t* const SETTING_USE_PREVIEW_MODE_IN_MAIN_MENU = L"UsePreviewModeInMainMenu";
const wchar_t* const SETTING_DROP_REDUNDANT_WORDS = L"DropRedundantWords";
const wchar_t* const SETTING_ALWAYS_SHOW_SUBMENU = L"AlwaysShowSubmenu";
const wchar_t* const SETTING_COPY_PATHS_RECURSIVELY = L"CopyPathsRecursively";
const wchar_t* const SETTING_PATHS_SEPARATOR = L"PathsSeparator";
const wchar_t* const SETTING_TRUE_LNK_PATHS = L"TrueLnkPaths";
const wchar_t* const SETTING_WSL_PATH_PREFIX = L"WSLPathPrefix";
Expand Down Expand Up @@ -110,6 +111,7 @@ namespace
constexpr bool SETTING_USE_PREVIEW_MODE_IN_MAIN_MENU_DEFAULT = false;
constexpr bool SETTING_DROP_REDUNDANT_WORDS_DEFAULT = false;
constexpr bool SETTING_ALWAYS_SHOW_SUBMENU_DEFAULT = true;
constexpr bool SETTING_COPY_PATHS_RECURSIVELY_DEFAULT = false;
const wchar_t* const SETTING_PATHS_SEPARATOR_DEFAULT = L"";
constexpr bool SETTING_TRUE_LNK_PATHS_DEFAULT = false;
const wchar_t* const SETTING_WSL_PATH_PREFIX_DEFAULT = L"/mnt";
Expand Down Expand Up @@ -502,6 +504,25 @@ namespace PCC
return alwaysShowSubmenu;
}

//
// Checks whether to copy paths recursively.
//
// @return true if we need to copy paths recursively.
//
bool Settings::GetCopyPathsRecursively() const
{
// Perform late-revising.
Revise();

// Check if value exists. If so, read it, otherwise use default value.
bool copyPathsRecursively = SETTING_COPY_PATHS_RECURSIVELY_DEFAULT;
DWORD regCopyPathsRecursively = 0;
if (m_UserKey.QueryDWORDValue(SETTING_COPY_PATHS_RECURSIVELY, regCopyPathsRecursively) == ERROR_SUCCESS) {
copyPathsRecursively = regCopyPathsRecursively != 0;
}
return copyPathsRecursively;
}

//
// Returns the string to use between each path copied. An empty string
// instructs PCC to use the default value (usually a newline).
Expand Down

0 comments on commit a0647ad

Please sign in to comment.