shadowm

Untitled

Sep 22nd, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. bool open_in_file_manager(const std::string& path)
  2. {
  3. #if defined(_X11) || defined(__APPLE__)
  4.  
  5. #ifndef __APPLE__
  6.     LOG_DE << "open_in_file_manager(): on X11, will use xdg-open\n";
  7.     const char launcher[] = "xdg-open";
  8. #else
  9.     LOG_DE << "open_in_file_manager(): on OS X, will use open\n";
  10.     const char launcher[] = "open";
  11. #endif
  12.  
  13.     int child_status = 0;
  14.     const pid_t child = fork();
  15.  
  16.     if(child == -1) {
  17.         ERR_DE << "open_in_file_manager(): fork() failed\n";
  18.         return false;
  19.     } else if(child == 0) {
  20.         execlp(launcher, launcher, path.c_str(), reinterpret_cast<char*>(NULL));
  21.         _exit(1); // This shouldn't happen.
  22.     } else if(waitpid(child, &child_status, 0) == -1) {
  23.         ERR_DE << "open_in_file_manager(): waitpid() failed\n";
  24.         return false;
  25.     }
  26.  
  27.     if(child_status) {
  28.         if(WIFEXITED(child_status)) {
  29.             ERR_DE << "open_in_file_manager(): " << launcher << " returned "
  30.                    << WEXITSTATUS(child_status) << '\n';
  31.         } else {
  32.             ERR_DE << "open_in_file_manager(): " << launcher << " failed\n";
  33.         }
  34.  
  35.         return false;
  36.     }
  37.  
  38.     return true;
  39.  
  40. #elif defined(_WIN32)
  41.  
  42.     LOG_DE << "open_in_file_manager(): on Win32, will use ShellExecute()\n";
  43.  
  44.     const wide_string& wpath = utils::string_to_wstring(path);
  45.  
  46.     const int res = reinterpret_cast<int>(ShellExecute(NULL, L"open", wpath.data(), NULL, NULL, SW_SHOW));
  47.     if(res <= 32) {
  48.         ERR_DE << "open_in_file_manager(): ShellExecute() failed (" << res << ")\n";
  49.         return false;
  50.     }
  51.  
  52.     return true;
  53.  
  54. #else
  55.  
  56.     ERR_DE << "open_in_file_manager(): unsupported platform\n";
  57.     return false;
  58.  
  59. #endif
  60. }
Advertisement
Add Comment
Please, Sign In to add comment