3 // Conversion routines for vjournals (VCALENDAR, etc)
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2006-2010, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
30 namespace Barry
{ namespace Sync
{
32 //////////////////////////////////////////////////////////////////////////////
42 if( m_gJournalData
) {
43 g_free(m_gJournalData
);
47 bool vJournal::HasMultipleVJournals() const
50 b_VFormat
*format
= const_cast<b_VFormat
*>(Format());
51 GList
*attrs
= format
? b_vformat_get_attributes(format
) : 0;
52 for( ; attrs
; attrs
= attrs
->next
) {
53 b_VFormatAttribute
*attr
= (b_VFormatAttribute
*) attrs
->data
;
54 if( strcasecmp(b_vformat_attribute_get_name(attr
), "BEGIN") == 0 &&
55 strcasecmp(b_vformat_attribute_get_nth_value(attr
, 0), "VJOURNAL") == 0 )
64 // Main conversion routine for converting from Barry::Memo to
65 // a vJournal string of data.
66 const std::string
& vJournal::ToMemo(const Barry::Memo
&memo
)
68 // Trace trace("vJournal::ToMemo");
69 std::ostringstream oss
;
71 // trace.logf("ToMemo, initial Barry record: %s", oss.str().c_str());
75 SetFormat( b_vformat_new() );
77 throw ConvertError("resource error allocating vformat");
79 // store the Barry object we're working with
82 // begin building vJournal data
83 AddAttr(NewAttr("PRODID", "-//OpenSync//NONSGML Barry Memo Record//EN"));
84 AddAttr(NewAttr("BEGIN", "VJOURNAL"));
85 AddAttr(NewAttr("SEQUENCE", "0"));
86 AddAttr(NewAttr("SUMMARY", memo
.Title
.c_str()));
87 AddAttr(NewAttr("DESCRIPTION", memo
.Body
.c_str()));
88 AddAttr(NewAttr("CATEGORIES", ToStringList(memo
.Categories
).c_str()));
91 // FIXME - add a truly globally unique "UID" string?
93 AddAttr(NewAttr("END", "VJOURNAL"));
95 // generate the raw VJOURNAL data
96 m_gJournalData
= b_vformat_to_string(Format(), VFORMAT_NOTE
);
97 m_vJournalData
= m_gJournalData
;
99 // trace.logf("ToMemo, resulting vjournal data: %s", m_vJournalData.c_str());
100 return m_vJournalData
;
103 // Main conversion routine for converting from vJournal data string
104 // to a Barry::Memo object.
105 const Barry::Memo
& vJournal::ToBarry(const char *vjournal
, uint32_t RecordId
)
109 // Trace trace("vJournal::ToBarry");
110 // trace.logf("ToBarry, working on vmemo data: %s", vjournal);
112 // we only handle vJournal data with one vmemo block
113 if( HasMultipleVJournals() )
114 throw ConvertError("vCalendar data contains more than one VJOURNAL block, unsupported");
119 // store the vJournal raw data
120 m_vJournalData
= vjournal
;
122 // create format parser structures
123 SetFormat( b_vformat_new_from_string(vjournal
) );
125 throw ConvertError("resource error allocating vjournal");
127 string title
= GetAttr("SUMMARY", "/vjournal");
128 // trace.logf("SUMMARY attr retrieved: %s", title.c_str());
129 if( title
.size() == 0 ) {
130 title
= "<blank subject>";
131 // trace.logf("ERROR: bad data, blank SUMMARY: %s", vjournal);
134 string body
= GetAttr("DESCRIPTION", "/vjournal");
135 // trace.logf("DESCRIPTION attr retrieved: %s", body.c_str());
139 // Now, run checks and convert into Barry object
143 Barry::Memo
&rec
= m_BarryMemo
;
144 rec
.SetIds(Barry::Memo::GetDefaultRecType(), RecordId
);
148 rec
.Categories
= GetValueVector("CATEGORIES","/vjournal");
150 std::ostringstream oss
;
151 m_BarryMemo
.Dump(oss
);
152 // trace.logf("ToBarry, resulting Barry record: %s", oss.str().c_str());
156 // Transfers ownership of m_gMemoData to the caller.
157 char* vJournal::ExtractVJournal()
159 char *ret
= m_gJournalData
;
164 void vJournal::Clear()
167 m_vJournalData
.clear();
170 if( m_gJournalData
) {
171 g_free(m_gJournalData
);
176 }} // namespace Barry::Sync