Skip to content

Commit c5bd769

Browse files
committed
Fixed <open> command for directories and urls.
1 parent 22804cd commit c5bd769

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/ActionOpen.cpp

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,26 @@
2424

2525
#include "shellanything/ActionOpen.h"
2626
#include "rapidassist/process.h"
27+
#include "rapidassist/filesystem.h"
2728
#include "PropertyManager.h"
2829
#include "Platform.h"
30+
#include "utf_strings.h"
2931

3032
#pragma warning( push )
3133
#pragma warning( disable: 4355 ) // glog\install_dir\include\glog/logging.h(1167): warning C4355: 'this' : used in base member initializer list
3234
#include <glog/logging.h>
3335
#pragma warning( pop )
3436

37+
//#define WIN32_LEAN_AND_MEAN 1
38+
#include <windows.h>
39+
#undef GetEnvironmentVariable
40+
#undef DeleteFile
41+
#undef CreateDirectory
42+
#undef CopyFile
43+
#undef CreateFile
44+
45+
#pragma comment(lib, "Urlmon.lib") //for IsValidURL()
46+
3547
namespace shellanything
3648
{
3749

@@ -43,17 +55,58 @@ namespace shellanything
4355
{
4456
}
4557

58+
bool OpenPathGeneric(const std::string & iPath) {
59+
SHELLEXECUTEINFO info = { 0 };
60+
61+
info.cbSize = sizeof(SHELLEXECUTEINFO);
62+
63+
info.fMask |= SEE_MASK_NOCLOSEPROCESS;
64+
info.fMask |= SEE_MASK_NOASYNC;
65+
info.fMask |= SEE_MASK_FLAG_DDEWAIT;
66+
67+
info.hwnd = HWND_DESKTOP;
68+
info.nShow = SW_SHOWDEFAULT;
69+
info.lpVerb = "open";
70+
info.lpFile = iPath.c_str();
71+
info.lpParameters = NULL; //arguments
72+
info.lpDirectory = NULL; // default directory
73+
74+
BOOL success = ShellExecuteEx(&info);
75+
return (success == TRUE);
76+
}
77+
4678
bool ActionOpen::execute(const Context & iContext) const
4779
{
4880
PropertyManager & pmgr = PropertyManager::getInstance();
4981
std::string path = pmgr.expand(mPath);
5082

51-
//debug
52-
LOG(INFO) << "Open file '" << path << "'";
83+
//is path a file?
84+
if (ra::filesystem::FileExists(path.c_str()))
85+
{
86+
LOG(INFO) << "Open file '" << path << "'";
87+
uint32_t pId = ra::process::OpenDocument(path);
88+
return pId != ra::process::INVALID_PROCESS_ID;
89+
}
90+
91+
//is path a directory?
92+
if (ra::filesystem::DirectoryExists(path.c_str()))
93+
{
94+
LOG(INFO) << "Open directory '" << path << "'";
95+
bool success = OpenPathGeneric(path);
96+
return success;
97+
}
5398

54-
uint32_t pId = ra::process::OpenDocument(path);
99+
//is path a valid url?
100+
std::wstring wide_path = encoding::utf::ansi_to_unicode(path);
101+
if (IsValidURL(NULL, wide_path.c_str(), 0) == S_OK)
102+
{
103+
LOG(INFO) << "Open url '" << path << "'";
104+
bool success = OpenPathGeneric(path);
105+
return success;
106+
}
55107

56-
return pId != 0;
108+
LOG(ERROR) << "Unable to open '" << path << "'";
109+
return false; //file not found
57110
}
58111

59112
const std::string & ActionOpen::getPath() const

src/shellext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,13 @@ HRESULT STDMETHODCALLTYPE CContextMenu::InvokeCommand(LPCMINVOKECOMMANDINFO lpcm
622622
const shellanything::Action * action = actions[i];
623623
if (action)
624624
{
625-
SetLastError(0); //reset win32 error code in case the action fails.
625+
ra::errors::ResetLastErrorCode(); //reset win32 error code in case the action fails.
626626
bool success = action->execute(m_Context);
627627

628628
if (!success)
629629
{
630630
//try to get an error mesage from win32
631-
uint32_t dwError = ra::errors::GetLastErrorCode();
631+
ra::errors::errorcode_t dwError = ra::errors::GetLastErrorCode();
632632
if (dwError)
633633
{
634634
std::string error_message = ra::errors::GetErrorCodeDescription(dwError);

0 commit comments

Comments
 (0)