shadowm

Untitled

Sep 10th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. bool open_in_file_manager(const std::string& path)
  2. {
  3. #if defined(_X11)
  4.  
  5. #ifdef DE_USE_FORK_EXEC_WAIT
  6.  
  7.     LOG_DE << "open_in_file_manager(): on X11, will fork xdg-open\n";
  8.  
  9.     const pid_t child = fork();
  10.  
  11.     if(child == -1) {
  12.         ERR_DE << "open_in_file_manager(): fork() failed\n";
  13.         return false;
  14.     } else if(child == 0) {
  15.         execl("/usr/bin/xdg-open", "/usr/bin/xdg-open", path.c_str(), reinterpret_cast<char*>(NULL));
  16.         execl("/bin/xdg-open", "/bin/xdg-open", path.c_str(), reinterpret_cast<char*>(NULL));
  17.         // We shouldn't reach this point.
  18.         exit(1);
  19.     } else {
  20.         if(waitpid(child, NULL, 0) == -1) {
  21.             ERR_DE << "open_in_file_manager(): waitpid() failed\n";
  22.             exit(1);
  23.         }
  24.     }
  25.  
  26.     // Doing further diagnostics would be a waste of time, let's pretend
  27.     // xdg-open is always going to deliver rainbows and happiness to our
  28.     // users.
  29.  
  30.     return true;
  31.  
  32. #else // ! DE_USE_FORK_EXEC_WAIT
  33.  
  34.     LOG_DE << "open_in_file_manager(): on X11, will use xdg-open\n";
  35.  
  36.     const int res = system(("xdg-open " + path).c_str());
  37.  
  38.     if(res == -1) {
  39.         ERR_DE << "open_in_file_manager(): system() failed\n";
  40.     } else if(res != 0) {
  41.         ERR_DE << "open_in_file_manager(): xdg-open returned " << res << '\n';
  42.     } else {
  43.         return true;
  44.     }
  45.  
  46.     return false;
  47.  
  48. #endif
  49.  
  50. #elif defined(_WIN32) && defined(UNTESTED_DE_CODE_ON_WIN32)
  51.  
  52.     LOG_DE << "open_in_file_manager(): on Win32, will use ShellExecute()\n";
  53.  
  54.     std::wstring wpath = string_to_wstring(path);
  55.  
  56.     const int res = static_cast<int>(ShellExecute(NULL, L"open", wpath.data(), NULL, NULL, SW_SHOW));
  57.     if(res <= 32) {
  58.         ERR_DE << "open_in_file_manager(): ShellExecute() failed (" << res << ")\n";
  59.         return false;
  60.     }
  61.  
  62.     return true;
  63.  
  64. #elif defined(__APPLE__) && defined(UNTESTED_DE_CODE_ON_OSX)
  65.  
  66. #else
  67.  
  68.     ERR_DE << "open_in_file_manager(): unsupported platform\n";
  69.     return false;
  70.  
  71. #endif
  72. }
Advertisement
Add Comment
Please, Sign In to add comment