Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kinfocenter / samba / kcmsambastatistics.cpp
blobae6213b754dc9aab7036303d28a2356482e1ed16
1 /*
2 * kcmsambastatistics.cpp
4 * Copyright (c) 2000 Alexander Neundorf <alexander.neundorf@rz.tu-ilmenau.de>
6 * Requires the Qt widget libraries, available at no cost at
7 * http://www.troll.no/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "kcmsambastatistics.h"
24 #include "kcmsambastatistics.moc"
26 #include <QApplication>
27 #include <QLayout>
28 #include <Qt3Support/Q3CheckListItem>
29 #include <QLabel>
30 #include <QLineEdit>
31 #include <QCheckBox>
32 #include <QComboBox>
33 #include <QPushButton>
34 #include <QRegExp>
36 #include <kglobal.h>
37 #include <klocale.h>
38 #include <kdebug.h>
39 #include <kdialog.h>
41 StatisticsView::StatisticsView(QWidget *parent, KConfig *config) :
42 QWidget(parent), configFile(config), dataList(0), connectionsCount(0), filesCount(0), calcCount(0) {
43 viewStatistics = new Q3ListView( this );
44 connectionsL = new QLabel( i18n( "Connections: 0" ), this );
45 filesL = new QLabel( i18n( "File accesses: 0" ), this );
46 eventCb = new QComboBox( this );
47 eventL = new QLabel( i18n( "Event: " ), this );
48 eventL->setBuddy(eventCb);
49 serviceLe = new QLineEdit( this );
50 serviceL = new QLabel( i18n( "Service/File:" ), this );
51 serviceL->setBuddy(serviceLe);
52 hostLe = new QLineEdit( this );
53 hostL = new QLabel( i18n( "Host/User:" ), this );
54 hostL->setBuddy(hostLe);
55 calcButton = new QPushButton( i18n( "&Search" ), this );
56 clearButton = new QPushButton( i18n( "Clear Results" ), this );
57 expandedInfoCb = new QCheckBox( i18n( "Show expanded service info" ), this );
58 expandedUserCb = new QCheckBox( i18n( "Show expanded host info" ), this );
60 viewStatistics->setAllColumnsShowFocus(true);
61 viewStatistics->setFocusPolicy(Qt::ClickFocus);
62 viewStatistics->setShowSortIndicator(true);
64 viewStatistics->addColumn(i18n("Nr"), 30);
65 viewStatistics->addColumn(i18n("Event"), 130);
66 viewStatistics->addColumn(i18n("Service/File"), 200);
67 viewStatistics->addColumn(i18n("Host/User"), 90);
68 viewStatistics->addColumn(i18n("Hits"), 50);
69 //viewStatistics->addColumn(i18n("Percentage"),100);
71 eventCb->setEditable(false);
72 eventCb->addItem(i18n("Connection"));
73 eventCb->addItem(i18n("File Access"));
75 expandedInfoCb->setChecked(false);
76 expandedUserCb->setChecked(false);
77 clearStatistics();
78 serviceLe->setText("*");
79 hostLe->setText("*");
81 viewStatistics->setMinimumSize(375, 200);
82 connectionsL->setMinimumSize(connectionsL->sizeHint());
83 filesL->setMinimumSize(filesL->sizeHint());
84 eventL->setMinimumSize(eventL->sizeHint());
85 eventCb->setMinimumSize(eventCb->sizeHint());
86 hostL->setMinimumSize(hostL->sizeHint());
87 hostLe->setMinimumSize(120, hostLe->sizeHint().height());
88 serviceL->setMinimumSize(serviceL->sizeHint());
89 serviceLe->setMinimumSize(120, serviceLe->sizeHint().height());
90 calcButton->setMinimumSize(calcButton->sizeHint());
91 clearButton->setMinimumSize(clearButton->sizeHint());
92 expandedInfoCb->setMinimumSize(expandedInfoCb->sizeHint());
93 expandedUserCb->setMinimumSize(expandedUserCb->sizeHint());
95 QVBoxLayout *topLayout=new QVBoxLayout(this);
96 topLayout->setMargin(KDialog::marginHint());
97 topLayout->setSpacing(KDialog::spacingHint());
98 topLayout->addWidget(viewStatistics, 1);
99 QGridLayout *subLayout=new QGridLayout();
100 topLayout->addItem(subLayout);
101 subLayout->setColumnStretch(1, 1);
102 subLayout->setColumnStretch(2, 1);
104 QHBoxLayout *twoButtonsLayout=new QHBoxLayout;
105 twoButtonsLayout->addWidget(calcButton, 1);
106 twoButtonsLayout->addWidget(clearButton, 1);
108 subLayout->addWidget(connectionsL, 0, 0);
109 subLayout->addWidget(filesL, 0, 1);
110 subLayout->addWidget(eventL, 1, 0);
111 subLayout->addWidget(serviceL, 1, 1);
112 subLayout->addWidget(hostL, 1, 2);
113 subLayout->addWidget(eventCb, 2, 0);
114 subLayout->addWidget(serviceLe, 2, 1);
115 subLayout->addWidget(hostLe, 2, 2);
116 subLayout->addLayout(twoButtonsLayout, 3, 0);
117 subLayout->addWidget(expandedInfoCb, 3, 1);
118 subLayout->addWidget(expandedUserCb, 3, 2);
120 connect(clearButton, SIGNAL(clicked()), this, SLOT(clearStatistics()));
121 connect(calcButton, SIGNAL(clicked()), this, SLOT(calculate()));
122 setListInfo(0, 0, 0);
125 void StatisticsView::setListInfo(Q3ListView *list, int nrOfFiles, int nrOfConnections) {
126 dataList=list;
127 filesCount=nrOfFiles;
128 connectionsCount=nrOfConnections;
129 connectionsL->setText(i18n("Connections: %1", KGlobal::locale()->formatNumber(connectionsCount, 0)));
130 filesL->setText(i18n("File accesses: %1", KGlobal::locale()->formatNumber(filesCount, 0)));
131 clearStatistics();
134 void StatisticsView::calculate() {
135 if (dataList==0)
136 return;
137 QApplication::setOverrideCursor(Qt::WaitCursor);
138 int connCount(0);
139 if (eventCb->currentText()==i18n("Connection"))
140 connCount=1;
141 //something has to be counted exactly
142 if ((expandedInfoCb->isChecked()) || (expandedUserCb->isChecked())) {
143 SambaLog sLog;
144 QRegExp rService(serviceLe->text(), Qt::CaseInsensitive, QRegExp::Wildcard);
145 QRegExp rHost(hostLe->text(), Qt::CaseInsensitive, QRegExp::Wildcard);
146 QString item2, item3;
147 Q3ListViewItem* item=dataList->firstChild();
148 while (item!=0) {
149 if (connCount) {
150 if ((QString(item->text(1)).contains(i18n("CONNECTION OPENED"))) && (QString(item->text(2)).contains(rService)) && (QString(item->text(3)).contains(rHost))) {
151 if (expandedInfoCb->isChecked())
152 item2=item->text(2);
153 else
154 item2=serviceLe->text();
155 if (expandedUserCb->isChecked())
156 item3=item->text(3);
157 else
158 item3=hostLe->text();
159 sLog.addItem(item2, item3);
160 //count++;
162 } else {
163 if ((QString(item->text(1)).contains(i18n("FILE OPENED"))) && (QString(item->text(2)).contains(rService)) && (QString(item->text(3)).contains(rHost))) {
164 if (expandedInfoCb->isChecked())
165 item2=item->text(2);
166 else
167 item2=serviceLe->text();
168 if (expandedUserCb->isChecked())
169 item3=item->text(3);
170 else
171 item3=hostLe->text();
172 sLog.addItem(item2, item3);
176 item=item->nextSibling();
178 for (LogItem* tmpItem=sLog.items.first(); tmpItem!=0; tmpItem=sLog.items.next()) {
179 for (SmallLogItem *tmpStr=tmpItem->accessed.first(); tmpStr!=0; tmpStr=tmpItem->accessed.next()) {
180 calcCount++;
181 QString number("");
182 number.sprintf("%6d", calcCount);
183 QString hits("");
184 hits.sprintf("%7d", tmpStr->count);
185 new Q3ListViewItem(viewStatistics,number,eventCb->currentText(),tmpItem->name,tmpStr->name,hits);
189 //no expanded info needed
190 else {
191 calcCount++;
192 int count(0);
193 QRegExp rService(serviceLe->text(), Qt::CaseInsensitive, QRegExp::Wildcard);
194 QRegExp rHost(hostLe->text(), Qt::CaseInsensitive, QRegExp::Wildcard);
195 Q3ListViewItem* item=dataList->firstChild();
196 while (item!=0) {
197 if (connCount) {
198 if ((QString(item->text(1)).contains(i18n("CONNECTION OPENED"))) && (QString(item->text(2)).contains(rService)) && (QString(item->text(3)).contains(rHost)))
199 count++;
200 } else {
201 if ((QString(item->text(1)).contains(i18n("FILE OPENED"))) && (QString(item->text(2)).contains(rService)) && (QString(item->text(3)).contains(rHost)))
202 count++;
204 item=item->nextSibling();
206 QString number("");
207 number.sprintf("%6d", calcCount);
208 QString hits("");
209 hits.sprintf("%7d", count);
210 new Q3ListViewItem(viewStatistics,number,eventCb->currentText(),serviceLe->text(),hostLe->text(),hits);
212 QApplication::restoreOverrideCursor();
215 void StatisticsView::clearStatistics() {
216 viewStatistics->clear();
217 calcCount=0;
220 void SambaLog::printItems() {
221 kDebug() << "****** printing items: ******";
222 for (LogItem* tmpItem=items.first(); tmpItem!=0; tmpItem=items.next()) {
223 kDebug() << "SERVICE: " << tmpItem->name;
224 for (SmallLogItem* tmpLog=tmpItem->accessed.first(); tmpLog!=0; tmpLog=tmpItem->accessed.next())
225 kDebug() << " accessed by: " << tmpLog->name << " " << tmpLog->count;
227 kDebug() << "------ end of printing ------";
230 LogItem* SambaLog::itemInList(const QString &name) {
231 LogItem* tmpItem(items.first());
232 LogItem* foundItem(0);
233 while ((tmpItem!=0) && (foundItem==0)) {
234 if (tmpItem->name==name)
235 foundItem=tmpItem;
236 tmpItem=items.next();
238 return foundItem;
241 void SambaLog::addItem(const QString &share, const QString &user) {
242 //cout<<" adding connection: -"<<share<<"- -"<<user<<"-"<<endl;
243 LogItem* tmp(itemInList(share));
244 if (tmp!=0) {
245 tmp->count++;
246 tmp->addItem(user);
247 } else {
248 items.append(new LogItem(share,user));
252 SmallLogItem* LogItem::itemInList(const QString &name) {
253 SmallLogItem* tmpItem(accessed.first());
254 SmallLogItem* foundItem(0);
255 while ((tmpItem!=0) && (foundItem==0)) {
256 if (tmpItem->name==name)
257 foundItem=tmpItem;
258 tmpItem=accessed.next();
260 return foundItem;
263 void LogItem::addItem(const QString &host) {
264 SmallLogItem* tmp(itemInList(host));
265 if (tmp!=0)
266 tmp->count++;
267 else
268 accessed.append(new SmallLogItem(host));