File indexing completed on 2025-01-26 04:57:25
0001 /* 0002 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "trackingwarningwidget.h" 0008 #include "trackingdetailsdialog.h" 0009 #include <KLocalizedString> 0010 0011 using namespace WebEngineViewer; 0012 TrackingWarningWidget::TrackingWarningWidget(QWidget *parent) 0013 : KMessageWidget(parent) 0014 { 0015 setVisible(false); 0016 setCloseButtonVisible(true); 0017 setMessageType(Warning); 0018 setWordWrap(true); 0019 setPosition(KMessageWidget::Header); 0020 0021 setText(i18n("Some Mail Tracker was found and was blocked.<a href=\"mailtrackingdetails\">(Details...)")); 0022 connect(this, &TrackingWarningWidget::linkActivated, this, &TrackingWarningWidget::slotShowDetails); 0023 } 0024 0025 TrackingWarningWidget::~TrackingWarningWidget() = default; 0026 0027 void TrackingWarningWidget::slotShowDetails(const QString &content) 0028 { 0029 if (content == QLatin1StringView("mailtrackingdetails")) { 0030 if (!mMailTrackingDetailDialog) { 0031 mMailTrackingDetailDialog = new WebEngineViewer::TrackingDetailsDialog; 0032 } 0033 mMailTrackingDetailDialog->setDetails(generateDetails()); 0034 mMailTrackingDetailDialog->show(); 0035 } 0036 } 0037 0038 QString TrackingWarningWidget::generateDetails() const 0039 { 0040 QString details = QLatin1StringView("<b>") + i18n("Details:") + QLatin1StringView("</b><ul>"); 0041 0042 QMapIterator<QString, blackListFound> i(mBackLists); 0043 while (i.hasNext()) { 0044 i.next(); 0045 details += QLatin1StringView("<li>") 0046 + i18np("1 tracker from the company %2 (%3)", "%1 trackers from the company %2 (%3)", i.value().number, i.key(), i.value().url); 0047 } 0048 details += QLatin1StringView("</ul>"); 0049 return details; 0050 } 0051 0052 void TrackingWarningWidget::addTracker(const WebEngineViewer::BlockTrackingUrlInterceptor::TrackerBlackList &tracker) 0053 { 0054 blackListFound item = mBackLists.value(tracker.mCompanyName); 0055 if (item.url.isEmpty()) { 0056 item.url = tracker.mCompanyUrl; 0057 mBackLists.insert(tracker.mCompanyName, item); 0058 } else { 0059 item.number = item.number + 1; 0060 mBackLists.insert(tracker.mCompanyName, item); 0061 } 0062 if (!isVisible()) { 0063 animatedShow(); 0064 } 0065 } 0066 0067 void TrackingWarningWidget::hideAndClear() 0068 { 0069 mBackLists.clear(); 0070 setVisible(false); 0071 } 0072 0073 #include "moc_trackingwarningwidget.cpp"