summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2023-05-19 22:39:03 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2023-05-20 00:57:51 +0200
commit4bf725379af5b98e4403769abe608ea17dcd5bdb (patch)
treef9eda3106167cbbef3cceefa5f6e110816c362ae
parentab5f176de0bafd59eb8b10db80099299a4ca414c (diff)
Fix adding duplicate stickers and strip file extension
-rw-r--r--resources/qml/dialogs/ImagePackEditorDialog.qml9
-rw-r--r--src/SingleImagePackModel.cpp29
-rw-r--r--src/SingleImagePackModel.h2
3 files changed, 32 insertions, 8 deletions
diff --git a/resources/qml/dialogs/ImagePackEditorDialog.qml b/resources/qml/dialogs/ImagePackEditorDialog.qml
index de516dd9..4f30e78a 100644
--- a/resources/qml/dialogs/ImagePackEditorDialog.qml
+++ b/resources/qml/dialogs/ImagePackEditorDialog.qml
@@ -255,8 +255,13 @@ ApplicationWindow {
Layout.fillWidth: true
Layout.columnSpan: 2
label: qsTr("Shortcode")
- text: imagePack.data(imagePack.index(currentImageIndex, 0), SingleImagePackModel.ShortCode)
- onTextEdited: imagePack.setData(imagePack.index(currentImageIndex, 0), text, SingleImagePackModel.ShortCode)
+ property int bindingCounter: 0
+ text: bindingCounter, imagePack.data(imagePack.index(currentImageIndex, 0), SingleImagePackModel.ShortCode)
+ onTextEdited: {
+ imagePack.setData(imagePack.index(currentImageIndex, 0), text, SingleImagePackModel.ShortCode);
+ // force text field to update in case the model disagreed with the new value.
+ bindingCounter++;
+ }
}
MatrixTextField {
diff --git a/src/SingleImagePackModel.cpp b/src/SingleImagePackModel.cpp
index 97d17e23..b26396ba 100644
--- a/src/SingleImagePackModel.cpp
+++ b/src/SingleImagePackModel.cpp
@@ -5,6 +5,7 @@
#include "SingleImagePackModel.h"
#include <QFile>
+#include <QFileInfo>
#include <QMimeDatabase>
#include <mtx/responses/media.hpp>
@@ -93,8 +94,7 @@ SingleImagePackModel::setData(const QModelIndex &index, const QVariant &value, i
auto newCode = value.toString().toStdString();
// otherwise we delete this by accident
- if (pack.images.count(newCode))
- return false;
+ newCode = unconflictingShortcode(newCode);
auto tmp = img;
auto oldCode = shortcodes.at(index.row());
@@ -336,11 +336,12 @@ SingleImagePackModel::addStickers(QList<QUrl> files)
info.mimetype = QMimeDatabase().mimeTypeForFile(f.toLocalFile()).name().toStdString();
auto filename = f.fileName().toStdString();
+ auto basename = QFileInfo(file).baseName().toStdString();
http::client()->upload(
bytes.toStdString(),
QMimeDatabase().mimeTypeForFile(f.toLocalFile()).name().toStdString(),
filename,
- [this, filename, info](const mtx::responses::ContentURI &uri, mtx::http::RequestErr e) {
+ [this, basename, info](const mtx::responses::ContentURI &uri, mtx::http::RequestErr e) {
if (e) {
ChatPage::instance()->showNotification(
tr("Failed to upload image: %1")
@@ -348,7 +349,7 @@ SingleImagePackModel::addStickers(QList<QUrl> files)
return;
}
- emit addImage(uri.content_uri, filename, info);
+ emit addImage(uri.content_uri, basename, info);
});
}
}
@@ -393,6 +394,20 @@ SingleImagePackModel::remove(int idx)
}
}
+std::string
+SingleImagePackModel::unconflictingShortcode(const std::string &shortcode)
+{
+ if (pack.images.count(shortcode)) {
+ for (int i = 0; i < 64'000; i++) {
+ auto tempCode = shortcode + std::to_string(i);
+ if (!pack.images.count(tempCode)) {
+ return tempCode;
+ }
+ }
+ }
+ return shortcode;
+}
+
void
SingleImagePackModel::addImageCb(std::string uri, std::string filename, mtx::common::ImageInfo info)
{
@@ -402,8 +417,10 @@ SingleImagePackModel::addImageCb(std::string uri, std::string filename, mtx::com
beginInsertRows(
QModelIndex(), static_cast<int>(shortcodes.size()), static_cast<int>(shortcodes.size()));
- pack.images[filename] = img;
- shortcodes.push_back(filename);
+ auto shortcode = unconflictingShortcode(filename);
+
+ pack.images[shortcode] = img;
+ shortcodes.push_back(shortcode);
endInsertRows();
diff --git a/src/SingleImagePackModel.h b/src/SingleImagePackModel.h
index 09a59a2f..65a27bcf 100644
--- a/src/SingleImagePackModel.h
+++ b/src/SingleImagePackModel.h
@@ -87,6 +87,8 @@ private slots:
void addImageCb(std::string uri, std::string filename, mtx::common::ImageInfo info);
private:
+ std::string unconflictingShortcode(const std::string &shortcode);
+
std::string roomid_;
std::string statekey_, old_statekey_;