1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:07:34 +00:00

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
10 changed files with 51 additions and 25 deletions

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();
};
}