1/* This file is part of the KDE libraries
2 Copyright (C) 2000, 2009 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kdebugdialog.h"
21#include "klistdebugdialog.h"
22#include <kcmdlineargs.h>
23#include <kaboutdata.h>
24#include <kstandarddirs.h>
25#include <QTextStream>
26#include <klocale.h>
27#include <kdebug.h>
28#include <kuniqueapplication.h>
29#include <kconfig.h>
30#include <kconfiggroup.h>
31
32#include <QFile>
33
34static KAbstractDebugDialog::AreaMap readAreas()
35{
36 KAbstractDebugDialog::AreaMap areas;
37 // Group 0 is not used anymore. kDebug() uses the area named after the appname.
38 //areas.insert( " 0" /*cf rightJustified below*/, "0 (generic)" );
39
40 const QString confAreasFile = KStandardDirs::locate("config", "kdebug.areas");
41 QFile file( confAreasFile );
42 if (!file.open(QIODevice::ReadOnly)) {
43 kWarning() << "Couldn't open" << confAreasFile;
44 } else {
45 QString data;
46
47 QTextStream ts(&file);
48 ts.setCodec( "ISO-8859-1" );
49 while (!ts.atEnd()) {
50 data = ts.readLine().simplified();
51
52 int pos = data.indexOf("#");
53 if ( pos != -1 ) {
54 data.truncate( pos );
55 data = data.simplified();
56 }
57
58 if (data.isEmpty())
59 continue;
60
61 const int space = data.indexOf(' ');
62 if (space == -1)
63 kError() << "No space:" << data << endl;
64
65 bool longOK;
66 unsigned long number = data.left(space).toULong(&longOK);
67 if (!longOK)
68 kError() << "The first part wasn't a number : " << data << endl;
69
70 const QString description = data.mid(space).simplified();
71
72 // In the key, right-align the area number to 6 digits for proper sorting
73 const QString key = QString::number(number).rightJustified(6);
74 areas.insert( key, QString("%1 %2").arg(number).arg(description) );
75 }
76 }
77
78 bool ok;
79#ifndef NDEBUG
80 // Builtin unittest for our expectations of QString::toInt
81 QString("4a").toInt(&ok);
82 Q_ASSERT(!ok);
83#endif
84
85 KConfig config("kdebugrc", KConfig::NoGlobals);
86 Q_FOREACH(const QString& groupName, config.groupList()) {
87 groupName.toInt(&ok);
88 if (ok)
89 continue; // we are not interested in old-style number-only groups
90 areas.insert(groupName, groupName); // ID == description
91 }
92
93 return areas;
94}
95
96int main(int argc, char ** argv)
97{
98 KAboutData data( "kdebugdialog", 0, ki18n( "KDebugDialog"),
99 "1.0", ki18n("A dialog box for setting preferences for debug output"),
100 KAboutData::License_GPL, ki18n("Copyright 1999-2009, David Faure <email>faure@kde.org</email>"));
101 data.addAuthor(ki18n("David Faure"), ki18n("Maintainer"), "faure@kde.org");
102 data.setProgramIconName("tools-report-bug");
103 KCmdLineArgs::init( argc, argv, &data );
104
105 KCmdLineOptions options;
106 options.add("fullmode", ki18n("Show the fully-fledged dialog instead of the default list dialog"));
107 options.add("on <area>", ki18n(/*I18N_NOOP TODO*/ "Turn area on"));
108 options.add("off <area>", ki18n(/*I18N_NOOP TODO*/ "Turn area off"));
109 KCmdLineArgs::addCmdLineOptions( options );
110 KUniqueApplication::addCmdLineOptions();
111 KUniqueApplication app;
112 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
113
114 KAbstractDebugDialog * dialog;
115 if (args->isSet("fullmode")) {
116 dialog = new KDebugDialog(readAreas());
117 } else {
118 KListDebugDialog * listdialog = new KListDebugDialog(readAreas());
119 if (args->isSet("on"))
120 {
121 listdialog->activateArea( args->getOption("on").toUtf8(), true );
122 /*listdialog->save();
123 listdialog->config()->sync();
124 return 0;*/
125 } else if ( args->isSet("off") )
126 {
127 listdialog->activateArea( args->getOption("off").toUtf8(), false );
128 /*listdialog->save();
129 listdialog->config()->sync();
130 return 0;*/
131 }
132 dialog = listdialog;
133 }
134
135 /* Show dialog */
136 int nRet = dialog->exec();
137 if( nRet == QDialog::Accepted )
138 {
139 dialog->save();
140 dialog->config()->sync();
141 }
142 else
143 dialog->config()->markAsClean();
144
145 return 0;
146}
147