/* main.cpp from tuxboot Copyright (C) 2007-2008 Geza Kovacs This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program 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. See the GNU General Public License at for more details. */ #include "tuxboot.h" #ifdef Q_OS_WIN32 void configsysUndo(QString uninstPathL) { if (!QFile::copy(QDir::toNativeSeparators(QString("%1unetbtin/config.sys").arg(uninstPathL)), QDir::toNativeSeparators(QString("%1config.sys").arg(uninstPathL)))) { QFile::remove(QDir::toNativeSeparators(QString("%1config.sys").arg(uninstPathL))); QFile::copy(QDir::toNativeSeparators(QString("%1unetbtin/config.sys").arg(uninstPathL)), QDir::toNativeSeparators(QString("%1config.sys").arg(uninstPathL))); } SetFileAttributesA(QDir::toNativeSeparators(QString("%1config.sys").arg(uninstPathL)).toLocal8Bit(), FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE); } void bootiniUndo(QString uninstPathL) { if (!QFile::copy(QDir::toNativeSeparators(QString("%1unetbtin/boot.ini").arg(uninstPathL)), QDir::toNativeSeparators(QString("%1boot.ini").arg(uninstPathL)))) { QFile::remove(QDir::toNativeSeparators(QString("%1boot.ini").arg(uninstPathL))); QFile::copy(QDir::toNativeSeparators(QString("%1unetbtin/boot.ini").arg(uninstPathL)), QDir::toNativeSeparators(QString("%1boot.ini").arg(uninstPathL))); } SetFileAttributesW(LPWSTR(QDir::toNativeSeparators(QString("%1boot.ini").arg(uninstPathL)).utf16()), FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE); } void vistabcdUndo(QString uninstPathL) { QSettings vdtustor("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tuxboot", QSettings::NativeFormat); QVariant warch64varL(QVariant::Bool); warch64varL = vdtustor.value("WArch64"); bool warch64L = warch64varL.value(); if (warch64L) { tuxboot::callexternapp(QDir::toNativeSeparators(QString("%1unetbtin/emtxfile.exe").arg(uninstPathL)), QDir::toNativeSeparators(QString("%1unetbtin/vbcdundo.bat runas").arg(uninstPathL))); } else { tuxboot::callexternapp(QDir::toNativeSeparators(QString("%1unetbtin/vbcdundo.bat").arg(uninstPathL)), ""); } vdtustor.remove("WArch64"); } #endif void clearOutDir(QString pDirToDel) { QDir dirToDel(pDirToDel); QStringList rmfileList = dirToDel.entryList(QDir::Files); for (int i = 0; i < rmfileList.size(); ++i) { QFile::setPermissions(QDir::toNativeSeparators(QString("%1/%2").arg(pDirToDel).arg(rmfileList.at(i))), QFile::WriteOther); QFile::remove(QDir::toNativeSeparators(QString("%1/%2").arg(pDirToDel).arg(rmfileList.at(i)))); } dirToDel.rmdir(pDirToDel); } void ubnUninst() { #ifdef Q_OS_UNIX QSettings chkinstL(QSettings::SystemScope, "Tuxboot"); QString uninstPath = "/"; QString uninstsubDir = QDir::toNativeSeparators(QString("%1boot/").arg(uninstPath)); if (QFile::exists(QString("%1ubninit").arg(uninstsubDir))) QFile::remove(QString("%1ubninit").arg(uninstsubDir)); if (QFile::exists(QString("%1ubnkern").arg(uninstsubDir))) QFile::remove(QString("%1ubnkern").arg(uninstsubDir)); if (QFile::exists(QString("%1grub/menu.lst.bak").arg(uninstsubDir))) { if (QFile::exists(QString("%1grub/menu.lst").arg(uninstsubDir))) { QFile::remove(QString("%1grub/menu.lst").arg(uninstsubDir)); } QFile::rename(QString("%1grub/menu.lst.bak").arg(uninstsubDir), QString("%1grub/menu.lst").arg(uninstsubDir)); } if (QFile::exists(QString("%1grub/grub.cfg.bak").arg(uninstsubDir))) { if (QFile::exists(QString("%1grub/grub.cfg").arg(uninstsubDir))) { QFile::remove(QString("%1grub/grub.cfg").arg(uninstsubDir)); } QFile::rename(QString("%1grub/grub.cfg.bak").arg(uninstsubDir), QString("%1grub/grub.cfg").arg(uninstsubDir)); } #endif #ifdef Q_OS_WIN32 QSettings autostrt("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce", QSettings::NativeFormat); if (autostrt.contains("tuxboot Uninstaller")) { autostrt.remove("tuxboot Uninstaller"); } QSettings chkinstL("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tuxboot", QSettings::NativeFormat); QVariant uninstvar(QVariant::String); uninstvar = chkinstL.value("Location"); QString uninstPath = uninstvar.value(); QString uninstsubDir = QDir::toNativeSeparators(QString("%1unetbtin/").arg(uninstPath)); if (QSysInfo::WindowsVersion == QSysInfo::WV_32s || QSysInfo::WindowsVersion == QSysInfo::WV_95 || QSysInfo::WindowsVersion == QSysInfo::WV_98 || QSysInfo::WindowsVersion == QSysInfo::WV_Me) { configsysUndo(uninstPath); } else if (QSysInfo::WindowsVersion == QSysInfo::WV_NT || QSysInfo::WindowsVersion == QSysInfo::WV_2000 || QSysInfo::WindowsVersion == QSysInfo::WV_XP || QSysInfo::WindowsVersion == QSysInfo::WV_2003 ) { bootiniUndo(uninstPath); } else if (QSysInfo::WindowsVersion == QSysInfo::WV_VISTA) { vistabcdUndo(uninstPath); } else { configsysUndo(uninstPath); bootiniUndo(uninstPath); vistabcdUndo(uninstPath); } #endif if (QFile::exists(QString("%1ubnfilel.txt").arg(uninstsubDir))) { QFile ubnfilelF(QString("%1ubnfilel.txt").arg(uninstsubDir)); ubnfilelF.open(QIODevice::ReadOnly | QIODevice::Text); QTextStream ubnfilelS(&ubnfilelF); while (!ubnfilelS.atEnd()) { QFile::remove(QString("%1%2").arg(uninstPath).arg(ubnfilelS.readLine())); } ubnfilelF.close(); QFile::remove(QString("%1ubnfilel.txt").arg(uninstsubDir)); } if (QFile::exists(QString("%1ubnpathl.txt").arg(uninstsubDir))) { QFile ubnpathlF(QString("%1ubnpathl.txt").arg(uninstsubDir)); ubnpathlF.open(QIODevice::ReadOnly | QIODevice::Text); QTextStream ubnpathlS(&ubnpathlF); QDir unrdir(uninstPath); while (!ubnpathlS.atEnd()) { unrdir.rmdir(ubnpathlS.readLine()); } ubnpathlF.close(); QFile::remove(QString("%1ubnpathl.txt").arg(uninstsubDir)); } #ifdef Q_OS_WIN32 clearOutDir(QDir::toNativeSeparators(QString("%1unetbtin").arg(uninstPath))); QFile::remove(QDir::toNativeSeparators(QString("%1ubnldr.exe").arg(uninstPath))); QFile::remove(QDir::toNativeSeparators(QString("%1ubnldr").arg(uninstPath))); QFile::remove(QDir::toNativeSeparators(QString("%1ubnldr.mbr").arg(uninstPath))); #endif chkinstL.clear(); QMessageBox finmsgb; finmsgb.setIcon(QMessageBox::Information); finmsgb.setWindowTitle(uninstaller::tr("Uninstallation Complete")); finmsgb.setText(uninstaller::tr("Tuxboot has been uninstalled.")); finmsgb.setStandardButtons(QMessageBox::Ok); switch (finmsgb.exec()) { case QMessageBox::Ok: break; default: break; } } QString checkforgraphicalsu(QString graphicalsu) { QProcess whereiscommand; whereiscommand.start(QString("which %1").arg(graphicalsu)); whereiscommand.waitForFinished(-1); QString commandbinpath = QString(whereiscommand.readAll()).trimmed(); if (!commandbinpath.isEmpty() && QFile::exists(commandbinpath)){ //printf("sudo is %s\n", qPrintable(commandbinpath)); return commandbinpath; } else return "REQCNOTFOUND"; } int main(int argc, char **argv) { QApplication app(argc, argv, true); QTranslator custranldr; QTranslator translator; QString tnapplang; QString tnappcoun; QString clangcode = ""; QStringList allappargs = app.arguments(); QString current_home_path; QList > oppairs; for (QList::const_iterator i = allappargs.constBegin(); i < allappargs.constEnd(); ++i) { if (i->count('=') == 1) oppairs.append(QPair(i->section('=', 0, 0).simplified(), i->section('=',1, 1).simplified())); } for (QList >::const_iterator i = oppairs.constBegin(); i < oppairs.constEnd(); ++i) { if (i->first.contains("lang", Qt::CaseInsensitive)) { clangcode = i->second; tnapplang = clangcode.left(2); if (clangcode.contains('_') && clangcode.size() == 5) { tnappcoun = clangcode.section('_', -1, -1); } break; } } if (clangcode.isEmpty()) { clangcode = QLocale::system().name(); tnapplang = clangcode.left(2); if (clangcode.contains('_') && clangcode.size() == 5) { tnappcoun = clangcode.section('_', -1, -1); } } QDir applocdir(app.applicationDirPath()); QStringList applocfiles = applocdir.entryList(QStringList() << "*.qm", QDir::Files); if (!applocfiles.isEmpty()) { QString custqmfilepath = applocfiles.at(0); if (!applocfiles.filter("tuxboot").isEmpty()) { custqmfilepath = applocfiles.filter("tuxboot").at(0); if (!applocfiles.filter("tuxboot").filter(tnapplang).isEmpty()) { custqmfilepath = applocfiles.filter("tuxboot").filter(tnapplang).at(0); if (!tnappcoun.isEmpty() && !applocfiles.filter("tuxboot").filter(tnapplang).filter(tnappcoun).isEmpty()) custqmfilepath = applocfiles.filter("tuxboot").filter(tnapplang).filter(tnappcoun).at(0); } } if (custranldr.load(custqmfilepath, app.applicationDirPath())) app.installTranslator(&custranldr); } if (!tnappcoun.isEmpty() && QFile::exists(QString("%1/tuxboot_%2_%3.qm").arg(app.applicationDirPath()).arg(tnapplang).arg(tnappcoun)) && translator.load(QString("%1/tuxboot_%2_%3.qm").arg(app.applicationDirPath()).arg(tnapplang).arg(tnappcoun))) { app.installTranslator(&translator); } else if (!tnappcoun.isEmpty() && QFile::exists(QString(":/tuxboot_%1_%2.qm").arg(tnapplang).arg(tnappcoun)) && translator.load(QString(":/tuxboot_%1_%2.qm").arg(tnapplang).arg(tnappcoun))) { app.installTranslator(&translator); } else if (!tnappcoun.isEmpty() && QFile::exists(QString("/usr/share/tuxboot/tuxboot_%1_%2.qm").arg(tnapplang).arg(tnappcoun)) && translator.load(QString("/usr/share/tuxboot/tuxboot_%1_%2.qm").arg(tnapplang).arg(tnappcoun))) { app.installTranslator(&translator); } else if (QFile::exists(QString("%1/tuxboot_%2.qm").arg(app.applicationDirPath(), tnapplang)) && translator.load(QString("%1/tuxboot_%2.qm").arg(app.applicationDirPath(), tnapplang))) { app.installTranslator(&translator); } else if (QFile::exists(QString(":/tuxboot_%1.qm").arg(tnapplang)) && translator.load(QString(":/tuxboot_%1.qm").arg(tnapplang))) { app.installTranslator(&translator); } else if (QFile::exists(QString("/usr/share/tuxboot/tuxboot_%1.qm").arg(tnapplang)) && translator.load(QString("/usr/share/tuxboot/tuxboot_%1.qm").arg(tnapplang))) { app.installTranslator(&translator); } else { tnapplang = "en"; tnappcoun = "US"; clangcode = "en_US"; } app.installTranslator(&translator); if (QObject::tr("LeftToRight") == "RightToLeft") app.setLayoutDirection(Qt::RightToLeft); if (current_home_path.isEmpty()){ current_home_path = QDir::homePath(); } for (QList >::const_iterator i = oppairs.constBegin(); i < oppairs.constEnd(); ++i) { if (i->first.contains("homepath", Qt::CaseInsensitive)){ current_home_path = i->second; break; } } #ifdef Q_OS_UNIX bool disabledrootcheck = false; for (QList >::const_iterator i = oppairs.constBegin(); i < oppairs.constEnd(); ++i) { if (i->first.contains("rootcheck", Qt::CaseInsensitive)) { if (i->second.contains('n', Qt::CaseInsensitive)) disabledrootcheck = true; break; } } if (!disabledrootcheck) { QProcess whoamip; whoamip.start("whoami"); whoamip.waitForFinished(); if (QString(whoamip.readAll()).remove("\r").remove("\n") != "root") { QString argsconc = ""; for (int i = 1; i < allappargs.size(); ++i) { argsconc += QString("\"%1\" ").arg(allappargs.at(i)); } argsconc += "'rootcheck=no' "; argsconc += QString("\"homepath=%1\" ").arg(current_home_path); /* markup by Ceasar QString pkexeclocation = checkforgraphicalsu("pkexec"); if (pkexeclocation != "REQCNOTFOUND") { printf("%s\n", qPrintable(QString("%1 env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY %2 %3").arg(pkexeclocation).arg(app.applicationFilePath()).arg(argsconc))); QProcess::startDetached(QString("%1 env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY %2 %3").arg(pkexeclocation).arg(app.applicationFilePath()).arg(argsconc)); return 0; } */ QString gksudolocation = checkforgraphicalsu("gksudo"); if (gksudolocation != "REQCNOTFOUND") { //note: disable -k option for bug with ubuntu 10.10.; get HOME before su to avoid HOME path changed //QString suOption = "-k"; //QProcess::startDetached(QString("%1 %2 %3 %4").arg(gksudolocation).arg(suOption).arg(app.applicationFilePath()).arg(argsconc)); QProcess::startDetached(QString("%1 %2 %3").arg(gksudolocation).arg(app.applicationFilePath()).arg(argsconc)); return 0; } QString gksulocation = checkforgraphicalsu("gksu"); if (gksulocation != "REQCNOTFOUND") { QProcess::startDetached(QString("%1 %2 %3").arg(gksulocation).arg(app.applicationFilePath()).arg(argsconc)); return 0; } QString kdesulocation = checkforgraphicalsu("kdesu"); if (kdesulocation != "REQCNOTFOUND") { QProcess::startDetached(QString("%1 %2 %3").arg(kdesulocation).arg(app.applicationFilePath()).arg(argsconc)); return 0; } QString gnomesulocation = checkforgraphicalsu("gnomesu"); if (gnomesulocation != "REQCNOTFOUND") { QProcess::startDetached(QString("%1 %2 %3").arg(gnomesulocation).arg(app.applicationFilePath()).arg(argsconc)); return 0; } QString kdesudolocation = checkforgraphicalsu("kdesudo"); if (kdesudolocation != "REQCNOTFOUND") { QProcess::startDetached(QString("%1 %2 %3").arg(kdesudolocation).arg(app.applicationFilePath()).arg(argsconc)); return 0; } QMessageBox rootmsgb; rootmsgb.setIcon(QMessageBox::Warning); rootmsgb.setWindowTitle(uninstaller::tr("Must run as root")); rootmsgb.setTextFormat(Qt::RichText); rootmsgb.setText(uninstaller::tr("Tuxboot must be run as root. Close it, and re-run using either:
sudo %1
or:
su -c '%1'").arg(app.applicationFilePath())); rootmsgb.setStandardButtons(QMessageBox::Ok); switch (rootmsgb.exec()) { case QMessageBox::Ok: break; default: break; } } } #endif #ifdef Q_OS_WIN32 QString Compatkey = QDir::toNativeSeparators(QString("%1").arg(app.arguments().at(0))); QSettings CompatFlags("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers", QSettings::NativeFormat); CompatFlags.setValue(Compatkey, "VISTARTM"); CompatFlags.sync(); QSettings CompatAssist("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Compatibility Assistant\\Persisted", QSettings::NativeFormat); CompatAssist.setValue(Compatkey, 1); CompatAssist.sync(); QSettings chkinst("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Tuxboot", QSettings::NativeFormat); #endif #ifdef Q_OS_UNIX QSettings chkinst(QSettings::SystemScope, "Tuxboot"); #endif if (chkinst.contains("Location")) { QMessageBox uninstmsgb; uninstmsgb.setIcon(QMessageBox::Information); uninstmsgb.setWindowTitle(uninstaller::tr("Tuxboot Uninstaller")); uninstmsgb.setText(uninstaller::tr("Tuxboot is currently installed. Remove the existing version?")); uninstmsgb.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); switch (uninstmsgb.exec()) { case QMessageBox::Ok: { ubnUninst(); } case QMessageBox::Cancel: break; default: break; } return 0; } tuxboot tuxboot; tuxboot.appNlang = tnapplang; tuxboot.appDir = QDir::toNativeSeparators(QString("%1/").arg(app.applicationDirPath())); tuxboot.appLoc = app.applicationFilePath(); tuxboot.userhomeDir = current_home_path; bool automate = tuxboot.ubninitialize(oppairs); tuxboot.show(); if (automate) tuxboot.on_okbutton_clicked(); return app.exec(); }