File indexing completed on 2022-11-29 20:24:02

0001 /*
0002     SPDX-FileCopyrightText: 2016-2022 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "loadcategoriesjob.h"
0009 #include "kdebugsettingsutil.h"
0010 
0011 LoadCategoriesJob::LoadCategoriesJob() = default;
0012 
0013 void LoadCategoriesJob::setFileName(const QString &filename)
0014 {
0015     mFileName = filename;
0016 }
0017 
0018 bool LoadCategoriesJob::foundOverrideRule() const
0019 {
0020     return mFoundOverrideRule;
0021 }
0022 
0023 LoggingCategory::LoggingType updateLoggingType(const LoggingCategory &cat)
0024 {
0025     if (!cat.enabled) {
0026         return LoggingCategory::Off;
0027     }
0028     return cat.loggingType;
0029 }
0030 
0031 LoggingCategory::LoggingType
0032 canDisplayType(const QMap<KDebugSettingsUtil::LoadLoggingCategory::LogType, KDebugSettingsUtil::LoadLoggingCategory::Status> &types)
0033 {
0034     KDebugSettingsUtil::LoadLoggingCategory::Status warning = types.value(KDebugSettingsUtil::LoadLoggingCategory::Warning);
0035     KDebugSettingsUtil::LoadLoggingCategory::Status debug = types.value(KDebugSettingsUtil::LoadLoggingCategory::Debug);
0036     KDebugSettingsUtil::LoadLoggingCategory::Status critical = types.value(KDebugSettingsUtil::LoadLoggingCategory::Critical);
0037     KDebugSettingsUtil::LoadLoggingCategory::Status info = types.value(KDebugSettingsUtil::LoadLoggingCategory::Info);
0038     KDebugSettingsUtil::LoadLoggingCategory::Status all = types.value(KDebugSettingsUtil::LoadLoggingCategory::All);
0039 
0040     if (all == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
0041         return LoggingCategory::All;
0042     } else if (all == KDebugSettingsUtil::LoadLoggingCategory::Disabled) {
0043         return LoggingCategory::Off;
0044     } else if (warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled && debug == KDebugSettingsUtil::LoadLoggingCategory::Enabled
0045                && critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled && info == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
0046         return LoggingCategory::All;
0047     } else if (debug == KDebugSettingsUtil::LoadLoggingCategory::Enabled && warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled
0048                && critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
0049         return LoggingCategory::Undefined;
0050     } else if (info == KDebugSettingsUtil::LoadLoggingCategory::Enabled && warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled
0051                && critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
0052         return LoggingCategory::Info;
0053     } else if (warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled && critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
0054         return LoggingCategory::Warning;
0055     } else if (critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
0056         return LoggingCategory::Critical;
0057     } else if (info == KDebugSettingsUtil::LoadLoggingCategory::Enabled && warning == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
0058                && debug == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus && critical == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus) {
0059         return LoggingCategory::Undefined;
0060     } else if (warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled && info == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
0061                && debug == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus && critical == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus) {
0062         return LoggingCategory::Undefined;
0063     } else if (debug == KDebugSettingsUtil::LoadLoggingCategory::Enabled && info == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
0064                && warning == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus && critical == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus) {
0065         return LoggingCategory::Undefined;
0066     } else {
0067         return LoggingCategory::Off;
0068     }
0069 }
0070 
0071 void LoadCategoriesJob::start()
0072 {
0073     mCustomCategories.clear();
0074     mQtKdeCategories.clear();
0075     mFoundOverrideRule = false;
0076     const int number(mCategories.count());
0077     const QList<KDebugSettingsUtil::LoadLoggingCategory> originalQtCategories = KDebugSettingsUtil::readLoggingQtCategories(mFileName);
0078     QList<KDebugSettingsUtil::LoadLoggingCategory> qtCategories;
0079     for (KDebugSettingsUtil::LoadLoggingCategory cat : originalQtCategories) { // clazy:exclude=range-loop-reference
0080         for (const RenameCategory &catRenamed : std::as_const(mRenameCategories)) {
0081             if (cat.logName == catRenamed.originalName) {
0082                 cat.logName = catRenamed.newName;
0083                 break;
0084             }
0085         }
0086         qtCategories.append(cat);
0087     }
0088 
0089     for (int i = 0; i < number; ++i) {
0090         KdeLoggingCategory kdeCat = mCategories.at(i);
0091 
0092         bool foundInConfigFile = false;
0093         for (const KDebugSettingsUtil::LoadLoggingCategory &cat : std::as_const(qtCategories)) {
0094             if (cat.logName == kdeCat.categoryName) {
0095                 LoggingCategory tmp;
0096                 LoggingCategory::LoggingType newType = canDisplayType(cat.loggingTypes);
0097                 if (newType != LoggingCategory::Undefined) {
0098                     tmp.loggingType = canDisplayType(cat.loggingTypes);
0099                     if (tmp.loggingType == LoggingCategory::Off) {
0100                         tmp.enabled = false;
0101                     }
0102                     tmp.description = kdeCat.description;
0103                     tmp.categoryName = kdeCat.categoryName;
0104                     tmp.defaultSeverityType = KDebugSettingsUtil::convertCategoryTypeFromString(kdeCat.defaultSeverity);
0105                     tmp.identifierName = kdeCat.identifierName;
0106 
0107                     mQtKdeCategories.append(tmp);
0108                     foundInConfigFile = true;
0109                     qtCategories.removeAll(cat);
0110                     break;
0111                 }
0112             }
0113             if (cat.logName == QLatin1String("*")) {
0114                 mFoundOverrideRule = true;
0115             }
0116         }
0117         if (!foundInConfigFile) {
0118             LoggingCategory tmp;
0119             tmp.description = kdeCat.description;
0120             tmp.categoryName = kdeCat.categoryName;
0121             tmp.loggingType = KDebugSettingsUtil::convertCategoryTypeFromString(kdeCat.defaultSeverity);
0122             tmp.defaultSeverityType = KDebugSettingsUtil::convertCategoryTypeFromString(kdeCat.defaultSeverity);
0123             tmp.identifierName = kdeCat.identifierName;
0124             mQtKdeCategories.append(tmp);
0125         }
0126     }
0127 
0128     // qDebug()<<" KEEP "<< qtCategories.count();
0129     for (const KDebugSettingsUtil::LoadLoggingCategory &cat : std::as_const(qtCategories)) {
0130         QMapIterator<KDebugSettingsUtil::LoadLoggingCategory::LogType, KDebugSettingsUtil::LoadLoggingCategory::Status> i(cat.loggingTypes);
0131         while (i.hasNext()) {
0132             i.next();
0133             if (i.value() != KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus) {
0134                 LoggingCategory tmp;
0135                 tmp.categoryName = cat.logName;
0136                 switch (i.key()) {
0137                 case KDebugSettingsUtil::LoadLoggingCategory::Unknown:
0138                     tmp.loggingType = LoggingCategory::Undefined;
0139                     break;
0140                 case KDebugSettingsUtil::LoadLoggingCategory::Off:
0141                     tmp.loggingType = LoggingCategory::Off;
0142                     break;
0143                 case KDebugSettingsUtil::LoadLoggingCategory::Info:
0144                     tmp.loggingType = LoggingCategory::Info;
0145                     break;
0146                 case KDebugSettingsUtil::LoadLoggingCategory::Warning:
0147                     tmp.loggingType = LoggingCategory::Warning;
0148                     break;
0149                 case KDebugSettingsUtil::LoadLoggingCategory::Debug:
0150                     tmp.loggingType = LoggingCategory::Debug;
0151                     break;
0152                 case KDebugSettingsUtil::LoadLoggingCategory::Critical:
0153                     tmp.loggingType = LoggingCategory::Critical;
0154                     break;
0155                 case KDebugSettingsUtil::LoadLoggingCategory::All:
0156                     tmp.loggingType = LoggingCategory::All;
0157                     break;
0158                 }
0159                 tmp.enabled = (i.value() == KDebugSettingsUtil::LoadLoggingCategory::Enabled);
0160                 mCustomCategories.append(tmp);
0161             }
0162         }
0163     }
0164 }
0165 
0166 void LoadCategoriesJob::setCategories(const KdeLoggingCategory::List &categories)
0167 {
0168     mCategories = categories;
0169 }
0170 
0171 void LoadCategoriesJob::setRenamedCategories(const RenameCategory::List &renameCategories)
0172 {
0173     mRenameCategories = renameCategories;
0174 }
0175 
0176 LoggingCategory::List LoadCategoriesJob::customCategories() const
0177 {
0178     return mCustomCategories;
0179 }
0180 
0181 LoggingCategory::List LoadCategoriesJob::qtKdeCategories() const
0182 {
0183     return mQtKdeCategories;
0184 }