first release
[groupsms] / sms / xmlcontroler.cpp
1 #include <QtGui>
2 #include <QXmlStreamWriter>
3
4 #include "xmlcontroler.h"
5 #include "xmlstring.h"
6 #include "groupwidgetitem.h"
7
8 XmlControler* XmlControler::instance = 0;
9
10 XmlControler* XmlControler::getInstance()
11 {
12     if( !instance )
13     {
14         instance = new XmlControler();
15     }
16     return instance;
17 }
18
19 XmlControler::XmlControler(QObject *parent) :
20         QObject(parent)
21 {
22     instance = this;
23 }
24
25 XmlControler::~XmlControler()
26 {
27     closeXmlFile();
28 }
29
30 void XmlControler::closeXmlFile()
31 {
32     if( m_XmlFileOut->isOpen() )
33     {
34         m_XmlFileOut->close();
35         delete m_XmlFileOut;
36         m_XmlFileOut = NULL;
37     }
38
39     if( m_XmlFileIn->isOpen() )
40     {
41         writeXml( m_XmlFileIn );
42         m_XmlFileIn->close();
43         delete m_XmlFileIn;
44         m_XmlFileIn = NULL;
45     }
46 }
47
48 bool XmlControler::newXml( const QString &filename )
49 {
50     //qDebug() << "XmlControler::newXml( const QString &filename ), Entry";
51
52     m_filename = filename;
53     m_XmlFileOut = new QFile( filename );
54     if( !m_XmlFileOut->open( QFile::ReadWrite | QFile::Text ) )
55     {
56         //qDebug() << "open file is failed:" << m_XmlFileOut->errorString();
57         return false;
58     }
59
60     newXml( m_XmlFileOut );
61     m_XmlFileOut->close();
62     delete m_XmlFileOut;
63     m_XmlFileOut = NULL;
64
65     return true;
66 }
67
68 bool XmlControler::newXml( QIODevice *device )
69 {
70     //qDebug() << "XmlControler::newXml( QIODevice *device ), Entry";
71
72     const int Indent = 4;
73
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() );
78
79     QDomElement root = m_DomDoc.createElement( STR_XML_GROUPSMS );
80     root.setAttribute( STR_XML_VERSION, STR_XML_VERSION_NUMBER );
81     m_DomDoc.appendChild( root );
82
83     m_DomDoc.save( out, Indent );
84     return true;
85 }
86
87 bool XmlControler::readXml( const QString &filename )
88 {
89     //qDebug() << "XmlControler::readXml( const QString &filename ), Entry";
90
91     m_filename = filename;
92     m_XmlFileOut = new QFile( filename );
93     if( !m_XmlFileOut->open( QFile::ReadOnly | QFile::Text ) )
94     {
95         //qDebug() << "open file is failed:" << m_XmlFileOut->errorString();
96         return false;
97     }
98
99     if( !readXml( m_XmlFileOut ) )
100     {
101         return false;
102     }
103
104     return true;
105 }
106
107 bool XmlControler::readXml( QIODevice *device )
108 {
109     //qDebug() << "XmlControler::readXml( QIODevice *device ), Entry";
110
111     QString errorStr;
112     int errorLine;
113     int errorColumn;
114
115     if( !m_DomDoc.setContent( device, true, &errorStr, &errorLine, &errorColumn ) )
116     {
117         QMessageBox::information( NULL, tr("XML file"),
118                                   tr("Parse error at line %1, column %2:\n%3")
119                                   .arg(errorLine)
120                                   .arg(errorColumn)
121                                   .arg(errorStr) );
122         return false;
123     }
124
125     return true;
126 }
127
128 bool XmlControler::writeXml( QIODevice *device )
129 {
130     //qDebug() << "XmlControler::writeXml( QIODevice *device ), Entry";
131
132     const int IndentSize = 4;
133
134     QTextStream out(device);
135     m_DomDoc.save( out, IndentSize );
136     return true;
137 }
138
139 bool XmlControler::writeXml()
140 {
141     //qDebug() << "XmlControler::writeXml(), Entry";
142
143     const int IndentSize = 4;
144
145     m_XmlFileIn = new QFile( m_filename );
146     if( !m_XmlFileIn->open( QFile::WriteOnly | QFile::Text ) )
147     {
148         //qDebug() << "open file is failed:" << m_XmlFileIn->errorString();
149         return false;
150     }
151
152     QTextStream out(m_XmlFileIn);
153     m_DomDoc.save( out, IndentSize );
154     m_XmlFileIn->close();
155     delete m_XmlFileIn;
156     m_XmlFileIn = NULL;
157     return true;
158 }
159
160 bool XmlControler::createAllContacts()
161 {
162     //qDebug() << "XmlControler::createAllContacts, Entry";
163
164     QDomElement root = m_DomDoc.documentElement();
165     QDomElement allcontacts = m_DomDoc.createElement( STR_XML_ALLCONTACTS );
166     root.appendChild( allcontacts );
167
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 );
174
175     m_DomDoc.appendChild( root );
176
177     return true;
178 }
179
180 bool XmlControler::createGroup(const QString &groupname)
181 {
182     //qDebug() << "XmlControler::createGroup(const QString &groupname), Entry";
183
184     findGroup( groupname );
185     if( foundgroup )
186         return true;
187
188     QDomElement root = m_DomDoc.documentElement();
189     QDomElement group = m_DomDoc.createElement( STR_XML_GROUP );
190     group.setAttribute( STR_XML_GROUP_NAME, groupname );
191
192
193     root.appendChild( group );
194     writeXml();
195
196     Item *item = new Item();
197
198     item->group_name = groupname;
199     item->m_isGroup = true;
200
201     itemObserver->addGroup( item );
202
203     return true;
204 }
205
206 bool XmlControler::createContact(const QString &group, ItemListPtr contacts)
207 {
208     QDomNode root = findGroup(group);
209     if( !foundgroup )
210     {
211         qDebug() << "could not find this" << group << "existed. create it.";
212         createGroup( group );
213         root = findGroup(group);
214     }
215
216     for( int i = 0; i < contacts.size(); i++ )
217     {
218         contacts.at(i)->group_owner = group;
219         createContact( &root, contacts.at(i) );
220     }
221     itemObserver->addContact( contacts, group);
222     return true;
223 }
224
225 bool XmlControler::createContact(const QString &group, Item *contact)
226 {
227     //qDebug() << "XmlControler::createContact(const QString &group, Item *contact), Entry";
228
229     QDomNode root = findGroup(group);
230     if( !foundgroup )
231     {
232         //qDebug() << "could not find this" << group << "existed. create it.";
233         createGroup( group );
234         root = findGroup(group);
235     }
236
237     contact->group_owner = group;
238     createContact( &root, contact );
239
240     itemObserver->addContact( contact, group);
241
242     return true;
243 }
244
245 bool XmlControler::createContact(QDomNode *group, Item *contact)
246 {
247     //qDebug() << "XmlControler::createContact(QDomElement *group, Item *contact), Entry";
248     Q_ASSERT(contact);
249
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);
256
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");
262
263     group->appendChild(node);
264     writeXml();
265
266     return true;
267 }
268
269 QDomNode XmlControler::findGroup(const QString &groupname)
270 {
271     //qDebug() << "XmlControler::findGroup(const QString &group), Entry";
272
273     QDomNode root;
274     QDomNode node;
275     foundgroup = false;
276
277     node = m_DomDoc.firstChild();
278     while( !node.isNull() && !foundgroup )
279     {
280         //qDebug() << "node1 name : " << node.toElement().tagName();
281         //qDebug() << "node1 attr : " << node.toElement().attribute(STR_XML_VERSION);
282         if( node.hasChildNodes() )
283         {
284             QDomNode node2 = node.firstChild();
285             while( !node2.isNull() )
286             {
287                 //qDebug() << "node2 name : " << node2.toElement().tagName();
288                 //qDebug() << "node2 attr : " << node2.toElement().attribute(STR_XML_GROUP_NAME);
289
290                 if( groupname == node2.toElement().attribute(STR_XML_GROUP_NAME) )
291                 {
292                     //qDebug() << "group is found";
293                     root = node2;
294                     foundgroup = true;
295                     break;
296                 }
297
298                 node2 = node2.nextSibling();
299             }
300         }
301
302         node = node.nextSibling();
303     }
304     return root;
305 }
306
307 QDomNode XmlControler::findContact(const QString &groupname, Item *contact)
308 {
309     //qDebug() << "XmlControler::findContact(const QString &groupname, ContactWidgetItem *contact), Entry";
310
311     QDomNode element;
312     QDomNode group = findGroup(groupname);
313     if( !foundgroup )
314         return group;
315     element = findContact(group, contact);
316
317     return element;
318 }
319
320 QDomNode XmlControler::findContact(QDomNode group, Item *contact)
321 {
322     //qDebug() << "XmlControler::findContact(QDomNode *group, ContactWidgetItem *contact), Entry";
323
324     QDomNode element;
325     QDomNode node = group;
326     foundcontact = false;
327     while( !node.isNull() && !foundcontact )
328     {
329         if( node.hasChildNodes() )
330         {
331             QDomNode node2 = node.firstChild();
332             while( !node2.isNull() )
333             {
334                 //qDebug() << "node2 name : " << node2.toElement().tagName();
335                 //qDebug() << "node2 uid : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
336
337                 if( contact->uid == node2.toElement().attribute(STR_XML_CONTACT_UID) )
338                 //if( "1" == node2.toElement().attribute(STR_XML_CONTACT_UID) ) // for Test
339                 {
340                     //qDebug() << "contact is found";
341                     element = node2;
342                     foundcontact = true;
343                     break;
344                 }
345
346                 node2 = node2.nextSibling();
347             }
348         }
349
350         node = node.nextSibling();
351     }
352
353     return element;
354 }
355
356 bool XmlControler::removeGroup(const QString &groupname)
357 {
358     //qDebug() << "XmlControler::removeGroup(const QString &groupname), Entry";
359
360     QDomNode group = findGroup(groupname);
361     if( !foundgroup )
362         return false;
363     QDomElement root = m_DomDoc.documentElement();
364     root.removeChild(group);
365     writeXml();
366
367     return true;
368 }
369
370 bool XmlControler::removeContact( Item *contact )
371 {
372     //qDebug() << "XmlControler::removeContact(const QString &group, ContactWidgetItem *contact), Entry";
373
374     QDomNode root = findGroup( contact->group_owner );
375     if( !foundgroup )
376     {
377         //qDebug() << "root is NULL";
378         return false;
379     }
380     removeContact( root, contact );
381
382     return true;
383 }
384
385 bool XmlControler::removeContact(const QString &group, Item *contact)
386 {
387     //qDebug() << "XmlControler::removeContact(const QString &group, ContactWidgetItem *contact), Entry";
388
389     QDomNode root = findGroup(group);
390     if( !foundgroup )
391     {
392         //qDebug() << "root is NULL";
393         return false;
394     }
395     removeContact( root, contact );
396
397     return true;
398 }
399
400 bool XmlControler::removeContact(QDomNode group, Item *contact)
401 {
402     //qDebug() << "XmlControler::removeContact(QDomNode *group, ContactWidgetItem *contact), Entry";
403
404     QDomNode node = findContact(group, contact);
405     if( node.isNull() )
406     {
407         //qDebug() << "node is NULL";
408         return false;
409     }
410     QDomNode del = group.removeChild(node);
411     if( del.isNull() )
412     {
413         //qDebug() << "del node is NULL";
414         return false;
415     }
416     writeXml();
417     itemObserver->removeContact(contact);
418
419     return true;
420 }
421
422 bool XmlControler::cleanAllContacts()
423 {
424     //TODO : clean all contacts in observer.
425     itemObserver->removeAllContacts();
426
427     return true;
428 }
429
430 bool XmlControler::cleanAllContactsGroup()
431 {
432     for( int i = 0; i < all_contacts_items_group.size(); i++ )
433     {
434         delete all_contacts_items_group.at(i);
435     }
436     all_contacts_items_group.clear();
437
438     return true;
439 }
440
441 void XmlControler::setItemObserver(ItemObserver *observer)
442 {
443     itemObserver = observer;
444 }
445
446 void XmlControler::setItemSelectObserver( ItemSelectObserver *observer )
447 {
448     itemSelectObserver = observer;
449 }
450
451 void XmlControler::getAllContactItems()
452 {
453     //qDebug() << "XmlControler::getAllContactItems(), Entry";
454
455     QDomElement root = m_DomDoc.documentElement();
456     QDomNode node = root.firstChild();
457     QString tagname;
458
459     cleanAllContacts();
460
461     while( !node.isNull() )
462     {
463         tagname = node.toElement().tagName();
464         //qDebug() << "node tagname : " << tagname;
465
466         if( STR_XML_GROUP == tagname )
467         {
468             //qDebug() << "node is : " << STR_XML_GROUP;
469
470             Item *item = new Item();
471
472             item->group_name = node.toElement().attribute(STR_XML_GROUP_NAME);
473             item->m_isGroup = true;
474
475             itemObserver->addGroup( item );
476
477             if( node.hasChildNodes() )
478             {
479                 QDomNode node2 = node.firstChild();
480                 while( !node2.isNull() )
481                 {
482                     tagname = node2.toElement().tagName();
483                     if( STR_XML_CONTACT == tagname )
484                     {
485                         //qDebug() << "node is : " << STR_XML_CONTACT;
486
487                         Item *item = new Item();
488                         item->m_isGroup = false;
489
490                         item->full_name = node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
491                         //qDebug() << "fullname is : " << node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
492
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);
495
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);
498
499                         item->uid = node2.toElement().attribute(STR_XML_CONTACT_UID);
500                         //qDebug() << "uid is : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
501
502                         itemObserver->addContact( item );
503                     }
504                     node2 = node2.nextSibling();
505                 }
506             }
507         }
508         node = node.nextSibling();
509     }
510     //qDebug() << "XmlControler::getAllContactItems(), Exit";
511 }
512
513 QStringList XmlControler::getAllGroupNames()
514 {
515     //qDebug() << "XmlControler::getAllGroupNames(), Entry";
516
517     QDomElement root = m_DomDoc.documentElement();
518     QDomNode node = root.firstChild();
519     QString tagname;
520     QStringList strlist;
521
522     while( !node.isNull() )
523     {
524         tagname = node.toElement().tagName();
525
526         if( STR_XML_GROUP == tagname )
527         {
528             //qDebug() << "group name : " << node.toElement().attribute(STR_XML_GROUP_NAME);
529             strlist.append( node.toElement().attribute(STR_XML_GROUP_NAME) );
530         }
531         node = node.nextSibling();
532     }
533     //qDebug() << "XmlControler::getAllGroupNames(), Exit";
534
535     return strlist;
536 }
537
538 void XmlControler::getAllContactItemsFromGroup( QString groupname )
539 {
540     //qDebug() << "XmlControler::getAllContactItemsFromGroup( QString groupname ), Entry";
541
542     QDomElement root = m_DomDoc.documentElement();
543     QDomNode node = root.firstChild();
544     QString tagname;
545     QString group;
546
547     if( old_groupname == groupname )
548     {
549         itemSelectObserver->getGroupContacts( all_contacts_items_group );
550         return;
551     }
552
553     cleanAllContactsGroup();
554
555     while( !node.isNull() )
556     {
557         tagname = node.toElement().tagName();
558         group = node.toElement().attribute(STR_XML_GROUP_NAME);
559         //qDebug() << "node tagname : " << tagname << "group:" << group;
560
561         if( (STR_XML_GROUP == tagname) && (group == groupname) )
562         {
563             //qDebug() << "node is : " << STR_XML_GROUP;
564
565             Item *item = new Item();
566
567             item->group_name = node.toElement().attribute(STR_XML_GROUP_NAME);
568             item->m_isGroup = true;
569
570             all_contacts_items_group.append( item );
571
572             if( node.hasChildNodes() )
573             {
574                 QDomNode node2 = node.firstChild();
575                 while( !node2.isNull() )
576                 {
577                     tagname = node2.toElement().tagName();
578                     if( STR_XML_CONTACT == tagname )
579                     {
580                         //qDebug() << "node is : " << STR_XML_CONTACT;
581
582                         Item *item = new Item();
583                         //qDebug() << "new ContactWidgetItem";
584
585                         item->full_name = node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
586                         //qDebug() << "fullname is : " << node2.toElement().attribute(STR_XML_CONTACT_FULL_NAME);
587
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);
590
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);
593
594                         item->uid = node2.toElement().attribute(STR_XML_CONTACT_UID);
595                         //qDebug() << "uid is : " << node2.toElement().attribute(STR_XML_CONTACT_UID);
596
597                         all_contacts_items_group.append( item );
598                     }
599                     node2 = node2.nextSibling();
600                 }
601             }
602         }
603         node = node.nextSibling();
604     }
605     itemSelectObserver->getGroupContacts( all_contacts_items_group );
606     old_groupname = groupname;
607     //qDebug() << "XmlControler::getAllContactItemsFromGroup( QString groupname ), Exit";
608 }
609
610 bool XmlControler::removeContactFromGroup( ItemListPtr items, const QString &groupname )
611 {
612     QDomNode root = findGroup(groupname);
613     if( !foundgroup )
614     {
615         //qDebug() << "root is NULL";
616         return false;
617     }
618     for( int i = 0; items.size(); i++ )
619     {
620         removeContact( root, items.at(i) );
621     }
622     itemObserver->refreshContactsList();
623
624     return true;
625 }
626
627 bool XmlControler::removeContactFromGroup( ItemListPtr items )
628 {
629     for( int i = 0; i < items.size(); i++ )
630     {
631         removeContact( items.at(i) );
632     }
633     itemObserver->refreshContactsList();
634
635     return true;
636 }
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656