Emoconfig.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "Emoconfig.h"
  2. #include <File.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <Bitmap.h>
  6. #include <String.h>
  7. #include <Path.h>
  8. #include <TranslationUtils.h>
  9. //tmp
  10. BMessage* faces = NULL;
  11. bool valid = false;
  12. bool fname = false;
  13. bool svg = false;
  14. bool size = true;
  15. BString filename;
  16. BString face;
  17. BPath path;
  18. BString gCharacters;
  19. Emoconfig::Emoconfig(const char* xmlfile): BMessage()
  20. {
  21. fEmoticonSize = 16.0; //default
  22. numfaces = 0;
  23. fParser = XML_ParserCreate(NULL);
  24. XML_SetUserData(fParser, this);
  25. XML_SetElementHandler(fParser, StartElement, EndElement);
  26. XML_SetCharacterDataHandler(fParser, Characters);
  27. //path!
  28. BPath p(xmlfile);
  29. p.GetParent(&path);
  30. // loading the config file..
  31. BFile* settings = new BFile(xmlfile, B_READ_ONLY);
  32. off_t size;
  33. settings->GetSize(&size);
  34. if (size) {
  35. void* buffer = malloc((size_t)size);
  36. size = settings->Read(buffer, (size_t)size);
  37. XML_Parse(fParser, (const char*)buffer, (int)size, true);
  38. free(buffer);
  39. }
  40. delete settings;
  41. if (fParser)
  42. XML_ParserFree(fParser);
  43. printf("Emoconfig: loaded %d faces\n", numfaces);
  44. }
  45. Emoconfig::~Emoconfig()
  46. {
  47. }
  48. void
  49. Emoconfig::StartElement(void * /*pUserData*/, const char* pName, const char** /*pAttr*/)
  50. {
  51. //printf("StartElement %s\n",pName);
  52. BString name(pName);
  53. if (name.ICompare("emoticon") == 0) {
  54. faces = new BMessage();
  55. svg = false;
  56. } else if (name.ICompare("text") == 0 && faces) {
  57. valid = true;
  58. } else if (name.ICompare("file") == 0 && faces) {
  59. fname = true;
  60. } else if (name.ICompare("svg") == 0 && faces) {
  61. // printf("File is SVG\n");
  62. svg = true;
  63. } else if (name.ICompare("size") == 0) {
  64. size = true;
  65. gCharacters = "";
  66. }
  67. }
  68. void
  69. Emoconfig::EndElement(void* pUserData, const char* pName)
  70. {
  71. //printf("EndElement %s\n",pName);
  72. BString name(pName);
  73. if (name.ICompare("emoticon") == 0 && faces) {
  74. //faces->PrintToStream(); //debug
  75. delete faces;
  76. faces = NULL;
  77. } else if (name.ICompare("text") == 0 && faces) {
  78. valid = false;
  79. faces->AddString("face", face);
  80. //printf("to ]%s[\n",face.String());
  81. face.SetTo("");
  82. } else if (name.ICompare("file") == 0 && faces) {
  83. //load file
  84. //compose the filename
  85. BPath p(path);
  86. p.Append(filename.String());
  87. BBitmap* icons = NULL;
  88. if ( !svg ) {
  89. //
  90. icons = BTranslationUtils::GetBitmap(p.Path());
  91. }
  92. //assign to faces;
  93. fname = false;
  94. // printf("Filename %s [%s]\n",p.Path(),path.Path());
  95. if (!icons) return;
  96. int i = 0;
  97. BString s;
  98. while (faces->FindString("face", i, &s) == B_OK) {
  99. if (i == 0) {
  100. ((Emoconfig*)pUserData)->menu.AddPointer(s.String(), (const void*)icons);
  101. ((Emoconfig*)pUserData)->menu.AddString("face", s.String());
  102. }
  103. ((BMessage*)pUserData)->AddPointer(s.String(), (const void*)icons);
  104. ((BMessage*)pUserData)->AddString("face", s.String());
  105. ((Emoconfig*)pUserData)->numfaces++;
  106. i++;
  107. }
  108. } else if (name.ICompare("size") == 0) {
  109. if ( size ) {
  110. ((Emoconfig*)pUserData)->fEmoticonSize = atoi(gCharacters.String());
  111. }
  112. size = false;
  113. }
  114. }
  115. void
  116. Emoconfig::Characters(void * /*pUserData*/, const char* pString, int pLen)
  117. {
  118. BString f(pString, pLen);
  119. //printf("Characters %s\n",f.String());
  120. if (faces && valid) {
  121. f.RemoveAll(" ");
  122. f.RemoveAll("\"");
  123. if (f.Length() > 0)
  124. face.Append(f);
  125. } else if (fname) {
  126. f.RemoveAll(" ");
  127. filename = f;
  128. } else {
  129. gCharacters.Append(f);
  130. }
  131. }