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

0001 /* This file is part of the KDE libraries
0002    Copyright (C) 2000 David Faure <faure@kde.org>
0003    Copyright (C) 2005 Hamish Rodda <rodda@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "klistdebugdialog.h"
0022 
0023 #include <kconfig.h>
0024 #include <kdebug.h>
0025 #include <klocale.h>
0026 #include <ktreewidgetsearchline.h>
0027 #include <ktreewidgetsearchlinewidget.h>
0028 
0029 #include <QDBusConnection>
0030 #include <QDBusMessage>
0031 #include <QLayout>
0032 #include <QTreeWidget>
0033 #include <QPushButton>
0034 
0035 KListDebugDialog::KListDebugDialog(const AreaMap& areaMap, QWidget *parent)
0036     : KAbstractDebugDialog(parent)
0037 {
0038     setCaption(i18n("Debug Settings"));
0039     QWidget* mainWidget = new QWidget( this );
0040     QVBoxLayout *lay = new QVBoxLayout( mainWidget );
0041     lay->setMargin( KDialog::marginHint() );
0042     lay->setSpacing( KDialog::spacingHint() );
0043 
0044     m_incrSearch = new KTreeWidgetSearchLineWidget();
0045     m_incrSearch->searchLine()->setClearButtonEnabled(true);
0046     lay->addWidget( m_incrSearch );
0047     //  connect( m_incrSearch, SIGNAL(textChanged(QString)),
0048     //           SLOT(filterCheckBoxes(QString)) );
0049 
0050     //@TODO: Change back to QListWidget once Trolltech fixed the task: #214420
0051     //See http://trolltech.com/developer/task-tracker/index_html?id=214420&method=entry
0052     m_areaWidget = new QTreeWidget();
0053     m_areaWidget->setHeaderHidden(true);
0054     m_areaWidget->setItemsExpandable(false);
0055     m_areaWidget->setRootIsDecorated(false);
0056     m_areaWidget->setUniformRowHeights(true);
0057     lay->addWidget(m_areaWidget);
0058 
0059     m_incrSearch->searchLine()->addTreeWidget(m_areaWidget);
0060 
0061     for( QMap<QString,QString>::const_iterator it = areaMap.begin(); it != areaMap.end(); ++it ) {
0062         QTreeWidgetItem* item = new QTreeWidgetItem(m_areaWidget, QStringList() << it.value());
0063         item->setData(0, Qt::UserRole, it.key().simplified());
0064     }
0065 
0066     m_buttonContainer = new QWidget(mainWidget);
0067     QHBoxLayout* selectButs = new QHBoxLayout(m_buttonContainer);
0068     lay->addWidget(m_buttonContainer);
0069     QPushButton* all = new QPushButton(i18n("&Select All"), m_buttonContainer);
0070     QPushButton* none = new QPushButton(i18n("&Deselect All"), m_buttonContainer);
0071     selectButs->addWidget( all );
0072     selectButs->addWidget( none );
0073 
0074     connect( all, SIGNAL(clicked()), this, SLOT(selectAll()) );
0075     connect( none, SIGNAL(clicked()), this, SLOT(deSelectAll()) );
0076 
0077     m_disableAll = new QCheckBox(mainWidget);
0078     m_disableAll->setText(i18n("Disable all debug output"));
0079     connect(m_disableAll, SIGNAL(toggled(bool)), this, SLOT(disableAllClicked()));
0080     lay->addWidget(m_disableAll);
0081 
0082     load();
0083 
0084     buildButtons();
0085     resize( 350, 400 );
0086     setMainWidget( mainWidget );
0087     m_incrSearch->searchLine()->setFocus();
0088 }
0089 
0090 void KListDebugDialog::selectAll()
0091 {
0092     for (int i = 0; i < m_areaWidget->topLevelItemCount(); ++i) {
0093         QTreeWidgetItem* item = m_areaWidget->topLevelItem(i);
0094         if (!item->isHidden()) {
0095             item->setCheckState(0, Qt::Checked);
0096         }
0097     }
0098 }
0099 
0100 void KListDebugDialog::deSelectAll()
0101 {
0102     for (int i = 0; i < m_areaWidget->topLevelItemCount(); ++i) {
0103         QTreeWidgetItem* item = m_areaWidget->topLevelItem(i);
0104         if (!item->isHidden()) {
0105             item->setCheckState(0, Qt::Unchecked);
0106         }
0107     }
0108 }
0109 
0110 void KListDebugDialog::doLoad()
0111 {
0112     for (int i = 0; i < m_areaWidget->topLevelItemCount(); ++i) {
0113         QTreeWidgetItem* item = m_areaWidget->topLevelItem(i);
0114         KConfigGroup group = pConfig->group( item->data(0, Qt::UserRole).toByteArray() ); // Group name = debug area code = cb's name
0115 
0116         int setting = group.readEntry("InfoOutput", -1);
0117 
0118         switch (setting) {
0119         case 4: // off
0120             item->setCheckState(0, Qt::Unchecked);
0121             break;
0122         case -1: // default
0123         case 2: //shell
0124             item->setCheckState(0, Qt::Checked);
0125             break;
0126         case 3: //syslog
0127         case 1: //msgbox
0128         case 0: //file
0129         default:
0130             item->setCheckState(0, Qt::PartiallyChecked);
0131             /////// Uses the triState capability of checkboxes
0132             break;
0133         }
0134     }
0135 }
0136 
0137 void KListDebugDialog::doSave()
0138 {
0139     for (int i = 0; i < m_areaWidget->topLevelItemCount(); ++i) {
0140         QTreeWidgetItem* item = m_areaWidget->topLevelItem(i);
0141         KConfigGroup group = pConfig->group( item->data(0, Qt::UserRole).toByteArray() ); // Group name = debug area code = cb's name
0142         if (item->checkState(0) != Qt::PartiallyChecked)
0143         {
0144             int setting = (item->checkState(0) == Qt::Checked) ? 2 : 4;
0145             group.writeEntry( "InfoOutput", setting );
0146         }
0147     }
0148     //sync done by main.cpp
0149 
0150     // send DBus message to all clients
0151     QDBusMessage msg = QDBusMessage::createSignal("/", "org.kde.KDebug", "configChanged" );
0152     if (!QDBusConnection::sessionBus().send(msg))
0153     {
0154         kError() << "Unable to send D-BUS message" << endl;
0155     }
0156 }
0157 
0158 void KListDebugDialog::activateArea( const QByteArray& area, bool activate )
0159 {
0160     foreach(QTreeWidgetItem* item, m_areaWidget->findItems(area, Qt::MatchContains)) {
0161         item->setCheckState( 0, activate ? Qt::Checked : Qt::Unchecked );
0162         return;
0163     }
0164 }
0165 
0166 void KListDebugDialog::disableAllClicked()
0167 {
0168     bool allDisabled = m_disableAll->isChecked();
0169     m_incrSearch->setEnabled(!allDisabled);
0170     m_areaWidget->setEnabled(!allDisabled);
0171     m_buttonContainer->setEnabled(!allDisabled);
0172 }
0173 
0174 #include "moc_klistdebugdialog.cpp"