tomb.cpp (5513B)
1 /* Tomb - encrypted storage undertaker 2 * 3 * (c) Copyright 2015 Gianluca Montecchi <gian@grys.it> 4 * 5 * This source code is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Public License as published 7 * by the Free Software Foundation; either version 3 of the License, 8 * or (at your option) any later version. 9 * 10 * This source code is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 * Please refer to the GNU Public License for more details. 14 * 15 * You should have received a copy of the GNU Public License along with 16 * this source code; if not, write to: 17 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 20 21 #include "tomb.h" 22 #include <QDir> 23 #include <QStorageInfo> 24 #include <QMessageBox> 25 #include <QQuickView> 26 27 #include <mntent.h> 28 29 Tomb::Tomb(QWidget *parent) 30 : QDialog(parent) 31 { 32 struct mntent *ent; 33 FILE *aFile; 34 35 36 if (QApplication::arguments().length() > 1) { 37 this->info = QFileInfo(QApplication::arguments().takeAt(1)); 38 this->tombName = QString(info.baseName()); 39 this->tombPath = info.path(); 40 } else { 41 QMessageBox::critical(this, tr("tomb-qt-tray"), tr("You need to specify a Tomb File.\nExiting"), QMessageBox::Ok); 42 exit(EXIT_FAILURE); 43 } 44 45 46 // Build the menĂ¹ 47 this->icon = QIcon( QCoreApplication::applicationDirPath() + QDir::separator()+QString("pixmaps/tomb_icon.png")); 48 this->trayIcon = new QSystemTrayIcon(this->icon); 49 this->trayIconMenu = new QMenu(); 50 51 this->tombBuildMenu(); 52 53 this->trayIcon->setContextMenu(this->trayIconMenu); 54 55 this->trayIcon->setToolTip(QString(info.baseName())); 56 this->trayIcon->show(); 57 this->trayIcon->showMessage(tr("Tomb encrypted undertaker"),tr("We started digging out bones"), QSystemTrayIcon::Information); 58 if (QT_VERSION >= 0x050400) { 59 for (auto volume : QStorageInfo::mountedVolumes()) { 60 if (QString(volume.device()).contains(this->tombName) == true) { 61 this->tombMountPoint = QString(volume.rootPath()); 62 break; 63 } 64 } 65 } else { 66 aFile = setmntent("/proc/mounts", "r"); 67 if (aFile == NULL) { 68 perror("setmntent"); 69 exit(1); 70 } 71 while (NULL != (ent = getmntent(aFile))) { 72 if (QString( ent->mnt_fsname).contains(this->tombName) == true) { 73 this->tombMountPoint = QString(ent->mnt_dir); 74 break; 75 } 76 } 77 endmntent(aFile); 78 } 79 } 80 81 void Tomb::closeEvent(QCloseEvent *event) { 82 event->accept(); 83 } 84 85 void Tomb::tombBuildMenu() { 86 87 // Create the menu items 88 // this->tombOpen = new QAction(tr("Open"), this); 89 //this->trayIconMenu->addAction(tombOpen); 90 91 this->menu_tombExplore = new QAction(tr("Explore"), this); 92 this->trayIconMenu->addAction(menu_tombExplore); 93 94 this->trayIconMenu->addSeparator(); 95 96 this->menu_tombClose = new QAction(tr("Close"), this); 97 this->trayIconMenu->addAction(menu_tombClose); 98 99 this->menu_tombSlam = new QAction(tr("Slam"), this); 100 this->trayIconMenu->addAction(menu_tombSlam); 101 102 connect(this->menu_tombExplore, SIGNAL(triggered()), this, SLOT(tombExplore())); 103 connect(this->menu_tombClose, SIGNAL(triggered()), this, SLOT(tombClose())); 104 connect(this->menu_tombSlam, SIGNAL(triggered()), this, SLOT(tombSlam())); 105 106 } 107 108 void Tomb::tombExplore() { 109 110 QDesktopServices::openUrl(QUrl(QUrl::fromLocalFile(this->tombMountPoint))); 111 112 } 113 114 void Tomb::tombClose() { 115 116 QProcess *tomb; 117 tomb = new QProcess(this); 118 tomb->start("pkexec", QStringList() << "tomb" << "close"); 119 connect(tomb, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(tombCheckCmdRet(int , QProcess::ExitStatus ))); 120 connect(tomb, SIGNAL(error(QProcess::ProcessError)), this, SLOT(tombStartError(QProcess::ProcessError))); 121 tomb->waitForFinished(30000); 122 123 } 124 125 void Tomb::tombSlam() { 126 127 QProcess *tomb; 128 tomb = new QProcess(this); 129 tomb->start("pkexec", QStringList() << "tomb" << "slam"); 130 connect(tomb, SIGNAL(finished(int , QProcess::ExitStatus )), this, SLOT(tombCheckCmdRet(int , QProcess::ExitStatus ))); 131 connect(tomb, SIGNAL(error(QProcess::ProcessError)), this, SLOT(tombStartError(QProcess::ProcessError))); 132 tomb->waitForFinished(30000); 133 134 } 135 136 void Tomb::tombCheckCmdRet(int exitCode, QProcess::ExitStatus exitStatus) { 137 138 if (exitStatus == QProcess::CrashExit) { 139 QMessageBox::critical(this, tr("tomb-qt-tray"), tr("polkit is not installed"),QMessageBox::Ok); 140 } else { 141 if (exitCode != QProcess::NormalExit) { 142 QMessageBox::critical(this, tr("tomb-qt-tray"), tr("The program crashed\nTry to run 'tomb close' in a console"),QMessageBox::Ok); 143 } 144 } 145 146 } 147 148 void Tomb::tombStartError(QProcess::ProcessError err) { 149 150 QString msg = QString(); 151 152 switch (err) { 153 case QProcess::FailedToStart : 154 msg = tr("The process failed to start. Either the invoked program is missing, or you may have insufficient permissions to invoke the program."); 155 break; 156 case QProcess::Crashed: 157 case QProcess::Timedout: 158 case QProcess::ReadError: 159 case QProcess::WriteError: 160 case QProcess::UnknownError: 161 break; 162 } 163 QMessageBox::critical(this, tr("tomb-qt-tray"), msg, QMessageBox::Ok); 164 165 } 166 167 168 Tomb::~Tomb() { 169 170 } 171