diff --git a/src/main.cpp b/src/main.cpp index 4e36e9b4c..be266c6d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,11 +29,16 @@ #include #include #include +#include #include #include "toxme.h" +#include + +#define EXIT_UPDATE_MACX 218 //We track our state using unique exit codes when debugging + #ifdef LOG_TO_FILE static QtMessageHandler dflt; static QTextStream* logFile {nullptr}; @@ -110,7 +115,7 @@ int main(int argc, char *argv[]) } else { - fprintf(stderr, "Couldn't open log file!!!\n"); + fprintf(stderr, "Couldn't open log file!\n"); delete logFile; logFile = nullptr; } @@ -123,6 +128,44 @@ int main(int argc, char *argv[]) qDebug() << "built on: " << __TIME__ << __DATE__ << "(" << TIMESTAMP << ")"; qDebug() << "commit: " << GIT_VERSION << "\n"; +#ifdef Q_OS_MACX + if (qApp->applicationDirPath() != "/Applications/qtox.app/Contents/MacOS") { + qDebug() << "OS X: Not in Applications folder"; + + QMessageBox AskInstall; + AskInstall.setIcon(QMessageBox::Question); + AskInstall.setWindowModality(Qt::ApplicationModal); + AskInstall.setText("Move to Applications folder?"); + AskInstall.setInformativeText("I can move myself to the Applications folder, keeping your downloads folder less cluttered.\r\n"); + AskInstall.setStandardButtons(QMessageBox::Yes|QMessageBox::No); + AskInstall.setDefaultButton(QMessageBox::Yes); + + int AskInstallAttempt = AskInstall.exec(); //Actually ask the user + + if (AskInstallAttempt == QMessageBox::Yes) { + qDebug() << "Installing"; + QProcess *sudoprocess = new QProcess; + QProcess *qtoxprocess = new QProcess; + + QString bindir = qApp->applicationDirPath(); + QString appdir = bindir; + appdir.chop(15); + QString sudo = bindir + "/qtox_sudo rsync -avzh --remove-source-file " + appdir + " /Applications/qtox.app"; + QString qtox = "open /Applications/qtox.app"; + + if (fork() != 0) { //cheap hack + return EXIT_UPDATE_MACX; //Note that if we don't do this the update process will get killed. Also, errors just crash it + } + + sudoprocess->start(sudo); + sudoprocess->waitForFinished(); + qtoxprocess->start(qtox); + + return 0; //Actually kills it + } + } +#endif + // Install Unicode 6.1 supporting font QFontDatabase::addApplicationFont("://DejaVuSans.ttf");