kdebugsettingsdialog.cpp 9.82 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
  Copyright (c) 2015-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

  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.

*/

Yuri Chornoivan's avatar
Yuri Chornoivan committed
21
#include "kdebugsettingsdialog.h"
Laurent Montel's avatar
Laurent Montel committed
22
23
#include "customdebugsettingspage.h"
#include "kdeapplicationdebugsettingpage.h"
Laurent Montel's avatar
Laurent Montel committed
24
#include "environmentsettingsrulespage.h"
Laurent Montel's avatar
Laurent Montel committed
25
#include "kdebugsettingsutil.h"
Laurent Montel's avatar
Laurent Montel committed
26
#include "categorywarning.h"
Laurent Montel's avatar
Laurent Montel committed
27
#include "loadcategoriesjob.h"
28
#include "saverulesjob.h"
Laurent Montel's avatar
Laurent Montel committed
29
#include "loadtoolbutton.h"
Laurent Montel's avatar
Laurent Montel committed
30
#include "savetoolbutton.h"
Laurent Montel's avatar
Laurent Montel committed
31
32
#include "loadgroupmenu.h"
#include "kdebugsettings_debug.h"
Laurent Montel's avatar
Laurent Montel committed
33
#include "groupmanagementdialog.h"
Laurent Montel's avatar
Laurent Montel committed
34
35

#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
36
37
#include <KConfigGroup>
#include <KSharedConfig>
38
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
39
#include <QToolButton>
Laurent Montel's avatar
Laurent Montel committed
40

41
#include <QFileDialog>
Laurent Montel's avatar
Laurent Montel committed
42
#include <QDialogButtonBox>
Laurent Montel's avatar
Improve    
Laurent Montel committed
43
#include <QTabWidget>
Laurent Montel's avatar
Laurent Montel committed
44
#include <QVBoxLayout>
45
#include <QPushButton>
Laurent Montel's avatar
Laurent Montel committed
46
47
#include <QDesktopServices>
#include <QUrl>
Laurent Montel's avatar
Laurent Montel committed
48
#include <QInputDialog>
Laurent Montel's avatar
Laurent Montel committed
49
50
51
namespace {
constexpr char KDebugSettingsDialogGroupName[] = "KDebugSettingsDialog";
}
Laurent Montel's avatar
Laurent Montel committed
52
53
54
KDebugSettingsDialog::KDebugSettingsDialog(QWidget *parent)
    : QDialog(parent)
{
55
    auto mainLayout = new QVBoxLayout(this);
Laurent Montel's avatar
Improve    
Laurent Montel committed
56

Laurent Montel's avatar
Laurent Montel committed
57
58
59
60
    mCategoryWarning = new CategoryWarning(this);
    mCategoryWarning->setObjectName(QStringLiteral("categorywarning"));
    mainLayout->addWidget(mCategoryWarning);

Laurent Montel's avatar
Laurent Montel committed
61
    mTabWidget = new QTabWidget(this);
Laurent Montel's avatar
Improve    
Laurent Montel committed
62
63
64
    mTabWidget->setObjectName(QStringLiteral("tabwidget"));
    mainLayout->addWidget(mTabWidget);

Laurent Montel's avatar
Laurent Montel committed
65
    mKdeApplicationSettingsPage = new KDEApplicationDebugSettingPage(this);
Laurent Montel's avatar
Laurent Montel committed
66
67
68
    mKdeApplicationSettingsPage->setObjectName(QStringLiteral("kdeapplicationsettingspage"));
    mCustomSettingsPage = new CustomDebugSettingsPage(this);
    mCustomSettingsPage->setObjectName(QStringLiteral("customsettingspage"));
Laurent Montel's avatar
Laurent Montel committed
69
70
    mEnvironmentSettingsRulesPage = new EnvironmentSettingsRulesPage(this);
    mEnvironmentSettingsRulesPage->setObjectName(QStringLiteral("environmentsettingsrulespage"));
Laurent Montel's avatar
Laurent Montel committed
71
    mTabWidget->addTab(mKdeApplicationSettingsPage, i18n("KDE Application"));
72
    mTabWidget->addTab(mCustomSettingsPage, i18n("Custom Rules"));
Laurent Montel's avatar
Laurent Montel committed
73
    mTabWidget->addTab(mEnvironmentSettingsRulesPage, i18n("Rules Settings With Environment Variable"));
Laurent Montel's avatar
Laurent Montel committed
74
    mTabWidget->setFocusPolicy(Qt::NoFocus);
Laurent Montel's avatar
Laurent Montel committed
75

Laurent Montel's avatar
Laurent Montel committed
76
    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::RestoreDefaults | QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Help | QDialogButtonBox::Apply, this);
Laurent Montel's avatar
Improve    
Laurent Montel committed
77
    buttonBox->setObjectName(QStringLiteral("buttonbox"));
Laurent Montel's avatar
Laurent Montel committed
78

Laurent Montel's avatar
Laurent Montel committed
79
    auto saveAs = new SaveToolButton(this);
Laurent Montel's avatar
Laurent Montel committed
80
    saveAs->setText(i18n("Save As..."));
81
82
    saveAs->setObjectName(QStringLiteral("saveas_button"));
    buttonBox->addButton(saveAs, QDialogButtonBox::ActionRole);
Laurent Montel's avatar
Laurent Montel committed
83
84
    connect(saveAs, &SaveToolButton::saveAsFile, this, &KDebugSettingsDialog::slotSaveAs);
    connect(saveAs, &SaveToolButton::saveAsGroup, this, &KDebugSettingsDialog::slotSaveAsGroup);
Laurent Montel's avatar
Laurent Montel committed
85

Laurent Montel's avatar
Laurent Montel committed
86
87
88
89
90
91
92
    mLoadToolButton = new LoadToolButton(this);
    mLoadToolButton->setObjectName(QStringLiteral("load_button"));
    buttonBox->addButton(mLoadToolButton, QDialogButtonBox::ActionRole);
    connect(mLoadToolButton, &LoadToolButton::loadFromFile, this, &KDebugSettingsDialog::slotLoad);
    connect(mLoadToolButton, &LoadToolButton::loadGroupRequested, this, &KDebugSettingsDialog::slotLoadGroup);
    connect(mLoadToolButton, &LoadToolButton::manageGroupRequested, this, &KDebugSettingsDialog::slotManageGroup);
    connect(this, &KDebugSettingsDialog::updateLoadGroupMenu, mLoadToolButton, &LoadToolButton::updateLoadGroupMenu);
Laurent Montel's avatar
Laurent Montel committed
93

Laurent Montel's avatar
Laurent Montel committed
94
    auto insertCategories = new QPushButton(i18n("Insert..."), this);
95
96
97
98
    insertCategories->setObjectName(QStringLiteral("insert_button"));
    buttonBox->addButton(insertCategories, QDialogButtonBox::ActionRole);
    connect(insertCategories, &QPushButton::clicked, this, &KDebugSettingsDialog::slotInsertCategories);

Laurent Montel's avatar
Laurent Montel committed
99
    connect(buttonBox, &QDialogButtonBox::accepted, this, &KDebugSettingsDialog::slotAccepted);
Laurent Montel's avatar
Improve    
Laurent Montel committed
100
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
Laurent Montel's avatar
Laurent Montel committed
101
    connect(buttonBox, &QDialogButtonBox::helpRequested, this, &KDebugSettingsDialog::slotHelpRequested);
102
    connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &KDebugSettingsDialog::slotApply);
103
    connect(buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &KDebugSettingsDialog::slotRestoreDefault);
Laurent Montel's avatar
Improve    
Laurent Montel committed
104
    mainLayout->addWidget(buttonBox);
Laurent Montel's avatar
Laurent Montel committed
105
    readConfig();
Laurent Montel's avatar
Laurent Montel committed
106
    readQtLoggingFile();
Laurent Montel's avatar
Laurent Montel committed
107
    mKdeApplicationSettingsPage->forceFocus();
Laurent Montel's avatar
Laurent Montel committed
108
109
110
111
}

KDebugSettingsDialog::~KDebugSettingsDialog()
{
Laurent Montel's avatar
Laurent Montel committed
112
113
114
115
116
    saveConfig();
}

void KDebugSettingsDialog::readConfig()
{
Laurent Montel's avatar
Laurent Montel committed
117
    KConfigGroup group(KSharedConfig::openConfig(), QLatin1String(KDebugSettingsDialogGroupName));
Laurent Montel's avatar
Laurent Montel committed
118
119
120
121
    const QSize size = group.readEntry("Size", QSize(600, 400));
    if (size.isValid()) {
        resize(size);
    }
Laurent Montel's avatar
Laurent Montel committed
122
123
}

Laurent Montel's avatar
Laurent Montel committed
124
125
void KDebugSettingsDialog::saveConfig()
{
Laurent Montel's avatar
Laurent Montel committed
126
    KConfigGroup group(KSharedConfig::openConfig(), QLatin1String(KDebugSettingsDialogGroupName));
Laurent Montel's avatar
Laurent Montel committed
127
128
129
    group.writeEntry("Size", size());
    group.sync();
}
Laurent Montel's avatar
Laurent Montel committed
130

Laurent Montel's avatar
Laurent Montel committed
131
132
133
134
void KDebugSettingsDialog::slotLoadGroup(const QString &fullPath)
{
    if (!fullPath.isEmpty()) {
        mLoggings.readCategoriesFiles(fullPath);
Laurent Montel's avatar
Laurent Montel committed
135
        updateLoggingCategories();
Laurent Montel's avatar
Laurent Montel committed
136
137
138
    }
}

Laurent Montel's avatar
Laurent Montel committed
139
140
void KDebugSettingsDialog::readQtLoggingFile()
{
Laurent Montel's avatar
Laurent Montel committed
141
    mLoggings.readQtLoggingFile();
Laurent Montel's avatar
Laurent Montel committed
142
    updateLoggingCategories();
Laurent Montel's avatar
Laurent Montel committed
143
144
}

Laurent Montel's avatar
Laurent Montel committed
145
void KDebugSettingsDialog::updateLoggingCategories()
Laurent Montel's avatar
Laurent Montel committed
146
{
Laurent Montel's avatar
Laurent Montel committed
147
148
    if (!mLoggings.environmentrules().isEmpty()) {
        mEnvironmentSettingsRulesPage->setRules(mLoggings.environmentrules());
149
    }
Laurent Montel's avatar
Laurent Montel committed
150
151
152
    const LoggingCategory::List customCategories = mLoggings.customCategories();
    const LoggingCategory::List qtKdeCategories = mLoggings.qtKdeCategories();
    const bool foundOverrideRule = mLoggings.foundOverrideRule();
153

Laurent Montel's avatar
Laurent Montel committed
154
    mKdeApplicationSettingsPage->fillList(qtKdeCategories);
Laurent Montel's avatar
Laurent Montel committed
155
    mCustomSettingsPage->fillList(customCategories);
Laurent Montel's avatar
Laurent Montel committed
156
157
158
    if (foundOverrideRule) {
        mCategoryWarning->animatedShow();
    }
Laurent Montel's avatar
Laurent Montel committed
159
    mCategoriesList = mLoggings.categoriesList();
Laurent Montel's avatar
Laurent Montel committed
160
}
Laurent Montel's avatar
Laurent Montel committed
161

162
bool KDebugSettingsDialog::saveRules(const QString &path, bool forceSavingAllRules)
Laurent Montel's avatar
Laurent Montel committed
163
{
164
165
166
    SaveRulesJob job;
    job.setFileName(path);
    job.setListCustom(mCustomSettingsPage->rules());
167
    job.setListKde(mKdeApplicationSettingsPage->rules(forceSavingAllRules));
168
    if (!job.start()) {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
169
        KMessageBox::error(this, i18n("\'%1\' cannot be opened. Please verify it.", path));
170
171
172
173
174
175
176
        return false;
    }
    return true;
}

bool KDebugSettingsDialog::saveInQtLogging()
{
177
    return saveRules(KDebugSettingsUtil::qtFileName());
178
179
180
181
}

void KDebugSettingsDialog::slotAccepted()
{
182
183
184
    if (saveInQtLogging()) {
        accept();
    }
Laurent Montel's avatar
Laurent Montel committed
185
}
Laurent Montel's avatar
Laurent Montel committed
186
187
188

void KDebugSettingsDialog::slotHelpRequested()
{
Laurent Montel's avatar
Laurent Montel committed
189
    QDesktopServices::openUrl(QUrl(QStringLiteral("https://doc.qt.io/qt-5/qloggingcategory.html#details")));
Laurent Montel's avatar
Laurent Montel committed
190
}
191

192
193
void KDebugSettingsDialog::slotApply()
{
194
    saveInQtLogging();
195
}
196

197
198
void KDebugSettingsDialog::slotInsertCategories()
{
Laurent Montel's avatar
Laurent Montel committed
199
    const QString path = QFileDialog::getOpenFileName(this, i18n("Insert Categories"), QString(), QStringLiteral("%1 (*.categories)").arg(i18n("Categories Files")));
200
    if (!path.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
201
        const KdeLoggingCategory::List insertCategoriesList = KDebugSettingsUtil::readLoggingCategoriesForInserting(path, mCategoriesList);
Laurent Montel's avatar
Laurent Montel committed
202
        LoggingCategory::List newCategories;
Laurent Montel's avatar
Laurent Montel committed
203
        for (const KdeLoggingCategory &cat : qAsConst(insertCategoriesList)) {
Laurent Montel's avatar
Laurent Montel committed
204
205
            LoggingCategory loggingCat;
            loggingCat.description = cat.description;
Laurent Montel's avatar
Laurent Montel committed
206
            loggingCat.categoryName = cat.categoryName;
207
            loggingCat.defaultSeverityType = KDebugSettingsUtil::convertCategoryTypeFromString(cat.defaultSeverity);
Laurent Montel's avatar
Laurent Montel committed
208
209
210
211
212
            if (loggingCat.isValid()) {
                newCategories.append(loggingCat);
            }
        }
        mKdeApplicationSettingsPage->insertCategories(newCategories);
213
214
215
    }
}

Laurent Montel's avatar
Laurent Montel committed
216
217
void KDebugSettingsDialog::slotSaveAsGroup()
{
Laurent Montel's avatar
Laurent Montel committed
218
    const QString groupPath = LoadGroupMenu::defaultWritableGroupPath();
Laurent Montel's avatar
Laurent Montel committed
219
    const QString name = QInputDialog::getText(this, i18n("Group Name"), i18n("Name"));
Laurent Montel's avatar
Laurent Montel committed
220
221
    const QString trimmedName = name.trimmed();
    if (!trimmedName.isEmpty()) {
222
223
224
225
226
227
        if (mLoadToolButton->groupNames().contains(trimmedName)) {
            KMessageBox::error(this, i18n("%1 is already used as a group name.", trimmedName));
        } else {
            saveRules(groupPath + QLatin1Char('/') + trimmedName, true);
            Q_EMIT updateLoadGroupMenu();
        }
Laurent Montel's avatar
Laurent Montel committed
228
    }
Laurent Montel's avatar
Laurent Montel committed
229
230
}

231
232
void KDebugSettingsDialog::slotSaveAs()
{
Laurent Montel's avatar
Laurent Montel committed
233
    const QString path = QFileDialog::getSaveFileName(this, i18n("Save As"), QString(), i18n("KDebugSettings File (*.kdebugsettingsrules)"));
234
    if (!path.isEmpty()) {
235
        saveRules(path, true);
236
237
    }
}
Laurent Montel's avatar
Laurent Montel committed
238
239
240

void KDebugSettingsDialog::slotLoad()
{
Laurent Montel's avatar
Laurent Montel committed
241
    const QString path = QFileDialog::getOpenFileName(this, i18n("Load Debug Settings Files"), QString(), i18n("KDebugSettings File (*.kdebugsettingsrules)"));
Laurent Montel's avatar
Laurent Montel committed
242
    if (!path.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
243
        mLoggings.readCategoriesFiles(path);
Laurent Montel's avatar
Laurent Montel committed
244
        updateLoggingCategories();
Laurent Montel's avatar
Laurent Montel committed
245
246
    }
}
247
248
249
250
251

void KDebugSettingsDialog::slotRestoreDefault()
{
    mKdeApplicationSettingsPage->restoreToDefault();
}
252
253
254

void KDebugSettingsDialog::slotManageGroup()
{
Laurent Montel's avatar
Laurent Montel committed
255
    auto groupManagementDialog = new GroupManagementDialog(this);
Laurent Montel's avatar
Laurent Montel committed
256
    connect(groupManagementDialog, &GroupManagementDialog::groupsChanged, this, &KDebugSettingsDialog::updateLoadGroupMenu);
Laurent Montel's avatar
Laurent Montel committed
257
258
    groupManagementDialog->exec();
    delete groupManagementDialog;
259
}