Ignore:
Timestamp:
02/05/10 00:24:31 (2 years ago)
Author:
roeland
Message:

Parsing some more contact data.
Added doxygen comments. A lot of cleaning up or design choises are
needed. But lets first get a working prototype.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libQtGdata/trunk/qtgdata_contacts.cpp

    r3 r4  
    88QtGdata(username, password, "cp") 
    99{ 
    10  
    1110} 
    1211 
     12/** 
     13 * Retrieve all contacts and parse them 
     14 */ 
    1315void QtGdata_contacts::getAllContacts() 
    1416{ 
     
    2931} 
    3032 
    31 /* 
     33/** 
    3234 * Parse an XML file with contact info 
    3335 */ 
     
    3739 
    3840  if (!document.setContent(xml)) { 
    39     qDebug() << "O_NO!"; 
     41    qDebug() << "Could not parse xml"; 
     42    return; 
    4043  } 
    4144 
     
    4346 
    4447  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 */ 
    5253  next = QString(); 
    5354 
    5455  /* 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(); 
    5962 
    6063    /* Parese update element */ 
    61     if (n.tagName() == "updated") { 
    62       QDateTime t = QDateTime::fromString(n.text()); 
    63  
    64       /* Check if we need to updaet the 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 */ 
    6568      if (!update.isValid() || t > update) { 
    6669        update = t; 
     
    6871    } 
    6972    /* 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"); 
    7275      if (attribute == "next") { 
    73         next = QUrl::fromEncoded(n.attribute("href").toAscii()).toString(); 
     76        next = QUrl::fromEncoded(child.attribute("href").toAscii()).toString(); 
    7477      } 
    7578    } 
     
    7780 
    7881  /* 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++) { 
    8184    QtGdata_contact c; 
    8285 
    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      } 
    89124      /* 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"); 
    92127 
    93128        /* TODO parse birthday 
     
    96131      } 
    97132      /* 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; 
    104139        if (rel == "http://schemas.google.com/g/2005#home") { 
    105           type = QtGdata_contact::GDATA_CONTACT_EMAIL_HOME; 
     140          type = QtGdata_contact::EMAIL_HOME; 
    106141        } else if (rel == "http://schemas.google.com/g/2005#work") { 
    107           type = QtGdata_contact::GDATA_CONTACT_EMAIL_WORK; 
     142          type = QtGdata_contact::EMAIL_WORK; 
    108143        } 
    109144 
    110145        c.addEmail(email, type, primary); 
    111146      } 
     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      } 
    112173      /* 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; 
    118179        if (ref == "http://schemas.google.com/g/2005#mobile") { 
    119           type = QtGdata_contact::GDATA_CONTACT_PHONE_MOBILE; 
     180          type = QtGdata_contact::PHONE_MOBILE; 
    120181        } 
    121182        /* Add other */ 
     
    123184        c.addPhone(number, type); 
    124185      } 
     186      /* Title */ 
     187      else if (element.tagName() == "title") { 
     188        c.setTitle(element.text()); 
     189      } 
    125190      /* 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"); 
    128193        updated.setTimeSpec(Qt::UTC); 
    129194        c.setUpdated(updated); 
Note: See TracChangeset for help on using the changeset viewer.