diff --git a/src/main.cpp b/src/main.cpp index 79f6e41..163c802 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) if (parser.isSet(cssOption)) w.setBrowserStyle(QUrl::fromLocalFile(parser.value(cssOption))); if (parser.positionalArguments().count() > 0) - w.load(QUrl::fromLocalFile(parser.positionalArguments().last())); + w.load(parser.positionalArguments().last()); w.show(); return app.exec(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 4385ef8..b7ec38a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -25,6 +25,8 @@ #include #include +static const QString ipfsScheme = QStringLiteral("ipfs"); + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) @@ -64,25 +66,28 @@ void MainWindow::on_actionOpen_triggered() << "text/plain"); if (fileDialog.exec() != QDialog::Accepted) return; - load(fileDialog.selectedUrls().first()); + load(fileDialog.selectedFiles().first()); } -bool MainWindow::load(QUrl url) +bool MainWindow::load(QString url) { qDebug() << url; - if (url.isRelative()) { - QUrl res = url.resolved(m_mainWidget->document()->baseUrl()); // doesn't work for local files - qDebug() << url << res << res.fileName() << url.toString(); + QUrl urlForm = QUrl::fromUserInput(url); + if (urlForm.isRelative()) { + QUrl res = urlForm.resolved(m_mainWidget->document()->baseUrl()); // doesn't work for local files + qDebug() << url << res << res.fileName() << urlForm.toString(); // correct for QUrl::resolved() being broken - if (res.fileName() != url.toString()) - res = QUrl(res.toString() + QLatin1Char('/') + url.toString()); - qDebug() << url << res << res.fileName() << url.toString(); - url = res; + if (res.fileName() != urlForm.toString()) + res = QUrl(res.toString() + QLatin1Char('/') + urlForm.toString()); + qDebug() << url << res << res.fileName() << urlForm.toString(); + urlForm = res; + ui->urlField->setText(urlForm.toString()); + } else { + ui->urlField->setText(url); } - bool success = false; - if (url.isLocalFile()) { - QString f = url.toLocalFile(); + if (urlForm.isLocalFile()) { + QString f = urlForm.toLocalFile(); QFile file(f); if (QFile::exists(f) && file.open(QFile::ReadOnly)) { QByteArray data = file.readAll(); @@ -108,19 +113,26 @@ bool MainWindow::load(QUrl url) else success = false; } + } else if (urlForm.scheme() == ipfsScheme || url.startsWith(QLatin1String("Qm"))) { + qDebug() << "ipfs get" << url; } else { - statusBar()->showMessage(tr("remote loading is not yet implemented: \"%1\"").arg(url.toString())); + statusBar()->showMessage(tr("remote loading is not yet implemented: \"%1\"").arg(url)); return false; } if (success) { m_history.push(url); - statusBar()->showMessage(tr("Opened \"%1\"").arg(url.toString())); + statusBar()->showMessage(tr("Opened \"%1\"").arg(url)); } else { - statusBar()->showMessage(tr("Could not open \"%1\"").arg(url.toString())); + statusBar()->showMessage(tr("Could not open \"%1\"").arg(url)); } return success; } +bool MainWindow::loadUrl(QUrl url) +{ + return load(url.toString()); +} + bool MainWindow::setBrowserStyle(QUrl url) { // TODO same url resolution as in load() @@ -152,3 +164,8 @@ void MainWindow::on_browser_backwardAvailable(bool a) Q_UNUSED(a) // ui->actionGo_back->setEnabled(a); } + +void MainWindow::on_urlField_returnPressed() +{ + load(ui->urlField->text()); +} diff --git a/src/mainwindow.h b/src/mainwindow.h index 19ac272..4468ec2 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -37,7 +37,8 @@ class MainWindow : public QMainWindow ~MainWindow(); public slots: - bool load(QUrl url); + bool load(QString url); + bool loadUrl(QUrl url); bool setBrowserStyle(QUrl url); private slots: @@ -46,10 +47,12 @@ private slots: void on_actionGo_back_triggered(); void on_browser_backwardAvailable(bool a); + void on_urlField_returnPressed(); + private: Ui::MainWindow *ui; QTextEdit *m_mainWidget; - QStack m_history; // correct for QTextBrowser history being broken (only for markdown?) + QStack m_history; // correct for QTextBrowser history being broken (only for markdown?) }; #endif // MAINWINDOW_H diff --git a/src/mainwindow.ui b/src/mainwindow.ui index ba2f531..46c05c3 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -121,7 +121,7 @@ browser anchorClicked(QUrl) MainWindow - load(QUrl) + loadUrl(QUrl) 136 @@ -135,6 +135,6 @@ - load(QUrl) + loadUrl(QUrl)