/*
 * kcmsambastatistics.cpp
 *
 * Copyright (c) 2000 Alexander Neundorf <alexander.neundorf@rz.tu-ilmenau.de>
 *
 * Requires the Qt widget libraries, available at no cost at
 * http://www.troll.no/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include "kcmsambastatistics.h"
#include "kcmsambastatistics.moc"

#include <qlayout.h>

#include <kapp.h>
#include <klocale.h>

#include <iostream.h>

#define SCREEN_XY_OFFSET 20

StatisticsView::StatisticsView(QWidget *parent,KConfig *config, const char *name)
   :QWidget (parent, name)
   ,configFile(config)
   ,dataList(0)
   ,viewStatistics(this)
   ,connectionsL(i18n("Connections: 0"),this)
   ,filesL(i18n("File accesses: 0"),this)
   ,eventCb(FALSE,this)
   ,eventL(&eventCb,i18n("Event: "),this)
   ,serviceLe(this)
   ,serviceL(&serviceLe,i18n("Service/File:"),this)
   ,hostLe(this)
   ,hostL(&hostLe,i18n("Host/User:"),this)
   ,calcButton(i18n("Search"),this)
   ,clearButton(i18n("Clear results"),this)
   ,expandedInfoCb(i18n("Show expanded service info"),this)
   ,expandedUserCb(i18n("Show expanded host info"),this)

   ,connectionsCount(0)
   ,filesCount(0)
   ,calcCount(0)
{
   viewStatistics.setAllColumnsShowFocus(TRUE);
   viewStatistics.setFocusPolicy(QWidget::ClickFocus);
   
   viewStatistics.addColumn(i18n("Nr"),50);
   viewStatistics.addColumn(i18n("Event"),150);
   viewStatistics.addColumn(i18n("Service/File"),210);
   viewStatistics.addColumn(i18n("Host/User"),100);
   viewStatistics.addColumn(i18n("Hits"),70);
   //viewStatistics.addColumn(i18n("Percentage"),100);

   eventCb.insertItem(i18n("Connection"));
   eventCb.insertItem(i18n("File Access"));
   
   expandedInfoCb.setChecked(FALSE);
   expandedUserCb.setChecked(FALSE);
   clearStatistics();
   serviceLe.setText("*");
   hostLe.setText("*");
   
   viewStatistics.setMinimumSize(425,200);
   connectionsL.setMinimumSize(connectionsL.sizeHint());
   filesL.setMinimumSize(filesL.sizeHint());
   eventL.setMinimumSize(eventL.sizeHint());
   eventCb.setMinimumSize(eventCb.sizeHint());
   hostL.setMinimumSize(hostL.sizeHint());
   hostLe.setMinimumSize(150,hostLe.sizeHint().height());
   serviceL.setMinimumSize(serviceL.sizeHint());
   serviceLe.setMinimumSize(150,serviceLe.sizeHint().height());
   calcButton.setMinimumSize(calcButton.sizeHint());
   clearButton.setMinimumSize(clearButton.sizeHint());
   expandedInfoCb.setMinimumSize(expandedInfoCb.sizeHint());
   expandedUserCb.setMinimumSize(expandedUserCb.sizeHint());
   
   QVBoxLayout *topLayout=new QVBoxLayout(this);
   topLayout->setMargin(SCREEN_XY_OFFSET);
   topLayout->setSpacing(10);
   topLayout->addWidget(&viewStatistics,1);
   QGridLayout *subLayout=new QGridLayout(topLayout,4,3);
   subLayout->setColStretch(1,1);
   subLayout->setColStretch(2,1);
   
   QHBoxLayout *twoButtonsLayout=new QHBoxLayout;
   twoButtonsLayout->addWidget(&calcButton,1);
   twoButtonsLayout->addWidget(&clearButton,1);
   
   subLayout->addWidget(&connectionsL,0,0);
   subLayout->addWidget(&filesL,0,1);
   subLayout->addWidget(&eventL,1,0);
   subLayout->addWidget(&serviceL,1,1);
   subLayout->addWidget(&hostL,1,2);
   subLayout->addWidget(&eventCb,2,0);
   subLayout->addWidget(&serviceLe,2,1);
   subLayout->addWidget(&hostLe,2,2);
   subLayout->addLayout(twoButtonsLayout,3,0);
   subLayout->addWidget(&expandedInfoCb,3,1);
   subLayout->addWidget(&expandedUserCb,3,2);

   connect(&clearButton,SIGNAL(clicked()),this,SLOT(clearStatistics()));
   connect(&calcButton,SIGNAL(clicked()),this,SLOT(calculate()));
   setListInfo(0,0,0);
};

void StatisticsView::setListInfo(QListView *list, int nrOfFiles, int nrOfConnections)
{
   dataList=list;
   filesCount=nrOfFiles;
   connectionsCount=nrOfConnections;
   QString tmp("");
   tmp.sprintf("%d",connectionsCount);
   connectionsL.setText(QString(i18n("Connections: "))+tmp);
   tmp.sprintf("%d",filesCount);
   filesL.setText(QString(i18n("File accesses: "))+tmp);
   clearStatistics();
};

void StatisticsView::calculate()
{
   if (dataList==0) return;
   QApplication::setOverrideCursor(waitCursor);
   int connCount(0);
   if (QString(eventCb.currentText())==i18n("Connection"))
      connCount=1;
   //something has to be counted exactly
   if ((expandedInfoCb.isChecked()) || (expandedUserCb.isChecked()))
   {
      SambaLog sLog;
      QRegExp rService(serviceLe.text(),FALSE,TRUE);
      QRegExp rHost(hostLe.text(),FALSE,TRUE);
      QString item2, item3;
      QListViewItem* item=dataList->firstChild();
      while (item!=0)
      {
         if (connCount)
         {
            if ((QString(item->text(1)).contains(i18n("CONNECTION OPENED")))
                && (QString(item->text(2)).contains(rService)) && (QString(item->text(3)).contains(rHost)))
            {
               if (expandedInfoCb.isChecked()) item2=item->text(2);
               else item2=serviceLe.text();
               if (expandedUserCb.isChecked()) item3=item->text(3);
               else item3=hostLe.text();
               sLog.addItem(item2,item3);
               //count++;
            };
         }
         else
         {
            if ((QString(item->text(1)).contains(i18n("FILE OPENED")))
                && (QString(item->text(2)).contains(rService)) && (QString(item->text(3)).contains(rHost)))
            {
               if (expandedInfoCb.isChecked()) item2=item->text(2);
               else item2=serviceLe.text();
               if (expandedUserCb.isChecked()) item3=item->text(3);
               else item3=hostLe.text();
               sLog.addItem(item2,item3);
               
            };
         };
         item=item->nextSibling();
      };
      for (LogItem* tmpItem=sLog.items.first();tmpItem!=0;tmpItem=sLog.items.next())
      {
         for (SmallLogItem *tmpStr=tmpItem->accessed.first();tmpStr!=0;tmpStr=tmpItem->accessed.next())
         {
            calcCount++;
            QString number("");
            number.sprintf("%6d",calcCount);
            QString hits("");
            hits.sprintf("%7d",tmpStr->count);
            new QListViewItem(&viewStatistics,number,eventCb.currentText(),tmpItem->name,tmpStr->name,hits);
         };
      };
   }
   //no expanded info needed
   else
   {
      calcCount++;
      int count(0);
      QRegExp rService(serviceLe.text(),FALSE,TRUE);
      QRegExp rHost(hostLe.text(),FALSE,TRUE);
      QListViewItem* item=dataList->firstChild();
      while (item!=0)
      {
         if (connCount)
         {
            if ((QString(item->text(1)).contains(i18n("CONNECTION OPENED")))
                && (QString(item->text(2)).contains(rService)) && (QString(item->text(3)).contains(rHost)))
               count++;
         }
         else
         {
            if ((QString(item->text(1)).contains(i18n("FILE OPENED")))
                && (QString(item->text(2)).contains(rService)) && (QString(item->text(3)).contains(rHost)))
               count++;
         };
         item=item->nextSibling();
      };
      QString number("");
      number.sprintf("%6d",calcCount);
      QString hits("");
      hits.sprintf("%7d",count);
      new QListViewItem(&viewStatistics,number,eventCb.currentText(),serviceLe.text(),hostLe.text(),hits);
   };
   QApplication::restoreOverrideCursor();
};

void StatisticsView::clearStatistics()
{
   viewStatistics.clear();
   calcCount=0;
};

void SambaLog::printItems()
{
   cout<<"****** printing items: ******"<<endl;
   for (LogItem* tmpItem=items.first();tmpItem!=0;tmpItem=items.next())
   {
      cout<<"SERVICE: "<<tmpItem->name<<endl;
      for (SmallLogItem* tmpLog=tmpItem->accessed.first();tmpLog!=0;tmpLog=tmpItem->accessed.next())
         cout<<"      accessed by: "<<tmpLog->name<<"  "<<tmpLog->count<<endl;
   };
   cout<<"------ end of printing ------"<<endl<<endl;
};

LogItem* SambaLog::itemInList(QString name)
{
   LogItem* tmpItem(items.first());
   LogItem* foundItem(0);
   while ((tmpItem!=0) && (foundItem==0))
   {
      if (tmpItem->name==name) foundItem=tmpItem;
      tmpItem=items.next();
   };
   return foundItem;
};

void SambaLog::addItem(QString share, QString user)
{
   //cout<<"            adding connection: -"<<share<<"-   -"<<user<<"-"<<endl;
   LogItem* tmp(itemInList(share));
   if (tmp!=0)
   {
      tmp->count++;
      tmp->addItem(user);
   }
   else
   {
      items.append(new LogItem(share,user));
   };
};

SmallLogItem* LogItem::itemInList(QString name)
{
   SmallLogItem* tmpItem(accessed.first());
   SmallLogItem* foundItem(0);
   while ((tmpItem!=0) && (foundItem==0))
   {
      if (tmpItem->name==name) foundItem=tmpItem;
      tmpItem=accessed.next();
   };
   return foundItem;
};

void LogItem::addItem (QString host)
{
   SmallLogItem* tmp(itemInList(host));
   if (tmp!=0)
      tmp->count++;
   else
      accessed.append(new SmallLogItem(host));
};
