Changeset View
Changeset View
Standalone View
Standalone View
src/desktopicon.cpp
Show First 20 Lines • Show All 292 Lines • ▼ Show 20 Line(s) | |||||
293 | { | 293 | { | ||
294 | if (newGeometry.size() != oldGeometry.size()) { | 294 | if (newGeometry.size() != oldGeometry.size()) { | ||
295 | m_changed = true; | 295 | m_changed = true; | ||
296 | update(); | 296 | update(); | ||
297 | } | 297 | } | ||
298 | QQuickItem::geometryChanged(newGeometry, oldGeometry); | 298 | QQuickItem::geometryChanged(newGeometry, oldGeometry); | ||
299 | } | 299 | } | ||
300 | 300 | | |||
301 | void DesktopIcon::handleFinished(QNetworkAccessManager* qnam, QNetworkReply* reply) { | ||||
302 | if (reply->error() == QNetworkReply::NoError) { | ||||
303 | const QUrl possibleRedirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); | ||||
304 | if (!possibleRedirectUrl.isEmpty()) { | ||||
305 | const QUrl redirectUrl = reply->url().resolved(possibleRedirectUrl); | ||||
306 | if (redirectUrl == reply->url()) { | ||||
307 | // no infinite redirections thank you very much | ||||
308 | reply->deleteLater(); | ||||
309 | return; | ||||
310 | } | ||||
311 | reply->deleteLater(); | ||||
312 | QNetworkRequest request(possibleRedirectUrl); | ||||
313 | request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); | ||||
314 | QNetworkReply* newReply = qnam->get(request); | ||||
315 | connect(newReply, &QNetworkReply::readyRead, this, [this, newReply](){ handleReadyRead(newReply); }); | ||||
316 | connect(newReply, &QNetworkReply::finished, this, [this, qnam, newReply](){ handleFinished(qnam, newReply); }); | ||||
317 | return; | ||||
318 | } | ||||
319 | } | ||||
320 | } | ||||
321 | | ||||
322 | void DesktopIcon::handleReadyRead(QNetworkReply* reply) | ||||
323 | { | ||||
324 | if (reply->attribute(QNetworkRequest::RedirectionTargetAttribute).isNull()) { | ||||
325 | QByteArray data; | ||||
326 | do { | ||||
327 | data.append(reply->read(32768)); | ||||
328 | // Because we are in the main thread, this could be potentially very expensive, so let's not block | ||||
329 | qApp->processEvents(); | ||||
330 | } while(!reply->atEnd()); | ||||
331 | m_loadedImage = QImage::fromData(data); | ||||
332 | if (m_loadedImage.isNull()) { | ||||
333 | // broken image from data, inform the user of this with some useful broken-image thing... | ||||
334 | const QSize size = QSize(width(), height()) * (window() ? window()->devicePixelRatio() : qApp->devicePixelRatio()); | ||||
335 | m_loadedImage = QIcon::fromTheme("unknown").pixmap(size, iconMode(), QIcon::On).toImage(); | ||||
336 | } | ||||
337 | m_changed = true; | ||||
338 | update(); | ||||
339 | } | ||||
340 | } | ||||
341 | | ||||
301 | QImage DesktopIcon::findIcon(const QSize &size) | 342 | QImage DesktopIcon::findIcon(const QSize &size) | ||
302 | { | 343 | { | ||
303 | QImage img; | 344 | QImage img; | ||
304 | QString iconSource = m_source.toString(); | 345 | QString iconSource = m_source.toString(); | ||
305 | if (iconSource.startsWith("image://")){ | 346 | if (iconSource.startsWith("image://")){ | ||
306 | QUrl iconUrl(iconSource); | 347 | QUrl iconUrl(iconSource); | ||
307 | QString iconProviderId = iconUrl.host(); | 348 | QString iconProviderId = iconUrl.host(); | ||
308 | QString iconId = iconUrl.path(); | 349 | QString iconId = iconUrl.path(); | ||
309 | QSize actualSize; | 350 | QSize actualSize; | ||
310 | QQuickImageProvider* imageProvider = dynamic_cast<QQuickImageProvider*>( | 351 | QQuickImageProvider* imageProvider = dynamic_cast<QQuickImageProvider*>( | ||
311 | qmlEngine(this)->imageProvider(iconProviderId)); | 352 | qmlEngine(this)->imageProvider(iconProviderId)); | ||
312 | if (!imageProvider) | 353 | if (!imageProvider) | ||
313 | return img; | 354 | return img; | ||
314 | switch(imageProvider->imageType()){ | 355 | switch(imageProvider->imageType()){ | ||
315 | case QQmlImageProviderBase::Image: | 356 | case QQmlImageProviderBase::Image: | ||
316 | img = imageProvider->requestImage(iconId, &actualSize, size); | 357 | img = imageProvider->requestImage(iconId, &actualSize, size); | ||
317 | case QQmlImageProviderBase::Pixmap: | 358 | case QQmlImageProviderBase::Pixmap: | ||
318 | img = imageProvider->requestPixmap(iconId, &actualSize, size).toImage(); | 359 | img = imageProvider->requestPixmap(iconId, &actualSize, size).toImage(); | ||
319 | case QQmlImageProviderBase::Texture: | 360 | case QQmlImageProviderBase::Texture: | ||
320 | case QQmlImageProviderBase::Invalid: | 361 | case QQmlImageProviderBase::Invalid: | ||
321 | case QQmlImageProviderBase::ImageResponse: | 362 | case QQmlImageProviderBase::ImageResponse: | ||
322 | //will have to investigate this more | 363 | //will have to investigate this more | ||
323 | break; | 364 | break; | ||
324 | } | 365 | } | ||
366 | } else if(iconSource.startsWith("http://") || iconSource.startsWith("https://")) { | ||||
mart: coding style, space between if and (
(in some other places as well) | |||||
367 | if(!m_loadedImage.isNull()) { | ||||
368 | return m_loadedImage.scaled(size, Qt::KeepAspectRatio, m_smooth ? Qt::SmoothTransformation : Qt::FastTransformation ); | ||||
369 | } | ||||
370 | QQmlEngine* engine = qmlEngine(this); | ||||
371 | QNetworkAccessManager* qnam; | ||||
372 | if (engine && (qnam = qmlEngine(this)->networkAccessManager())) { | ||||
373 | QNetworkRequest request(m_source.toUrl()); | ||||
374 | request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); | ||||
375 | QNetworkReply* reply = qnam->get(request); | ||||
376 | connect(reply, &QNetworkReply::readyRead, this, [this, reply](){ handleReadyRead(reply); }); | ||||
377 | connect(reply, &QNetworkReply::finished, this, [this, qnam, reply](){ handleFinished(qnam, reply); }); | ||||
378 | } | ||||
379 | // Temporary icon while we wait for the real image to load... | ||||
380 | img = QIcon::fromTheme("image-x-icon").pixmap(size, iconMode(), QIcon::On).toImage(); | ||||
325 | } else { | 381 | } else { | ||
326 | if (iconSource.startsWith("qrc:/")){ | 382 | if (iconSource.startsWith("qrc:/")){ | ||
327 | iconSource = iconSource.mid(3); | 383 | iconSource = iconSource.mid(3); | ||
328 | } | 384 | } | ||
329 | QIcon icon(iconSource); | 385 | QIcon icon(iconSource); | ||
330 | if (icon.availableSizes().isEmpty()) { | 386 | if (icon.availableSizes().isEmpty()) { | ||
331 | icon = QIcon::fromTheme(iconSource); | 387 | icon = QIcon::fromTheme(iconSource); | ||
332 | } | 388 | } | ||
Show All 18 Lines |
coding style, space between if and (
(in some other places as well)