Skip to content

Commit c45e16f

Browse files
committed
LibCore: Add StandardPaths thing to retrieve various standard locations
Fixes #1853.
1 parent 3b43406 commit c45e16f

File tree

10 files changed

+51
-25
lines changed

10 files changed

+51
-25
lines changed

Applications/FileManager/main.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <LibCore/ConfigFile.h>
3434
#include <LibCore/DesktopServices.h>
3535
#include <LibCore/MimeData.h>
36-
#include <LibCore/UserInfo.h>
36+
#include <LibCore/StandardPaths.h>
3737
#include <LibGUI/AboutDialog.h>
3838
#include <LibGUI/Action.h>
3939
#include <LibGUI/ActionGroup.h>
@@ -91,7 +91,7 @@ int main(int argc, char** argv)
9191
}
9292

9393
if (app.args().contains_slow("--desktop") || app.args().contains_slow("-d"))
94-
return run_in_desktop_mode(move(config), String::format("%s/Desktop", get_current_user_home_path().characters()));
94+
return run_in_desktop_mode(move(config), Core::StandardPaths::desktop_directory());
9595

9696
// our initial location is defined as, in order of precedence:
9797
// 1. the first command-line argument (e.g. FileManager /bin)
@@ -103,7 +103,7 @@ int main(int argc, char** argv)
103103
initial_location = argv[1];
104104

105105
if (initial_location.is_empty())
106-
initial_location = get_current_user_home_path();
106+
initial_location = Core::StandardPaths::home_directory();
107107

108108
if (initial_location.is_empty())
109109
initial_location = "/";
@@ -530,7 +530,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
530530

531531
auto go_home_action = GUI::CommonActions::make_go_home_action(
532532
[&](auto&) {
533-
directory_view.open(get_current_user_home_path());
533+
directory_view.open(Core::StandardPaths::home_directory());
534534
},
535535
window);
536536

Applications/SystemMenu/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <AK/QuickSort.h>
2929
#include <LibCore/ConfigFile.h>
3030
#include <LibCore/DirIterator.h>
31-
#include <LibCore/UserInfo.h>
31+
#include <LibCore/StandardPaths.h>
3232
#include <LibGUI/Action.h>
3333
#include <LibGUI/Application.h>
3434
#include <LibGUI/Desktop.h>
@@ -81,7 +81,7 @@ int main(int argc, char** argv)
8181
return 1;
8282
}
8383

84-
if (chdir(get_current_user_home_path().characters()) < 0) {
84+
if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
8585
perror("chdir");
8686
return 1;
8787
}

Applications/Taskbar/TaskbarWindow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "TaskbarButton.h"
2929
#include <AK/SharedBuffer.h>
3030
#include <LibCore/ConfigFile.h>
31-
#include <LibCore/UserInfo.h>
31+
#include <LibCore/StandardPaths.h>
3232
#include <LibGUI/BoxLayout.h>
3333
#include <LibGUI/Button.h>
3434
#include <LibGUI/Desktop.h>
@@ -105,7 +105,7 @@ void TaskbarWindow::create_quick_launch_bar()
105105
if (pid < 0) {
106106
perror("fork");
107107
} else if (pid == 0) {
108-
if (chdir(get_current_user_home_path().characters()) < 0) {
108+
if (chdir(Core::StandardPaths::home_directory().characters()) < 0) {
109109
perror("chdir");
110110
exit(1);
111111
}

Applications/Terminal/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#include <Kernel/KeyCode.h>
2828
#include <LibCore/ArgsParser.h>
29-
#include <LibCore/UserInfo.h>
3029
#include <LibGUI/AboutDialog.h>
3130
#include <LibGUI/Action.h>
3231
#include <LibGUI/ActionGroup.h>

Libraries/LibCore/ConfigFile.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <AK/StringBuilder.h>
2828
#include <LibCore/ConfigFile.h>
2929
#include <LibCore/File.h>
30-
#include <LibCore/UserInfo.h>
30+
#include <LibCore/StandardPaths.h>
3131
#include <pwd.h>
3232
#include <stdio.h>
3333
#include <unistd.h>
@@ -36,9 +36,7 @@ namespace Core {
3636

3737
NonnullRefPtr<ConfigFile> ConfigFile::get_for_app(const String& app_name)
3838
{
39-
String home_path = get_current_user_home_path();
40-
if (home_path == "/")
41-
home_path = String::format("/tmp");
39+
String home_path = StandardPaths::home_directory();
4240
auto path = String::format("%s/%s.ini", home_path.characters(), app_name.characters());
4341
return adopt(*new ConfigFile(path));
4442
}

Libraries/LibCore/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ OBJS = \
2323
ProcessStatisticsReader.o \
2424
Socket.o \
2525
SocketAddress.o \
26+
StandardPaths.o \
2627
TCPServer.o \
2728
TCPSocket.o \
2829
Timer.o \
2930
UDPServer.o \
3031
UDPSocket.o \
31-
UserInfo.o \
3232
puff.o
3333

3434
LIBRARY = libcore.a

Libraries/LibCore/UserInfo.cpp renamed to Libraries/LibCore/StandardPaths.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
2+
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -24,18 +24,38 @@
2424
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525
*/
2626

27-
#include <LibCore/UserInfo.h>
27+
#include <AK/FileSystemPath.h>
28+
#include <AK/String.h>
29+
#include <AK/StringBuilder.h>
30+
#include <LibCore/StandardPaths.h>
2831
#include <pwd.h>
2932
#include <stdlib.h>
3033
#include <unistd.h>
3134

32-
String get_current_user_home_path()
35+
namespace Core {
36+
37+
String StandardPaths::home_directory()
3338
{
3439
if (auto* home_env = getenv("HOME"))
35-
return home_env;
40+
return canonicalized_path(home_env);
3641

3742
auto* pwd = getpwuid(getuid());
3843
String path = pwd ? pwd->pw_dir : "/";
3944
endpwent();
40-
return path;
45+
return canonicalized_path(path);
46+
}
47+
48+
String StandardPaths::desktop_directory()
49+
{
50+
StringBuilder builder;
51+
builder.append(home_directory());
52+
builder.append("/Desktop");
53+
return canonicalized_path(builder.to_string());
54+
}
55+
56+
String StandardPaths::tempfile_directory()
57+
{
58+
return "/tmp";
59+
}
60+
4161
}

Libraries/LibCore/UserInfo.h renamed to Libraries/LibCore/StandardPaths.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
2+
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -26,6 +26,15 @@
2626

2727
#pragma once
2828

29-
#include <AK/String.h>
29+
#include <AK/Forward.h>
3030

31-
String get_current_user_home_path();
31+
namespace Core {
32+
33+
class StandardPaths {
34+
public:
35+
static String home_directory();
36+
static String desktop_directory();
37+
static String tempfile_directory();
38+
};
39+
40+
}

Libraries/LibGUI/FilePicker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ FilePicker::FilePicker(Mode mode, const StringView& file_name, const StringView&
128128
toolbar.add_action(*open_parent_directory_action);
129129

130130
auto go_home_action = CommonActions::make_go_home_action([this](auto&) {
131-
m_model->set_root_path(get_current_user_home_path());
131+
m_model->set_root_path(Core::StandardPaths::home_directory());
132132
});
133133
toolbar.add_action(go_home_action);
134134
toolbar.add_separator();

Libraries/LibGUI/FilePicker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include <AK/FileSystemPath.h>
2828
#include <AK/Optional.h>
29-
#include <LibCore/UserInfo.h>
29+
#include <LibCore/StandardPaths.h>
3030
#include <LibGUI/Dialog.h>
3131

3232
namespace GUI {
@@ -52,7 +52,7 @@ class FilePicker final : public Dialog {
5252
void clear_preview();
5353
void on_file_return();
5454

55-
FilePicker(Mode type = Mode::Open, const StringView& file_name = "Untitled", const StringView& path = String(get_current_user_home_path()), Window* parent_window = nullptr);
55+
FilePicker(Mode type = Mode::Open, const StringView& file_name = "Untitled", const StringView& path = Core::StandardPaths::home_directory(), Window* parent_window = nullptr);
5656

5757
static String ok_button_name(Mode mode)
5858
{

0 commit comments

Comments
 (0)