[network/neochat/release/26.08] src: Actually paste attachments if you only have a file list in the clipboard

Joshua Goins <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit d1b7609541697bd94a5b4e7586a1fd9a3653c5b2 by Joshua Goins.
Committed on 24/07/2026 at 20:14.
Pushed by redstrate into branch 'release/26.08'.

Actually paste attachments if you only have a file list in the clipboard

For example, the "Copy" command in Dolphin never actually worked in
tandem with NeoChat which is *very* strange from a user's PoV. Now

NeoChat is a bit smarter and this should significantly improve the
image/attachment pasting experience.

(cherry picked from commit 261d8a98d37adeee226e9feb5d23a530b6089f6f)

M  +10   -5    src/libneochat/chatkeyhelper.cpp
M  +3    -3    src/libneochat/chatkeyhelper.h
M  +19   -8    src/libneochat/clipboard.cpp
M  +5    -6    src/libneochat/clipboard.h
M  +1    -3    src/messagecontent/models/chatbarmessagecontentmodel.cpp

https://invent.kde.org/network/neochat/-/commit/d1b7609541697bd94a5b4e7586a1fd9a3653c5b2

diff --git a/src/libneochat/chatkeyhelper.cpp b/src/libneochat/chatkeyhelper.cpp
index 1158b74b8..8492a9fac 100644
--- a/src/libneochat/chatkeyhelper.cpp
+++ b/src/libneochat/chatkeyhelper.cpp
@@ -70,7 +70,7 @@ bool ChatKeyHelper::handleKey(Qt::Key key, Qt::KeyboardModifiers modifiers)
 bool ChatKeyHelper::vKey(Qt::KeyboardModifiers modifiers)
 {
     if (modifiers.testFlag(Qt::ControlModifier)) {
-        return pasteImage();
+        return pasteAttachments();
     }
     return false;
 }
@@ -298,14 +298,19 @@ bool ChatKeyHelper::cancel()
     return false;
 }
 
-bool ChatKeyHelper::pasteImage()
+bool ChatKeyHelper::pasteAttachments()
 {
     if (!m_textItem) {
         return false;
     }
-    const auto savePath = Clipboard().saveImage();
-    if (!savePath.isEmpty()) {
-        Q_EMIT imagePasted(savePath);
+    Clipboard clipboard;
+    if (clipboard.hasImage()) {
+        const auto savePath = clipboard.saveImage();
+        if (!savePath.isEmpty()) {
+            Q_EMIT attachmentPasted(savePath);
+        }
+    } else if (clipboard.hasUriList() && !clipboard.uriList().isEmpty()) {
+        Q_EMIT attachmentPasted(clipboard.uriList().constFirst());
     }
     return false;
 }
diff --git a/src/libneochat/chatkeyhelper.h b/src/libneochat/chatkeyhelper.h
index fa46a1088..2cc1a274c 100644
--- a/src/libneochat/chatkeyhelper.h
+++ b/src/libneochat/chatkeyhelper.h
@@ -125,9 +125,9 @@ Q_SIGNALS:
     void requestReply(const QString &eventId);
 
     /**
-     * @brief An image has been pasted.
+     * @brief An attachment has been pasted.
      */
-    void imagePasted(const QString &filePath);
+    void attachmentPasted(const QUrl &filePath);
 
 private:
     QPointer<ChatTextItemHelper> m_textItem;
@@ -154,7 +154,7 @@ private:
 
     bool cancel();
 
-    bool pasteImage();
+    bool pasteAttachments();
 
     bool selectLeft(QTextCursor &cursor);
 
diff --git a/src/libneochat/clipboard.cpp b/src/libneochat/clipboard.cpp
index 9c3578ed1..30f78be54 100644
--- a/src/libneochat/clipboard.cpp
+++ b/src/libneochat/clipboard.cpp
@@ -33,7 +33,22 @@ QImage Clipboard::image() const
     return m_clipboard->image();
 }
 
-QString Clipboard::saveImage(QString localPath) const
+bool Clipboard::hasUriList() const
+{
+    return !uriList().isEmpty();
+}
+
+QList<QUrl> Clipboard::uriList() const
+{
+    const auto mimeData = m_clipboard->mimeData();
+    if (mimeData->hasUrls()) {
+        return mimeData->urls();
+    }
+
+    return {};
+}
+
+QUrl Clipboard::saveImage() const
 {
     QString imageDir(u"%1/screenshots"_s.arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)));
 
@@ -41,10 +56,7 @@ QString Clipboard::saveImage(QString localPath) const
         QDir().mkdir(imageDir);
     }
 
-    if (localPath.isEmpty()) {
-        localPath = u"file://%1/%2.png"_s.arg(imageDir, QDateTime::currentDateTime().toString(u"yyyy-MM-dd-hh-mm-ss"_s));
-    }
-    QUrl url(localPath);
+    QUrl url(u"file://%1/%2.png"_s.arg(imageDir, QDateTime::currentDateTime().toString(u"yyyy-MM-dd-hh-mm-ss"_s)));
     if (!url.isLocalFile()) {
         return {};
     }
@@ -55,10 +67,9 @@ QString Clipboard::saveImage(QString localPath) const
     }
 
     if (image.save(url.toLocalFile())) {
-        return localPath;
-    } else {
-        return {};
+        return url;
     }
+    return {};
 }
 
 void Clipboard::saveText(QString message)
diff --git a/src/libneochat/clipboard.h b/src/libneochat/clipboard.h
index d9789898c..e08d24860 100644
--- a/src/libneochat/clipboard.h
+++ b/src/libneochat/clipboard.h
@@ -42,21 +42,20 @@ public:
 
     [[nodiscard]] QImage image() const;
 
+    [[nodiscard]] bool hasUriList() const;
+
+    [[nodiscard]] QList<QUrl> uriList() const;
+
     /**
      * @brief Save the current clipboard image to file.
      *
      * If the clipboard does not contain an image or if it contains an image in an
      * unsupported image format nothing happens.
      *
-     * The given file path must be both valid and local or nothing happens.
-     *
-     * @param localPath the path to save the image. A default path for the app cache
-     *                  will be used if available and this is empty.
-     *
      * @return A QString with the path that the image was saved to. The string will
      *         be empty if nothing was saved.
      */
-    Q_INVOKABLE QString saveImage(QString localPath = {}) const;
+    Q_INVOKABLE QUrl saveImage() const;
 
     /**
      * @brief Set the clipboard content to the input message.
diff --git a/src/messagecontent/models/chatbarmessagecontentmodel.cpp b/src/messagecontent/models/chatbarmessagecontentmodel.cpp
index c7e5debc7..545f42ec0 100644
--- a/src/messagecontent/models/chatbarmessagecontentmodel.cpp
+++ b/src/messagecontent/models/chatbarmessagecontentmodel.cpp
@@ -315,9 +315,7 @@ void ChatBarMessageContentModel::connectKeyHelper()
     connect(m_keyHelper, &ChatKeyHelper::requestReply, this, [this](const QString &eventId) {
         addReply(eventId);
     });
-    connect(m_keyHelper, &ChatKeyHelper::imagePasted, this, [this](const QString &filePath) {
-        addAttachment(QUrl(filePath));
-    });
+    connect(m_keyHelper, &ChatKeyHelper::attachmentPasted, this, &ChatBarMessageContentModel::addAttachment);
 }
 
 int ChatBarMessageContentModel::focusRow() const
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.