File indexing completed on 2024-04-21 14:55:21

0001 /* This file is part of the KDE libraries
0002    Copyright (C) 2000, 2009 David Faure <faure@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kdebugdialog.h"
0021 #include "klistdebugdialog.h"
0022 #include <kcmdlineargs.h>
0023 #include <k4aboutdata.h>
0024 #include <kdelibs4configmigrator.h>
0025 
0026 #include <QTextStream>
0027 #include <klocale.h>
0028 #include <kdebug.h>
0029 #include <kuniqueapplication.h>
0030 #include <kconfig.h>
0031 #include <kconfiggroup.h>
0032 
0033 #include <QFile>
0034 #include <QStandardPaths>
0035 
0036 static KAbstractDebugDialog::AreaMap readAreas()
0037 {
0038     KAbstractDebugDialog::AreaMap areas;
0039     // Group 0 is not used anymore. kDebug() uses the area named after the appname.
0040     //areas.insert( "      0" /*cf rightJustified below*/, "0 (generic)" );
0041 
0042     const QString confAreasFile = QStandardPaths::locate(QStandardPaths::ConfigLocation, "kdebug.areas");
0043     QFile file( confAreasFile );
0044     if (!file.open(QIODevice::ReadOnly)) {
0045         kWarning() << "Couldn't open" << confAreasFile;
0046     } else {
0047         QString data;
0048 
0049         QTextStream ts(&file);
0050         ts.setCodec( "ISO-8859-1" );
0051         while (!ts.atEnd()) {
0052             data = ts.readLine().simplified();
0053 
0054             int pos = data.indexOf("#");
0055             if ( pos != -1 ) {
0056                 data.truncate( pos );
0057                 data = data.simplified();
0058             }
0059 
0060             if (data.isEmpty())
0061                 continue;
0062 
0063             const int space = data.indexOf(' ');
0064             if (space == -1)
0065                 kError() << "No space:" << data << endl;
0066 
0067             bool longOK;
0068             unsigned long number = data.left(space).toULong(&longOK);
0069             if (!longOK)
0070                 kError() << "The first part wasn't a number : " << data << endl;
0071 
0072             const QString description = data.mid(space).simplified();
0073 
0074             // In the key, right-align the area number to 6 digits for proper sorting
0075             const QString key = QString::number(number).rightJustified(6);
0076             areas.insert( key, QString("%1 %2").arg(number).arg(description) );
0077         }
0078     }
0079 
0080     bool ok;
0081 #ifndef NDEBUG
0082     // Builtin unittest for our expectations of QString::toInt
0083     QString("4a").toInt(&ok);
0084     Q_ASSERT(!ok);
0085 #endif
0086 
0087     KConfig config("kdebugrc", KConfig::NoGlobals);
0088     Q_FOREACH(const QString& groupName, config.groupList()) {
0089         groupName.toInt(&ok);
0090         if (ok)
0091             continue; // we are not interested in old-style number-only groups
0092         areas.insert(groupName, groupName); // ID == description
0093     }
0094 
0095     return areas;
0096 }
0097 
0098 int main(int argc, char ** argv)
0099 {
0100     K4AboutData data( "kdebugdialog5", "kdelibs4support", ki18n( "KDebugDialog"),
0101             "1.0", ki18n("A dialog box for setting preferences for debug output"),
0102             K4AboutData::License_GPL, ki18n("Copyright 1999-2009, David Faure <faure@kde.org>"));
0103     data.addAuthor(ki18n("David Faure"), ki18n("Maintainer"), "faure@kde.org");
0104     data.setProgramIconName("tools-report-bug");
0105     KCmdLineArgs::init( argc, argv, &data );
0106 
0107     KCmdLineOptions options;
0108     options.add("fullmode", ki18n("Show the fully-fledged dialog instead of the default list dialog"));
0109     options.add("on <area>", ki18n(/*I18N_NOOP TODO*/ "Turn area on"));
0110     options.add("off <area>", ki18n(/*I18N_NOOP TODO*/ "Turn area off"));
0111     KCmdLineArgs::addCmdLineOptions( options );
0112     KUniqueApplication::addCmdLineOptions();
0113     KUniqueApplication app;
0114     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
0115     Kdelibs4ConfigMigrator migrate(QStringLiteral("kdebugdialog"));
0116     migrate.setConfigFiles(QStringList() << QStringLiteral("kdebugrc"));
0117     migrate.migrate();
0118 
0119 
0120     KAbstractDebugDialog * dialog;
0121     if (args->isSet("fullmode")) {
0122         dialog = new KDebugDialog(readAreas());
0123     } else {
0124         KListDebugDialog * listdialog = new KListDebugDialog(readAreas());
0125         if (args->isSet("on"))
0126         {
0127             listdialog->activateArea( args->getOption("on").toUtf8(), true );
0128             /*listdialog->save();
0129               listdialog->config()->sync();
0130               return 0;*/
0131         } else if ( args->isSet("off") )
0132         {
0133             listdialog->activateArea( args->getOption("off").toUtf8(), false );
0134             /*listdialog->save();
0135               listdialog->config()->sync();
0136               return 0;*/
0137         }
0138         dialog = listdialog;
0139     }
0140 
0141     /* Show dialog */
0142     int nRet = dialog->exec();
0143     if( nRet == QDialog::Accepted )
0144     {
0145         dialog->save();
0146         dialog->config()->sync();
0147     }
0148     else
0149         dialog->config()->markAsClean();
0150 
0151     return 0;
0152 }