--- /dev/null
+#include <QtGui>
+#include <QXmlStreamWriter>
+
+#include "xmlcontroler.h"
+#include "xmlstring.h"
+#include "groupwidgetitem.h"
+
+XmlControler* XmlControler::instance = 0;
+
+XmlControler* XmlControler::getInstance()
+{
+ if( !instance )
+ {
+ instance = new XmlControler();
+ }
+ return instance;
+}
+
+XmlControler::XmlControler(QObject *parent) :
+ QObject(parent)
+{
+ instance = this;
+}
+
+XmlControler::~XmlControler()
+{
+ closeXmlFile();
+}
+
+void XmlControler::closeXmlFile()
+{
+ if( m_XmlFileOut->isOpen() )
+ {
+ m_XmlFileOut->close();
+ delete m_XmlFileOut;
+ m_XmlFileOut = NULL;
+ }
+
+ if( m_XmlFileIn->isOpen() )
+ {
+ writeXml( m_XmlFileIn );
+ m_XmlFileIn->close();
+ delete m_XmlFileIn;
+ m_XmlFileIn = NULL;
+ }
+}
+
+bool XmlControler::newXml( const QString &filename )
+{
+ //qDebug() << "XmlControler::newXml( const QString &filename ), Entry";
+
+ m_filename = filename;
+ m_XmlFileOut = new QFile( filename );
+ if( !m_XmlFileOut->open( QFile::ReadWrite | QFile::Text ) )
+ {
+ //qDebug() << "open file is failed:" << m_XmlFileOut->errorString();
+ return false;
+ }
+
+ newXml( m_XmlFileOut );
+ m_XmlFileOut->close();
+ delete m_XmlFileOut;
+ m_XmlFileOut = NULL;
+
+ return true;
+}
+
+bool XmlControler::newXml( QIODevice *device )
+{
+ //qDebug() << "XmlControler::newXml( QIODevice *device ), Entry";
+
+ const int Indent = 4;
+
+ QTextStream out( device );
+ QDomNode xmlNode = m_DomDoc.createProcessingInstruction( "xml",
+ "version=\"1.0\" encoding=\"UTF-8\"");
+ m_DomDoc.insertBefore( xmlNode, m_DomDoc.firstChild() );
+
+ QDomElement root = m_DomDoc.createElement( STR_XML_GROUPSMS );
+ root.setAttribute( STR_XML_VERSION, STR_XML_VERSION_NUMBER );
+ m_DomDoc.appendChild( root );
+
+ m_DomDoc.save( out, Indent );
+ return true;
+}
+
+bool XmlControler::readXml( const QString &filename )
+{
+ //qDebug() << "XmlControler::readXml( const QString &filename ), Entry";
+
+ m_filename = filename;
+ m_XmlFileOut = new QFile( filename );
+ if( !m_XmlFileOut->open( QFile::ReadOnly | QFile::Text ) )
+ {
+ //qDebug() << "open file is failed:" << m_XmlFileOut->errorString();
+ return false;
+ }
+
+ if( !readXml( m_XmlFileOut ) )
+ {
+ return false;
+ }
+
+ return true;
+}
+
+bool XmlControler::readXml( QIODevice *device )
+{
+ //qDebug() << "XmlControler::readXml( QIODevice *device ), Entry";
+
+ QString errorStr;
+ int errorLine;
+ int errorColumn;
+
+ if( !m_DomDoc.setContent( device, true, &errorStr, &errorLine, &errorColumn ) )
+ {
+ QMessageBox::information( NULL, tr("XML file"),
+ tr("Parse error at line %1, column %2:\n%3")
+ .arg(errorLine)
+ .arg(errorColumn)
+ .arg(errorStr) );
+ return false;
+ }
+
+ return true;
+}
+
+bool XmlControler::writeXml( QIODevice *device )
+{
+ //qDebug() << "XmlControler::writeXml( QIODevice *device ), Entry";
+
+ const int IndentSize = 4;
+
+ QTextStream out(device);
+ m_DomDoc.save( out, IndentSize );
+ return true;
+}
+
+bool XmlControler::writeXml()
+{
+ //qDebug() << "XmlControler::writeXml(), Entry";
+
+ const int IndentSize = 4;
+
+ m_XmlFileIn = new QFile( m_filename );
+ if( !m_XmlFileIn->open( QFile::WriteOnly | QFile::Text ) )
+ {
+ //qDebug() << "open file is failed:" << m_XmlFileIn->errorString();
+ return false;
+ }
+
+ QTextStream out(m_XmlFileIn);
+ m_DomDoc.save( out, IndentSize );
+ m_XmlFileIn->close();
+ delete m_XmlFileIn;
+ m_XmlFileIn = NULL;
+ return true;
+}
+
+bool XmlControler::createAllContacts()
+{
+ //qDebug() << "XmlControler::createAllContacts, Entry";
+
+ QDomElement root = m_DomDoc.documentElement();
+ QDomElement allcontacts = m_DomDoc.createElement( STR_XML_ALLCONTACTS );
+ root.appendChild( allcontacts );
+
+ QDomElement node_contact = m_DomDoc.createElement( STR_XML_CONTACT );
+ allcontacts.appendChild( node_contact );
+ QDomText contact_fullname = m_DomDoc.createTextNode( "Tom for Test" );
+ node_contact.appendChild( contact_fullname );
+ QDomText contact_mobilenumber = m_DomDoc.createTextNode( "number for Test" );
+ node_contact.appendChild( contact_mobilenumber );
+
+ m_DomDoc.appendChild( root );
+
+ return true;
+}
+
+bool XmlControler::createGroup(const QString &groupname)
+{
+ //qDebug() << "XmlControler::createGroup(const QString &groupname), Entry";
+
+ findGroup( groupname );
+ if( foundgroup )
+ return true;
+
+ QDomElement root = m_DomDoc.documentElement();
+ QDomElement group = m_DomDoc.createElement( STR_XML_GROUP );
+ group.setAttribute( STR_XML_GROUP_NAME, groupname );
+
+
+ root.appendChild( group );
+ writeXml();
+
+ Item *item = new Item();
+
+ item->group_name = groupname;
+ item->m_isGroup = true;
+
+ itemObserver->addGroup( item );
+
+ return true;
+}
+
+bool XmlControler::createContact(const QString &group, ItemListPtr contacts)
+{
+ QDomNode root = findGroup(group);
+ if( !foundgroup )
+ {
+ qDebug() << "could not find this" << group << "existed. create it.";
+ createGroup( group );
+ root = findGroup(group);
+ }
+
+ for( int i = 0; i < contacts.size(); i++ )
+ {
+ contacts.at(i)->group_owner = group;
+ createContact( &root, contacts.at(i) );
+ }
+ itemObserver->addContact( contacts, group);
+ return true;
+}
+
+bool XmlControler::createContact(const QString &group, Item *contact)
+{
+ //qDebug() << "XmlControler::createContact(const QString &group, Item *contact), Entry";
+
+ QDomNode root = findGroup(group);
+ if( !foundgroup )
+ {
+ //qDebug() << "could not find this" << group << "existed. create it.";
+ createGroup( group );
+ root = findGroup(group);
+ }
+
+ contact->group_owner = group;
+ createContact( &root, contact );
+
+ itemObserver->addContact( contact, group);
+
+ return true;
+}
+
+bool XmlControler::createContact(QDomNode *group, Item *contact)
+{
+ //qDebug() << "XmlControler::createContact(QDomElement *group, Item *contact), Entry";
+ Q_ASSERT(contact);
+
+ QDomElement node = m_DomDoc.createElement(STR_XML_CONTACT);
+ node.setAttribute( STR_XML_CONTACT_UID, contact->uid);
+ node.setAttribute( STR_XML_CONTACT_FULL_NAME, contact->full_name);
+ node.setAttribute( STR_XML_CONTACT_MOBILE_NUMBER, contact->mobile_number);
+ node.setAttribute( STR_XML_CONTACT_PIC_URI, contact->user_pic_uri);
+ node.setAttribute( STR_XML_CONTACT_GROUP_OWNER, contact->group_owner);
+
+// node.setAttribute( STR_XML_CONTACT_UID, "1");
+// node.setAttribute( STR_XML_CONTACT_FULL_NAME, "Tom 1");
+// node.setAttribute( STR_XML_CONTACT_MOBILE_NUMBER, "12345678901");
+// node.setAttribute( STR_XML_CONTACT_PIC_URI, "/alsdj/a;sdj/pic");
+// node.setAttribute( STR_XML_CONTACT_GROUP_OWNER, "groupowner");
+
+ group->appendChild(node);
+ writeXml();
+
+ return true;
+}
+
+QDomNode XmlControler::findGroup(const QString &groupname)
+{
+ //qDebug() << "XmlControler::findGroup(const QString &group), Entry";
+
+ QDomNode root;
+ QDomNode node;
+ foundgroup = false;
+
+ node = m_DomDoc.firstChild();
+ while( !node.isNull() && !foundgroup )
+ {
+ //qDebug() << "node1 name : " << node.toElement().tagName();
+ //qDebug() << "node1 attr : " << node.toElement().attribute(STR_XML_VERSION);
+ if( node.hasChildNodes() )
+ {
+ QDomNode node2 = node.firstChild();
+ while( !node2.isNull() )
+ {
+ //qDebug() << "node2 name : " << node2.toElement().tagName();
+ //qDebug() << "node2 attr : " << node2.toElement().attribute(STR_XML_GROUP_NAME);
+
+ if( groupname == node2.toElement().attribute(STR_XML_GROUP_NAME) )
+ {
+ //qDebug() << "group is found";
+ root = node2;
+ foundgroup = true;
+ break;
+ }
+
+ node2 = node2.nextSibling();
+ }
+ }
+
+ node = node.nextSibling();
+ }
+ return root;
+}
+
+QDomNode XmlControler::findContact(const QString &groupname, Item *contact)
+{
+ //qDebug() << "XmlControler::findContact(const QString &groupname, ContactWidgetItem *contact), Entry";
+
+ QDomNode element;
+ QDomNode group = findGroup(groupname);
+ if( !foundgroup )
+ return group;
+ element = findContact(group, contact);
+
+ return element;
+}
+
+QDomNode XmlControler::findContact(QDomNode group, Item *contact)
+{
+ //qDebug() << "XmlControler::findContact(QDomNode *group, ContactWidgetItem *contact), Entry";
+
+ QDomNode element;
+ QDomNode node = group;
+ foundcontact = false;
+ while( !node.isNull() && !foundcontact )
+ {
+ if( node.hasChildNodes() )
+ {
+ QDomNode node2 = node.firstChild();
+ while( !node2.isNull() )
+ {
+ //qDebug() << "node2 name : " << node2.toElement().tagName();
+ //qDebug() << "node2 uid : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
+
+ if( contact->uid == node2.toElement().attribute(STR_XML_CONTACT_UID) )
+ //if( "1" == node2.toElement().attribute(STR_XML_CONTACT_UID) ) // for Test
+ {
+ //qDebug() << "contact is found";
+ element = node2;
+ foundcontact = true;
+ break;
+ }
+
+ node2 = node2.nextSibling();
+ }
+ }
+
+ node = node.nextSibling();
+ }
+
+ return element;
+}
+
+bool XmlControler::removeGroup(const QString &groupname)
+{
+ //qDebug() << "XmlControler::removeGroup(const QString &groupname), Entry";
+
+ QDomNode group = findGroup(groupname);
+ if( !foundgroup )
+ return false;
+ QDomElement root = m_DomDoc.documentElement();
+ root.removeChild(group);
+ writeXml();
+
+ return true;
+}
+
+bool XmlControler::removeContact( Item *contact )
+{
+ //qDebug() << "XmlControler::removeContact(const QString &group, ContactWidgetItem *contact), Entry";
+
+ QDomNode root = findGroup( contact->group_owner );
+ if( !foundgroup )
+ {
+ //qDebug() << "root is NULL";
+ return false;
+ }
+ removeContact( root, contact );
+
+ return true;
+}
+
+bool XmlControler::removeContact(const QString &group, Item *contact)
+{
+ //qDebug() << "XmlControler::removeContact(const QString &group, ContactWidgetItem *contact), Entry";
+
+ QDomNode root = findGroup(group);
+ if( !foundgroup )
+ {
+ //qDebug() << "root is NULL";
+ return false;
+ }
+ removeContact( root, contact );
+
+ return true;
+}
+
+bool XmlControler::removeContact(QDomNode group, Item *contact)
+{
+ //qDebug() << "XmlControler::removeContact(QDomNode *group, ContactWidgetItem *contact), Entry";
+
+ QDomNode node = findContact(group, contact);
+ if( node.isNull() )
+ {
+ //qDebug() << "node is NULL";
+ return false;
+ }
+ QDomNode del = group.removeChild(node);
+ if( del.isNull() )
+ {
+ //qDebug() << "del node is NULL";
+ return false;
+ }
+ writeXml();
+ itemObserver->removeContact(contact);
+
+ return true;
+}
+
+bool XmlControler::cleanAllContacts()
+{
+ //TODO : clean all contacts in observer.
+ itemObserver->removeAllContacts();
+
+ return true;
+}
+
+bool XmlControler::cleanAllContactsGroup()
+{
+ for( int i = 0; i < all_contacts_items_group.size(); i++ )
+ {
+ delete all_contacts_items_group.at(i);
+ }
+ all_contacts_items_group.clear();
+
+ return true;
+}
+
+void XmlControler::setItemObserver(ItemObserver *observer)
+{
+ itemObserver = observer;
+}
+
+void XmlControler::setItemSelectObserver( ItemSelectObserver *observer )
+{
+ itemSelectObserver = observer;
+}
+
+void XmlControler::getAllContactItems()
+{
+ //qDebug() << "XmlControler::getAllContactItems(), Entry";
+
+ QDomElement root = m_DomDoc.documentElement();
+ QDomNode node = root.firstChild();
+ QString tagname;
+
+ cleanAllContacts();
+
+ while( !node.isNull() )
+ {
+ tagname = node.toElement().tagName();
+ //qDebug() << "node tagname : " << tagname;
+
+ if( STR_XML_GROUP == tagname )
+ {
+ //qDebug() << "node is : " << STR_XML_GROUP;
+
+ Item *item = new Item();
+
+ item->group_name = node.toElement().attribute(STR_XML_GROUP_NAME);
+ item->m_isGroup = true;
+
+ itemObserver->addGroup( item );
+
+ if( node.hasChildNodes() )
+ {
+ QDomNode node2 = node.firstChild();
+ while( !node2.isNull() )
+ {
+ tagname = node2.toElement().tagName();
+ if( STR_XML_CONTACT == tagname )
+ {
+ //qDebug() << "node is : " << STR_XML_CONTACT;
+
+ Item *item = new Item();
+ item->m_isGroup = false;
+
+ item->full_name = node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
+ //qDebug() << "fullname is : " << node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
+
+ item->mobile_number = node2.toElement().attribute(STR_XML_CONTACT_MOBILE_NUMBER);
+ //qDebug() << "mobile number is : " << node2.toElement().attribute(STR_XML_CONTACT_MOBILE_NUMBER);
+
+ item->group_owner = node2.toElement().attribute(STR_XML_CONTACT_GROUP_OWNER);
+ //qDebug() << "group owner is : " << node2.toElement().attribute(STR_XML_CONTACT_GROUP_OWNER);
+
+ item->uid = node2.toElement().attribute(STR_XML_CONTACT_UID);
+ //qDebug() << "uid is : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
+
+ itemObserver->addContact( item );
+ }
+ node2 = node2.nextSibling();
+ }
+ }
+ }
+ node = node.nextSibling();
+ }
+ //qDebug() << "XmlControler::getAllContactItems(), Exit";
+}
+
+QStringList XmlControler::getAllGroupNames()
+{
+ //qDebug() << "XmlControler::getAllGroupNames(), Entry";
+
+ QDomElement root = m_DomDoc.documentElement();
+ QDomNode node = root.firstChild();
+ QString tagname;
+ QStringList strlist;
+
+ while( !node.isNull() )
+ {
+ tagname = node.toElement().tagName();
+
+ if( STR_XML_GROUP == tagname )
+ {
+ //qDebug() << "group name : " << node.toElement().attribute(STR_XML_GROUP_NAME);
+ strlist.append( node.toElement().attribute(STR_XML_GROUP_NAME) );
+ }
+ node = node.nextSibling();
+ }
+ //qDebug() << "XmlControler::getAllGroupNames(), Exit";
+
+ return strlist;
+}
+
+void XmlControler::getAllContactItemsFromGroup( QString groupname )
+{
+ //qDebug() << "XmlControler::getAllContactItemsFromGroup( QString groupname ), Entry";
+
+ QDomElement root = m_DomDoc.documentElement();
+ QDomNode node = root.firstChild();
+ QString tagname;
+ QString group;
+
+ if( old_groupname == groupname )
+ {
+ itemSelectObserver->getGroupContacts( all_contacts_items_group );
+ return;
+ }
+
+ cleanAllContactsGroup();
+
+ while( !node.isNull() )
+ {
+ tagname = node.toElement().tagName();
+ group = node.toElement().attribute(STR_XML_GROUP_NAME);
+ //qDebug() << "node tagname : " << tagname << "group:" << group;
+
+ if( (STR_XML_GROUP == tagname) && (group == groupname) )
+ {
+ //qDebug() << "node is : " << STR_XML_GROUP;
+
+ Item *item = new Item();
+
+ item->group_name = node.toElement().attribute(STR_XML_GROUP_NAME);
+ item->m_isGroup = true;
+
+ all_contacts_items_group.append( item );
+
+ if( node.hasChildNodes() )
+ {
+ QDomNode node2 = node.firstChild();
+ while( !node2.isNull() )
+ {
+ tagname = node2.toElement().tagName();
+ if( STR_XML_CONTACT == tagname )
+ {
+ //qDebug() << "node is : " << STR_XML_CONTACT;
+
+ Item *item = new Item();
+ //qDebug() << "new ContactWidgetItem";
+
+ item->full_name = node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
+ //qDebug() << "fullname is : " << node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
+
+ item->mobile_number = node2.toElement().attribute(STR_XML_CONTACT_MOBILE_NUMBER);
+ //qDebug() << "mobile number is : " << node2.toElement().attribute(STR_XML_CONTACT_MOBILE_NUMBER);
+
+ item->group_owner = node2.toElement().attribute(STR_XML_CONTACT_GROUP_OWNER);
+ //qDebug() << "group owner is : " << node2.toElement().attribute(STR_XML_CONTACT_GROUP_OWNER);
+
+ item->uid = node2.toElement().attribute(STR_XML_CONTACT_UID);
+ //qDebug() << "uid is : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
+
+ all_contacts_items_group.append( item );
+ }
+ node2 = node2.nextSibling();
+ }
+ }
+ }
+ node = node.nextSibling();
+ }
+ itemSelectObserver->getGroupContacts( all_contacts_items_group );
+ old_groupname = groupname;
+ //qDebug() << "XmlControler::getAllContactItemsFromGroup( QString groupname ), Exit";
+}
+
+bool XmlControler::removeContactFromGroup( ItemListPtr items, const QString &groupname )
+{
+ QDomNode root = findGroup(groupname);
+ if( !foundgroup )
+ {
+ //qDebug() << "root is NULL";
+ return false;
+ }
+ for( int i = 0; items.size(); i++ )
+ {
+ removeContact( root, items.at(i) );
+ }
+ itemObserver->refreshContactsList();
+
+ return true;
+}
+
+bool XmlControler::removeContactFromGroup( ItemListPtr items )
+{
+ for( int i = 0; i < items.size(); i++ )
+ {
+ removeContact( items.at(i) );
+ }
+ itemObserver->refreshContactsList();
+
+ return true;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+