Skip to content

Commit c73112e

Browse files
committed
Settings (ladybird:a39b1f334aa85b6faefd8fafbf987d613262df98, ladybird:4a445cf084907e0d8535aff2ae258de3cc9ad0f0, ladybird:c9722377f1650d38f81c445d3a5434cdec1e09b6)
1 parent c5f1386 commit c73112e

9 files changed

+149
-2
lines changed

BrowserWindow.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
*/
77

88
#include "BrowserWindow.h"
9+
#include "Settings.h"
10+
#include "SettingsDialog.h"
911
#include "WebView.h"
1012
#include <LibCore/EventLoop.h>
1113
#include <QAction>
14+
#include <QDialog>
1215
#include <QPlainTextEdit>
1316
#include <QMessageBox>
1417

1518
extern String s_serenity_resource_root;
19+
extern Browser::Settings* s_settings;
1620

1721
BrowserWindow::BrowserWindow(Core::EventLoop& event_loop)
1822
: m_event_loop(event_loop)
@@ -100,7 +104,6 @@ BrowserWindow::BrowserWindow(Core::EventLoop& event_loop)
100104
auto* preferences_action = new QAction("Prefe&rences");
101105
preferences_action->setIcon(QIcon(QString("%1/res/icons/16x16/settings.png").arg(s_serenity_resource_root.characters())));
102106
preferences_action->setShortcut(QKeySequence(QKeySequence::Preferences)); // "Ctrl+, (macOS)"
103-
preferences_action->setEnabled(false);
104107
edit_menu->addAction(preferences_action);
105108

106109
auto* view_menu = menuBar()->addMenu("&View");
@@ -263,6 +266,9 @@ BrowserWindow::BrowserWindow(Core::EventLoop& event_loop)
263266
about_action->setIcon(QIcon(QString("../icons/16x16/app-coccinellidae.png")));
264267
help_menu->addAction(about_action);
265268

269+
QObject::connect(preferences_action, &QAction::triggered, this, [this] {
270+
new SettingsDialog(this);
271+
});
266272
QObject::connect(about_action, &QAction::triggered, this, &BrowserWindow::about);
267273

268274
QObject::connect(new_tab_action, &QAction::triggered, this, &BrowserWindow::new_tab);

CMakeLists.qt6.txt

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ set(SOURCES
4343
main.cpp
4444
WebView.cpp
4545
History.cpp
46+
Settings.cpp
47+
SettingsDialog.cpp
4648
Tab.cpp
4749
)
4850

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ set(SOURCES
4545
main.cpp
4646
WebView.cpp
4747
History.cpp
48+
Settings.cpp
49+
SettingsDialog.cpp
4850
Tab.cpp
4951
)
5052

Settings.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2022, Filiph Sandström <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include "Settings.h"
8+
9+
namespace Browser {
10+
11+
Settings::Settings()
12+
{
13+
m_qsettings = new QSettings("Serenity", "Ladybird");
14+
}
15+
16+
QString Settings::homepage()
17+
{
18+
return m_qsettings->value("homepage", "https://home.coccinellidae.serenityos.net").toString();
19+
}
20+
21+
void Settings::set_homepage(QString const& homepage)
22+
{
23+
m_qsettings->setValue("homepage", homepage);
24+
}
25+
26+
}

Settings.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2022, Filiph Sandström <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#define AK_DONT_REPLACE_STD
10+
11+
#include <AK/String.h>
12+
#include <QSettings>
13+
14+
namespace Browser {
15+
16+
class Settings {
17+
public:
18+
Settings();
19+
20+
QString homepage();
21+
void set_homepage(QString const& homepage);
22+
23+
private:
24+
QSettings* m_qsettings;
25+
};
26+
27+
}

SettingsDialog.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022, Filiph Sandström <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include "Settings.h"
8+
#include "SettingsDialog.h"
9+
#include <QCloseEvent>
10+
#include <QLabel>
11+
12+
extern Browser::Settings* s_settings;
13+
14+
SettingsDialog::SettingsDialog(QMainWindow* window)
15+
: m_window(window)
16+
{
17+
m_layout = new QFormLayout;
18+
m_homepage = new QLineEdit;
19+
m_ok_button = new QPushButton("&Save");
20+
21+
m_layout->addWidget(new QLabel("Homepage"));
22+
m_layout->addWidget(m_homepage);
23+
m_layout->addWidget(m_ok_button);
24+
25+
m_homepage->setText(s_settings->homepage());
26+
27+
QObject::connect(m_ok_button, &QPushButton::released, this, [this] {
28+
close();
29+
});
30+
31+
setWindowTitle("Settings");
32+
setFixedWidth(300);
33+
setLayout(m_layout);
34+
show();
35+
setFocus();
36+
}
37+
38+
void SettingsDialog::closeEvent(QCloseEvent *event)
39+
{
40+
save();
41+
event->accept();
42+
}
43+
44+
void SettingsDialog::save()
45+
{
46+
// FIXME: Validate data.
47+
s_settings->set_homepage(m_homepage->text());
48+
}

SettingsDialog.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2022, Filiph Sandström <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <QFormLayout>
8+
#include <QDialog>
9+
#include <QMainWindow>
10+
#include <QLineEdit>
11+
#include <QPushButton>
12+
13+
#pragma once
14+
15+
class SettingsDialog : public QDialog {
16+
Q_OBJECT
17+
public:
18+
explicit SettingsDialog(QMainWindow* window);
19+
20+
void save();
21+
22+
virtual void closeEvent(QCloseEvent*) override;
23+
24+
private:
25+
QFormLayout* m_layout;
26+
QPushButton* m_ok_button { nullptr };
27+
QLineEdit* m_homepage { nullptr };
28+
QMainWindow* m_window { nullptr };
29+
};

Tab.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77

88
#include "Tab.h"
99
#include "BrowserWindow.h"
10+
#include "Settings.h"
1011
#include "History.h"
1112
#include <QCoreApplication>
1213
#include <QStatusBar>
1314
#include <QLabel>
1415

1516
extern String s_serenity_resource_root;
17+
extern Browser::Settings* s_settings;
1618

1719
Tab::Tab(QMainWindow* window)
1820
: m_window(window)
@@ -151,7 +153,8 @@ void Tab::forward()
151153

152154
void Tab::home()
153155
{
154-
navigate("https://home.coccinellidae.serenityos.net");
156+
//navigate("https://home.coccinellidae.serenityos.net");
157+
navigate(s_settings->homepage());
155158
}
156159

157160
void Tab::reload()

main.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "BrowserWindow.h"
8+
#include "Settings.h"
89
#include "WebView.h"
910
#include <LibCore/ArgsParser.h>
1011
#include <LibCore/EventLoop.h>
@@ -14,6 +15,7 @@
1415
#include <QWidget>
1516

1617
extern void initialize_web_engine();
18+
Browser::Settings* s_settings;
1719

1820
ErrorOr<int> serenity_main(Main::Arguments arguments)
1921
{
@@ -25,6 +27,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
2527
args_parser.add_positional_argument(url, "URL to open", "url", Core::ArgsParser::Required::No);
2628
args_parser.parse(arguments);
2729

30+
s_settings = new Browser::Settings();
31+
2832
Core::EventLoop event_loop;
2933

3034
QApplication app(arguments.argc, arguments.argv);

0 commit comments

Comments
 (0)