summaryrefslogtreecommitdiffstats
path: root/kdeaccounts-plugin/kdeaccountsformat.cpp
blob: 97a07d4d8dc1a26da63e9e9de1035700876abe95 (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 <tqcstring.h>
#include <tqfile.h>

#include <tdeabc/addressbook.h>
#include <tdeabc/addressee.h>
#include <tdeabc/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,
                              TQFile *file )
{
    if ( !book || !file ) // eh?
        return false;

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

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

    TQByteArray::ConstIterator it = array.begin();
    TQByteArray::ConstIterator end = array.end();
    TQByteArray::ConstIterator startLine;
    TQString 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 = TQString::fromUtf8( startLine, length ).simplifyWhiteSpace();

        TQString nickName;
        TQString name;
        TQString 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 + TQString::number( id++ ));

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

    return true;
}