Changeset 4 for libQtGdata/trunk/qtgdata_contacts.cpp
- Timestamp:
- 02/05/10 00:24:31 (2 years ago)
- File:
-
- 1 edited
-
libQtGdata/trunk/qtgdata_contacts.cpp (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libQtGdata/trunk/qtgdata_contacts.cpp
r3 r4 8 8 QtGdata(username, password, "cp") 9 9 { 10 11 10 } 12 11 12 /** 13 * Retrieve all contacts and parse them 14 */ 13 15 void QtGdata_contacts::getAllContacts() 14 16 { … … 29 31 } 30 32 31 /* 33 /** 32 34 * Parse an XML file with contact info 33 35 */ … … 37 39 38 40 if (!document.setContent(xml)) { 39 qDebug() << "O_NO!"; 41 qDebug() << "Could not parse xml"; 42 return; 40 43 } 41 44 … … 43 46 44 47 if (root.tagName() != "feed") { 45 qDebug() << "other root tag"; 46 } 47 48 QDomElement n; 49 QDomNodeList nl; 50 51 /* Reset */ 48 qDebug() << "invalid root tag"; 49 return; 50 } 51 52 /* Reset */ 52 53 next = QString(); 53 54 54 55 /* Parse children */ 55 nl = root.childNodes(); 56 57 for (int i = 0; i < nl.size(); i++) { 58 n = nl.at(i).toElement(); 56 QDomNodeList children = root.childNodes(); 57 QDomElement child; 58 59 /* Loop over children to parse "general" data */ 60 for (int i = 0; i < children.size(); i++) { 61 child = children.at(i).toElement(); 59 62 60 63 /* Parese update element */ 61 if ( n.tagName() == "updated") {62 QDateTime t = QDateTime::fromString( n.text());63 64 /* Check if we need to upda etthe update variable */64 if (child.tagName() == "updated") { 65 QDateTime t = QDateTime::fromString(child.text()); 66 67 /* Check if we need to update the update variable */ 65 68 if (!update.isValid() || t > update) { 66 69 update = t; … … 68 71 } 69 72 /* Parse link elements */ 70 else if ( n.tagName() == "link") {71 QString attribute = n.attribute("rel");73 else if (child.tagName() == "link") { 74 QString attribute = child.attribute("rel"); 72 75 if (attribute == "next") { 73 next = QUrl::fromEncoded( n.attribute("href").toAscii()).toString();76 next = QUrl::fromEncoded(child.attribute("href").toAscii()).toString(); 74 77 } 75 78 } … … 77 80 78 81 /* Parse actual contacts */ 79 nl= root.elementsByTagName("entry");80 for (int i = 0; i < nl.size(); i++) {82 QDomNodeList entries = root.elementsByTagName("entry"); 83 for (int i = 0; i < entries.size(); i++) { 81 84 QtGdata_contact c; 82 85 83 n = nl.at(i).toElement(); 84 QDomNodeList nl2 = n.childNodes(); 85 86 for (int j = 0; j < nl2.size(); j++) { 87 n = nl2.at(j).toElement(); 88 86 QDomNodeList entry = entries.at(i).toElement().childNodes(); 87 88 /* Parse all children of a contact */ 89 for (int j = 0; j < entry.size(); j++) { 90 QDomElement element = entry.at(j).toElement(); 91 92 /* Address */ 93 if (element.tagName() == "gd:structuredPostalAddress") { 94 QDomNodeList tmp_nl = element.childNodes(); 95 96 /** @todo this can be done way more elegant */ 97 /** @todo Get type of address */ 98 QString street; 99 QString postcode; 100 QString city; 101 QString region; 102 QString country; 103 104 for (int k = 0; k < tmp_nl.size(); k++) { 105 QDomElement tmp_e = tmp_nl.at(i).toElement(); 106 107 if (tmp_e.tagName() == "gd:formattedAddress") { 108 /* Do we need this one? */ 109 } else if (tmp_e.tagName() == "gd:street") { 110 street = tmp_e.text(); 111 } else if (tmp_e.tagName() == "gd:postcode") { 112 postcode = tmp_e.text(); 113 } else if (tmp_e.tagName() == "gd:city") { 114 city = tmp_e.text(); 115 } else if (tmp_e.tagName() == "gd:region") { 116 region = tmp_e.text(); 117 } else if (tmp_e.tagName() == "gd:country") { 118 country = tmp_e.text(); 119 } 120 } 121 122 c.setAddress(street, postcode, city, region, country); 123 } 89 124 /* Birthday */ 90 if (n.tagName() == "gContact:birthday") {91 QString birthday = n.attribute("when");125 else if (element.tagName() == "gContact:birthday") { 126 QString birthday = element.attribute("when"); 92 127 93 128 /* TODO parse birthday … … 96 131 } 97 132 /* Email */ 98 else if ( n.tagName() == "gd:email") {99 QString email = n.attribute("address");100 QString rel = n.attribute("rel");101 bool primary = ( n.hasAttribute("primary") && n.attribute("primary") == "yes");102 103 QtGdata_contact:: GDATA_CONTACT_EMAIL type = QtGdata_contact::GDATA_CONTACT_EMAIL_OTHER;133 else if (element.tagName() == "gd:email") { 134 QString email = element.attribute("address"); 135 QString rel = element.attribute("rel"); 136 bool primary = (element.hasAttribute("primary") && element.attribute("primary") == "yes"); 137 138 QtGdata_contact::EMAIL type = QtGdata_contact::EMAIL_OTHER; 104 139 if (rel == "http://schemas.google.com/g/2005#home") { 105 type = QtGdata_contact:: GDATA_CONTACT_EMAIL_HOME;140 type = QtGdata_contact::EMAIL_HOME; 106 141 } else if (rel == "http://schemas.google.com/g/2005#work") { 107 type = QtGdata_contact:: GDATA_CONTACT_EMAIL_WORK;142 type = QtGdata_contact::EMAIL_WORK; 108 143 } 109 144 110 145 c.addEmail(email, type, primary); 111 146 } 147 /* Link */ 148 else if(element.tagName() == "link") { 149 QString rel = element.attribute("rel"); 150 151 if (rel == "self") { 152 c.setLinkSelf(element.text()); 153 } else if (rel == "edit") { 154 c.setLinkEdit(element.text()); 155 } 156 } 157 /* Parse name(s) */ 158 else if (element.tagName() == "gd:name") { 159 QDomNodeList tmp_nl = element.childNodes(); 160 161 for (int k = 0; k < tmp_nl.size(); k++) { 162 QDomElement tmp_e = tmp_nl.at(i).toElement(); 163 if (tmp_e.tagName() == "gd:fullName") { 164 c.setFullName(tmp_e.text()); 165 } else if (tmp_e.tagName() == "gd:givenName") { 166 c.setGivenName(tmp_e.text()); 167 } else if (tmp_e.tagName() == "gd:familyName") { 168 c.setFamilyName(tmp_e.text()); 169 } 170 /** @todo Other name stuff */ 171 } 172 } 112 173 /* Phone number */ 113 else if( n.tagName() == "gd:phoneNumber") {114 QString ref = n.attribute("rel");115 QString number = n.text();116 117 QtGdata_contact:: GDATA_CONTACT_PHONE type = QtGdata_contact::GDATA_CONTACT_PHONE_OTHER;174 else if(element.tagName() == "gd:phoneNumber") { 175 QString ref = element.attribute("rel"); 176 QString number = element.text(); 177 178 QtGdata_contact::PHONE type = QtGdata_contact::PHONE_OTHER; 118 179 if (ref == "http://schemas.google.com/g/2005#mobile") { 119 type = QtGdata_contact:: GDATA_CONTACT_PHONE_MOBILE;180 type = QtGdata_contact::PHONE_MOBILE; 120 181 } 121 182 /* Add other */ … … 123 184 c.addPhone(number, type); 124 185 } 186 /* Title */ 187 else if (element.tagName() == "title") { 188 c.setTitle(element.text()); 189 } 125 190 /* Update */ 126 else if ( n.tagName() == "updated") {127 QDateTime updated = QDateTime::fromString( n.text(), "yyyy-M-ddThh:mm:ss.zzzZ");191 else if (element.tagName() == "updated") { 192 QDateTime updated = QDateTime::fromString(element.text(), "yyyy-M-ddThh:mm:ss.zzzZ"); 128 193 updated.setTimeSpec(Qt::UTC); 129 194 c.setUpdated(updated);
Note: See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)