Skip to content

Commit

Permalink
Made the log persistent. Added windows executable icon(s).
Browse files Browse the repository at this point in the history
  • Loading branch information
raptorswing committed Feb 13, 2012
1 parent 85a7158 commit b0957a1
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 13 deletions.
121 changes: 121 additions & 0 deletions MainWindow.cpp
Expand Up @@ -8,7 +8,13 @@
#include <QSettings>
#include <QTimer>
#include <QStringBuilder>
#include <QFileDialog>
#include <QDateTime>
#include <QMessageBox>
#include <QFile>
#include <QDir>

const quint32 saveSettingsIntervalMS = 5 * 60 * 1000; //Five minutes

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
Expand All @@ -35,6 +41,14 @@ MainWindow::MainWindow(QWidget *parent) :
//If our settings say to start minimized, do so
if (this->startMinimized)
this->setWindowState(Qt::WindowMinimized);

//Write settings (and more importantly, log data) to disk every once in a while
QTimer * timer = new QTimer(this);
connect(timer,
SIGNAL(timeout()),
this,
SLOT(saveSettings()));
timer->start(saveSettingsIntervalMS);
}

MainWindow::~MainWindow()
Expand Down Expand Up @@ -66,6 +80,7 @@ void MainWindow::changeEvent(QEvent *e)
//If the window has just been minimized and our settings say to minimize to tray, do so
if (this->windowState() & Qt::WindowMinimized)
{
//The timer is a hack
if (this->minimzeToTray)
QTimer::singleShot(5,this,SLOT(hide()));
}
Expand Down Expand Up @@ -145,6 +160,40 @@ void MainWindow::on_startButton_toggled(bool checked)
}
}

void MainWindow::on_exportLogButton_clicked()
{
QString filePath = QFileDialog::getSaveFileName(this,
"Select output file for log export",
QString(),
"*.txt");
//If the filePath is empty, the user probably cancelled the dialog
if (filePath.isEmpty())
return;

//Try to write --- show an error otherwise
QString errorMessage;
if (!this->writeLogToFile(filePath,&errorMessage))
{
qWarning() << errorMessage;
QMessageBox::warning(this,
"Failed to export log",
errorMessage);
return;
}

//Show a message saying the export succeeded
const QString message = "Log exported successfully.";
qDebug() << message;
QMessageBox::information(this,
"Log Export",
message);
}

void MainWindow::on_clearLogButton_clicked()
{
this->ui->logTextEdit->clear();
}

//private slot
void MainWindow::restoreSettings()
{
Expand All @@ -161,18 +210,59 @@ void MainWindow::restoreSettings()
this->ui->startMinimizedCheckbox->setChecked(settings.value("startMinimized").toBool());
if (settings.contains("geometry"))
this->setGeometry(settings.value("geometry").toRect());

//Load the log from disk if it exists
QFile fp(this->logFilePath());
if (fp.exists())
{
//Try to open the file
if (!fp.open(QFile::ReadOnly))
{
const QString error = "Failed to open log file for reading.";
qWarning() << error;
QMessageBox::warning(this,
"Warning",
error);
}

const QByteArray bytes = fp.readAll();
const QString string (bytes);

//Make sure we read ALL the bytes
if (bytes.size() < fp.size())
{
const QString error = "Failed to read complete log file";
qWarning() << error;
QMessageBox::warning(this,
"Warning",
error);
}
else
this->ui->logTextEdit->setPlainText(string);
}
}

//private slot
void MainWindow::saveSettings()
{
//Save the GUI settings
QSettings settings;
settings.setValue("ports",this->ui->portEntryLine->text());
settings.setValue("listenOnStartup",this->ui->listenOnStartCheckbox->isChecked());
settings.setValue("minimizeToTray",this->ui->minimizeToTrayCheckbox->isChecked());
settings.setValue("enableNotifications",this->ui->enableNotificationsCheckbox->isChecked());
settings.setValue("startMinimized",this->ui->startMinimizedCheckbox->isChecked());
settings.setValue("geometry",this->geometry());

//Save the current log to disk
QString errorMessage;
if (!this->writeLogToFile(this->logFilePath(),&errorMessage))
{
qWarning() << errorMessage;
QMessageBox::warning(this,
"Warning",
errorMessage);
}
}

//private slot
Expand Down Expand Up @@ -220,6 +310,12 @@ void MainWindow::handleScanDetected(QHostAddress host, QList<quint16> ports)
this->logMessage(msg);
}

//private static
QString MainWindow::logFilePath()
{
return QDir::homePath() % QDir::separator() % QCoreApplication::applicationName() % "-log.txt";
}


//private
void MainWindow::createTrayIcon()
Expand All @@ -234,5 +330,30 @@ void MainWindow::createTrayIcon()
SLOT(showNormal()));
}

bool MainWindow::writeLogToFile(const QString &filePath, QString *errorMessage)
{
//Try to open the file for writing. Return an error on failure
QFile fp(filePath);
if (!fp.open(QFile::ReadWrite | QFile::Truncate))
{
if (errorMessage)
*errorMessage = "Failed to open " % filePath % " to write the log. Error:" % fp.errorString();
return false;
}

//Try to write the data. Return an error on failure.
QByteArray writeBytes;
writeBytes += this->ui->logTextEdit->toPlainText();

if (fp.write(writeBytes) < writeBytes.size())
{
if (errorMessage)
*errorMessage = "Failed to completely write log file. Error:" % fp.errorString();
return false;
}

//Shouldn't be strictly necessary
fp.close();

return true;
}
7 changes: 6 additions & 1 deletion MainWindow.h
Expand Up @@ -33,6 +33,10 @@ private slots:

void on_startButton_toggled(bool checked);

void on_exportLogButton_clicked();

void on_clearLogButton_clicked();

void restoreSettings();

void saveSettings();
Expand All @@ -47,9 +51,10 @@ private slots:

void handleScanDetected(QHostAddress,QList<quint16>);


private:
static QString logFilePath();
void createTrayIcon();
bool writeLogToFile(const QString& filePath, QString * errorMessage = 0);
Ui::MainWindow *ui;

ScanDetector * detector;
Expand Down
72 changes: 60 additions & 12 deletions MainWindow.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>286</width>
<height>231</height>
<width>395</width>
<height>270</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -38,16 +38,6 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QPushButton" name="startButton">
<property name="text">
<string>Start</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
Expand Down Expand Up @@ -113,6 +103,33 @@
</property>
</widget>
</item>
<item row="5" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="startButton">
<property name="text">
<string>Start</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
<item>
Expand All @@ -122,6 +139,37 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="exportLogButton">
<property name="text">
<string>Export Log</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="clearLogButton">
<property name="text">
<string>Clear Log</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QPlainTextEdit" name="logTextEdit">
<property name="readOnly">
Expand Down
3 changes: 3 additions & 0 deletions PortScanDetector.pro
Expand Up @@ -24,6 +24,9 @@ FORMS += MainWindow.ui
RESOURCES += \
resources.qrc

#This makes the icon work with win32
win32:RC_FILE += icon.rc




Expand Down
4 changes: 4 additions & 0 deletions icon.rc
@@ -0,0 +1,4 @@
IDI_ICON1 ICON DISCARDABLE "target-green16.ico"
IDI_ICON2 ICON DISCARDABLE "target-green64.ico"
IDI_ICON3 ICON DISCARDABLE "target-green32.ico"
IDI_ICON4 ICON DISCARDABLE "target-green128.ico"
Binary file added target-green128.ico
Binary file not shown.
Binary file added target-green16.ico
Binary file not shown.
Binary file added target-green32.ico
Binary file not shown.
Binary file added target-green64.ico
Binary file not shown.

0 comments on commit b0957a1

Please sign in to comment.