diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a0343f8..1c92e60 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,6 +22,7 @@ set(SOURCES mainwindow.cpp ogurlhandler.cpp desktopparser.cpp + ogwebpage.cpp ) file(GLOB TRANSLATIONS "${PROJECT_SOURCE_DIR}/i18n/*.ts") diff --git a/src/desktopparser.cpp b/src/desktopparser.cpp new file mode 100644 index 0000000..74d1efb --- /dev/null +++ b/src/desktopparser.cpp @@ -0,0 +1,67 @@ +#include "desktopparser.h" +#include +#include +#include + + + +DesktopParser::DesktopParser() { + _applicationPaths += qgetenv("HOME") + "./local/share/applications"; + +} + +void DesktopParser::loadSchemes() { + + qDebug() << "Loading URL schemes"; + _schemes.clear(); + + for(const auto& appdir : _applicationPaths) { + qDebug() << "Looking in" << appdir; + + QDir dir(appdir); + for(const QFileInfo &fi : dir.entryInfoList(QDir::Files)) { + //qDebug() << "File: " << fi; + + QFile file(fi.absoluteFilePath()); + if (file.open(QIODevice::ReadOnly)) { + QTextStream fs(&file); + + QString commandString; + QString mimeTypesString; + + while(!fs.atEnd()) { + QString line = fs.readLine(); + QStringList parts = line.split("="); + + if (!parts.length() > 1) continue; + + if ( parts[0] == "Exec") { + commandString=parts[1]; + } + + if (parts[0] == "MimeType" ) { + mimeTypesString=parts[1]; + } + } + + if (!commandString.isEmpty() && !mimeTypesString.isEmpty()) { + //qDebug() << "Found MIME handler: " << mimeTypesString << "; " << commandString; + + QStringList handlers = mimeTypesString.split(";"); + + for(const QString &handler : handlers) { + QStringList parts = handler.split("/"); + if (parts.length() > 1 && parts[0] == "x-scheme-handler") { + qInfo() << "Found URL scheme handler: " << parts[1] << " => " << commandString; + _schemes[parts[1]] = commandString; + } + } + + + } + } else { + qWarning() << "Failed to open" << fi.absoluteFilePath(); + } + } + } +} \ No newline at end of file diff --git a/src/desktopparser.h b/src/desktopparser.h new file mode 100644 index 0000000..8d0c279 --- /dev/null +++ b/src/desktopparser.h @@ -0,0 +1,38 @@ +#include +#include +#include + + +class DesktopParser { + public: + + static DesktopParser &getInstance() { + static DesktopParser instance; + return instance; + } + + void loadSchemes(); + + QString getScheme(const QString &scheme) { + QString s = scheme.toLower().trimmed(); + + if (_schemes.contains(s)) { + return _schemes[s]; + } + + return ""; + } + + + QMap& getSchemes() { return _schemes; } + + + + + private: + DesktopParser(); + + QStringList _applicationPaths{"/usr/share/applications"}; + QMap _schemes{}; + +}; diff --git a/src/ogcommandline.h b/src/ogcommandline.h new file mode 100644 index 0000000..4a6c992 --- /dev/null +++ b/src/ogcommandline.h @@ -0,0 +1,45 @@ + + +#pragma once + +#include + +class OGCommandLineOptions { + public: + + enum NewTabBehavior { + /** + * Ignore attempt to open new tab + */ + Ignore, + + /** + * Replace current page, ignore "new tab" directive. + */ + ReplacePage, + + /** + * Open a new tab. + */ + NewTab + }; + + static OGCommandLineOptions &getInstance() { + static OGCommandLineOptions instance; + return instance; + } + + QString getUrl() const { return _url; } + void setUrl(const QString& url) { _url = url; } + + NewTabBehavior getNewTabBehavior() const { return _newTabBehavior; } + void setNewTabBehavior(NewTabBehavior nt) { _newTabBehavior = nt; } + +private: + OGCommandLineOptions() { + + } + + QString _url; + NewTabBehavior _newTabBehavior{ReplacePage}; +}; \ No newline at end of file diff --git a/src/ogwebpage.cpp b/src/ogwebpage.cpp new file mode 100644 index 0000000..6a23870 --- /dev/null +++ b/src/ogwebpage.cpp @@ -0,0 +1,42 @@ +#include "ogwebpage.h" + + +#include +#include + +#include "ogcommandlne.h" +#include "mainwindow.h" +#include + + +bool OGWebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame) { + + qInfo() << "Link clicked: URL = " << url << "; type = " << type << "; isMainFrame = " << isMainFrame; + + if ( type == QWebEnginePage::NavigationType::NavigationTypeLinkClicked) { + + } + + return true; +} + +QWebEnginePage *OGWebPage::createWindow(QWebEnginePage::WebWindowType type) { + qInfo() << "Create new window: " << type; + + OGCommandLineOptions &options = OGCommandLineOptions::getInstance(); + + switch(options.getNewTabBehavior()) { + case OGCommandLineOptions::NewTabBehavior::NewTab: + qWarning() << "Unimplemented"; + return this; + case OGCommandLineOptions::NewTabBehavior::ReplacePage: + qInfo() << "Replacing page"; + return this; + case OGCommandLineOptions::NewTabBehavior::Ignore: + qInfo() << "Ignoring attempt to open new tab"; + return nullptr; + } + + qCritical() << "Unknown new tab behavior, this should be unreachable"; + return this; +} diff --git a/src/ogwebpage.h b/src/ogwebpage.h new file mode 100644 index 0000000..432e627 --- /dev/null +++ b/src/ogwebpage.h @@ -0,0 +1,23 @@ + +#include +#include + +#include "ogcommandlne.h" + +class MainWindow; + +class OGWebPage : public QWebEnginePage { + public: + + OGWebPage(QObject *parent) : QWebEnginePage(parent) { + qDebug() << "OGWebPage constructed!"; + } + + virtual bool acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame); + virtual QWebEnginePage *createWindow(QWebEnginePage::WebWindowType type); + + void setMainWindow(MainWindow *mw) { m_mainWindow = mw; } + +private: + MainWindow *m_mainWindow; +};