-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomb.cpp
171 lines (131 loc) · 5.38 KB
/
tomb.cpp
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
/* Tomb - encrypted storage undertaker
*
* (c) Copyright 2015 Gianluca Montecchi <[email protected]>
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Public License as published
* by the Free Software Foundation; either version 3 of the License,
* or (at your option) any later version.
*
* This source code 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.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "tomb.h"
#include <QDir>
#include <QStorageInfo>
#include <QMessageBox>
#include <QQuickView>
#include <mntent.h>
Tomb::Tomb(QWidget *parent)
: QDialog(parent)
{
struct mntent *ent;
FILE *aFile;
if (QApplication::arguments().length() > 1) {
this->info = QFileInfo(QApplication::arguments().takeAt(1));
this->tombName = QString(info.baseName());
this->tombPath = info.path();
} else {
QMessageBox::critical(this, tr("tomb-qt-tray"), tr("You need to specify a Tomb File.\nExiting"), QMessageBox::Ok);
exit(EXIT_FAILURE);
}
// Build the menù
this->icon = QIcon( QCoreApplication::applicationDirPath() + QDir::separator()+QString("pixmaps/tomb_icon.png"));
this->trayIcon = new QSystemTrayIcon(this->icon);
this->trayIconMenu = new QMenu();
this->tombBuildMenu();
this->trayIcon->setContextMenu(this->trayIconMenu);
this->trayIcon->setToolTip(QString(info.baseName()));
this->trayIcon->show();
this->trayIcon->showMessage(tr("Tomb encrypted undertaker"),tr("We started digging out bones"), QSystemTrayIcon::Information);
if (QT_VERSION >= 0x050400) {
for (auto volume : QStorageInfo::mountedVolumes()) {
if (QString(volume.device()).contains(this->tombName) == true) {
this->tombMountPoint = QString(volume.rootPath());
break;
}
}
} else {
aFile = setmntent("/proc/mounts", "r");
if (aFile == NULL) {
perror("setmntent");
exit(1);
}
while (NULL != (ent = getmntent(aFile))) {
if (QString( ent->mnt_fsname).contains(this->tombName) == true) {
this->tombMountPoint = QString(ent->mnt_dir);
break;
}
}
endmntent(aFile);
}
}
void Tomb::closeEvent(QCloseEvent *event) {
event->accept();
}
void Tomb::tombBuildMenu() {
// Create the menu items
// this->tombOpen = new QAction(tr("Open"), this);
//this->trayIconMenu->addAction(tombOpen);
this->menu_tombExplore = new QAction(tr("Explore"), this);
this->trayIconMenu->addAction(menu_tombExplore);
this->trayIconMenu->addSeparator();
this->menu_tombClose = new QAction(tr("Close"), this);
this->trayIconMenu->addAction(menu_tombClose);
this->menu_tombSlam = new QAction(tr("Slam"), this);
this->trayIconMenu->addAction(menu_tombSlam);
connect(this->menu_tombExplore, SIGNAL(triggered()), this, SLOT(tombExplore()));
connect(this->menu_tombClose, SIGNAL(triggered()), this, SLOT(tombClose()));
connect(this->menu_tombSlam, SIGNAL(triggered()), this, SLOT(tombSlam()));
}
void Tomb::tombExplore() {
QDesktopServices::openUrl(QUrl(QUrl::fromLocalFile(this->tombMountPoint)));
}
void Tomb::tombClose() {
QProcess *tomb;
tomb = new QProcess(this);
tomb->start("pkexec", QStringList() << "tomb" << "close");
connect(tomb, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(tombCheckCmdRet(int , QProcess::ExitStatus )));
connect(tomb, SIGNAL(error(QProcess::ProcessError)), this, SLOT(tombStartError(QProcess::ProcessError)));
tomb->waitForFinished(30000);
}
void Tomb::tombSlam() {
QProcess *tomb;
tomb = new QProcess(this);
tomb->start("pkexec", QStringList() << "tomb" << "slam");
connect(tomb, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(tombCheckCmdRet(int , QProcess::ExitStatus )));
connect(tomb, SIGNAL(error(QProcess::ProcessError)), this, SLOT(tombStartError(QProcess::ProcessError)));
tomb->waitForFinished(30000);
}
void Tomb::tombCheckCmdRet(int exitCode, QProcess::ExitStatus exitStatus) {
if (exitStatus == QProcess::CrashExit) {
QMessageBox::critical(this, tr("tomb-qt-tray"), tr("polkit is not installed"),QMessageBox::Ok);
} else {
if (exitCode != QProcess::NormalExit) {
QMessageBox::critical(this, tr("tomb-qt-tray"), tr("The program crashed\nTry to run 'tomb close' in a console"),QMessageBox::Ok);
}
}
}
void Tomb::tombStartError(QProcess::ProcessError err) {
QString msg = QString();
switch (err) {
case QProcess::FailedToStart :
msg = tr("The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program.");
break;
case QProcess::Crashed:
case QProcess::Timedout:
case QProcess::ReadError:
case QProcess::WriteError:
case QProcess::UnknownError:
break;
}
QMessageBox::critical(this, tr("tomb-qt-tray"), msg, QMessageBox::Ok);
}
Tomb::~Tomb() {
}