aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-04-13 20:06:58 +0530
committerAkshay Nair <phenax5@gmail.com>2025-04-13 21:06:23 +0530
commit0d9aedc087f56a3da35b6abc8a0ee233c2d2483b (patch)
tree533bb26e33358f601ac37eb12a897ba13d046667
parentbbe50215cbb9078ba457f93a5e90096e844c611a (diff)
downloadnull-browser-0d9aedc087f56a3da35b6abc8a0ee233c2d2483b.tar.gz
null-browser-0d9aedc087f56a3da35b6abc8a0ee233c2d2483b.zip
Add downloads_dir configuration + auto-accept download requests
Diffstat (limited to '')
-rw-r--r--config.lua1
-rw-r--r--src/Configuration.hpp8
-rw-r--r--src/WindowActionRouter.cpp15
-rw-r--r--src/WindowMediator.cpp14
-rw-r--r--src/WindowMediator.hpp5
-rw-r--r--src/widgets/BrowserApp.cpp8
-rw-r--r--src/widgets/BrowserApp.hpp3
-rw-r--r--src/widgets/BrowserWindow.cpp7
-rw-r--r--src/widgets/WebViewStack.cpp7
-rw-r--r--src/widgets/WebViewStack.hpp1
10 files changed, 58 insertions, 11 deletions
diff --git a/config.lua b/config.lua
index e434ba9..eafc413 100644
--- a/config.lua
+++ b/config.lua
@@ -17,6 +17,7 @@ end
web.set('new_view_url', 'https://lite.duckduckgo.com')
web.set('close_window_when_no_views', true)
web.set('user_agent', 'MacOS | Safari - $500 edition')
+web.set('downloads_dir', os.getenv('HOME') .. '/Downloads/firefox')
local dmenu = require 'null-browser.extras.dmenu'
local history = require 'null-browser.extras.history'
diff --git a/src/Configuration.hpp b/src/Configuration.hpp
index 3310262..3b491b2 100644
--- a/src/Configuration.hpp
+++ b/src/Configuration.hpp
@@ -12,6 +12,7 @@ private:
{"new_view_url", "https://duckduckgo.com"},
{"close_window_when_no_views", true},
{"user_agent", QWebEngineProfile::defaultProfile()->httpUserAgent()},
+ {"downloads_dir", QWebEngineProfile::defaultProfile()->downloadPath()},
};
public:
@@ -23,7 +24,8 @@ public:
}
QVariant get_config(const QString &name, QVariant default_value = "") const {
- if (!config_map.contains(name)) return default_value;
+ if (!config_map.contains(name))
+ return default_value;
return config_map.at(name);
}
@@ -33,13 +35,17 @@ public:
}
QString new_view_url() const { return get_config("new_view_url").toString(); }
QString user_agent() const { return get_config("user_agent").toString(); }
+ QString downloads_dir() const { return get_config("downloads_dir").toString(); }
private:
void on_update(const QString &name, const QVariant &value) {
if (name == "user_agent")
emit user_agent_updated(value.toString());
+ if (name == "downloads_dir")
+ emit downloads_dir_updated(value.toString());
}
signals:
void user_agent_updated(const QString &value);
+ void downloads_dir_updated(const QString &value);
};
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp
index 8aa4cd9..d0b529b 100644
--- a/src/WindowActionRouter.cpp
+++ b/src/WindowActionRouter.cpp
@@ -21,11 +21,16 @@ void WindowActionRouter::initialize(Configuration *config) {
connect(&runtime, &LuaRuntime::keymap_added, this, &WindowActionRouter::add_keymap);
connect(&runtime, &LuaRuntime::config_updated, configuration, &Configuration::set_config);
- connect(configuration, &Configuration::user_agent_updated, this, [this](const QString &user_agent) {
- for (auto &win_match : window_map) {
- win_match.second->mediator()->set_user_agent(user_agent);
- }
- });
+ connect(configuration, &Configuration::user_agent_updated, this,
+ [this](const QString &user_agent) {
+ for (auto &win_match : window_map)
+ win_match.second->mediator()->update_user_agent(user_agent);
+ });
+ connect(configuration, &Configuration::downloads_dir_updated, this,
+ [this](const QString &downloads_dir) {
+ for (auto &win_match : window_map)
+ win_match.second->mediator()->update_downloads_dir(downloads_dir);
+ });
connect(&runtime, &LuaRuntime::history_back_requested, this,
[this](WebViewId webview_id, qsizetype history_index) {
diff --git a/src/WindowMediator.cpp b/src/WindowMediator.cpp
index a2ff2a6..f27e009 100644
--- a/src/WindowMediator.cpp
+++ b/src/WindowMediator.cpp
@@ -16,9 +16,6 @@ WindowMediator::WindowMediator(WebViewStack *webview_stack) : webview_stack(webv
connect(this, &WindowMediator::webview_closed, webview_stack, &WebViewStack::close);
connect(this, &WindowMediator::webview_selected, webview_stack, &WebViewStack::focus_webview);
- auto *profile = webview_stack->get_profile();
- connect(this, &WindowMediator::set_user_agent, profile, &QWebEngineProfile::setHttpUserAgent);
-
// Delegate signal
connect(webview_stack, &WebViewStack::new_window_requested, this,
&WindowMediator::new_window_requested);
@@ -26,6 +23,17 @@ WindowMediator::WindowMediator(WebViewStack *webview_stack) : webview_stack(webv
&WindowMediator::close_window_requested);
}
+void WindowMediator::update_user_agent(const QString &user_agent) {
+ auto *profile = webview_stack->get_profile();
+ profile->setHttpUserAgent(user_agent);
+}
+
+void WindowMediator::update_downloads_dir(const QString &downloads_dir) {
+ qDebug() << "::: dl in med" << downloads_dir;
+ auto *profile = webview_stack->get_profile();
+ profile->setDownloadPath(downloads_dir);
+}
+
WindowMediator::~WindowMediator() {
disconnect(this);
delete webview_stack;
diff --git a/src/WindowMediator.hpp b/src/WindowMediator.hpp
index f1e99ca..b2ecb77 100644
--- a/src/WindowMediator.hpp
+++ b/src/WindowMediator.hpp
@@ -25,7 +25,10 @@ signals:
void webview_selected(WebViewId webview_id);
void new_window_requested(const QUrl &url);
void close_window_requested();
- void set_user_agent(const QString &user_agent);
+
+public slots:
+ void update_user_agent(const QString &user_agent);
+ void update_downloads_dir(const QString &downloads_dir);
private:
WebViewStack *webview_stack;
diff --git a/src/widgets/BrowserApp.cpp b/src/widgets/BrowserApp.cpp
index 4a43dfb..9b65b3a 100644
--- a/src/widgets/BrowserApp.cpp
+++ b/src/widgets/BrowserApp.cpp
@@ -20,6 +20,13 @@ BrowserApp::BrowserApp() {
// NOTE: TMP
lua.load_file_sync("./config.lua");
+ // Initializes profile
+ QList profiles{&default_profile};
+ for (auto *profile : profiles) {
+ profile->setDownloadPath(configuration.downloads_dir());
+ profile->setHttpUserAgent(configuration.user_agent());
+ }
+
connect(&window_action_router, &WindowActionRouter::new_window_requested, this,
[this](const QUrl &url) { create_window({url.toString()}); });
};
@@ -28,6 +35,7 @@ BrowserWindow *BrowserApp::create_window(const QStringList &urls) {
auto *window = new BrowserWindow((const Configuration &)configuration, urls);
window->setWindowTitle("null-browser");
WindowActionRouter::instance().add_window(window);
+
window->show();
return window;
}
diff --git a/src/widgets/BrowserApp.hpp b/src/widgets/BrowserApp.hpp
index 1b8fdcf..90d1ae7 100644
--- a/src/widgets/BrowserApp.hpp
+++ b/src/widgets/BrowserApp.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "widgets/BrowserWindow.hpp"
+#include <qlist.h>
class BrowserApp : public QObject {
Q_OBJECT
@@ -15,4 +16,6 @@ protected:
private:
Configuration configuration;
+ QWebEngineProfile default_profile{"default"};
+ QList<QWebEngineProfile *> profiles{&default_profile};
};
diff --git a/src/widgets/BrowserWindow.cpp b/src/widgets/BrowserWindow.cpp
index dbefdd6..5a8a062 100644
--- a/src/widgets/BrowserWindow.cpp
+++ b/src/widgets/BrowserWindow.cpp
@@ -21,8 +21,13 @@ BrowserWindow::BrowserWindow(const Configuration &configuration, const QStringLi
layout->setStackingMode(QStackedLayout::StackAll);
centralWidget()->setLayout(layout);
+ // Webengine profile
+ auto *profile = new QWebEngineProfile("null-browser");
+ profile->setDownloadPath(configuration.downloads_dir());
+ profile->setHttpUserAgent(configuration.user_agent());
+
// Web engine
- auto *webview_stack = new WebViewStack(&configuration, new QWebEngineProfile("null-browser"));
+ auto *webview_stack = new WebViewStack(&configuration, profile);
layout->addWidget(webview_stack);
// Open webviews for given urls
diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp
index bdec508..258fbc6 100644
--- a/src/widgets/WebViewStack.cpp
+++ b/src/widgets/WebViewStack.cpp
@@ -21,6 +21,7 @@ WebViewStack::WebViewStack(const Configuration *configuration, QWebEngineProfile
connect(layout, &QStackedLayout::currentChanged, this,
&WebViewStack::current_webview_title_changed);
+ connect(profile, &QWebEngineProfile::downloadRequested, this, &WebViewStack::on_download_request);
}
void WebViewStack::open_url(const QUrl &url, OpenType open_type, WebViewId webview_id) {
@@ -93,6 +94,12 @@ void WebViewStack::on_new_webview_request(const QWebEngineNewWindowRequest &requ
}
}
+void WebViewStack::on_download_request(QWebEngineDownloadRequest *download) {
+ qDebug() << "DOWNLOADING" << download->downloadDirectory() << download->downloadFileName();
+ emit download->accept();
+ // TODO: Emit event to see download progress
+}
+
int32_t WebViewStack::get_webview_index(WebViewId webview_id) {
if (webview_id == 0) {
// TODO: Check how to detect "active" window before qapp start (lua init)?
diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp
index 8f66c09..05362d6 100644
--- a/src/widgets/WebViewStack.hpp
+++ b/src/widgets/WebViewStack.hpp
@@ -57,6 +57,7 @@ protected:
WebView *create_new_webview(const QUrl &url, bool focus = false);
int32_t get_webview_index(WebViewId webview_id);
WebView *get_webview(WebViewId webview_id);
+ void on_download_request(QWebEngineDownloadRequest *download);
public slots:
void open_url(const QUrl &url, OpenType open_type = OpenType::OpenUrl, WebViewId webview_id = 0);