summaryrefslogtreecommitdiff
path: root/src/kolabconversions.h
blob: 056839a766d259fb606e1bddcdaed2e5a9a9b2b3 (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
/*
 * Copyright (C) 2012  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef KOLABOBJECTCONVERSION_H
#define KOLABOBJECTCONVERSION_H

#include <boost/shared_ptr.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>

#include <XMLParserWrapper.h>

#include <bindings/kolabformat.hxx>
#include <bindings/kolabformat-xcard.hxx>

#include "kolabcontainers.h"
#include "global_definitions.h"
#include "utils.h"
#include "kolabnote.h"
#include "shared_conversions.h"

namespace Kolab {
    namespace KolabObjects {
        
const char* const KOLAB_NAMESPACE = "http://kolab.org";
    
using namespace Kolab::Utils;
using namespace Kolab::Shared;

template <typename T>
std::string serializeObject(const Kolab::Note &note, const std::string prod = std::string())
{
    clearErrors();
    try {

        const std::string &uid = getUID(note.uid());
        setCreatedUid(uid);
        
        KolabXSD::Note::creation_date_type created = fromDateTime(note.created());
        if (!note.created().isValid()) {
            created = fromDateTime(timestamp());
        }

        KolabXSD::Note n(getUID(note.uid()), prod+KOLAB_LIB_VERSION_STRING, created, fromDateTime(timestamp()));

        if (!note.categories().empty()) {
            KolabXSD::Note::categories_sequence categories;
            BOOST_FOREACH(const std::string &c, note.categories()) {
                categories.push_back(c);
            }
            n.categories(categories);
        }
        switch (note.classification()) {
            case Kolab::ClassPublic:
                n.classification(KolabXSD::Note::classification_type::PUBLIC);
                break;
            case Kolab::ClassPrivate:
                n.classification(KolabXSD::Note::classification_type::PRIVATE);
                break;
            case Kolab::ClassConfidential:
                n.classification(KolabXSD::Note::classification_type::CONFIDENTIAL);
                break;
            default:
                ERROR("unknown classification");
        }
        //TODO
//         n.attachment();
        n.summary(note.summary());
        n.description(note.description());
        n.color(note.color());

        if (!note.customProperties().empty()) {
            BOOST_FOREACH(const Kolab::CustomProperty &a, note.customProperties()) {
                n.x_custom().push_back(KolabXSD::CustomType(a.identifier, a.value));
            }
        }

        xml_schema::namespace_infomap map;
        map[""].name = KOLAB_NAMESPACE;

        std::ostringstream ostringstream;
        KolabXSD::note(ostringstream, n, map);
        return ostringstream.str();
    } catch  (const xml_schema::exception& e) {
        CRITICAL("Unknown Exception: failed to write Note");
    }
    return std::string();
}

template <typename T>
boost::shared_ptr<Kolab::Note> deserializeObject(const std::string& s, bool isUrl)
{
    clearErrors();
    try {
        std::auto_ptr<KolabXSD::Note> note;
        if (isUrl) {
            xsd::cxx::xml::dom::auto_ptr <xercesc::DOMDocument > doc = XMLParserWrapper::inst().parseFile(s);
            if (doc.get()) {
                note = KolabXSD::note(doc);
            }
        } else {
            xsd::cxx::xml::dom::auto_ptr <xercesc::DOMDocument > doc = XMLParserWrapper::inst().parseString(s);
            if (doc.get()) {
                note = KolabXSD::note(doc);
            }
        }

        if (!note.get()) {
            CRITICAL("failed to parse note!");
            return boost::shared_ptr<Kolab::Note>();
        }

        boost::shared_ptr<Kolab::Note> n = boost::shared_ptr<Kolab::Note>(new Kolab::Note);
        n->setUid(note->uid());
        n->setCreated(*toDate(note->creation_date()));
        n->setLastModified(*toDate(note->last_modification_date()));
        std::vector<std::string> categories;
        std::copy(note->categories().begin(), note->categories().end(), std::back_inserter(categories));
        n->setCategories(categories);
        if (note->classification()) {
            switch (*note->classification()) {
                case KolabXSD::Note::classification_type::PUBLIC:
                    n->setClassification(Kolab::ClassPublic);
                    break;
                case KolabXSD::Note::classification_type::PRIVATE:
                    n->setClassification(Kolab::ClassPrivate);
                    break;
                case KolabXSD::Note::classification_type::CONFIDENTIAL:
                    n->setClassification(Kolab::ClassConfidential);
                    break;
                default:
                    ERROR("unknown classification");
            }
        }
        //TODO
//             n->setAttachments();
        if (note->summary()) {
            n->setSummary(*note->summary());
        }
        if (note->description()) {
            n->setDescription(*note->description());
        }
        if (note->color()) {
            n->setColor(*note->color());
        }
        
        setProductId( note->prodid() );
        //         setFormatVersion( vcards->vcard().version().text() );
        //         global_xCardVersion = vcalendar.properties().version().text();
        setKolabVersion( note->version() );
        
        if (!note->x_custom().empty()) {
            std::vector<Kolab::CustomProperty> customProperties;
            BOOST_FOREACH(const KolabXSD::CustomType &p, note->x_custom()) {
                customProperties.push_back(CustomProperty(p.identifier(), p.value()));
            }
            n->setCustomProperties(customProperties);
        }
        return n;
    } catch  (const xml_schema::exception& e) {
        std::cerr <<  e << std::endl;
        CRITICAL("Failed to read note!");
    }
    
    return boost::shared_ptr<T>();
}
    
    }//Namespace
} //Namespace

#endif