storevcard.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "storevcard.h"
  2. #include <QFile>
  3. #include <QDir>
  4. #include <QDebug>
  5. StoreVCard::StoreVCard(QObject *parent) : QObject(parent)
  6. {
  7. }
  8. QString StoreVCard::getElementStore( const QDomDocument *doc, const QString &nodeName )
  9. {
  10. QString ret("");
  11. QDomNode nodeElement = doc->elementsByTagName( nodeName ).item(0);
  12. QDomNode te = nodeElement.firstChild();
  13. if( !te.isNull() ) {
  14. ret = te.nodeValue();
  15. }
  16. return ret;
  17. }
  18. bool StoreVCard::setVCard( const QString &bareJid, vCardData &vCard )
  19. {
  20. QDomDocument vCardXMLDoc("vCard");
  21. if( pathCache.isEmpty() ) {
  22. return false;
  23. }
  24. QDomElement rootVCard = vCardXMLDoc.createElement("vCard");
  25. vCardXMLDoc.appendChild( rootVCard );
  26. QDomElement nodeNickname = vCardXMLDoc.createElement( "nickName" );
  27. rootVCard.appendChild(nodeNickname);
  28. QDomText txtNickname = vCardXMLDoc.createTextNode( vCard.nickName );
  29. nodeNickname.appendChild( txtNickname );
  30. //setElementStore( "firstName", vCard.firstName );
  31. nodeNickname = vCardXMLDoc.createElement( "firstName" );
  32. rootVCard.appendChild(nodeNickname);
  33. txtNickname = vCardXMLDoc.createTextNode( vCard.firstName );
  34. nodeNickname.appendChild( txtNickname );
  35. //setElementStore( "middleName", vCard.middleName );
  36. nodeNickname = vCardXMLDoc.createElement( "middleName" );
  37. rootVCard.appendChild(nodeNickname);
  38. txtNickname = vCardXMLDoc.createTextNode( vCard.middleName );
  39. nodeNickname.appendChild( txtNickname );
  40. //setElementStore( "lastName", vCard.lastName );
  41. nodeNickname = vCardXMLDoc.createElement( "lastName" );
  42. rootVCard.appendChild(nodeNickname);
  43. txtNickname = vCardXMLDoc.createTextNode( vCard.lastName );
  44. nodeNickname.appendChild( txtNickname );
  45. //setElementStore( "url", vCard.url );
  46. nodeNickname = vCardXMLDoc.createElement( "url" );
  47. rootVCard.appendChild(nodeNickname);
  48. txtNickname = vCardXMLDoc.createTextNode( vCard.url );
  49. nodeNickname.appendChild( txtNickname );
  50. //setElementStore( "eMail", vCard.eMail );
  51. nodeNickname = vCardXMLDoc.createElement( "eMail" );
  52. rootVCard.appendChild(nodeNickname);
  53. txtNickname = vCardXMLDoc.createTextNode( vCard.eMail );
  54. nodeNickname.appendChild( txtNickname );
  55. //setElementStore( "fullName", vCard.fullName );
  56. nodeNickname = vCardXMLDoc.createElement( "fullName" );
  57. rootVCard.appendChild(nodeNickname);
  58. txtNickname = vCardXMLDoc.createTextNode( vCard.fullName );
  59. nodeNickname.appendChild( txtNickname );
  60. //qDebug() << "doc=" << vCardXMLDoc.toString();
  61. QString fileVCard = pathCache + QDir::separator() + bareJid + QDir::separator() +"vCard.xml";
  62. QFile xmlVCardFile(fileVCard);
  63. if( !xmlVCardFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
  64. {
  65. qCritical() << "commitVCard: Failed to open file for writing: " << fileVCard;
  66. return false;
  67. }
  68. QTextStream stream( &xmlVCardFile );
  69. stream << vCardXMLDoc.toString();
  70. xmlVCardFile.close();
  71. return true;
  72. }
  73. vCardData StoreVCard::getVCard( const QString &bareJid )
  74. {
  75. QDomDocument vCardXMLDoc;
  76. vCardData data;
  77. QString fileVCard = pathCache + QDir::separator() + bareJid + QDir::separator() +"vCard.xml";
  78. QFile xmlVCardFile(fileVCard);
  79. if( xmlVCardFile.exists() )
  80. {
  81. if( xmlVCardFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
  82. if( vCardXMLDoc.setContent( &xmlVCardFile ) ) {
  83. xmlVCardFile.close();
  84. }
  85. } else {
  86. qWarning() << "initVCard: Failed to open file: " << fileVCard;
  87. }
  88. }
  89. else
  90. {
  91. return data;
  92. }
  93. data.nickName = getElementStore( &vCardXMLDoc, "nickName" );
  94. data.firstName = getElementStore( &vCardXMLDoc, "firstName" );
  95. data.middleName = getElementStore( &vCardXMLDoc, "middleName" );
  96. data.lastName = getElementStore( &vCardXMLDoc, "lastName" );
  97. data.url = getElementStore( &vCardXMLDoc, "url" );
  98. data.eMail = getElementStore( &vCardXMLDoc, "eMail" );
  99. data.fullName = getElementStore( &vCardXMLDoc, "fullName" );
  100. //qDebug() << "isVCardEmpty ? " << data.isEmpty();
  101. return data;
  102. }