Skip to content

Commit

Permalink
Remove redundant changes.
Browse files Browse the repository at this point in the history
Signed-off-by: alex-z <[email protected]>
  • Loading branch information
allexzander committed Nov 28, 2023
1 parent 7b65b03 commit efb69a4
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 197 deletions.
1 change: 0 additions & 1 deletion src/common/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
set(common_SOURCES
${CMAKE_CURRENT_LIST_DIR}/checksums.cpp
${CMAKE_CURRENT_LIST_DIR}/checksumcalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/clientstatusreportingrecord.cpp
${CMAKE_CURRENT_LIST_DIR}/filesystembase.cpp
${CMAKE_CURRENT_LIST_DIR}/ownsql.cpp
${CMAKE_CURRENT_LIST_DIR}/preparedsqlquerymanager.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ set(client_SRCS
navigationpanehelper.cpp
networksettings.h
networksettings.cpp
"${CMAKE_SOURCE_DIR}/src/libsync/ocsjob.h"
"${CMAKE_SOURCE_DIR}/src/libsync/ocsjob.cpp"
ocsnavigationappsjob.h
ocsnavigationappsjob.cpp
ocsjob.h
ocsjob.cpp
ocssharejob.h
ocssharejob.cpp
ocsshareejob.h
Expand Down
File renamed without changes.
8 changes: 3 additions & 5 deletions src/libsync/ocsjob.h → src/gui/ocsjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,11 @@ protected slots:
private slots:
bool finished() override;

protected:
QByteArray _verb;
QNetworkRequest _request;

private:
QVector<int> _passStatusCodes;
QByteArray _verb;
QHash<QString, QString> _params;
QVector<int> _passStatusCodes;
QNetworkRequest _request;
};
}

Expand Down
7 changes: 3 additions & 4 deletions src/libsync/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ set(libsync_SRCS
capabilities.cpp
clientproxy.h
clientproxy.cpp
clientstatusreporting.h
clientstatusreporting.cpp
clientstatusreportingrecord.h
clientstatusreportingrecord.cpp
cookiejar.h
cookiejar.cpp
discovery.h
Expand Down Expand Up @@ -54,10 +57,6 @@ set(libsync_SRCS
owncloudpropagator.cpp
nextcloudtheme.h
nextcloudtheme.cpp
ocsjob.h
ocsjob.cpp
ocsclientstatusreportingjob.h
ocsclientstatusreportingjob.cpp
abstractpropagateremotedeleteencrypted.h
abstractpropagateremotedeleteencrypted.cpp
deletejob.h
Expand Down
17 changes: 10 additions & 7 deletions src/libsync/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ constexpr int checksumRecalculateRequestServerVersionMinSupportedMajor = 24;
constexpr auto isSkipE2eeMetadataChecksumValidationAllowedInClientVersion = MIRALL_VERSION_MAJOR == 3 && MIRALL_VERSION_MINOR == 8;
}

namespace OCC
{
namespace OCC {
Q_LOGGING_CATEGORY(lcAccount, "nextcloud.sync.account", QtInfoMsg)
const char app_password[] = "_app-password";

Expand All @@ -88,7 +87,7 @@ AccountPtr Account::create()
return acc;
}

ClientSideEncryption *Account::e2e()
ClientSideEncryption* Account::e2e()
{
// Qt expects everything in the connect to be a pointer, so return a pointer.
return &_e2e;
Expand Down Expand Up @@ -268,10 +267,14 @@ void Account::setCredentials(AbstractCredentials *cred)
if (proxy.type() != QNetworkProxy::DefaultProxy) {
_am->setProxy(proxy);
}
connect(_am.data(), &QNetworkAccessManager::sslErrors, this, &Account::slotHandleSslErrors);
connect(_am.data(), &QNetworkAccessManager::proxyAuthenticationRequired, this, &Account::proxyAuthenticationRequired);
connect(_credentials.data(), &AbstractCredentials::fetched, this, &Account::slotCredentialsFetched);
connect(_credentials.data(), &AbstractCredentials::asked, this, &Account::slotCredentialsAsked);
connect(_am.data(), &QNetworkAccessManager::sslErrors,
this, &Account::slotHandleSslErrors);
connect(_am.data(), &QNetworkAccessManager::proxyAuthenticationRequired,
this, &Account::proxyAuthenticationRequired);
connect(_credentials.data(), &AbstractCredentials::fetched,
this, &Account::slotCredentialsFetched);
connect(_credentials.data(), &AbstractCredentials::asked,
this, &Account::slotCredentialsAsked);

trySetupPushNotifications();
}
Expand Down
125 changes: 56 additions & 69 deletions src/libsync/clientstatusreporting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
* for more details.
*/
#include "clientstatusreporting.h"
#include "creds/abstractcredentials.h"

#include "account.h"
#include "common/clientstatusreportingrecord.h"
#include "common/syncjournaldb.h"
#include "clientstatusreportingrecord.h"
#include <configfile.h>
#include "common/c_jhash.h"
#include <networkjobs.h>

namespace
Expand Down Expand Up @@ -45,18 +45,18 @@ ClientStatusReporting::~ClientStatusReporting()

void ClientStatusReporting::init()
{
Q_ASSERT(!_isInitialized);
if (_isInitialized) {
qCDebug(lcClientStatusReporting) << "Double call to init";
return;
}

for (int i = 0; i < ClientStatusReporting::Status::Count; ++i) {
const auto statusString = statusStringFromNumber(static_cast<Status>(i));
_statusNamesAndHashes[i] = {statusString, SyncJournalDb::getPHash(statusString)};
_statusNamesAndHashes[i] = {statusString, c_jhash64((uint8_t *)statusString.data(), statusString.size(), 0)};
}

const auto dbPath = makeDbPath();

_database = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"));
_database.setDatabaseName(dbPath);

Expand All @@ -66,30 +66,34 @@ void ClientStatusReporting::init()
}

QSqlQuery query;
const auto prepareResult = query.prepare(
const auto prepareResult = query.prepare(QStringLiteral(
"CREATE TABLE IF NOT EXISTS clientstatusreporting("
"nHash INTEGER(8) PRIMARY KEY,"
"name VARCHAR(4096) PRIMARY KEY,"
"status INTEGER(8),"
"name VARCHAR(4096),"
"count INTEGER,"
"lastOccurrence INTEGER(8))");
"lastOccurrence INTEGER(8))"));
if (!prepareResult || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not setup client clientstatusreporting table:" << query.lastError().text();
return;
}

if (!query.prepare("CREATE TABLE IF NOT EXISTS keyvalue(key VARCHAR(4096), value VARCHAR(4096), PRIMARY KEY(key))") || !query.exec()) {
if (!query.prepare(QStringLiteral("CREATE INDEX IF NOT EXISTS name ON clientstatusreporting(name);")) || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not create index on clientstatusreporting table:" << query.lastError().text();
return;
}

if (!query.prepare(QStringLiteral("CREATE TABLE IF NOT EXISTS keyvalue(key VARCHAR(4096), value VARCHAR(4096), PRIMARY KEY(key))")) || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not setup client keyvalue table:" << query.lastError().text();
return;
}

// prevent issues in case enum gets changed in future, hash its value and clean the db in case there was a change
QByteArray statusNamesContacenated;
QByteArray statusNamesContatenated;
for (int i = 0; i < ClientStatusReporting::Status::Count; ++i) {
statusNamesContacenated += statusStringFromNumber(static_cast<Status>(i));
statusNamesContatenated += statusStringFromNumber(static_cast<Status>(i));
}
statusNamesContacenated += QByteArray::number(ClientStatusReporting::Status::Count);
const auto statusNamesHashCurrent = QCryptographicHash::hash(statusNamesContacenated, QCryptographicHash::Md5).toHex();
statusNamesContatenated += QByteArray::number(ClientStatusReporting::Status::Count);
const auto statusNamesHashCurrent = QCryptographicHash::hash(statusNamesContatenated, QCryptographicHash::Md5).toHex();
const auto statusNamesHashFromDb = getStatusNamesHash();

if (statusNamesHashCurrent != statusNamesHashFromDb) {
Expand All @@ -112,40 +116,31 @@ QVector<ClientStatusReportingRecord> ClientStatusReporting::getClientStatusRepor
QMutexLocker locker(&_mutex);

QSqlQuery query;
const auto prepareResult = query.prepare("SELECT * FROM clientstatusreporting");

if (!prepareResult || !query.exec()) {
const auto errorMessage = query.lastError().text();
qCDebug(lcClientStatusReporting) << "Could not get records from clientstatusreporting:" << errorMessage;
if (!query.prepare(QStringLiteral("SELECT * FROM clientstatusreporting")) || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not get records from clientstatusreporting:" << query.lastError().text();
return records;
}

while (query.next()) {
ClientStatusReportingRecord record;
record._nameHash = query.value(query.record().indexOf("nHash")).toLongLong();
record._status = query.value(query.record().indexOf("status")).toLongLong();
record._name = query.value(query.record().indexOf("name")).toByteArray();
record._numOccurences = query.value(query.record().indexOf("count")).toLongLong();
record._lastOccurence = query.value(query.record().indexOf("lastOccurrence")).toLongLong();
record._status = query.value(query.record().indexOf(QStringLiteral("status"))).toLongLong();
record._name = query.value(query.record().indexOf(QStringLiteral("name"))).toByteArray();
record._numOccurences = query.value(query.record().indexOf(QStringLiteral("count"))).toLongLong();
record._lastOccurence = query.value(query.record().indexOf(QStringLiteral("lastOccurrence"))).toLongLong();
records.push_back(record);
}
return records;
}

bool ClientStatusReporting::deleteClientStatusReportingRecords()
void ClientStatusReporting::deleteClientStatusReportingRecords() const
{
QSqlQuery query;
const auto prepareResult = query.prepare("DELETE FROM clientstatusreporting");

if (!prepareResult || !query.exec()) {
const auto errorMessage = query.lastError().text();
qCDebug(lcClientStatusReporting) << "Could not get records from clientstatusreporting:" << errorMessage;
return false;
if (!query.prepare(QStringLiteral("DELETE FROM clientstatusreporting")) || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not delete records from clientstatusreporting:" << query.lastError().text();
}
return true;
}

Result<void, QString> ClientStatusReporting::setClientStatusReportingRecord(const ClientStatusReportingRecord &record)
Result<void, QString> ClientStatusReporting::setClientStatusReportingRecord(const ClientStatusReportingRecord &record) const
{
Q_ASSERT(record.isValid());
if (!record.isValid()) {
Expand All @@ -160,13 +155,12 @@ Result<void, QString> ClientStatusReporting::setClientStatusReportingRecord(cons
QSqlQuery query;

const auto prepareResult = query.prepare(
"INSERT OR REPLACE INTO clientstatusreporting (nHash, name, status, count, lastOccurrence) VALUES(:nHash, :name, :status, :count, :lastOccurrence) ON CONFLICT(nHash) "
"DO UPDATE SET count = count + 1, lastOccurrence = :lastOccurrence;");
query.bindValue(":nHash", recordCopy._nameHash);
query.bindValue(":name", recordCopy._name);
query.bindValue(":status", recordCopy._status);
query.bindValue(":count", 1);
query.bindValue(":lastOccurrence", recordCopy._lastOccurence);
QStringLiteral("INSERT OR REPLACE INTO clientstatusreporting (name, status, count, lastOccurrence) VALUES(:name, :status, :count, :lastOccurrence) ON CONFLICT(name) "
"DO UPDATE SET count = count + 1, lastOccurrence = :lastOccurrence;"));
query.bindValue(QStringLiteral(":name"), recordCopy._name);
query.bindValue(QStringLiteral(":status"), recordCopy._status);
query.bindValue(QStringLiteral(":count"), 1);
query.bindValue(QStringLiteral(":lastOccurrence"), recordCopy._lastOccurence);

if (!prepareResult || !query.exec()) {
const auto errorMessage = query.lastError().text();
Expand All @@ -177,10 +171,10 @@ Result<void, QString> ClientStatusReporting::setClientStatusReportingRecord(cons
return {};
}

void ClientStatusReporting::reportClientStatus(const Status status)
void ClientStatusReporting::reportClientStatus(const Status status) const
{
if (!_isInitialized) {
qCWarning(lcClientStatusReporting) << "Could not report status. Status reporting is not initialized";
qCDebug(lcClientStatusReporting) << "Could not report status. Status reporting is not initialized";
return;
}
Q_ASSERT(status >= 0 && status < Count);
Expand All @@ -192,7 +186,6 @@ void ClientStatusReporting::reportClientStatus(const Status status)
ClientStatusReportingRecord record;
record._name = _statusNamesAndHashes[status].first;
record._status = status;
record._nameHash = _statusNamesAndHashes[status].second;
record._lastOccurence = QDateTime::currentDateTimeUtc().toMSecsSinceEpoch();
const auto result = setClientStatusReportingRecord(record);
if (!result.isValid()) {
Expand Down Expand Up @@ -223,8 +216,8 @@ void ClientStatusReporting::sendReportToServer()
clientStatusReportingJob->setVerb(SimpleApiJob::Verb::Put);
connect(clientStatusReportingJob, &JsonApiJob::jsonReceived, [this](const QJsonDocument &json, int statusCode) {
if (statusCode == 0 || statusCode == 200 || statusCode == 201 || statusCode == 204) {
const auto metaFromJson = json.object().value("ocs").toObject().value("meta").toObject();
const auto codeFromJson = metaFromJson.value("statuscode").toInt();
const auto metaFromJson = json.object().value(QStringLiteral("ocs")).toObject().value(QStringLiteral("meta")).toObject();
const auto codeFromJson = metaFromJson.value(QStringLiteral("statuscode")).toInt();
if (codeFromJson == 0 || codeFromJson == 200 || codeFromJson == 201 || codeFromJson == 204) {
reportToServerSentSuccessfully();
return;
Expand All @@ -237,9 +230,7 @@ void ClientStatusReporting::sendReportToServer()

void ClientStatusReporting::reportToServerSentSuccessfully()
{
if (!deleteClientStatusReportingRecords()) {
qCDebug(lcClientStatusReporting) << "Error deleting client status report.";
}
deleteClientStatusReportingRecords();
setLastSentReportTimestamp(QDateTime::currentDateTimeUtc().toMSecsSinceEpoch());
}

Expand All @@ -254,12 +245,12 @@ QString ClientStatusReporting::makeDbPath() const
return ConfigFile().configPath() + QStringLiteral(".userdata_%1.db").arg(QString::fromLatin1(databaseIdHash.left(6).toHex()));
}

qulonglong ClientStatusReporting::getLastSentReportTimestamp() const
quint64 ClientStatusReporting::getLastSentReportTimestamp() const
{
QMutexLocker locker(&_mutex);
QSqlQuery query;
const auto prepareResult = query.prepare("SELECT value FROM keyvalue WHERE key = (:key)");
query.bindValue(":key", lastSentReportTimestamp);
const auto prepareResult = query.prepare(QStringLiteral("SELECT value FROM keyvalue WHERE key = (:key)"));
query.bindValue(QStringLiteral(":key"), lastSentReportTimestamp);
if (!prepareResult || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not get last sent report timestamp from keyvalue table. No such record:" << lastSentReportTimestamp;
return 0;
Expand All @@ -268,18 +259,16 @@ qulonglong ClientStatusReporting::getLastSentReportTimestamp() const
qCDebug(lcClientStatusReporting) << "Could not get last sent report timestamp from keyvalue table:" << query.lastError().text();
return 0;
}

int valueIndex = query.record().indexOf("value");
return query.value(valueIndex).toULongLong();
return query.value(query.record().indexOf(QStringLiteral("value"))).toULongLong();
}

void ClientStatusReporting::setStatusNamesHash(const QByteArray &hash)
void ClientStatusReporting::setStatusNamesHash(const QByteArray &hash) const
{
QMutexLocker locker(&_mutex);
QSqlQuery query;
const auto prepareResult = query.prepare("INSERT OR REPLACE INTO keyvalue (key, value) VALUES(:key, :value);");
query.bindValue(":key", statusNamesHash);
query.bindValue(":value", hash);
const auto prepareResult = query.prepare(QStringLiteral("INSERT OR REPLACE INTO keyvalue (key, value) VALUES(:key, :value);"));
query.bindValue(QStringLiteral(":key"), statusNamesHash);
query.bindValue(QStringLiteral(":value"), hash);
if (!prepareResult || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not set status names hash.";
return;
Expand All @@ -290,8 +279,8 @@ QByteArray ClientStatusReporting::getStatusNamesHash() const
{
QMutexLocker locker(&_mutex);
QSqlQuery query;
const auto prepareResult = query.prepare("SELECT value FROM keyvalue WHERE key = (:key)");
query.bindValue(":key", statusNamesHash);
const auto prepareResult = query.prepare(QStringLiteral("SELECT value FROM keyvalue WHERE key = (:key)"));
query.bindValue(QStringLiteral(":key"), statusNamesHash);
if (!prepareResult || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not get status names hash. No such record:" << statusNamesHash;
return 0;
Expand All @@ -300,9 +289,7 @@ QByteArray ClientStatusReporting::getStatusNamesHash() const
qCDebug(lcClientStatusReporting) << "Could not get status names hash:" << query.lastError().text();
return 0;
}

int valueIndex = query.record().indexOf("value");
return query.value(valueIndex).toByteArray();
return query.value(query.record().indexOf(QStringLiteral("value"))).toByteArray();
}

QVariantMap ClientStatusReporting::prepareReport() const
Expand Down Expand Up @@ -342,13 +329,13 @@ QVariantMap ClientStatusReporting::prepareReport() const
return report;
}

void ClientStatusReporting::setLastSentReportTimestamp(const qulonglong timestamp)
void ClientStatusReporting::setLastSentReportTimestamp(const quint64 timestamp) const
{
QMutexLocker locker(&_mutex);
QSqlQuery query;
const auto prepareResult = query.prepare("INSERT OR REPLACE INTO keyvalue (key, value) VALUES(:key, :value);");
query.bindValue(":key", lastSentReportTimestamp);
query.bindValue(":value", timestamp);
const auto prepareResult = query.prepare(QStringLiteral("INSERT OR REPLACE INTO keyvalue (key, value) VALUES(:key, :value);"));
query.bindValue(QStringLiteral(":key"), lastSentReportTimestamp);
query.bindValue(QStringLiteral(":value"), timestamp);
if (!prepareResult || !query.exec()) {
qCDebug(lcClientStatusReporting) << "Could not set last sent report timestamp from keyvalue table. No such record:" << lastSentReportTimestamp;
return;
Expand Down Expand Up @@ -394,7 +381,7 @@ QByteArray ClientStatusReporting::statusStringFromNumber(const Status status)
return {};
}

QString ClientStatusReporting::classifyStatus(const Status status)
QByteArray ClientStatusReporting::classifyStatus(const Status status)
{
Q_ASSERT(status >= 0 && status < Count);
if (status < 0 || status >= Status::Count) {
Expand All @@ -408,7 +395,7 @@ QString ClientStatusReporting::classifyStatus(const Status status)
case DownloadError_ConflictInvalidCharacters:
case UploadError_Conflict:
case UploadError_ConflictInvalidCharacters:
return QStringLiteral("sync_conflicts");
return QByteArrayLiteral("sync_conflicts");
case DownloadError_Cannot_Create_File:
case DownloadError_No_Free_Space:
case DownloadError_ServerError:
Expand Down
Loading

0 comments on commit efb69a4

Please sign in to comment.