ProtocolLooper.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2009-2011, Andrea Anzani. All rights reserved.
  3. * Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
  4. * Copyright 2021, Jaidyn Levesque. All rights reserved.
  5. * Distributed under the terms of the MIT License.
  6. *
  7. * Authors:
  8. * Andrea Anzani, andrea.anzani@gmail.com
  9. * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
  10. * Jaidyn Levesque, jadedctrl@teknik.io
  11. */
  12. #include "ProtocolLooper.h"
  13. #include <Bitmap.h>
  14. #include <String.h>
  15. #include "Account.h"
  16. #include "AppMessages.h"
  17. #include "ChatOMatic.h"
  18. #include "ChatProtocolMessages.h"
  19. #include "Conversation.h"
  20. #include "ConversationAccountItem.h"
  21. #include "ConversationView.h"
  22. #include "MainWindow.h"
  23. #include "NotifyMessage.h"
  24. #include "TheApp.h"
  25. ProtocolLooper::ProtocolLooper(ChatProtocol* protocol, int64 instance)
  26. :
  27. BLooper(),
  28. fProtocol(protocol),
  29. fInstance(instance),
  30. fListItem(NULL),
  31. fMySelf(NULL)
  32. {
  33. Account* account = reinterpret_cast<Account*>(
  34. protocol->MessengerInterface());
  35. LoadCommands();
  36. BString name(protocol->FriendlySignature());
  37. name << " - " << account->Name();
  38. SetName(name.String());
  39. _InitChatView();
  40. Run();
  41. }
  42. ProtocolLooper::~ProtocolLooper()
  43. {
  44. BMessage* msg = new BMessage(APP_ACCOUNT_DISABLED);
  45. BBitmap* icon = fProtocol->Icon();
  46. if (icon != NULL)
  47. icon->Archive(msg);
  48. msg->AddString("name", fProtocol->GetName());
  49. fProtocol->MessengerInterface()->SendMessage(msg);
  50. fProtocol->Shutdown();
  51. delete fProtocol;
  52. }
  53. void
  54. ProtocolLooper::MessageReceived(BMessage* msg)
  55. {
  56. if (Protocol()->Process(msg) != B_OK)
  57. BLooper::MessageReceived(msg);
  58. }
  59. ConversationView*
  60. ProtocolLooper::GetView()
  61. {
  62. return fSystemChatView;
  63. }
  64. void
  65. ProtocolLooper::ShowView()
  66. {
  67. MainWindow* win = ((TheApp*)be_app)->GetMainWindow();
  68. win->SetConversation(NULL);
  69. win->SetConversationView(fSystemChatView);
  70. }
  71. ChatProtocol*
  72. ProtocolLooper::Protocol()
  73. {
  74. return fProtocol;
  75. }
  76. ChatMap
  77. ProtocolLooper::Conversations() const
  78. {
  79. return fChatMap;
  80. }
  81. Conversation*
  82. ProtocolLooper::ConversationById(BString id)
  83. {
  84. bool found = false;
  85. return fChatMap.ValueFor(id, &found);
  86. }
  87. void
  88. ProtocolLooper::AddConversation(Conversation* chat)
  89. {
  90. if (chat != NULL)
  91. fChatMap.AddItem(chat->GetId(), chat);
  92. }
  93. void
  94. ProtocolLooper::RemoveConversation(Conversation* chat)
  95. {
  96. if (chat != NULL)
  97. fChatMap.RemoveItemFor(chat->GetId());
  98. }
  99. RosterMap
  100. ProtocolLooper::Contacts() const
  101. {
  102. return fRosterMap;
  103. }
  104. Contact*
  105. ProtocolLooper::ContactById(BString id)
  106. {
  107. bool found = false;
  108. return fRosterMap.ValueFor(id, &found);
  109. }
  110. void
  111. ProtocolLooper::AddContact(Contact* contact)
  112. {
  113. if (contact != NULL)
  114. fRosterMap.AddItem(contact->GetId(), contact);
  115. }
  116. void
  117. ProtocolLooper::RemoveContact(Contact* contact)
  118. {
  119. if (contact == NULL)
  120. return;
  121. fRosterMap.RemoveItemFor(contact->GetId());
  122. fUserMap.AddItem(contact->GetId(), (User*)contact);
  123. }
  124. UserMap
  125. ProtocolLooper::Users() const
  126. {
  127. UserMap users = fUserMap;
  128. for (int i = 0; i < fRosterMap.CountItems(); i++) {
  129. User* user = (User*)fRosterMap.ValueAt(i);
  130. users.AddItem(user->GetId(), user);
  131. }
  132. return users;
  133. }
  134. User*
  135. ProtocolLooper::UserById(BString id)
  136. {
  137. bool found = false;
  138. User* user = ContactById(id);
  139. if (user == NULL)
  140. user = fUserMap.ValueFor(id, &found);
  141. return user;
  142. }
  143. void
  144. ProtocolLooper::AddUser(User* user)
  145. {
  146. if (user != NULL)
  147. fUserMap.AddItem(user->GetId(), user);
  148. }
  149. CommandMap
  150. ProtocolLooper::Commands() const
  151. {
  152. return fCommands;
  153. }
  154. ChatCommand*
  155. ProtocolLooper::CommandById(BString id)
  156. {
  157. return fCommands.ValueFor(id);
  158. }
  159. Contact*
  160. ProtocolLooper::GetOwnContact()
  161. {
  162. return fMySelf;
  163. }
  164. void
  165. ProtocolLooper::SetOwnContact(Contact* contact)
  166. {
  167. if (contact != NULL) {
  168. fMySelf = contact;
  169. AddUser(contact);
  170. }
  171. }
  172. int64
  173. ProtocolLooper::GetInstance()
  174. {
  175. return fInstance;
  176. }
  177. ConversationAccountItem*
  178. ProtocolLooper::GetListItem()
  179. {
  180. if (fListItem == NULL)
  181. fListItem = new ConversationAccountItem(fProtocol->GetName(),
  182. fInstance, this);
  183. return fListItem;
  184. }
  185. void
  186. ProtocolLooper::LoadCommands()
  187. {
  188. fCommands = CommandMap();
  189. BObjectList<BMessage> commands = fProtocol->Commands();
  190. for (int i = 0; i < commands.CountItems(); i++) {
  191. ChatCommand* cmd = new ChatCommand(commands.ItemAt(i));
  192. fCommands.AddItem(cmd->GetName(), cmd);
  193. }
  194. }
  195. void
  196. ProtocolLooper::_InitChatView()
  197. {
  198. fSystemChatView = new ConversationView();
  199. BMessage clear(kClearText);
  200. fSystemChatView->MessageReceived(&clear);
  201. BMessage init(IM_MESSAGE);
  202. init.AddInt32("im_what", IM_MESSAGE_RECEIVED);
  203. init.AddString("user_name", APP_NAME);
  204. init.AddString("body", B_TRANSLATE("I'm rearing to go!"));
  205. fSystemChatView->MessageReceived(&init);
  206. fSystemChatView->ObserveString(STR_ROOM_NAME, fProtocol->GetName());
  207. fSystemChatView->ObserveString(STR_ROOM_SUBJECT, "System buffer");
  208. BBitmap* icon = fProtocol->Icon();
  209. if (icon != NULL)
  210. fSystemChatView->ObservePointer(PTR_ROOM_BITMAP, (void*)icon);
  211. }