Skip to content

Commit

Permalink
refactor: A bit of cleanup in tox/profile creation/loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 5, 2025
1 parent 6d6d83e commit 681144b
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 156 deletions.
4 changes: 3 additions & 1 deletion src/appmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,10 @@ int AppManager::startGui(QCommandLineParser& parser)
Profile* profile = nullptr;
if (autoLogin && Profile::exists(profileName, settings->getPaths())
&& !Profile::isEncrypted(profileName, settings->getPaths())) {
// Release ownership temporarily because we're calling Qt slot bootstrapWithProfile below.
profile = Profile::loadProfile(profileName, QString(), *settings, &parser, *cameraSource,
*messageBoxManager);
*messageBoxManager)
.release();
if (profile == nullptr) {
QMessageBox::information(nullptr, tr("Error"), tr("Failed to load profile automatically."));
}
Expand Down
64 changes: 16 additions & 48 deletions src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,26 @@ void Core::registerCallbacks(Tox* tox)
* @param settings Settings specific to Core
* @return nullptr or a Core object ready to start
*/
ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings& settings,
IBootstrapListGenerator& bootstrapNodes, ToxCoreErrors* err)
std::pair<ToxCorePtr, Core::ToxCoreErrors> Core::makeToxCore(const QByteArray& savedata,
const ICoreSettings& settings,
IBootstrapListGenerator& bootstrapNodes)
{
auto* thread = new QThread();
if (thread == nullptr) {
qCritical() << "Could not allocate Core thread";
return {};
return {nullptr, ToxCoreErrors::ERROR_ALLOC};
}
thread->setObjectName("qTox Core");

auto toxOptions = ToxOptions::makeToxOptions(savedata, settings);
if (toxOptions == nullptr) {
qCritical() << "Could not allocate ToxOptions data structure";
if (err != nullptr) {
*err = ToxCoreErrors::ERROR_ALLOC;
}
return {};
return {nullptr, ToxCoreErrors::ERROR_ALLOC};
}

ToxCorePtr core(new Core(thread, bootstrapNodes, settings));
if (core == nullptr) {
if (err != nullptr) {
*err = ToxCoreErrors::ERROR_ALLOC;
}
return {};
return {nullptr, ToxCoreErrors::ERROR_ALLOC};
}

Tox_Err_New tox_err;
Expand All @@ -141,10 +136,7 @@ ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings& se

case TOX_ERR_NEW_LOAD_BAD_FORMAT:
qCritical() << "Failed to parse Tox save data";
if (err != nullptr) {
*err = ToxCoreErrors::BAD_PROXY;
}
return {};
return {nullptr, ToxCoreErrors::INVALID_SAVE};

case TOX_ERR_NEW_PORT_ALLOC:
if (toxOptions->getIPv6Enabled()) {
Expand All @@ -158,54 +150,33 @@ ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings& se
}

qCritical() << "Can't to bind the port";
if (err != nullptr) {
*err = ToxCoreErrors::FAILED_TO_START;
}
return {};
return {nullptr, ToxCoreErrors::FAILED_TO_START};

case TOX_ERR_NEW_PROXY_BAD_HOST:
case TOX_ERR_NEW_PROXY_BAD_PORT:
case TOX_ERR_NEW_PROXY_BAD_TYPE:
qCritical() << "Bad proxy, error code:" << tox_err;
if (err != nullptr) {
*err = ToxCoreErrors::BAD_PROXY;
}
return {};
return {nullptr, ToxCoreErrors::BAD_PROXY};

case TOX_ERR_NEW_PROXY_NOT_FOUND:
qCritical() << "Proxy not found";
if (err != nullptr) {
*err = ToxCoreErrors::BAD_PROXY;
}
return {};
return {nullptr, ToxCoreErrors::BAD_PROXY};

case TOX_ERR_NEW_LOAD_ENCRYPTED:
qCritical() << "Attempted to load encrypted Tox save data";
if (err != nullptr) {
*err = ToxCoreErrors::INVALID_SAVE;
}
return {};
return {nullptr, ToxCoreErrors::INVALID_SAVE};

case TOX_ERR_NEW_MALLOC:
qCritical() << "Memory allocation failed";
if (err != nullptr) {
*err = ToxCoreErrors::ERROR_ALLOC;
}
return {};
return {nullptr, ToxCoreErrors::ERROR_ALLOC};

case TOX_ERR_NEW_NULL:
qCritical() << "A parameter was null";
if (err != nullptr) {
*err = ToxCoreErrors::FAILED_TO_START;
}
return {};
return {nullptr, ToxCoreErrors::FAILED_TO_START};

default:
qCritical() << "Toxcore failed to start, unknown error code:" << tox_err;
if (err != nullptr) {
*err = ToxCoreErrors::FAILED_TO_START;
}
return {};
return {nullptr, ToxCoreErrors::FAILED_TO_START};
}

// tox should be valid by now
Expand All @@ -215,10 +186,7 @@ ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings& se
core->file = CoreFile::makeCoreFile(core.get(), core->tox.get(), core->coreLoopLock);
if (!core->file) {
qCritical() << "CoreFile failed to start";
if (err != nullptr) {
*err = ToxCoreErrors::FAILED_TO_START;
}
return {};
return {nullptr, ToxCoreErrors::FAILED_TO_START};
}

registerCallbacks(core->tox.get());
Expand All @@ -229,7 +197,7 @@ ToxCorePtr Core::makeToxCore(const QByteArray& savedata, const ICoreSettings& se

// when leaving this function 'core' should be ready for it's start() action or
// a nullptr
return core;
return {std::move(core), ToxCoreErrors::OK};
}

void Core::onStarted()
Expand Down
9 changes: 5 additions & 4 deletions src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ class Core : public QObject,
public:
enum class ToxCoreErrors
{
OK,
BAD_PROXY,
INVALID_SAVE,
FAILED_TO_START,
ERROR_ALLOC
ERROR_ALLOC,
};

static ToxCorePtr makeToxCore(const QByteArray& savedata, const ICoreSettings& settings,
IBootstrapListGenerator& bootstrapNodes,
ToxCoreErrors* err = nullptr);
static std::pair<ToxCorePtr, Core::ToxCoreErrors>
makeToxCore(const QByteArray& savedata, const ICoreSettings& settings,
IBootstrapListGenerator& bootstrapNodes);
const CoreAV* getAv() const;
CoreAV* getAv();
void setAv(CoreAV* coreAv);
Expand Down
24 changes: 12 additions & 12 deletions src/nexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void Nexus::start()
frontAction = windowMenu->addAction(QString());
connect(frontAction, &QAction::triggered, this, &Nexus::bringAllToFront);

QAction* quitAction = new QAction(globalMenuBar);
auto* quitAction = new QAction(globalMenuBar);
quitAction->setMenuRole(QAction::QuitRole);
connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);

Expand Down Expand Up @@ -285,7 +285,7 @@ void Nexus::onLoadProfile(const QString& name, const QString& pass)
* Changes the loaded profile and notifies listeners.
* @param p
*/
void Nexus::setProfile(Profile* p)
void Nexus::setProfile(std::unique_ptr<Profile> p)
{
if (p == nullptr) {
emit profileLoadFailed();
Expand All @@ -294,8 +294,8 @@ void Nexus::setProfile(Profile* p)
}
emit profileLoaded();


emit currentProfileChanged(p);
// Pushing Profile ownership to the receiver of this signal.
emit currentProfileChanged(p.release());
}

void Nexus::setParser(QCommandLineParser* parser_)
Expand All @@ -315,7 +315,7 @@ bool Nexus::handleToxSave(const QString& path)
}

#ifdef Q_OS_MAC
void Nexus::retranslateUi()
void Nexus::retranslateUi() const
{
viewMenu->menuAction()->setText(tr("View", "macOS Menu bar"));
windowMenu->menuAction()->setText(tr("Window", "macOS Menu bar"));
Expand All @@ -328,10 +328,10 @@ void Nexus::onWindowStateChanged(Qt::WindowStates state)
minimizeAction->setEnabled(QApplication::activeWindow() != nullptr);

if (QApplication::activeWindow() != nullptr && sender() == QApplication::activeWindow()) {
if (state & Qt::WindowFullScreen)
if ((state & Qt::WindowFullScreen) != 0u)
minimizeAction->setEnabled(false);

if (state & Qt::WindowFullScreen)
if ((state & Qt::WindowFullScreen) != 0u)
fullScreenAction->setText(tr("Exit Full Screen"));
else
fullScreenAction->setText(tr("Enter Full Screen"));
Expand Down Expand Up @@ -363,7 +363,7 @@ void Nexus::updateWindowsArg(QWindow* closedWindow)

QWindow* activeWindow;

if (QApplication::activeWindow())
if (QApplication::activeWindow() != nullptr)
activeWindow = QApplication::activeWindow()->windowHandle();
else
activeWindow = nullptr;
Expand All @@ -380,7 +380,7 @@ void Nexus::updateWindowsArg(QWindow* closedWindow)
dockMenu->insertAction(dockLast, action);
}

if (dockLast && !dockLast->isSeparator())
if (dockLast != nullptr && !dockLast->isSeparator())
dockMenu->insertSeparator(dockLast);
}

Expand All @@ -389,7 +389,7 @@ void Nexus::updateWindowsClosed()
updateWindowsArg(static_cast<QWidget*>(sender())->windowHandle());
}

void Nexus::updateWindowsStates()
void Nexus::updateWindowsStates() const
{
bool exists = false;
QWindowList windowList = QApplication::topLevelWindows();
Expand All @@ -406,9 +406,9 @@ void Nexus::updateWindowsStates()

void Nexus::onOpenWindow(QObject* object)
{
QWindow* window = static_cast<QWindow*>(object);
auto* window = static_cast<QWindow*>(object);

if (window->windowState() & QWindow::Minimized)
if ((window->windowState() & QWindow::Minimized) != 0)
window->showNormal();

window->raise();
Expand Down
6 changes: 3 additions & 3 deletions src/nexus.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class Nexus : public QObject
QMenu* dockMenu;

public slots:
void retranslateUi();
void retranslateUi() const;
void onWindowStateChanged(Qt::WindowStates state);
void updateWindows();
void updateWindowsClosed();
void updateWindowsStates();
void updateWindowsStates() const;
void onOpenWindow(QObject* object);
void toggleFullScreen();
void bringAllToFront();
Expand All @@ -87,7 +87,7 @@ public slots:

private:
void connectLoginScreen(const LoginScreen& loginScreen);
void setProfile(Profile* p);
void setProfile(std::unique_ptr<Profile> p);

private:
std::unique_ptr<Profile> profile;
Expand Down
Loading

0 comments on commit 681144b

Please sign in to comment.