Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions RetroChessPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "gui/RetroChessChatWidgetHolder.h"
#include <retroshare-gui/RsAutoUpdatePage.h>

#include <retroshare/rsgxstunnel.h>
#include <retroshare/rsplugin.h>
#include <retroshare/rsversion.h>

Expand Down Expand Up @@ -90,6 +91,7 @@ RetroChessPlugin::RetroChessPlugin()
void RetroChessPlugin::setInterfaces(RsPlugInInterfaces &interfaces)
{
mPeers = interfaces.mPeers;
mGxsTunnels = interfaces.mGxsTunnels;
}

/*ConfigPage *RetroChessPlugin::qt_config_page() const
Expand Down Expand Up @@ -128,6 +130,7 @@ ChatWidgetHolder *RetroChessPlugin::qt_get_chat_widget_holder(ChatWidget *chatWi
case ChatWidget::CHATTYPE_UNKNOWN:
case ChatWidget::CHATTYPE_LOBBY:
case ChatWidget::CHATTYPE_DISTANT:
return new RetroChessChatWidgetHolder(chatWidget, mRetroChessNotify);
break;
}

Expand All @@ -136,15 +139,23 @@ ChatWidgetHolder *RetroChessPlugin::qt_get_chat_widget_holder(ChatWidget *chatWi

p3Service *RetroChessPlugin::p3_service() const
{
if(mRetroChess == NULL)
rsRetroChess = mRetroChess = new p3RetroChess(mPlugInHandler,mRetroChessNotify) ; // , 3600 * 24 * 30 * 6); // 6 Months

return mRetroChess ;
if(mRetroChess == NULL)
{
// Create the service
rsRetroChess = mRetroChess = new p3RetroChess(mPlugInHandler, mRetroChessNotify);

// Register it for GXS Tunnels immediately if the interface is available
if (mGxsTunnels) {
mGxsTunnels->registerClientService(RETRO_CHESS_GXS_TUNNEL_SERVICE_ID, mRetroChess);
}
}
return mRetroChess;
}

void RetroChessPlugin::setPlugInHandler(RsPluginHandler *pgHandler)
{
mPlugInHandler = pgHandler;
mPlugInHandler = pgHandler;
// No need to register here if done in p3_service
}

QIcon *RetroChessPlugin::qt_icon() const
Expand Down
3 changes: 3 additions & 0 deletions RetroChessPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

/*libretroshare"*/
#include <retroshare/rsplugin.h>
#include <retroshare/rsgxstunnel.h>

#include "gui/NEMainpage.h"

Expand Down Expand Up @@ -76,6 +77,8 @@ class RetroChessPlugin: public RsPlugin
mutable QIcon *mIcon;
mutable MainPage* mainpage ;

RsGxsTunnelService *mGxsTunnels;
RsGxsTunnelService::RsGxsTunnelClientService *mGxsTunnelClient;
RetroChessNotify *mRetroChessNotify ;
RetroChessGUIHandler *mRetroChessGUIHandler ;
};
Expand Down
8 changes: 8 additions & 0 deletions gui/NEMainpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ NEMainpage::NEMainpage(QWidget *parent, RetroChessNotify *notify) :

connect(mNotify, SIGNAL(NeMsgArrived(RsPeerId,QString)), this, SLOT(NeMsgArrived(RsPeerId,QString)));
connect(mNotify, SIGNAL(chessStart(RsPeerId)), this, SLOT(chessStart(RsPeerId)));
connect(mNotify, SIGNAL(chessStartGxs(RsGxsId)), this, SLOT(showChessWindowGxs(RsGxsId)));
connect(ui->friendSelectionWidget, SIGNAL(itemSelectionChanged()), this, SLOT(friendSelectionChanged()));

// enable/disable the invite button
Expand Down Expand Up @@ -178,6 +179,13 @@ void NEMainpage::create_chess_window(std::string peer_id, int player_id)
ui->active_games->addItem(QString::fromStdString(peer_id));
}

void NEMainpage::showChessWindowGxs(const RsGxsId &gxs_id)
{
// Open the window with the GXS constructor
RetroChessWindow *win = new RetroChessWindow(gxs_id, 0);
win->show();
}

// enable the invite button when selected a friend
void NEMainpage::enable_inviteButton()
{
Expand Down
1 change: 1 addition & 0 deletions gui/NEMainpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private slots:
void friendSelectionChanged();
void NeMsgArrived(const RsPeerId &peer_id, QString str);
void chessStart(const RsPeerId &peer_id);
void showChessWindowGxs(const RsGxsId &gxs_id);

void on_broadcastButton_clicked();

Expand Down
127 changes: 111 additions & 16 deletions gui/RetroChessChatWidgetHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "RetroChessChatWidgetHolder.h"

#include <retroshare/rsidentity.h>
#include <retroshare/rsstatus.h>
#include <retroshare/rspeers.h>

Expand All @@ -41,7 +42,6 @@ RetroChessChatWidgetHolder::RetroChessChatWidgetHolder(ChatWidget *chatWidget, R
QIcon icon ;
icon.addPixmap(QPixmap(IMAGE_RetroChess)) ;


playChessButton = new QToolButton ;
playChessButton->setIcon(icon) ;
playChessButton->setToolTip(tr("Invite Friend to Chess"));
Expand All @@ -51,6 +51,10 @@ RetroChessChatWidgetHolder::RetroChessChatWidgetHolder(ChatWidget *chatWidget, R
mChatWidget->addChatBarWidget(playChessButton);
connect(playChessButton, SIGNAL(clicked()), this, SLOT(chessPressed()));
connect(notify, SIGNAL(chessInvited(RsPeerId)), this, SLOT(chessnotify(RsPeerId)));
connect(notify, SIGNAL(chessInvitedGxs(RsGxsId)), this, SLOT(chessnotifyGxs(RsGxsId)));
// When the p3RetroChess service detects the tunnel is CONNECTED,
// it calls notifyGxsTunnelReady, which emits this signal:
connect(notify, SIGNAL(gxsTunnelReady(RsGxsId)), this, SLOT(handleGxsTunnelReady(RsGxsId)));

}

Expand Down Expand Up @@ -109,34 +113,125 @@ void RetroChessChatWidgetHolder::chessnotify(RsPeerId from_peer_id)
}
}

void RetroChessChatWidgetHolder::chessnotifyGxs(const RsGxsId &from_gxs_id)
{
ChatId chatId = mChatWidget->getChatId();

// Check if the current chat window matches the GXS ID of the person who invited us
if (!chatId.isDistantChatId()) {
return;
}

// You need a way to check if an invite exists.
// If you haven't implemented hasInviteFromGxs, you can use the active tunnel status.
if (true) // In GXS, the receipt of the signal itself implies an invite
{
if (mChatWidget)
{
// Get the name from the GXS Identity system
std::string identityName;
RsGxsId nameId = from_gxs_id;
// Attempt to get a readable name for the GXS ID
// If rsIdentity doesn't have a direct getName, we use a placeholder or the ID string
QString buttonName = QString::fromStdString(from_gxs_id.toStdString()).left(8);

// Clear old buttons for this chat to avoid duplicates
button_map::iterator it = buttonMapTakeChess.begin();
while (it != buttonMapTakeChess.end())
{
it = buttonMapTakeChess.erase(it);
}

// Add the system message and the "Accept" button to the chat history
mChatWidget->addChatMsg(true, tr("Chess Status"), QDateTime::currentDateTime(), QDateTime::currentDateTime(),
tr("%1 is inviting you to start Chess via GXS. Accept?").arg(buttonName),
ChatWidget::MSGTYPE_SYSTEM);

RSButtonOnText *button = mChatWidget->getNewButtonOnTextBrowser(tr("Accept GXS Invite"));
button->setToolTip(tr("Accept Chess Invitation"));

// Apply your specific green styling
button->setStyleSheet(QString("border: 1px solid #199909;")
.append("font-size: 12pt; color: white;")
.append("min-width: 128px; min-height: 24px;")
.append("border-radius: 6px;")
.append("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0.67, "
"stop: 0 #22c70d, stop: 1 #116a06);"));

button->updateImage();

// Connect to the logic that starts the game/tunnel
connect(button, SIGNAL(clicked()), this, SLOT(chessStart()));
connect(button, SIGNAL(mouseEnter()), this, SLOT(botMouseEnter()));
connect(button, SIGNAL(mouseLeave()), this, SLOT(botMouseLeave()));

buttonMapTakeChess.insert(buttonName, button);
}
}
}

void RetroChessChatWidgetHolder::chessPressed()
{
RsPeerId peer_id = mChatWidget->getChatId().toPeerId();//TODO support GXSID
if (rsRetroChess->hasInviteFrom(peer_id))
{
ChatId chatId = mChatWidget->getChatId();
QString peerName;
if (chatId.isDistantChatId()) {
rsRetroChess->sendGxsInvite(RsGxsId(chatId.toDistantChatId().toStdString()));

mChatWidget->addChatMsg(true, tr("RetroChess"), QDateTime::currentDateTime(),
QDateTime::currentDateTime(),
tr("Requesting secure game tunnel..."),
ChatWidget::MSGTYPE_SYSTEM);
} else {
RsPeerId peer_id = chatId.toPeerId();

if (rsRetroChess->hasInviteFrom(peer_id)){
rsRetroChess->acceptedInvite(peer_id);
mRetroChessNotify->notifyChessStart(peer_id);
return;
}

rsRetroChess->acceptedInvite(peer_id);
mRetroChessNotify->notifyChessStart(peer_id);
return;
rsRetroChess->sendInvite(peer_id);

peerName = QString::fromUtf8(rsPeers->getPeerName(peer_id).c_str());
}
rsRetroChess->sendInvite(peer_id);

QString peerName = QString::fromUtf8(rsPeers->getPeerName(peer_id).c_str());
mChatWidget->addChatMsg(true, tr("Chess Status"), QDateTime::currentDateTime(), QDateTime::currentDateTime()
, tr("You're now inviting %1 to play Chess").arg(peerName), ChatWidget::MSGTYPE_SYSTEM);
mChatWidget->addChatMsg(true, tr("Chess Status"), QDateTime::currentDateTime(), QDateTime::currentDateTime()
, tr("You're now inviting %1 to play Chess").arg(peerName), ChatWidget::MSGTYPE_SYSTEM);

}

void RetroChessChatWidgetHolder::chessStart()
{
RsPeerId peer_id = mChatWidget->getChatId().toPeerId();//TODO support GXSID

rsRetroChess->acceptedInvite(peer_id);
mRetroChessNotify->notifyChessStart(peer_id);
ChatId chatId = mChatWidget->getChatId();
if (chatId.isDistantChatId()) {
rsRetroChess->acceptedInviteGxs(RsGxsId(chatId.toDistantChatId().toStdString()));

// We wait for the service to signal handleGxsTunnelReady.
mChatWidget->addChatMsg(true, tr("RetroChess"), QDateTime::currentDateTime(),
QDateTime::currentDateTime(),
tr("Establishing secure GXS tunnel..."),
ChatWidget::MSGTYPE_SYSTEM);

// Notify the UI to open the Chess window for this GXS ID
mRetroChessNotify->notifyChessStartGxs(RsGxsId(chatId.toDistantChatId().toStdString()));
} else {
RsPeerId peer_id = chatId.toPeerId();
rsRetroChess->acceptedInvite(peer_id);
mRetroChessNotify->notifyChessStart(peer_id);
}
return;
}

void RetroChessChatWidgetHolder::handleGxsTunnelReady(const RsGxsId &gxs_id)
{
ChatId chatId = mChatWidget->getChatId();
if (chatId.isDistantChatId()) {
// Now the tunnel is safe to use. Open the window.
mRetroChessNotify->notifyChessStartGxs(gxs_id);

if (playChessButton) playChessButton->hide();
}
}

void RetroChessChatWidgetHolder::botMouseEnter()
{
RSButtonOnText *source = qobject_cast<RSButtonOnText *>(QObject::sender());
Expand Down
2 changes: 2 additions & 0 deletions gui/RetroChessChatWidgetHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public slots:
void chessStart();
void chessnotify(RsPeerId from_peer_id);

void chessnotifyGxs(const RsGxsId &from_gxs_id);
void handleGxsTunnelReady(const RsGxsId &gxs_id);

private slots:
void botMouseEnter();
Expand Down
15 changes: 15 additions & 0 deletions gui/RetroChessNotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ void RetroChessNotify::notifyChessInvite(const RsPeerId &peer_id)
emit chessInvited(peer_id) ;

}

void RetroChessNotify::notifyChessMoveGxs(const RsGxsId &gxs_id, int col, int row, int count)
{
emit chessMoveGxs(gxs_id, col, row, count);
}

void RetroChessNotify::notifyGxsTunnelReady(const RsGxsId &gxs_id)
{
emit gxsTunnelReady(gxs_id);
}

void RetroChessNotify::notifyChessStartGxs(const RsGxsId &gxs_id)
{
emit chessStartGxs(gxs_id);
}
12 changes: 12 additions & 0 deletions gui/RetroChessNotify.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,25 @@ class RetroChessNotify : public QObject
void notifyReceivedMsg(const RsPeerId &peer_id, QString str) ;
void notifyChessStart(const RsPeerId &peer_id) ;
void notifyChessInvite(const RsPeerId &peer_id) ;
/** Notify the UI of a chess move received via a GXS tunnel */
void notifyChessMoveGxs(const RsGxsId &gxs_id, int col, int row, int count);

/** Notify the UI that a GXS tunnel is now ready for use */
void notifyGxsTunnelReady(const RsGxsId &gxs_id);

void notifyChessStartGxs(const RsGxsId &gxs_id);

signals:
void NeMsgArrived(const RsPeerId &peer_id, QString str) ; // emitted when the peer gets a msg

void chessStart(const RsPeerId &peer_id) ;
void chessInvited(const RsPeerId &peer_id) ;

void chessMoveGxs(const RsGxsId &gxs_id, int col, int row, int count);
void gxsTunnelReady(const RsGxsId &gxs_id);

void chessStartGxs(const RsGxsId &gxs_id);

public slots:
};

Expand Down
Loading