From: Michael Schuster Date: Thu, 25 Jun 2020 18:02:22 +0000 (+0200) Subject: Fix crash in UserModel::currentUser() and add more sanity checks X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~222^2^2~135 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=d71249bb6e82c92c7d0afb53b226e42ccd85a893;p=nextcloud-desktop.git Fix crash in UserModel::currentUser() and add more sanity checks Commit 07bede8 (PR #1892) introduced a new helper method currentUser() that didn't check for _users.count() thus causing to throw an "index out of range" exception when no accounts are configured. This commit uses the opportunity to add more sanity checks to UserModel. Signed-off-by: Michael Schuster --- diff --git a/src/gui/tray/UserModel.cpp b/src/gui/tray/UserModel.cpp index 2d60920b3..77c5e3991 100644 --- a/src/gui/tray/UserModel.cpp +++ b/src/gui/tray/UserModel.cpp @@ -560,17 +560,15 @@ Q_INVOKABLE int UserModel::currentUserId() const Q_INVOKABLE bool UserModel::isUserConnected(const int &id) { - if (!_users.isEmpty()) { - return _users[id]->isConnected(); - } else { + if (_users.isEmpty()) return false; - } + return _users[id]->isConnected(); } Q_INVOKABLE QImage UserModel::currentUserAvatar() { - if (_users.count() >= 1) { + if (!_users.isEmpty()) { return _users[_currentUserId]->avatar(); } else { QImage image(128, 128, QImage::Format_ARGB32); @@ -585,25 +583,26 @@ Q_INVOKABLE QImage UserModel::currentUserAvatar() QImage UserModel::avatarById(const int &id) { + if (_users.isEmpty()) + return {}; + return _users[id]->avatar(true); } Q_INVOKABLE QString UserModel::currentUserServer() { - if (_users.count() >= 1) { - return _users[_currentUserId]->server(); - } else { - return QString(""); - } + if (_users.isEmpty()) + return {}; + + return _users[_currentUserId]->server(); } Q_INVOKABLE bool UserModel::currentServerHasTalk() { - if (_users.count() >= 1) { - return _users[_currentUserId]->serverHasTalk(); - } else { + if (_users.isEmpty()) return false; - } + + return _users[_currentUserId]->serverHasTalk(); } void UserModel::addUser(AccountStatePtr &user, const bool &isCurrent) @@ -636,11 +635,17 @@ int UserModel::currentUserIndex() Q_INVOKABLE void UserModel::openCurrentAccountLocalFolder() { + if (_users.isEmpty()) + return; + _users[_currentUserId]->openLocalFolder(); } Q_INVOKABLE void UserModel::openCurrentAccountTalk() { + if (_users.isEmpty()) + return; + QString url = _users[_currentUserId]->server(false) + "/apps/spreed"; if (!(url.contains("http://") || url.contains("https://"))) { url = "https://" + _users[_currentUserId]->server(false) + "/apps/spreed"; @@ -651,7 +656,9 @@ Q_INVOKABLE void UserModel::openCurrentAccountTalk() Q_INVOKABLE void UserModel::openCurrentAccountServer() { // Don't open this URL when the QML appMenu pops up on click (see Window.qml) - if(appList().count() > 0) + if (appList().count() > 0) + return; + if (_users.isEmpty()) return; QString url = _users[_currentUserId]->server(false); @@ -663,6 +670,9 @@ Q_INVOKABLE void UserModel::openCurrentAccountServer() Q_INVOKABLE void UserModel::switchCurrentUser(const int &id) { + if (_users.isEmpty()) + return; + _users[_currentUserId]->setCurrentUser(false); _users[id]->setCurrentUser(true); _currentUserId = id; @@ -672,18 +682,27 @@ Q_INVOKABLE void UserModel::switchCurrentUser(const int &id) Q_INVOKABLE void UserModel::login(const int &id) { + if (_users.isEmpty()) + return; + _users[id]->login(); emit refreshCurrentUserGui(); } Q_INVOKABLE void UserModel::logout(const int &id) { + if (_users.isEmpty()) + return; + _users[id]->logout(); emit refreshCurrentUserGui(); } Q_INVOKABLE void UserModel::removeAccount(const int &id) { + if (_users.isEmpty()) + return; + QMessageBox messageBox(QMessageBox::Question, tr("Confirm Account Removal"), tr("

Do you really want to remove the connection to the account %1?

" @@ -755,35 +774,47 @@ QHash UserModel::roleNames() const ActivityListModel *UserModel::currentActivityModel() { + if (_users.isEmpty()) + return nullptr; + return _users[currentUserIndex()]->getActivityModel(); } bool UserModel::currentUserHasActivities() { + if (_users.isEmpty()) + return false; + return _users[currentUserIndex()]->hasActivities(); } bool UserModel::currentUserHasLocalFolder() { + if (_users.isEmpty()) + return false; + return _users[currentUserIndex()]->getFolder() != nullptr; } void UserModel::fetchCurrentActivityModel() { - _users[currentUserId()]->slotRefresh(); + if (!_users.isEmpty()) + _users[currentUserId()]->slotRefresh(); } AccountAppList UserModel::appList() const { - if (_users.count() > 0) { - return _users[_currentUserId]->appList(); - } else { + if (_users.isEmpty()) return AccountAppList(); - } + + return _users[_currentUserId]->appList(); } User *UserModel::currentUser() const { + if (_users.isEmpty()) + return nullptr; + return _users[currentUserId()]; } @@ -832,10 +863,10 @@ void UserAppsModel::buildAppList() endRemoveRows(); } - if(UserModel::instance()->appList().count() > 0) { - foreach(AccountApp *app, UserModel::instance()->appList()) { + if (UserModel::instance()->appList().count() > 0) { + foreach (AccountApp *app, UserModel::instance()->appList()) { // Filter out Talk because we have a dedicated button for it - if(app->id() == QLatin1String("spreed")) + if (app->id() == QLatin1String("spreed")) continue; beginInsertRows(QModelIndex(), rowCount(), rowCount());