Load::Load(QObject *parent) : QObject(parent), d_ptr(new LoadPrivate(this)) { Q_D(Load); ins = this; setObjectName("Load"); auto avProcess = [this](QNetworkReply *reply){ Q_D(Load); Task &task = d->queue.head(); int sharp = task.code.indexOf(QRegularExpression("[#_]")); switch (task.state){ case None: { QString i = task.code.mid(2, sharp - 2); QString p = sharp == -1 ? QString() : task.code.mid(sharp + 1); QString url("http://www.%1/video/av%2/"); url = url.arg(Utils::customUrl(Utils::Bilibili)).arg(i); if (!p.isEmpty()){ url += QString("index_%1.html").arg(p); } forward(QNetworkRequest(url), Page); break; } case Page: { d->model->clear(); QString api, id, video(reply->readAll()); int part = video.indexOf("<select"); if (part != -1 && sharp == -1){ QRegularExpression r("(?<=>).*?(?=</option>)"); QStringRef list(&video, part, video.indexOf("</select>", part) - part); QRegularExpressionMatchIterator i = r.globalMatch(list); api = "http://www.%1/video/%2/index_%3.html"; api = api.arg(Utils::customUrl(Utils::Bilibili)); while (i.hasNext()){ int index = d->model->rowCount() + 1; QStandardItem *item = new QStandardItem; item->setData(QUrl(api.arg(task.code).arg(index)), UrlRole); item->setData((task.code + "#%1").arg(index), StrRole); item->setData(Page, NxtRole); item->setData(Utils::decodeXml(i.next().captured()), Qt::EditRole); d->model->appendRow(item); } } if (d->model->rowCount() > 0){ emit stateChanged(task.state = Part); } else{ QRegularExpression r = QRegularExpression("cid[=\":]*\\d+", QRegularExpression::CaseInsensitiveOption); QRegularExpressionMatchIterator i = r.globalMatch(video); while (i.hasNext()){ QString m = i.next().captured(); m = QRegularExpression("\\d+").match(m).captured(); if (id.isEmpty()){ id = m; } else if (id != m){ id.clear(); break; } } if (!id.isEmpty()){ api = "http://comment.%1/%2.xml"; api = api.arg(Utils::customUrl(Utils::Bilibili)); forward(QNetworkRequest(api.arg(id)), File); } else{ emit stateChanged(203); qDebug() << "Fail to load danmaku, try biliApi"; dequeue(); } } break; } case File: { dumpDanmaku(reply->readAll(), Utils::Bilibili, false); emit stateChanged(task.state = None); dequeue(); break; } } }; auto avRegular = [](QString &code){ code.remove(QRegularExpression("/index(?=_\\d+\\.html)")); QRegularExpression r("a(v(\\d+([#_])?(\\d+)?)?)?"); r.setPatternOptions(QRegularExpression::CaseInsensitiveOption); return getRegular(r)(code); }; d->pool.append({ avRegular, 0, avProcess }); auto bbProcess = [this, avProcess](QNetworkReply *reply) { Q_D(Load); Task &task = d->queue.head(); switch (task.state) { case None: { QString i = task.code.mid(2); QString u = "http://www.%1/bangumi/i/%2/"; u = u.arg(Utils::customUrl(Utils::Bilibili)).arg(i); forward(QNetworkRequest(u), Page); break; } case Page: { d->model->clear(); QString page(reply->readAll()); QStringList list = page.split("<li data-index"); if (list.size() < 2) { emit stateChanged(task.state = None); dequeue(); break; } list.removeFirst(); QListIterator<QString> iter(list); iter.toBack(); while (iter.hasPrevious()) { QRegularExpression r; const QString &i = iter.previous(); r.setPattern("(?<=href=\")[^\"]+"); QString c = r.match(i).captured(); fixCode(c); r.setPattern("(?<=<span>).+(?=</span>)"); QString t = Utils::decodeXml(r.match(i).captured()); QStandardItem *item = new QStandardItem; item->setData(c, StrRole); item->setData(None, NxtRole); item->setData(t, Qt::EditRole); d->model->appendRow(item); } emit stateChanged(task.state = Part); } } }; auto bbRegular = [](QString &code) { code.replace(QRegularExpression("bangumi/i/(?=\\d+)"), "bb"); QRegularExpression r("b(b(\\d+)?)?"); r.setPatternOptions(QRegularExpression::CaseInsensitiveOption); return getRegular(r)(code); }; d->pool.append({ bbRegular, 0, bbProcess }); auto acProcess = [this](QNetworkReply *reply){ Q_D(Load); Task &task = d->queue.head(); int sharp = task.code.indexOf(QRegularExpression("[#_]")); switch (task.state){ case None: { QString i = task.code.mid(2, sharp - 2); QString p = sharp == -1 ? QString() : task.code.mid(sharp + 1); QString url("http://www.%1/v/ac%2"); url = url.arg(Utils::customUrl(Utils::AcFun)).arg(i); if (!p.isEmpty()){ url += QString("_%1").arg(p); } forward(QNetworkRequest(url), Page); break;; } case Page: { d->model->clear(); QRegularExpressionMatchIterator match = QRegularExpression("data-vid.*?</a>").globalMatch(reply->readAll()); while (match.hasNext()){ QStandardItem *item = new QStandardItem; QString part = match.next().captured(); QRegularExpression r; r.setPattern("(?<=>)[^>]+?(?=</a>)"); item->setData(Utils::decodeXml(r.match(part).captured()), Qt::EditRole); r.setPattern("(?<=data-vid=\").+?(?=\")"); QString next("http://static.comment.%1/V2/%2?pageSize=1000&pageNo=1"); next = next.arg(Utils::customUrl(Utils::AcFun)).arg(r.match(part).captured()); item->setData(next, UrlRole); item->setData((task.code + "#%1").arg(d->model->rowCount() + 1), StrRole); item->setData(File, NxtRole); d->model->appendRow(item); } if (sharp == -1 && d->model->rowCount() >= 2){ emit stateChanged(task.state = Part); } else{ int i = sharp == -1 ? 0 : task.code.mid(sharp + 1).toInt() - 1; if (i >= 0 && i < d->model->rowCount()){ forward(QNetworkRequest(d->model->item(i)->data(UrlRole).toUrl()), File); } else{ emit stateChanged(203); dequeue(); } } break; } case File: { QByteArray data = reply->readAll(); if (data != "[[],[],[]]"){ QNetworkRequest &request = task.request; QUrl url = request.url(); int page = QUrlQuery(url).queryItemValue("pageNo").toInt(); url.setQuery(QString()); request.setUrl(url); dumpDanmaku(data, Utils::AcFun, false); QUrlQuery query; query.addQueryItem("pageSize", "1000"); query.addQueryItem("pageNo", QString::number(page + 1)); url.setQuery(query); request.setUrl(url); forward(request, File); } else{ emit stateChanged(task.state = None); dequeue(); } break; } } }; auto acRegular = getRegular(QRegularExpression("a(c(\\d+([#_])?(\\d+)?)?)?", QRegularExpression::CaseInsensitiveOption)); d->pool.append({ acRegular, 0, acProcess }); auto abProcess = [this, acProcess](QNetworkReply *reply){ Q_D(Load); Task &task = d->queue.head(); int sharp = task.code.indexOf(QRegularExpression("[#_]")); switch (task.state){ case None: { QString url("http://www.%1/bangumi/video/page?bangumiId=%2&pageSize=30&pageNo=%3&order=2"); url = url.arg(Utils::customUrl(Utils::AcFun)).arg(task.code.mid(2, sharp - 2)); url = url.arg(sharp == -1 ? 1 : (task.code.mid(sharp + 1).toInt() - 1) / 30 + 1); forward(QNetworkRequest(url), Page); break; } case Page: { if (sharp != -1){ QJsonObject data = QJsonDocument::fromJson(reply->readAll()).object()["data"].toObject(); int i = task.code.mid(sharp + 1).toInt(); if (i > 0){ i = (i - 1) % 30; } else{ i = data["totalCount"].toInt(); if (i > 30){ task.code = task.code.left(sharp) + QString("#%1").arg(i); task.state = None; task.processer->process(nullptr); break; } } QJsonArray list = data["list"].toArray(); if (i < 0 || i >= list.size()){ emit stateChanged(203); dequeue(); break; } QString head("http://static.comment.%1/V2/%2?pageSize=1000&pageNo=1"); head = head.arg(Utils::customUrl(Utils::AcFun)); head = head.arg(list[i].toObject()["danmakuId"].toString()); forward(QNetworkRequest(head), File); break; } else{ d->model->clear(); } } case Part: { QJsonObject info = QJsonDocument::fromJson(reply->readAll()).object(); if (!info["success"].toBool() && d->model->rowCount() == 0){ emit stateChanged(info["status"].toInt()); dequeue(); } QJsonObject data = info["data"].toObject(); for (const QJsonValue &value : data["list"].toArray()){ QStandardItem *item = new QStandardItem; QJsonObject data = value.toObject(); item->setData(data["title"].toString(), Qt::EditRole); QString head("http://static.comment.%1/V2/%2?pageSize=1000&pageNo=1"); head = head.arg(Utils::customUrl(Utils::AcFun)).arg(data["danmakuId"].toString()); item->setData(head, UrlRole); item->setData((task.code + "#%1").arg(d->model->rowCount() + 1), StrRole); item->setData(File, NxtRole); d->model->appendRow(item); } if (task.state != Part){ emit stateChanged(task.state = Part); } if (data["pageNo"].toInt() < data["totalPage"].toInt()){ QUrl url = reply->request().url(); auto arg = QUrlQuery(url).queryItems(); for (auto &p : arg){ if (p.first == "pageNo"){ p.second = QString::number(p.second.toInt() + 1); break; } } QUrlQuery query; query.setQueryItems(arg); url.setQuery(query); d->remain.insert(d->manager.get(QNetworkRequest(url))); } break; } case File: { acProcess(reply); break; } } }; auto abRegular = getRegular(QRegularExpression("a(b(\\d+([#_])?(\\d+)?)?)?", QRegularExpression::CaseInsensitiveOption)); d->pool.append({ abRegular, 0, abProcess }); auto ccProcess = [this](QNetworkReply *reply){ Q_D(Load); Task &task = d->queue.head(); int sharp = task.code.indexOf(QRegularExpression("[#_]")); switch (task.state){ case None: { QString i = task.code.mid(2, sharp - 2); QString p = sharp == -1 ? QString() : task.code.mid(sharp + 1); QString url("http://www.%1/play/h%2/"); url = url.arg(Utils::customUrl(Utils::TuCao)).arg(i); if (!p.isEmpty()){ url += QString("#%1").arg(p); } forward(QNetworkRequest(url), Page); break; } case Page: { QString page = reply->readAll(); d->model->clear(); QRegularExpressionMatch m; QRegularExpression r("(?<=<li>)[^<]*(?=</li>)"); m = r.match(page, page.indexOf("<ul id=\"player_code\"")); QStringList list = m.captured().split("**"); m = r.match(page, m.capturedEnd()); QString code = m.captured(); for (const QString &iter : list){ QStandardItem *item = new QStandardItem; item->setData(iter.mid(iter.indexOf('|') + 1), Qt::EditRole); QString api("http://www.%1/index.php?m=mukio&c=index&a=init&playerID=%2"); api = api.arg(Utils::customUrl(Utils::TuCao)).arg((code + "-%1").arg(d->model->rowCount())); item->setData(api, UrlRole); item->setData((task.code + "#%1").arg(d->model->rowCount() + 1), StrRole); item->setData(File, NxtRole); d->model->appendRow(item); } if (sharp == -1 && d->model->rowCount() >= 2){ emit stateChanged(task.state = Part); } else{ int i = sharp == -1 ? 0 : task.code.mid(sharp + 1).toInt() - 1; if (i >= 0 && i < d->model->rowCount()){ forward(QNetworkRequest(d->model->item(i)->data(UrlRole).toUrl()), File); } else{ emit stateChanged(203); dequeue(); } } break; } case File: { dumpDanmaku(reply->readAll(), Utils::TuCao, false); emit stateChanged(task.state = None); dequeue(); break; } } }; auto ccRegular = [](QString &code){ code.replace(QRegularExpression("[Hh](?=\\d)"), "cc"); QRegularExpression r("c(c(\\d+([#_])?(\\d+)?)?)?"); r.setPatternOptions(QRegularExpression::CaseInsensitiveOption); return getRegular(r)(code); }; d->pool.append({ ccRegular, 0, ccProcess }); d->pool.append(Proc()); Proc *directProc = &d->pool.last(); directProc->process = [this](QNetworkReply *reply){ Q_D(Load); Task &task = d->queue.head(); switch (task.state){ case None: { QUrl url = QUrl::fromUserInput(task.code); task.request.setUrl(url); task.state = File; forward(); break; } case File: { Record load; QUrl url = reply->url(); QByteArray data(reply->readAll()); load.source = url.url(); load.access = url.isLocalFile() ? url.toLocalFile() : load.source; load.string = QFileInfo(task.code).fileName(); load.delay = task.delay; QString head = Utils::decodeTxt(data.left(512)); if (head.startsWith("[Script Info]")){ load.danmaku = Parse::parseComment(data, Utils::ASS); } else if (!head.startsWith("<?xml")){ load.danmaku = Parse::parseComment(data, Utils::AcFun); } else if (head.indexOf("<packet>") != -1){ load.danmaku = Parse::parseComment(data, Utils::Niconico); } else if (head.indexOf("<i>") != -1){ load.danmaku = Parse::parseComment(data, Utils::Bilibili); QString i = QRegularExpression("(?<=<chatid>)\\d+(?=</chatid>)").match(head).captured(); if (!i.isEmpty()){ load.source = "http://comment.%1/%2.xml"; load.source = load.source.arg(Utils::customUrl(Utils::Bilibili)).arg(i); } } else if (head.indexOf("<c>") != -1){ load.danmaku = Parse::parseComment(data, Utils::AcfunLocalizer); } if (load.delay != 0){ for (Comment &c : load.danmaku){ c.time += load.delay; } } Danmaku::instance()->appendToPool(&load); emit stateChanged(task.state = None); dequeue(); break; } } }; directProc->priority = -100; directProc->regular = [this, directProc](QString &code){ if (code.startsWith("full?") || code.startsWith("hist?")){ code.clear(); return false; } QUrl u = QUrl::fromUserInput(code); if (!u.host().isEmpty() && !u.path().isEmpty()){ return true; } if (QFileInfo(code).exists()){ return true; } code.clear(); return false; }; auto fullBiProcess = [this](QNetworkReply *reply){ Q_D(Load); Task &task = d->queue.head(); switch (task.state) { case None: { emit progressChanged(0); QString api("http://comment.%1/rolldate,%2"); api = api.arg(Utils::customUrl(Utils::Bilibili)); task.code = QUrlQuery(task.code.mid(5)).queryItemValue("source"); forward(QNetworkRequest(api.arg(QFileInfo(task.code).baseName())), Page); break; } case Page: { QByteArray data = reply->readAll(); QJsonArray date = QJsonDocument::fromJson(data).array(); if (date.isEmpty()) { emit stateChanged(203); dequeue(); break; } QJsonObject head = date.first().toObject(); QString url("http://comment.%1/dmroll,%2,%3"); url = url.arg(Utils::customUrl(Utils::Bilibili)); url = url.arg(head["timestamp"].toVariant().toInt()); url = url.arg(QFileInfo(task.code).baseName()); QNetworkRequest request(url); request.setAttribute(QNetworkRequest::User, data); forward(request, Code); break; } case Code: { QByteArray data = task.request.attribute(QNetworkRequest::User).toByteArray(); QJsonArray date = QJsonDocument::fromJson(data).array(); QMap<int, int> count; for (auto iter : date) { QJsonObject item = iter.toObject(); count[item["timestamp"].toVariant().toInt()] += item["new"].toVariant().toInt(); } data = reply->readAll(); if (count.size() >= 2) { int max = QRegularExpression("(?<=\\<max_count\\>).+(?=\\</max_count\\>)").match(data).captured().toInt(); int now = 0; auto getHistory = [d, &count, &task](int date) { QString url("http://comment.%1/dmroll,%2,%3"); url = url.arg(Utils::customUrl(Utils::Bilibili)); url = url.arg(date); url = url.arg(QFileInfo(task.code).baseName()); return d->manager.get(QNetworkRequest(url)); }; for (auto iter = count.begin() + 1;; ++iter) { now += iter.value(); if (iter + 1 == count.end()) { d->remain += getHistory(iter.key()); break; } else if (now + (iter + 1).value() > max) { d->remain += getHistory(iter.key()); now = 0; } } auto pool = QSharedPointer<QVector<Parse::ResultDelegate>>::create(); pool->append(Parse::parseComment(data, Utils::Bilibili)); double total = d->remain.size() + 2; for (QNetworkReply *iter : d->remain) { connect(iter, &QNetworkReply::finished, [=, &task]() { QByteArray data = iter->readAll(); pool->append(Parse::parseComment(data, Utils::Bilibili)); switch (iter->error()) { case QNetworkReply::NoError: emit progressChanged((total - d->remain.size()) / total); case QNetworkReply::OperationCanceledError: if (d->remain.isEmpty() && !pool->empty()) { Record load; load.full = true; for (auto &iter : *pool) { load.danmaku.append(iter); } load.source = task.code; Danmaku::instance()->appendToPool(&load); emit stateChanged(task.state = None); dequeue(); } default: break; } }); } emit progressChanged(2 / total); emit stateChanged(task.state = File); break; } else { emit progressChanged(1); dumpDanmaku(data, Utils::Bilibili, true); emit stateChanged(task.state = None); dequeue(); break; } } } }; auto fullBiRegular = QRegularExpression("^full\\?source=http://comment\\.bilibili\\.com/\\d+\\.xml$"); fullBiRegular.setPatternOptions(QRegularExpression::CaseInsensitiveOption); d->pool.append({ getRegular(fullBiRegular), 100, fullBiProcess }); auto histBiProcess = [this](QNetworkReply *reply){ Q_D(Load); Task &task = d->queue.head(); switch (task.state){ case None: { QUrlQuery query(task.code.mid(5)); task.code = query.queryItemValue("source"); QString cid = QFileInfo(task.code).baseName(); QString dat = query.queryItemValue("date"); QString url; QNetworkRequest request; if (dat != "0" && dat.toUInt() != QDateTime(QDate::currentDate()).toTime_t()){ url = QString("http://comment.%1/dmroll,%2,%3"); url = url.arg(Utils::customUrl(Utils::Bilibili)); url = url.arg(dat).arg(cid); int limit = QDateTime(QDateTime::fromTime_t(dat.toInt()).date().addDays(1)).toTime_t(); request.setAttribute(QNetworkRequest::User, limit); } else{ url = QString("http://comment.%1/%2.xml").arg(Utils::customUrl(Utils::Bilibili)); url = url.arg(cid); } request.setUrl(url); forward(request, File); break; } case File: { Record load; load.danmaku = Parse::parseComment(reply->readAll(), Utils::Bilibili); load.source = task.code; for (Record &iter : Danmaku::instance()->getPool()){ if (iter.source == load.source){ iter.full = false; iter.danmaku.clear(); iter.limit = 1; break; } } load.limit = task.request.attribute(QNetworkRequest::User).toInt(); Danmaku::instance()->appendToPool(&load); emit stateChanged(task.state = None); dequeue(); break; } } }; auto histBiRegular = QRegularExpression("^hist\\?source=http://comment\\.bilibili\\.com/\\d+\\.xml&date=\\d+$"); histBiRegular.setPatternOptions(QRegularExpression::CaseInsensitiveOption); d->pool.append({ getRegular(histBiRegular), 100, histBiProcess }); connect(this, &Load::stateChanged, [this](int code){ switch (code){ case None: case Page: case Part: case Code: case File: break; default: { Q_D(Load); if (!d->tryNext()){ emit errorOccured(code); } break; } } }); }
void IPTVStreamHandler::run(void) { RunProlog(); LOG(VB_GENERAL, LOG_INFO, LOC + "run()"); SetRunning(true, false, false); // TODO Error handling.. // Setup CetonRTSP *rtsp = NULL; IPTVTuningData tuning = m_tuning; if (m_tuning.GetURL(0).scheme().toLower() == "rtsp") { rtsp = new CetonRTSP(m_tuning.GetURL(0)); // Check RTSP capabilities QStringList options; if (!(rtsp->GetOptions(options) && options.contains("OPTIONS") && options.contains("DESCRIBE") && options.contains("SETUP") && options.contains("PLAY") && options.contains("TEARDOWN"))) { LOG(VB_RECORD, LOG_ERR, LOC + "RTSP interface did not support the necessary options"); delete rtsp; SetRunning(false, false, false); RunEpilog(); return; } if (!rtsp->Describe()) { LOG(VB_RECORD, LOG_ERR, LOC + "RTSP Describe command failed"); delete rtsp; SetRunning(false, false, false); RunEpilog(); return; } tuning = IPTVTuningData( QString("rtp://%1@%2:0") .arg(m_tuning.GetURL(0).host()) .arg(QHostAddress(QHostAddress::Any).toString()), 0, IPTVTuningData::kNone, QString("rtp://%1@%2:0") .arg(m_tuning.GetURL(0).host()) .arg(QHostAddress(QHostAddress::Any).toString()), 0, "", 0); } for (uint i = 0; i < IPTV_SOCKET_COUNT; i++) { QUrl url = tuning.GetURL(i); if (url.port() < 0) continue; m_sockets[i] = new QTcpSocket(); m_sockets[i]->setSocketOption(QAbstractSocket::LowDelayOption, 1); m_read_helpers[i] = new IPTVStreamHandlerReadHelper( this, m_sockets[i], i); if (!url.userInfo().isEmpty()) m_sender[i] = QHostAddress(url.userInfo()); m_sockets[i]->connectToHost(url.host().toAscii(), 3000); if (m_sockets[i]->waitForConnected(1000)) { m_sockets[i]->write("GET " + url.path().toAscii() + " HTTP/1.0\r\n\r\n\r\n\r\n"); m_sockets[i]->waitForBytesWritten(500); } else { qDebug("not Connected!"); m_sockets[i]->connectToHost(url.host().toAscii(), 3000); m_sockets[i]->write("GET " + url.path().toAscii() + " HTTP/1.0\r\n\r\n\r\n\r\n"); m_sockets[i]->waitForBytesWritten(500); } } //if (m_use_rtp_streaming) // m_buffer = new RTPPacketBuffer(tuning.GetBitrate(0)); //else m_buffer = new UDPPacketBuffer(tuning.GetBitrate(0)); m_write_helper = new IPTVStreamHandlerWriteHelper(this); m_write_helper->Start(); bool error = false; /*if (rtsp) { // Start Streaming if (!rtsp->Setup(m_sockets[0]->localPort(), m_sockets[1]->localPort()) || !rtsp->Play()) { LOG(VB_RECORD, LOG_ERR, LOC + "Starting recording (RTP initialization failed). Aborting."); error = true; } } */ if (!error) { // Enter event loop exec(); } // Clean up for (uint i = 0; i < IPTV_SOCKET_COUNT; i++) { if (m_sockets[i]) { delete m_sockets[i]; m_sockets[i] = NULL; delete m_read_helpers[i]; m_read_helpers[i] = NULL; } } delete m_buffer; m_buffer = NULL; delete m_write_helper; m_write_helper = NULL; if (rtsp) { rtsp->Teardown(); delete rtsp; } SetRunning(false, false, false); RunEpilog(); }
QNetworkReplyFileImpl::QNetworkReplyFileImpl(QObject *parent, const QNetworkRequest &req, const QNetworkAccessManager::Operation op) : QNetworkReply(*new QNetworkReplyFileImplPrivate(), parent) { setRequest(req); setUrl(req.url()); setOperation(op); setFinished(true); QNetworkReply::open(QIODevice::ReadOnly); QNetworkReplyFileImplPrivate *d = (QNetworkReplyFileImplPrivate*) d_func(); QUrl url = req.url(); if (url.host() == QLatin1String("localhost")) url.setHost(QString()); #if !defined(Q_OS_WIN) // do not allow UNC paths on Unix if (!url.host().isEmpty()) { // we handle only local files QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString()); setError(QNetworkReply::ProtocolInvalidOperationError, msg); QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ProtocolInvalidOperationError)); QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); return; } #endif if (url.path().isEmpty()) url.setPath(QLatin1String("/")); setUrl(url); QString fileName = url.toLocalFile(); if (fileName.isEmpty()) { if (url.scheme() == QLatin1String("qrc")) { fileName = QLatin1Char(':') + url.path(); } else { #if defined(Q_OS_ANDROID) if (url.scheme() == QLatin1String("assets")) fileName = QLatin1String("assets:") + url.path(); else #endif fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery); } } QFileInfo fi(fileName); if (fi.isDir()) { QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Cannot open %1: Path is a directory").arg(url.toString()); setError(QNetworkReply::ContentOperationNotPermittedError, msg); QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentOperationNotPermittedError)); QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); return; } d->realFile.setFileName(fileName); bool opened = d->realFile.open(QIODevice::ReadOnly | QIODevice::Unbuffered); // could we open the file? if (!opened) { QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2") .arg(d->realFile.fileName(), d->realFile.errorString()); if (d->realFile.exists()) { setError(QNetworkReply::ContentAccessDenied, msg); QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentAccessDenied)); } else { setError(QNetworkReply::ContentNotFoundError, msg); QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, Q_ARG(QNetworkReply::NetworkError, QNetworkReply::ContentNotFoundError)); } QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); return; } setHeader(QNetworkRequest::LastModifiedHeader, fi.lastModified()); d->realFileSize = fi.size(); setHeader(QNetworkRequest::ContentLengthHeader, d->realFileSize); QMetaObject::invokeMethod(this, "metaDataChanged", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "downloadProgress", Qt::QueuedConnection, Q_ARG(qint64, d->realFileSize), Q_ARG(qint64, d->realFileSize)); QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection); QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); }
bool TreeWidget::dropMimeData(QTreeWidgetItem* parent, int, const QMimeData* data, Qt::DropAction action) { if (action == Qt::IgnoreAction) { return true; } if (parent && !parent->text(1).isEmpty()) { // parent is a bookmark, go one level up! parent = parent->parent(); } if (!parent) { parent = invisibleRootItem(); } bool ok = false; if (data->hasUrls()) { QString folder = (parent == invisibleRootItem()) ? QLatin1String("unsorted") : parent->text(0); QUrl url = data->urls().at(0); QString title = data->text().isEmpty() ? url.host() + url.path() : data->text(); emit linkWasDroped(url, title, data->imageData(), folder, &ok); return ok; } if (!data->hasFormat(m_mimeType)) { return false; } QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); QByteArray ba = data->data(m_mimeType); QDataStream stream(&ba, QIODevice::ReadOnly); if (stream.atEnd()) { return false; } QSqlDatabase db = QSqlDatabase::database(); db.transaction(); setUpdatesEnabled(false); while (!stream.atEnd()) { QTreeWidgetItem* item = new QTreeWidgetItem; item->read(stream); bool parentIsRoot = item->data(0, ITEM_IS_TOPLEVEL).toBool(); QString oldParentTitle = item->data(0, ITEM_PARENT_TITLE).toString(); bool isFolder = item->text(1).isEmpty(); if (isFolder && (item->text(0) == _bookmarksMenu || item->text(0) == _bookmarksToolbar)) { continue; } bool parentIsOldParent = parentIsRoot ? (parent == invisibleRootItem()) : (oldParentTitle == parent->text(0)); if (parentIsOldParent || (isFolder && parent != invisibleRootItem() && parent->text(0) != _bookmarksToolbar)) { // just 'Bookmarks In ToolBar' folder can have subfolders continue; } if (isFolder) { emit folderParentChanged(item->text(0), parent->text(0) == _bookmarksToolbar, &ok); } else { emit bookmarkParentChanged(item->data(0, Qt::UserRole + 10).toInt(), parent->text(0), oldParentTitle, &ok); } if (!ok) { continue; } } db.commit(); clearSelection(); setUpdatesEnabled(true); QApplication::restoreOverrideCursor(); return true; }
void SettingsManager::setValue(const QString &key, const QVariant &value, const QUrl &url) { if (!url.isEmpty()) { if (value.isNull()) { QSettings(m_overridePath, QSettings::IniFormat).remove((url.isLocalFile() ? QLatin1String("localhost") : url.host()) + QLatin1Char('/') + key); } else { QSettings(m_overridePath, QSettings::IniFormat).setValue((url.isLocalFile() ? QLatin1String("localhost") : url.host()) + QLatin1Char('/') + key, value); } return; } if (getValue(key) != value) { QSettings(m_globalPath, QSettings::IniFormat).setValue(key, value); emit m_instance->valueChanged(key, value); } }
void AddressWidget::handleUserInput(const QString &text) { BookmarksItem *bookmark = BookmarksManager::getBookmark(text); if (bookmark) { WindowsManager *windowsManager = SessionsManager::getWindowsManager(); if (windowsManager) { windowsManager->open(bookmark); return; } } if (text == QString(QLatin1Char('~')) || text.startsWith(QLatin1Char('~') + QDir::separator())) { const QStringList locations = QStandardPaths::standardLocations(QStandardPaths::HomeLocation); if (!locations.isEmpty()) { emit requestedLoadUrl(QUrl(locations.first() + text.mid(1))); return; } } if (QFileInfo(text).exists()) { emit requestedLoadUrl(QUrl::fromLocalFile(QFileInfo(text).canonicalFilePath())); return; } const QUrl url = QUrl::fromUserInput(text); if (url.isValid() && (url.isLocalFile() || QRegularExpression(QLatin1String("^(\\w+\\:\\S+)|([\\w\\-]+\\.[a-zA-Z]{2,}(/\\S*)?$)")).match(text).hasMatch())) { emit requestedLoadUrl(url); return; } const QString keyword = text.section(QLatin1Char(' '), 0, 0); const QStringList engines = SearchesManager::getSearchEngines(); SearchInformation *engine = NULL; for (int i = 0; i < engines.count(); ++i) { engine = SearchesManager::getSearchEngine(engines.at(i)); if (engine && keyword == engine->keyword) { emit requestedSearch(text.section(QLatin1Char(' '), 1), engine->identifier); return; } } const int lookupTimeout = SettingsManager::getValue(QLatin1String("AddressField/HostLookupTimeout")).toInt(); if (url.isValid() && lookupTimeout > 0) { if (text == m_lookupQuery) { return; } m_lookupQuery = text; if (m_lookupTimer != 0) { QHostInfo::abortHostLookup(m_lookupIdentifier); killTimer(m_lookupTimer); m_lookupTimer = 0; } m_lookupIdentifier = QHostInfo::lookupHost(url.host(), this, SLOT(verifyLookup(QHostInfo))); m_lookupTimer = startTimer(lookupTimeout); return; } emit requestedSearch(text, SettingsManager::getValue(QLatin1String("Search/DefaultSearchEngine")).toString()); }
QVariant SettingsManager::getValue(const QString &key, const QUrl &url) { if (!url.isEmpty()) { return QSettings(m_overridePath, QSettings::IniFormat).value((url.isLocalFile() ? QLatin1String("localhost") : url.host()) + QLatin1Char('/') + key, getValue(key)); } return QSettings(m_globalPath, QSettings::IniFormat).value(key, getDefaultValue(key)); }
bool PixmapChannel::CacheChannelIcon(void) { if (icon.isEmpty()) return false; m_localIcon = icon; // Is icon local? if (QFile(icon).exists()) return true; QString localDirStr = QString("%1/channels").arg(GetConfDir()); QDir localDir(localDirStr); if (!localDir.exists() && !localDir.mkdir(localDirStr)) { VERBOSE(VB_IMPORTANT, QString("Icons directory is missing and could " "not be created: %1").arg(localDirStr)); icon.clear(); return false; } // Has it been saved to the local cache? m_localIcon = QString("%1/%2").arg(localDirStr) .arg(QFileInfo(icon).fileName()); if (QFile(m_localIcon).exists()) return true; // Get address of master backed QString url = gCoreContext->GetMasterHostPrefix("ChannelIcons"); if (url.length() < 1) { icon.clear(); return false; } url.append(icon); QUrl qurl = url; if (qurl.host().isEmpty()) { icon.clear(); return false; } RemoteFile *rf = new RemoteFile(url, false, false, 0); QByteArray data; bool ret = rf->SaveAs(data); delete rf; if (ret && data.size()) { QImage image; image.loadFromData(data); //if (image.loadFromData(data) && image.width() > 0 if (image.save(m_localIcon)) { VERBOSE(VB_GENERAL, QString("Caching channel icon %1").arg(m_localIcon)); return true; } else VERBOSE(VB_GENERAL, QString("Failed to save to %1").arg(m_localIcon)); } // if we get here then the icon is set in the db but couldn't be found // anywhere so maybe we should remove it from the DB? icon.clear(); return false; }
QNetworkReply *AccessManager::createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData) { const QUrl reqUrl(req.url()); if (!d->externalContentAllowed && !KDEPrivate::AccessManagerReply::isLocalRequest(reqUrl) && reqUrl.scheme() != QL1S("data")) { //qDebug() << "Blocked: " << reqUrl; return new KDEPrivate::AccessManagerReply(op, req, QNetworkReply::ContentAccessDenied, i18n("Blocked request."), this); } // Check if the internal ignore content disposition header is set. const bool ignoreContentDisposition = req.hasRawHeader("x-kdewebkit-ignore-disposition"); // Retrieve the KIO meta data... KIO::MetaData metaData; d->setMetaDataForRequest(req, metaData); KIO::SimpleJob *kioJob = 0; switch (op) { case HeadOperation: { //qDebug() << "HeadOperation:" << reqUrl; kioJob = KIO::mimetype(reqUrl, KIO::HideProgressInfo); break; } case GetOperation: { //qDebug() << "GetOperation:" << reqUrl; if (!reqUrl.path().isEmpty() || reqUrl.host().isEmpty()) { kioJob = KIO::storedGet(reqUrl, KIO::NoReload, KIO::HideProgressInfo); } else { kioJob = KIO::stat(reqUrl, KIO::HideProgressInfo); } // WORKAROUND: Avoid the brain damaged stuff QtWebKit does when a POST // operation is redirected! See BR# 268694. metaData.remove(QStringLiteral("content-type")); // Remove the content-type from a GET/HEAD request! break; } case PutOperation: { //qDebug() << "PutOperation:" << reqUrl; if (outgoingData) { Q_ASSERT(outgoingData->isReadable()); StoredTransferJob* storedJob = KIO::storedPut(outgoingData, reqUrl, -1, KIO::HideProgressInfo); storedJob->setAsyncDataEnabled(outgoingData->isSequential()); QVariant len = req.header(QNetworkRequest::ContentLengthHeader); if (len.isValid()) { storedJob->setTotalSize(len.toInt()); } kioJob = storedJob; } else { kioJob = KIO::put(reqUrl, -1, KIO::HideProgressInfo); } break; } case PostOperation: { kioJob = KIO::storedHttpPost(outgoingData, reqUrl, sizeFromRequest(req), KIO::HideProgressInfo); if (!metaData.contains(QStringLiteral("content-type"))) { const QVariant header = req.header(QNetworkRequest::ContentTypeHeader); if (header.isValid()) { metaData.insert(QStringLiteral("content-type"), (QStringLiteral("Content-Type: ") + header.toString())); } else { metaData.insert(QStringLiteral("content-type"), QStringLiteral("Content-Type: application/x-www-form-urlencoded")); } } break; } case DeleteOperation: { //qDebug() << "DeleteOperation:" << reqUrl; kioJob = KIO::http_delete(reqUrl, KIO::HideProgressInfo); break; } case CustomOperation: { const QByteArray &method = req.attribute(QNetworkRequest::CustomVerbAttribute).toByteArray(); //qDebug() << "CustomOperation:" << reqUrl << "method:" << method << "outgoing data:" << outgoingData; if (method.isEmpty()) { return new KDEPrivate::AccessManagerReply(op, req, QNetworkReply::ProtocolUnknownError, i18n("Unknown HTTP verb."), this); } const qint64 size = sizeFromRequest(req); if (size > 0) { kioJob = KIO::http_post(reqUrl, outgoingData, size, KIO::HideProgressInfo); } else { kioJob = KIO::get(reqUrl, KIO::NoReload, KIO::HideProgressInfo); } metaData.insert(QStringLiteral("CustomHTTPMethod"), method); break; } default: { qCWarning(KIO_WIDGETS) << "Unsupported KIO operation requested! Defering to QNetworkAccessManager..."; return QNetworkAccessManager::createRequest(op, req, outgoingData); } } // Set the job priority switch (req.priority()) { case QNetworkRequest::HighPriority: KIO::Scheduler::setJobPriority(kioJob, -5); break; case QNetworkRequest::LowPriority: KIO::Scheduler::setJobPriority(kioJob, 5); break; default: break; } KDEPrivate::AccessManagerReply *reply; /* NOTE: Here we attempt to handle synchronous XHR requests. Unfortunately, due to the fact that QNAM is both synchronous and multi-thread while KIO is completely the opposite (asynchronous and not thread safe), the code below might cause crashes like the one reported in bug# 287778 (nested event loops are inherently dangerous). Unfortunately, all attempts to address the crash has so far failed due to the many regressions they caused, e.g. bug# 231932 and 297954. Hence, until a solution is found, we have to live with the side effects of creating nested event loops. */ if (req.attribute(gSynchronousNetworkRequestAttribute).toBool()) { KJobWidgets::setWindow(kioJob, d->window); kioJob->setRedirectionHandlingEnabled(true); if (kioJob->exec()) { QByteArray data; if (StoredTransferJob *storedJob = qobject_cast< KIO::StoredTransferJob * >(kioJob)) { data = storedJob->data(); } reply = new KDEPrivate::AccessManagerReply(op, req, data, kioJob->url(), kioJob->metaData(), this); //qDebug() << "Synchronous XHR:" << reply << reqUrl; } else { qCWarning(KIO_WIDGETS) << "Failed to create a synchronous XHR for" << reqUrl; qCWarning(KIO_WIDGETS) << "REASON:" << kioJob->errorString(); reply = new KDEPrivate::AccessManagerReply(op, req, QNetworkReply::UnknownNetworkError, kioJob->errorText(), this); } } else { // Set the window on the KIO ui delegate if (d->window) { KJobWidgets::setWindow(kioJob, d->window); } // Disable internal automatic redirection handling kioJob->setRedirectionHandlingEnabled(false); // Set the job priority switch (req.priority()) { case QNetworkRequest::HighPriority: KIO::Scheduler::setJobPriority(kioJob, -5); break; case QNetworkRequest::LowPriority: KIO::Scheduler::setJobPriority(kioJob, 5); break; default: break; } // Set the meta data for this job... kioJob->setMetaData(metaData); // Create the reply... reply = new KDEPrivate::AccessManagerReply(op, req, kioJob, d->emitReadyReadOnMetaDataChange, this); //qDebug() << reply << reqUrl; } if (ignoreContentDisposition && reply) { //qDebug() << "Content-Disposition WILL BE IGNORED!"; reply->setIgnoreContentDisposition(ignoreContentDisposition); } return reply; }
// return true if jsonp applied bool tryApplyJsonp(const DomainMap::JsonpConfig &config, bool *ok, QString *errorMessage) { *ok = true; // must be a GET if(requestData.method != "GET") return false; QString callbackParam = QString::fromUtf8(config.callbackParam); if(callbackParam.isEmpty()) callbackParam = "callback"; QString bodyParam; if(!config.bodyParam.isEmpty()) bodyParam = QString::fromUtf8(config.bodyParam); QUrl uri = requestData.uri; QUrlQuery query(uri); // two ways to activate JSON-P: // 1) callback param present // 2) default callback specified in configuration and body param present if(!query.hasQueryItem(callbackParam) && (config.defaultCallback.isEmpty() || bodyParam.isEmpty() || !query.hasQueryItem(bodyParam))) { return false; } QByteArray callback; if(query.hasQueryItem(callbackParam)) { callback = parsePercentEncoding(query.queryItemValue(callbackParam, QUrl::FullyEncoded).toUtf8()); if(callback.isEmpty()) { log_debug("requestsession: id=%s invalid callback parameter, rejecting", rid.second.data()); *ok = false; *errorMessage = "Invalid callback parameter."; return false; } query.removeAllQueryItems(callbackParam); } else callback = config.defaultCallback; QString method; if(query.hasQueryItem("_method")) { method = QString::fromLatin1(parsePercentEncoding(query.queryItemValue("_method", QUrl::FullyEncoded).toUtf8())); if(!validMethod(method)) { log_debug("requestsession: id=%s invalid _method parameter, rejecting", rid.second.data()); *ok = false; *errorMessage = "Invalid _method parameter."; return false; } query.removeAllQueryItems("_method"); } HttpHeaders headers; if(query.hasQueryItem("_headers")) { QJsonParseError e; QJsonDocument doc = QJsonDocument::fromJson(parsePercentEncoding(query.queryItemValue("_headers", QUrl::FullyEncoded).toUtf8()), &e); if(e.error != QJsonParseError::NoError || !doc.isObject()) { log_debug("requestsession: id=%s invalid _headers parameter, rejecting", rid.second.data()); *ok = false; *errorMessage = "Invalid _headers parameter."; return false; } QVariantMap headersMap = doc.object().toVariantMap(); QMapIterator<QString, QVariant> vit(headersMap); while(vit.hasNext()) { vit.next(); if(vit.value().type() != QVariant::String) { log_debug("requestsession: id=%s invalid _headers parameter, rejecting", rid.second.data()); *ok = false; *errorMessage = "Invalid _headers parameter."; return false; } QByteArray key = vit.key().toUtf8(); // ignore some headers that we explicitly set later on if(qstricmp(key.data(), "host") == 0) continue; if(qstricmp(key.data(), "accept") == 0) continue; headers += HttpHeader(key, vit.value().toString().toUtf8()); } query.removeAllQueryItems("_headers"); } QByteArray body; if(!bodyParam.isEmpty()) { if(query.hasQueryItem(bodyParam)) { body = parsePercentEncoding(query.queryItemValue(bodyParam, QUrl::FullyEncoded).toUtf8()); if(body.isNull()) { log_debug("requestsession: id=%s invalid body parameter, rejecting", rid.second.data()); *ok = false; *errorMessage = "Invalid body parameter."; return false; } headers.removeAll("Content-Type"); headers += HttpHeader("Content-Type", "application/json"); query.removeAllQueryItems(bodyParam); } } else { if(query.hasQueryItem("_body")) { body = parsePercentEncoding(query.queryItemValue("_body").toUtf8()); if(body.isNull()) { log_debug("requestsession: id=%s invalid _body parameter, rejecting", rid.second.data()); *ok = false; *errorMessage = "Invalid _body parameter."; return false; } query.removeAllQueryItems("_body"); } } uri.setQuery(query); // if we have no query items anymore, strip the '?' if(query.isEmpty()) { QByteArray tmp = uri.toEncoded(); if(tmp.length() > 0 && tmp[tmp.length() - 1] == '?') { tmp.truncate(tmp.length() - 1); uri = QUrl::fromEncoded(tmp, QUrl::StrictMode); } } if(method.isEmpty()) method = config.defaultMethod; requestData.method = method; requestData.uri = uri; headers += HttpHeader("Host", uri.host().toUtf8()); headers += HttpHeader("Accept", "*/*"); // carry over the rest of the headers foreach(const HttpHeader &h, requestData.headers) { if(qstricmp(h.first.data(), "host") == 0) continue; if(qstricmp(h.first.data(), "accept") == 0) continue; headers += h; } requestData.headers = headers; in += body; jsonpCallback = callback; jsonpExtendedResponse = (config.mode == DomainMap::JsonpConfig::Extended); return true; }
/** * \brief Static function for performing a http post request to a url. * * This is a synchronous function, it will block according to the vars. */ QString HttpComms::postHttp( QUrl &url, QHttpRequestHeader *pAddlHdr, QIODevice *pData, int timeoutMS, int maxRetries, int maxRedirects, bool allowGzip, Credentials *webCred, bool isInQtEventThread, QString userAgent) { int redirectCount = 0; int timeoutCount = 0; QString res; HttpComms *httpGrabber = NULL; QString hostname; QHttpRequestHeader header("POST", url.path() + url.encodedQuery()); // Add main header values header.setValue("Host", url.host()); if (userAgent.toLower() == "<default>") { userAgent = "Mozilla/9.876 (X11; U; Linux 2.2.12-20 i686, en) " "Gecko/25250101 Netscape/5.432b1"; } if (!userAgent.isEmpty()) header.setValue("User-Agent", userAgent); if (allowGzip) header.setValue( "Accept-Encoding", "gzip"); // Merge any Additional Headers passed by caller. if (pAddlHdr) { QList< QPair< QString, QString > > values = pAddlHdr->values(); for (QList< QPair< QString, QString > >::iterator it = values.begin(); it != values.end(); ++it ) { header.setValue( (*it).first, (*it).second ); } } while (1) { if (hostname.isEmpty()) hostname = url.host(); // hold onto original host if (url.host().isEmpty()) // can occur on redirects to partial paths url.setHost(hostname); VERBOSE(VB_NETWORK, QString("postHttp: grabbing: %1").arg(url.toString())); delete httpGrabber; httpGrabber = new HttpComms; if (webCred) httpGrabber->setCredentials(*webCred, CRED_WEB); httpGrabber->request(url, header, timeoutMS, pData ); while (!httpGrabber->isDone()) { if (isInQtEventThread) qApp->processEvents(); usleep(10000); } // Handle timeout if (httpGrabber->isTimedout()) { VERBOSE(VB_NETWORK, QString("timeout for url: %1").arg(url.toString())); // Increment the counter and check we're not over the limit if (timeoutCount++ >= maxRetries) { VERBOSE(VB_IMPORTANT, QString("Failed to contact server for url: %1").arg(url.toString())); break; } // Try again VERBOSE(VB_NETWORK, QString("Attempt # %1/%2 for url: %3") .arg(timeoutCount + 1) .arg(maxRetries) .arg(url.toString())); continue; } // Check for redirection if (!httpGrabber->getRedirectedURL().isEmpty()) { VERBOSE(VB_NETWORK, QString("Redirection: %1, count: %2, max: %3") .arg(httpGrabber->getRedirectedURL()) .arg(redirectCount) .arg(maxRedirects)); // Increment the counter and check we're not over the limit if (redirectCount++ >= maxRedirects) { VERBOSE(VB_IMPORTANT, QString("Maximum redirections reached for url: %1").arg(url.toString())); break; } url = QUrl( httpGrabber->getRedirectedURL() ); // Try again timeoutCount = 0; continue; } res = httpGrabber->getData(); break; } delete httpGrabber; VERBOSE(VB_NETWORK, QString("Got %1 bytes from url: '%2'") .arg(res.length()) .arg(url.toString())); VERBOSE(VB_NETWORK, res); return res; }
int UPNPSubscription::SendSubscribeRequest(const QString &callback, const QString &usn, const QUrl &url, const QString &path, const QString &uuidin, QString &uuidout) { QString host = url.host(); int port = url.port(); QByteArray sub; QTextStream data(&sub); data.setCodec(QTextCodec::codecForName("UTF-8")); // N.B. Play On needs an extra space between SUBSCRIBE and path... data << QString("SUBSCRIBE %1 HTTP/1.1\r\n").arg(path); data << QString("HOST: %1:%2\r\n").arg(host).arg(QString::number(port)); if (uuidin.isEmpty()) // new subscription { data << QString("CALLBACK: <%1%2>\r\n") .arg(callback).arg(usn); data << "NT: upnp:event\r\n"; } else // renewal data << QString("SID: uuid:%1\r\n").arg(uuidin); data << QString("TIMEOUT: Second-%1\r\n").arg(SUBSCRIPTION_TIME); data << "\r\n"; data.flush(); LOG(VB_UPNP, LOG_DEBUG, LOC + "\n\n" + sub); MSocketDevice *sockdev = new MSocketDevice(MSocketDevice::Stream); BufferedSocketDevice *sock = new BufferedSocketDevice(sockdev); sockdev->setBlocking(true); QString uuid; QString timeout; uint result = 0; if (sock->Connect(QHostAddress(host), port)) { if (sock->WriteBlockDirect(sub.constData(), sub.size()) != -1) { bool ok = false; QString line = sock->ReadLine(MAX_WAIT); while (!line.isEmpty()) { LOG(VB_UPNP, LOG_DEBUG, LOC + line); if (line.contains("HTTP/1.1 200 OK", Qt::CaseInsensitive)) ok = true; if (line.startsWith("SID:", Qt::CaseInsensitive)) uuid = line.mid(4).trimmed().mid(5).trimmed(); if (line.startsWith("TIMEOUT:", Qt::CaseInsensitive)) timeout = line.mid(8).trimmed().mid(7).trimmed(); if (ok && !uuid.isEmpty() && !timeout.isEmpty()) break; line = sock->ReadLine(MAX_WAIT); } if (ok && !uuid.isEmpty() && !timeout.isEmpty()) { uuidout = uuid; result = timeout.toUInt(); } else { LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to subscribe to %1").arg(usn)); } } else { LOG(VB_GENERAL, LOG_ERR, LOC + QString("Socket write error for %1:%2") .arg(host).arg(port)); } sock->Close(); } else { LOG(VB_GENERAL, LOG_ERR, LOC + QString("Failed to open socket for %1:%2") .arg(host).arg(port)); } delete sock; delete sockdev; return result; }
bool WebPage::acceptNavigationRequest(QWebFrame *frame, const QNetworkRequest &request, NavigationType type, const QByteArray& postData) { QString scheme = request.url().scheme(); if (scheme == QLatin1String("mailto") || scheme == QLatin1String("ftp")) { // QDesktopServices::openUrl(request.url()); return false; } if (IS_CRAWLER) // when crawler, we run the webapplication is the same process // make it easier for replay return QWebPage::acceptNavigationRequest(frame, request, type, postData); WebView::OpenType t = WebView::NewWebApp; WebView* view = qobject_cast<WebView*>(parent()); Q_ASSERT(view != 0); // little trick to avoid double post if (m_posted) { m_posted = false; } else if (postData.size() > 0) { m_posted = true; } // ctrl open in new tab // ctrl-shift open in new tab and select // ctrl-alt open in new window if (type == QWebPage::NavigationTypeLinkClicked && (m_keyboardModifiers & Qt::ControlModifier || m_pressedButtons == Qt::MidButton)) { bool newWindow = (m_keyboardModifiers & Qt::AltModifier); if (newWindow) { t = WebView::NewWindow; } else { bool selectNewTab = (m_keyboardModifiers & Qt::ShiftModifier); if (selectNewTab) t = WebView::NewTabSelect; else t = WebView::NewTabNoSelect; } // check and load view->loadPolicy(request, postData, t); m_keyboardModifiers = Qt::NoModifier; m_pressedButtons = Qt::NoButton; return false; } //qDebug() << "navigation request url: " << request.url(); QUrl::FormattingOptions format = QUrl::RemoveQuery | QUrl::RemoveFragment | QUrl::StripTrailingSlash; if (frame == mainFrame()) { //qDebug() << "mainFrame navigation request to " << request.url(); m_loadingUrl = request.url(); QUrl current = view->url(); bool sop = (m_loadingUrl.scheme().toLower() == current.scheme().toLower()) && (m_loadingUrl.host().toLower() == current.host().toLower()) && (m_loadingUrl.port() == current.port()); //QUrl::FormattingOptions format = QUrl::RemoveQuery | QUrl::RemoveFragment | QUrl::StripTrailingSlash; if (!sop //(view->loaded() || type == QWebPage::NavigationTypeLinkClicked) && !sop /*(m_loadingUrl.toString(format) != current.toString(format)) */) { t = WebView::NewWebApp; // check and load if (m_posted) view->loadPolicy(request, postData, t); else view->loadPolicy(request, QByteArray(), t); return false; } else { emit loadingUrl(m_loadingUrl); if ((type != NavigationTypeOther) && (type != NavigationTypeReload) && (type != NavigationTypeBackOrForward) || (view->m_userAction)) { QVariant var(m_loadingUrl.toString()); OPNET::OpNetwork::instance()->sendUIMsg(MSG_addHistoryItem, getByteArray(var)); view->m_userAction = false; } if (type == NavigationTypeBackOrForward) { //QVariant var(m_back); //OPNET::OpNetwork::instance()->sendUIMsg(MSG_navBackOrForward, getByteArray(var)); m_back = false; } } } else if(frame == 0) { t = WebView::NewTabNoSelect; // check and load view->loadPolicy(request, postData, t); return false; } // same-origin and "about:blank" navigation requests fall-through to acceptNavigationRequest else if( m_iframeHandled && (request.url().toString(format | QUrl::RemovePath) != view->url().toString(format | QUrl::RemovePath) && request.url().toString() != "about:blank")) { //qDebug() << "navigation request for sub-frame \"" << frame->frameName() << "\" to " << request.url() << " type: " << type; t = WebView::NewSubFrame; OPNET::OpNetwork::instance()->sendSysCall(MSG_EMBED_FRAME, 0, frame->frameName().toAscii()); view->loadPolicy(request, postData, t); return false; } return QWebPage::acceptNavigationRequest(frame, request, type, postData); }
ServerItem *ServerItem::fromMimeData(const QMimeData *mime, QWidget *p) { if (mime->hasFormat(QLatin1String("OriginatedInMumble"))) return NULL; QUrl url; #if QT_VERSION >= 0x050000 QUrlQuery query(url); #endif if (mime->hasUrls() && ! mime->urls().isEmpty()) url = mime->urls().at(0); else if (mime->hasText()) url = QUrl::fromEncoded(mime->text().toUtf8()); QString qsFile = url.toLocalFile(); if (! qsFile.isEmpty()) { QFile f(qsFile); // Make sure we don't accidently read something big the user // happened to have in his clipboard. We only want to look // at small link files. if (f.open(QIODevice::ReadOnly) && f.size() < 10240) { QByteArray qba = f.readAll(); f.close(); url = QUrl::fromEncoded(qba, QUrl::StrictMode); if (! url.isValid()) { QSettings qs(qsFile, QSettings::IniFormat); url = QUrl::fromEncoded(qs.value(QLatin1String("InternetShortcut/URL")).toByteArray(), QUrl::StrictMode); } } } if (! url.isValid() || (url.scheme() != QLatin1String("mumble"))) return NULL; if (url.userName().isEmpty()) { if (g.s.qsUsername.isEmpty()) { bool ok; QString defUserName = QInputDialog::getText(p, ConnectDialog::tr("Adding host %1").arg(url.host()), ConnectDialog::tr("Enter username"), QLineEdit::Normal, g.s.qsUsername, &ok).trimmed(); if (! ok) return NULL; if (defUserName.isEmpty()) return NULL; g.s.qsUsername = defUserName; } url.setUserName(g.s.qsUsername); } #if QT_VERSION >= 0x050000 if (! query.hasQueryItem(QLatin1String("title"))) query.addQueryItem(QLatin1String("title"), url.host()); ServerItem *si = new ServerItem(query.queryItemValue(QLatin1String("title")), url.host(), static_cast<unsigned short>(url.port(DEFAULT_MUMBLE_PORT)), url.userName(), url.password()); if (query.hasQueryItem(QLatin1String("url"))) si->qsUrl = query.queryItemValue(QLatin1String("url")); #else if (! url.hasQueryItem(QLatin1String("title"))) url.addQueryItem(QLatin1String("title"), url.host()); ServerItem *si = new ServerItem(url.queryItemValue(QLatin1String("title")), url.host(), static_cast<unsigned short>(url.port(DEFAULT_MUMBLE_PORT)), url.userName(), url.password()); if (url.hasQueryItem(QLatin1String("url"))) si->qsUrl = url.queryItemValue(QLatin1String("url")); #endif return si; }
//! [2] bool checkUrl(const QUrl &url) { if (!url.isValid()) { qDebug(QString("Invalid URL: %1").arg(url.toString())); return false; } return true; } //! [2] //! [3] QFtp ftp; ftp.connectToHost(url.host(), url.port(21)); //! [3] //! [4] http://www.example.com/cgi-bin/drawgraph.cgi?type-pie/color-green //! [4] //! [5] QUrl baseUrl("http://qt.nokia.com/support"); QUrl relativeUrl("../products/solutions"); qDebug(baseUrl.resolved(relativeUrl).toString()); // prints "http://qt.nokia.com/products/solutions" //! [5]
QString SettingsManager::getHost(const QUrl &url) { return (url.isLocalFile() ? QLatin1String("localhost") : url.host()); }
void AdBlockIcon::createMenu(QMenu* menu) { if (!menu) { menu = qobject_cast<QMenu*>(sender()); if (!menu) { return; } } menu->clear(); AdBlockManager* manager = AdBlockManager::instance(); AdBlockCustomList* customList = manager->customList(); WebPage* page = qobject_cast<WebPage*>(m_window->currentNewsTab->webView_->page()); const QUrl pageUrl = page->mainFrame()->url(); menu->addAction(tr("Show AdBlock &Settings"), manager, SLOT(showDialog())); menu->addSeparator(); if (!pageUrl.host().isEmpty() && m_enabled && manager->canRunOnScheme(pageUrl.scheme())) { const QString host = pageUrl.host().contains(QLatin1String("www.")) ? pageUrl.host().mid(4) : pageUrl.host(); const QString hostFilter = QString("@@||%1^$document").arg(host); const QString pageFilter = QString("@@|%1|$document").arg(pageUrl.toString()); QAction* act = menu->addAction(tr("Disable on %1").arg(host)); act->setCheckable(true); act->setChecked(customList->containsFilter(hostFilter)); act->setData(hostFilter); connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter())); act = menu->addAction(tr("Disable only on this page")); act->setCheckable(true); act->setChecked(customList->containsFilter(pageFilter)); act->setData(pageFilter); connect(act, SIGNAL(triggered()), this, SLOT(toggleCustomFilter())); menu->addSeparator(); } if (!m_blockedPopups.isEmpty()) { menu->addAction(tr("Blocked Popup Windows"))->setEnabled(false); for (int i = 0; i < m_blockedPopups.count(); i++) { const QPair<AdBlockRule, QUrl> &pair = m_blockedPopups.at(i); QString address = pair.second.toString().right(55); QString actionText = tr("%1 with (%2)").arg(address, pair.first.filter()).replace(QLatin1Char('&'), QLatin1String("&&")); QAction* action = menu->addAction(actionText, manager, SLOT(showRule())); action->setData(QVariant::fromValue((void*)&pair.first)); } } menu->addSeparator(); QVector<WebPage::AdBlockedEntry> entries = page->adBlockedEntries(); if (entries.isEmpty()) { menu->addAction(tr("No content blocked"))->setEnabled(false); } else { menu->addAction(tr("Blocked URL (AdBlock Rule) - click to edit rule"))->setEnabled(false); foreach (const WebPage::AdBlockedEntry &entry, entries) { QString address = entry.url.toString().right(55); QString actionText = tr("%1 with (%2)").arg(address, entry.rule->filter()).replace(QLatin1Char('&'), QLatin1String("&&")); QAction* action = menu->addAction(actionText, manager, SLOT(showRule())); action->setData(QVariant::fromValue((void*)entry.rule)); } }
void DkUpnpControlPoint::rootDeviceOnline(Herqq::Upnp::HClientDevice* clientDevice) { qDebug() << "rootDeviceOnline:" << clientDevice->info().deviceType().toString(); qDebug() << "manufacturer:" << clientDevice->info().manufacturer(); if(clientDevice->info().manufacturer()=="nomacs") { qDebug() << "nomacs found!!!"; //qDebug() << "description:" << clientDevice->description(); QList<QUrl> locations = clientDevice->locations(Herqq::Upnp::AbsoluteUrl); QUrl url; for (QList<QUrl>::iterator itr = locations.begin(); itr != locations.end(); itr++) { url = *itr; } if(url.isEmpty()) { qDebug() << "url is empty, aborting"; return; } QHostAddress host = QHostAddress(url.host()); if(isLocalHostAddress(host)) { qDebug() << "is local address ... aborting"; return; } Herqq::Upnp::HClientService* service = clientDevice->serviceById(Herqq::Upnp::HServiceId("urn:nomacs-org:service:nomacsService")); if (!service) { qDebug() << "nomacs service is empty ... aborting"; return; } Herqq::Upnp::HClientActions actions = service->actions(); qDebug() << "service:" << service->description(); Herqq::Upnp::HActionArguments aas; Herqq::Upnp::HClientActionOp cao; // ask for LAN server if (!actions.value("getTCPServerURL")) { qDebug() << "action.value(getTCPServerURL) is null ... aborting"; return; } connect(actions.value("getTCPServerURL"), SIGNAL(invokeComplete(Herqq::Upnp::HClientAction*, const Herqq::Upnp::HClientActionOp&)), this, SLOT(invokeComplete(Herqq::Upnp::HClientAction*, const Herqq::Upnp::HClientActionOp&))); cao = actions.value("getTCPServerURL")->beginInvoke(aas); // ask for RC server if (!actions.value("getWhiteListServerURL")) { qDebug() << "action.value(getWhiteListServerURL) is null ... aborting"; return; } connect(actions.value("getWhiteListServerURL"), SIGNAL(invokeComplete(Herqq::Upnp::HClientAction*, const Herqq::Upnp::HClientActionOp&)), this, SLOT(invokeComplete(Herqq::Upnp::HClientAction*, const Herqq::Upnp::HClientActionOp&))); cao = actions.value("getWhiteListServerURL")->beginInvoke(aas); connect(service->stateVariables().value("tcpServerPort"), SIGNAL(valueChanged(const Herqq::Upnp::HClientStateVariable*, const Herqq::Upnp::HStateVariableEvent&)), this, SLOT(tcpValueChanged(const Herqq::Upnp::HClientStateVariable*, const Herqq::Upnp::HStateVariableEvent&))); connect(service->stateVariables().value("whiteListServerPort"), SIGNAL(valueChanged(const Herqq::Upnp::HClientStateVariable*, const Herqq::Upnp::HStateVariableEvent&)), this, SLOT(wlValueChanged(const Herqq::Upnp::HClientStateVariable*, const Herqq::Upnp::HStateVariableEvent&))); //Herqq::Upnp::HClientServices services = clientDevice->services(); //for(int i = 0; i < (int) services.size(); i++) { // Herqq::Upnp::HClientService* service = services.at(i); // qDebug() << "service " << i << ":" << service->description() ; // Herqq::Upnp::HClientActions actions = service->actions(); // QList<QString> keys = actions.keys(); // for (int j = 0; j < (int) keys.size(); j++) { // qDebug() << "action " << j << ": " << keys.at(j); // } // Herqq::Upnp::HActionArgument aa("newTargetValue", Herqq::Upnp::HStateVariableInfo("Target", Herqq::Upnp::HUpnpDataTypes::boolean, Herqq::Upnp::HInclusionRequirement::InclusionOptional)); // aa.setValue(true); // Herqq::Upnp::HActionArguments aas; // aas.append(aa); // actions.value("SetTarget")->beginInvoke(aas); //} }
void QNetworkAccessFileBackend::open() { QUrl url = this->url(); if (url.host() == QLatin1String("localhost")) url.setHost(QString()); #if !defined(Q_OS_WIN) // do not allow UNC paths on Unix if (!url.host().isEmpty()) { // we handle only local files error(QNetworkReply::ProtocolInvalidOperationError, QCoreApplication::translate("QNetworkAccessFileBackend", "Request for opening non-local file %1").arg(url.toString())); finished(); return; } #endif // !defined(Q_OS_WIN) if (url.path().isEmpty()) url.setPath(QLatin1String("/")); setUrl(url); QString fileName = url.toLocalFile(); if (fileName.isEmpty()) { if (url.scheme() == QLatin1String("qrc")) fileName = QLatin1Char(':') + url.path(); else fileName = url.toString(QUrl::RemoveAuthority | QUrl::RemoveFragment | QUrl::RemoveQuery); } file.setFileName(fileName); if (operation() == QNetworkAccessManager::GetOperation) { if (!loadFileInfo()) return; } QIODevice::OpenMode mode; switch (operation()) { case QNetworkAccessManager::GetOperation: mode = QIODevice::ReadOnly; break; case QNetworkAccessManager::PutOperation: mode = QIODevice::WriteOnly | QIODevice::Truncate; uploadByteDevice = createUploadByteDevice(); QObject::connect(uploadByteDevice, SIGNAL(readyRead()), this, SLOT(uploadReadyReadSlot())); QMetaObject::invokeMethod(this, "uploadReadyReadSlot", Qt::QueuedConnection); break; default: Q_ASSERT_X(false, "QNetworkAccessFileBackend::open", "Got a request operation I cannot handle!!"); return; } mode |= QIODevice::Unbuffered; bool opened = file.open(mode); // could we open the file? if (!opened) { QString msg = QCoreApplication::translate("QNetworkAccessFileBackend", "Error opening %1: %2") .arg(this->url().toString(), file.errorString()); // why couldn't we open the file? // if we're opening for reading, either it doesn't exist, or it's access denied // if we're opening for writing, not existing means it's access denied too if (file.exists() || operation() == QNetworkAccessManager::PutOperation) error(QNetworkReply::ContentAccessDenied, msg); else error(QNetworkReply::ContentNotFoundError, msg); finished(); } }
void SettingsManager::removeOverride(const QUrl &url) { QSettings(m_overridePath, QSettings::IniFormat).remove(url.isLocalFile() ? QLatin1String("localhost") : url.host()); }
bool SettingsManager::hasOverride(const QUrl &url, const QString &key) { if (key.isEmpty()) { return QSettings(m_overridePath, QSettings::IniFormat).childGroups().contains(url.isLocalFile() ? QLatin1String("localhost") : url.host()); } else { return QSettings(m_overridePath, QSettings::IniFormat).contains((url.isLocalFile() ? QLatin1String("localhost") : url.host()) + QLatin1Char('/') + key); } }
void QmitkMitkWorkbenchIntroPart::DelegateMeTo(const QUrl& showMeNext) { QString scheme = showMeNext.scheme(); #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0) QByteArray urlHostname = showMeNext.encodedHost(); QByteArray urlPath = showMeNext.encodedPath(); QByteArray dataset = showMeNext.encodedQueryItemValue("dataset"); QByteArray clear = showMeNext.encodedQueryItemValue("clear"); #else QByteArray urlHostname = QUrl::toAce(showMeNext.host()); QByteArray urlPath = showMeNext.path().toLatin1(); QUrlQuery query(showMeNext); QByteArray dataset = query.queryItemValue("dataset").toLatin1(); QByteArray clear = query.queryItemValue("clear").toLatin1();//showMeNext.encodedQueryItemValue("clear"); #endif if (scheme.isEmpty()) MITK_INFO << " empty scheme of the to be delegated link" ; // if the scheme is set to mitk, it is to be tested which action should be applied if (scheme.contains(QString("mitk")) ) { if(urlPath.isEmpty() ) MITK_INFO << " mitk path is empty " ; // searching for the perspective keyword within the host name if(urlHostname.contains(QByteArray("perspectives")) ) { // the simplified method removes every whitespace // ( whitespace means any character for which the standard C++ isspace() method returns true) urlPath = urlPath.simplified(); QString tmpPerspectiveId(urlPath.data()); tmpPerspectiveId.replace(QString("/"), QString("") ); QString perspectiveId = tmpPerspectiveId; // is working fine as long as the perspective id is valid, if not the application crashes GetIntroSite()->GetWorkbenchWindow()->GetWorkbench()->ShowPerspective(perspectiveId, GetIntroSite()->GetWorkbenchWindow() ); // search the Workbench for opened StdMultiWidgets to ensure the focus does not stay on the welcome screen and is switched to // a render window editor if one available ctkPluginContext* context = QmitkExtApplicationPlugin::GetDefault()->GetPluginContext(); mitk::IDataStorageService* service = nullptr; ctkServiceReference serviceRef = context->getServiceReference<mitk::IDataStorageService>(); if (serviceRef) service = context->getService<mitk::IDataStorageService>(serviceRef); if (service) { berry::IEditorInput::Pointer editorInput(new mitk::DataStorageEditorInput( service->GetActiveDataStorage() )); // search for opened StdMultiWidgetEditors berry::IEditorPart::Pointer editorPart = GetIntroSite()->GetPage()->FindEditor( editorInput ); // if an StdMultiWidgetEditor open was found, give focus to it if(editorPart) { GetIntroSite()->GetPage()->Activate( editorPart ); } } } } // if the scheme is set to http, by default no action is performed, if an external webpage needs to be // shown it should be implemented below else if (scheme.contains(QString("http")) ) { QDesktopServices::openUrl(showMeNext); // m_view->load( ) ; } else if(scheme.contains("qrc")) { m_view->load(showMeNext); } }