aboutsummaryrefslogtreecommitdiff
path: root/sb.cpp
blob: 163876a787e42dfc52865cc45eaf7034418cc543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <regex>

#include <QtWidgets/QApplication>
#include <QAction>
#include <QtGui/QWindow>
#include <QtWidgets/QMainWindow>
#include <QtWebEngineWidgets/QWebEngineView>
#include <QWebEngineView>
#include <QtWebEngineWidgets/QWebEngineProfile>
#include <QtWidgets/QWidget>
#include <QtWidgets/QLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMenu>
#include <QtGui/QKeyEvent>
#include <QProgressBar>
#include <QtX11Extras/QX11Info>
#include <X11/Xlib.h>
#include <QShortcut>
#include <QVBoxLayout>


bool is_valid_url(const std::string & url);
void open_url(const std::string & url);
void search();
void page_scroll(int scroll);
void goto_end(bool end);
void get_window_id();
void history_move(bool back);
void reload();
uint32_t parent_id = 0;

#include "webengine.hpp"

#include "config.hpp"

bool is_valid_url(const std::string & url) {
	std::regex r(".*://.*\\..*");
	std::smatch m;

	return std::regex_match(url, m, r);
}

QWebEngineView * web;
QMainWindow * view;
Display * dpy;


std::string read_from_dmenu() {
	std::string output;

	std::string cmd = std::string("printf '' | dmenu -p 'open: ' ") + dmenu_args + " -w ";
	cmd += std::to_string(view->winId());

	FILE * f = popen(cmd.c_str(), "r");

	char string[8092] = {0};
	fgets(string, 8092, f);
	fclose(f);

	if (string[0] != 0) {
		output = string;
		output.pop_back();
	}

	return output;
}

inline void __web_load(const std::string &url) {
	web->load(QString::fromStdString(url));
}

void open_url(const std::string & url) {
	if (url.empty())
		return;

	if (is_valid_url(url)) {
		__web_load(url);
		return;
	}

	if (is_valid_url("https://" + url)) {
		__web_load("https://" + url);
		return;
	}

	__web_load(search_engine + url);
}

void search() {
	auto url = read_from_dmenu();
	open_url(url);
}

void page_scroll(int scroll) {
	std::string js = "window.scrollBy({ top: " + std::to_string(scroll) + ", behavior: 'smooth' });";
	web->page()->runJavaScript(QString::fromStdString(js));
}

void goto_end(bool end) {
	std::string js;
	if (end) js = "window.scroll({ top: document.body.scrollHeight, behavior: 'smooth' })";
	else     js = "window.scroll({ top: 0, behavior: 'smooth' })";
	web->page()->runJavaScript(QString::fromStdString(js));
}

void history_move(bool back) {
	if (back)
		web->back();
	else
		web->forward();
}

void reload() {
	web->reload();
}

void help(char *arg0) {
	std::cout << arg0;
	std::cout << " [url] [-e <xid>]\n";
	exit(-1);
}

int
main(int argc, char **argv) {
	QApplication app(argc, argv);
	QWebEngineProfile profile;
	WebEnginePage page(&profile);
	QProgressBar progressbar;
	QLabel url_label;
	QWidget *main = new QWidget;
	QVBoxLayout layout(main);
	std::string url = getenv("WWW_HOME");
	bool url_set_by_arg = false;


	view = new QMainWindow();
	web = new QWebEngineView();
	web->setPage(&page);
	page.setBackgroundColor(QColor(40, 40, 40));
	progressbar.setMaximum(100);
	progressbar.setMinimum(0);
	progressbar.setTextVisible(false);
	progressbar.setMaximumHeight(10);
	progressbar.setStyleSheet("QProgressBar::chunk { background: #b16286; border-radius: 0px }");
	url_label.setMaximumHeight(30);
	url_label.setMargin(5);
	url_label.setStyleSheet("QLabel { background: #1d2021; color: #ebdbb2 }");


	for (int i = 1; i < argc; ++i) {
		if (argv[i] == std::string("-e")) {
			if (++i == argc || (argv[i][0] < '0' || argv[i][0] > '9')) 
				help(argv[0]);

			dpy = XOpenDisplay(NULL);
			parent_id = std::stoi(argv[i]);
			XReparentWindow(dpy, view->winId(), parent_id, 0, 0);
			XFlush(dpy);

		} else if (!url_set_by_arg) {
			url = argv[i];
			url_set_by_arg = true;
		} else {
			help(argv[0]);
		}
	}

	open_url(url);

	layout.setContentsMargins(0, 0, 0, 0);
	layout.setSpacing(0);
	layout.addWidget(web);
	layout.addWidget(&progressbar);
	layout.addWidget(&url_label);
	view->setCentralWidget(main);
	view->show();

	web->connect(web, &QWebEngineView::titleChanged, [&](const QString & title) {
			view->setWindowTitle(title);
	});

	if (parent_id != 0) {
		web->setContextMenuPolicy(Qt::CustomContextMenu);
		web->connect(web,
				&QWebEngineView::customContextMenuRequested, [&](){
				auto menu = web->page()->createStandardContextMenu();
				menu->popup(QCursor::pos());
				return menu;
				});
	}

	web->connect(web, &QWebEngineView::loadProgress, [&](int progress) {
		progressbar.show();
		progressbar.setValue(progress);
	});
	

	web->connect(web, &QWebEngineView::loadFinished, [&](bool ok) {
		progressbar.hide();
	});

	web->connect(web, &QWebEngineView::urlChanged, [&](const QUrl & url) {
		url_label.setText(url.toString());
	});

	register_shortcuts(view);

	return app.exec();
}