3 /// Conversion routines for vjournals (VCALENDAR, etc)
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2006-2012, 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 //////////////////////////////////////////////////////////////////////////////
35 vJournal::vJournal(vTimeConverter
&vtc
)
43 if( m_gJournalData
) {
44 g_free(m_gJournalData
);
48 bool vJournal::HasMultipleVJournals() const
51 b_VFormat
*format
= const_cast<b_VFormat
*>(Format());
52 GList
*attrs
= format
? b_vformat_get_attributes(format
) : 0;
53 for( ; attrs
; attrs
= attrs
->next
) {
54 b_VFormatAttribute
*attr
= (b_VFormatAttribute
*) attrs
->data
;
55 if( strcasecmp(b_vformat_attribute_get_name(attr
), "BEGIN") == 0 &&
56 strcasecmp(b_vformat_attribute_get_nth_value(attr
, 0), "VJOURNAL") == 0 )
65 // Main conversion routine for converting from Barry::Memo to
66 // a vJournal string of data.
67 const std::string
& vJournal::ToMemo(const Barry::Memo
&memo
)
69 // Trace trace("vJournal::ToMemo");
70 std::ostringstream oss
;
72 // trace.logf("ToMemo, initial Barry record: %s", oss.str().c_str());
76 SetFormat( b_vformat_new() );
78 throw ConvertError("resource error allocating vformat");
80 // store the Barry object we're working with
83 // RFC section 4.8.7.2 requires DTSTAMP in all VEVENT, VTODO,
84 // VJOURNAL, and VFREEBUSY calendar components, and it must be
85 // in UTC. DTSTAMP holds the timestamp of when the iCal object itself
86 // was created, not when the object was created in the device or app.
87 // So, find out what time it is "now".
88 time_t now
= time(NULL
);
90 // begin building vJournal data
91 AddAttr(NewAttr("PRODID", "-//OpenSync//NONSGML Barry Memo Record//EN"));
92 AddAttr(NewAttr("BEGIN", "VJOURNAL"));
93 AddAttr(NewAttr("DTSTAMP", m_vtc
.unix2vtime(&now
).c_str())); // see note above
94 AddAttr(NewAttr("SEQUENCE", "0"));
95 AddAttr(NewAttr("SUMMARY", memo
.Title
.c_str()));
96 AddAttr(NewAttr("DESCRIPTION", memo
.Body
.c_str()));
97 AddAttr(NewAttr("CATEGORIES", ToStringList(memo
.Categories
).c_str()));
100 // FIXME - add a truly globally unique "UID" string?
102 AddAttr(NewAttr("END", "VJOURNAL"));
104 // generate the raw VJOURNAL data
105 m_gJournalData
= b_vformat_to_string(Format(), VFORMAT_NOTE
);
106 m_vJournalData
= m_gJournalData
;
108 // trace.logf("ToMemo, resulting vjournal data: %s", m_vJournalData.c_str());
109 return m_vJournalData
;
112 // Main conversion routine for converting from vJournal data string
113 // to a Barry::Memo object.
114 const Barry::Memo
& vJournal::ToBarry(const char *vjournal
, uint32_t RecordId
)
118 // Trace trace("vJournal::ToBarry");
119 // trace.logf("ToBarry, working on vmemo data: %s", vjournal);
121 // we only handle vJournal data with one vmemo block
122 if( HasMultipleVJournals() )
123 throw ConvertError("vCalendar data contains more than one VJOURNAL block, unsupported");
128 // store the vJournal raw data
129 m_vJournalData
= vjournal
;
131 // create format parser structures
132 SetFormat( b_vformat_new_from_string(vjournal
) );
134 throw ConvertError("resource error allocating vjournal");
136 string title
= GetAttr("SUMMARY", "/vjournal");
137 // trace.logf("SUMMARY attr retrieved: %s", title.c_str());
138 if( title
.size() == 0 ) {
139 title
= "<blank subject>";
140 // trace.logf("ERROR: bad data, blank SUMMARY: %s", vjournal);
143 string body
= GetAttr("DESCRIPTION", "/vjournal");
144 // trace.logf("DESCRIPTION attr retrieved: %s", body.c_str());
148 // Now, run checks and convert into Barry object
152 Barry::Memo
&rec
= m_BarryMemo
;
153 rec
.SetIds(Barry::Memo::GetDefaultRecType(), RecordId
);
157 rec
.Categories
= GetValueVector("CATEGORIES","/vjournal");
159 std::ostringstream oss
;
160 m_BarryMemo
.Dump(oss
);
161 // trace.logf("ToBarry, resulting Barry record: %s", oss.str().c_str());
165 // Transfers ownership of m_gMemoData to the caller.
166 char* vJournal::ExtractVJournal()
168 char *ret
= m_gJournalData
;
173 void vJournal::Clear()
176 m_vJournalData
.clear();
179 if( m_gJournalData
) {
180 g_free(m_gJournalData
);
185 }} // namespace Barry::Sync