lib: fixed parsing of recurring VEVENTS: DAILY and interval support
[barry/progweb.git] / src / vjournal.cc
blob5bf174c3ac82b1655c1f4643149b68cd405db718
1 ///
2 /// \file vjournal.cc
3 /// Conversion routines for vjournals (VCALENDAR, etc)
4 ///
6 /*
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.
23 #include "vjournal.h"
24 //#include "trace.h"
25 #include <stdint.h>
26 #include <glib.h>
27 #include <strings.h>
28 #include <sstream>
30 namespace Barry { namespace Sync {
32 //////////////////////////////////////////////////////////////////////////////
33 // vJournal
35 vJournal::vJournal(vTimeConverter &vtc)
36 : m_vtc(vtc)
37 , m_gJournalData(0)
41 vJournal::~vJournal()
43 if( m_gJournalData ) {
44 g_free(m_gJournalData);
48 bool vJournal::HasMultipleVJournals() const
50 int count = 0;
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 )
58 count++;
61 return count > 1;
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;
71 memo.Dump(oss);
72 // trace.logf("ToMemo, initial Barry record: %s", oss.str().c_str());
74 // start fresh
75 Clear();
76 SetFormat( b_vformat_new() );
77 if( !Format() )
78 throw ConvertError("resource error allocating vformat");
80 // store the Barry object we're working with
81 m_BarryMemo = memo;
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)
116 using namespace std;
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");
125 // start fresh
126 Clear();
128 // store the vJournal raw data
129 m_vJournalData = vjournal;
131 // create format parser structures
132 SetFormat( b_vformat_new_from_string(vjournal) );
133 if( !Format() )
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);
155 rec.Title = title;
156 rec.Body = body;
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());
162 return m_BarryMemo;
165 // Transfers ownership of m_gMemoData to the caller.
166 char* vJournal::ExtractVJournal()
168 char *ret = m_gJournalData;
169 m_gJournalData = 0;
170 return ret;
173 void vJournal::Clear()
175 vBase::Clear();
176 m_vJournalData.clear();
177 m_BarryMemo.Clear();
179 if( m_gJournalData ) {
180 g_free(m_gJournalData);
181 m_gJournalData = 0;
185 }} // namespace Barry::Sync