Skip to content

Commit

Permalink
add tools to systray menu #37
Browse files Browse the repository at this point in the history
  • Loading branch information
startx2017 committed Jul 6, 2019
1 parent d6514a5 commit 4e05453
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/fstats/fstats.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
<file>resources/fstats.png</file>
<file>resources/fstats-minimal.png</file>
</qresource>
</RCC>
33 changes: 24 additions & 9 deletions src/fstats/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Error: invalid option\n");
usage();
return 1;
}
}
}

#if QT_VERSION >= 0x050000
struct stat s;
struct stat s;
// test run time dependencies - print warning and continue program
QString ppath = QLibraryInfo::location(QLibraryInfo::PluginsPath);
ppath += "/imageformats/libqsvg.so";
Expand All @@ -69,34 +69,49 @@ int main(int argc, char *argv[]) {
svg_not_found = 1;
}
#endif

// test run time dependencies - exit
if (!which("firejail")) {
fprintf(stderr, "Error: firejail package not found, please install it!\n");
exit(1);
}

// create firetools config directory if it doesn't exist
create_config_directory();

// initialize resources
Q_INIT_RESOURCE(fstats);

QApplication app(argc, argv);
StatsDialog sd;

sd.show();

// Configure system tray
QSystemTrayIcon icon(QIcon(":resources/fstats-minimal.png"));
icon.show();
icon.setToolTip("Firetools (click to open)");
QMenu *trayIconMenu = new QMenu(&sd);
trayIconMenu->addAction(sd.minimizeAction);
trayIconMenu->addAction(sd.restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(sd.quitAction);
icon.setContextMenu(trayIconMenu);
icon.connect(&icon, SIGNAL(activated(QSystemTrayIcon: :ActivationReason)), &sd, SLOT(trayActivated(QSystemTrayIcon: :ActivationReason)));



// direct all errror to /dev/null to work around this qt bug:
// https://bugreports.qt.io/browse/QTBUG-43270
FILE *rv = NULL;
if (!arg_debug) {
rv = freopen( "/dev/null", "w", stderr );
(void) rv;
}

// start application
int tmp = sd.exec();
int tmp = app.exec();
(void) tmp;

if (rv)
fclose(rv);
}
Expand Down
Binary file added src/fstats/resources/fstats-minimal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions src/fstats/stats_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ StatsDialog::StatsDialog(): QDialog(), mode_(MODE_TOP), pid_(0), uid_(0), lts_(f

thread_ = new PidThread();
connect(thread_, SIGNAL(cycleReady()), this, SLOT(cycleReady()));
createTrayActions();
}

StatsDialog::~StatsDialog() {
Expand All @@ -131,6 +132,44 @@ void StatsDialog::cleanStorage() {
storage_network_ = "";
}

// Shutdown sequence
void StatsDialog::main_quit() {
printf("exiting...\n");

qApp->quit();
}

void StatsDialog::trayActivated(QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Context)
return;
if (reason == QSystemTrayIcon::DoubleClick)
return;
if (reason == QSystemTrayIcon::MiddleClick)
return;

if (isVisible()) {
hide();
// stats_->hide();
}
else {
show();
// stats_->hide();
}
}

void StatsDialog::createTrayActions() {
minimizeAction = new QAction(tr("Mi&nimize"), this);
connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
// connect(minimizeAction, SIGNAL(triggered()), stats_, SLOT(hide()));

restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
// connect(restoreAction, SIGNAL(triggered()), stats_, SLOT(show()));

quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), this, SLOT(main_quit()));
}

QString StatsDialog::header() {
QString msg;
if (mode_ == MODE_TOP) {
Expand Down
11 changes: 11 additions & 0 deletions src/fstats/stats_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <pwd.h>
#include <QWidget>
#include <QDialog>
#include <QAction>
#include <QSystemTrayIcon>
#include "fstats.h"

class QTextBrowser;
Expand All @@ -37,9 +39,13 @@ Q_OBJECT
StatsDialog();
~StatsDialog();

private slots:
void main_quit();

public slots:
void cycleReady();
void anchorClicked(const QUrl & link);
void trayActivated(QSystemTrayIcon::ActivationReason);

private:
QString header();
Expand All @@ -52,6 +58,7 @@ public slots:
void updateCaps();
void updateFirewall();
void cleanStorage();
void createTrayActions();

private:
QTextBrowser *procView_;
Expand Down Expand Up @@ -94,6 +101,10 @@ public slots:
QString storage_seccomp_;
QString storage_intro_;
QString storage_network_;
public:
QAction *minimizeAction;
QAction *restoreAction;
QAction *quitAction;
};


Expand Down

0 comments on commit 4e05453

Please sign in to comment.