File indexing completed on 2024-03-24 04:02:23

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1999 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "kbugreport.h"
0009 
0010 #include <QDesktopServices>
0011 #include <QDialogButtonBox>
0012 #include <QHBoxLayout>
0013 #include <QLabel>
0014 #include <QPushButton>
0015 #include <QUrl>
0016 #include <QUrlQuery>
0017 
0018 #include <KAboutData>
0019 #include <KConfig>
0020 #include <KLocalizedString>
0021 #include <KMessageBox>
0022 #include <KMessageDialog>
0023 #include <KTitleWidget>
0024 
0025 #include "config-xmlgui.h"
0026 #include "systeminformation_p.h"
0027 #include <kxmlgui_version.h>
0028 
0029 class KBugReportPrivate
0030 {
0031 public:
0032     KBugReportPrivate(KBugReport *qq)
0033         : q(qq)
0034     {
0035     }
0036 
0037     enum BugDestination {
0038         BugsKdeOrg,
0039         CustomUrl,
0040     };
0041 
0042     // Update the url to match the current OS, selected app, etc
0043     void updateUrl();
0044 
0045     KBugReport *const q;
0046 
0047     QLabel *m_version = nullptr;
0048     QString m_strVersion;
0049 
0050     QString lastError;
0051     QString kde_version;
0052     QString appname;
0053     QString os;
0054     QUrl url;
0055     BugDestination bugDestination = KBugReportPrivate::CustomUrl;
0056 };
0057 
0058 KBugReport::KBugReport(const KAboutData &aboutData, QWidget *_parent)
0059     : QDialog(_parent)
0060     , d(new KBugReportPrivate(this))
0061 {
0062     setWindowTitle(i18nc("@title:window", "Submit Bug Report"));
0063 
0064     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0065     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0066     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0067     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0068 
0069     const QString bugAddress = aboutData.bugAddress();
0070     if (bugAddress == QLatin1String("submit@bugs.kde.org")) {
0071         // This is a core KDE application -> redirect to the web form
0072         d->bugDestination = KBugReportPrivate::BugsKdeOrg;
0073     } else if (!QUrl(bugAddress).scheme().isEmpty()) {
0074         // The bug reporting address is a URL -> redirect to that
0075         d->bugDestination = KBugReportPrivate::CustomUrl;
0076     }
0077 
0078     KGuiItem::assign(buttonBox->button(QDialogButtonBox::Cancel), KStandardGuiItem::close());
0079 
0080     QLabel *tmpLabel;
0081     QVBoxLayout *lay = new QVBoxLayout(this);
0082 
0083     KTitleWidget *title = new KTitleWidget(this);
0084     title->setText(i18n("Submit Bug Report"));
0085     title->setIconSize(QSize(32, 32));
0086     title->setIcon(QIcon::fromTheme(QStringLiteral("tools-report-bug")));
0087     lay->addWidget(title);
0088 
0089     QGridLayout *glay = new QGridLayout();
0090     lay->addLayout(glay);
0091 
0092     int row = 0;
0093 
0094     // Program name
0095     QString qwtstr =
0096         i18n("The application for which you wish to submit a bug report - if incorrect, please use the Report Bug menu item of the correct application");
0097     tmpLabel = new QLabel(i18n("Application: "), this);
0098     glay->addWidget(tmpLabel, row, 0);
0099     tmpLabel->setWhatsThis(qwtstr);
0100     QLabel *appLabel = new QLabel(this);
0101     d->appname = aboutData.productName();
0102     appLabel->setText(d->appname);
0103     glay->addWidget(appLabel, row, 1);
0104     tmpLabel->setWhatsThis(qwtstr);
0105 
0106     // Version
0107     qwtstr = i18n("The version of this application - please make sure that no newer version is available before sending a bug report");
0108     tmpLabel = new QLabel(i18n("Version:"), this);
0109     glay->addWidget(tmpLabel, ++row, 0);
0110     tmpLabel->setWhatsThis(qwtstr);
0111     d->m_strVersion = aboutData.version();
0112     if (d->m_strVersion.isEmpty()) {
0113         d->m_strVersion = i18n("no version set (programmer error)");
0114     }
0115     d->kde_version = QStringLiteral(KXMLGUI_VERSION_STRING);
0116     if (d->bugDestination != KBugReportPrivate::BugsKdeOrg) {
0117         d->m_strVersion += QLatin1Char(' ') + d->kde_version;
0118     }
0119     d->m_version = new QLabel(d->m_strVersion, this);
0120     d->m_version->setTextInteractionFlags(Qt::TextBrowserInteraction);
0121     // glay->addWidget( d->m_version, row, 1 );
0122     glay->addWidget(d->m_version, row, 1, 1, 2);
0123     d->m_version->setWhatsThis(qwtstr);
0124 
0125     tmpLabel = new QLabel(i18n("OS:"), this);
0126     glay->addWidget(tmpLabel, ++row, 0);
0127 
0128 #ifdef Q_OS_WINDOWS
0129     d->os = i18nc("%1 is the operating system name, e.g. 'Windows 10', %2 is the CPU architecture, e.g. 'x86_64'",
0130                   "%1 (%2)",
0131                   QSysInfo::prettyProductName(),
0132                   QSysInfo::currentCpuArchitecture());
0133 #else
0134     if (QSysInfo::productVersion() != QLatin1String("unknown")) {
0135         d->os = i18nc(
0136             "%1 is the operating system name, e.g. 'Fedora Linux', %2 is the operating system version, e.g. '35', %3 is the CPU architecture, e.g. 'x86_64'",
0137             "%1 %2 (%3)",
0138             QSysInfo::prettyProductName(),
0139             QSysInfo::productVersion(),
0140             QSysInfo::currentCpuArchitecture());
0141     } else {
0142         d->os = i18nc("%1 is the operating system name, e.g. 'Fedora Linux', %2 is the CPU architecture, e.g. 'x86_64'",
0143                       "%1 (%2)",
0144                       QSysInfo::prettyProductName(),
0145                       QSysInfo::currentCpuArchitecture());
0146     }
0147 #endif
0148 
0149     tmpLabel = new QLabel(d->os, this);
0150     tmpLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
0151     glay->addWidget(tmpLabel, row, 1, 1, 2);
0152     // Point to the web form
0153 
0154     QString text;
0155     if (d->bugDestination == KBugReportPrivate::BugsKdeOrg) {
0156         text = i18n(
0157             "<qt>To submit a bug report, click on the button below. This will open a web browser "
0158             "window on <a href=\"https://bugs.kde.org\">https://bugs.kde.org</a> where you will find "
0159             "a form to fill in. The information displayed above will be transferred to that server.</qt>");
0160         d->updateUrl();
0161     } else {
0162         text = i18n(
0163             "<qt>To submit a bug report, click on the button below. This will open a web browser "
0164             "window on <a href=\"%1\">%2</a>.</qt>",
0165             bugAddress,
0166             bugAddress);
0167         d->url = QUrl(bugAddress);
0168     }
0169 
0170     lay->addSpacing(10);
0171     QLabel *label = new QLabel(text, this);
0172     label->setOpenExternalLinks(true);
0173     label->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard);
0174     label->setWordWrap(true);
0175     lay->addWidget(label);
0176     lay->addSpacing(10);
0177 
0178     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0179     if (d->bugDestination == KBugReportPrivate::BugsKdeOrg) {
0180         okButton->setText(i18nc("@action:button", "&Launch Bug Report Wizard"));
0181     } else {
0182         okButton->setText(i18nc("@action:button", "&Submit Bug Report"));
0183     }
0184     okButton->setIcon(QIcon::fromTheme(QStringLiteral("tools-report-bug")));
0185 
0186     lay->addWidget(buttonBox);
0187     setMinimumHeight(sizeHint().height() + 20); // WORKAROUND: prevent "cropped" qcombobox
0188 }
0189 
0190 KBugReport::~KBugReport() = default;
0191 
0192 void KBugReportPrivate::updateUrl()
0193 {
0194     url = QUrl(QStringLiteral("https://bugs.kde.org/enter_bug.cgi"));
0195     QUrlQuery query;
0196     query.addQueryItem(QStringLiteral("format"), QStringLiteral("guided")); // use the guided form
0197 
0198     // the string format is product/component, where component is optional
0199     QStringList list = appname.split(QLatin1Char('/'));
0200     query.addQueryItem(QStringLiteral("product"), list[0]);
0201     if (list.size() == 2) {
0202         query.addQueryItem(QStringLiteral("component"), list[1]);
0203     }
0204 
0205     query.addQueryItem(QStringLiteral("version"), m_strVersion);
0206     url.setQuery(query);
0207 
0208     // TODO: guess and fill OS(sys_os) and Platform(rep_platform) fields
0209 }
0210 
0211 void KBugReport::accept()
0212 {
0213     QDesktopServices::openUrl(d->url);
0214     QDialog::accept();
0215 }
0216 
0217 #include "moc_kbugreport.cpp"