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