1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Cleanup main() a bit

This commit is contained in:
tux3 2015-06-05 12:44:22 +02:00
parent 6bf0317e3e
commit 192d9a5e29
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
10 changed files with 106 additions and 73 deletions

View File

@ -440,6 +440,14 @@ unix:!macx {
src/platform/camera/v4l2.cpp
}
macx {
SOURCES += \
src/platform/install_osx.cpp
HEADERS += \
src/platform/install_osx.h
}
SOURCES += \
src/audio.cpp \
src/historykeeper.cpp \

View File

@ -12,7 +12,6 @@
See the COPYING file for more details.
*/
#include "toxme.h"
#include "widget/widget.h"
#include "misc/settings.h"
#include "src/nexus.h"
@ -28,17 +27,15 @@
#include <QCommandLineParser>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFontDatabase>
#include <QMutexLocker>
#include <QProcess>
#include <sodium.h>
#include <unistd.h>
#define EXIT_UPDATE_MACX 218 //We track our state using unique exit codes when debugging
#define EXIT_UPDATE_MACX_FAIL 216
#if defined(Q_OS_MACX) && defined(QT_RELEASE)
#include "platform/install_osx.h"
#endif
#ifdef LOG_TO_FILE
static QTextStream* logFile {nullptr};
@ -118,7 +115,7 @@ int main(int argc, char *argv[])
if (parser.isSet("p"))
{
QString profileName = parser.value("p");
if (QDir(Settings::getSettingsDirPath()).exists(profileName + ".tox"))
if (Profile::exists(profileName))
{
qDebug() << "Setting profile to" << profileName;
if (Profile::isEncrypted(profileName))
@ -147,7 +144,7 @@ int main(int argc, char *argv[])
#ifdef LOG_TO_FILE
logFile = new QTextStream;
QFile logfile(QDir(Settings::getSettingsDirPath()).filePath("qtox.log"));
QFile logfile(Settings::getSettingsDirPath()+"qtox.log");
if (logfile.open(QIODevice::Append))
{
logFile->setDevice(&logfile);
@ -169,60 +166,7 @@ int main(int argc, char *argv[])
qDebug() << "commit: " << GIT_VERSION << "\n";
#if defined(Q_OS_MACX) && defined(QT_RELEASE)
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) {
QProcess *sudoprocess = new QProcess;
QProcess *qtoxprocess = new QProcess;
QString bindir = qApp->applicationDirPath();
QString appdir = bindir;
appdir.chop(15);
QString sudo = bindir + "/qtox_sudo rsync -avzhpltK " + appdir + " /Applications";
QString qtox = "open /Applications/qtox.app";
QString appdir_noqtox = appdir;
appdir_noqtox.chop(8);
if ((appdir_noqtox + "qtox.app") != appdir) //quick safety check
{
qDebug() << "OS X: Attmepted to delete non qTox directory!";
return EXIT_UPDATE_MACX_FAIL;
}
QDir old_app(appdir);
sudoprocess->start(sudo); //Where the magic actually happens, safety checks ^
sudoprocess->waitForFinished();
if (old_app.removeRecursively()) //We've just deleted the running program
{
qDebug() << "OS X: Cleaned up old directory";
}
else
{
qDebug() << "OS X: This should never happen, the directory failed to delete";
}
if (fork() != 0) //Forking is required otherwise it won't actually cleanly launch
return EXIT_UPDATE_MACX;
qtoxprocess->start(qtox);
return 0; //Actually kills it
}
}
osx::moveToAppFolder();
#endif
// Install Unicode 6.1 supporting font

View File

@ -407,14 +407,15 @@ uint32_t Settings::makeProfileId(const QString& profile)
QString Settings::getSettingsDirPath()
{
if (makeToxPortable)
return ".";
return QString(".")+QDir::separator();
// workaround for https://bugreports.qt-project.org/browse/QTBUG-38845
#ifdef Q_OS_WIN
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)
+ QDir::separator() + "AppData" + QDir::separator() + "Roaming" + QDir::separator() + "tox");
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QDir::separator()
+ "AppData" + QDir::separator() + "Roaming" + QDir::separator() + "tox" + QDir::separator());
#else
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + QDir::separator() + "tox");
return QDir::cleanPath(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
+ QDir::separator() + "tox" + QDir::separator());
#endif
}

View File

@ -36,7 +36,7 @@ public:
void createPersonal(QString basename); ///< Write a default personnal settings file for a profile
static QString getSettingsDirPath();
static QString getSettingsDirPath(); ///< The returned path ends with a directory separator
struct DhtServer
{

View File

@ -0,0 +1,64 @@
#include "install_osx.h"
#include <QApplication>
#include <QMessageBox>
#include <QDebug>
#include <QProcess>
#include <QDir>
#include <unistd.h>
void osx::moveToAppFolder()
{
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)
{
QProcess *sudoprocess = new QProcess;
QProcess *qtoxprocess = new QProcess;
QString bindir = qApp->applicationDirPath();
QString appdir = bindir;
appdir.chop(15);
QString sudo = bindir + "/qtox_sudo rsync -avzhpltK " + appdir + " /Applications";
QString qtox = "open /Applications/qtox.app";
QString appdir_noqtox = appdir;
appdir_noqtox.chop(8);
if ((appdir_noqtox + "qtox.app") != appdir) //quick safety check
{
qDebug() << "OS X: Attmepted to delete non qTox directory!";
exit(EXIT_UPDATE_MACX_FAIL);
}
QDir old_app(appdir);
sudoprocess->start(sudo); //Where the magic actually happens, safety checks ^
sudoprocess->waitForFinished();
if (old_app.removeRecursively()) //We've just deleted the running program
qDebug() << "OS X: Cleaned up old directory";
else
qDebug() << "OS X: This should never happen, the directory failed to delete";
if (fork() != 0) //Forking is required otherwise it won't actually cleanly launch
exit(EXIT_UPDATE_MACX);
qtoxprocess->start(qtox);
exit(0); //Actually kills it
}
}
}

View File

@ -0,0 +1,16 @@
#ifndef INSTALLOSX_H
#define INSTALLOSX_H
#ifndef Q_OS_MACX
#error "This file is only meant to be compiled for Mac OSX targets"
#endif
namespace osx
{
static constexpr int EXIT_UPDATE_MACX = 218; // We track our state using unique exit codes when debugging
static constexpr int EXIT_UPDATE_MACX_FAIL = 216;
void moveToAppFolder();
}
#endif // INSTALLOSX_H

View File

@ -54,7 +54,7 @@ Profile* Profile::createProfile(QString name, QString password)
return nullptr;
}
if (profileExists(name))
if (exists(name))
{
qCritical() << "Tried to create profile "<<name<<", but it already exists!";
return nullptr;
@ -108,7 +108,7 @@ void Profile::scanProfiles()
void Profile::importProfile(QString name)
{
assert(!profileExists(name));
assert(!exists(name));
Settings::getInstance().createPersonal(name);
}
@ -237,7 +237,7 @@ void Profile::saveToxSave(QByteArray data)
newProfile = false;
}
bool Profile::profileExists(QString name)
bool Profile::exists(QString name)
{
QString path = Settings::getSettingsDirPath() + QDir::separator() + name;
return QFile::exists(path+".tox") && QFile::exists(path+".ini");

View File

@ -49,7 +49,7 @@ public:
static void scanProfiles();
static QVector<QString> getProfiles();
static bool profileExists(QString name);
static bool exists(QString name);
static bool isEncrypted(QString name); ///< Returns false on error. Checks the actual file on disk.
private:

View File

@ -240,7 +240,7 @@ void ProfileForm::onRenameClicked()
if (name.isEmpty()) break;
name = Core::sanitize(name);
if (Profile::profileExists(name))
if (Profile::exists(name))
GUI::showError(tr("Profile already exists", "rename failure title"),
tr("A profile named \"%1\" already exists.", "rename confirm text").arg(name));
else if (!nexus.getProfile()->rename(name))

View File

@ -92,7 +92,7 @@ void LoginScreen::onCreateNewProfile()
return;
}
if (Profile::profileExists(name))
if (Profile::exists(name))
{
QMessageBox::critical(this, tr("Couldn't create a new profile"), tr("This profile already exists."));
return;