]> pilppa.org Git - vcard-tracker-import.git/blob - src/VCardDirectoryImporter.cpp
initial commit
[vcard-tracker-import.git] / src / VCardDirectoryImporter.cpp
1 /*
2  * Copyright (c) 2011, Mika Laitio. <lamikr@pilppa.org>, Tom Swindell
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 #include "VCardDirectoryImporter.h"
11
12 #include <QDir>
13 #include <QFile>
14 #include <QStringList>
15 #include <QCoreApplication>
16
17 #include <QContactSaveRequest>
18
19 #include <QVersitReader>
20 #include <QVersitContactImporter>
21
22 #include <seasidesyncmodel.h>
23
24 QTM_USE_NAMESPACE
25
26 VCardDirectoryImporter::VCardDirectoryImporter(QObject *parent) : QObject(parent) {
27
28 }
29
30 VCardDirectoryImporter::~VCardDirectoryImporter()
31 {
32 }
33
34 int VCardDirectoryImporter::importVCardsDirectoryContacts(const QString &path)
35 {
36     QDir                    directory(path);
37     QVersitReader           reader;
38     QVersitContactImporter  importer;
39     QList<QVersitDocument>  documents;
40     int                     ret_val;
41
42     ret_val = -1;
43     if (directory.exists() == true) {
44         qDebug() << "Importing v-card contacts from directory " << path;
45         foreach(QString fileName, directory.entryList((QStringList() << "*.vcf"), QDir::Files)) {
46             QFile file(directory.filePath(fileName));
47             file.open(QFile::ReadOnly);
48
49             reader.setDevice(&file);
50             reader.startReading();
51             reader.waitForFinished();
52
53             documents.append(reader.results());
54             qDebug() << "Read " << reader.results().count() << " contacts: " << file.fileName();
55         }
56         importer.importDocuments(documents);
57         qDebug() << "Total count of contacts read: " << importer.contacts().count();
58
59         contactList.append(importer.contacts());
60         ret_val = 0;
61     }
62     else {
63         qWarning() << "Error, V-Card contact directory" << directory.path() << "not found.";
64     }
65     return ret_val;
66 }
67
68 int VCardDirectoryImporter::saveContacts()
69 {
70     int                 ret_val = 0;
71         SeasideSyncModel        *syncModel;
72
73         syncModel = SeasideSyncModel::instance();
74     //syncModel->addContacts(contactList, contactList.count());
75         foreach (const QContact& cnt, contactList) {
76                 syncModel->updatePerson(&cnt);
77         }
78     return ret_val;
79 }