1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/***************************************************************************
* Copyright (C) 2004 by Daniel Rocher *
* daniel.rocher@adella.org *
* *
* 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., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <QtGui>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include "log.h"
#include "mysortfilterproxymodel.h"
#define name_of_share "Share"
#define name_of_file "File"
extern void writeToConsole(const QString & message);
/**
\class LogForm
\brief Form to view CIFS/SMB activities
\date 2007-06-27
\version 1.1
\param parent pointer to the parent object
\author Daniel Rocher
*/
LogForm::LogForm(QWidget *parent) : QDialog(parent)
{
debugQt("LogForm::LogForm()");
setupUi(this);
model = new QStandardItemModel(0, 5,tableView);
// sort and filter model
proxyModel = new MySortFilterProxyModel(name_of_share,name_of_file,tableView);
proxyModel->setSourceModel(model);
// populate model
tableView->setModel ( proxyModel);
// sort enable
tableView->setSortingEnabled(true);
// proxy model is dynamically sorted and filtered whenever the contents of the source model change.
proxyModel->setDynamicSortFilter ( true);
//not editable
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableView->verticalHeader ()->setVisible(false); // hide vertical header
setHeader();
}
LogForm::~LogForm()
{
debugQt("LogForm::~LogForm()");
}
/**
Set tableView's header
*/
void LogForm::setHeader()
{
debugQt("LogForm::setHeader()");
QStringList labels;
labels << tr("Date") << tr("Machine") << tr("User") << tr("Service") << tr("Type");
model->setHorizontalHeaderLabels ( labels );
tableView->setColumnWidth ( 0, 200 );
tableView->setColumnWidth ( 1, 100 );
tableView->setColumnWidth ( 2, 100 );
tableView->setColumnWidth ( 3, 450 );
tableView->setColumnWidth ( 4, 0);
tableView->setColumnHidden ( 4, true );
}
/**
Save log file.
CSV format
*/
void LogForm::on_SaveLogButton_clicked()
{
debugQt("LogForm::on_SaveLogButton_clicked()");
QStandardItem * item;
QString date;
QString machine;
QString user;
QString type_of_service;
QString service;
QFileDialog fileDialog;
fileDialog.setDefaultSuffix ( "csv" );
QString fileName = fileDialog.getSaveFileName(this, tr("Open File"), QDir::homePath (),"*.csv");
if (fileName.isEmpty())
return;
if (!fileName.contains(".csv", Qt::CaseInsensitive)) fileName+=".csv";
QFile f1(fileName);
if ( !f1.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
writeToConsole("Impossible to create file !");
QMessageBox::warning ( this, "Error" , "Impossible to create file !");
return;
}
QTextStream t( &f1 );
t << "Date;Machine;User;Type;Service\n"; // header of CSV
for (int i=0; i<model->rowCount (); ++i)
{
item =model->item ( i, 0 );
if (item) date=item->text (); else continue;
item =model->item ( i, 1 );
if (item) machine=item->text (); else continue;
item =model->item ( i, 2 );
if (item) user=item->text (); else continue;
item =model->item ( i, 3 );
if (item) service=item->text (); else continue;
item =model->item ( i, 4 );
if (item) type_of_service=item->text (); else continue;
t << date << ";" << machine << ";" << user << ";" << type_of_service << ";" << service << "\n";
}
f1.close();
}
/**
erase old log
\sa limitLog
*/
void LogForm::eraseOldLog()
{
debugQt("LogForm::eraseOldLog()");
QDateTime dateItem;
QStandardItem * item;
for (int i=0; i<model->rowCount (); ++i)
{
item =model->item ( i, 0 );
if (item) dateItem=QDateTime::fromString (item->text (),"yyyy-MM-dd hh:mm:ss"); else continue;
if (!dateItem.isValid()) continue;
if (dateItem<QDateTime::currentDateTime().addDays(-limitLog))
{
model->removeRow ( i );
i--;
}
}
}
/**
Clear tableView
*/
void LogForm::on_clearButton_clicked()
{
debugQt("LogForm::on_clearButton_clicked()");
model->clear();
setHeader();
}
/**
Add new data
\sa type_message
*/
void LogForm::append(const type_message & Tmessage)
{
QIcon icon_file(":/icons/document.png");
QIcon icon_share(":/icons/folder_open.png");
QString datetimeStr;
QList<QStandardItem *> listItem;
// change date format
QDateTime datetime=QDateTime::fromString ( Tmessage.date );
if (datetime.isValid ())
datetimeStr=datetime.toString("yyyy-MM-dd hh:mm:ss");
else
datetimeStr=Tmessage.date;
listItem << new QStandardItem(datetimeStr);
// machine
listItem << new QStandardItem(Tmessage.machine);
// user
listItem << new QStandardItem(Tmessage.user);
// service
if (Tmessage.type_message==0) listItem << new QStandardItem(icon_share, Tmessage.opened) << new QStandardItem(name_of_share); // a share
if (Tmessage.type_message==1) listItem << new QStandardItem(icon_file, Tmessage.opened) << new QStandardItem(name_of_file); // a file
model->appendRow (listItem);
}
/**
User apply filter
*/
void LogForm::on_filterEdit_textChanged()
{
debugQt("LogForm::on_filterEdit_textChanged()");
QString filter=filterEdit->text();
proxyModel->setFilterCaseSensitivity ( Qt::CaseInsensitive );
proxyModel->setFilterFixedString (filter);
}
/**
view/hide Shares
*/
void LogForm::on_checkShare_stateChanged ( int state)
{
debugQt("LogForm::on_checkShare_stateChanged ()");
proxyModel->setFilterShare(state);
}
/**
view/hide Files
*/
void LogForm::on_checkFile_stateChanged ( int state)
{
debugQt("LogForm::on_checkFile_stateChanged ()");
proxyModel->setFilterFile(state);
}
|