CustomFriendsHelper.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "stdafx.h"
  2. #include "CustomFriendsHelper.h"
  3. #include "CP_Main.h"
  4. #include "Shared\Tokenizer.h"
  5. #include "Shared\TextConvert.h"
  6. CCustomFriendsHelper::CCustomFriendsHelper()
  7. {
  8. }
  9. CCustomFriendsHelper::~CCustomFriendsHelper()
  10. {
  11. }
  12. void CCustomFriendsHelper::Load()
  13. {
  14. m_list.clear();
  15. CString oldValues = CGetSetOptions::GetCustomSendToList();
  16. TiXmlDocument doc;
  17. CStringA xmlA;
  18. CTextConvert::ConvertToUTF8(oldValues, xmlA);
  19. doc.Parse(xmlA);
  20. TiXmlElement *ItemHeader = doc.FirstChildElement("CustomFriends");
  21. if (ItemHeader != NULL)
  22. {
  23. TiXmlElement *ItemElement = ItemHeader->FirstChildElement();
  24. while (ItemElement)
  25. {
  26. Name_Desc array_item;
  27. array_item.m_name = ItemElement->Attribute("name");
  28. array_item.m_desc = ItemElement->Attribute("desc");
  29. m_list.push_back(array_item);
  30. ItemElement = ItemElement->NextSiblingElement();
  31. }
  32. }
  33. }
  34. void CCustomFriendsHelper::Save()
  35. {
  36. /*CString values = _T("");
  37. int count = m_list.size();
  38. for (int i = 0; i < count; i++)
  39. {
  40. CString lineValue = m_list[i];
  41. values += _T(",");
  42. values += lineValue;
  43. }*/
  44. TiXmlDocument doc;
  45. TiXmlElement* friendOuter = new TiXmlElement("CustomFriends");
  46. doc.LinkEndChild(friendOuter);
  47. for (auto & listItem : m_list)
  48. {
  49. TiXmlElement* friendElement = new TiXmlElement("Friend");
  50. CStringA nameA;
  51. CTextConvert::ConvertToUTF8(listItem.m_name, nameA);
  52. friendElement->SetAttribute("name", nameA);
  53. CStringA descA;
  54. CTextConvert::ConvertToUTF8(listItem.m_desc, descA);
  55. friendElement->SetAttribute("desc", descA);
  56. friendOuter->LinkEndChild(friendElement);
  57. }
  58. TiXmlPrinter printer;
  59. printer.SetLineBreak("");
  60. doc.Accept(&printer);
  61. CString cs = printer.CStr();
  62. CGetSetOptions::SetCustomSendToList(cs);
  63. }
  64. void CCustomFriendsHelper::AddToMenu(CMenu *pMenu)
  65. {
  66. bool addedItem = false;
  67. int id = 0;
  68. for (auto & element : m_list)
  69. {
  70. if (addedItem == false)
  71. {
  72. addedItem = true;
  73. }
  74. CString cs;
  75. if (element.m_desc != _T(""))
  76. {
  77. cs.Format(_T("(%s) - %s"), element.m_name, element.m_desc);
  78. }
  79. else
  80. {
  81. cs.Format(_T("%s"), element.m_name);
  82. }
  83. pMenu->AppendMenuW(MF_STRING, (CustomFriendStartId + id), cs);
  84. id++;
  85. }
  86. if (addedItem)
  87. {
  88. pMenu->AppendMenu(MF_SEPARATOR);
  89. }
  90. pMenu->AppendMenuW(MF_STRING, (CustomFriendStartId + PromptForCustom), _T("Prompt For Name"));
  91. }
  92. void CCustomFriendsHelper::Add(CString item, CString desc)
  93. {
  94. int count = (int)m_list.size();
  95. if (count < MaxCustomFriends)
  96. {
  97. Name_Desc array_item;
  98. array_item.m_name = item;
  99. array_item.m_desc = desc;
  100. m_list.push_back(array_item);
  101. Save();
  102. }
  103. }
  104. CString CCustomFriendsHelper::GetSendTo(int id, bool &showDlg)
  105. {
  106. int index = id - CustomFriendStartId;
  107. if (index >= 0 && index < m_list.size())
  108. {
  109. return m_list[index].m_name;
  110. }
  111. if (index == PromptForCustom)
  112. {
  113. showDlg = true;
  114. }
  115. return _T("");
  116. }
  117. void CCustomFriendsHelper::ClearList()
  118. {
  119. m_list.clear();
  120. this->Save();
  121. }