mysmiles.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "mysmiles.h"
  2. #include <QDomDocument>
  3. #include <QFile>
  4. #include <QDir>
  5. #include <QTextDocument>
  6. #include <QDebug>
  7. MySmiles::MySmiles(QObject *parent) : QObject(parent)
  8. {
  9. listOfSmiles = new SmilesListModel( this );
  10. m_themeSmiles = new SmilesThemeListModel( this );
  11. this->getBuffThemes();
  12. }
  13. void MySmiles::parseXMLSmiles( const QString &inFile )
  14. {
  15. QDomDocument xmldoc;
  16. QFile file( inFile );
  17. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  18. qDebug() << "MySmiles::parseXMLSmiles(): Error open file: [" << inFile << "]: " << (int)file.error();
  19. return;
  20. }
  21. QString parseError;
  22. bool parseRes = xmldoc.setContent( &file, &parseError );
  23. if( ! parseRes ) {
  24. qDebug() << "MySmiles::parseXMLSmiles(): " << parseError;
  25. }
  26. listOfSmiles->clearList();
  27. QDomNodeList listSmNode = xmldoc.elementsByTagName("icon");
  28. QMap< QString, QString > tmpBuf;
  29. for( unsigned int n=0; n<listSmNode.length(); n++ )
  30. {
  31. QDomNode nodeSmile = listSmNode.item( n );
  32. QDomNodeList textNodeList = nodeSmile.toElement().elementsByTagName("text");
  33. QString object = nodeSmile.toElement().elementsByTagName("object").item(0).toElement().text();
  34. QFileInfo fInf( inFile );
  35. QString pathToFile = fInf.canonicalPath() + QDir::separator();
  36. for( unsigned int m=0; m<textNodeList.length(); m++ )
  37. {
  38. QString smb = textNodeList.item( m ).toElement().text();
  39. if( smb.isEmpty() || smb.isNull() || object.isEmpty() || object.isNull() ) { continue; }
  40. QString f = pathToFile + object;
  41. bufOfSmiles[ smb ] = f;
  42. if( !tmpBuf.contains(f) )
  43. {
  44. SmilesItemModel *sim = new SmilesItemModel( this );
  45. sim->setPicture( bufOfSmiles[ smb ] );
  46. sim->setText( smb );
  47. listOfSmiles->append( sim );
  48. tmpBuf[f] = smb;
  49. }
  50. //qDebug() << "** smile:["<<smb<<"] => ["<<object<<"]";
  51. }
  52. }
  53. emit smilesModelChanged();
  54. }
  55. QString MySmiles::parseMsgString( const QString inString ) //Q_INVOKABLE
  56. {
  57. QString outString = inString;
  58. QList<QString> listKeys = bufOfSmiles.keys();
  59. QList<QString>::const_iterator itr;
  60. for( itr = listKeys.constBegin(); itr != listKeys.constEnd(); itr++ )
  61. {
  62. QString smile = *itr;
  63. int pos = 0;
  64. //qDebug() << "***>" << pos << " " << smile;
  65. while( (pos = outString.indexOf( smile, pos )) >= 0 )
  66. {
  67. QString subStr1 = outString.mid( 0, pos );
  68. QString subStr2 = outString.mid( pos + smile.length() );
  69. QString img_smile = "<img width=\"32\" height=\"32\" alt=\"" + smile + "\" src=\"file://" + bufOfSmiles[ smile ] + "\"/>";
  70. outString = subStr1 + img_smile + subStr2;
  71. //qDebug() << outString;
  72. pos += img_smile.length();
  73. }
  74. }
  75. /* замена переносов строк */
  76. outString = outString.replace("\n","<br>");
  77. return outString;
  78. }
  79. void MySmiles::setFile( const QString &_fileXML )
  80. {
  81. if( _fileXML != fileXML ) //нужна ли эта проверка. При переинициализации файла, парсинга не будет. Спасет только перезапуск
  82. {
  83. fileXML = _fileXML;
  84. this->parseXMLSmiles( _fileXML );
  85. emit fileChanged();
  86. }
  87. }
  88. QString MySmiles::htmlToPlainText(const QString inString) const
  89. {
  90. QTextDocument doc;
  91. doc.setHtml( inString );
  92. return doc.toPlainText();
  93. }
  94. void MySmiles::getBuffThemes()
  95. {
  96. QString s = QString( QDir::separator() );
  97. QString dirSmileThemes = QDir::home().absolutePath()+s+".config"+s+"meegim"+s+"smiles"+ s ;
  98. QDir qd( dirSmileThemes );
  99. qd.setFilter( QDir::Dirs );
  100. m_themeSmiles->clearList();
  101. QFileInfoList list = qd.entryInfoList();
  102. //qDebug() << dirSmileThemes << "|" << list.length() ;
  103. for (int i = 0; i < list.size(); ++i )
  104. {
  105. QFileInfo dirInfo = list.at(i);
  106. QString fTheme = dirInfo.absoluteFilePath()+s+"theme.xml";
  107. if( QFile::exists(fTheme) )
  108. {
  109. //qDebug() << "Smiles: " <<fTheme;
  110. QMap<QString, QString> dataSmiles = this->getTheme( fTheme );
  111. if( (dataSmiles["file"] != "") && (dataSmiles["name"] != "") )
  112. {
  113. SmilesThemeItemModel *item = new SmilesThemeItemModel( this );
  114. item->setAuthor( dataSmiles["author"] );
  115. item->setFile( dataSmiles["file"] );
  116. item->setName( dataSmiles["name"] );
  117. item->setPicture( "file://" +dirInfo.absoluteFilePath()+ s + dataSmiles["picture"] );
  118. m_themeSmiles->append( item );
  119. }
  120. }
  121. }
  122. //for /opt/meegim/smiles - default
  123. QString defaultDir = "/opt/meegim/smiles";
  124. QMap<QString, QString> dataSmiles = this->getTheme( defaultDir + s + "theme.xml" );
  125. if( (dataSmiles["file"] != "") && (dataSmiles["name"] != "") )
  126. {
  127. SmilesThemeItemModel *item = new SmilesThemeItemModel( this );
  128. item->setAuthor( dataSmiles["author"] );
  129. item->setFile( dataSmiles["file"] );
  130. item->setName( dataSmiles["name"] );
  131. item->setPicture( "file://" +defaultDir+ s + dataSmiles["picture"] );
  132. m_themeSmiles->append( item );
  133. }
  134. }
  135. QMap<QString, QString> MySmiles::getTheme( const QString &fileTheme ) const
  136. {
  137. QMap<QString, QString> retBuf;
  138. QDomDocument xmldoc;
  139. QFile file( fileTheme );
  140. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  141. qDebug() << "MySmiles::getTheme(): Error open file: [" << fileTheme << "]: " << (int)file.error();
  142. return retBuf;
  143. }
  144. QString parseError;
  145. bool parseRes = xmldoc.setContent( &file, &parseError );
  146. if( ! parseRes ) {
  147. qDebug() << "MySmiles::getTheme(): " << parseError;
  148. }
  149. QDomElement elementMeta = xmldoc.elementsByTagName("meta").item(0).toElement();
  150. if( !elementMeta.isNull() )
  151. {
  152. QString name = elementMeta.elementsByTagName("name").item(0).toElement().text();
  153. QString author = elementMeta.elementsByTagName("author").item(0).toElement().text();
  154. QString picture = elementMeta.elementsByTagName("picture").item(0).toElement().text();
  155. //qDebug() << "** smiles:" << name << "|" << author;
  156. retBuf["file"] = fileTheme;
  157. retBuf["author"] = author;
  158. retBuf["name"] = name;
  159. retBuf["picture"] = picture;
  160. }
  161. return retBuf;
  162. }