mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
Applied a bit of foutf's suggestions. Replace uploadQueue.size() with .empty() and swap if/else bodies, move non empty queue check outside of the timer on line 60, move getImageFileFormat to an anonymous namespace, rename pasteFromClipoard to upload(), removed usesless comment, shortened message on line 83, use QMimeData.hasUrls() and QMimeData.urls(), moved GIF format case in upload() more to the top, call original functions in canInsertFromMimeData and dragEnterEvent which are overriden
This commit is contained in:
parent
298c013fa0
commit
4e9951371f
4 changed files with 99 additions and 97 deletions
|
@ -37,7 +37,12 @@ void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel,
|
||||||
.multiPart(payload)
|
.multiPart(payload)
|
||||||
.onSuccess([&textEdit, channel](NetworkResult result) -> Outcome {
|
.onSuccess([&textEdit, channel](NetworkResult result) -> Outcome {
|
||||||
textEdit.insertPlainText(result.getData() + QString(" "));
|
textEdit.insertPlainText(result.getData() + QString(" "));
|
||||||
if (uploadQueue.size())
|
if (uploadQueue.empty())
|
||||||
|
{
|
||||||
|
channel->addMessage(makeSystemMessage(
|
||||||
|
QString("Your image has been uploaded.")));
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
channel->addMessage(makeSystemMessage(
|
channel->addMessage(makeSystemMessage(
|
||||||
QString("Your image has been uploaded. %1 left. Please "
|
QString("Your image has been uploaded. %1 left. Please "
|
||||||
|
@ -49,19 +54,14 @@ void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel,
|
||||||
// 2 seconds for the timer that's there not to spam Nuuls' server
|
// 2 seconds for the timer that's there not to spam Nuuls' server
|
||||||
// and 1 second of actual uploading.
|
// and 1 second of actual uploading.
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
channel->addMessage(makeSystemMessage(
|
|
||||||
QString("Your image has been uploaded.")));
|
|
||||||
}
|
|
||||||
isUploading = false;
|
isUploading = false;
|
||||||
QTimer::singleShot(2000, [channel, &textEdit]() {
|
if (uploadQueue.size())
|
||||||
if (uploadQueue.size())
|
{
|
||||||
{
|
QTimer::singleShot(2000, [channel, &textEdit]() {
|
||||||
uploadImageToNuuls(uploadQueue.front(), channel, textEdit);
|
uploadImageToNuuls(uploadQueue.front(), channel, textEdit);
|
||||||
uploadQueue.pop();
|
uploadQueue.pop();
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
return Success;
|
return Success;
|
||||||
})
|
})
|
||||||
.onError([channel](NetworkResult result) -> bool {
|
.onError([channel](NetworkResult result) -> bool {
|
||||||
|
@ -73,31 +73,14 @@ void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel,
|
||||||
})
|
})
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
QString getImageFileFormat(QString path)
|
|
||||||
{
|
|
||||||
static QStringList LIST_OF_IMAGE_FORMATS = {".png", ".jpg", ".jpeg"};
|
|
||||||
for (QString i : LIST_OF_IMAGE_FORMATS)
|
|
||||||
{
|
|
||||||
if (path.endsWith(i))
|
|
||||||
{
|
|
||||||
return i.replace('.', "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
|
|
||||||
void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
|
void upload(const QMimeData *source, ChannelPtr channel,
|
||||||
ResizingTextEdit &outputTextEdit)
|
ResizingTextEdit &outputTextEdit)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
http://localhost:7494/upload?password=xd
|
|
||||||
default port and password for nuuls' filehost.
|
|
||||||
*/
|
|
||||||
if (isUploading)
|
if (isUploading)
|
||||||
{
|
{
|
||||||
channel->addMessage(makeSystemMessage(
|
channel->addMessage(makeSystemMessage(
|
||||||
QString("You are already uploading an image. "
|
QString("Please wait until the upload finishes.")));
|
||||||
"Please wait until the upload finishes.")));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,62 +92,61 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
|
||||||
uploadImageToNuuls({source->data("image/png"), "png"}, channel,
|
uploadImageToNuuls({source->data("image/png"), "png"}, channel,
|
||||||
outputTextEdit);
|
outputTextEdit);
|
||||||
}
|
}
|
||||||
else if (source->hasFormat("text/uri-list"))
|
else if (source->hasFormat("image/jpeg"))
|
||||||
{
|
{
|
||||||
QStringList potientialPathsToSend =
|
uploadImageToNuuls({source->data("image/jpeg"), "jpeg"}, channel,
|
||||||
QString(source->data("text/uri-list").toStdString().c_str())
|
outputTextEdit);
|
||||||
.split("\r\n");
|
}
|
||||||
|
else if (source->hasFormat("image/gif"))
|
||||||
for (QString path : potientialPathsToSend)
|
{
|
||||||
|
uploadImageToNuuls({source->data("image/gif"), "gif"}, channel,
|
||||||
|
outputTextEdit);
|
||||||
|
}
|
||||||
|
else if (source->hasUrls())
|
||||||
|
{
|
||||||
|
for (QUrl path : source->urls())
|
||||||
{
|
{
|
||||||
if (path.isEmpty())
|
if (getImageFileFormat(path.toLocalFile()) != QString())
|
||||||
{
|
{
|
||||||
break;
|
channel->addMessage(makeSystemMessage(
|
||||||
|
QString("Uploading image: %1").arg(path.toLocalFile())));
|
||||||
|
QImage img = QImage(path.toLocalFile());
|
||||||
|
if (img.isNull())
|
||||||
|
{
|
||||||
|
channel->addMessage(
|
||||||
|
makeSystemMessage(QString("Couldn't load image :(")));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QByteArray imageData;
|
||||||
|
QBuffer buf(&imageData);
|
||||||
|
buf.open(QIODevice::WriteOnly);
|
||||||
|
img.save(&buf, "png");
|
||||||
|
|
||||||
|
TypedBytes data = {imageData, "png"};
|
||||||
|
uploadQueue.push(data);
|
||||||
|
}
|
||||||
|
else if (path.toLocalFile().endsWith(".gif"))
|
||||||
|
{
|
||||||
|
channel->addMessage(makeSystemMessage(
|
||||||
|
QString("Uploading GIF: %1").arg(path.toLocalFile())));
|
||||||
|
QFile file(path.toLocalFile());
|
||||||
|
bool isOkay = file.open(QIODevice::ReadOnly);
|
||||||
|
if (!isOkay)
|
||||||
|
{
|
||||||
|
channel->addMessage(
|
||||||
|
makeSystemMessage(QString("Failed to open file. :(")));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
TypedBytes data = {file.readAll(), "gif"};
|
||||||
|
uploadQueue.push(data);
|
||||||
|
file.close();
|
||||||
|
// file.readAll() => might be a bit big but it /should/ work
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (getImageFileFormat(path) != QString())
|
channel->addMessage(makeSystemMessage(
|
||||||
{
|
QString("Cannot upload file: %1, not an image")
|
||||||
channel->addMessage(makeSystemMessage(
|
.arg(path.toLocalFile())));
|
||||||
QString("Uploading image: %1").arg(path)));
|
|
||||||
QImage img = QImage(QUrl(path).toLocalFile());
|
|
||||||
if (img.isNull())
|
|
||||||
{
|
|
||||||
channel->addMessage(makeSystemMessage(
|
|
||||||
QString("Couldn't load image :(")));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
QByteArray imageData;
|
|
||||||
QBuffer buf(&imageData);
|
|
||||||
buf.open(QIODevice::WriteOnly);
|
|
||||||
img.save(&buf, "png");
|
|
||||||
|
|
||||||
TypedBytes data = {imageData, "png"};
|
|
||||||
uploadQueue.push(data);
|
|
||||||
}
|
|
||||||
else if (path.endsWith(".gif"))
|
|
||||||
{
|
|
||||||
channel->addMessage(makeSystemMessage(
|
|
||||||
QString("Uploading GIF: %1").arg(path)));
|
|
||||||
QFile file(QUrl(path).toLocalFile());
|
|
||||||
bool isOkay = file.open(QIODevice::ReadOnly);
|
|
||||||
if (!isOkay)
|
|
||||||
{
|
|
||||||
channel->addMessage(makeSystemMessage(
|
|
||||||
QString("Failed to open file. :(")));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
TypedBytes data = {file.readAll(), "gif"};
|
|
||||||
uploadQueue.push(data);
|
|
||||||
file.close();
|
|
||||||
// file.readAll() => might be a bit big but it /should/ work
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
channel->addMessage(makeSystemMessage(
|
|
||||||
QString("Cannot upload file: %1, not an image")
|
|
||||||
.arg(path)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (uploadQueue.size())
|
if (uploadQueue.size())
|
||||||
|
@ -173,11 +155,6 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
|
||||||
uploadQueue.pop();
|
uploadQueue.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (source->hasFormat("image/gif"))
|
|
||||||
{
|
|
||||||
TypedBytes data = {source->data("image/gif"), "gif"};
|
|
||||||
uploadImageToNuuls(data, channel, outputTextEdit);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{ // not PNG, try loading it into QImage and save it to a PNG.
|
{ // not PNG, try loading it into QImage and save it to a PNG.
|
||||||
QImage image = qvariant_cast<QImage>(source->imageData());
|
QImage image = qvariant_cast<QImage>(source->imageData());
|
||||||
|
@ -190,3 +167,19 @@ void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace chatterino
|
} // namespace chatterino
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
QString getImageFileFormat(QString path)
|
||||||
|
{
|
||||||
|
static QStringList listOfImageFormats = {".png", ".jpg", ".jpeg"};
|
||||||
|
for (const QString &format : listOfImageFormats)
|
||||||
|
{
|
||||||
|
if (path.endsWith(format))
|
||||||
|
{
|
||||||
|
return format.mid(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
|
@ -9,11 +9,14 @@ struct TypedBytes {
|
||||||
QByteArray data;
|
QByteArray data;
|
||||||
QString type;
|
QString type;
|
||||||
};
|
};
|
||||||
void uploadImageToNuuls(QByteArray imageData, ChannelPtr channel,
|
void upload(QByteArray imageData, ChannelPtr channel,
|
||||||
ResizingTextEdit &textEdit, std::string format);
|
ResizingTextEdit &textEdit, std::string format);
|
||||||
void uploadImageToNuuls(TypedBytes imageData, ChannelPtr channel,
|
void upload(TypedBytes imageData, ChannelPtr channel,
|
||||||
ResizingTextEdit &textEdit);
|
ResizingTextEdit &textEdit);
|
||||||
QString getImageFileFormat(QString path);
|
void upload(const QMimeData *source, ChannelPtr channel,
|
||||||
void pasteFromClipboard(const QMimeData *source, ChannelPtr channel,
|
ResizingTextEdit &outputTextEdit);
|
||||||
ResizingTextEdit &outputTextEdit);
|
|
||||||
} // namespace chatterino
|
} // namespace chatterino
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
QString getImageFileFormat(QString path);
|
||||||
|
}
|
||||||
|
|
|
@ -264,7 +264,10 @@ bool ResizingTextEdit::canInsertFromMimeData(const QMimeData *source) const
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
else
|
||||||
|
{
|
||||||
|
return QTextEdit::canInsertFromMimeData(source);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResizingTextEdit::insertFromMimeData(const QMimeData *source)
|
void ResizingTextEdit::insertFromMimeData(const QMimeData *source)
|
||||||
|
@ -285,8 +288,12 @@ void ResizingTextEdit::dragEnterEvent(QDragEnterEvent *event)
|
||||||
{
|
{
|
||||||
event->acceptProposedAction();
|
event->acceptProposedAction();
|
||||||
}
|
}
|
||||||
// QTextEdit doesn't implement dragEnterEvent, so there's nothing to call here.
|
else
|
||||||
|
{
|
||||||
|
QAbstractScrollArea::dragEnterEvent(event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ResizingTextEdit::dropEvent(QDropEvent *event)
|
void ResizingTextEdit::dropEvent(QDropEvent *event)
|
||||||
{
|
{
|
||||||
if (event->mimeData()->hasImage() || event->mimeData()->hasUrls())
|
if (event->mimeData()->hasImage() || event->mimeData()->hasUrls())
|
||||||
|
|
|
@ -209,8 +209,7 @@ Split::Split(QWidget *parent)
|
||||||
[this] { this->focusLost.invoke(); });
|
[this] { this->focusLost.invoke(); });
|
||||||
this->input_->ui_.textEdit->pastedImage.connect(
|
this->input_->ui_.textEdit->pastedImage.connect(
|
||||||
[this](const QMimeData *source) {
|
[this](const QMimeData *source) {
|
||||||
pasteFromClipboard(source, this->getChannel(),
|
upload(source, this->getChannel(), *this->input_->ui_.textEdit);
|
||||||
*this->input_->ui_.textEdit);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue