File indexing completed on 2024-12-22 05:28:43

0001 /*
0002     SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "changedebugmodejob.h"
0009 #include "kdebugsettingscore_debug.h"
0010 #include "kdebugsettingsloadingcategories.h"
0011 #include "kdebugsettingsutil.h"
0012 #include "saverulesjob.h"
0013 
0014 ChangeDebugModeJob::ChangeDebugModeJob() = default;
0015 
0016 ChangeDebugModeJob::~ChangeDebugModeJob() = default;
0017 
0018 bool ChangeDebugModeJob::canStart() const
0019 {
0020     if (debugModeIsValid(mDebugMode)) {
0021         return true;
0022     }
0023     if (mWithoutArguments) {
0024         return true;
0025     }
0026     if (mLoggingCategoriesName.isEmpty()) {
0027         return false;
0028     }
0029     return true;
0030 }
0031 
0032 bool ChangeDebugModeJob::start()
0033 {
0034     if (!canStart()) {
0035         return false;
0036     }
0037     KDebugSettingsLoadingCategories loading;
0038     loading.readQtLoggingFile();
0039     const LoggingCategory::LoggingType type = convertDebugModeToLoggingType(mDebugMode);
0040     LoggingCategory::List customCategories = loading.customCategories();
0041     for (int i = 0, total = customCategories.count(); i < total; ++i) {
0042         LoggingCategory cat = customCategories[i];
0043         if (mWithoutArguments) {
0044             cat.loggingType = type;
0045             customCategories[i] = cat;
0046         } else {
0047             for (const QString &categoryName : std::as_const(mLoggingCategoriesName)) {
0048                 if (cat.categoryName.contains(categoryName)) {
0049                     cat.loggingType = type;
0050                     customCategories[i] = cat;
0051                 }
0052             }
0053         }
0054     }
0055     LoggingCategory::List qtKdeCategories = loading.qtKdeCategories();
0056     for (int i = 0; i < qtKdeCategories.count(); ++i) {
0057         LoggingCategory cat = qtKdeCategories[i];
0058         if (mWithoutArguments) {
0059             cat.loggingType = type;
0060             qtKdeCategories[i] = cat;
0061         } else {
0062             for (const QString &categoryName : std::as_const(mLoggingCategoriesName)) {
0063                 if (cat.categoryName.contains(categoryName)) {
0064                     cat.loggingType = type;
0065                     qtKdeCategories[i] = cat;
0066                 }
0067             }
0068         }
0069     }
0070     SaveRulesJob job;
0071     job.setFileName(KDebugSettingsUtil::qtFileName());
0072     job.setListKde(qtKdeCategories);
0073     job.setListCustom(customCategories);
0074     if (!job.start()) {
0075         qCWarning(KDEBUGSETTINGSCORE_LOG) << "Impossible to save in file " << job.fileName();
0076     }
0077     return true;
0078 }
0079 
0080 LoggingCategory::LoggingType ChangeDebugModeJob::convertDebugModeToLoggingType(const QString &value) const
0081 {
0082     if (value == QLatin1StringView("Full")) {
0083         return LoggingCategory::LoggingType::All;
0084     } else if (value == QLatin1StringView("Info")) {
0085         return LoggingCategory::LoggingType::Info;
0086     } else if (value == QLatin1StringView("Warning")) {
0087         return LoggingCategory::LoggingType::Warning;
0088     } else if (value == QLatin1StringView("Critical")) {
0089         return LoggingCategory::LoggingType::Critical;
0090     } else if (value == QLatin1StringView("Off")) {
0091         return LoggingCategory::LoggingType::Off;
0092     }
0093     return LoggingCategory::LoggingType::Undefined;
0094 }
0095 
0096 void ChangeDebugModeJob::setWithoutArguments(bool b)
0097 {
0098     mWithoutArguments = b;
0099 }
0100 
0101 bool ChangeDebugModeJob::withoutArguments() const
0102 {
0103     return mWithoutArguments;
0104 }
0105 
0106 void ChangeDebugModeJob::setDebugMode(const QString &mode)
0107 {
0108     mDebugMode = mode;
0109 }
0110 
0111 QString ChangeDebugModeJob::debugMode() const
0112 {
0113     return mDebugMode;
0114 }
0115 
0116 QStringList ChangeDebugModeJob::loggingCategoriesName() const
0117 {
0118     return mLoggingCategoriesName;
0119 }
0120 
0121 void ChangeDebugModeJob::setLoggingCategoriesName(const QStringList &loggingCategoryName)
0122 {
0123     mLoggingCategoriesName = loggingCategoryName;
0124 }
0125 
0126 bool ChangeDebugModeJob::debugModeIsValid(const QString &value) const
0127 {
0128     if (convertDebugModeToLoggingType(value) == LoggingCategory::LoggingType::Undefined) {
0129         return false;
0130     }
0131     return true;
0132 }