ManaPlus
socialwindow.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2010 The Mana Developers
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  * Copyright (C) 2019-2021 Andrei Karas
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "gui/popups/popupmenu.h"
26 
28 
29 #include "gui/widgets/button.h"
30 #include "gui/widgets/label.h"
31 #include "gui/widgets/tabbedarea.h"
32 
40 
41 #ifdef TMWA_SUPPORT
43 
44 #include "net/tmwa/guildmanager.h"
45 #endif // TMWA_SUPPORT
46 
47 #include "debug.h"
48 
50 
52  // TRANSLATORS: social window name
53  Window(_("Social"), Modal_false, nullptr, "social.xml"),
56  mGuildInvited(0),
57  mGuildAcceptDialog(nullptr),
58  mGuildCreateDialog(nullptr),
59  mPartyInviter(),
60  mGuilds(),
61  mParties(),
62  mPartyAcceptDialog(nullptr),
63  mAttackFilter(nullptr),
64  mPickupFilter(nullptr),
65  // TRANSLATORS: here P is title for visible players tab in social window
66  mPlayers(new SocialPlayersTab(this, _("P"),
67  fromBool(getOptionBool("showtabbackground", false), Opaque))),
68  mNavigation(new SocialNavigationTab(this,
69  fromBool(getOptionBool("showtabbackground", false), Opaque))),
70  // TRANSLATORS: here F is title for friends tab in social window
71  mFriends(new SocialFriendsTab(this, _("F"),
72  fromBool(getOptionBool("showtabbackground", false), Opaque))),
73  // TRANSLATORS: social window button
74  mMenuButton(new Button(this, _("Menu"), "menu", BUTTON_SKIN, this)),
75  mCountLabel(new Label(this, "1000 / 1000")),
76  mTabs(CREATEWIDGETR(TabbedArea, this)),
77  mMap(nullptr),
78  mLastUpdateTime(0),
79  mPartyId(0),
80  mNeedUpdate(false),
81  mProcessedPortals(false)
82 {
83 }
84 
86 {
88  setWindowName("Social");
90  setSaveVisible(true);
91  setResizable(true);
92  setSaveVisible(true);
93  setCloseButton(true);
94  setStickyButtonLock(true);
95 
96  setMinWidth(120);
97  setMinHeight(55);
98  setDefaultSize(590, 200, 180, 300);
99  if (setupWindow != nullptr)
101 
102  place(0, 0, mMenuButton, 1, 1);
103  place(0, 1, mCountLabel, 1, 1);
104  place(0, 2, mTabs, 2, 4);
105 
106  widgetResized(Event(nullptr));
107 
108  loadWindowState();
109 
113  mTabs->setSelectable(false);
116 
117  if (config.getBoolValue("enableAttackFilter"))
118  {
119  mAttackFilter = new SocialAttackTab(this,
120  fromBool(getOptionBool("showtabbackground", false), Opaque));
122  }
123  else
124  {
125  mAttackFilter = nullptr;
126  }
127 
128  if (config.getBoolValue("enablePickupFilter"))
129  {
130  mPickupFilter = new SocialPickupTab(this,
131  fromBool(getOptionBool("showtabbackground", false), Opaque));
133  }
134  else
135  {
136  mPickupFilter = nullptr;
137  }
138 
139  if ((localPlayer != nullptr) && (localPlayer->getParty() != nullptr))
141 
142  if ((localPlayer != nullptr) && (localPlayer->getGuild() != nullptr))
144 
145  enableVisibleSound(true);
146  updateButtons();
148 }
149 
151 {
153  if (mGuildAcceptDialog != nullptr)
154  {
157  mGuildAcceptDialog = nullptr;
158 
159  mGuildInvited = 0;
160  }
161 
162  if (mPartyAcceptDialog != nullptr)
163  {
166  mPartyAcceptDialog = nullptr;
167 
168  mPartyInviter.clear();
169  }
175  FOR_EACH (GuildMap::iterator, it, mGuilds)
176  {
177  delete (*it).second;
178  }
179  mGuilds.clear();
180  FOR_EACH (PartyMap::iterator, it, mParties)
181  {
182  delete (*it).second;
183  }
184  mParties.clear();
185 }
186 
188 {
189  if (guild == nullptr)
190  return false;
191 
192  if (mGuilds.find(guild) != mGuilds.end())
193  return false;
194 
195  SocialTab *tab = nullptr;
196  if (guild->getServerGuild())
197  {
198  tab = new SocialGuildTab(this, guild,
199  fromBool(getOptionBool("showtabbackground", false), Opaque));
200  }
201 #ifdef TMWA_SUPPORT
202  else
203  {
204  tab = new SocialGuildTab2(this, guild,
205  fromBool(getOptionBool("showtabbackground", false), Opaque));
206  }
207 #endif // TMWA_SUPPORT
208 
209  mGuilds[guild] = tab;
210  mTabs->addTab(tab, tab->mScroll);
211 
212  updateButtons();
213 
214  return true;
215 }
216 
218 {
219  const GuildMap::iterator it = mGuilds.find(guild);
220  if (it == mGuilds.end())
221  return false;
222 
223  mTabs->removeTab(it->second);
224  delete it->second;
225  mGuilds.erase(it);
226 
227  updateButtons();
228 
229  return true;
230 }
231 
233 {
234  if (party == nullptr)
235  return false;
236 
237  if (mParties.find(party) != mParties.end())
238  return false;
239 
240  SocialPartyTab *const tab = new SocialPartyTab(this, party,
241  fromBool(getOptionBool("showtabbackground", false), Opaque));
242  mParties[party] = tab;
243 
244  mTabs->addTab(tab, tab->mScroll);
245 
246  updateButtons();
247 
248  return true;
249 }
250 
252 {
253  const PartyMap::iterator it = mParties.find(party);
254  if (it == mParties.end())
255  return false;
256 
257  mTabs->removeTab(it->second);
258  delete it->second;
259  mParties.erase(it);
260 
261  updateButtons();
262 
263  return true;
264 }
265 
267 {
268  const std::string &eventId = event.getId();
269 
270  if (event.getSource() == mPartyAcceptDialog)
271  {
272  if (eventId == "yes")
273  {
274  if (localChatTab != nullptr)
275  {
277  // TRANSLATORS: chat message
278  strprintf(_("Accepted party invite from %s."),
279  mPartyInviter.c_str()),
283  }
285  }
286  else if (eventId == "no")
287  {
288  if (localChatTab != nullptr)
289  {
291  // TRANSLATORS: chat message
292  strprintf(_("Rejected party invite from %s."),
293  mPartyInviter.c_str()),
297  }
299  }
300 
301  mPartyInviter.clear();
302  mPartyAcceptDialog = nullptr;
303  }
304  else if (event.getSource() == mGuildAcceptDialog)
305  {
306  if (eventId == "yes")
307  {
308  if (localChatTab != nullptr)
309  {
311  // TRANSLATORS: chat message
312  strprintf(_("Accepted guild invite from %s."),
313  mPartyInviter.c_str()),
317  }
318 #ifdef TMWA_SUPPORT
319  if (guildManager == nullptr || !GuildManager::getEnableGuildBot())
321  else
323 #else // TMWA_SUPPORT
324 
326 #endif // TMWA_SUPPORT
327  }
328  else if (eventId == "no")
329  {
330  if (localChatTab != nullptr)
331  {
333  // TRANSLATORS: chat message
334  strprintf(_("Rejected guild invite from %s."),
335  mPartyInviter.c_str()),
339  }
340 #ifdef TMWA_SUPPORT
341  if (guildManager == nullptr || !GuildManager::getEnableGuildBot())
343  else
345 #else // TMWA_SUPPORT
346 
348 #endif // TMWA_SUPPORT
349  }
350 
351  mGuildInvited = 0;
352  mGuildAcceptDialog = nullptr;
353  }
354  else if (eventId == "party")
355  {
357  }
358  else if (eventId == "guild")
359  {
361  }
362  else if (eventId == "attack")
363  {
365  }
366  else if (eventId == "friends")
367  {
369  }
370  else if (eventId == "navigation")
371  {
373  }
374  else if (eventId == "pickup")
375  {
377  }
378  else if (eventId == "players")
379  {
381  }
382 }
383 
384 void SocialWindow::showGuildInvite(const std::string &restrict guildName,
385  const int guildId,
386  const std::string &restrict inviterName)
387 {
388  // check there isnt already an invite showing
389  if (mGuildInvited != 0)
390  {
391  if (localChatTab != nullptr)
392  {
393  // TRANSLATORS: chat message
394  localChatTab->chatLog(_("Received guild request, but one already "
395  "exists."),
399  }
400  return;
401  }
402 
403  const std::string msg = strprintf(
404  // TRANSLATORS: chat message
405  _("%s has invited you to join the guild %s."),
406  inviterName.c_str(), guildName.c_str());
407 
408  if (localChatTab != nullptr)
409  {
414  }
415 
417  // TRANSLATORS: guild invite message
418  _("Accept Guild Invite"),
419  msg,
421  false,
422  Modal_false,
423  this);
425  mGuildInvited = guildId;
426 }
427 
428 void SocialWindow::showPartyInvite(const std::string &restrict partyName,
429  const std::string &restrict inviter,
430  const int partyId)
431 {
432  // check there isnt already an invite showing
433  if (!mPartyInviter.empty())
434  {
435  if (localChatTab != nullptr)
436  {
437  // TRANSLATORS: chat message
438  localChatTab->chatLog(_("Received party request, but one already "
439  "exists."),
443  }
444  return;
445  }
446 
447  std::string msg;
448  if (inviter.empty())
449  {
450  if (partyName.empty())
451  {
452  // TRANSLATORS: party invite message
453  msg = _("You have been invited you to join a party.");
454  }
455  else
456  {
457  // TRANSLATORS: party invite message
458  msg = strprintf(_("You have been invited to join the %s party."),
459  partyName.c_str());
460  }
461  }
462  else
463  {
464  if (partyName.empty())
465  {
466  // TRANSLATORS: party invite message
467  msg = strprintf(_("%s has invited you to join their party."),
468  inviter.c_str());
469  }
470  else
471  {
472  // TRANSLATORS: party invite message
473  msg = strprintf(_("%s has invited you to join the %s party."),
474  inviter.c_str(), partyName.c_str());
475  }
476  }
477 
478  if (localChatTab != nullptr)
479  {
484  }
485 
486  // show invite
488  // TRANSLATORS: party invite message
489  _("Accept Party Invite"),
490  msg,
492  false,
493  Modal_false,
494  this);
496  mPartyInviter = inviter;
497  mPartyId = partyId;
498 }
499 
501 {
502  mNeedUpdate = true;
503 }
504 
506 {
507  BLOCK_START("SocialWindow::slowLogic")
508  const time_t nowTime = cur_time;
509  if (mNeedUpdate && nowTime - mLastUpdateTime > 1)
510  {
511  mPlayers->updateList();
512  mFriends->updateList();
513  mNeedUpdate = false;
514  mLastUpdateTime = nowTime;
515  }
516  else if (nowTime - mLastUpdateTime > 5)
517  {
518  mPlayers->updateList();
519  mNeedUpdate = false;
520  mLastUpdateTime = nowTime;
521  }
522  BLOCK_END("SocialWindow::slowLogic")
523 }
524 
525 void SocialWindow::updateAvatar(const std::string &name)
526 {
527  mPlayers->updateAvatar(name);
528 }
529 
530 void SocialWindow::resetDamage(const std::string &name)
531 {
532  mPlayers->resetDamage(name);
533 }
534 
536 {
537 // if (!mTabs)
538 // return;
539 
540 // const bool hasTabs = mTabs->getNumberOfTabs() > 0;
541 }
542 
544 {
545  if (mNavigation != nullptr)
547 }
548 
550 {
551  if (mNavigation != nullptr)
552  static_cast<SocialNavigationTab*>(mNavigation)->updateNames();
553 }
554 
555 void SocialWindow::selectPortal(const unsigned num)
556 {
557  if (mNavigation != nullptr)
558  mNavigation->selectIndex(num);
559 }
560 
561 int SocialWindow::getPortalIndex(const int x, const int y)
562 {
563  if (mNavigation != nullptr)
564  {
565  return static_cast<SocialNavigationTab*>(
567  }
568  return -1;
569 }
570 
571 void SocialWindow::addPortal(const int x, const int y)
572 {
573  if (mNavigation != nullptr)
574  static_cast<SocialNavigationTab*>(mNavigation)->addPortal(x, y);
575 }
576 
577 void SocialWindow::removePortal(const int x, const int y)
578 {
579  if (mNavigation != nullptr)
580  static_cast<SocialNavigationTab*>(mNavigation)->removePortal(x, y);
581 }
582 
584 {
585  if (mTabs != nullptr)
586  mTabs->selectNextTab();
587 }
588 
590 {
591  if (mTabs != nullptr)
592  mTabs->selectPrevTab();
593 }
594 
596 {
597  if (mAttackFilter != nullptr)
599 }
600 
602 {
603  if (mPickupFilter != nullptr)
605 }
606 
608 {
609  if (localPlayer == nullptr)
610  return;
611 
612  Party *const party = localPlayer->getParty();
613  if (party != nullptr)
614  {
615  const PartyMap::iterator it = mParties.find(party);
616  if (it != mParties.end())
617  {
618  SocialTab *const tab = (*it).second;
619  tab->buildCounter(0, 0);
620  }
621  }
622 }
623 
625 {
626  Window::widgetResized(event);
627  if (mTabs != nullptr)
628  mTabs->adjustSize();
629 }
630 
632  const std::string &counter)
633 {
634  if (mTabs->getSelectedTab() == tab)
635  {
636  mCountLabel->setCaption(counter);
638  }
639 }
640 
641 void SocialWindow::updateMenu(const SocialTab *const tab,
642  const std::string &menu)
643 {
644  if (mTabs->getSelectedTab() == tab)
646 }
647 
648 void SocialWindow::updateGuildCounter(const int online, const int total)
649 {
650  if (localPlayer == nullptr)
651  return;
652 
653  Guild *const guild = localPlayer->getGuild();
654  if (guild != nullptr)
655  {
656  const GuildMap::iterator it = mGuilds.find(guild);
657  if (it != mGuilds.end())
658  {
659  SocialTab *const tab = (*it).second;
660  tab->buildCounter(online, total);
661  }
662  }
663 }
664 
665 void SocialWindow::updatedPlayer(const std::string &name A_UNUSED)
666 {
667  mNeedUpdate = true;
668 }
669 
671 {
672  mNeedUpdate = true;
673 }
674 
675 #ifdef USE_PROFILER
677 {
678  BLOCK_START("SocialWindow::logicChildren")
681 }
682 #endif // USE_PROFILER
volatile time_t cur_time
Definition: timer.cpp:58
#define fromBool(val, name)
Definition: booldefines.h:49
const std::string BUTTON_SKIN
Definition: button.h:89
ChatTab * localChatTab
Definition: chattab.cpp:62
virtual void logicChildren()
Party * getParty() const
Definition: being.h:330
const Guild * getGuild(const std::string &guildName) const
Definition: being.cpp:1258
Definition: button.h:102
void chatLog(std::string line, ChatMsgTypeT own, const IgnoreRecord ignoreRecord, const TryRemoveColors tryRemoveColors)
Definition: chattab.cpp:111
bool getBoolValue(const std::string &key) const
Definition: event.h:79
Widget * getSource() const
Definition: event.h:104
static void inviteResponse(const bool response)
static bool getEnableGuildBot()
Definition: guildmanager.h:56
Definition: guild.h:70
Definition: label.h:91
void adjustSize()
Definition: label.cpp:200
void setCaption(const std::string &caption)
Definition: label.cpp:264
virtual void inviteResponse(const int guildId, const bool response) const =0
virtual void inviteResponse(const int partyId, const bool accept) const =0
Definition: party.h:63
void addListener(PlayerRelationsListener *const listener)
void removeListener(PlayerRelationsListener *const listener)
void showAttackPopup()
Definition: popupmenu.cpp:3570
void showPickupPopup()
Definition: popupmenu.cpp:3590
void showPlayersPopup()
Definition: popupmenu.cpp:3600
void showFriendsPopup()
Definition: popupmenu.cpp:3610
void showPartyPopup()
Definition: popupmenu.cpp:3546
void showNavigationPopup()
Definition: popupmenu.cpp:3580
void showGuildPopup()
Definition: popupmenu.cpp:3556
void registerWindowForReset(Window *const window)
virtual void updateAvatar(const std::string &name)
Definition: socialtab.h:58
ScrollArea * mScroll
Definition: socialtab.h:138
virtual void selectIndex(const unsigned num)
Definition: socialtab.h:66
virtual void buildCounter(const int online, const int total)
Definition: socialtab.h:70
virtual void resetDamage(const std::string &name)
Definition: socialtab.h:62
virtual void updateList()
Definition: socialtab.h:54
void updateGuildCounter(const int online, const int total)
PartyMap mParties
Definition: socialwindow.h:150
void resetDamage(const std::string &name)
void updatePortals()
void removePortal(const int x, const int y)
void showGuildInvite(const std::string &guildName, const int guildId, const std::string &inviterName)
ConfirmDialog * mPartyAcceptDialog
Definition: socialwindow.h:152
SocialTab * mPickupFilter
Definition: socialwindow.h:155
void widgetResized(const Event &event)
int getPortalIndex(const int x, const int y)
Button * mMenuButton
Definition: socialwindow.h:160
std::string mPartyInviter
Definition: socialwindow.h:148
void updateAvatar(const std::string &name)
void updatePickupFilter()
void updateAttackFilter()
void addPortal(const int x, const int y)
bool removeTab(Guild *const guild)
TabbedArea * mTabs
Definition: socialwindow.h:162
void selectPortal(const unsigned num)
ConfirmDialog * mGuildAcceptDialog
Definition: socialwindow.h:146
void updateMenu(const SocialTab *const tab, const std::string &menu)
SocialTab * mPlayers
Definition: socialwindow.h:156
SocialTab * mFriends
Definition: socialwindow.h:158
void updateCounter(const SocialTab *const tab, const std::string &count)
void action(const ActionEvent &event)
void updatePortalNames()
SocialTab * mNavigation
Definition: socialwindow.h:157
void updateButtons()
bool addTab(Guild *const guild)
SocialTab * mAttackFilter
Definition: socialwindow.h:154
void showPartyInvite(const std::string &partyName, const std::string &inviter, const int partyId)
GuildMap mGuilds
Definition: socialwindow.h:149
void updateActiveList()
void updatedPlayer(const std::string &name)
Label * mCountLabel
Definition: socialwindow.h:161
time_t mLastUpdateTime
Definition: socialwindow.h:165
void updateParty()
void adjustSize()
Definition: tabbedarea.cpp:569
void removeTab(Tab *const tab)
Definition: tabbedarea.cpp:323
Widget * getTabContainer() const
Definition: tabbedarea.h:241
void selectPrevTab()
Definition: tabbedarea.cpp:828
void selectNextTab()
Definition: tabbedarea.cpp:819
Widget * getWidgetContainer() const
Definition: tabbedarea.h:244
void addTab(Tab *const tab, Widget *const widget)
Definition: tabbedarea.cpp:238
Tab * getSelectedTab() const
Definition: tabbedarea.h:175
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
void setSelectable(const bool selectable)
Definition: widget.h:948
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
Definition: window.h:102
void setSaveVisible(const bool save)
Definition: window.h:300
void setResizable(const bool resize)
Definition: window.cpp:627
virtual void close()
Definition: window.cpp:902
virtual void setVisible(Visible visible)
Definition: window.cpp:778
bool getOptionBool(const std::string &name, const bool def) const
Definition: window.cpp:1466
void setWindowName(const std::string &name)
Definition: window.h:355
void setMinHeight(const int height)
Definition: window.cpp:604
void postInit()
Definition: window.cpp:249
void setMinWidth(const int width)
Definition: window.cpp:591
void enableVisibleSound(bool b)
Definition: window.h:481
void widgetResized(const Event &event)
Definition: window.cpp:655
void setCloseButton(const bool flag)
Definition: window.cpp:749
virtual void scheduleDelete()
Definition: window.cpp:831
void setStickyButtonLock(const bool sticky)
Definition: window.cpp:772
LayoutCell & place(const int x, const int y, Widget *const wg, const int w, const int h)
Definition: window.cpp:1384
void setDefaultSize()
Definition: window.cpp:1198
void loadWindowState()
Definition: window.cpp:1087
Configuration config
#define CREATEWIDGETR(type,...)
Definition: createwidget.h:36
#define CREATEWIDGETV(var, type,...)
Definition: createwidget.h:25
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define _(s)
Definition: gettext.h:35
Net::GuildHandler * guildHandler
Definition: net.cpp:92
GuildManager * guildManager
const bool IgnoreRecord_false
Definition: ignorerecord.h:30
#define restrict
Definition: localconsts.h:165
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
LocalPlayer * localPlayer
uint32_t guild
uint32_t party
const bool Modal_false
Definition: modal.h:30
bool msg(InputEvent &event)
Definition: chat.cpp:39
MenuTypeT menu
Definition: menu.cpp:28
std::string mMap
Definition: gamerecv.cpp:46
Net::PartyHandler * partyHandler
Definition: net.cpp:94
bool Opaque
Definition: opaque.h:30
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
PlayerRelationsManager playerRelations
PopupMenu * popupMenu
Definition: popupmenu.cpp:103
SetupWindow * setupWindow
Definition: setupwindow.cpp:64
SocialWindow * socialWindow
static const std::string SOUND_REQUEST
Definition: sound.h:29
std::string strprintf(const char *const format,...)
const bool TryRemoveColors_true
const bool Visible_false
Definition: visible.h:30