Skip to content

Commit

Permalink
fix: [thumbnail] The thumbnail of the FTP/SMB file is abnormal
Browse files Browse the repository at this point in the history
1.Because AsyncFileInfo is cached, it is unable to respond correctly to thumbnail display settings
2.Creating thumbnail icon in a thread may cause the app to crash

Log: fix bug
Bug: https://pms.uniontech.com/bug-view-210681.html
  • Loading branch information
Kakueeen authored and deepin-bot[bot] committed Jul 21, 2023
1 parent fa2ef5f commit 7bfa569
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 30 deletions.
2 changes: 0 additions & 2 deletions src/dfm-base/utils/thumbnail/private/thumbnailworker_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class ThumbnailWorkerPrivate
void insertUrl(const QUrl &key, const QUrl &value);
QUrl takeUrl(const QUrl &key);

QIcon createIcon(const QString &iconName);

ThumbnailWorker *q { nullptr };
DMimeDatabase mimeDb;
QMutex mutex;
Expand Down
2 changes: 1 addition & 1 deletion src/dfm-base/utils/thumbnail/thumbnailfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ThumbnailFactory final : public QObject
bool registerThumbnailCreator(const QString &mimeType, ThumbnailCreator creator);

Q_SIGNALS:
void produceFinished(const QUrl &src, const QIcon &thumbIcon);
void produceFinished(const QUrl &src, const QString &thumb);
void produceFailed(const QUrl &src);

void addTask(const QUrl &url, DFMGLOBAL_NAMESPACE::ThumbnailSize size);
Expand Down
19 changes: 2 additions & 17 deletions src/dfm-base/utils/thumbnail/thumbnailworker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,6 @@ QUrl ThumbnailWorkerPrivate::takeUrl(const QUrl &key)
return localAndVirtualHash.take(key);
}

QIcon ThumbnailWorkerPrivate::createIcon(const QString &iconName)
{
QIcon icon(iconName);
if (!icon.isNull()) {
QPixmap pixmap = icon.pixmap(Global::kLarge, Global::kLarge);
QPainter pa(&pixmap);
pa.setPen(Qt::gray);
pa.drawPixmap(0, 0, pixmap);

icon.addPixmap(pixmap);
}

return icon;
}

ThumbnailWorker::ThumbnailWorker(QObject *parent)
: QObject(parent),
d(new ThumbnailWorkerPrivate(this))
Expand Down Expand Up @@ -163,7 +148,7 @@ void ThumbnailWorker::onTaskAdded(const QUrl &url, Global::ThumbnailSize size)

const auto &img = ThumbnailHelper::instance()->thumbnailImage(fileUrl, size);
if (!img.isNull()) {
Q_EMIT thumbnailCreateFinished(url, d->createIcon(img.text(QT_STRINGIFY(Thumb::Path))));
Q_EMIT thumbnailCreateFinished(url, img.text(QT_STRINGIFY(Thumb::Path)));
return;
}

Expand Down Expand Up @@ -224,7 +209,7 @@ void ThumbnailWorker::doWork()
// create thumbnail
const auto &thumbnailPath = d->createThumbnail(task.srcUrl, task.size);
if (!thumbnailPath.isEmpty())
Q_EMIT thumbnailCreateFinished(d->takeUrl(task.srcUrl), d->createIcon(thumbnailPath));
Q_EMIT thumbnailCreateFinished(d->takeUrl(task.srcUrl), thumbnailPath);
else
Q_EMIT thumbnailCreateFailed(d->takeUrl(task.srcUrl));
}
Expand Down
2 changes: 1 addition & 1 deletion src/dfm-base/utils/thumbnail/thumbnailworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Q_SLOTS:
void onTaskRemoved(const QUrl &url);

Q_SIGNALS:
void thumbnailCreateFinished(const QUrl &url, const QIcon &thumbnail);
void thumbnailCreateFinished(const QUrl &url, const QString &thumbnail);
void thumbnailCreateFailed(const QUrl &url);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void FileInfoModelPrivate::dataUpdated(const QUrl &url, const bool isLinkOrg)
emit q->dataChanged(index, index);
}

void FileInfoModelPrivate::thumbUpdated(const QUrl &url, const QIcon &thumbIcon)
void FileInfoModelPrivate::thumbUpdated(const QUrl &url, const QString &thumb)
{
using namespace dfmbase::Global;
FileInfoPointer info { nullptr };
Expand All @@ -227,6 +227,11 @@ void FileInfoModelPrivate::thumbUpdated(const QUrl &url, const QIcon &thumbIcon)
return;
}

// Creating thumbnail icon in a thread may cause the program to crash
QIcon thumbIcon(thumb);
if (thumbIcon.isNull())
return;

info->setExtendedAttributes(ExtInfoType::kFileThumbnail, thumbIcon);
const QModelIndex &index = q->index(url);
if (Q_UNLIKELY(!index.isValid()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public slots:
void replaceData(const QUrl &oldUrl, const QUrl &newUrl);
void updateData(const QUrl &url);
void dataUpdated(const QUrl &url, const bool isLinkOrg);
void thumbUpdated(const QUrl &url, const QIcon &thumbIcon);
void thumbUpdated(const QUrl &url, const QString &thumb);

public:
QDir::Filters filters = QDir::NoFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FileProvider : public QObject
void fileRenamed(const QUrl &oldurl, const QUrl &newurl);
void fileUpdated(const QUrl &url);
void fileInfoUpdated(const QUrl &url, const bool isLinkOrg);
void fileThumbUpdated(const QUrl &url, const QIcon &thumb);
void fileThumbUpdated(const QUrl &url, const QString &thumb);
protected slots:
void traversalFinished();
void reset(QList<QUrl> children);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ FileItemData::FileItemData(const QUrl &url, const FileInfoPointer &info, FileIte
url(url),
info(info)
{
if (info)
info->customData(kItemFileRefreshIcon);
}

FileItemData::FileItemData(const SortInfoPointer &info, FileItemData *parent)
Expand Down Expand Up @@ -85,8 +87,11 @@ QVariant FileItemData::data(int role) const
switch (role) {
case kItemCreateFileInfoRole:
assert(qApp->thread() == QThread::currentThread());
if (info.isNull())
if (info.isNull()) {
const_cast<FileItemData *>(this)->info = InfoFactory::create<FileInfo>(url);
if (info)
info->customData(kItemFileRefreshIcon);
}
return QVariant();
case kItemFilePathRole:
if (info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ void FileViewModel::setReadOnly(bool value)
readOnly = value;
}

void FileViewModel::updateThumbnailIcon(const QModelIndex &index, const QIcon &thumbIcon)
void FileViewModel::updateThumbnailIcon(const QModelIndex &index, const QString &thumb)
{
if (!index.isValid() || index.row() < 0 || filterSortWorker.isNull())
return;
Expand All @@ -616,16 +616,21 @@ void FileViewModel::updateThumbnailIcon(const QModelIndex &index, const QIcon &t
if (!item || !item->fileInfo())
return;

// Creating thumbnail icon in a thread may cause the program to crash
QIcon thumbIcon(thumb);
if (thumbIcon.isNull())
return;

item->fileInfo()->setExtendedAttributes(ExtInfoType::kFileThumbnail, thumbIcon);
}

void FileViewModel::onFileThumbUpdated(const QUrl &url, const QIcon &thumbIcon)
void FileViewModel::onFileThumbUpdated(const QUrl &url, const QString &thumb)
{
auto updateIndex = getIndexByUrl(url);
if (!updateIndex.isValid())
return;

updateThumbnailIcon(updateIndex, thumbIcon);
updateThumbnailIcon(updateIndex, thumb);
auto view = qobject_cast<FileView *>(QObject::parent());
if (view) {
view->update(updateIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class FileViewModel : public QAbstractItemModel
void toggleHiddenFiles();
void setReadOnly(bool value);

void updateThumbnailIcon(const QModelIndex &index, const QIcon &thumbIcon);
void updateThumbnailIcon(const QModelIndex &index, const QString &thumb);

Q_SIGNALS:
void stateChanged();
Expand All @@ -112,7 +112,7 @@ class FileViewModel : public QAbstractItemModel
void requestSetFilterCallback(FileViewFilterCallback callback);

public Q_SLOTS:
void onFileThumbUpdated(const QUrl &url, const QIcon &thumbIcon);
void onFileThumbUpdated(const QUrl &url, const QString &thumb);
void onFileUpdated(int show);
void onInsert(int firstIndex, int count);
void onInsertFinish();
Expand Down

0 comments on commit 7bfa569

Please sign in to comment.