Skip to content

Commit

Permalink
itemsync: Avoid duplicate items from clipboard
Browse files Browse the repository at this point in the history
Fixes #2228
  • Loading branch information
hluk committed Jan 19, 2023
1 parent 182404f commit 5583489
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions plugins/itemsync/tests/itemsynctests.cpp
Expand Up @@ -731,3 +731,23 @@ void ItemSyncTests::moveOwnItemsSortsBaseNames()
RUN(args << "read(0,1,2,3)", "B,C,D,A");
RUN(args << testScript, "");
}

void ItemSyncTests::avoidDuplicateItemsAddedFromClipboard()
{
TestDir dir1(1);
const QString tab1 = testTab(1);
RUN("show" << tab1, "");

const Args args = Args() << "separator" << "," << "tab" << tab1;

RUN("config" << "clipboard_tab" << tab1, tab1 + "\n");

TEST( m_test->setClipboard("one") );
WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "one,,,");

TEST( m_test->setClipboard("two") );
WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "two,one,,");

TEST( m_test->setClipboard("one") );
WAIT_ON_OUTPUT(args << "read(0,1,2,3)", "one,two,,");
}
2 changes: 2 additions & 0 deletions plugins/itemsync/tests/itemsynctests.h
Expand Up @@ -50,6 +50,8 @@ private slots:

void moveOwnItemsSortsBaseNames();

void avoidDuplicateItemsAddedFromClipboard();

private:
TestInterfacePtr m_test;
};
Expand Down
12 changes: 12 additions & 0 deletions src/common/textdata.cpp
Expand Up @@ -13,6 +13,8 @@

namespace {

const QLatin1String mimePluginPrefix(COPYQ_MIME_PREFIX "item");

QString escapeHtmlSpaces(const QString &str)
{
QString str2 = str;
Expand All @@ -21,6 +23,13 @@ QString escapeHtmlSpaces(const QString &str)
.replace('\n', "<br />");
}

bool isPluginFormat(const QString &mime)
{
return mime.startsWith(mimePluginPrefix)
&& mime.size() > mimePluginPrefix.size()
&& mime[mimePluginPrefix.size()] != '-';
}

} // namespace

uint hash(const QVariantMap &data)
Expand All @@ -35,6 +44,9 @@ uint hash(const QVariantMap &data)
if (mime == mimeWindowTitle || mime == mimeOwner || mime == mimeClipboardMode)
continue;

if ( isPluginFormat(mime) )
continue;

seed = hash(seed, mime);
seed = hash(seed, data[mime].toByteArray());
}
Expand Down

0 comments on commit 5583489

Please sign in to comment.