Skip to content

Commit

Permalink
Merge branch 'feature/CHT-400_remove-megachatroom-deprecated-method' …
Browse files Browse the repository at this point in the history
…into 'develop'

CHT-400. Remove MegaChatRoom deprecated method

Closes CHT-400

See merge request megachat/MEGAchat!1348
  • Loading branch information
alfredo-mega committed Feb 5, 2024
2 parents 02ccac9 + 8d1af23 commit 4639378
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 410 deletions.
106 changes: 88 additions & 18 deletions examples/megaclc/megaclc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,39 @@ void exec_getunreadchatlistitems(ac::ACState&)
}
}

void printChatInfoFromCache(const c::MegaChatRoom* room)
{
conlock(cout) << "Chat ID: " << ch_s(room->getChatId()) << endl;
conlock(cout) << "\tTitle: " << room->getTitle() << endl;
conlock(cout) << "\tGroup chat: " << ((room->isGroup()) ? "yes" : "no") << endl;
conlock(cout) << "\tPublic chat: " << ((room->isPublic()) ? "yes" : "no") << endl;
conlock(cout) << "\tPreview mode: " << ((room->isPreview()) ? "yes" : "no") << endl;
conlock(cout) << "\tOwn privilege: "
<< c::MegaChatRoom::privToString(room->getOwnPrivilege()) << endl;
conlock(cout) << "\tCreation ts: " << room->getCreationTs() << endl;
conlock(cout) << "\tArchived: " << ((room->isArchived()) ? "yes" : "no") << endl;
conlock(cout) << "\t" << room->getPeerCount() << " participants in chat:" << endl;
for (unsigned i = 0; i < room->getPeerCount(); i++)
{
c::MegaChatHandle uh = room->getPeerHandle(i);
conlock(cout) << "\t\t" << ch_s(uh) << "\t"
<< g_chatApi->getUserFullnameFromCache(uh);
auto userEmailFromCache =
std::unique_ptr<const char[]>(g_chatApi->getUserEmailFromCache(uh));
if (userEmailFromCache)
{
conlock(cout) << " (" << userEmailFromCache.get() << ")";
}
conlock(cout) << "\tPriv: "
<< c::MegaChatRoom::privToString(room->getPeerPrivilege(i))
<< endl;
}
}

unsigned int g_remainingPrints;
std::mutex g_mutexPrintChatInfo;
std::condition_variable g_cvChatInfoPrinted;

void printChatInfo(const c::MegaChatRoom *room)
{
if (!room)
Expand All @@ -2021,43 +2054,80 @@ void printChatInfo(const c::MegaChatRoom *room)
}
else
{
conlock(cout) << "Chat ID: " << ch_s(room->getChatId()) << endl;
conlock(cout) << "\tTitle: " << room->getTitle() << endl;
conlock(cout) << "\tGroup chat: " << ((room->isGroup()) ? "yes" : "no") << endl;
conlock(cout) << "\tPublic chat: " << ((room->isPublic()) ? "yes" : "no") << endl;
conlock(cout) << "\tPreview mode: " << ((room->isPreview()) ? "yes" : "no") << endl;
conlock(cout) << "\tOwn privilege: " << c::MegaChatRoom::privToString(room->getOwnPrivilege()) << endl;
conlock(cout) << "\tCreation ts: " << room->getCreationTs() << endl;
conlock(cout) << "\tArchived: " << ((room->isArchived()) ? "yes" : "no") << endl;
conlock(cout) << "\t" << room->getPeerCount() << " participants in chat:" << endl;
for (unsigned i = 0; i < room->getPeerCount(); i++)
{
conlock(cout) << "\t\t" << ch_s(room->getPeerHandle(i)) << "\t" << room->getPeerFullname(i);
if (room->getPeerEmail(i))
// for the sake of keeping the order there are two iterations over the peers in the chat
auto missingPeersList =
std::unique_ptr<m::MegaHandleList>(m::MegaHandleList::createInstance());
unsigned int totalPeers = room->getPeerCount();
for (unsigned int peerIdx = 0; peerIdx < totalPeers; ++peerIdx)
{
auto uh = room->getPeerHandle(peerIdx);
auto userCached =
std::unique_ptr<const char[]>(g_chatApi->getUserFirstnameFromCache(uh));
if (!userCached)
{
conlock(cout) << " (" << room->getPeerEmail(i) << ")";
missingPeersList->addMegaHandle(uh);
}
conlock(cout) << "\tPriv: " << c::MegaChatRoom::privToString(room->getPeerPrivilege(i)) << endl;
}

if (missingPeersList->size())
{
// lk it's already locked in exec_chatinfo
++g_remainingPrints;

auto allUserDataReceivedListener = new OneShotChatRequestListener(
[room](c::MegaChatApi*, c::MegaChatRequest*, c::MegaChatError* e)
{
std::unique_lock<std::mutex> lk(g_mutexPrintChatInfo);
if (check_err("checkLoadUAForChatInfo", e))
{
printChatInfoFromCache(room);
}
--g_remainingPrints;
lk.unlock();
g_cvChatInfoPrinted.notify_one();
});
g_chatApi->loadUserAttributes(room->getChatId(), missingPeersList.get(),
allUserDataReceivedListener);
}
else
{
printChatInfoFromCache(room);
}
}
}

void exec_chatinfo(ac::ACState& s)
{
std::unique_ptr<c::MegaChatRoomList> chats;
std::unique_ptr<c::MegaChatRoom> room;

std::unique_lock<std::mutex> lk(g_mutexPrintChatInfo);
g_remainingPrints = 0;
if (s.words.size() == 1) // print all chats
{
std::unique_ptr<c::MegaChatRoomList> chats = std::unique_ptr<c::MegaChatRoomList>(g_chatApi->getChatRooms());
chats.reset(g_chatApi->getChatRooms());
for (unsigned int i = 0; i < chats->size(); i++)
{
printChatInfo(chats->get(i));
}
}
if (s.words.size() == 2)
else if (s.words.size() == 2)
{
c::MegaChatHandle chatid = s_ch(s.words[1].s);
std::unique_ptr<c::MegaChatRoom> room = std::unique_ptr<c::MegaChatRoom>(g_chatApi->getChatRoom(chatid));
room.reset(g_chatApi->getChatRoom(chatid));
printChatInfo(room.get());
}
else // just in case the parameter precon check changes at some point
{
conlock(cout) << "Incorrect number of parameters. Check help." << endl;
return;
}

if (g_remainingPrints && !g_cvChatInfoPrinted.wait_for(lk, std::chrono::milliseconds(500),
[]{ return !g_remainingPrints; }))
{
conlock(cout) << "Timeout on request to get chat information" << endl;
}
}

void exec_getchathandlebyuser(ac::ACState& s)
Expand Down
1 change: 1 addition & 0 deletions examples/qtmegachatapi/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class MainWindow :
friend class CallAnswerGui;
friend class ChatWindow;
friend class ChatMessage;
friend class MeetingView;
};

#endif // MAINWINDOW_H
Expand Down
27 changes: 14 additions & 13 deletions examples/qtmegachatapi/meetingSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,20 @@ void MeetingSession::updateWidget(const megachat::MegaChatSession &session)
}

// title lbl
std::string title = mMeetingView->sessionToString(session);
if (session.isAudioDetected())
{
title.append(" Speaking");
}
else
{
title.append( "No Speaking");
}

mTitleLabel.reset(new QLabel(title.c_str()));
layout()->addWidget(mTitleLabel.get());
setToolTip(title.c_str());
std::function<void()> setTitle;
auto sPeerId = session.getPeerid();
auto sClientId = session.getClientid();
std::string sTitlePreffix = session.isAudioDetected() ? " Speaking" : "No Speaking";
setTitle = std::function<void()>(
[this, sPeerId, sClientId, sTitlePreffix, setTitle]()
{
std::string title = sTitlePreffix
+ mMeetingView->sessionToString(sPeerId, sClientId, setTitle);
mTitleLabel.reset(new QLabel(title.c_str()));
layout()->addWidget(mTitleLabel.get());
setToolTip(title.c_str());
});
setTitle();

// audio lbl
mAudio = session.hasAudio();
Expand Down
45 changes: 38 additions & 7 deletions examples/qtmegachatapi/meetingView.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef KARERE_DISABLE_WEBRTC
#include "meetingView.h"
#include "MainWindow.h"
#include <QMenu>
#include <QApplication>
#include <QInputDialog>
Expand Down Expand Up @@ -146,6 +147,8 @@ MeetingView::MeetingView(megachat::MegaChatApi &megaChatApi, mega::MegaHandle ch
std::unique_ptr<megachat::MegaChatRoom> chatroom = std::unique_ptr<megachat::MegaChatRoom>(mMegaChatApi.getChatRoom(chatid));
assert(chatroom);
setWindowTitle(chatroom->getTitle());

mLogger = ((MainWindow *)parent)->mLogger;
}

MeetingView::~MeetingView()
Expand Down Expand Up @@ -523,32 +526,60 @@ void MeetingView::setOnHold(bool isOnHold, megachat::MegaChatHandle cid)
}
}

std::string MeetingView::sessionToString(const megachat::MegaChatSession &session)
void MeetingView::onRequestFinish(megachat::MegaChatApi*, megachat::MegaChatRequest*,
megachat::MegaChatError* e)
{
if (e->getErrorCode() == megachat::MegaChatError::ERROR_OK)
{
mUserDataReceivedFunc();
}
else
{
assert(mLogger);
mLogger->postLog("Couldn't retrieve user data for user pariticipating in the session.");
}
}

std::string MeetingView::sessionToString(megachat::MegaChatHandle sessionPeerId,
megachat::MegaChatHandle sessionClientId,
std::function<void()> userDataReceived)
{
std::string returnedString;
std::unique_ptr<megachat::MegaChatRoom> chatRoom(mMegaChatApi.getChatRoom(mChatid));
for (size_t i = 0; i < chatRoom->getPeerCount(); i++)
{
if (chatRoom->getPeerHandle(static_cast<unsigned int>(i)) == session.getPeerid())
megachat::MegaChatHandle userHandle = chatRoom->getPeerHandle(static_cast<unsigned int>(i));
if (userHandle == sessionPeerId)
{
const char *firstName = chatRoom->getPeerFirstname(static_cast<unsigned int>(i));
auto firstName =
std::unique_ptr<const char[]>(mMegaChatApi.getUserFirstnameFromCache(userHandle));
if (firstName)
{
returnedString.append(firstName);
returnedString.append(firstName.get());
}
else
{
returnedString.append("Retrieving data...");
mUserDataReceivedFunc = userDataReceived;
auto peersList = std::unique_ptr<::mega::MegaHandleList>(
::mega::MegaHandleList::createInstance());
peersList->addMegaHandle(userHandle);
mMegaChatApi.loadUserAttributes(mChatid, peersList.get(), this);
}

const char *email = chatRoom->getPeerEmail(static_cast<unsigned int>(i));
auto email =
std::unique_ptr<const char[]>(mMegaChatApi.getUserEmailFromCache(userHandle));
if (email)
{
returnedString.append(" (");
returnedString.append(email);
returnedString.append(email.get());
returnedString.append(" )");
}
}
}

returnedString.append(" [ClientId: ");
returnedString.append(std::to_string(session.getClientid())).append("]");
returnedString.append(std::to_string(sessionClientId)).append("]");
return returnedString;
}

Expand Down
13 changes: 11 additions & 2 deletions examples/qtmegachatapi/meetingView.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define MEETINGVIEW_H

#include "peerWidget.h"
#include "megaLoggerApplication.h"
#include <megachatapi.h>

#include <QDialog>
Expand All @@ -16,7 +17,7 @@
#include "meetingSession.h"

class MeetingSession;
class MeetingView : public QDialog
class MeetingView : public QDialog, public megachat::MegaChatRequestListener
{
Q_OBJECT
public:
Expand All @@ -32,7 +33,8 @@ class MeetingView : public QDialog
void updateAudioButtonText(const megachat::MegaChatCall &call);
void updateVideoButtonText(const megachat::MegaChatCall &call);
void setOnHold(bool mIsOnHold, megachat::MegaChatHandle cid);
std::string sessionToString(const megachat::MegaChatSession& session);
std::string sessionToString(megachat::MegaChatHandle, megachat::MegaChatHandle,
std::function<void()>);
void updateAudioMonitor(bool enabled);
void updateLabel(megachat::MegaChatCall *call);
void setNotParticipating();
Expand All @@ -49,6 +51,10 @@ class MeetingView : public QDialog
void createRingingWindow(megachat::MegaChatHandle callid);
void destroyRingingWindow();

// megachat::MegaChatRequestListener
virtual void onRequestFinish(megachat::MegaChatApi* api, megachat::MegaChatRequest *request,
megachat::MegaChatError* e) override;

protected:
megachat::MegaChatApi &mMegaChatApi;
mega::MegaHandle mChatid;
Expand Down Expand Up @@ -91,6 +97,9 @@ class MeetingView : public QDialog

std::unique_ptr<QMessageBox> mRingingWindow;

std::function<void()> mUserDataReceivedFunc;
MegaLoggerApplication* mLogger;

public slots:
void onHangUp();
void onEndCall();
Expand Down
2 changes: 1 addition & 1 deletion examples/qtmegachatapi/peerWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void PeerWidget::drawPeerAvatar(QImage &image)
if (chatroom->getPeerHandle(i) != mMegaChatApi.getMyUserHandle())
{
peerHandle = chatroom->getPeerHandle(i);
title = chatroom->getPeerFullname(i);
title = mMegaChatApi.getUserFullnameFromCache(peerHandle);
break;
}
}
Expand Down
40 changes: 0 additions & 40 deletions src/megachatapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1698,26 +1698,6 @@ int MegaChatRoom::getPeerPrivilegeByHandle(MegaChatHandle /*userhandle*/) const
return PRIV_UNKNOWN;
}

const char *MegaChatRoom::getPeerFirstnameByHandle(MegaChatHandle /*userhandle*/) const
{
return NULL;
}

const char *MegaChatRoom::getPeerLastnameByHandle(MegaChatHandle /*userhandle*/) const
{
return NULL;
}

const char *MegaChatRoom::getPeerFullnameByHandle(MegaChatHandle /*userhandle*/) const
{
return NULL;
}

const char *MegaChatRoom::getPeerEmailByHandle(MegaChatHandle /*userhandle*/) const
{
return NULL;
}

unsigned int MegaChatRoom::getPeerCount() const
{
return 0;
Expand All @@ -1733,26 +1713,6 @@ int MegaChatRoom::getPeerPrivilege(unsigned int /*i*/) const
return PRIV_UNKNOWN;
}

const char *MegaChatRoom::getPeerFirstname(unsigned int /*i*/) const
{
return NULL;
}

const char *MegaChatRoom::getPeerLastname(unsigned int /*i*/) const
{
return NULL;
}

const char *MegaChatRoom::getPeerFullname(unsigned int /*i*/) const
{
return NULL;
}

const char *MegaChatRoom::getPeerEmail(unsigned int /*i*/) const
{
return NULL;
}

bool MegaChatRoom::isGroup() const
{
return false;
Expand Down
Loading

0 comments on commit 4639378

Please sign in to comment.