File indexing completed on 2024-05-19 05:16:11

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "infodialog.h"
0008 
0009 #include "migration_debug.h"
0010 #include <QIcon>
0011 
0012 #include <QApplication>
0013 #include <QDialogButtonBox>
0014 #include <QHBoxLayout>
0015 #include <QLabel>
0016 #include <QListWidget>
0017 #include <QProgressBar>
0018 #include <QPushButton>
0019 #include <QScrollBar>
0020 #include <QVBoxLayout>
0021 
0022 enum {
0023     // The max value of the scrollbar. Don't change this without making the kmail
0024     // migrator use this. It still uses hardcoded "100".
0025     MAX_PROGRESS = 100
0026 };
0027 
0028 bool InfoDialog::mError = false;
0029 
0030 InfoDialog::InfoDialog(bool closeWhenDone)
0031     : mButtonBox(new QDialogButtonBox(QDialogButtonBox::Close, this))
0032     , mList(new QListWidget(this))
0033     , mStatusLabel(new QLabel(this))
0034     , mProgressBar(new QProgressBar(this))
0035     , mCloseWhenDone(closeWhenDone)
0036 {
0037     setAttribute(Qt::WA_DeleteOnClose);
0038     auto mainLayout = new QVBoxLayout(this);
0039 
0040     mList->setMinimumWidth(640);
0041     mainLayout->addWidget(mList);
0042 
0043     auto statusLayout = new QHBoxLayout;
0044     mainLayout->addLayout(statusLayout);
0045 
0046     mStatusLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
0047     statusLayout->addWidget(mStatusLabel);
0048 
0049     mProgressBar->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
0050     mProgressBar->setMinimumWidth(200);
0051     statusLayout->addWidget(mProgressBar);
0052 
0053     connect(mButtonBox, &QDialogButtonBox::accepted, this, &InfoDialog::accept);
0054     connect(mButtonBox, &QDialogButtonBox::rejected, this, &InfoDialog::reject);
0055     mButtonBox->button(QDialogButtonBox::Close)->setEnabled(false);
0056     mainLayout->addWidget(mButtonBox);
0057 }
0058 
0059 InfoDialog::~InfoDialog() = default;
0060 
0061 static KMigratorBase::MessageType convertType(MigratorBase::MessageType type)
0062 {
0063     switch (type) {
0064     case MigratorBase::Success:
0065         return KMigratorBase::Success;
0066     case MigratorBase::Error:
0067         return KMigratorBase::Error;
0068     case MigratorBase::Skip:
0069         return KMigratorBase::Skip;
0070     case MigratorBase::Warning:
0071         return KMigratorBase::Warning;
0072     case MigratorBase::Info:
0073         return KMigratorBase::Info;
0074     }
0075     return KMigratorBase::Info;
0076 }
0077 
0078 void InfoDialog::message(MigratorBase::MessageType type, const QString &msg)
0079 {
0080     message(convertType(type), msg);
0081 }
0082 
0083 void InfoDialog::message(KMigratorBase::MessageType type, const QString &msg)
0084 {
0085     bool autoScroll = mAutoScrollList;
0086 
0087     auto item = new QListWidgetItem(msg, mList);
0088     switch (type) {
0089     case KMigratorBase::Success:
0090         item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok-apply")));
0091         mChange = true;
0092         qCDebug(MIGRATION_LOG) << msg;
0093         break;
0094     case KMigratorBase::Skip:
0095         item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok")));
0096         qCDebug(MIGRATION_LOG) << msg;
0097         break;
0098     case KMigratorBase::Info:
0099         item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-information")));
0100         qCDebug(MIGRATION_LOG) << msg;
0101         break;
0102     case KMigratorBase::Warning:
0103         item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-warning")));
0104         qCDebug(MIGRATION_LOG) << msg;
0105         break;
0106     case KMigratorBase::Error: {
0107         item->setIcon(QIcon::fromTheme(QStringLiteral("dialog-error")));
0108         QFont currentFont = font();
0109         currentFont.setBold(true);
0110         item->setFont(currentFont);
0111         mError = true;
0112         qCCritical(MIGRATION_LOG) << msg;
0113         break;
0114     }
0115     default:
0116         qCCritical(MIGRATION_LOG) << "WTF?";
0117     }
0118 
0119     mAutoScrollList = autoScroll;
0120 
0121     if (autoScroll) {
0122         mList->scrollToItem(item);
0123     }
0124 }
0125 
0126 void InfoDialog::migratorAdded()
0127 {
0128     ++mMigratorCount;
0129     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
0130 }
0131 
0132 void InfoDialog::migratorDone()
0133 {
0134     QApplication::restoreOverrideCursor();
0135 
0136     --mMigratorCount;
0137     if (mMigratorCount == 0) {
0138         mButtonBox->button(QDialogButtonBox::Close)->setEnabled(true);
0139         status(QString());
0140         if (mCloseWhenDone && !hasError() && !hasChange()) {
0141             accept();
0142         }
0143     }
0144 }
0145 
0146 void InfoDialog::status(const QString &msg)
0147 {
0148     mStatusLabel->setText(msg);
0149     if (msg.isEmpty()) {
0150         progress(0, MAX_PROGRESS, MAX_PROGRESS);
0151         mProgressBar->setFormat(QString());
0152     }
0153 }
0154 
0155 void InfoDialog::progress(int value)
0156 {
0157     mProgressBar->setFormat(QStringLiteral("%p%"));
0158     mProgressBar->setValue(value);
0159 }
0160 
0161 void InfoDialog::progress(int min, int max, int value)
0162 {
0163     mProgressBar->setFormat(QStringLiteral("%p%"));
0164     mProgressBar->setRange(min, max);
0165     mProgressBar->setValue(value);
0166 }
0167 
0168 void InfoDialog::scrollBarMoved(int value)
0169 {
0170     mAutoScrollList = (value == mList->verticalScrollBar()->maximum());
0171 }
0172 
0173 #include "moc_infodialog.cpp"