2013-08-08 21:01:20 -03:00
|
|
|
// Aseprite Base Library
|
2015-02-12 12:46:56 -03:00
|
|
|
// Copyright (c) 2001-2013, 2015 David Capello
|
2013-08-05 21:20:19 -03:00
|
|
|
//
|
2014-03-29 20:08:05 -03:00
|
|
|
// This file is released under the terms of the MIT license.
|
|
|
|
// Read LICENSE.txt for more information.
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "base/exception.h"
|
2014-04-17 01:18:24 -03:00
|
|
|
#include "base/fs.h"
|
|
|
|
#include "base/launcher.h"
|
2013-10-14 19:58:11 -03:00
|
|
|
#include "base/string.h"
|
2013-08-05 21:20:19 -03:00
|
|
|
|
2015-02-12 12:46:56 -03:00
|
|
|
#ifdef _WIN32
|
2013-08-05 21:20:19 -03:00
|
|
|
#include <windows.h>
|
|
|
|
#ifndef SEE_MASK_DEFAULT
|
|
|
|
#define SEE_MASK_DEFAULT 0x00000000
|
|
|
|
#endif
|
|
|
|
|
2013-10-14 19:58:11 -03:00
|
|
|
static int win32_shell_execute(const wchar_t* verb, const wchar_t* file, const wchar_t* params)
|
2013-08-05 21:20:19 -03:00
|
|
|
{
|
|
|
|
SHELLEXECUTEINFO sh;
|
|
|
|
ZeroMemory((LPVOID)&sh, sizeof(sh));
|
|
|
|
sh.cbSize = sizeof(sh);
|
|
|
|
sh.fMask = SEE_MASK_DEFAULT;
|
|
|
|
sh.lpVerb = verb;
|
|
|
|
sh.lpFile = file;
|
|
|
|
sh.lpParameters = params;
|
|
|
|
sh.nShow = SW_SHOWNORMAL;
|
|
|
|
|
|
|
|
if (!ShellExecuteEx(&sh)) {
|
|
|
|
int ret = GetLastError();
|
|
|
|
#if 0
|
|
|
|
if (ret != 0) {
|
|
|
|
DWORD flags =
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS;
|
|
|
|
LPSTR msgbuf;
|
|
|
|
|
|
|
|
if (FormatMessageA(flags, NULL, ret,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
reinterpret_cast<LPSTR>(&msgbuf),
|
|
|
|
0, NULL)) {
|
|
|
|
ui::Alert::show("Problem<<Cannot open:<<%s<<%s||&Close", file, msgbuf);
|
|
|
|
LocalFree(msgbuf);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2015-02-12 12:46:56 -03:00
|
|
|
#endif // _WIN32
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
namespace base {
|
|
|
|
namespace launcher {
|
|
|
|
|
|
|
|
bool open_url(const std::string& url)
|
|
|
|
{
|
|
|
|
return open_file(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool open_file(const std::string& file)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
2015-02-12 12:46:56 -03:00
|
|
|
#ifdef _WIN32
|
2013-08-05 21:20:19 -03:00
|
|
|
|
2013-10-14 19:58:11 -03:00
|
|
|
ret = win32_shell_execute(L"open",
|
|
|
|
base::from_utf8(file).c_str(), NULL);
|
2013-08-05 21:20:19 -03:00
|
|
|
|
|
|
|
#elif __APPLE__
|
|
|
|
|
|
|
|
ret = system(("open \"" + file + "\"").c_str());
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
ret = system(("xdg-open \"" + file + "\"").c_str());
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool open_folder(const std::string& file)
|
|
|
|
{
|
2015-02-12 12:46:56 -03:00
|
|
|
#ifdef _WIN32
|
2014-08-13 00:35:34 -03:00
|
|
|
|
2014-04-17 01:18:24 -03:00
|
|
|
int ret;
|
2014-04-17 17:12:55 -03:00
|
|
|
if (base::is_directory(file)) {
|
2014-04-17 01:18:24 -03:00
|
|
|
ret = win32_shell_execute(NULL, L"explorer",
|
|
|
|
(L"/n,/e,\"" + base::from_utf8(file) + L"\"").c_str());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = win32_shell_execute(NULL, L"explorer",
|
|
|
|
(L"/e,/select,\"" + base::from_utf8(file) + L"\"").c_str());
|
|
|
|
}
|
2013-08-05 21:20:19 -03:00
|
|
|
return (ret == 0);
|
2014-08-13 00:35:34 -03:00
|
|
|
|
|
|
|
#elif __APPLE__
|
|
|
|
|
2014-08-23 23:53:31 -03:00
|
|
|
int ret;
|
|
|
|
if (base::is_directory(file)) {
|
|
|
|
ret = system(("open \"" + file + "\"").c_str());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = system(("open --reveal \"" + file + "\"").c_str());
|
|
|
|
}
|
2014-08-13 00:35:34 -03:00
|
|
|
return (ret == 0);
|
|
|
|
|
2013-08-05 21:20:19 -03:00
|
|
|
#else
|
2014-08-13 00:35:34 -03:00
|
|
|
|
2013-08-05 21:20:19 -03:00
|
|
|
return false;
|
2014-08-13 00:35:34 -03:00
|
|
|
|
2013-08-05 21:20:19 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool support_open_folder()
|
|
|
|
{
|
2015-02-12 12:46:56 -03:00
|
|
|
#if defined(_WIN32) || defined(__APPLE__)
|
2013-08-05 21:20:19 -03:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace launcher
|
|
|
|
} // namespace base
|