L'app sul telefono si apre e si chiude. Dal launcher non si vede niente: né un messaggio, né una schermata. Il motore non cede — ho riprodotto la sequenza esatta del guscio (sessione, CSS della demo, zoom 2,5, clic) sul renderer compilato per aarch64 e eseguito sotto qemu: disegna 1350×4278 senza errori. Quindi il difetto è dove non arrivo a vedere. Questa versione lo fa parlare. - `src/main.cpp`: log su file dal primo istante — versione di Qt, versione di Sailfish OS, contenuto di `/etc/os-release` e `/etc/sailfish-release` (la versione di sistema decide se il binario può caricarsi), stato del QML con gli errori di caricamento, e i segnali di crash (SIGSEGV, SIGABRT, SIGBUS, SIGILL, SIGFPE) scritti prima di uscire - `qInstallMessageHandler`: ogni `qDebug`/`qWarning`, compresi quelli del QML, finisce nello stesso file - `engineview.cpp`: percorso e dimensione della demo letta, ogni disegno con dimensioni e tempo, il font risolto e se esiste - `src/ffi.rs`: un panico Rust attraverso il confine C diventa un `abort` e l'app sparisce senza dire niente. Ora un gancio scrive `~/mosaic-panic.txt` con posizione e messaggio. Separato in `annota_panico()` per poterlo verificare senza far abortire il processo: c'è un test che controlla che il file venga scritto davvero Due file, quindi: `mosaic.log` (dove si arriva) e `mosaic-panic.txt` (se è il motore a cedere). Se non compare nemmeno il primo, l'app muore prima di `main()` — cioè il caricatore non riesce a linkare il binario, ed è un'informazione altrettanto utile. Aggiunto anche `tests/ffi_app_smoke.c`: la sequenza dell'app in C, per provarla sul telefono e sotto qemu.
651 lines
21 KiB
C++
651 lines
21 KiB
C++
#include "engineview.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include <QDebug>
|
|
#include <QMetaObject>
|
|
#include <QPointer>
|
|
|
|
#include <cstdint>
|
|
#include <thread>
|
|
|
|
#include <QCoreApplication>
|
|
#include <QDebug>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QElapsedTimer>
|
|
#include <QFileInfo>
|
|
#include <QPainter>
|
|
#include <QStandardPaths>
|
|
|
|
extern "C" {
|
|
#include "engine.h"
|
|
}
|
|
|
|
EngineView::EngineView(QQuickItem *parent)
|
|
: QQuickPaintedItem(parent)
|
|
{
|
|
m_vivo = std::make_shared<std::atomic<bool>>(true);
|
|
setAntialiasing(false);
|
|
m_fontPath = resolveFont();
|
|
qInfo() << "mosaic: EngineView costruito, font" << m_fontPath
|
|
<< "esiste" << QFileInfo::exists(m_fontPath);
|
|
m_sessione = std::shared_ptr<EngineSession>(engine_session_new(),
|
|
[](EngineSession *s) { engine_session_free(s); });
|
|
// i timer della pagina: il motore non legge l'ora, gliela diamo noi
|
|
m_timerJs.setInterval(100);
|
|
connect(&m_timerJs, &QTimer::timeout, this, &EngineView::pompaTimer);
|
|
m_timerJs.start();
|
|
// Le immagini con src relativo si risolvono nella cartella dati dell'app.
|
|
m_baseDir = QCoreApplication::applicationDirPath() + QStringLiteral("/../share/harbour-mosaic");
|
|
if (!QFileInfo::exists(m_baseDir))
|
|
m_baseDir = QStringLiteral("/usr/share/harbour-mosaic");
|
|
if (m_fontPath.isEmpty()) {
|
|
setError(QStringLiteral("font non trovato: /usr/share/harbour-mosaic/fonts/DejaVuSans.ttf"));
|
|
}
|
|
}
|
|
|
|
void EngineView::setHtml(const QString &html)
|
|
{
|
|
if (m_html == html)
|
|
return;
|
|
m_html = html;
|
|
creaDocumento();
|
|
emit htmlChanged();
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::setExtraCss(const QString &css)
|
|
{
|
|
if (m_css == css)
|
|
return;
|
|
m_css = css;
|
|
emit extraCssChanged();
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::setZoom(qreal zoom)
|
|
{
|
|
const qreal clamped = qBound<qreal>(0.75, zoom, 4.0);
|
|
if (qFuzzyCompare(clamped, m_zoom))
|
|
return;
|
|
m_zoom = clamped;
|
|
emit zoomChanged();
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
|
|
{
|
|
QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
|
|
// Solo la larghezza cambia l'impaginazione: l'altezza la decide il documento
|
|
// (altezza automatica), quindi reagire anche a quella chiuderebbe un ciclo
|
|
// di binding fra height e contentHeight.
|
|
if (!qFuzzyCompare(newGeometry.width(), oldGeometry.width()))
|
|
rerender();
|
|
}
|
|
|
|
EngineView::~EngineView()
|
|
{
|
|
if (m_vivo) {
|
|
m_vivo->store(false);
|
|
}
|
|
m_timerJs.stop();
|
|
if (m_doc) {
|
|
engine_doc_free(m_doc);
|
|
m_doc = nullptr;
|
|
}
|
|
m_sessione.reset();
|
|
}
|
|
|
|
void EngineView::paint(QPainter *painter)
|
|
{
|
|
if (m_image.isNull()) {
|
|
painter->fillRect(boundingRect(), Qt::white);
|
|
return;
|
|
}
|
|
painter->drawImage(QPointF(0, 0), m_image);
|
|
}
|
|
|
|
QString EngineView::resolveFont() const
|
|
{
|
|
QStringList candidates;
|
|
const QByteArray env = qgetenv("HARBOUR_MOSAIC_FONT");
|
|
if (!env.isEmpty())
|
|
candidates << QString::fromLocal8Bit(env);
|
|
candidates << QStringLiteral("/usr/share/harbour-mosaic/fonts/DejaVuSans.ttf")
|
|
<< QStringLiteral("/usr/share/fonts/truetype/DejaVuSans.ttf")
|
|
<< QStringLiteral("/usr/share/fonts/dejavu/DejaVuSans.ttf");
|
|
for (const QString &c : candidates) {
|
|
if (QFileInfo::exists(c))
|
|
return c;
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
/// Carica la pagina di prova dal pacchetto: è la stessa del desktop, così le due
|
|
/// piattaforme mostrano lo stesso documento invece di due demo diverse.
|
|
bool EngineView::caricaDemo()
|
|
{
|
|
const QStringList candidati{
|
|
QStringLiteral("/usr/share/harbour-mosaic/demo.html"),
|
|
QStringLiteral("/usr/share/harbour-mosaic/qml/pages/demo.html"),
|
|
};
|
|
for (const QString &percorso : candidati) {
|
|
QFile file(percorso);
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
continue;
|
|
}
|
|
m_html = QString::fromUtf8(file.readAll());
|
|
qInfo() << "mosaic: demo letta da" << percorso << "byte" << m_html.size();
|
|
m_url.clear();
|
|
creaDocumento();
|
|
rerender();
|
|
emit urlChanged();
|
|
return true;
|
|
}
|
|
qWarning() << "mosaic: demo.html non trovata nel pacchetto";
|
|
return false;
|
|
}
|
|
|
|
/// Crea (o ricrea) il documento con stato: DOM + macchina JavaScript.
|
|
void EngineView::creaDocumento()
|
|
{
|
|
if (m_doc) {
|
|
engine_doc_free(m_doc);
|
|
m_doc = nullptr;
|
|
}
|
|
// la sessione NON si tocca: i cookie devono sopravvivere al documento
|
|
if (!m_sessione) {
|
|
m_sessione = std::shared_ptr<EngineSession>(engine_session_new(),
|
|
[](EngineSession *s) { engine_session_free(s); });
|
|
}
|
|
if (m_html.isEmpty()) {
|
|
return;
|
|
}
|
|
const QByteArray indirizzo = m_url.toUtf8();
|
|
// con la sessione: le sotto-risorse usano gli stessi cookie del documento
|
|
m_doc = engine_doc_new_session(m_sessione.get(),
|
|
m_html.toUtf8().constData(),
|
|
m_css.toUtf8().constData(),
|
|
m_baseDir.toUtf8().constData(),
|
|
m_url.isEmpty() ? nullptr : indirizzo.constData());
|
|
if (!m_doc) {
|
|
setError(QStringLiteral("documento non creabile"));
|
|
return;
|
|
}
|
|
if (char *errori = engine_doc_errors(m_doc)) {
|
|
const QString testo = QString::fromUtf8(errori);
|
|
engine_string_free(errori);
|
|
for (const QString &riga : testo.split(QLatin1Char('\n'), QString::SkipEmptyParts)) {
|
|
qWarning() << "mosaic: script:" << riga;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Consegna un clic alle coordinate CSS e ridisegna se qualcuno ha reagito.
|
|
void EngineView::clicCss(qreal cssX, qreal cssY)
|
|
{
|
|
if (!m_doc) {
|
|
return;
|
|
}
|
|
const int gestori = engine_doc_click(m_doc, float(cssX), float(cssY));
|
|
qDebug() << "mosaic: click js" << cssX << cssY << "gestori" << gestori;
|
|
|
|
// pulsante di invio senza preventDefault: il formulario va mandato davvero
|
|
if (eseguiInvio()) {
|
|
return; // la risposta sostituisce la pagina e la ridisegna
|
|
}
|
|
if (gestori > 0) {
|
|
rerender();
|
|
}
|
|
}
|
|
|
|
/// Esegue l'invio del formulario che il motore ha lasciato in attesa, se c'è.
|
|
/// Restituisce vero se la pagina è stata sostituita dalla risposta.
|
|
bool EngineView::eseguiInvio()
|
|
{
|
|
if (!m_doc || !m_sessione) {
|
|
return false;
|
|
}
|
|
const QByteArray stato = stateJson().toUtf8();
|
|
char *json = engine_doc_invio_json(m_doc, stato.constData());
|
|
if (!json) {
|
|
return false;
|
|
}
|
|
const QJsonObject richiesta = QJsonDocument::fromJson(QByteArray(json)).object();
|
|
engine_string_free(json);
|
|
if (richiesta.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
const QString metodo = richiesta.value(QStringLiteral("metodo")).toString(QStringLiteral("GET"));
|
|
const QString url = richiesta.value(QStringLiteral("url")).toString();
|
|
const QString corpo = richiesta.value(QStringLiteral("corpo")).toString();
|
|
const QString tipo = richiesta.value(QStringLiteral("tipo")).toString();
|
|
if (url.isEmpty()) {
|
|
setError(QStringLiteral("invio senza indirizzo"));
|
|
return false;
|
|
}
|
|
qDebug() << "mosaic: invio" << metodo << url;
|
|
|
|
int statoHttp = 0;
|
|
char *errore = nullptr;
|
|
char *testo = engine_session_fetch(m_sessione.get(),
|
|
url.toUtf8().constData(),
|
|
metodo.toUtf8().constData(),
|
|
corpo.toUtf8().constData(),
|
|
tipo.toUtf8().constData(),
|
|
&statoHttp,
|
|
&errore);
|
|
if (errore) {
|
|
setError(QStringLiteral("invio fallito — %1").arg(QString::fromUtf8(errore)));
|
|
engine_string_free(errore);
|
|
}
|
|
if (!testo) {
|
|
return false;
|
|
}
|
|
m_html = QString::fromUtf8(testo);
|
|
engine_string_free(testo);
|
|
m_url = url;
|
|
creaDocumento();
|
|
rerender();
|
|
emit htmlChanged();
|
|
emit urlChanged();
|
|
return true;
|
|
}
|
|
|
|
/// Fa avanzare i timer della pagina. Restituisce vero se qualcosa è cambiato.
|
|
bool EngineView::serviFetch()
|
|
{
|
|
if (!m_doc || !m_sessione) {
|
|
return false;
|
|
}
|
|
char *elenco = engine_doc_requests_json(m_doc);
|
|
if (!elenco) {
|
|
return false;
|
|
}
|
|
const QByteArray json(elenco);
|
|
engine_string_free(elenco);
|
|
const QJsonArray richieste = QJsonDocument::fromJson(json).array();
|
|
if (richieste.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
bool cambiato = false;
|
|
const QByteArray base = m_url.toUtf8();
|
|
for (const QJsonValue &voce : richieste) {
|
|
const QJsonObject r = voce.toObject();
|
|
const QString riferito = r.value(QStringLiteral("url")).toString();
|
|
const QString metodo = r.value(QStringLiteral("metodo")).toString(QStringLiteral("GET"));
|
|
const QString corpo = r.value(QStringLiteral("corpo")).toString();
|
|
const quint64 id = quint64(r.value(QStringLiteral("id")).toDouble());
|
|
|
|
// `fetch('dati.json')` è relativo al documento
|
|
char *assoluto = engine_risolvi_url(base.constData(), riferito.toUtf8().constData());
|
|
if (!assoluto) {
|
|
qWarning() << "mosaic: fetch non risolvibile:" << riferito;
|
|
engine_doc_deliver(m_doc, id, -1, "");
|
|
continue;
|
|
}
|
|
const QByteArray url(assoluto);
|
|
engine_string_free(assoluto);
|
|
|
|
int stato = 0;
|
|
char *messaggio_fetch = nullptr;
|
|
char *corpo_risposta = engine_session_fetch(m_sessione.get(),
|
|
url.constData(),
|
|
metodo.toUtf8().constData(),
|
|
corpo.toUtf8().constData(),
|
|
"",
|
|
&stato,
|
|
&messaggio_fetch);
|
|
const QByteArray dati(corpo_risposta ? corpo_risposta : "");
|
|
if (corpo_risposta) {
|
|
engine_string_free(corpo_risposta);
|
|
}
|
|
if (!corpo_risposta) {
|
|
const QString motivo = messaggio_fetch ? QString::fromUtf8(messaggio_fetch)
|
|
: QStringLiteral("motivo non riportato");
|
|
qWarning() << "mosaic: fetch fallita" << url << "—" << motivo;
|
|
}
|
|
if (messaggio_fetch) {
|
|
engine_string_free(messaggio_fetch);
|
|
}
|
|
if (engine_doc_deliver(m_doc, id, corpo_risposta ? stato : -1, dati.constData()) > 0) {
|
|
cambiato = true;
|
|
}
|
|
}
|
|
return cambiato;
|
|
}
|
|
|
|
bool EngineView::pompaTimer()
|
|
{
|
|
if (!m_doc) {
|
|
return false;
|
|
}
|
|
bool cambiato = false;
|
|
|
|
// fetch: le richieste accodate dagli script le esegue il guscio
|
|
if (serviFetch()) {
|
|
cambiato = true;
|
|
}
|
|
|
|
// timer: solo se c'è qualcosa in scadenza
|
|
const qint64 adesso = QDateTime::currentMSecsSinceEpoch();
|
|
const qint64 prossimo = engine_doc_next_timer(m_doc);
|
|
if (prossimo >= 0 && prossimo <= adesso) {
|
|
if (engine_doc_pump(m_doc, quint64(adesso)) > 0) {
|
|
cambiato = true;
|
|
}
|
|
}
|
|
if (cambiato) {
|
|
rerender();
|
|
}
|
|
return cambiato;
|
|
}
|
|
|
|
void EngineView::rerender()
|
|
{
|
|
if (m_html.isEmpty())
|
|
return;
|
|
const int w = int(width());
|
|
if (w <= 0)
|
|
return;
|
|
if (m_fontPath.isEmpty()) {
|
|
setError(QStringLiteral("font non trovato: il motore non può impaginare testo"));
|
|
return;
|
|
}
|
|
// Altezza minima: il documento può essere più alto, mai più basso.
|
|
const int minH = qMax(1, int(qMax<qreal>(0.0, height())));
|
|
|
|
QElapsedTimer timer;
|
|
timer.start();
|
|
// con un documento vivo il rendering parte dal suo DOM (che gli script
|
|
// possono aver modificato), altrimenti dall'HTML di partenza
|
|
EngineImage *img = m_doc
|
|
? engine_doc_render(m_doc, quint32(w), quint32(minH), float(m_zoom),
|
|
1 /* auto_height */, m_fontPath.toUtf8().constData(),
|
|
stateJson().toUtf8().constData())
|
|
: engine_render_form(m_html.toUtf8().constData(),
|
|
m_css.toUtf8().constData(),
|
|
quint32(w), quint32(minH),
|
|
float(m_zoom),
|
|
1 /* auto_height */,
|
|
m_fontPath.toUtf8().constData(),
|
|
m_baseDir.toUtf8().constData(),
|
|
stateJson().toUtf8().constData());
|
|
m_renderMs = int(timer.elapsed());
|
|
qInfo() << "mosaic: disegno" << (img ? int(img->width) : -1) << "x"
|
|
<< (img ? int(img->height) : -1) << "in" << m_renderMs << "ms, zoom" << m_zoom
|
|
<< "doc" << (m_doc != nullptr);
|
|
emit renderMsChanged();
|
|
|
|
if (!img) {
|
|
setError(QStringLiteral("engine_render ha restituito NULL"));
|
|
return;
|
|
}
|
|
if (img->error) {
|
|
setError(QString::fromUtf8(img->error));
|
|
engine_image_free(img);
|
|
return;
|
|
}
|
|
|
|
// controlli del documento: chiave, tipo e rettangolo in pixel CSS
|
|
if (img->controls) {
|
|
const QJsonDocument doc = QJsonDocument::fromJson(QByteArray(img->controls));
|
|
m_controls = doc.array().toVariantList();
|
|
emit controlsChanged();
|
|
} else if (!m_controls.isEmpty()) {
|
|
m_controls.clear();
|
|
emit controlsChanged();
|
|
}
|
|
if (img->pixels && img->width > 0 && img->height > 0) {
|
|
QImage view(img->pixels, int(img->width), int(img->height), QImage::Format_RGBA8888);
|
|
m_image = view.copy();
|
|
setError(QString());
|
|
emit contentChanged();
|
|
update();
|
|
}
|
|
engine_image_free(img);
|
|
}
|
|
|
|
void EngineView::setError(const QString &error)
|
|
{
|
|
if (m_error == error)
|
|
return;
|
|
m_error = error;
|
|
if (!error.isEmpty())
|
|
qWarning() << "harbour-mosaic:" << error;
|
|
emit lastErrorChanged();
|
|
}
|
|
|
|
QString EngineView::stateJson() const
|
|
{
|
|
QJsonObject values;
|
|
for (auto it = m_values.constBegin(); it != m_values.constEnd(); ++it) {
|
|
values.insert(it.key(), it.value());
|
|
}
|
|
QJsonObject stato;
|
|
stato.insert(QStringLiteral("values"), values);
|
|
if (!m_focus.isEmpty()) {
|
|
stato.insert(QStringLiteral("focus"), m_focus);
|
|
}
|
|
if (m_caret >= 0) {
|
|
stato.insert(QStringLiteral("caret"), m_caret);
|
|
}
|
|
if (!m_openSelect.isEmpty()) {
|
|
stato.insert(QStringLiteral("open_select"), m_openSelect);
|
|
}
|
|
if (m_selectionStart >= 0 && m_selectionEnd >= 0 && m_selectionStart != m_selectionEnd) {
|
|
stato.insert(
|
|
QStringLiteral("selection"),
|
|
QJsonArray{m_selectionStart, m_selectionEnd});
|
|
}
|
|
return QString::fromUtf8(QJsonDocument(stato).toJson(QJsonDocument::Compact));
|
|
}
|
|
|
|
/// Controllo che contiene il punto indicato (pixel CSS): i rettangoli arrivano
|
|
/// dal motore, quindi il guscio non duplica la geometria.
|
|
QString EngineView::controlAt(qreal cssX, qreal cssY) const
|
|
{
|
|
for (const QVariant &v : m_controls) {
|
|
const QVariantMap m = v.toMap();
|
|
const qreal x = m.value(QStringLiteral("x")).toDouble();
|
|
const qreal y = m.value(QStringLiteral("y")).toDouble();
|
|
const qreal w = m.value(QStringLiteral("w")).toDouble();
|
|
const qreal h = m.value(QStringLiteral("h")).toDouble();
|
|
if (cssX >= x && cssX <= x + w && cssY >= y && cssY <= y + h) {
|
|
return m.value(QStringLiteral("key")).toString();
|
|
}
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
QString EngineView::kindOf(const QString &key) const
|
|
{
|
|
for (const QVariant &v : m_controls) {
|
|
const QVariantMap m = v.toMap();
|
|
if (m.value(QStringLiteral("key")).toString() == key) {
|
|
return m.value(QStringLiteral("kind")).toString();
|
|
}
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
QString EngineView::value(const QString &key) const
|
|
{
|
|
return m_values.value(key);
|
|
}
|
|
|
|
void EngineView::setValue(const QString &key, const QString &value)
|
|
{
|
|
if (key.isEmpty() || m_values.value(key) == value) {
|
|
return;
|
|
}
|
|
m_values.insert(key, value);
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::setFocus(const QString &key)
|
|
{
|
|
qDebug() << "mosaic: setFocus" << key;
|
|
if (m_focus == key) {
|
|
return;
|
|
}
|
|
m_focus = key;
|
|
emit focusedKeyChanged();
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::toggle(const QString &key)
|
|
{
|
|
if (key.isEmpty()) {
|
|
return;
|
|
}
|
|
const bool acceso = m_values.value(key) == QLatin1String("on");
|
|
m_values.insert(key, acceso ? QString() : QStringLiteral("on"));
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::setCaret(int pos)
|
|
{
|
|
if (m_caret == pos) {
|
|
return;
|
|
}
|
|
m_caret = pos;
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::setSelection(int inizio, int fine)
|
|
{
|
|
if (m_selectionStart == inizio && m_selectionEnd == fine) {
|
|
return;
|
|
}
|
|
m_selectionStart = inizio;
|
|
m_selectionEnd = fine;
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::setOpenSelect(const QString &key)
|
|
{
|
|
qDebug() << "mosaic: setOpenSelect" << key;
|
|
if (m_openSelect == key) {
|
|
return;
|
|
}
|
|
m_openSelect = key;
|
|
rerender();
|
|
}
|
|
|
|
void EngineView::chooseOption(const QString &key, int index)
|
|
{
|
|
qDebug() << "mosaic: chooseOption" << key << index;
|
|
for (const QVariant &v : m_controls) {
|
|
const QVariantMap m = v.toMap();
|
|
if (m.value(QStringLiteral("key")).toString() != key) {
|
|
continue;
|
|
}
|
|
const QVariantList valori = m.value(QStringLiteral("values")).toList();
|
|
if (index >= 0 && index < valori.size()) {
|
|
m_values.insert(key, valori.at(index).toString());
|
|
}
|
|
break;
|
|
}
|
|
m_openSelect.clear();
|
|
rerender();
|
|
}
|
|
|
|
QString EngineView::textOf(const QString &key) const
|
|
{
|
|
for (const QVariant &v : m_controls) {
|
|
const QVariantMap m = v.toMap();
|
|
if (m.value(QStringLiteral("key")).toString() == key) {
|
|
return m.value(QStringLiteral("text")).toString();
|
|
}
|
|
}
|
|
return QString();
|
|
}
|
|
|
|
void EngineView::toggleSelect(const QString &key)
|
|
{
|
|
const QString nuovo = (m_openSelect == key) ? QString() : key;
|
|
qDebug() << "mosaic: toggleSelect" << key << "->" << nuovo;
|
|
setOpenSelect(nuovo);
|
|
}
|
|
|
|
void EngineView::fetchDone(const QString &html, const QString &errore, int stato)
|
|
{
|
|
if (!errore.isEmpty()) {
|
|
m_networkError = stato > 0
|
|
? tr("HTTP %1: %2").arg(stato).arg(errore)
|
|
: errore;
|
|
m_loadState = 3;
|
|
emit loadStateChanged();
|
|
return;
|
|
}
|
|
m_html = html;
|
|
creaDocumento();
|
|
rerender();
|
|
emit loadStateChanged();
|
|
}
|
|
|
|
void EngineView::loadUrl(const QString &url)
|
|
{
|
|
const QString indirizzo = url.trimmed();
|
|
if (indirizzo.isEmpty()) {
|
|
return;
|
|
}
|
|
if (!indirizzo.startsWith(QLatin1String("http://"))
|
|
&& !indirizzo.startsWith(QLatin1String("https://"))) {
|
|
m_networkError = tr("Indirizzo non valido: servono http:// o https://");
|
|
m_loadState = 3;
|
|
emit loadStateChanged();
|
|
return;
|
|
}
|
|
|
|
m_url = indirizzo;
|
|
m_networkError.clear();
|
|
m_loadState = 1;
|
|
emit urlChanged();
|
|
emit loadStateChanged();
|
|
|
|
// la richiesta gira fuori dal thread grafico; il risultato torna via coda
|
|
auto vivo = m_vivo;
|
|
// riferimento alla sessione: sopravvive alla finestra finché il thread la usa
|
|
auto sessione = m_sessione;
|
|
QPointer<EngineView> self(this);
|
|
const std::string u = indirizzo.toStdString();
|
|
std::thread([self, vivo, sessione, u]() {
|
|
std::string html;
|
|
std::string errore;
|
|
int stato = 0;
|
|
char *messaggio = nullptr;
|
|
// con la sessione: i cookie ricevuti restano per le richieste successive
|
|
char *testo = engine_session_fetch(sessione.get(), u.c_str(), "GET", "", "",
|
|
&stato, &messaggio);
|
|
if (testo) {
|
|
html = testo;
|
|
engine_string_free(testo);
|
|
} else if (messaggio) {
|
|
errore = messaggio;
|
|
engine_string_free(messaggio);
|
|
} else {
|
|
errore = "richiesta fallita (motivo non riportato)";
|
|
}
|
|
if (!errore.empty()) {
|
|
qWarning() << "mosaic: caricamento fallito" << u.c_str() << "—"
|
|
<< QString::fromStdString(errore);
|
|
}
|
|
if (!vivo->load() || self.isNull()) {
|
|
return; // il guscio non c'è più
|
|
}
|
|
// consegna per nome: la variante con lambda non c'è su tutte le Qt dell'SDK
|
|
QMetaObject::invokeMethod(self.data(),
|
|
"fetchDone",
|
|
Qt::QueuedConnection,
|
|
Q_ARG(QString, QString::fromStdString(html)),
|
|
Q_ARG(QString, QString::fromStdString(errore)),
|
|
Q_ARG(int, stato));
|
|
}).detach();
|
|
}
|