Allow more than one media file when importing from the web

To import media files from the web, increas the size of the
dialog box and allow several URLs separated by newlines.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
This commit is contained in:
Robert C. Helling 2022-03-31 22:39:47 +02:00 committed by Dirk Hohndel
parent bc2de35242
commit 96e825fb2e
5 changed files with 59 additions and 52 deletions

View file

@ -1,4 +1,4 @@
- Desktop: Allow more than one media file to be imported from web
---
* Always add new entries at the very top of this file above other existing entries and this note.
* Use this layout for new entries: `[Area]: [Details about the change] [reference thread / issue]`

View file

@ -905,46 +905,53 @@ void DiveListView::loadWebImages()
URLDialog urlDialog(this);
if (!urlDialog.exec())
return;
loadImageFromURL(QUrl::fromUserInput(urlDialog.url()));
loadImagesFromURLs(urlDialog.url());
}
void DiveListView::loadImageFromURL(QUrl url)
void DiveListView::loadImagesFromURLs(QString urls)
{
if (url.isValid()) {
QEventLoop loop;
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
while (reply->isRunning()) {
loop.processEvents();
sleep(1);
}
QByteArray imageData = reply->readAll();
QStringList validUrls;
QStringList urlList = urls.split('\n');
QStringList::ConstIterator userUrl;
for (userUrl = urlList.constBegin(); userUrl != urlList.constEnd(); ++userUrl) {
QUrl url = QUrl::fromUserInput(*userUrl);
if (url.isValid()) {
QEventLoop loop;
QNetworkRequest request(url);
QNetworkReply *reply = manager.get(request);
while (reply->isRunning()) {
loop.processEvents();
sleep(1);
}
QByteArray imageData = reply->readAll();
QImage image = QImage();
image.loadFromData(imageData);
if (image.isNull()) {
// If this is not an image, maybe it's an html file and Miika can provide some xslr magic to extract images.
// In this case we would call the function recursively on the list of image source urls;
report_error(qPrintable(tr("%1 does not appear to be an image").arg(url.toString())));
return;
}
QImage image = QImage();
image.loadFromData(imageData);
if (image.isNull()) {
// If this is not an image, maybe it's an html file and Miika can provide some xslr magic to extract images.
// In this case we would call the function recursively on the list of image source urls;
report_error(qPrintable(tr("%1 does not appear to be an image").arg(url.toString())));
return;
}
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(url.toString().toUtf8());
QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first();
QDir dir(path);
if (!dir.exists())
dir.mkpath(path);
QFile imageFile(path.append("/").append(hash.result().toHex()));
if (imageFile.open(QIODevice::WriteOnly)) {
QDataStream stream(&imageFile);
stream.writeRawData(imageData.data(), imageData.length());
imageFile.waitForBytesWritten(-1);
imageFile.close();
learnPictureFilename(url.toString(), imageFile.fileName());
matchImagesToDives(QStringList(url.toString()));
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(url.toString().toUtf8());
QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first();
QDir dir(path);
if (!dir.exists())
dir.mkpath(path);
QFile imageFile(path.append("/").append(hash.result().toHex()));
if (imageFile.open(QIODevice::WriteOnly)) {
QDataStream stream(&imageFile);
stream.writeRawData(imageData.data(), imageData.length());
imageFile.waitForBytesWritten(-1);
imageFile.close();
learnPictureFilename(url.toString(), imageFile.fileName());
validUrls << url.toString();
}
}
}
matchImagesToDives(validUrls);
}
QString DiveListView::lastUsedImageDir()

View file

@ -82,7 +82,7 @@ private:
int lastImageTimeOffset();
void addToTrip(int delta);
void matchImagesToDives(QStringList fileNames);
void loadImageFromURL(QUrl url);
void loadImagesFromURLs(QString urls);
bool eventFilter(QObject *, QEvent *) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override;

View file

@ -279,7 +279,7 @@ URLDialog::URLDialog(QWidget *parent) : QDialog(parent)
QString URLDialog::url() const
{
return ui.urlField->text();
return ui.urlField->toPlainText();
}
bool isGnome3Session()

View file

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>397</width>
<height>103</height>
<width>806</width>
<height>320</height>
</rect>
</property>
<property name="windowTitle">
@ -16,8 +16,8 @@
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>40</x>
<y>60</y>
<x>460</x>
<y>280</y>
<width>341</width>
<height>32</height>
</rect>
@ -29,27 +29,27 @@
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QLineEdit" name="urlField">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>371</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>151</width>
<width>281</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Enter URL for media files</string>
<string>Enter URLs for media files, one URL per line</string>
</property>
</widget>
<widget class="QTextEdit" name="urlField">
<property name="geometry">
<rect>
<x>10</x>
<y>30</y>
<width>781</width>
<height>251</height>
</rect>
</property>
</widget>
</widget>