Verified Commit d5b53eff authored by Eugene Popov's avatar Eugene Popov 🇺🇦 Committed by Fushan Wen
Browse files

Avoid memory leaks by misusing HistoryItem::mimeData()

parent 8f44e846
......@@ -18,6 +18,12 @@ public:
~HistoryImageItem() override
{
}
HistoryItemType type() const override
{
return HistoryItemType::Image;
}
QString text() const override;
bool operator==(const HistoryItem &rhs) const override
{
......
......@@ -15,6 +15,13 @@ class QDataStream;
class HistoryItem;
typedef QSharedPointer<HistoryItem> HistoryItemPtr;
typedef QSharedPointer<const HistoryItem> HistoryItemConstPtr;
enum class HistoryItemType {
Text,
Image,
Url,
};
/**
* An entry in the clipboard history.
*/
......@@ -24,6 +31,11 @@ public:
explicit HistoryItem(const QByteArray &uuid);
virtual ~HistoryItem();
/**
* Returns the item type.
*/
virtual HistoryItemType type() const = 0;
/**
* Return the current item as text
* An image would be returned as a descriptive
......@@ -112,3 +124,4 @@ inline QDataStream &operator<<(QDataStream &lhs, HistoryItem const *const rhs)
Q_DECLARE_METATYPE(HistoryItem *)
Q_DECLARE_METATYPE(HistoryItemPtr)
Q_DECLARE_METATYPE(HistoryItemConstPtr)
Q_DECLARE_METATYPE(HistoryItemType)
......@@ -56,14 +56,6 @@ QVariant HistoryModel::data(const QModelIndex &index, int role) const
}
QSharedPointer<HistoryItem> item = m_items.at(index.row());
HistoryItemType type = HistoryItemType::Text;
if (dynamic_cast<HistoryStringItem *>(item.data())) {
type = HistoryItemType::Text;
} else if (dynamic_cast<HistoryImageItem *>(item.data())) {
type = HistoryItemType::Image;
} else if (dynamic_cast<HistoryURLItem *>(item.data())) {
type = HistoryItemType::Url;
}
switch (role) {
case Qt::DisplayRole:
......@@ -75,11 +67,11 @@ QVariant HistoryModel::data(const QModelIndex &index, int role) const
case UuidRole:
return item->uuid();
case TypeRole:
return QVariant::fromValue<HistoryItemType>(type);
return QVariant::fromValue<HistoryItemType>(item->type());
case Base64UuidRole:
return item->uuid().toBase64();
case TypeIntRole:
return int(type);
return int(item->type());
}
return QVariant();
}
......
......@@ -10,12 +10,6 @@
class HistoryItem;
enum class HistoryItemType {
Text,
Image,
Url,
};
class HistoryModel : public QAbstractListModel
{
Q_OBJECT
......@@ -80,5 +74,3 @@ inline void HistoryModel::setDisplayImages(bool show)
{
m_displayImages = show;
}
Q_DECLARE_METATYPE(HistoryItemType)
......@@ -19,6 +19,12 @@ public:
~HistoryStringItem() override
{
}
HistoryItemType type() const override
{
return HistoryItemType::Text;
}
QString text() const override;
bool operator==(const HistoryItem &rhs) const override
{
......
......@@ -16,6 +16,12 @@ class HistoryURLItem : public HistoryItem
{
public:
HistoryURLItem(const QList<QUrl> &urls, const KUrlMimeData::MetaDataMap &metaData, bool cut);
HistoryItemType type() const override
{
return HistoryItemType::Url;
}
QString text() const override;
bool operator==(const HistoryItem &rhs) const override;
QMimeData *mimeData() const override;
......
......@@ -605,7 +605,7 @@ HistoryItemPtr Klipper::applyClipChanges(const QMimeData *clipData)
Ignore lock(m_locklevel);
if (!(history()->empty())) {
if (m_bIgnoreImages && history()->first()->mimeData()->hasImage()) {
if (m_bIgnoreImages && history()->first()->type() == HistoryItemType::Image) {
history()->remove(history()->first());
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment