LibCore: Add StandardPaths thing to retrieve various standard locations

Fixes #1853.
This commit is contained in:
Andreas Kling 2020-04-19 19:57:05 +02:00
parent 3b434068eb
commit c45e16f605
Notes: sideshowbarker 2024-07-19 07:27:23 +09:00
10 changed files with 51 additions and 25 deletions

View file

@ -33,7 +33,7 @@
#include <LibCore/ConfigFile.h>
#include <LibCore/DesktopServices.h>
#include <LibCore/MimeData.h>
#include <LibCore/UserInfo.h>
#include <LibCore/StandardPaths.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
#include <LibGUI/ActionGroup.h>
@ -91,7 +91,7 @@ int main(int argc, char** argv)
}
if (app.args().contains_slow("--desktop") || app.args().contains_slow("-d"))
return run_in_desktop_mode(move(config), String::format("%s/Desktop", get_current_user_home_path().characters()));
return run_in_desktop_mode(move(config), Core::StandardPaths::desktop_directory());
// our initial location is defined as, in order of precedence:
// 1. the first command-line argument (e.g. FileManager /bin)
@ -103,7 +103,7 @@ int main(int argc, char** argv)
initial_location = argv[1];
if (initial_location.is_empty())
initial_location = get_current_user_home_path();
initial_location = Core::StandardPaths::home_directory();
if (initial_location.is_empty())
initial_location = "/";
@ -530,7 +530,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
auto go_home_action = GUI::CommonActions::make_go_home_action(
[&](auto&) {
directory_view.open(get_current_user_home_path());
directory_view.open(Core::StandardPaths::home_directory());
},
window);

View file

@ -28,7 +28,7 @@
#include <AK/QuickSort.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/DirIterator.h>
#include <LibCore/UserInfo.h>
#include <LibCore/StandardPaths.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/Desktop.h>
@ -81,7 +81,7 @@ int main(int argc, char** argv)
return 1;
}
if (chdir(get_current_user_home_path().characters()) < 0) {
if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
perror("chdir");
return 1;
}

View file

@ -28,7 +28,7 @@
#include "TaskbarButton.h"
#include <AK/SharedBuffer.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/UserInfo.h>
#include <LibCore/StandardPaths.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Desktop.h>
@ -105,7 +105,7 @@ void TaskbarWindow::create_quick_launch_bar()
if (pid < 0) {
perror("fork");
} else if (pid == 0) {
if (chdir(get_current_user_home_path().characters()) < 0) {
if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
perror("chdir");
exit(1);
}

View file

@ -26,7 +26,6 @@
#include <Kernel/KeyCode.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/UserInfo.h>
#include <LibGUI/AboutDialog.h>
#include <LibGUI/Action.h>
#include <LibGUI/ActionGroup.h>

View file

@ -27,7 +27,7 @@
#include <AK/StringBuilder.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/File.h>
#include <LibCore/UserInfo.h>
#include <LibCore/StandardPaths.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
@ -36,9 +36,7 @@ namespace Core {
NonnullRefPtr<ConfigFile> ConfigFile::get_for_app(const String& app_name)
{
String home_path = get_current_user_home_path();
if (home_path == "/")
home_path = String::format("/tmp");
String home_path = StandardPaths::home_directory();
auto path = String::format("%s/%s.ini", home_path.characters(), app_name.characters());
return adopt(*new ConfigFile(path));
}

View file

@ -23,12 +23,12 @@ OBJS = \
ProcessStatisticsReader.o \
Socket.o \
SocketAddress.o \
StandardPaths.o \
TCPServer.o \
TCPSocket.o \
Timer.o \
UDPServer.o \
UDPSocket.o \
UserInfo.o \
puff.o
LIBRARY = libcore.a

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -24,18 +24,38 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibCore/UserInfo.h>
#include <AK/FileSystemPath.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibCore/StandardPaths.h>
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
String get_current_user_home_path()
namespace Core {
String StandardPaths::home_directory()
{
if (auto* home_env = getenv("HOME"))
return home_env;
return canonicalized_path(home_env);
auto* pwd = getpwuid(getuid());
String path = pwd ? pwd->pw_dir : "/";
endpwent();
return path;
return canonicalized_path(path);
}
String StandardPaths::desktop_directory()
{
StringBuilder builder;
builder.append(home_directory());
builder.append("/Desktop");
return canonicalized_path(builder.to_string());
}
String StandardPaths::tempfile_directory()
{
return "/tmp";
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -26,6 +26,15 @@
#pragma once
#include <AK/String.h>
#include <AK/Forward.h>
String get_current_user_home_path();
namespace Core {
class StandardPaths {
public:
static String home_directory();
static String desktop_directory();
static String tempfile_directory();
};
}

View file

@ -128,7 +128,7 @@ FilePicker::FilePicker(Mode mode, const StringView& file_name, const StringView&
toolbar.add_action(*open_parent_directory_action);
auto go_home_action = CommonActions::make_go_home_action([this](auto&) {
m_model->set_root_path(get_current_user_home_path());
m_model->set_root_path(Core::StandardPaths::home_directory());
});
toolbar.add_action(go_home_action);
toolbar.add_separator();

View file

@ -26,7 +26,7 @@
#include <AK/FileSystemPath.h>
#include <AK/Optional.h>
#include <LibCore/UserInfo.h>
#include <LibCore/StandardPaths.h>
#include <LibGUI/Dialog.h>
namespace GUI {
@ -52,7 +52,7 @@ private:
void clear_preview();
void on_file_return();
FilePicker(Mode type = Mode::Open, const StringView& file_name = "Untitled", const StringView& path = String(get_current_user_home_path()), Window* parent_window = nullptr);
FilePicker(Mode type = Mode::Open, const StringView& file_name = "Untitled", const StringView& path = Core::StandardPaths::home_directory(), Window* parent_window = nullptr);
static String ok_button_name(Mode mode)
{