2020-04-19 11:33:26 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-04-22 23:51:19 +03:00
|
|
|
* Copyright (c) 2020, Linus Groh <linusg@serenityos.org>
|
2020-04-19 11:33:26 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-19 11:33:26 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/URL.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-05-14 21:14:03 +03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-04-26 22:30:01 +03:00
|
|
|
#include <LibDesktop/Launcher.h>
|
2023-05-20 02:40:19 +03:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2022-01-30 22:01:49 +03:00
|
|
|
#include <LibMain/Main.h>
|
2020-04-19 11:33:26 +03:00
|
|
|
|
2022-01-30 22:01:49 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-04-19 11:33:26 +03:00
|
|
|
{
|
2020-05-14 21:14:03 +03:00
|
|
|
Core::EventLoop loop;
|
2022-01-30 22:17:42 +03:00
|
|
|
Vector<StringView> urls_or_paths;
|
2020-04-19 11:33:26 +03:00
|
|
|
Core::ArgsParser parser;
|
2020-12-05 18:22:58 +03:00
|
|
|
parser.set_general_help("Open a file or URL by executing the appropriate program.");
|
2020-04-19 11:33:26 +03:00
|
|
|
parser.add_positional_argument(urls_or_paths, "URL or file path to open", "url-or-path");
|
2022-01-30 22:01:49 +03:00
|
|
|
parser.parse(arguments);
|
2020-04-19 11:33:26 +03:00
|
|
|
|
|
|
|
bool all_ok = true;
|
|
|
|
|
|
|
|
for (auto& url_or_path : urls_or_paths) {
|
2023-10-03 23:50:32 +03:00
|
|
|
auto path_or_error = FileSystem::real_path(url_or_path);
|
|
|
|
URL url;
|
|
|
|
if (path_or_error.is_error()) {
|
|
|
|
url = url_or_path;
|
|
|
|
if (!url.is_valid()) {
|
|
|
|
warnln("Failed to open: '{}': {}", url_or_path, strerror(path_or_error.error().code()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-15 19:23:24 +03:00
|
|
|
url = URL::create_with_url_or_path(path_or_error.value());
|
2023-10-03 23:50:32 +03:00
|
|
|
}
|
2020-08-08 12:46:44 +03:00
|
|
|
|
2020-05-10 00:57:43 +03:00
|
|
|
if (!Desktop::Launcher::open(url)) {
|
2020-12-24 03:28:30 +03:00
|
|
|
warnln("Failed to open '{}'", url);
|
2020-05-10 00:57:43 +03:00
|
|
|
all_ok = false;
|
|
|
|
}
|
2020-04-19 11:33:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return all_ok ? 0 : 1;
|
|
|
|
}
|