~ubuntu-branches/ubuntu/quantal/qtmobility/quantal

« back to all changes in this revision

Viewing changes to examples/samplephonebook/contactlistpage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-11-16 16:18:07 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20101116161807-k2dzt2nyse975r3l
Tags: 1.1.0-0ubuntu1
* New upstream release
* Syncronise with Debian, no remaining changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include "qversitcontactimporter.h"
45
45
#include "qversitwriter.h"
46
46
#include "qversitcontactexporter.h"
 
47
#include <QDesktopServices>
47
48
#endif
48
49
 
49
50
#include <QtGui>
337
338
        qWarning() << "No manager selected; cannot import";
338
339
        return;
339
340
    }
 
341
    QString docPath = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
 
342
    if (docPath.isEmpty())
 
343
        docPath = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
 
344
    if (docPath.isEmpty())
 
345
        docPath = ".";
340
346
    QString fileName = QFileDialog::getOpenFileName(this,
341
 
       tr("Select vCard file"), ".", tr("vCard files (*.vcf)"));
342
 
    QFile file(fileName);
343
 
    file.open(QIODevice::ReadOnly);
344
 
    if (file.isReadable()) {
345
 
        QVersitReader reader;
346
 
        reader.setDevice(&file);
347
 
        if (reader.startReading() && reader.waitForFinished()) {
348
 
            QVersitContactImporter importer;
349
 
            if (importer.importDocuments(reader.results())) {
350
 
                QList<QContact> contacts = importer.contacts();
351
 
                QMap<int, QContactManager::Error> errorMap;
352
 
                QList<QContact>::iterator it = contacts.begin();
353
 
                while (it != contacts.end()) {
354
 
                    *it = m_manager->compatibleContact(*it);
355
 
                    it++;
 
347
       tr("Select vCard file"), docPath, tr("vCard files (*.vcf)"));
 
348
 
 
349
    if (!fileName.isEmpty()) {
 
350
        QFile file(fileName);
 
351
        file.open(QIODevice::ReadOnly);
 
352
        bool success = false;
 
353
        if (file.isReadable()) {
 
354
            QVersitReader reader;
 
355
            reader.setDevice(&file);
 
356
            if (reader.startReading() && reader.waitForFinished()) {
 
357
                QVersitContactImporter importer;
 
358
                if (importer.importDocuments(reader.results())) {
 
359
                    QList<QContact> contacts = importer.contacts();
 
360
                    qDebug() << "Contacts imported: " << contacts;
 
361
                    QMap<int, QContactManager::Error> errorMap;
 
362
                    QList<QContact>::iterator it = contacts.begin();
 
363
                    while (it != contacts.end()) {
 
364
                        *it = m_manager->compatibleContact(*it);
 
365
                        it++;
 
366
                    }
 
367
                    if (contacts.count() > 0)
 
368
                        success = m_manager->saveContacts(&contacts, &errorMap);
 
369
                    //rebuildList(m_currentFilter);
356
370
                }
357
 
                m_manager->saveContacts(&contacts, &errorMap);
358
 
                //rebuildList(m_currentFilter);
359
371
            }
360
372
        }
 
373
 
 
374
        // We could be more specific, probably
 
375
        if (!success)
 
376
            QMessageBox::warning(this, tr("Error importing vCard"), tr("Unable to import vCard file.  Check that the file exists and is valid."));
361
377
    }
362
378
#endif
363
379
}
369
385
        qWarning() << "No manager selected; cannot export";
370
386
        return;
371
387
    }
 
388
    QString docPath = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
 
389
    if (docPath.isEmpty())
 
390
        docPath = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
 
391
    if (docPath.isEmpty())
 
392
        docPath = ".";
 
393
    docPath.append("/contacts.vcf");
372
394
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save vCard"),
373
 
                                                    "./contacts.vcf",
 
395
                                                    docPath,
374
396
                                                    tr("vCards (*.vcf)"));
375
 
    QFile file(fileName);
376
 
    file.open(QIODevice::WriteOnly);
377
 
    if (file.isWritable()) {
378
 
        QVersitContactExporter exporter;
379
 
        if(exporter.exportContacts(m_contacts, QVersitDocument::VCard30Type)) {
380
 
            QList<QVersitDocument> documents = exporter.documents();
381
 
            QVersitWriter writer;
382
 
            writer.setDevice(&file);
383
 
            writer.startWriting(documents);
384
 
            writer.waitForFinished();
 
397
 
 
398
    if (!fileName.isEmpty()) {
 
399
        bool success = false;
 
400
        QFile file(fileName);
 
401
        file.open(QIODevice::WriteOnly);
 
402
        if (file.isWritable()) {
 
403
            QVersitContactExporter exporter;
 
404
            if(exporter.exportContacts(m_contacts, QVersitDocument::VCard30Type)) {
 
405
                QList<QVersitDocument> documents = exporter.documents();
 
406
                QVersitWriter writer;
 
407
                writer.setDevice(&file);
 
408
                writer.startWriting(documents);
 
409
                writer.waitForFinished();
 
410
                success = (writer.error() == QVersitWriter::NoError);
 
411
            }
385
412
        }
 
413
 
 
414
        if (!success)
 
415
            QMessageBox::warning(this, tr("Error saving vCard"), tr("Unable to save vCard file.  Check that the path specified is writable."));
386
416
    }
387
417
#endif
388
418
}