void Fetcher::fetchNextShader() { #ifdef SHADERTOY_API_KEY while (!shadersToFetch.empty() && currentNetworkRequests <= 4) { QString nextShaderId = shadersToFetch.front(); shadersToFetch.pop_front(); QString shaderFile = CONFIG_DIR.absoluteFilePath("shadertoy/" + nextShaderId + ".json"); QString shaderPreviewFile = CONFIG_DIR.absoluteFilePath("shadertoy/" + nextShaderId + ".jpg"); if (QFile(shaderFile).exists() && QFile(shaderPreviewFile).exists()) { continue; } if (!QFile(shaderFile).exists()) { qDebug() << "Fetching shader " << nextShaderId; QUrl url(SHADERTOY_API_URL + QString().sprintf("/%s?key=%s", nextShaderId.toLocal8Bit().constData(), SHADERTOY_API_KEY)); fetchUrl(url, [&, shaderFile](const QByteArray & replyBuffer) { QFile outputFile(shaderFile); outputFile.open(QIODevice::WriteOnly); outputFile.write(replyBuffer); outputFile.close(); }); } if (!QFile(shaderPreviewFile).exists()) { fetchFile(QUrl(SHADERTOY_MEDIA_URL + nextShaderId + ".jpg"), shaderPreviewFile); } } if (shadersToFetch.isEmpty()) { timer.stop(); return; } #endif }
void Fetcher::fetchFile(const QUrl & url, const QString & path) { fetchUrl(url, [&, path](const QByteArray & replyBuffer) { QFile outputFile(path); outputFile.open(QIODevice::WriteOnly); outputFile.write(replyBuffer); outputFile.close(); }); }
HttpContainer::HttpContainer(const QUrl &url, QObject *parent) : Plasma::DataContainer(parent), m_url(url) { // Since we are grabbing data over the network, we request a // backing store. This way, if the network is down or on first start // before we get our first reply back, if this source was previously // available, we'll still have our data. This is a very nice "freebie" // DataContainer gives us. setStorageEnabled(true); // Now, start an initial fetch. fetchUrl(false); }
void Fetcher::fetchNetworkShaders() { #ifdef SHADERTOY_API_KEY qDebug() << "Fetching shader list"; QUrl url(SHADERTOY_API_URL + QString().sprintf("?key=%s", SHADERTOY_API_KEY)); fetchUrl(url, [&](const QByteArray & replyBuffer) { QJsonDocument jsonResponse = QJsonDocument::fromJson(replyBuffer); QJsonObject jsonObject = jsonResponse.object(); QJsonArray shaders = jsonObject["Results"].toArray(); for (int i = 0; i < shaders.count(); ++i) { QString shaderId = shaders.at(i).toString(); shadersToFetch.push_back(shaderId); } timer.start(1000); }); #endif }
DownloadFaviconReply::DownloadFaviconReply( const DownloadFaviconRequest &request, QNetworkAccessManager *network, QObject *parent) : AbstractDownloadReply(network, parent) { m_request = request; QUrl favIconUrl = request.url(); favIconUrl.setPath("/favicon.ico"); favIconUrl.setFragment(QString()); #if QT_VERSION >= 0x050000 favIconUrl.setQuery(QUrlQuery()); #else favIconUrl.setQueryItems(QList<QPair<QString, QString> >()); #endif fetchUrl(favIconUrl, request.maxRetryCount(), request.maxRedirectCount(), true); }
void PaymentServer::handleURIOrFile(const QString& s) { if (saveURIs) { savedPaymentRequests.append(s); return; } if (s.startsWith(BITCOIN_IPC_PREFIX, Qt::CaseInsensitive)) // bitcoin: URI { #if QT_VERSION < 0x050000 QUrl uri(s); #else QUrlQuery uri((QUrl(s))); #endif if (uri.hasQueryItem("r")) // payment request URI { QByteArray temp; temp.append(uri.queryItemValue("r")); QString decoded = QUrl::fromPercentEncoding(temp); QUrl fetchUrl(decoded, QUrl::StrictMode); if (fetchUrl.isValid()) { qDebug() << "PaymentServer::handleURIOrFile: fetchRequest(" << fetchUrl << ")"; fetchRequest(fetchUrl); } else { qWarning() << "PaymentServer::handleURIOrFile: Invalid URL: " << fetchUrl; Q_EMIT message(tr("URI handling"), tr("Payment request fetch URL is invalid: %1").arg(fetchUrl.toString()), CClientUIInterface::ICON_WARNING); } return; } else // normal URI { SendCoinsRecipient recipient; if (GUIUtil::parseBitcoinURI(s, &recipient)) { if (!IsValidDestinationString(recipient.address.toStdString())) { Q_EMIT message(tr("URI handling"), tr("Invalid payment address %1").arg(recipient.address), CClientUIInterface::MSG_ERROR); } else Q_EMIT receivedPaymentRequest(recipient); } else Q_EMIT message(tr("URI handling"), tr("URI cannot be parsed! This can be caused by an invalid Bitcoin address or malformed URI parameters."), CClientUIInterface::ICON_WARNING); return; } } if (QFile::exists(s)) // payment request file { PaymentRequestPlus request; SendCoinsRecipient recipient; if (!readPaymentRequestFromFile(s, request)) { Q_EMIT message(tr("Payment request file handling"), tr("Payment request file cannot be read! This can be caused by an invalid payment request file."), CClientUIInterface::ICON_WARNING); } else if (processPaymentRequest(request, recipient)) Q_EMIT receivedPaymentRequest(recipient); return; } }
bool readSubreddits(const Options& options) { for (int i = 0; i < (int)options.subreddits.size(); i++) { if (gTotal >= options.maxToOpen) break; // open subreddit if (options.openSubreddit) openUrl("http://www.reddit.com/r/" + options.subreddits[i]); vector<HistoryItem> history; loadHistory(options.subreddits[i], history); if (options.clearHistory) history.clear(); // continue if we don't actual need to fetch the json if (options.openLink || options.openPermalink) { // fetch the url from the server string server = string("www.reddit.com"); string path = string("/r/") + options.subreddits[i] + string("/.json?limit=") + toStr(options.numToOpen); string page = fetchUrl(server, path); if (page.size() > 0) { // convert to json block_allocator allocator(1 << 10); json_value* root = toJson(page, allocator); if (root) { // find the data we want. this is gross. for (json_value* it = root->first_child; it; it = it->next_sibling) { if (!strcmp(it->name, "data") && it->type == JSON_OBJECT) { json_value* data = it; for (json_value* it = data->first_child; it; it = it->next_sibling) { if (!strcmp(it->name, "children") && it->type == JSON_ARRAY) { json_value* child = it; for (json_value* it = child->first_child; it; it = it->next_sibling) { json_value* rec = it; for (json_value* it = rec->first_child; it; it = it->next_sibling) { if (!strcmp(it->name, "data")) handleStory(options, history, it); if (gTotal >= options.maxToOpen) goto out; } } } } } } } } } out: if (history.size() || options.clearHistory) saveHistory(options.subreddits[i], history); } return true; }