summaryrefslogtreecommitdiff
path: root/libopie/pim/opimresolver.cpp
blob: 4ebbd6e1cd81f3eebfa1141ff40caa5e4cdf01ad (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <qcopchannel_qws.h>

#include <qpe/qcopenvelope_qws.h>

#include "otodoaccess.h"
#include "ocontactaccess.h"

//#include "opimfactory.h"
#include "opimresolver.h"

OPimResolver* OPimResolver::m_self = 0l;

OPimResolver::OPimResolver() {
    /* the built in channels */
    m_builtIns << "Todolist" << "Addressbook" << "Datebook";
}
OPimResolver* OPimResolver::self() {
    if (!m_self)
        m_self = new OPimResolver();

    return m_self;
}

/*
 * FIXME use a cache here too
 */
OPimRecord* OPimResolver::record( const QString& service, int uid ) {
    OPimRecord* rec = 0l;
    OPimBase* base = backend( service );

    if ( base )
        rec = base->record( uid );
    delete base;

    return rec;
}
OPimRecord* OPimResolver::record( const QString& service ) {
    return record( serviceId( service ) );
}
OPimRecord* OPimResolver::record( int rtti ) {
    OPimRecord* rec = 0l;
    switch( rtti ) {
    case 1: /* todolist */
        rec = new OTodo();
    case 2: /* contact  */
        rec = new OContact();
    default:
        break;
    }
    /*
     * FIXME resolve externally
     */
    if (!rec ) {
        ;
    }
    return 0l;
}
bool OPimResolver::isBuiltIn( const QString& str) const{
    return m_builtIns.contains( str );
}
QCString OPimResolver::qcopChannel( enum BuiltIn& built)const {
    QCString str("QPE/");
    switch( built ) {
    case TodoList:
        str += "Todolist";
        break;
    case DateBook:
        str += "Datebook";
        break;
    case AddressBook:
        str += "Addressbook";
        break;
    default:
        break;
    }

    return str;
}
QCString OPimResolver::qcopChannel( const QString& service )const {
    QCString str("QPE/");
    str += service.latin1();
    return str;
}
/*
 * Implement services!!
 * FIXME
 */
QCString OPimResolver::applicationChannel( enum BuiltIn& built)const {
    QCString str("QPE/Application/");
    switch( built ) {
    case TodoList:
        str += "todolist";
        break;
    case DateBook:
        str += "datebook";
        break;
    case AddressBook:
        str += "addressbook";
        break;
    }

    return str;
}
QCString OPimResolver::applicationChannel( const QString& service )const {
    QCString str("QPE/Application/");

    if ( isBuiltIn( service ) ) {
        if ( service == "Todolist" )
            str += "todolist";
        else if ( service == "Datebook" )
            str += "datebook";
        else if ( service == "Addressbook" )
            str += "addressbook";
    }else
        ; // FIXME for additional stuff

    return str;
}
QStringList OPimResolver::services()const {
    return m_builtIns;
}
QString OPimResolver::serviceName( int rtti ) const{
    QString str;
    switch ( rtti ) {
    case TodoList:
        str = "Todolist";
        break;
    case DateBook:
        str = "Datebook";
        break;
    case AddressBook:
        str = "Addressbook";
        break;
    default:
        break;
    }
    return str;
    // FIXME me for 3rd party
}
int OPimResolver::serviceId( const QString& service ) {
    int rtti = 0;
    if ( service == "Todolist" )
        rtti = TodoList;
    else if ( service == "Datebook" )
        rtti = DateBook;
    else if ( service == "Addressbook" )
        rtti = AddressBook;

    return rtti;
}
/**
 * check if the 'service' is registered and if so we'll
 */
bool OPimResolver::add( const QString& service,  const OPimRecord& rec) {
    if ( QCopChannel::isRegistered( applicationChannel( service ) ) ) {
        QByteArray data;
        QDataStream arg(data, IO_WriteOnly );
        if ( rec.saveToStream( arg ) ) {
            QCopEnvelope env( applicationChannel( service ), "add(int,QByteArray)" );
            env << rec.rtti();
            env << data;
        }else
            return false;
    }else{
        OPimBase* base = backend( service );
        if (!base ) return false;

        base->load();
        base->add( rec );
        base->save();
        delete base;
    }

    return true;
}
OPimBase* OPimResolver::backend( const QString& service ) {
    return backend( serviceId( service ) );
}
OPimBase* OPimResolver::backend( int rtti ) {
    OPimBase* base = 0l;
    switch( rtti ) {
    case TodoList:
        base = new OTodoAccess();
        break;
    case DateBook:
        break;
    case AddressBook:
        base = new OContactAccess("Resolver");
        break;
    default:
        break;
    }
    // FIXME for 3rd party
    if (!base )
        ;

    return base;
}