Skip to content

Commit

Permalink
Fix handling of tags containing '&' character
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Jul 7, 2024
1 parent 5e81347 commit 95e431a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/gui/torrenttagsdialog.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023 Vladimir Golovnev <[email protected]>
* Copyright (C) 2023-2024 Vladimir Golovnev <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -37,6 +37,7 @@
#include "base/global.h"
#include "autoexpandabledialog.h"
#include "flowlayout.h"
#include "utils.h"

#include "ui_torrenttagsdialog.h"

Expand All @@ -55,7 +56,7 @@ TorrentTagsDialog::TorrentTagsDialog(const TagSet &initialTags, QWidget *parent)
auto *tagsLayout = new FlowLayout(m_ui->scrollArea);
for (const QString &tag : asConst(initialTags.united(BitTorrent::Session::instance()->tags())))
{
auto *tagWidget = new QCheckBox(tag);
auto *tagWidget = new QCheckBox(Utils::Gui::tagToWidgetText(tag));
if (initialTags.contains(tag))
tagWidget->setChecked(true);
tagsLayout->addWidget(tagWidget);
Expand Down Expand Up @@ -83,7 +84,7 @@ TagSet TorrentTagsDialog::tags() const
{
const auto *tagWidget = static_cast<QCheckBox *>(layout->itemAt(i)->widget());
if (tagWidget->isChecked())
tags.insert(tagWidget->text());
tags.insert(Utils::Gui::widgetTextToTag(tagWidget->text()));
}

return tags;
Expand Down Expand Up @@ -113,7 +114,7 @@ void TorrentTagsDialog::addNewTag()
{
auto *layout = m_ui->scrollArea->layout();
auto *btn = layout->takeAt(layout->count() - 1);
auto *tagWidget = new QCheckBox(tag);
auto *tagWidget = new QCheckBox(Utils::Gui::tagToWidgetText(tag));
tagWidget->setChecked(true);
layout->addWidget(tagWidget);
layout->addItem(btn);
Expand Down
3 changes: 2 additions & 1 deletion src/gui/transferlistwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2023-2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2006 Christophe Dumez <[email protected]>
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -1191,7 +1192,7 @@ void TransferListWidget::displayListMenu()

for (const QString &tag : asConst(tags))
{
auto *action = new TriStateAction(tag, tagsMenu);
auto *action = new TriStateAction(Utils::Gui::tagToWidgetText(tag), tagsMenu);
action->setCloseOnInteraction(false);

const Qt::CheckState initialState = tagsInAll.contains(tag) ? Qt::Checked
Expand Down
27 changes: 27 additions & 0 deletions src/gui/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2017 Mike Tzou
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -218,3 +219,29 @@ void Utils::Gui::openFolderSelect(const Path &path)
openPath(path.parentPath());
#endif
}

QString Utils::Gui::tagToWidgetText(const QString &tag)
{
return QString(tag).replace(u'&', u"&&"_s);
}

QString Utils::Gui::widgetTextToTag(const QString &text)
{
// replace pairs of '&' with single '&' and remove non-paired occurrences of '&'
QString cleanedText;
cleanedText.reserve(text.size());
bool amp = false;
for (const QChar c : text)
{
if (c == u'&')
{
amp = !amp;
if (amp)
continue;
}

cleanedText.append(c);
}

return cleanedText;
}
5 changes: 5 additions & 0 deletions src/gui/utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Vladimir Golovnev <[email protected]>
* Copyright (C) 2017 Mike Tzou
*
* This program is free software; you can redistribute it and/or
Expand Down Expand Up @@ -34,6 +35,7 @@ class QIcon;
class QPixmap;
class QPoint;
class QSize;
class QString;
class QWidget;

namespace Utils::Gui
Expand All @@ -51,4 +53,7 @@ namespace Utils::Gui

void openPath(const Path &path);
void openFolderSelect(const Path &path);

QString tagToWidgetText(const QString &tag);
QString widgetTextToTag(const QString &text);
}

0 comments on commit 95e431a

Please sign in to comment.