From 0db159ef0f611ba014c59e773a59661b92ab7fde Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 21 Jul 2012 22:03:13 +0300 Subject: Add support for opening urls in system default browser. --- src/utils/process.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++++----- src/utils/process.h | 9 +++-- 2 files changed, 96 insertions(+), 10 deletions(-) (limited to 'src/utils') diff --git a/src/utils/process.cpp b/src/utils/process.cpp index 2d64f3a37..54f85b066 100644 --- a/src/utils/process.cpp +++ b/src/utils/process.cpp @@ -37,8 +37,8 @@ const int timeOut = 10; #include -int execFile(std::string pathName, std::string name A_UNUSED, - std::string arg1, std::string arg2, int waitTime) +int execFileWait(std::string pathName, std::string name A_UNUSED, + std::string arg1, std::string arg2, int waitTime) { if (!waitTime) waitTime = timeOut; @@ -74,21 +74,59 @@ int execFile(std::string pathName, std::string name A_UNUSED, return -1; } +bool execFile(std::string pathName, std::string name A_UNUSED, + std::string arg1, std::string arg2) +{ + STARTUPINFO siStartupInfo; + PROCESS_INFORMATION piProcessInfo; + memset(&siStartupInfo, 0, sizeof(siStartupInfo)); + memset(&piProcessInfo, 0, sizeof(piProcessInfo)); + siStartupInfo.cb = sizeof(siStartupInfo); + std::string args(pathName + " " + arg1); + if (!arg2.empty()) + args += " " + arg2; + + bool res = CreateProcess(pathName.c_str(), (char*)args.c_str(), nullptr, + nullptr, false, CREATE_DEFAULT_ERROR_MODE, nullptr, nullptr, + &siStartupInfo, &piProcessInfo); + + CloseHandle(piProcessInfo.hProcess); + CloseHandle(piProcessInfo.hThread); + return res; +} + +bool openBrowser(std::string url) +{ + return (int)ShellExecute(nullptr, "open", url.c_str(), nullptr, + nullptr, SW_SHOWNORMAL) > 32; +} + #elif defined(__APPLE__) -int execFile(std::string pathName, std::string name, - std::string arg1, std::string arg2, int waitTime) +int execFileWait(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime) { return -1; } +bool execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2) +{ + return false; +} + +bool openBrowser(std::string url) +{ + return false; +} + #elif defined __linux__ || defined __linux #include #include -int execFile(std::string pathName, std::string name, - std::string arg1, std::string arg2, int waitTime) +int execFileWait(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime) { pid_t mon_pid; int status; @@ -161,12 +199,55 @@ int execFile(std::string pathName, std::string name, return -1; } +bool execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2) +{ + int status; + + pid_t pid; + if ((pid = fork()) == -1) + { // fork error + return false; + } + else if (!pid) + { // work child + if (arg2.empty()) + { + execl(pathName.c_str(), name.c_str(), + arg1.c_str(), static_cast(nullptr)); + } + else + { + execl(pathName.c_str(), name.c_str(), arg1.c_str(), + arg2.c_str(), static_cast(nullptr)); + } + return false; + } + return true; +} + +bool openBrowser(std::string url) +{ + return execFile("/usr/bin/xdg-open", "/usr/bin/xdg-open", url, "") == 0; +} + #else -int execFile(std::string pathName, std::string name, - std::string arg1, std::string arg2, int waitTime) +int execFileWait(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime) { return -1; } +bool execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2) +{ + return false; +} + +bool openBrowser(std::string url) +{ + return false; +} + #endif diff --git a/src/utils/process.h b/src/utils/process.h index 12421729f..1895ed880 100644 --- a/src/utils/process.h +++ b/src/utils/process.h @@ -23,7 +23,12 @@ #include -int execFile(std::string pathName, std::string name, - std::string arg1, std::string arg2, int waitTime = 0); +int execFileWait(std::string pathName, std::string name, + std::string arg1, std::string arg2, int waitTime = 0); + +bool execFile(std::string pathName, std::string name, + std::string arg1, std::string arg2); + +bool openBrowser(std::string url); #endif // UTILS_PROCESS_H -- cgit v1.2.3-70-g09d2