Skip to content

Commit

Permalink
Use "return object" instead of return parameters
Browse files Browse the repository at this point in the history
In comparison to alternative approaches like tuples, this allows naming the return parameters, which makes them easier to use for callers.
  • Loading branch information
Fabian Müller committed Apr 15, 2021
1 parent 6e35c8f commit fcca25e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
37 changes: 18 additions & 19 deletions src/gui/folderman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,12 +1230,9 @@ void FolderMan::setDirtyNetworkLimits()
}
}

void FolderMan::trayOverallStatus(const QList<Folder *> &folders, SyncResult::Status &status, bool &unresolvedConflicts, QDateTime &lastSyncDone)
TrayOverallStatusResult FolderMan::trayOverallStatus(const QList<Folder *> &folders)
{
status = SyncResult::Undefined;
unresolvedConflicts = false;
// initialize with a "null" value (i.e., isNull() returns true)
lastSyncDone = QDateTime();
TrayOverallStatusResult result;

int cnt = folders.count();

Expand All @@ -1250,31 +1247,31 @@ void FolderMan::trayOverallStatus(const QList<Folder *> &folders, SyncResult::St
if (folder) {
auto syncResult = folder->syncResult();
if (folder->syncPaused()) {
status = SyncResult::Paused;
result.overallStatus = SyncResult::Paused;
} else {
SyncResult::Status syncStatus = syncResult.status();
switch (syncStatus) {
case SyncResult::Undefined:
status = SyncResult::Error;
result.overallStatus = SyncResult::Error;
break;
case SyncResult::Problem: // don't show the problem icon in tray.
status = SyncResult::Success;
result.overallStatus = SyncResult::Success;
break;
default:
status = syncStatus;
result.overallStatus = syncStatus;
break;
}
}

unresolvedConflicts = syncResult.hasUnresolvedConflicts();
result.hasUnresolvedConflicts = syncResult.hasUnresolvedConflicts();

auto currentFolderLastSyncDone = QDateTime::currentDateTime().addMSecs(-1 * folder->msecSinceLastSync().count());

if (lastSyncDone.isNull()) {
lastSyncDone = currentFolderLastSyncDone;
if (result.lastSyncDone.isNull()) {
result.lastSyncDone = currentFolderLastSyncDone;
} else {
if (currentFolderLastSyncDone > lastSyncDone) {
lastSyncDone = currentFolderLastSyncDone;
if (currentFolderLastSyncDone > result.lastSyncDone) {
result.lastSyncDone = currentFolderLastSyncDone;
}
}
}
Expand Down Expand Up @@ -1316,19 +1313,21 @@ void FolderMan::trayOverallStatus(const QList<Folder *> &folders, SyncResult::St
}
}
if (folderResult.hasUnresolvedConflicts())
unresolvedConflicts = true;
result.hasUnresolvedConflicts = true;
}
if (errorsSeen > 0) {
status = SyncResult::Error;
result.overallStatus = SyncResult::Error;
} else if (abortOrPausedSeen > 0 && abortOrPausedSeen == cnt) {
// only if all folders are paused
status = SyncResult::Paused;
result.overallStatus = SyncResult::Paused;
} else if (runSeen > 0) {
status = SyncResult::SyncRunning;
result.overallStatus = SyncResult::SyncRunning;
} else if (goodSeen > 0) {
status = SyncResult::Success;
result.overallStatus = SyncResult::Success;
}
}

return result;
}

QString FolderMan::trayTooltipStatusString(
Expand Down
19 changes: 17 additions & 2 deletions src/gui/folderman.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ class SyncResult;
class SocketApi;
class LockWatcher;

/**
* @brief Return object for Folder::trayOverallStatus.
* @ingroup gui
*/
class TrayOverallStatusResult
{
public:
SyncResult::Status overallStatus;
bool hasUnresolvedConflicts;
QDateTime lastSyncDone;
};

/**
* @brief The FolderMan class
* @ingroup gui
Expand Down Expand Up @@ -134,8 +146,11 @@ class FolderMan : public QObject
/// Produce text for use in the tray tooltip
static QString trayTooltipStatusString(SyncResult::Status syncStatus, bool hasUnresolvedConflicts, bool paused);

/// Compute status summarizing multiple folders
static void trayOverallStatus(const QList<Folder *> &folders, SyncResult::Status &status, bool &unresolvedConflicts, QDateTime&lastSyncDone);
/**
* Compute status summarizing multiple folders
* @return tuple containing folders, status, unresolvedConflicts and lastSyncDone
*/
static TrayOverallStatusResult trayOverallStatus(const QList<Folder *> &folders);

// Escaping of the alias which is used in QSettings AND the file
// system, thus need to be escaped.
Expand Down
23 changes: 10 additions & 13 deletions src/gui/owncloudgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,12 @@ void ownCloudGui::slotComputeOverallSyncStatus()
FolderMan *folderMan = FolderMan::instance();
Folder::Map map = folderMan->map();

SyncResult::Status overallStatus = SyncResult::Undefined;
bool hasUnresolvedConflicts = false;
QDateTime lastSyncDone;
FolderMan::trayOverallStatus(map.values(), overallStatus, hasUnresolvedConflicts, lastSyncDone);
auto trayOverallStatusResult = FolderMan::trayOverallStatus(map.values());

// If the sync succeeded but there are unresolved conflicts,
// show the problem icon!
auto iconStatus = overallStatus;
if (iconStatus == SyncResult::Success && hasUnresolvedConflicts) {
auto iconStatus = trayOverallStatusResult.overallStatus;
if (iconStatus == SyncResult::Success && trayOverallStatusResult.hasUnresolvedConflicts) {
iconStatus = SyncResult::Problem;
}

Expand All @@ -287,7 +284,7 @@ void ownCloudGui::slotComputeOverallSyncStatus()
if (map.count() > 0) {
#ifdef Q_OS_WIN
// Windows has a 128-char tray tooltip length limit.
trayMessage = folderMan->trayTooltipStatusString(overallStatus, hasUnresolvedConflicts, false);
trayMessage = folderMan->trayTooltipStatusString(trayOverallStatusResult.overallStatus, trayOverallStatusResult.hasUnresolvedConflicts, false);
#else
QStringList allStatusStrings;
foreach (Folder *folder, map.values()) {
Expand All @@ -301,22 +298,22 @@ void ownCloudGui::slotComputeOverallSyncStatus()
#endif
_tray->setToolTip(trayMessage);

if (overallStatus == SyncResult::Success || overallStatus == SyncResult::Problem) {
if (hasUnresolvedConflicts) {
if (trayOverallStatusResult.overallStatus == SyncResult::Success || trayOverallStatusResult.overallStatus == SyncResult::Problem) {
if (trayOverallStatusResult.hasUnresolvedConflicts) {
setStatusText(tr("Unresolved conflicts"));
} else {
QString lastSyncDoneString;

// display only the time in case the last sync was today
if (QDateTime::currentDateTime().date() == lastSyncDone.date()) {
lastSyncDoneString = QLocale().toString(lastSyncDone.time());
if (QDateTime::currentDateTime().date() == trayOverallStatusResult.lastSyncDone.date()) {
lastSyncDoneString = QLocale().toString(trayOverallStatusResult.lastSyncDone.time());
} else {
lastSyncDoneString = QLocale().toString(lastSyncDone);
lastSyncDoneString = QLocale().toString(trayOverallStatusResult.lastSyncDone);
}

setStatusText(tr("Up to date (%1)").arg(lastSyncDoneString));
}
} else if (overallStatus == SyncResult::Paused) {
} else if (trayOverallStatusResult.overallStatus == SyncResult::Paused) {
setStatusText(tr("Synchronization is paused"));
} else {
setStatusText(tr("Error during synchronization"));
Expand Down

0 comments on commit fcca25e

Please sign in to comment.