void About::aboutInit() { setModal(true); resize(400, 400); setWindowTitle("About"); setWindowIcon(QIcon("icons/backupsoft.png")); QWidget *icon = new QWidget(this); icon->setStyleSheet("background-image: url(icons/backupsoft.png)"); icon->setGeometry(250 , 10, 100, 100); QLabel *title = new QLabel("BackupSoft", this); title->setFont(QFont("Helvetica", 25, 10, false)); title->setGeometry(10, 10, 200, 30); QLabel *version = new QLabel("Copyright 2010 by\nMichael Kohler and Fabian Gammenthaler\n\nVersion: 1.0", this); version->setFont(QFont("Helvetica", 8, 2, false)); version->setGeometry(10, 70, 200, 55); QTextEdit *licence = new QTextEdit(this); licence->setGeometry(10, 160, 380, 230); licence->setReadOnly(true); QFile *file = new QFile("licence.txt"); if (file->open(QIODevice::ReadOnly)) { QTextStream *stream = new QTextStream(file); licence->setText(stream->readAll()); } else { QString errorMsg = "Could not open licence.txt.. Please make sure it is existent and readable."; AlertWindow *alertWin = new AlertWindow("ERROR", "", errorMsg); alertWin->show(); } }
void ProfileManager::saveProfiles() { QString name = nameField->text(); QString src = sourceField->text(); QString dest = destinationField->text(); QString state = tr("Deactivated"); if (stateBox->checkState()) { state = tr("Activated"); } // Check wether directory even exists QDir *srcDir = new QDir(src); QDir *destDir = new QDir(dest); if (srcDir->exists() && destDir->exists()) { QString dataString = "\n" + name + "|" + src + "|" + dest + "|" + state; if(origin == 1) { QByteArray dataBA = dataString.toLatin1(); helper->writeFile("profile.bs", true, dataBA); } if(origin == 2) { QString oldProfile; for(int i = 0; i < 4; i++) { oldProfile.append(extractedInfo.value(i)); if(i != 3) oldProfile.append('|'); } helper->overwriteFile("profile.bs", oldProfile, true, dataString); } this->close(); } else { QString msg = tr("Either the source or the destination path doesn't exist. Please file it accordingly or create the needed directories."); AlertWindow *alertDoesntExist = new AlertWindow("ERROR", msg); alertDoesntExist->show(); } }
QStringList* Helper::readFile(QString aFileName) { // read the data line by line QFile *file = new QFile(aFileName); QStringList *data = new QStringList(); if (file->open(QIODevice::ReadOnly) ) { // file opened successfully QTextStream *tstream = new QTextStream(file); while (!tstream->atEnd()) { data->append(tstream->readLine()); } file->close(); // ignore whitespaces for (int i = 0; i < data->length(); i++) { if (data->value(i) == "") { data->removeAt(i); } } } else { QString msg = tr("Could not open %1.. Please make sure it is existent and readable.").arg(aFileName); AlertWindow *alertWin = new AlertWindow("ERROR", msg); alertWin->show(); } return data; }
void Helper::overwriteFile(QString aFileName, QString aProfile, bool aAppend, QString aData) { QStringList *fileData; QString data, temp; QFile file(aFileName); int index; bool success; fileData = readFile(aFileName); for(int i = 0; i < fileData->length(); i++) { temp = fileData->value(i); if(temp == aProfile) { index = i; break; } } file.open(QIODevice::WriteOnly); success = file.remove(); if(success) { fileData->replace(index, aData); for(int i = 0; i < fileData->length(); i++) { data = fileData->value(i) + "\n"; writeFile(aFileName, aAppend, data.toLatin1()); } file.close(); } else { QString msg = tr("An error occured while overwriting the configuration file. We are sorry for the inconvenience."); AlertWindow *alertWin = new AlertWindow("ERROR", msg); alertWin->show(); } }
void Helper::writeFile(QString aFileName, bool aAppend, QByteArray aData) { QFile file(aFileName); bool success; if (aAppend) success = file.open(QIODevice::WriteOnly | QIODevice::Append); else success = file.open(QIODevice::WriteOnly); if (success) { file.write(aData); file.close(); } else { QString msg = tr("Could not open %1.. Please make sure it is existent and readable.").arg(aFileName); AlertWindow *alertWin = new AlertWindow("ERROR", msg); alertWin->show(); } }
void Helper::deleteProfile(QString aFileName, QString aProfile) { QStringList *fileData; QString data, temp; QFile file(aFileName); int index; bool success; bool aAppend = true; fileData = readFile(aFileName); for(int i = 0; i < fileData->length(); i++) { temp = fileData->value(i); if(temp == aProfile) { index = i; break; } } file.open(QIODevice::WriteOnly); success = file.remove(); if(success) { fileData->removeAt(index); if (fileData->length() == 0) { writeFile(aFileName, aAppend, data.toLatin1()); } else { for(int i = 0; i < fileData->length(); i++) { data = fileData->value(i); data.append("\n"); writeFile(aFileName, aAppend, data.toLatin1()); } } file.close(); } else { QString msg = tr("Error while overwriting the file. Please try again."); AlertWindow *alertWin = new AlertWindow("ERROR", msg); alertWin->show(); } }