2 #include <QXmlStreamWriter>
4 #include "xmlcontroler.h"
6 #include "groupwidgetitem.h"
8 XmlControler* XmlControler::instance = 0;
10 XmlControler* XmlControler::getInstance()
14 instance = new XmlControler();
19 XmlControler::XmlControler(QObject *parent) :
25 XmlControler::~XmlControler()
30 void XmlControler::closeXmlFile()
32 if( m_XmlFileOut->isOpen() )
34 m_XmlFileOut->close();
39 if( m_XmlFileIn->isOpen() )
41 writeXml( m_XmlFileIn );
48 bool XmlControler::newXml( const QString &filename )
50 //qDebug() << "XmlControler::newXml( const QString &filename ), Entry";
52 m_filename = filename;
53 m_XmlFileOut = new QFile( filename );
54 if( !m_XmlFileOut->open( QFile::ReadWrite | QFile::Text ) )
56 //qDebug() << "open file is failed:" << m_XmlFileOut->errorString();
60 newXml( m_XmlFileOut );
61 m_XmlFileOut->close();
68 bool XmlControler::newXml( QIODevice *device )
70 //qDebug() << "XmlControler::newXml( QIODevice *device ), Entry";
74 QTextStream out( device );
75 QDomNode xmlNode = m_DomDoc.createProcessingInstruction( "xml",
76 "version=\"1.0\" encoding=\"UTF-8\"");
77 m_DomDoc.insertBefore( xmlNode, m_DomDoc.firstChild() );
79 QDomElement root = m_DomDoc.createElement( STR_XML_GROUPSMS );
80 root.setAttribute( STR_XML_VERSION, STR_XML_VERSION_NUMBER );
81 m_DomDoc.appendChild( root );
83 m_DomDoc.save( out, Indent );
87 bool XmlControler::readXml( const QString &filename )
89 //qDebug() << "XmlControler::readXml( const QString &filename ), Entry";
91 m_filename = filename;
92 m_XmlFileOut = new QFile( filename );
93 if( !m_XmlFileOut->open( QFile::ReadOnly | QFile::Text ) )
95 //qDebug() << "open file is failed:" << m_XmlFileOut->errorString();
99 if( !readXml( m_XmlFileOut ) )
107 bool XmlControler::readXml( QIODevice *device )
109 //qDebug() << "XmlControler::readXml( QIODevice *device ), Entry";
115 if( !m_DomDoc.setContent( device, true, &errorStr, &errorLine, &errorColumn ) )
117 QMessageBox::information( NULL, tr("XML file"),
118 tr("Parse error at line %1, column %2:\n%3")
128 bool XmlControler::writeXml( QIODevice *device )
130 //qDebug() << "XmlControler::writeXml( QIODevice *device ), Entry";
132 const int IndentSize = 4;
134 QTextStream out(device);
135 m_DomDoc.save( out, IndentSize );
139 bool XmlControler::writeXml()
141 //qDebug() << "XmlControler::writeXml(), Entry";
143 const int IndentSize = 4;
145 m_XmlFileIn = new QFile( m_filename );
146 if( !m_XmlFileIn->open( QFile::WriteOnly | QFile::Text ) )
148 //qDebug() << "open file is failed:" << m_XmlFileIn->errorString();
152 QTextStream out(m_XmlFileIn);
153 m_DomDoc.save( out, IndentSize );
154 m_XmlFileIn->close();
160 bool XmlControler::createAllContacts()
162 //qDebug() << "XmlControler::createAllContacts, Entry";
164 QDomElement root = m_DomDoc.documentElement();
165 QDomElement allcontacts = m_DomDoc.createElement( STR_XML_ALLCONTACTS );
166 root.appendChild( allcontacts );
168 QDomElement node_contact = m_DomDoc.createElement( STR_XML_CONTACT );
169 allcontacts.appendChild( node_contact );
170 QDomText contact_fullname = m_DomDoc.createTextNode( "Tom for Test" );
171 node_contact.appendChild( contact_fullname );
172 QDomText contact_mobilenumber = m_DomDoc.createTextNode( "number for Test" );
173 node_contact.appendChild( contact_mobilenumber );
175 m_DomDoc.appendChild( root );
180 bool XmlControler::createGroup(const QString &groupname)
182 //qDebug() << "XmlControler::createGroup(const QString &groupname), Entry";
184 findGroup( groupname );
188 QDomElement root = m_DomDoc.documentElement();
189 QDomElement group = m_DomDoc.createElement( STR_XML_GROUP );
190 group.setAttribute( STR_XML_GROUP_NAME, groupname );
193 root.appendChild( group );
196 Item *item = new Item();
198 item->group_name = groupname;
199 item->m_isGroup = true;
201 itemObserver->addGroup( item );
206 bool XmlControler::createContact(const QString &group, ItemListPtr contacts)
208 QDomNode root = findGroup(group);
211 qDebug() << "could not find this" << group << "existed. create it.";
212 createGroup( group );
213 root = findGroup(group);
216 for( int i = 0; i < contacts.size(); i++ )
218 contacts.at(i)->group_owner = group;
219 createContact( &root, contacts.at(i) );
221 itemObserver->addContact( contacts, group);
225 bool XmlControler::createContact(const QString &group, Item *contact)
227 //qDebug() << "XmlControler::createContact(const QString &group, Item *contact), Entry";
229 QDomNode root = findGroup(group);
232 //qDebug() << "could not find this" << group << "existed. create it.";
233 createGroup( group );
234 root = findGroup(group);
237 contact->group_owner = group;
238 createContact( &root, contact );
240 itemObserver->addContact( contact, group);
245 bool XmlControler::createContact(QDomNode *group, Item *contact)
247 //qDebug() << "XmlControler::createContact(QDomElement *group, Item *contact), Entry";
250 QDomElement node = m_DomDoc.createElement(STR_XML_CONTACT);
251 node.setAttribute( STR_XML_CONTACT_UID, contact->uid);
252 node.setAttribute( STR_XML_CONTACT_FULL_NAME, contact->full_name);
253 node.setAttribute( STR_XML_CONTACT_MOBILE_NUMBER, contact->mobile_number);
254 node.setAttribute( STR_XML_CONTACT_PIC_URI, contact->user_pic_uri);
255 node.setAttribute( STR_XML_CONTACT_GROUP_OWNER, contact->group_owner);
257 // node.setAttribute( STR_XML_CONTACT_UID, "1");
258 // node.setAttribute( STR_XML_CONTACT_FULL_NAME, "Tom 1");
259 // node.setAttribute( STR_XML_CONTACT_MOBILE_NUMBER, "12345678901");
260 // node.setAttribute( STR_XML_CONTACT_PIC_URI, "/alsdj/a;sdj/pic");
261 // node.setAttribute( STR_XML_CONTACT_GROUP_OWNER, "groupowner");
263 group->appendChild(node);
269 QDomNode XmlControler::findGroup(const QString &groupname)
271 //qDebug() << "XmlControler::findGroup(const QString &group), Entry";
277 node = m_DomDoc.firstChild();
278 while( !node.isNull() && !foundgroup )
280 //qDebug() << "node1 name : " << node.toElement().tagName();
281 //qDebug() << "node1 attr : " << node.toElement().attribute(STR_XML_VERSION);
282 if( node.hasChildNodes() )
284 QDomNode node2 = node.firstChild();
285 while( !node2.isNull() )
287 //qDebug() << "node2 name : " << node2.toElement().tagName();
288 //qDebug() << "node2 attr : " << node2.toElement().attribute(STR_XML_GROUP_NAME);
290 if( groupname == node2.toElement().attribute(STR_XML_GROUP_NAME) )
292 //qDebug() << "group is found";
298 node2 = node2.nextSibling();
302 node = node.nextSibling();
307 QDomNode XmlControler::findContact(const QString &groupname, Item *contact)
309 //qDebug() << "XmlControler::findContact(const QString &groupname, ContactWidgetItem *contact), Entry";
312 QDomNode group = findGroup(groupname);
315 element = findContact(group, contact);
320 QDomNode XmlControler::findContact(QDomNode group, Item *contact)
322 //qDebug() << "XmlControler::findContact(QDomNode *group, ContactWidgetItem *contact), Entry";
325 QDomNode node = group;
326 foundcontact = false;
327 while( !node.isNull() && !foundcontact )
329 if( node.hasChildNodes() )
331 QDomNode node2 = node.firstChild();
332 while( !node2.isNull() )
334 //qDebug() << "node2 name : " << node2.toElement().tagName();
335 //qDebug() << "node2 uid : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
337 if( contact->uid == node2.toElement().attribute(STR_XML_CONTACT_UID) )
338 //if( "1" == node2.toElement().attribute(STR_XML_CONTACT_UID) ) // for Test
340 //qDebug() << "contact is found";
346 node2 = node2.nextSibling();
350 node = node.nextSibling();
356 bool XmlControler::removeGroup(const QString &groupname)
358 //qDebug() << "XmlControler::removeGroup(const QString &groupname), Entry";
360 QDomNode group = findGroup(groupname);
363 QDomElement root = m_DomDoc.documentElement();
364 root.removeChild(group);
370 bool XmlControler::removeContact( Item *contact )
372 //qDebug() << "XmlControler::removeContact(const QString &group, ContactWidgetItem *contact), Entry";
374 QDomNode root = findGroup( contact->group_owner );
377 //qDebug() << "root is NULL";
380 removeContact( root, contact );
385 bool XmlControler::removeContact(const QString &group, Item *contact)
387 //qDebug() << "XmlControler::removeContact(const QString &group, ContactWidgetItem *contact), Entry";
389 QDomNode root = findGroup(group);
392 //qDebug() << "root is NULL";
395 removeContact( root, contact );
400 bool XmlControler::removeContact(QDomNode group, Item *contact)
402 //qDebug() << "XmlControler::removeContact(QDomNode *group, ContactWidgetItem *contact), Entry";
404 QDomNode node = findContact(group, contact);
407 //qDebug() << "node is NULL";
410 QDomNode del = group.removeChild(node);
413 //qDebug() << "del node is NULL";
417 itemObserver->removeContact(contact);
422 bool XmlControler::cleanAllContacts()
424 //TODO : clean all contacts in observer.
425 itemObserver->removeAllContacts();
430 bool XmlControler::cleanAllContactsGroup()
432 for( int i = 0; i < all_contacts_items_group.size(); i++ )
434 delete all_contacts_items_group.at(i);
436 all_contacts_items_group.clear();
441 void XmlControler::setItemObserver(ItemObserver *observer)
443 itemObserver = observer;
446 void XmlControler::setItemSelectObserver( ItemSelectObserver *observer )
448 itemSelectObserver = observer;
451 void XmlControler::getAllContactItems()
453 //qDebug() << "XmlControler::getAllContactItems(), Entry";
455 QDomElement root = m_DomDoc.documentElement();
456 QDomNode node = root.firstChild();
461 while( !node.isNull() )
463 tagname = node.toElement().tagName();
464 //qDebug() << "node tagname : " << tagname;
466 if( STR_XML_GROUP == tagname )
468 //qDebug() << "node is : " << STR_XML_GROUP;
470 Item *item = new Item();
472 item->group_name = node.toElement().attribute(STR_XML_GROUP_NAME);
473 item->m_isGroup = true;
475 itemObserver->addGroup( item );
477 if( node.hasChildNodes() )
479 QDomNode node2 = node.firstChild();
480 while( !node2.isNull() )
482 tagname = node2.toElement().tagName();
483 if( STR_XML_CONTACT == tagname )
485 //qDebug() << "node is : " << STR_XML_CONTACT;
487 Item *item = new Item();
488 item->m_isGroup = false;
490 item->full_name = node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
491 //qDebug() << "fullname is : " << node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
493 item->mobile_number = node2.toElement().attribute(STR_XML_CONTACT_MOBILE_NUMBER);
494 //qDebug() << "mobile number is : " << node2.toElement().attribute(STR_XML_CONTACT_MOBILE_NUMBER);
496 item->group_owner = node2.toElement().attribute(STR_XML_CONTACT_GROUP_OWNER);
497 //qDebug() << "group owner is : " << node2.toElement().attribute(STR_XML_CONTACT_GROUP_OWNER);
499 item->uid = node2.toElement().attribute(STR_XML_CONTACT_UID);
500 //qDebug() << "uid is : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
502 itemObserver->addContact( item );
504 node2 = node2.nextSibling();
508 node = node.nextSibling();
510 //qDebug() << "XmlControler::getAllContactItems(), Exit";
513 QStringList XmlControler::getAllGroupNames()
515 //qDebug() << "XmlControler::getAllGroupNames(), Entry";
517 QDomElement root = m_DomDoc.documentElement();
518 QDomNode node = root.firstChild();
522 while( !node.isNull() )
524 tagname = node.toElement().tagName();
526 if( STR_XML_GROUP == tagname )
528 //qDebug() << "group name : " << node.toElement().attribute(STR_XML_GROUP_NAME);
529 strlist.append( node.toElement().attribute(STR_XML_GROUP_NAME) );
531 node = node.nextSibling();
533 //qDebug() << "XmlControler::getAllGroupNames(), Exit";
538 void XmlControler::getAllContactItemsFromGroup( QString groupname )
540 //qDebug() << "XmlControler::getAllContactItemsFromGroup( QString groupname ), Entry";
542 QDomElement root = m_DomDoc.documentElement();
543 QDomNode node = root.firstChild();
547 if( old_groupname == groupname )
549 itemSelectObserver->getGroupContacts( all_contacts_items_group );
553 cleanAllContactsGroup();
555 while( !node.isNull() )
557 tagname = node.toElement().tagName();
558 group = node.toElement().attribute(STR_XML_GROUP_NAME);
559 //qDebug() << "node tagname : " << tagname << "group:" << group;
561 if( (STR_XML_GROUP == tagname) && (group == groupname) )
563 //qDebug() << "node is : " << STR_XML_GROUP;
565 Item *item = new Item();
567 item->group_name = node.toElement().attribute(STR_XML_GROUP_NAME);
568 item->m_isGroup = true;
570 all_contacts_items_group.append( item );
572 if( node.hasChildNodes() )
574 QDomNode node2 = node.firstChild();
575 while( !node2.isNull() )
577 tagname = node2.toElement().tagName();
578 if( STR_XML_CONTACT == tagname )
580 //qDebug() << "node is : " << STR_XML_CONTACT;
582 Item *item = new Item();
583 //qDebug() << "new ContactWidgetItem";
585 item->full_name = node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
586 //qDebug() << "fullname is : " << node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
588 item->mobile_number = node2.toElement().attribute(STR_XML_CONTACT_MOBILE_NUMBER);
589 //qDebug() << "mobile number is : " << node2.toElement().attribute(STR_XML_CONTACT_MOBILE_NUMBER);
591 item->group_owner = node2.toElement().attribute(STR_XML_CONTACT_GROUP_OWNER);
592 //qDebug() << "group owner is : " << node2.toElement().attribute(STR_XML_CONTACT_GROUP_OWNER);
594 item->uid = node2.toElement().attribute(STR_XML_CONTACT_UID);
595 //qDebug() << "uid is : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
597 all_contacts_items_group.append( item );
599 node2 = node2.nextSibling();
603 node = node.nextSibling();
605 itemSelectObserver->getGroupContacts( all_contacts_items_group );
606 old_groupname = groupname;
607 //qDebug() << "XmlControler::getAllContactItemsFromGroup( QString groupname ), Exit";
610 bool XmlControler::removeContactFromGroup( ItemListPtr items, const QString &groupname )
612 QDomNode root = findGroup(groupname);
615 //qDebug() << "root is NULL";
618 for( int i = 0; items.size(); i++ )
620 removeContact( root, items.at(i) );
622 itemObserver->refreshContactsList();
627 bool XmlControler::removeContactFromGroup( ItemListPtr items )
629 for( int i = 0; i < items.size(); i++ )
631 removeContact( items.at(i) );
633 itemObserver->refreshContactsList();