Skip to content

Commit

Permalink
Update URL resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
chardoncs committed Jun 10, 2023
1 parent d597bd3 commit 92794c9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
6 changes: 3 additions & 3 deletions arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Arguments::Arguments(int argc, char *argv[]) : QObject()
continue;
}

_path = QString(argv[i]);
url_str = QString(argv[i]);
break;
}
}

const QString &Arguments::path() const {
return _path;
const QString &Arguments::url() const {
return url_str;
}
6 changes: 4 additions & 2 deletions arguments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
#define ARGUMENTS_HPP

#include <QObject>
#include <QRegularExpression>
#include <QUrl>

class Arguments : public QObject
{
Q_OBJECT
public:
explicit Arguments(int argc, char *argv[]);

const QString &path() const;
const QString &url() const;

private:
QString _path;
QString url_str;

signals:

Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(int argc, char *argv[])

Arguments args(argc, argv);

StartupWidget sw(args.path());
StartupWidget sw(args.url());
sw.startup();

return a.exec();
Expand Down
31 changes: 23 additions & 8 deletions startupwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
#include <QFont>
#include <QUrl>

StartupWidget::StartupWidget(const QString &path) : QWidget()
StartupWidget::StartupWidget(const QString &url) : QWidget()
{
this->path_p = path.trimmed();
this->_url = resoluteUrl(url);
this->view = nullptr;
this->directMode = url.length();

loadingWidget = new LoadingWidget();

view = new QWebEngineView();

// Startup behaviors
connect(this, &StartupWidget::askForUrl, &StartupWidget::interact);
connect(this, &StartupWidget::launchDirectly, &StartupWidget::launch);
connect(this, &StartupWidget::direct, &StartupWidget::launch);
connect(this, &StartupWidget::loading, loadingWidget, &LoadingWidget::show);

// Web engine behaviors
Expand All @@ -26,19 +27,19 @@ StartupWidget::StartupWidget(const QString &path) : QWidget()
}

void StartupWidget::startup() {
if (path_p.length()) {
emit launchDirectly(path_p);
if (directMode) {
emit direct(_url);
}
else {
emit askForUrl();
}
}

void StartupWidget::launch(const QString &path) {
void StartupWidget::launch(const QUrl &url) {
emit loading();

// Initialize web engine
view->setUrl(QUrl(path));
view->setUrl(url);
view->showMaximized();
}

Expand Down Expand Up @@ -70,5 +71,19 @@ void StartupWidget::updateStartupButtonStatus() {
}

void StartupWidget::launchWithInput() {
this->launch(entry->text().trimmed());
this->launch(resoluteUrl(entry->text()));
}

QUrl StartupWidget::resoluteUrl(const QString &url) {
QString trimmed = url.trimmed();

if (patternPrefix.match(trimmed).hasMatch()) {
return QUrl(trimmed);
}

if (patternDomain.match(trimmed.split("/")[0]).hasMatch()) {
return QUrl("https://" + trimmed);
}

return QUrl::fromLocalFile(trimmed);
}
16 changes: 12 additions & 4 deletions startupwidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QPointer>
#include <QPushButton>
#include <QLineEdit>
#include <QRegularExpression>
#include <QWebEngineView>
#include <QWidget>
#include <QVBoxLayout>
Expand All @@ -15,13 +16,15 @@ class StartupWidget : public QWidget
{
Q_OBJECT
public:
explicit StartupWidget(const QString &path);
explicit StartupWidget(const QString &);

public slots:
void startup();

private:
QString path_p;
QUrl _url;
bool directMode;

QPointer<QWebEngineView> view;

QPointer<LoadingWidget> loadingWidget;
Expand All @@ -33,16 +36,21 @@ public slots:

void showLoading();

QRegularExpression patternPrefix{"^(https?|ftp)://"};
QRegularExpression patternDomain{"^([\\d\\w]+\\.)+[\\d\\w]+$"};

QUrl resoluteUrl(const QString &);

private slots:
void launch(const QString &path);
void launch(const QUrl &);
void interact();

void updateStartupButtonStatus();

void launchWithInput();

signals:
void launchDirectly(const QString &);
void direct(const QUrl &);
void askForUrl();
void loading();
};
Expand Down

0 comments on commit 92794c9

Please sign in to comment.