~brand-nmapsi4/nmapsi4/master

« back to all changes in this revision

Viewing changes to src/app/logUtils.cpp

  • Committer: Francesco Cecconi
  • Date: 2011-12-18 11:02:04 UTC
  • Revision ID: git-v1:60a69e132c8ffeb2727e558b2b2e8ac1713e332e
Removed nmapsi4-logr (dedicate repository) but it could be deprecated.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2007-2011 by Francesco Cecconi                          *
 
3
 *   francesco.cecconi@gmail.com                                           *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License.        *
 
8
 *                                                                         *
 
9
 *   This program 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         *
 
12
 *   GNU General Public License for more details.                          *
 
13
 *                                                                         *
 
14
 *   You should have received a copy of the GNU General Public License     *
 
15
 *   along with this program; if not, write to the                         *
 
16
 *   Free Software Foundation, Inc.,                                       *
 
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
18
 ***************************************************************************/
 
19
 
 
20
#include "mainwin.h"
 
21
 
 
22
void nmapClass::openBrowser()
 
23
{
 
24
    _utilities->openFileBrowser(lineInputFile);
 
25
}
 
26
 
 
27
QFile* nmapClass::create_logFile(const QString Path)
 
28
{
 
29
    QFile *Pfile = new QFile();
 
30
    QDir::setCurrent(Path);
 
31
    Pfile->setFileName(FileName);
 
32
    return Pfile;
 
33
}
 
34
 
 
35
void nmapClass::fileSession()
 
36
{
 
37
    int FLAGS = 0;
 
38
 
 
39
    if ((!_logFilePath) || (_logFilePath && (firstPath != logPath) && (FLAGS = 1))) 
 
40
    {
 
41
 
 
42
        if (FLAGS) 
 
43
        {
 
44
            QDir::setCurrent(firstPath);
 
45
#ifndef TOOLS_NO_DEBUG
 
46
            qDebug() << "DEBUG::SESSION:: " << _logFilePath << firstPath << logPath;
 
47
#endif
 
48
            _logFilePath->remove();
 
49
        }
 
50
 
 
51
        FileName = QDate::currentDate().toString("[dd-MM-yyyy]");
 
52
        FileName.append(QTime::currentTime().toString("[h-m-s]"));
 
53
        FileName.append("-nmapsi4.log");
 
54
        firstPath = logPath;
 
55
        _logFilePath = create_logFile(logPath);
 
56
        _logFilePath->open(QIODevice::WriteOnly | QIODevice::Text);
 
57
    }
 
58
}
 
59
 
 
60
void nmapClass::isEmptyLog()
 
61
{
 
62
    qint64 tmp = _logFilePath->size();
 
63
 
 
64
    if (!tmp) 
 
65
    {
 
66
        QDir::setCurrent(logPath);
 
67
        _logFilePath->setFileName(FileName);
 
68
        _logFilePath->remove();
 
69
    } 
 
70
    else if (firstPath.compare(logPath)) 
 
71
    {
 
72
        QString pathTmp = logPath;
 
73
        pathTmp.append("/");
 
74
        pathTmp = QDir::toNativeSeparators(pathTmp);
 
75
        pathTmp.append(FileName);
 
76
 
 
77
        if (!_logFilePath->copy(FileName, pathTmp)) 
 
78
        {
 
79
            QMessageBox::critical(this, "NmapSI4",
 
80
                                  tr("Save File permission Error (Log stored in /tmp)\n"), tr("Close"));
 
81
        } 
 
82
        else 
 
83
        {
 
84
            _logFilePath->remove();
 
85
        }
 
86
    }
 
87
}
 
88
 
 
89
void nmapClass::saveAsLog()
 
90
{
 
91
    if (FileName.isNull() || listClearFlag) 
 
92
    {
 
93
        QMessageBox::critical(this, tr("Save Log"), tr("No Scan found\n"), tr("Close"));
 
94
        return;
 
95
    }
 
96
 
 
97
    QFile *tmpFile = new QFile();
 
98
    QString newFilePath = logPath;
 
99
    newFilePath.append("/");
 
100
    newFilePath = QDir::toNativeSeparators(newFilePath);
 
101
    newFilePath.append(FileName);
 
102
    tmpFile->setFileName(newFilePath);
 
103
 
 
104
    if (!tmpFile->size())
 
105
    {
 
106
        QMessageBox::critical(this, tr("Save Log"),tr("No Scan found\n"), tr("Close"));
 
107
        delete tmpFile;
 
108
        return;
 
109
    }
 
110
 
 
111
    QString url;
 
112
    url = QDir::homePath();
 
113
    url.append("/");
 
114
    url = QDir::toNativeSeparators(url);
 
115
    url.append("si4");
 
116
    url.append(_VERSION_);
 
117
    url.append("-");
 
118
    url.append(hostEdit->currentText());
 
119
    url.append(".log");
 
120
 
 
121
    QString FileNameTmp;
 
122
    FileNameTmp = QFileDialog::getSaveFileName(this, tr("Save Log"), url, tr("Log (*.log)"));
 
123
 
 
124
    if (!FileNameTmp.isEmpty()) 
 
125
    {
 
126
 
 
127
        QFile *freeFile = new QFile();
 
128
        freeFile->setFileName(FileNameTmp);
 
129
        if (freeFile->exists())
 
130
        {
 
131
            freeFile->remove();
 
132
        }
 
133
        delete freeFile;
 
134
 
 
135
        if (!tmpFile->copy(FileNameTmp))
 
136
        {
 
137
            QMessageBox::critical(this, tr("Information"),
 
138
                                  tr("Save File permission Error (Log stored in /tmp)\n"), tr("Close"));
 
139
        }
 
140
 
 
141
        logSessionFile = FileNameTmp;
 
142
    }
 
143
 
 
144
    delete tmpFile;
 
145
}
 
146
 
 
147
void nmapClass::saveLog()
 
148
{
 
149
 
 
150
    if (logSessionFile.isEmpty()) 
 
151
    {
 
152
        return;
 
153
    }
 
154
 
 
155
    QFile *tmpFile = new QFile();
 
156
    QString newFile = logPath;
 
157
    newFile.append("/");
 
158
    newFile = QDir::toNativeSeparators(newFile);
 
159
    newFile.append(FileName);
 
160
    tmpFile->setFileName(newFile);
 
161
 
 
162
    QFile *freeFile = new QFile();
 
163
    freeFile->setFileName(logSessionFile);
 
164
    
 
165
    if (freeFile->exists()) 
 
166
    {
 
167
        freeFile->remove();
 
168
    }
 
169
    
 
170
    delete freeFile;
 
171
 
 
172
    if (!tmpFile->copy(logSessionFile)) 
 
173
    {
 
174
        QMessageBox::critical(this, tr("Information"),
 
175
                              tr("Save File permission Error\n"), tr("Close"));
 
176
    }
 
177
 
 
178
    delete tmpFile;
 
179
}