CustomFriendsHelper.cpp 3.0 KB

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