2021-04-26 19:18:57 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
#include <AK/ByteString.h>
|
2021-04-26 19:18:57 +03:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/Vector.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2021-04-26 19:18:57 +03:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2023-03-21 18:35:30 +03:00
|
|
|
#include <LibFileSystem/FileSystem.h>
|
2021-04-26 19:18:57 +03:00
|
|
|
#include <LibGUI/CommonLocationsProvider.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
static bool s_initialized = false;
|
|
|
|
static Vector<CommonLocationsProvider::CommonLocation> s_common_locations;
|
|
|
|
|
|
|
|
static void initialize_if_needed()
|
|
|
|
{
|
|
|
|
if (s_initialized)
|
|
|
|
return;
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
auto user_config = ByteString::formatted("{}/CommonLocations.json", Core::StandardPaths::config_directory());
|
2023-03-21 18:35:30 +03:00
|
|
|
if (FileSystem::exists(user_config)) {
|
2023-01-09 00:25:28 +03:00
|
|
|
auto maybe_error = CommonLocationsProvider::load_from_json(user_config);
|
|
|
|
if (!maybe_error.is_error())
|
|
|
|
return;
|
|
|
|
dbgln("Unable to read Common Locations file: {}", maybe_error.error());
|
|
|
|
dbgln("Using the default set instead.");
|
2021-04-26 19:18:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback : If the user doesn't have custom locations, use some default ones.
|
|
|
|
s_common_locations.append({ "Root", "/" });
|
|
|
|
s_common_locations.append({ "Home", Core::StandardPaths::home_directory() });
|
|
|
|
s_common_locations.append({ "Downloads", Core::StandardPaths::downloads_directory() });
|
|
|
|
s_initialized = true;
|
|
|
|
}
|
|
|
|
|
2023-01-09 00:25:28 +03:00
|
|
|
ErrorOr<void> CommonLocationsProvider::load_from_json(StringView json_path)
|
2021-04-26 19:18:57 +03:00
|
|
|
{
|
2023-02-09 05:02:46 +03:00
|
|
|
auto file = TRY(Core::File::open(json_path, Core::File::OpenMode::Read));
|
2023-01-09 00:25:28 +03:00
|
|
|
auto json = JsonValue::from_string(TRY(file->read_until_eof()));
|
|
|
|
if (json.is_error())
|
|
|
|
return Error::from_string_literal("File is not a valid JSON");
|
|
|
|
if (!json.value().is_array())
|
|
|
|
return Error::from_string_literal("File must contain a JSON array");
|
2021-04-26 19:18:57 +03:00
|
|
|
|
|
|
|
s_common_locations.clear();
|
2021-11-15 03:46:51 +03:00
|
|
|
auto const& contents = json.value().as_array();
|
2021-06-28 12:57:37 +03:00
|
|
|
for (size_t i = 0; i < contents.size(); ++i) {
|
2021-04-26 19:18:57 +03:00
|
|
|
auto entry_value = contents.at(i);
|
|
|
|
if (!entry_value.is_object())
|
|
|
|
continue;
|
|
|
|
auto entry = entry_value.as_object();
|
2023-12-16 17:19:34 +03:00
|
|
|
auto name = entry.get_byte_string("name"sv).value_or({});
|
|
|
|
auto path = entry.get_byte_string("path"sv).value_or({});
|
2023-01-09 00:25:28 +03:00
|
|
|
TRY(s_common_locations.try_append({ name, path }));
|
2021-04-26 19:18:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
s_initialized = true;
|
2023-01-09 00:25:28 +03:00
|
|
|
return {};
|
2021-04-26 19:18:57 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Vector<CommonLocationsProvider::CommonLocation> const& CommonLocationsProvider::common_locations()
|
2021-04-26 19:18:57 +03:00
|
|
|
{
|
|
|
|
initialize_if_needed();
|
|
|
|
return s_common_locations;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|