Skip to content

Commit cd85367

Browse files
author
kun.ran
committed
fix: my device access code sync db
1 parent f82c8af commit cd85367

7 files changed

Lines changed: 70 additions & 0 deletions

File tree

QuickDesk/src/controller/MainController.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ MainController::MainController(QObject* parent)
187187
m_cloudDeviceManager->stopSync();
188188
});
189189

190+
// Sync access code changes from cloud devices to recent connections
191+
connect(m_cloudDeviceManager.get(), &CloudDeviceManager::myDevicesChanged, this, [this]() {
192+
for (const auto& v : m_cloudDeviceManager->myDevices()) {
193+
QVariantMap device = v.toMap();
194+
QString deviceId = device["device_id"].toString();
195+
QString accessCode = device["access_code"].toString();
196+
if (!deviceId.isEmpty() && !accessCode.isEmpty()) {
197+
m_remoteDeviceManager->updateDevicePassword(deviceId, accessCode);
198+
}
199+
}
200+
});
201+
190202
// WebSocket API Server
191203
m_wsApiServer = std::make_unique<WebSocketApiServer>(this, this);
192204
connect(m_wsApiServer.get(), &WebSocketApiServer::listeningChanged,

QuickDesk/src/core/db/userdatadatabase.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,26 @@ bool UserDataDataBase::updateDeviceLastConnected(const QString& deviceId)
320320
return true;
321321
}
322322

323+
bool UserDataDataBase::updateDevicePassword(const QString& deviceId, const QString& encryptedPassword)
324+
{
325+
QString sql = QString(R"(
326+
UPDATE %1 SET access_password = :access_password
327+
WHERE device_id = :device_id
328+
)").arg(kRemoteDevicesTable);
329+
330+
QSqlQuery query(QSqlDatabase::database(kDeviceDb));
331+
query.prepare(sql);
332+
query.bindValue(":access_password", encryptedPassword);
333+
query.bindValue(":device_id", deviceId);
334+
335+
if (!query.exec()) {
336+
LOG_ERROR("[database] update device password failed: {}", query.lastError().text().toStdString());
337+
return false;
338+
}
339+
340+
return query.numRowsAffected() > 0;
341+
}
342+
323343
bool UserDataDataBase::cleanOldDevices(int maxCount)
324344
{
325345
// Keep only the most recent maxCount devices (excluding favorites)

QuickDesk/src/core/db/userdatadatabase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class UserDataDataBase : public infra::DBBase {
2626
bool getRemoteDevice(const QString& deviceId, RemoteDevice& device);
2727
bool getAllRemoteDevices(QVector<RemoteDevice>& devices);
2828
bool updateDeviceLastConnected(const QString& deviceId);
29+
bool updateDevicePassword(const QString& deviceId, const QString& encryptedPassword);
2930
bool cleanOldDevices(int maxCount);
3031

3132
private:

QuickDesk/src/core/userdatacenter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ bool UserDataCenter::updateDeviceLastConnected(const QString& deviceId)
6161
return m_userDataDB->updateDeviceLastConnected(deviceId);
6262
}
6363

64+
bool UserDataCenter::updateDevicePassword(const QString& deviceId, const QString& encryptedPassword)
65+
{
66+
return m_userDataDB->updateDevicePassword(deviceId, encryptedPassword);
67+
}
68+
6469
bool UserDataCenter::cleanOldDevices(int maxCount)
6570
{
6671
return m_userDataDB->cleanOldDevices(maxCount);

QuickDesk/src/core/userdatacenter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class UserDataCenter : public QObject, public base::Singleton<UserDataCenter> {
2828
bool getRemoteDevice(const QString& deviceId, RemoteDevice& device);
2929
bool getAllRemoteDevices(QVector<RemoteDevice>& devices);
3030
bool updateDeviceLastConnected(const QString& deviceId);
31+
bool updateDevicePassword(const QString& deviceId, const QString& encryptedPassword);
3132
bool cleanOldDevices(int maxCount);
3233

3334
private:

QuickDesk/src/manager/RemoteDeviceManager.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,34 @@ void RemoteDeviceManager::updateDeviceConnected(const QString& deviceId)
126126
}
127127
}
128128

129+
bool RemoteDeviceManager::updateDevicePassword(const QString& deviceId, const QString& newPassword)
130+
{
131+
if (deviceId.isEmpty() || newPassword.isEmpty()) {
132+
return false;
133+
}
134+
135+
// Check if device exists in recent connections
136+
core::RemoteDevice device;
137+
if (!m_dataCenter.getRemoteDevice(deviceId, device)) {
138+
return false; // Device not in recent connections, nothing to update
139+
}
140+
141+
QString encrypted = encryptPassword(newPassword);
142+
if (!m_dataCenter.updateDevicePassword(deviceId, encrypted)) {
143+
LOG_ERROR("[RemoteDeviceManager] Failed to update password for device: {}", deviceId.toStdString());
144+
return false;
145+
}
146+
147+
// Reload device list
148+
m_devices.clear();
149+
if (m_dataCenter.getAllRemoteDevices(m_devices)) {
150+
emit deviceListChanged();
151+
}
152+
153+
LOG_INFO("[RemoteDeviceManager] Updated password for device: {}", deviceId.toStdString());
154+
return true;
155+
}
156+
129157
QString RemoteDeviceManager::encryptPassword(const QString& password) const
130158
{
131159
// Simple XOR encryption with Base64 encoding

QuickDesk/src/manager/RemoteDeviceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class RemoteDeviceManager : public QObject
4848
// Update device last connected time
4949
Q_INVOKABLE void updateDeviceConnected(const QString& deviceId);
5050

51+
// Update device password (for access code sync from cloud)
52+
Q_INVOKABLE bool updateDevicePassword(const QString& deviceId, const QString& newPassword);
53+
5154
signals:
5255
void deviceListChanged();
5356
void deviceAdded(const QString& deviceId);

0 commit comments

Comments
 (0)