forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibGUI: Make common locations configurable
- Loading branch information
1 parent
fddcaaf
commit 91c210c
Showing
6 changed files
with
132 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[ | ||
{ "name": "Root", "path": "/" }, | ||
{ "name": "Home", "path": "/home/anon" }, | ||
{ "name": "Documents", "path": "/home/anon/Documents" }, | ||
{ "name": "Desktop", "path": "/home/anon/Desktop" }, | ||
{ "name": "Downloads", "path": "/home/anon/Downloads" } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2021, Dex♪ <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/JsonArray.h> | ||
#include <AK/JsonObject.h> | ||
#include <AK/LexicalPath.h> | ||
#include <AK/String.h> | ||
#include <AK/Vector.h> | ||
#include <LibCore/ConfigFile.h> | ||
#include <LibCore/File.h> | ||
#include <LibCore/StandardPaths.h> | ||
#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; | ||
|
||
auto user_config = String::formatted("{}/CommonLocations.json", Core::StandardPaths::config_directory()); | ||
if (Core::File::exists(user_config)) { | ||
CommonLocationsProvider::load_from_json(user_config); | ||
return; | ||
} | ||
|
||
// 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; | ||
} | ||
|
||
void CommonLocationsProvider::load_from_json(const String& json_path) | ||
{ | ||
auto file = Core::File::construct(json_path); | ||
if (!file->open(Core::IODevice::ReadOnly)) { | ||
dbgln("Unable to open {}", file->filename()); | ||
return; | ||
} | ||
|
||
auto json = JsonValue::from_string(file->read_all()); | ||
if (!json.has_value()) { | ||
dbgln("Common locations file {} is not a valid JSON file.", file->filename()); | ||
return; | ||
} | ||
if (!json.value().is_array()) { | ||
dbgln("Common locations file {} should contain a JSON array.", file->filename()); | ||
return; | ||
} | ||
|
||
s_common_locations.clear(); | ||
auto contents = json.value().as_array(); | ||
for (auto i = 0; i < contents.size(); ++i) { | ||
auto entry_value = contents.at(i); | ||
if (!entry_value.is_object()) | ||
continue; | ||
auto entry = entry_value.as_object(); | ||
auto name = entry.get("name").to_string(); | ||
auto path = entry.get("path").to_string(); | ||
s_common_locations.append({ name, path }); | ||
} | ||
|
||
s_initialized = true; | ||
} | ||
|
||
const Vector<CommonLocationsProvider::CommonLocation>& CommonLocationsProvider::common_locations() | ||
{ | ||
initialize_if_needed(); | ||
return s_common_locations; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2021, Dex♪ <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <AK/Forward.h> | ||
#include <LibGUI/Forward.h> | ||
#include <sys/types.h> | ||
|
||
namespace GUI { | ||
|
||
class CommonLocationsProvider { | ||
public: | ||
struct CommonLocation { | ||
String name; | ||
String path; | ||
}; | ||
|
||
static void load_from_json(const String& json_path); | ||
static const Vector<CommonLocation>& common_locations(); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters