summaryrefslogtreecommitdiffstats
path: root/kdeaccounts-plugin/kdeaccountsformat.cpp
blob: d445e31185be6a78c1a4500b2cc97e2b75aa4dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "kdeaccountsformat.h"

#include <qcstring.h>
#include <qfile.h>

#include <kabc/addressbook.h>
#include <kabc/addressee.h>
#include <kabc/resourcefile.h>

extern "C"
{
  KDE_EXPORT KABC::FormatPlugin *format()
  {
    return new KDEAccountsFormat();
  }
}

/**
 * Loads addresses of the kde-common/accounts file-> The format is
 * pfeiffer    Carsten Pfeiffer               pfeiffer@kde.org
 */

bool KDEAccountsFormat::loadAll( KABC::AddressBook *book,
                              KABC::Resource *resource,
                              QFile *file )
{
    if ( !book || !file ) // eh?
        return false;

    QString uuid = "KDEAccountsEntry.";
    int id = 0;

    QByteArray array = file->readAll();
    file->close();

    QByteArray::ConstIterator it = array.begin();
    QByteArray::ConstIterator end = array.end();
    QByteArray::ConstIterator startLine;
    QString line;
    char eol = '\n';
    char delim = ' ';

    for ( ; it < end; it++ )
    {
        startLine = it;

        for ( ; it && it < end && *it != eol; it++ )
        { } // find eol

        uint length = it - startLine;
        line = QString::fromUtf8( startLine, length ).simplifyWhiteSpace();

        QString nickName;
        QString name;
        QString email;

        int firstSpace = line.find( delim );
        if ( firstSpace > 0 )
        {
            nickName = line.left( firstSpace );

            int lastSpace = line.findRev( delim );
            if ( lastSpace > firstSpace )
            {
                email = line.mid( lastSpace +1 );

                int start = firstSpace + 1;
                int length = lastSpace - start;
                name = line.mid( start, length );

                if ( !email.isEmpty() )
                {
                    KABC::Addressee address;
                    address.setNickName( nickName  );
                    address.setNameFromString( name );
                    address.setOrganization("KDE Project");
                    address.insertCategory("KDE Developer");
                    address.insertEmail( email  );
                    address.setUid( uuid + QString::number( id++ ));

                    address.setResource( resource );
                    book->insertAddressee( address );
                }
            }
        }
    }

    return true;
}