KDELibs4Support

main.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000, 2009 David Faure <[email protected]>
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 <k4aboutdata.h>
24 #include <kdelibs4configmigrator.h>
25 
26 #include <QTextStream>
27 #include <klocale.h>
28 #include <kdebug.h>
29 #include <kuniqueapplication.h>
30 #include <kconfig.h>
31 #include <kconfiggroup.h>
32 
33 #include <QFile>
34 #include <QStandardPaths>
35 
36 static KAbstractDebugDialog::AreaMap readAreas()
37 {
39  // Group 0 is not used anymore. kDebug() uses the area named after the appname.
40  //areas.insert( " 0" /*cf rightJustified below*/, "0 (generic)" );
41 
42  const QString confAreasFile = QStandardPaths::locate(QStandardPaths::ConfigLocation, "kdebug.areas");
43  QFile file( confAreasFile );
44  if (!file.open(QIODevice::ReadOnly)) {
45  kWarning() << "Couldn't open" << confAreasFile;
46  } else {
47  QString data;
48 
49  QTextStream ts(&file);
50  ts.setCodec( "ISO-8859-1" );
51  while (!ts.atEnd()) {
52  data = ts.readLine().simplified();
53 
54  int pos = data.indexOf("#");
55  if ( pos != -1 ) {
56  data.truncate( pos );
57  data = data.simplified();
58  }
59 
60  if (data.isEmpty())
61  continue;
62 
63  const int space = data.indexOf(' ');
64  if (space == -1)
65  kError() << "No space:" << data << endl;
66 
67  bool longOK;
68  unsigned long number = data.left(space).toULong(&longOK);
69  if (!longOK)
70  kError() << "The first part wasn't a number : " << data << endl;
71 
72  const QString description = data.mid(space).simplified();
73 
74  // In the key, right-align the area number to 6 digits for proper sorting
75  const QString key = QString::number(number).rightJustified(6);
76  areas.insert( key, QString("%1 %2").arg(number).arg(description) );
77  }
78  }
79 
80  bool ok;
81 #ifndef NDEBUG
82  // Builtin unittest for our expectations of QString::toInt
83  QString("4a").toInt(&ok);
84  Q_ASSERT(!ok);
85 #endif
86 
87  KConfig config("kdebugrc", KConfig::NoGlobals);
88  Q_FOREACH(const QString& groupName, config.groupList()) {
89  groupName.toInt(&ok);
90  if (ok)
91  continue; // we are not interested in old-style number-only groups
92  areas.insert(groupName, groupName); // ID == description
93  }
94 
95  return areas;
96 }
97 
98 int main(int argc, char ** argv)
99 {
100  K4AboutData data( "kdebugdialog5", "kdelibs4support", ki18n( "KDebugDialog"),
101  "1.0", ki18n("A dialog box for setting preferences for debug output"),
102  K4AboutData::License_GPL, ki18n("Copyright 1999-2009, David Faure <[email protected]>"));
103  data.addAuthor(ki18n("David Faure"), ki18n("Maintainer"), "[email protected]");
104  data.setProgramIconName("tools-report-bug");
105  KCmdLineArgs::init( argc, argv, &data );
106 
107  KCmdLineOptions options;
108  options.add("fullmode", ki18n("Show the fully-fledged dialog instead of the default list dialog"));
109  options.add("on <area>", ki18n(/*I18N_NOOP TODO*/ "Turn area on"));
110  options.add("off <area>", ki18n(/*I18N_NOOP TODO*/ "Turn area off"));
113  KUniqueApplication app;
115  Kdelibs4ConfigMigrator migrate(QStringLiteral("kdebugdialog"));
116  migrate.setConfigFiles(QStringList() << QStringLiteral("kdebugrc"));
117  migrate.migrate();
118 
119 
120  KAbstractDebugDialog * dialog;
121  if (args->isSet("fullmode")) {
122  dialog = new KDebugDialog(readAreas());
123  } else {
124  KListDebugDialog * listdialog = new KListDebugDialog(readAreas());
125  if (args->isSet("on"))
126  {
127  listdialog->activateArea( args->getOption("on").toUtf8(), true );
128  /*listdialog->save();
129  listdialog->config()->sync();
130  return 0;*/
131  } else if ( args->isSet("off") )
132  {
133  listdialog->activateArea( args->getOption("off").toUtf8(), false );
134  /*listdialog->save();
135  listdialog->config()->sync();
136  return 0;*/
137  }
138  dialog = listdialog;
139  }
140 
141  /* Show dialog */
142  int nRet = dialog->exec();
143  if( nRet == QDialog::Accepted )
144  {
145  dialog->save();
146  dialog->config()->sync();
147  }
148  else
149  dialog->config()->markAsClean();
150 
151  return 0;
152 }
ulong toULong(bool *ok, int base) const const
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
static void addCmdLineOptions(const KCmdLineOptions &options, const KLocalizedString &name=KLocalizedString(), const QByteArray &id=QByteArray(), const QByteArray &afterId=QByteArray())
Add options to your application.
void truncate(int position)
Control debug output of KDE applications This dialog offers a reduced functionality compared to the f...
KCmdLineOptions & add(const QByteArray &name, const KLocalizedString &description=KLocalizedString(), const QByteArray &defaultValue=QByteArray())
Add command line option, by providing its name, description, and possibly a default value...
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
Access parsed arguments.
A class for command-line argument handling.
Definition: kcmdlineargs.h:286
QString simplified() const const
virtual int exec()
QTextStream & endl(QTextStream &stream)
bool isSet(const QByteArray &option) const
Read out a boolean option or check for the presence of string option.
KSharedConfigPtr config()
Returns the general config object.
Definition: kglobal.cpp:104
KIOCORE_EXPORT QString number(KIO::filesize_t size)
QString number(int n, int base)
QString rightJustified(int width, QChar fill, bool truncate) const const
Control debug/warning/error/fatal output of KDE applications.
Definition: kdebugdialog.h:39
int toInt(bool *ok, int base) const const
bool isEmpty() const const
This class is used to store information about a program.
Definition: k4aboutdata.h:199
static void addCmdLineOptions()
Adds command line options specific for KUniqueApplication.
QString mid(int position, int n) const const
static void init(int argc, char **argv, const QByteArray &appname, const QByteArray &catalog, const KLocalizedString &programName, const QByteArray &version, const KLocalizedString &description=KLocalizedString(), StdCmdLineArgs stdargs=StdCmdLineArgs(CmdLineArgQt|CmdLineArgKDE))
Initialize class.
QString left(int n) const const
KLocalizedString KI18N_EXPORT ki18n(const char *text)
QString getOption(const QByteArray &option) const
Read out a string option.
KUniqueApplication is a KApplication which only uses a single process.
QMap::iterator insert(const Key &key, const T &value)
Class that holds command line options.
Definition: kcmdlineargs.h:53
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jul 10 2020 22:56:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.