| 1 |
/* |
| 2 |
* Copyright © 2016 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software: you can redistribute it and/or modify it |
| 5 |
* under the terms of the GNU General Public License version 3, as published |
| 6 |
* by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, but |
| 9 |
* WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
* PURPOSE. See the GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Authors: |
| 17 |
* Ted Gould <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifdef HAVE_LIBERTINE |
| 21 |
|
| 22 |
#include "application-impl-libertine.h" |
| 23 |
#include "libertine.h" |
| 24 |
#include "registry-impl.h" |
| 25 |
#include "string-util.h" |
| 26 |
|
| 27 |
#include <lomiri/util/GlibMemory.h> |
| 28 |
|
| 29 |
using namespace lomiri::util; |
| 30 |
|
| 31 |
namespace lomiri |
| 32 |
{ |
| 33 |
namespace app_launch |
| 34 |
{ |
| 35 |
namespace app_impls |
| 36 |
{ |
| 37 |
|
| 38 |
Libertine::Libertine(const AppID::Package& container, |
| 39 |
const AppID::AppName& appname, |
| 40 |
const std::shared_ptr<Registry::Impl>& registry) |
| 41 |
: Base(registry) |
| 42 |
, _container(container) |
| 43 |
, _appname(appname) |
| 44 |
{ |
| 45 |
auto gcontainer_path = unique_gchar(libertine_container_path(container.value().c_str())); |
| 46 |
if (gcontainer_path) |
| 47 |
{ |
| 48 |
_container_path = gcontainer_path.get(); |
| 49 |
} |
| 50 |
|
| 51 |
if (!_keyfile) |
| 52 |
{ |
| 53 |
auto system_app_path = unique_gchar(g_build_filename(_container_path.c_str(), "usr", "share", nullptr)); |
| 54 |
_basedir = system_app_path.get(); |
| 55 |
|
| 56 |
_keyfile = findDesktopFile(_basedir, "applications", appname.value() + ".desktop"); |
| 57 |
} |
| 58 |
|
| 59 |
if (!_keyfile) |
| 60 |
{ |
| 61 |
auto container_home_path = unique_gchar(libertine_container_home_path(container.value().c_str())); |
| 62 |
auto local_app_path = unique_gchar(g_build_filename(container_home_path.get(), ".local", "share", nullptr)); |
| 63 |
_basedir = local_app_path.get(); |
| 64 |
|
| 65 |
_keyfile = findDesktopFile(_basedir, "applications", appname.value() + ".desktop"); |
| 66 |
} |
| 67 |
|
| 68 |
if (!_keyfile) |
| 69 |
throw std::runtime_error{"Unable to find a keyfile for application '" + appname.value() + "' in container '" + |
| 70 |
container.value() + "'"}; |
| 71 |
|
| 72 |
appinfo_ = std::make_shared<app_info::Desktop>(appId(), _keyfile, _basedir, _container_path, |
| 73 |
app_info::DesktopFlags::NO_SPLASH, registry_); |
| 74 |
|
| 75 |
g_debug("Application Libertine object for container '%s' app '%s'", container.value().c_str(), |
| 76 |
appname.value().c_str()); |
| 77 |
} |
| 78 |
|
| 79 |
std::shared_ptr<GKeyFile> Libertine::keyfileFromPath(const std::string& pathname) |
| 80 |
{ |
| 81 |
auto keyfile = share_glib(g_key_file_new()); |
| 82 |
GError* error = nullptr; |
| 83 |
|
| 84 |
g_key_file_load_from_file(keyfile.get(), pathname.c_str(), G_KEY_FILE_NONE, &error); |
| 85 |
|
| 86 |
if (error != nullptr) |
| 87 |
{ |
| 88 |
g_error_free(error); |
| 89 |
return {}; |
| 90 |
} |
| 91 |
|
| 92 |
return keyfile; |
| 93 |
} |
| 94 |
|
| 95 |
std::shared_ptr<GKeyFile> Libertine::findDesktopFile(const std::string& basepath, |
| 96 |
const std::string& subpath, |
| 97 |
const std::string& filename) |
| 98 |
{ |
| 99 |
auto fullpath = unique_gchar(g_build_filename(basepath.c_str(), subpath.c_str(), filename.c_str(), nullptr)); |
| 100 |
std::string sfullpath(fullpath.get()); |
| 101 |
|
| 102 |
if (g_file_test(sfullpath.c_str(), G_FILE_TEST_IS_REGULAR)) |
| 103 |
{ |
| 104 |
return keyfileFromPath(sfullpath); |
| 105 |
} |
| 106 |
|
| 107 |
GError* error = nullptr; |
| 108 |
auto dirpath = unique_gchar(g_build_filename(basepath.c_str(), subpath.c_str(), nullptr)); |
| 109 |
auto dir = unique_glib(g_dir_open(dirpath.get(), 0, &error)); |
| 110 |
if (error != nullptr) |
| 111 |
{ |
| 112 |
g_error_free(error); |
| 113 |
return {}; |
| 114 |
} |
| 115 |
|
| 116 |
const gchar* file; |
| 117 |
while ((file = g_dir_read_name(dir.get())) != nullptr) |
| 118 |
{ |
| 119 |
auto new_subpath = unique_gchar(g_build_filename(subpath.c_str(), file, nullptr)); |
| 120 |
auto new_fullpath = unique_gchar(g_build_filename(basepath.c_str(), new_subpath.get(), nullptr)); |
| 121 |
if (g_file_test(new_fullpath.get(), G_FILE_TEST_IS_DIR)) |
| 122 |
{ |
| 123 |
auto desktop_file = findDesktopFile(basepath, new_subpath.get(), filename); |
| 124 |
|
| 125 |
if (desktop_file) |
| 126 |
{ |
| 127 |
return desktop_file; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return {}; |
| 133 |
} |
| 134 |
|
| 135 |
std::shared_ptr<Application::Info> Libertine::info() |
| 136 |
{ |
| 137 |
return appinfo_; |
| 138 |
} |
| 139 |
|
| 140 |
std::vector<std::shared_ptr<Application::Instance>> Libertine::instances() |
| 141 |
{ |
| 142 |
auto vbase = registry_->jobs()->instances(appId(), "application-legacy"); |
| 143 |
return std::vector<std::shared_ptr<Application::Instance>>(vbase.begin(), vbase.end()); |
| 144 |
} |
| 145 |
|
| 146 |
/** Grabs all the environment variables for the application to |
| 147 |
launch in. It sets up the confinement ones and then adds in |
| 148 |
the APP_EXEC line. |
| 149 |
|
| 150 |
This function adds 'libertine-launch' at the beginning of the |
| 151 |
Exec line with the container name as a parameter. The command |
| 152 |
can be overridden with the LOMIRI_APP_LAUNCH_LIBERTINE_LAUNCH |
| 153 |
environment variable. |
| 154 |
*/ |
| 155 |
std::list<std::pair<std::string, std::string>> Libertine::launchEnv() |
| 156 |
{ |
| 157 |
std::list<std::pair<std::string, std::string>> retval; |
| 158 |
|
| 159 |
info(); |
| 160 |
|
| 161 |
/* The container is our confinement */ |
| 162 |
retval.emplace_back(std::make_pair("APP_EXEC_POLICY", "unconfined")); |
| 163 |
|
| 164 |
auto libertine_launch = g_getenv("LOMIRI_APP_LAUNCH_LIBERTINE_LAUNCH"); |
| 165 |
if (libertine_launch == nullptr) |
| 166 |
{ |
| 167 |
libertine_launch = LIBERTINE_LAUNCH; |
| 168 |
} |
| 169 |
|
| 170 |
auto desktopexec = appinfo_->execLine().value(); |
| 171 |
auto execline = std::string(libertine_launch) + " \"--id=" + _container.value() + "\" " + desktopexec; |
| 172 |
retval.emplace_back(std::make_pair("APP_EXEC", execline)); |
| 173 |
|
| 174 |
/* TODO: Go multi instance */ |
| 175 |
retval.emplace_back(std::make_pair("INSTANCE_ID", "")); |
| 176 |
|
| 177 |
return retval; |
| 178 |
} |
| 179 |
|
| 180 |
std::shared_ptr<Application::Instance> Libertine::launch(const std::vector<Application::URL>& urls) |
| 181 |
{ |
| 182 |
auto instance = getInstance(appinfo_); |
| 183 |
std::function<std::list<std::pair<std::string, std::string>>(void)> envfunc = [this]() { return launchEnv(); }; |
| 184 |
return registry_->jobs()->launch(appId(), "application-legacy", instance, urls, jobs::manager::launchMode::STANDARD, |
| 185 |
envfunc); |
| 186 |
} |
| 187 |
|
| 188 |
std::shared_ptr<Application::Instance> Libertine::launchTest(const std::vector<Application::URL>& urls) |
| 189 |
{ |
| 190 |
auto instance = getInstance(appinfo_); |
| 191 |
std::function<std::list<std::pair<std::string, std::string>>(void)> envfunc = [this]() { return launchEnv(); }; |
| 192 |
return registry_->jobs()->launch(appId(), "application-legacy", instance, urls, jobs::manager::launchMode::TEST, |
| 193 |
envfunc); |
| 194 |
} |
| 195 |
|
| 196 |
std::shared_ptr<Application::Instance> Libertine::findInstance(const std::string& instanceid) |
| 197 |
{ |
| 198 |
return registry_->jobs()->existing(appId(), "application-legacy", instanceid, std::vector<Application::URL>{}); |
| 199 |
} |
| 200 |
|
| 201 |
} // namespace app_impls |
| 202 |
} // namespace app_launch |
| 203 |
} // namespace lomiri |
| 204 |
|
| 205 |
#endif // HAVE_LIBERTINE |