debian: added giffgaff chatscripts
[barry.git] / src / vjournal.cc
blob70b7028a309ca966d6c61cf1abfb86b9094e7ed5
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-2013, 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 "i18n.h"
24 #include "vjournal.h"
25 //#include "trace.h"
26 #include <stdint.h>
27 #include <glib.h>
28 #include <strings.h>
29 #include <sstream>
31 namespace Barry { namespace Sync {
33 //////////////////////////////////////////////////////////////////////////////
34 // vJournal
36 vJournal::vJournal(vTimeConverter &vtc)
37 : m_vtc(vtc)
38 , m_gJournalData(0)
42 vJournal::~vJournal()
44 if( m_gJournalData ) {
45 g_free(m_gJournalData);
49 bool vJournal::HasMultipleVJournals() const
51 int count = 0;
52 b_VFormat *format = const_cast<b_VFormat*>(Format());
53 GList *attrs = format ? b_vformat_get_attributes(format) : 0;
54 for( ; attrs; attrs = attrs->next ) {
55 b_VFormatAttribute *attr = (b_VFormatAttribute*) attrs->data;
56 if( strcasecmp(b_vformat_attribute_get_name(attr), "BEGIN") == 0 &&
57 strcasecmp(b_vformat_attribute_get_nth_value(attr, 0), "VJOURNAL") == 0 )
59 count++;
62 return count > 1;
66 // Main conversion routine for converting from Barry::Memo to
67 // a vJournal string of data.
68 const std::string& vJournal::ToMemo(const Barry::Memo &memo)
70 // Trace trace("vJournal::ToMemo");
71 std::ostringstream oss;
72 memo.Dump(oss);
73 // trace.logf("ToMemo, initial Barry record: %s", oss.str().c_str());
75 // start fresh
76 Clear();
77 SetFormat( b_vformat_new() );
78 if( !Format() )
79 throw ConvertError(_("resource error allocating vformat"));
81 // store the Barry object we're working with
82 m_BarryMemo = memo;
84 // RFC section 4.8.7.2 requires DTSTAMP in all VEVENT, VTODO,
85 // VJOURNAL, and VFREEBUSY calendar components, and it must be
86 // in UTC. DTSTAMP holds the timestamp of when the iCal object itself
87 // was created, not when the object was created in the device or app.
88 // So, find out what time it is "now".
89 time_t now = time(NULL);
91 // begin building vJournal data
92 AddAttr(NewAttr("PRODID", "-//OpenSync//NONSGML Barry Memo Record//EN"));
93 AddAttr(NewAttr("BEGIN", "VJOURNAL"));
94 AddAttr(NewAttr("DTSTAMP", m_vtc.unix2vtime(&now).c_str())); // see note above
95 AddAttr(NewAttr("SEQUENCE", "0"));
96 AddAttr(NewAttr("SUMMARY", memo.Title.c_str()));
97 AddAttr(NewAttr("DESCRIPTION", memo.Body.c_str()));
98 AddAttr(NewAttr("CATEGORIES", ToStringList(memo.Categories).c_str()));
101 // FIXME - add a truly globally unique "UID" string?
103 AddAttr(NewAttr("END", "VJOURNAL"));
105 // generate the raw VJOURNAL data
106 m_gJournalData = b_vformat_to_string(Format(), VFORMAT_NOTE);
107 m_vJournalData = m_gJournalData;
109 // trace.logf("ToMemo, resulting vjournal data: %s", m_vJournalData.c_str());
110 return m_vJournalData;
113 // Main conversion routine for converting from vJournal data string
114 // to a Barry::Memo object.
115 const Barry::Memo& vJournal::ToBarry(const char *vjournal, uint32_t RecordId)
117 using namespace std;
119 // Trace trace("vJournal::ToBarry");
120 // trace.logf("ToBarry, working on vmemo data: %s", vjournal);
122 // we only handle vJournal data with one vmemo block
123 if( HasMultipleVJournals() )
124 throw ConvertError(_("vCalendar data contains more than one VJOURNAL block, unsupported"));
126 // start fresh
127 Clear();
129 // store the vJournal raw data
130 m_vJournalData = vjournal;
132 // create format parser structures
133 SetFormat( b_vformat_new_from_string(vjournal) );
134 if( !Format() )
135 throw ConvertError(_("resource error allocating vjournal"));
137 string title = GetAttr("SUMMARY", "/vjournal");
138 // trace.logf("SUMMARY attr retrieved: %s", title.c_str());
139 if( title.size() == 0 ) {
140 title = "<blank subject>";
141 // trace.logf("ERROR: bad data, blank SUMMARY: %s", vjournal);
144 string body = GetAttr("DESCRIPTION", "/vjournal");
145 // trace.logf("DESCRIPTION attr retrieved: %s", body.c_str());
149 // Now, run checks and convert into Barry object
153 Barry::Memo &rec = m_BarryMemo;
154 rec.SetIds(Barry::Memo::GetDefaultRecType(), RecordId);
156 rec.Title = title;
157 rec.Body = body;
158 rec.Categories = GetValueVector("CATEGORIES","/vjournal");
160 std::ostringstream oss;
161 m_BarryMemo.Dump(oss);
162 // trace.logf("ToBarry, resulting Barry record: %s", oss.str().c_str());
163 return m_BarryMemo;
166 // Transfers ownership of m_gMemoData to the caller.
167 char* vJournal::ExtractVJournal()
169 char *ret = m_gJournalData;
170 m_gJournalData = 0;
171 return ret;
174 void vJournal::Clear()
176 vBase::Clear();
177 m_vJournalData.clear();
178 m_BarryMemo.Clear();
180 if( m_gJournalData ) {
181 g_free(m_gJournalData);
182 m_gJournalData = 0;
186 }} // namespace Barry::Sync