tools: MimeDump<> can have all static members
[barry.git] / src / vjournal.cc
blobef8e81dad4b4656aea01aef2129ccc09d28ee5dd
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-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.
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()
36 : m_gJournalData(0)
40 vJournal::~vJournal()
42 if( m_gJournalData ) {
43 g_free(m_gJournalData);
47 bool vJournal::HasMultipleVJournals() const
49 int count = 0;
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 )
57 count++;
60 return count > 1;
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;
70 memo.Dump(oss);
71 // trace.logf("ToMemo, initial Barry record: %s", oss.str().c_str());
73 // start fresh
74 Clear();
75 SetFormat( b_vformat_new() );
76 if( !Format() )
77 throw ConvertError("resource error allocating vformat");
79 // store the Barry object we're working with
80 m_BarryMemo = memo;
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)
107 using namespace std;
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");
116 // start fresh
117 Clear();
119 // store the vJournal raw data
120 m_vJournalData = vjournal;
122 // create format parser structures
123 SetFormat( b_vformat_new_from_string(vjournal) );
124 if( !Format() )
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);
146 rec.Title = title;
147 rec.Body = body;
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());
153 return m_BarryMemo;
156 // Transfers ownership of m_gMemoData to the caller.
157 char* vJournal::ExtractVJournal()
159 char *ret = m_gJournalData;
160 m_gJournalData = 0;
161 return ret;
164 void vJournal::Clear()
166 vBase::Clear();
167 m_vJournalData.clear();
168 m_BarryMemo.Clear();
170 if( m_gJournalData ) {
171 g_free(m_gJournalData);
172 m_gJournalData = 0;
176 }} // namespace Barry::Sync