123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- #include "Emoconfig.h"
- #include <File.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <Bitmap.h>
- #include <String.h>
- #include <Path.h>
- #include <TranslationUtils.h>
- //tmp
- BMessage* faces = NULL;
- bool valid = false;
- bool fname = false;
- bool svg = false;
- bool size = true;
- BString filename;
- BString face;
- BPath path;
- BString gCharacters;
- Emoconfig::Emoconfig(const char* xmlfile): BMessage()
- {
- fEmoticonSize = 16.0; //default
- numfaces = 0;
- fParser = XML_ParserCreate(NULL);
- XML_SetUserData(fParser, this);
- XML_SetElementHandler(fParser, StartElement, EndElement);
- XML_SetCharacterDataHandler(fParser, Characters);
- //path!
- BPath p(xmlfile);
- p.GetParent(&path);
- // loading the config file..
- BFile* settings = new BFile(xmlfile, B_READ_ONLY);
- off_t size;
- settings->GetSize(&size);
- if (size) {
- void* buffer = malloc((size_t)size);
- size = settings->Read(buffer, (size_t)size);
- XML_Parse(fParser, (const char*)buffer, (int)size, true);
- free(buffer);
- }
- delete settings;
- if (fParser)
- XML_ParserFree(fParser);
- printf("Emoconfig: loaded %d faces\n", numfaces);
- }
- Emoconfig::~Emoconfig()
- {
- }
- void
- Emoconfig::StartElement(void * /*pUserData*/, const char* pName, const char** /*pAttr*/)
- {
- //printf("StartElement %s\n",pName);
- BString name(pName);
- if (name.ICompare("emoticon") == 0) {
- faces = new BMessage();
- svg = false;
- } else if (name.ICompare("text") == 0 && faces) {
- valid = true;
- } else if (name.ICompare("file") == 0 && faces) {
- fname = true;
- } else if (name.ICompare("svg") == 0 && faces) {
- // printf("File is SVG\n");
- svg = true;
- } else if (name.ICompare("size") == 0) {
- size = true;
- gCharacters = "";
- }
- }
- void
- Emoconfig::EndElement(void* pUserData, const char* pName)
- {
- //printf("EndElement %s\n",pName);
- BString name(pName);
- if (name.ICompare("emoticon") == 0 && faces) {
- //faces->PrintToStream(); //debug
- delete faces;
- faces = NULL;
- } else if (name.ICompare("text") == 0 && faces) {
- valid = false;
- faces->AddString("face", face);
- //printf("to ]%s[\n",face.String());
- face.SetTo("");
- } else if (name.ICompare("file") == 0 && faces) {
- //load file
- //compose the filename
- BPath p(path);
- p.Append(filename.String());
- BBitmap* icons = NULL;
- if ( !svg ) {
- //
- icons = BTranslationUtils::GetBitmap(p.Path());
- }
- //assign to faces;
- fname = false;
- // printf("Filename %s [%s]\n",p.Path(),path.Path());
- if (!icons) return;
- int i = 0;
- BString s;
- while (faces->FindString("face", i, &s) == B_OK) {
- if (i == 0) {
- ((Emoconfig*)pUserData)->menu.AddPointer(s.String(), (const void*)icons);
- ((Emoconfig*)pUserData)->menu.AddString("face", s.String());
- }
- ((BMessage*)pUserData)->AddPointer(s.String(), (const void*)icons);
- ((BMessage*)pUserData)->AddString("face", s.String());
- ((Emoconfig*)pUserData)->numfaces++;
- i++;
- }
- } else if (name.ICompare("size") == 0) {
- if ( size ) {
- ((Emoconfig*)pUserData)->fEmoticonSize = atoi(gCharacters.String());
- }
- size = false;
- }
- }
- void
- Emoconfig::Characters(void * /*pUserData*/, const char* pString, int pLen)
- {
- BString f(pString, pLen);
- //printf("Characters %s\n",f.String());
- if (faces && valid) {
- f.RemoveAll(" ");
- f.RemoveAll("\"");
- if (f.Length() > 0)
- face.Append(f);
- } else if (fname) {
- f.RemoveAll(" ");
- filename = f;
- } else {
- gCharacters.Append(f);
- }
- }
|