loadcategoriesjob.cpp 9.28 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2016-2020 Laurent Montel <montel@kde.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.

*/

#include "loadcategoriesjob.h"
#include "kdebugsettingsutil.h"

LoadCategoriesJob::LoadCategoriesJob()
{
}

void LoadCategoriesJob::setFileName(const QString &filename)
{
    mFileName = filename;
}

bool LoadCategoriesJob::foundOverrideRule() const
{
    return mFoundOverrideRule;
}

Laurent Montel's avatar
Laurent Montel committed
38 39 40 41 42 43 44 45
LoggingCategory::LoggingType updateLoggingType(const LoggingCategory &cat)
{
    if (!cat.enabled) {
        return LoggingCategory::Off;
    }
    return cat.loggingType;
}

46
LoggingCategory::LoggingType canDisplayType(const QMap<KDebugSettingsUtil::LoadLoggingCategory::LogType, KDebugSettingsUtil::LoadLoggingCategory::Status> &types)
Laurent Montel's avatar
Laurent Montel committed
47 48 49 50 51 52 53 54 55
{
    KDebugSettingsUtil::LoadLoggingCategory::Status warning = types.value(KDebugSettingsUtil::LoadLoggingCategory::Warning);
    KDebugSettingsUtil::LoadLoggingCategory::Status debug = types.value(KDebugSettingsUtil::LoadLoggingCategory::Debug);
    KDebugSettingsUtil::LoadLoggingCategory::Status critical = types.value(KDebugSettingsUtil::LoadLoggingCategory::Critical);
    KDebugSettingsUtil::LoadLoggingCategory::Status info = types.value(KDebugSettingsUtil::LoadLoggingCategory::Info);
    KDebugSettingsUtil::LoadLoggingCategory::Status all = types.value(KDebugSettingsUtil::LoadLoggingCategory::All);

    if (all == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
        return LoggingCategory::All;
Laurent Montel's avatar
Laurent Montel committed
56
    } else if (all == KDebugSettingsUtil::LoadLoggingCategory::Disabled) {
Laurent Montel's avatar
Laurent Montel committed
57
        return LoggingCategory::Off;
Laurent Montel's avatar
Laurent Montel committed
58 59 60 61
    } else if (warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && debug == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && info == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
Laurent Montel's avatar
Laurent Montel committed
62
        return LoggingCategory::All;
Laurent Montel's avatar
Laurent Montel committed
63 64 65
    } else if (debug == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
66
        return LoggingCategory::Undefined;
Laurent Montel's avatar
Laurent Montel committed
67 68 69
    } else if (info == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
Laurent Montel's avatar
Laurent Montel committed
70
        return LoggingCategory::Info;
Laurent Montel's avatar
Laurent Montel committed
71 72
    } else if (warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
Laurent Montel's avatar
Laurent Montel committed
73 74 75
        return LoggingCategory::Warning;
    } else if (critical == KDebugSettingsUtil::LoadLoggingCategory::Enabled) {
        return LoggingCategory::Critical;
Laurent Montel's avatar
Laurent Montel committed
76 77 78 79
    } else if (info == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && warning == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
               && debug == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
               && critical == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus) {
Laurent Montel's avatar
Laurent Montel committed
80
        return LoggingCategory::Undefined;
Laurent Montel's avatar
Laurent Montel committed
81 82 83 84
    } else if (warning == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && info == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
               && debug == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
               && critical == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus) {
Laurent Montel's avatar
Laurent Montel committed
85
        return LoggingCategory::Undefined;
Laurent Montel's avatar
Laurent Montel committed
86 87 88 89
    } else if (debug == KDebugSettingsUtil::LoadLoggingCategory::Enabled
               && info == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
               && warning == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus
               && critical == KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus) {
Laurent Montel's avatar
Laurent Montel committed
90 91 92 93 94 95
        return LoggingCategory::Undefined;
    } else {
        return LoggingCategory::Off;
    }
}

96 97 98 99 100
void LoadCategoriesJob::start()
{
    mCustomCategories.clear();
    mQtKdeCategories.clear();
    mFoundOverrideRule = false;
Laurent Montel's avatar
Laurent Montel committed
101
    const int number(mCategories.count());
102 103
    const QList<KDebugSettingsUtil::LoadLoggingCategory> originalQtCategories = KDebugSettingsUtil::readLoggingQtCategories(mFileName);
    QList<KDebugSettingsUtil::LoadLoggingCategory> qtCategories;
Laurent Montel's avatar
Laurent Montel committed
104
    for (KDebugSettingsUtil::LoadLoggingCategory cat : originalQtCategories) { // clazy:exclude=range-loop
Laurent Montel's avatar
Laurent Montel committed
105
        for (const RenameCategory &catRenamed : qAsConst(mRenameCategories)) {
106 107 108 109 110 111 112 113
            if (cat.logName == catRenamed.originalName) {
                cat.logName = catRenamed.newName;
                break;
            }
        }
        qtCategories.append(cat);
    }

Laurent Montel's avatar
Laurent Montel committed
114 115
    for (int i = 0; i < number; ++i) {
        KdeLoggingCategory kdeCat = mCategories.at(i);
116

Laurent Montel's avatar
Laurent Montel committed
117
        bool foundInConfigFile = false;
Laurent Montel's avatar
Laurent Montel committed
118
        for (const KDebugSettingsUtil::LoadLoggingCategory &cat : qAsConst(qtCategories)) {
Laurent Montel's avatar
Laurent Montel committed
119
            if (cat.logName == kdeCat.categoryName) {
Laurent Montel's avatar
Laurent Montel committed
120 121 122 123 124 125
                LoggingCategory tmp;
                LoggingCategory::LoggingType newType = canDisplayType(cat.loggingTypes);
                if (newType != LoggingCategory::Undefined) {
                    tmp.loggingType = canDisplayType(cat.loggingTypes);
                    if (tmp.loggingType == LoggingCategory::Off) {
                        tmp.enabled = false;
Laurent Montel's avatar
Laurent Montel committed
126
                    }
Laurent Montel's avatar
Laurent Montel committed
127
                    tmp.description = kdeCat.description;
Laurent Montel's avatar
Laurent Montel committed
128
                    tmp.categoryName = kdeCat.categoryName;
129 130
                    tmp.defaultSeverityType = KDebugSettingsUtil::convertCategoryTypeFromString(kdeCat.defaultSeverity);
                    tmp.identifierName = kdeCat.identifierName;
Laurent Montel's avatar
Laurent Montel committed
131 132 133 134 135

                    mQtKdeCategories.append(tmp);
                    foundInConfigFile = true;
                    qtCategories.removeAll(cat);
                    break;
136 137
                }
            }
Laurent Montel's avatar
Laurent Montel committed
138 139
            if (cat.logName == QLatin1String("*")) {
                mFoundOverrideRule = true;
140
            }
Laurent Montel's avatar
Laurent Montel committed
141
        }
Laurent Montel's avatar
Laurent Montel committed
142 143 144
        if (!foundInConfigFile) {
            LoggingCategory tmp;
            tmp.description = kdeCat.description;
Laurent Montel's avatar
Laurent Montel committed
145
            tmp.categoryName = kdeCat.categoryName;
146 147 148
            tmp.loggingType = KDebugSettingsUtil::convertCategoryTypeFromString(kdeCat.defaultSeverity);
            tmp.defaultSeverityType = KDebugSettingsUtil::convertCategoryTypeFromString(kdeCat.defaultSeverity);
            tmp.identifierName = kdeCat.identifierName;
Laurent Montel's avatar
Laurent Montel committed
149 150 151
            mQtKdeCategories.append(tmp);
        }
    }
Laurent Montel's avatar
Laurent Montel committed
152

Laurent Montel's avatar
Laurent Montel committed
153
    //qDebug()<<" KEEP "<< qtCategories.count();
Laurent Montel's avatar
Laurent Montel committed
154
    for (const KDebugSettingsUtil::LoadLoggingCategory &cat : qAsConst(qtCategories)) {
Laurent Montel's avatar
Laurent Montel committed
155 156 157 158 159
        QMapIterator<KDebugSettingsUtil::LoadLoggingCategory::LogType, KDebugSettingsUtil::LoadLoggingCategory::Status> i(cat.loggingTypes);
        while (i.hasNext()) {
            i.next();
            if (i.value() != KDebugSettingsUtil::LoadLoggingCategory::UnknownStatus) {
                LoggingCategory tmp;
Laurent Montel's avatar
Laurent Montel committed
160
                tmp.categoryName = cat.logName;
Laurent Montel's avatar
Laurent Montel committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
                switch (i.key()) {
                case KDebugSettingsUtil::LoadLoggingCategory::Unknown:
                    tmp.loggingType = LoggingCategory::Undefined;
                    break;
                case KDebugSettingsUtil::LoadLoggingCategory::Off:
                    tmp.loggingType = LoggingCategory::Off;
                    break;
                case KDebugSettingsUtil::LoadLoggingCategory::Info:
                    tmp.loggingType = LoggingCategory::Info;
                    break;
                case KDebugSettingsUtil::LoadLoggingCategory::Warning:
                    tmp.loggingType = LoggingCategory::Warning;
                    break;
                case KDebugSettingsUtil::LoadLoggingCategory::Debug:
                    tmp.loggingType = LoggingCategory::Debug;
                    break;
                case KDebugSettingsUtil::LoadLoggingCategory::Critical:
                    tmp.loggingType = LoggingCategory::Critical;
                    break;
                case KDebugSettingsUtil::LoadLoggingCategory::All:
                    tmp.loggingType = LoggingCategory::All;
                    break;
Laurent Montel's avatar
Laurent Montel committed
183
                }
Laurent Montel's avatar
Laurent Montel committed
184 185
                tmp.enabled = (i.value() == KDebugSettingsUtil::LoadLoggingCategory::Enabled);
                mCustomCategories.append(tmp);
Laurent Montel's avatar
Laurent Montel committed
186
            }
187 188 189 190 191 192 193 194 195
        }
    }
}

void LoadCategoriesJob::setCategories(const KdeLoggingCategory::List &categories)
{
    mCategories = categories;
}

Laurent Montel's avatar
Laurent Montel committed
196 197 198 199 200
void LoadCategoriesJob::setRenamedCategories(const RenameCategory::List &renameCategories)
{
    mRenameCategories = renameCategories;
}

201 202 203 204 205 206 207 208 209
LoggingCategory::List LoadCategoriesJob::customCategories() const
{
    return mCustomCategories;
}

LoggingCategory::List LoadCategoriesJob::qtKdeCategories() const
{
    return mQtKdeCategories;
}