Skip to content

Commit

Permalink
Enrich the document of QCefDownloadItem::start and fix a bug of empty…
Browse files Browse the repository at this point in the history
… suggested file name
  • Loading branch information
tishion committed Jul 29, 2023
1 parent 87ac1cd commit 2f433cb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions include/QCefDownloadItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class QCEFVIEW_EXPORT QCefDownloadItem
/// </summary>
/// <param name="path">The full path name (must include file name) to save the downloaded item</param>
/// <param name="useDefaultDialog">Whether to use the default 'Save As...' dialog or not</param>
/// <remarks>
/// The 'path' parameter only works when 'useDefaultDialog' is set to false.
/// If you set 'useDefaultDialog' to true then you cannot control the initial
/// locatio nof the opened 'Save As...' dialog, it is determined by CEF internal implementation.
/// </remarks>
void start(const QString& path, bool useDefaultDialog = true) const;

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion src/details/CCefClientDelegate_DownloadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ CCefClientDelegate::onBeforeDownload(CefRefPtr<CefBrowser> browser,
item = QCefDownloadItemPrivate::create(shared_from_this());
}

// set suggested file name
auto suggestedFileName = QString::fromStdString(suggested_name);
QCefDownloadItemPrivate::setSuggestedName(item.data(), suggestedFileName);

// update
QCefDownloadItemPrivate::update(item.data(), *(download_item.get()));
QCefDownloadItemPrivate::setBeforeDownloadCallback(item.data(), callback);

// notify user of the new download item
weakRefItem = item;
pCefViewPrivate_->onNewDownloadItem(item, QString::fromStdString(suggested_name));
pCefViewPrivate_->onNewDownloadItem(item, suggestedFileName);
item.reset();
}

Expand Down
21 changes: 19 additions & 2 deletions src/details/QCefDownloadItemPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ QCefDownloadItemPrivate::create(CCefClientDelegate::RefPtr handler)
return QSharedPointer<QCefDownloadItem>(new QCefDownloadItem(), &QCefDownloadItemPrivate::destroy);
}

void
QCefDownloadItemPrivate::setSuggestedName(QCefDownloadItem* item, const QString& suggestedFileName)
{
auto p = item->d_ptr.data();
p->suggestedFileName = suggestedFileName;
}

void
QCefDownloadItemPrivate::update(QCefDownloadItem* item, CefDownloadItem& cefItem)
{
Expand Down Expand Up @@ -49,9 +56,19 @@ QCefDownloadItemPrivate::update(QCefDownloadItem* item, CefDownloadItem& cefItem
p->fullPath = QString::fromStdString(cefItem.GetFullPath().ToString());
p->url = QString::fromStdString(cefItem.GetURL().ToString());
p->originalUrl = QString::fromStdString(cefItem.GetOriginalUrl().ToString());
p->suggestedFileName = QString::fromStdString(cefItem.GetSuggestedFileName().ToString());
p->contentDisposition = QString::fromStdString(cefItem.GetContentDisposition().ToString());
p->mimeType = QString::fromStdString(cefItem.GetMimeType().ToString());

// update suggested name if not empty
auto suggestedFileName = QString::fromStdString(cefItem.GetSuggestedFileName().ToString());
if (!suggestedFileName.isEmpty()) {
p->suggestedFileName = suggestedFileName;
}

// update content disposition if not empty
auto contentDisposition = QString::fromStdString(cefItem.GetContentDisposition().ToString());
if (!contentDisposition.isEmpty()) {
p->contentDisposition = contentDisposition;
}
}

void
Expand Down
2 changes: 2 additions & 0 deletions src/details/QCefDownloadItemPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class QCefDownloadItemPrivate
public:
static QSharedPointer<QCefDownloadItem> create(CCefClientDelegate::RefPtr handler);

static void setSuggestedName(QCefDownloadItem* item, const QString& suggestedFileName);

static void update(QCefDownloadItem* item, CefDownloadItem& cefItem);

static void setBeforeDownloadCallback(QCefDownloadItem* item,
Expand Down

0 comments on commit 2f433cb

Please sign in to comment.