Commit 44a3d071 authored by Anthony Léonard's avatar Anthony Léonard Committed by Guillaume Roguez

fix code for datatransfer

Many fixes to adapt LRC to the new datatransfer.

Change-Id: Ibbe322a32d44800c54e9a7ffd99c42a57200deb7
Signed-off-by: Guillaume Roguez's avatarGuillaume Roguez <guillaume.roguez@savoirfairelinux.com>
parent 4ce62577
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
// Data // Data
#include "api/conversation.h" #include "api/conversation.h"
#include "api/profile.h" #include "api/profile.h"
#include "api/datatransfer.h"
namespace lrc namespace lrc
{ {
...@@ -46,6 +47,8 @@ namespace api ...@@ -46,6 +47,8 @@ namespace api
namespace account { struct Info; } namespace account { struct Info; }
namespace interaction { struct Info; } namespace interaction { struct Info; }
class Lrc;
class BehaviorController; class BehaviorController;
class NewAccountModel; class NewAccountModel;
...@@ -60,6 +63,7 @@ public: ...@@ -60,6 +63,7 @@ public:
const account::Info& owner; const account::Info& owner;
ConversationModel(const account::Info& owner, ConversationModel(const account::Info& owner,
Lrc& lrc,
Database& db, Database& db,
const CallbacksHandler& callbacksHandler, const CallbacksHandler& callbacksHandler,
const api::BehaviorController& behaviorController); const api::BehaviorController& behaviorController);
...@@ -154,6 +158,8 @@ public: ...@@ -154,6 +158,8 @@ public:
void cancelTransfer(const std::string& convUid, uint64_t interactionId); void cancelTransfer(const std::string& convUid, uint64_t interactionId);
void getTransferInfo(uint64_t interactionId, api::datatransfer::Info& info);
Q_SIGNALS: Q_SIGNALS:
/** /**
* Emitted when a conversation receives a new interaction * Emitted when a conversation receives a new interaction
......
...@@ -91,7 +91,7 @@ to_status(const std::string& status) ...@@ -91,7 +91,7 @@ to_status(const std::string& status)
struct Info struct Info
{ {
const std::string uid; ///< long-term and unique identifier (used for historic) std::string uid; ///< long-term and unique identifier (used for historic)
Status status; Status status;
bool isOutgoing; bool isOutgoing;
std::size_t totalSize; std::size_t totalSize;
......
...@@ -52,16 +52,15 @@ class LIB_EXPORT DataTransferModel : public QObject { ...@@ -52,16 +52,15 @@ class LIB_EXPORT DataTransferModel : public QObject {
Q_OBJECT Q_OBJECT
public: public:
DataTransferModel(Database& database, DataTransferModel();
const CallbacksHandler& callbacksHandler);
~DataTransferModel(); ~DataTransferModel();
void sendFile(const std::string& account_id, const std::string& peer_uri, void sendFile(const std::string& account_id, const std::string& peer_uri,
const std::string& file_path, const std::string& display_name); const std::string& file_path, const std::string& display_name);
datatransfer::Info transferInfo(const std::string& uid); void transferInfo(long long ringId, datatransfer::Info& lrc_info);
std::streamsize bytesProgress(int interactionId); void bytesProgress(int interactionId, int64_t& total, int64_t& progress);
void accept(int interactionId, const std::string& file_path, std::size_t offset); void accept(int interactionId, const std::string& file_path, std::size_t offset);
...@@ -71,6 +70,8 @@ public: ...@@ -71,6 +70,8 @@ public:
int getInteractionIdFromDringId(long long dringId); int getInteractionIdFromDringId(long long dringId);
long long getDringIdFromInteractionId(int interactionId);
Q_SIGNALS: Q_SIGNALS:
/** /**
* Connect this signal to know when a data transfer is incoming. * Connect this signal to know when a data transfer is incoming.
......
...@@ -125,6 +125,7 @@ to_string(const Status& status) ...@@ -125,6 +125,7 @@ to_string(const Status& status)
return "TRANSFER_AWAITING"; return "TRANSFER_AWAITING";
case Status::TRANSFER_FINISHED: case Status::TRANSFER_FINISHED:
return "TRANSFER_FINISHED"; return "TRANSFER_FINISHED";
case Status::INVALID:
default: default:
return "INVALID"; return "INVALID";
} }
......
...@@ -34,6 +34,7 @@ namespace api ...@@ -34,6 +34,7 @@ namespace api
class BehaviorController; class BehaviorController;
class NewAccountModel; class NewAccountModel;
class DataTransferModel;
class LIB_EXPORT Lrc { class LIB_EXPORT Lrc {
public: public:
...@@ -49,6 +50,11 @@ public: ...@@ -49,6 +50,11 @@ public:
* @return a BehaviorController&. * @return a BehaviorController&.
*/ */
const BehaviorController& getBehaviorController() const; const BehaviorController& getBehaviorController() const;
/**
* get a reference on the DataTransfer controller.
* @return a DataTransferModel&.
*/
DataTransferModel& getDataTransferModel() const;
private: private:
std::unique_ptr<LrcPimpl> lrcPimpl_; std::unique_ptr<LrcPimpl> lrcPimpl_;
......
...@@ -39,6 +39,8 @@ class NewAccountModelPimpl; ...@@ -39,6 +39,8 @@ class NewAccountModelPimpl;
namespace api namespace api
{ {
class Lrc;
class BehaviorController; class BehaviorController;
namespace account { struct Info; } namespace account { struct Info; }
...@@ -51,7 +53,8 @@ class LIB_EXPORT NewAccountModel : public QObject { ...@@ -51,7 +53,8 @@ class LIB_EXPORT NewAccountModel : public QObject {
public: public:
using AccountInfoMap = std::map<std::string, account::Info>; using AccountInfoMap = std::map<std::string, account::Info>;
NewAccountModel(Database& database, NewAccountModel(Lrc& lrc,
Database& database,
const CallbacksHandler& callbackHandler, const CallbacksHandler& callbackHandler,
const api::BehaviorController& behaviorController); const api::BehaviorController& behaviorController);
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include "api/profile.h" #include "api/profile.h"
#include "api/datatransfer.h" #include "api/datatransfer.h"
#include <datatransfer_interface.h>
namespace lrc namespace lrc
{ {
...@@ -181,7 +183,7 @@ getHistory(Database& db, api::conversation::Info& conversation) ...@@ -181,7 +183,7 @@ getHistory(Database& db, api::conversation::Info& conversation)
{{":conversation_id", conversation.uid}}); {{":conversation_id", conversation.uid}});
if (interactionsResult.nbrOfCols == 6) { if (interactionsResult.nbrOfCols == 6) {
auto payloads = interactionsResult.payloads; auto payloads = interactionsResult.payloads;
for (auto i = 0; i < payloads.size(); i += 6) { for (decltype(payloads.size()) i = 0; i < payloads.size(); i += 6) {
auto msg = api::interaction::Info({payloads[i + 1], payloads[i + 2], auto msg = api::interaction::Info({payloads[i + 1], payloads[i + 2],
std::stoi(payloads[i + 3]), std::stoi(payloads[i + 3]),
api::interaction::to_type(payloads[i + 4]), api::interaction::to_type(payloads[i + 4]),
...@@ -214,21 +216,30 @@ int ...@@ -214,21 +216,30 @@ int
addDataTransferToConversation(Database& db, addDataTransferToConversation(Database& db,
const std::string& accountProfileId, const std::string& accountProfileId,
const std::string& conversationId, const std::string& conversationId,
const DataTransferInfo& infoFromDaemon) const api::datatransfer::Info& infoFromDaemon)
{ {
auto peerProfileId = getProfileId(db, infoFromDaemon.peer.toStdString()); auto peerProfileId = getProfileId(db, infoFromDaemon.peerUri);
auto authorId = (infoFromDaemon.isOutgoing) ? peerProfileId : peerProfileId; auto authorId = peerProfileId;
return db.insertInto("interactions", return db.insertInto("interactions", {
{{":account_id", "account_id"}, {":author_id", "author_id"}, {":account_id", "account_id"},
{":conversation_id", "conversation_id"}, {":timestamp", "timestamp"}, {":author_id", "author_id"},
{":body", "body"}, {":type", "type"}, {":conversation_id", "conversation_id"},
{":status", "status"}}, {":timestamp", "timestamp"},
{{":account_id", accountProfileId}, {":author_id", authorId}, {":body", "body"},
{":conversation_id", conversationId}, {":type", "type"},
{":timestamp", std::to_string(std::time(nullptr))}, {":status", "status"}
{":body", infoFromDaemon.displayName.toStdString()}, {":type", (infoFromDaemon.isOutgoing)?"OUTGOING_DATA_TRANSFER":"INCOMING_DATA_TRANSFER"}, }, {
{":status", "TRANSFER_CREATED"}}); {":account_id", accountProfileId},
{":author_id", authorId},
{":conversation_id", conversationId},
{":timestamp", std::to_string(std::time(nullptr))},
{":body", infoFromDaemon.displayName},
{":type", infoFromDaemon.isOutgoing ?
"OUTGOING_DATA_TRANSFER" :
"INCOMING_DATA_TRANSFER"},
{":status", "TRANSFER_CREATED"}
});
} }
int int
...@@ -296,6 +307,21 @@ void updateInteractionStatus(Database& db, unsigned int id, ...@@ -296,6 +307,21 @@ void updateInteractionStatus(Database& db, unsigned int id,
"id=:id", {{":id", std::to_string(id)}}); "id=:id", {{":id", std::to_string(id)}});
} }
std::string
conversationIdFromInteractionId(Database& db, unsigned int interactionId)
{
auto result = db.select("conversation_id",
"interactions",
"id=:interaction_id",
{{":interaction_id", std::to_string(interactionId)}});
if (result.nbrOfCols == 1) {
auto payloads = result.payloads;
return payloads[0];
}
return {};
}
void clearHistory(Database& db, void clearHistory(Database& db,
const std::string& conversationId) const std::string& conversationId)
{ {
......
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
namespace lrc namespace lrc
{ {
namespace api { namespace datatransfer {
struct Info;
}}
namespace authority namespace authority
{ {
...@@ -232,7 +236,10 @@ int countUnreadFromInteractions(Database& db, const std::string& conversationId) ...@@ -232,7 +236,10 @@ int countUnreadFromInteractions(Database& db, const std::string& conversationId)
int addDataTransferToConversation(Database& db, int addDataTransferToConversation(Database& db,
const std::string& accountProfileId, const std::string& accountProfileId,
const std::string& conversationId, const std::string& conversationId,
const DataTransferInfo& infoFromDaemon); const api::datatransfer::Info& infoFromDaemon);
std::string conversationIdFromInteractionId(Database& db, unsigned int interactionId);
} // namespace database } // namespace database
} // namespace authority } // namespace authority
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include "api/account.h" #include "api/account.h"
#include "api/lrc.h" #include "api/lrc.h"
#include "api/newaccountmodel.h" #include "api/newaccountmodel.h"
#include "api/datatransfer.h" #include "api/datatransfermodel.h"
// Lrc // Lrc
#include "account.h" #include "account.h"
...@@ -218,8 +218,9 @@ CallbacksHandler::slotRegistrationStateChanged(const QString& accountId, ...@@ -218,8 +218,9 @@ CallbacksHandler::slotRegistrationStateChanged(const QString& accountId,
unsigned detail_code, unsigned detail_code,
const QString& detail_str) const QString& detail_str)
{ {
(void) detail_code;
(void) detail_str;
emit accountStatusChanged(accountId.toStdString(), lrc::api::account::to_status(registration_state.toStdString())); emit accountStatusChanged(accountId.toStdString(), lrc::api::account::to_status(registration_state.toStdString()));
} }
void void
...@@ -286,28 +287,35 @@ CallbacksHandler::slotDataTransferEvent(qulonglong dringId, uint codeStatus) ...@@ -286,28 +287,35 @@ CallbacksHandler::slotDataTransferEvent(qulonglong dringId, uint codeStatus)
{ {
auto event = DRing::DataTransferEventCode(codeStatus); auto event = DRing::DataTransferEventCode(codeStatus);
api::datatransfer::Info info;
parent.getDataTransferModel().transferInfo(dringId, info);
// WARNING: info.status could be INVALID in case of async signaling
// So listeners must only take account of dringId in such case.
// Is useful for "termination" status like unjoinable_peer.
switch (event) { switch (event) {
case DRing::DataTransferEventCode::created: case DRing::DataTransferEventCode::created:
emit incomingTransfer(static_cast<long long>(dringId)); emit transferStatusCreated(static_cast<long long>(dringId), info);
break; break;
case DRing::DataTransferEventCode::closed_by_host: case DRing::DataTransferEventCode::closed_by_host:
case DRing::DataTransferEventCode::closed_by_peer: case DRing::DataTransferEventCode::closed_by_peer:
emit transferStatusCanceled(static_cast<long long>(dringId)); emit transferStatusCanceled(static_cast<long long>(dringId), info);
break; break;
case DRing::DataTransferEventCode::wait_peer_acceptance: case DRing::DataTransferEventCode::wait_peer_acceptance:
case DRing::DataTransferEventCode::wait_host_acceptance: case DRing::DataTransferEventCode::wait_host_acceptance:
emit transferStatusAwaiting(static_cast<long long>(dringId)); emit transferStatusAwaiting(static_cast<long long>(dringId), info);
break; break;
case DRing::DataTransferEventCode::ongoing: case DRing::DataTransferEventCode::ongoing:
emit transferStatusOngoing(static_cast<long long>(dringId)); emit transferStatusOngoing(static_cast<long long>(dringId), info);
break; break;
case DRing::DataTransferEventCode::finished: case DRing::DataTransferEventCode::finished:
emit transferStatusFinished(static_cast<long long>(dringId)); emit transferStatusFinished(static_cast<long long>(dringId), info);
break; break;
case DRing::DataTransferEventCode::invalid_pathname: case DRing::DataTransferEventCode::invalid_pathname:
case DRing::DataTransferEventCode::unjoinable_peer: case DRing::DataTransferEventCode::unjoinable_peer:
case DRing::DataTransferEventCode::unsupported: case DRing::DataTransferEventCode::unsupported:
emit transferStatusError(static_cast<long long>(dringId)); emit transferStatusError(static_cast<long long>(dringId), info);
break; break;
} }
} }
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
// Lrc // Lrc
#include "typedefs.h" #include "typedefs.h"
#include "namedirectory.h" #include "namedirectory.h"
#include "api/datatransfer.h"
namespace lrc namespace lrc
{ {
...@@ -160,13 +161,12 @@ Q_SIGNALS: ...@@ -160,13 +161,12 @@ Q_SIGNALS:
const uint64_t id, const uint64_t id,
const std::string& to, int status); const std::string& to, int status);
void incomingTransfer(long long dringId); void transferStatusCreated(long long dringId, api::datatransfer::Info info);
//~ void transferStatusChanged(const long long dringId, uint codeStatus); void transferStatusCanceled(long long dringId, api::datatransfer::Info info);
void transferStatusCanceled(long long dringId); void transferStatusAwaiting(long long dringId, api::datatransfer::Info info);
void transferStatusAwaiting(long long dringId); void transferStatusOngoing(long long dringId, api::datatransfer::Info info);
void transferStatusOngoing(long long dringId); void transferStatusFinished(long long dringId, api::datatransfer::Info info);
void transferStatusFinished(long long dringId); void transferStatusError(long long dringId, api::datatransfer::Info info);
void transferStatusError(long long dringId);
private Q_SLOTS: private Q_SLOTS:
/** /**
......
...@@ -136,11 +136,6 @@ public Q_SLOTS: ...@@ -136,11 +136,6 @@ public Q_SLOTS:
void slotNewAccountMessage(std::string& accountId, void slotNewAccountMessage(std::string& accountId,
std::string& from, std::string& from,
std::map<std::string,std::string> payloads); std::map<std::string,std::string> payloads);
/**
* Listen from callbacksHandler for new account file transfer and add pending contact if not present
* @param dringId Id of transfer from daemon. Used to retrieve transfer informations.
*/
void slotNewAccountTransfer(long long dringId);
}; };
using namespace authority; using namespace authority;
...@@ -368,9 +363,6 @@ ContactModelPimpl::ContactModelPimpl(const ContactModel& linked, ...@@ -368,9 +363,6 @@ ContactModelPimpl::ContactModelPimpl(const ContactModel& linked,
this, &ContactModelPimpl::slotIncomingCall); this, &ContactModelPimpl::slotIncomingCall);
connect(&callbacksHandler, &lrc::CallbacksHandler::newAccountMessage, connect(&callbacksHandler, &lrc::CallbacksHandler::newAccountMessage,
this, &ContactModelPimpl::slotNewAccountMessage); this, &ContactModelPimpl::slotNewAccountMessage);
connect(&callbacksHandler, &lrc::CallbacksHandler::incomingTransfer,
this, &ContactModelPimpl::slotNewAccountTransfer);
} }
ContactModelPimpl::~ContactModelPimpl() ContactModelPimpl::~ContactModelPimpl()
...@@ -596,30 +588,6 @@ ContactModelPimpl::slotNewAccountMessage(std::string& accountId, ...@@ -596,30 +588,6 @@ ContactModelPimpl::slotNewAccountMessage(std::string& accountId,
emit linked.newAccountMessage(accountId, from, payloads); emit linked.newAccountMessage(accountId, from, payloads);
} }
void
ContactModelPimpl::slotNewAccountTransfer(long long dringId)
{
DataTransferInfo infoFromDaemon = ConfigurationManager::instance().dataTransferInfo(dringId);
auto accountId = infoFromDaemon.accountId.toStdString();
auto peerId = infoFromDaemon.peer.toStdString(); // TODO valeur à checker
if (accountId != linked.owner.id) return;
auto* account = AccountModel::instance().getById(linked.owner.id.c_str());
if (not account) {
qDebug() << "ContactModel::slotNewAccountTransfer(), nullptr";
return;
}
if (contacts.find(peerId) == contacts.end()) {
// Contact not found, load profile from database.
// The conversation model will create an entry and link the incomingCall.
auto* cm = PhoneDirectoryModel::instance().getNumber(infoFromDaemon.peer, account);
addToContacts(cm, profile::Type::PENDING);
}
emit linked.newAccountTransfer(dringId, infoFromDaemon);
}
} // namespace lrc } // namespace lrc
#include "api/moc_contactmodel.cpp" #include "api/moc_contactmodel.cpp"
......
This diff is collapsed.
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
// LRC // LRC
#include "api/datatransfermodel.h" #include "api/datatransfermodel.h"
#include "callbackshandler.h"
#include "database.h"
// Dbus // Dbus
#include "dbus/configurationmanager.h" #include "dbus/configurationmanager.h"
...@@ -30,18 +28,20 @@ ...@@ -30,18 +28,20 @@
// Std // Std
#include <map> #include <map>
#include <stdexcept> #include <stdexcept>
#include <type_traits>
// Qt // Qt
#include <QUuid> #include <QUuid>
namespace lrc { namespace api { namespace lrc { namespace api {
/// DRING to LRC event code conversion // DRING to LRC event code conversion
static inline static inline
datatransfer::Status datatransfer::Status
convertDataTransferEvent(DRing::DataTransferEventCode event) convertDataTransferEvent(DRing::DataTransferEventCode event)
{ {
switch (event) { switch (event) {
case DRing::DataTransferEventCode::invalid: return datatransfer::Status::INVALID;
case DRing::DataTransferEventCode::created: return datatransfer::Status::on_connection; case DRing::DataTransferEventCode::created: return datatransfer::Status::on_connection;
case DRing::DataTransferEventCode::unsupported: return datatransfer::Status::unsupported; case DRing::DataTransferEventCode::unsupported: return datatransfer::Status::unsupported;
case DRing::DataTransferEventCode::wait_peer_acceptance: return datatransfer::Status::on_connection; case DRing::DataTransferEventCode::wait_peer_acceptance: return datatransfer::Status::on_connection;
...@@ -61,25 +61,17 @@ class DataTransferModel::Impl : public QObject ...@@ -61,25 +61,17 @@ class DataTransferModel::Impl : public QObject
Q_OBJECT Q_OBJECT
public: public:
Impl(DataTransferModel& up_link, Impl(DataTransferModel& up_link);
Database& database,
const CallbacksHandler& callbacksHandler);
std::vector<std::string> transferIdList() const; std::vector<std::string> transferIdList() const;
DataTransferModel& upLink; DataTransferModel& upLink;
std::map<long long, int> dring2lrcIdMap; std::map<long long, int> dring2lrcIdMap;
std::map<int, long long> lrc2dringIdMap; // stricly the reverse map of dring2lrcIdMap std::map<int, long long> lrc2dringIdMap; // stricly the reverse map of dring2lrcIdMap
Database& database;
const CallbacksHandler& callbacksHandler;
}; };
DataTransferModel::Impl::Impl(DataTransferModel& up_link, DataTransferModel::Impl::Impl(DataTransferModel& up_link)
Database& database,
const CallbacksHandler& callbacksHandler)
: QObject {} : QObject {}
, callbacksHandler {callbacksHandler}
, database {database}
, upLink {up_link} , upLink {up_link}
{} {}
...@@ -93,10 +85,9 @@ DataTransferModel::registerTransferId(long long dringId, int interactionId) ...@@ -93,10 +85,9 @@ DataTransferModel::registerTransferId(long long dringId, int interactionId)
} }
DataTransferModel::DataTransferModel(Database& database, DataTransferModel::DataTransferModel()
const CallbacksHandler& callbacksHandler)
: QObject() : QObject()
, pimpl_ { std::make_unique<Impl>(*this, database, callbacksHandler) } ,<