00001 #include "MainWindow.h"
00002
00003 CMainWindow::CMainWindow() {
00004 setupUi(this);
00005
00006 connect(actionQuit, SIGNAL( triggered( bool ) ),
00007 qApp, SLOT( quit() ));
00008 connect(actionPreferences, SIGNAL( triggered( bool ) ),
00009 this, SLOT( showPreferences() ));
00010 connect(actionCcln, SIGNAL( triggered( bool ) ),
00011 this, SLOT( showAbout() ));
00012
00013 createActions();
00014 startupSettings();
00015 parseMountPoints();
00016 createTrayIcon();
00017 }
00018
00019 CMainWindow::~CMainWindow() {
00020 delete configuration;
00021 }
00022
00023 void CMainWindow::parseMountPoints() {
00024 QString cmd("df");
00025 readMountPoints = new QProcess(this);
00026
00027 readMountPoints->start(QFile::encodeName(cmd).data(), QStringList() << "-h");
00028
00029 if (!readMountPoints->waitForFinished()) {
00030 QMessageBox::critical(this, tr("Error"), tr("Could not read mount points\n"));
00031 return;
00032 }
00033
00034 QString line = readMountPoints->readAll();
00035
00036 if (line.isEmpty()) {
00037 QMessageBox::critical(this, tr("Error"), tr("Could not read data stream\n"));
00038 return;
00039 }
00040
00041 QStringList partitionLine = line.split("\n", QString::SkipEmptyParts);
00042
00043 SHardDrive *hdd = NULL;
00044 QStringList partitionSplit;
00045
00046 for (int i=0; i<partitionLine.size(); i++) {
00047 QRegExp rx("/dev/*");
00048 rx.setPatternSyntax(QRegExp::Wildcard);
00049
00050 if (rx.exactMatch(partitionLine[i])) {
00051 partitionSplit = partitionLine[i].split(QRegExp("\\s+"), QString::SkipEmptyParts);
00052 hdd = new SHardDrive;
00053 disk.append(hdd);
00054 hdd->filesystem = partitionSplit[0];
00055
00056 if (partitionSplit.size() < 2) {
00057 partitionSplit = partitionLine[i+1].split(QRegExp("\\s+"), QString::SkipEmptyParts);
00058 hdd->size = partitionSplit[0];
00059 hdd->used = partitionSplit[1];
00060 hdd->available = partitionSplit[2];
00061 hdd->usedPercent = partitionSplit[3];
00062 hdd->mountPoint = partitionSplit[4];
00063 } else {
00064 hdd->size = partitionSplit[1];
00065 hdd->used = partitionSplit[2];
00066 hdd->available = partitionSplit[3];
00067 hdd->usedPercent = partitionSplit[4];
00068 hdd->mountPoint = partitionSplit[5];
00069 }
00070 }
00071 }
00072
00073 for (int i=0; i<disk.size(); i++) {
00074 QRegExp rx("%");
00075 QString tmp = disk[i]->usedPercent.replace(rx, "");
00076 int used = tmp.toInt();
00077 hddProgressBar[i]->setRange(0, 100);
00078 QString format;
00079 format.append(disk[i]->available);
00080
00081 format.append(tr("/"));
00082
00083 format.append(disk[i]->size);
00084 format.append("\t");
00085 format.append("(");
00086 format.append(disk[i]->usedPercent);
00087 format.append("%");
00088 format.append(")");
00089 hddProgressBar[i]->setFormat(format);
00090 hddProgressBar[i]->setValue(used);
00091 if (used >= 90) {
00092 hddProgressBar[i]->setStyleSheet("QProgressBar::chunk {background-color: red;}"
00093 "QProgressBar {border: 2px solid grey; border-radius: 5px;"
00094 "text-align: center; font-weight: bold;}");
00095 } else if (used >= 75) {
00096 hddProgressBar[i]->setStyleSheet("QProgressBar::chunk {background-color: orange;}"
00097 "QProgressBar {border: 2px solid grey; border-radius: 5px;"
00098 "text-align: center; font-weight: bold;}");
00099 }
00100
00101 if (used < 75) {
00102 hddProgressBar[i]->setStyleSheet("QProgressBar::chunk {background-color: green;}"
00103 "QProgressBar {border: 2px solid grey; border-radius: 5px;"
00104 "text-align: center; font-weight: bold;}");
00105 }
00106
00107 QString toolTip;
00108 toolTip.append(tr("Filesystem:"));
00109 toolTip.append(disk[i]->filesystem);
00110 toolTip.append("\n");
00111 toolTip.append(tr("Mount:"));
00112 toolTip.append(disk[i]->mountPoint);
00113
00114 hddProgressBar[i]->setMinimumSize(150, 22);
00115 hddProgressBar[i]->setMaximumSize(150, 22);
00116 hddProgressBar[i]->setToolTip(toolTip);
00117 hddProgressBar[i]->show();
00118
00119 QString infoText;
00120
00121
00122
00123 infoText.append("<b>Mount: </b>");
00124 infoText.append(disk[i]->mountPoint);
00125
00126 hddInfoLabel[i]->setText(infoText);
00127 hddInfoLabel[i]->adjustSize();
00128 hddInfoLabel[i]->show();
00129 }
00130
00131 delete hdd;
00132 }
00133
00134 void CMainWindow::startupSettings() {
00135 hddProgressBar.append(hddProgressBar1);
00136 hddProgressBar.append(hddProgressBar2);
00137 hddProgressBar.append(hddProgressBar3);
00138 hddProgressBar.append(hddProgressBar4);
00139 hddProgressBar.append(hddProgressBar5);
00140 hddProgressBar.append(hddProgressBar6);
00141 hddProgressBar.append(hddProgressBar7);
00142 hddProgressBar.append(hddProgressBar8);
00143 hddProgressBar.append(hddProgressBar9);
00144 hddProgressBar.append(hddProgressBar10);
00145 hddProgressBar.append(hddProgressBar11);
00146
00147 hddInfoLabel.append(hddInfoLabel1);
00148 hddInfoLabel.append(hddInfoLabel2);
00149 hddInfoLabel.append(hddInfoLabel3);
00150 hddInfoLabel.append(hddInfoLabel4);
00151 hddInfoLabel.append(hddInfoLabel5);
00152 hddInfoLabel.append(hddInfoLabel6);
00153 hddInfoLabel.append(hddInfoLabel7);
00154 hddInfoLabel.append(hddInfoLabel8);
00155 hddInfoLabel.append(hddInfoLabel9);
00156 hddInfoLabel.append(hddInfoLabel10);
00157 hddInfoLabel.append(hddInfoLabel11);
00158
00159 for (int i=0; i<hddProgressBar.size(); i++) {
00160 hddProgressBar[i]->hide();
00161 hddInfoLabel[i]->hide();
00162 }
00163 }
00164
00165 void CMainWindow::showPreferences() {
00166 preferences = new CPreferences;
00167
00168 if (preferences->exec() == CPreferences::Accepted) {
00169 loadSettings();
00170 }
00171 }
00172
00173 void CMainWindow::showAbout() {
00174 about = new CAboutDialog;
00175 about->show();
00176 }
00177
00178 void CMainWindow::createActions() {
00179 restoreAction = new QAction(tr("&Restore"), this);
00180 connect(restoreAction, SIGNAL( triggered( bool ) ),
00181 this, SLOT( showNormal() ));
00182
00183 quitAction = new QAction(tr("&Quit"), this);
00184 connect(quitAction, SIGNAL( triggered( bool ) ),
00185 qApp, SLOT( quit() ));
00186 }
00187
00188 void CMainWindow::createTrayIcon() {
00189 trayIconMenu = new QMenu(this);
00190 trayIconMenu->addAction(restoreAction);
00191 trayIconMenu->addSeparator();
00192 trayIconMenu->addAction(quitAction);
00193
00194 QIcon icon(":/images/qconfigcontrol.png");
00195 trayIcon = new QSystemTrayIcon(this);
00196 trayIcon->setIcon(icon);
00197 trayIcon->setContextMenu(trayIconMenu);
00198 trayIcon->setToolTip(tr("ccln-client"));
00199 }
00200
00201 void CMainWindow::closeEvent( QCloseEvent *event ) {
00202 if ( trayIcon->isVisible() ) {
00203 QMessageBox::information(this, tr("Systray"), tr("The program will keep running int the "
00204 "system tray. To terminate the program, "
00205 "choose <b>Quit</b> in the context menu "
00206 "of the system tray entry"));
00207 hide();
00208 event->ignore();
00209 }
00210 }
00211
00212 void CMainWindow::loadSettings() {
00213 configuration = new CSettings;
00214 trayIcon->setVisible(configuration->settings.systemTray);
00215
00216 if (configuration->settings.otherLanuage) {
00217 if (QFile::exists(configuration->settings.language)) {
00218 QTranslator *translator = new QTranslator;
00219 translator->load(configuration->settings.language);
00220 QApplication::installTranslator(translator);
00221 } else {
00222 QMessageBox::critical(this, tr("Error"), QString("No language file found in\n%1").arg(configuration->settings.language));
00223 configuration->settings.otherLanuage = false;
00224 return;
00225 }
00226 }
00227 }