コード例 #1
0
ファイル: udxfile.cpp プロジェクト: DarkHobbit/doublecontact
bool UDXFile::importRecords(const QString &url, ContactList &list, bool append)
{
    if (!openFile(url, QIODevice::ReadOnly))
        return false;
    // Read XML
    QString err_msg;
    int err_line, err_col;
    if (!setContent(&file, &err_msg, &err_line, &err_col)) {
        _errors << QObject::tr("Can't read content from file %1\n%2\nline %3, col %4\n")
            .arg(url).arg(err_msg).arg(err_line).arg(err_col);
        closeFile();
        return false;
    }
    closeFile();
    // Root element
    QDomElement root = documentElement();
    if (root.nodeName()!="DataExchangeInfo") {
        _errors << QObject::tr("Root node is not 'DataExchangeInfo' at file\n%1").arg(url);
        return false;
    }
    // Codepage, version, expected record count
    QDomElement recInfo = root.firstChildElement("RecordInfo");
    if (recInfo.isNull()) {
        _errors << QObject::tr("Can't find 'RecordInfo' tag at file\n%1").arg(url);
        return false;
    }
    QString charSet = recInfo.firstChildElement("Encoding").text();
    if (charSet.isEmpty()) {
        _errors << QObject::tr("Warning: codepage not found, trying use UTF-8...");
        charSet = "UTF-8";
    }
    QString udxVer = recInfo.firstChildElement("UdxVersion").text();
    if (udxVer.isEmpty()) {
        _errors << QObject::tr("Warning: udx version not found, treat as 1.0...");
        udxVer = "1.0";
    }
    QDomElement vcfInfo = recInfo.firstChildElement("RecordOfvCard");
    QString vcVer = vcfInfo.firstChildElement("vCardVersion").text();
    int expCount = vcfInfo.firstChildElement("vCardRecord").text().toInt();
    // vCard set
    QDomElement vCard = root.firstChildElement("vCard");
    if (vCard.isNull()) {
        _errors << QObject::tr("Can't find 'vCard' records at file\n%1").arg(url);
        return false;
    }
    // QTextCodec* codec = QTextCodec::codecForName(charSet.toLocal8Bit()); TODO not works on windows
    ContactItem item;
    if (!append)
        list.clear();
    QDomElement vCardInfo = vCard.firstChildElement("vCardInfo");
    while (!vCardInfo.isNull()) {
        item.clear();
        item.originalFormat = "UDX";
        item.version = udxVer;
        item.subVersion = vcVer;
        item.id = vCardInfo.firstChildElement("Sequence").text();
        QDomElement fields = vCardInfo.firstChildElement("vCardField");
        if (fields.isNull())
            _errors << QObject::tr("Can't find 'vCardField' at sequence %1").arg(item.id);
        QDomElement field = fields.firstChildElement();
        while (!field.isNull()) {
            QString fldName = field.nodeName().toUpper();
            QString fldValue = field.text(); // codec->toUnicode(field.text().toLocal8Bit()); TODO not works on windows
            if (fldName=="N") {
                fldValue.replace("\\;", " ");
                // In ALL known me udx files part before first ; was EMPTY
                fldValue.remove(";");
                item.names = fldValue.split(" ");
                // If empty parts not in-middle, remove it
                item.dropFinalEmptyNames();
            }
            else if (fldName.startsWith("TEL")) {
                Phone phone;
                phone.number = fldValue;
                if (fldName=="TEL")
                    phone.tTypes << "CELL";
                else if (fldName=="TELHOME")
                    phone.tTypes << "HOME";
                else if (fldName=="TELWORK")
                    phone.tTypes << "WORK";
                else if (fldName=="TELFAX")
                    phone.tTypes << "FAX";
                else
                    _errors << QObject::tr("Unknown phone type: %1 (%2)").arg(phone.number).arg(item.names[0]);
                item.phones.push_back(phone);
            }
            else if (fldName=="ORGNAME")
                item.organization = fldValue;
            else if (fldName=="BDAY")
                item.birthday.value = QDateTime::fromString(fldValue, "yyyyMMdd"); // TODO Maybe, use DateItem::fromString
            else if (fldName=="EMAIL") {
                Email email;
                email.address = fldValue;
                email.emTypes << "pref";
                item.emails.push_back(email);
            }
            else
                _errors << QObject::tr("Unknown 'vCardfield' type: %1").arg(fldName);
            field = field.nextSiblingElement();
        }
        item.calculateFields();
        list.push_back(item);
        vCardInfo = vCardInfo.nextSiblingElement();
    }
    if (list.count()!=expCount)
        _errors << QObject::tr("%1 records read, %2 expected").arg(list.count()).arg(expCount);
    // Unknown tags statistics
    int totalUnknownTags = 0;
    foreach (const ContactItem& _item, list)
        totalUnknownTags += _item.unknownTags.count();
    if (totalUnknownTags)
        _errors << QObject::tr("%1 unknown tags found").arg(totalUnknownTags);
    // Ready
    return (!list.isEmpty());
}
コード例 #2
0
void ContactDialog::getData(ContactItem& c)
{
    // Names
    c.fullName = ui->leFullName->text();
    c.names.clear();
    for (int i=0; i<nameCount; i++)
        c.names.push_back(findChild<QLineEdit*>(QString("leName%1").arg(i+1))->text());
    // Phones
    c.phones.clear();
    for (int i=0; i<phoneCount; i++) {
        Phone ph;
        readTriplet("Phone", i+1, ph.number, ph.tTypes, Phone::standardTypes);
        c.phones.push_back(ph);
    }
    // Emails
    c.emails.clear();
    for (int i=0; i<emailCount; i++) {
        Email em;
        readTriplet("Email", i+1, em.address, em.emTypes, Email::standardTypes);
        c.emails.push_back(em);
    }
    // Birthday and anniversaries
    c.birthday = birthdayDetails;
    c.birthday.value = (ui->cbBirthday->isChecked()) ? ui->dteBirthday->dateTime() : QDateTime();
    for (int i=0; i<anniversaryCount; i++) {
        DateItem di;
        readAnniversary(i+1, di);
        c.anniversaries.push_back(di);
    }
    // Photo
    QString photoText = ui->lbPhotoContent->text();
    if ((!photoText.isEmpty()) && photoText!=S_PH_UNKNOWN_FORMAT) { // URL
        c.photoType = "URL";
        c.photoUrl = photoText;
    }
    else if (!photo.isEmpty()) { // image
        c.photoType = detectPhotoFormat();
        c.photo = photo;
    }
    else { // deleted by user
        c.photoType.clear();
        c.photoUrl.clear();
        c.photo.clear();
    }
    // Addresses
    readAddress(ui->gbAddrHome, c.addrHome);
    if (!c.addrHome.isEmpty() && !c.addrHome.paTypes.contains("home", Qt::CaseInsensitive))
        c.addrHome.paTypes << "home";
    readAddress(ui->gbAddrWork, c.addrWork);
    if (!c.addrWork.isEmpty() && !c.addrWork.paTypes.contains("work", Qt::CaseInsensitive))
        c.addrWork.paTypes << "work";
    // Internet
    c.nickName = ui->leNickName->text();
    c.url = ui->leURL->text();
    c.jabberName = ui->leJabber->text();
    c.icqName = ui->leICQ->text();
    c.skypeName = ui->leSkype->text();
    // Work
    c.organization = ui->leOrganization->text();
    c.title = ui->leTitle->text();
    // Other
    c.sortString = ui->leSortString->text();
    c.description = ui->edDescription->toPlainText();
    c.calculateFields();
}