Skip to content

Commit 41b7fdc

Browse files
author
Thomas
committed
Refracto context menu (right click)
`keybinds` and `providers` config are now under the same `qtwebflix.conf` file
1 parent c9811d2 commit 41b7fdc

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed

src/mainwindow.cpp

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <QContextMenuEvent>
22
#include <QDebug>
3-
#include <QMenu>
43
#include <QSettings>
54
#include <QStandardPaths>
65
#include <QWebEngineFullScreenRequest>
@@ -25,7 +24,7 @@ MainWindow::MainWindow(QWidget *parent)
2524
QWebEngineSettings::globalSettings()->setAttribute(
2625
QWebEngineSettings::PluginsEnabled, true);
2726
stateSettings = new QSettings("Qtwebflix", "Save State", this);
28-
keySettings = new QSettings("Qtwebflix", "keybinds", this);
27+
appSettings = new QSettings("Qtwebflix", "qtwebflix", this);
2928
QWebEngineProfile::defaultProfile()->setPersistentCookiesPolicy(
3029
QWebEngineProfile::ForcePersistentCookies);
3130

@@ -134,14 +133,18 @@ void MainWindow::registerMprisKeybinds() {
134133
actions["pause"] = std::make_pair(mpris.get(), SLOT(pauseVideo()));
135134
actions["play-pause"] = std::make_pair(mpris.get(), SLOT(togglePlayPause()));
136135
actions["next-episode"] = std::make_pair(mpris.get(), SLOT(goNextEpisode()));
136+
actions["seek-next"] = std::make_pair(mpris.get(), SLOT(setSeek(10)));
137+
actions["seek-prev"] = std::make_pair(mpris.get(), SLOT(setSeek(-10)));
137138

138-
for (auto action : keySettings->allKeys()) {
139-
auto keySequence = keySettings->value(action).toStringList().join(',');
139+
appSettings->beginGroup("keybinds");
140+
for (auto action : appSettings->allKeys()) {
141+
auto keySequence = appSettings->value(action).toStringList().join(',');
140142
for (auto key :
141143
keySequence.split(QRegExp("\\s+"), QString::SkipEmptyParts)) {
142144
registerShortcut(action, key);
143145
}
144146
}
147+
appSettings->endGroup();
145148
}
146149

147150
void MainWindow::exchangeMprisInterfaceIfNeeded() {
@@ -278,7 +281,35 @@ void MainWindow::restore() {
278281
restoreGeometry(geometryData);
279282
}
280283

281-
void MainWindow::readSettings() { restore(); }
284+
void MainWindow::createContextMenu(const QStringList &keys) {
285+
for (const auto &i : keys) {
286+
if (!i.startsWith("#")) {
287+
QString url = appSettings->value(i).toString();
288+
contextMenu.addAction(i, [this, url]() {
289+
qDebug() << "URL is :" << url;
290+
webview->setUrl(QUrl(url));
291+
});
292+
contextMenu.addSeparator();
293+
}
294+
}
295+
}
296+
297+
void MainWindow::readSettings() {
298+
appSettings->beginGroup("providers");
299+
QStringList providers = appSettings->allKeys();
300+
301+
// Check if config file exists,if not create a default key.
302+
if (!providers.size()) {
303+
qDebug() << "Config file does not exist, creating default";
304+
appSettings->setValue("netflix", "http://netflix.com");
305+
appSettings->sync();
306+
providers = appSettings->allKeys();
307+
}
308+
appSettings->endGroup();
309+
createContextMenu(providers);
310+
311+
restore();
312+
}
282313

283314
void MainWindow::fullScreenRequested(QWebEngineFullScreenRequest request) {
284315

@@ -295,48 +326,8 @@ void MainWindow::fullScreenRequested(QWebEngineFullScreenRequest request) {
295326

296327
void MainWindow::ShowContextMenu(const QPoint &pos) // this is a slot
297328
{
298-
299329
QPoint globalPos = webview->mapToGlobal(pos);
300-
provSettings = new QSettings("Qtwebflix", "Providers", this);
301-
provSettings->setIniCodec("UTF-8");
302-
provSettings->beginGroup("providers");
303-
QString conf(provSettings->fileName());
304-
305-
// Check if config file exists,if not create a default key.
306-
if (!QFile::exists(conf))
307-
308-
{
309-
qDebug() << "Config file does not exist, creating default";
310-
provSettings->setValue("netflix", "http://netflix.com");
311-
provSettings->sync();
312-
}
313-
314-
QStringList keys = provSettings->allKeys();
315-
316-
QMenu myMenu;
317-
for (const auto &i : keys) {
318-
// qDebug() << "keys" << i;
319-
320-
if (!i.startsWith("#")) {
321-
myMenu.addAction(i);
322-
myMenu.addSeparator();
323-
}
324-
}
325-
326-
QAction *selectedItem = myMenu.exec(globalPos);
327-
328-
if (selectedItem == nullptr) {
329-
return;
330-
} else if (selectedItem) {
331-
QString url = provSettings->value(selectedItem->text()).toString();
332-
qDebug() << "URL is :" << url;
333-
webview->setUrl(QUrl(url));
334-
provSettings->endGroup();
335-
}
336-
337-
else {
338-
// nothing was chosen
339-
}
330+
QAction *selectedItem = contextMenu.exec(globalPos);
340331
}
341332

342333
void MainWindow::parseCommand() {

src/mainwindow.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QCommandLineParser>
1111
#include <QMainWindow>
1212
#include <QMap>
13+
#include <QMenu>
1314
#include <QMessageBox>
1415
#include <QPair>
1516
#include <QSettings>
@@ -62,8 +63,9 @@ private slots:
6263
QString playRateStr;
6364

6465
QSettings *stateSettings;
65-
QSettings *keySettings;
66-
QSettings *provSettings;
66+
QSettings *appSettings;
67+
68+
QMenu contextMenu;
6769

6870
std::type_index mprisType;
6971
std::unique_ptr<MprisInterface> mpris;
@@ -75,6 +77,7 @@ private slots:
7577
void exchangeMprisInterfaceIfNeeded();
7678
void registerShortcut(QString, QString);
7779
void registerMprisKeybinds();
80+
void createContextMenu(const QStringList &keys);
7881

7982
QMap<QString, std::pair<const QObject *, const char *>> actions;
8083
QMap<QString, QShortcut *> shortcuts;

0 commit comments

Comments
 (0)