vjournal.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "vjournal.h"
00024
00025 #include <stdint.h>
00026 #include <glib.h>
00027 #include <strings.h>
00028 #include <sstream>
00029
00030 namespace Barry { namespace Sync {
00031
00032
00033
00034
00035 vJournal::vJournal()
00036 : m_gJournalData(0)
00037 {
00038 }
00039
00040 vJournal::~vJournal()
00041 {
00042 if( m_gJournalData ) {
00043 g_free(m_gJournalData);
00044 }
00045 }
00046
00047 bool vJournal::HasMultipleVJournals() const
00048 {
00049 int count = 0;
00050 b_VFormat *format = const_cast<b_VFormat*>(Format());
00051 GList *attrs = format ? b_vformat_get_attributes(format) : 0;
00052 for( ; attrs; attrs = attrs->next ) {
00053 b_VFormatAttribute *attr = (b_VFormatAttribute*) attrs->data;
00054 if( strcasecmp(b_vformat_attribute_get_name(attr), "BEGIN") == 0 &&
00055 strcasecmp(b_vformat_attribute_get_nth_value(attr, 0), "VJOURNAL") == 0 )
00056 {
00057 count++;
00058 }
00059 }
00060 return count > 1;
00061 }
00062
00063
00064
00065
00066 const std::string& vJournal::ToMemo(const Barry::Memo &memo)
00067 {
00068
00069 std::ostringstream oss;
00070 memo.Dump(oss);
00071
00072
00073
00074 Clear();
00075 SetFormat( b_vformat_new() );
00076 if( !Format() )
00077 throw ConvertError("resource error allocating vformat");
00078
00079
00080 m_BarryMemo = memo;
00081
00082
00083 AddAttr(NewAttr("PRODID", "-//OpenSync//NONSGML Barry Memo Record//EN"));
00084 AddAttr(NewAttr("BEGIN", "VJOURNAL"));
00085 AddAttr(NewAttr("SEQUENCE", "0"));
00086 AddAttr(NewAttr("SUMMARY", memo.Title.c_str()));
00087 AddAttr(NewAttr("DESCRIPTION", memo.Body.c_str()));
00088 AddAttr(NewAttr("CATEGORIES", ToStringList(memo.Categories).c_str()));
00089
00090
00091
00092
00093 AddAttr(NewAttr("END", "VJOURNAL"));
00094
00095
00096 m_gJournalData = b_vformat_to_string(Format(), VFORMAT_NOTE);
00097 m_vJournalData = m_gJournalData;
00098
00099
00100 return m_vJournalData;
00101 }
00102
00103
00104
00105 const Barry::Memo& vJournal::ToBarry(const char *vjournal, uint32_t RecordId)
00106 {
00107 using namespace std;
00108
00109
00110
00111
00112
00113 if( HasMultipleVJournals() )
00114 throw ConvertError("vCalendar data contains more than one VJOURNAL block, unsupported");
00115
00116
00117 Clear();
00118
00119
00120 m_vJournalData = vjournal;
00121
00122
00123 SetFormat( b_vformat_new_from_string(vjournal) );
00124 if( !Format() )
00125 throw ConvertError("resource error allocating vjournal");
00126
00127 string title = GetAttr("SUMMARY", "/vjournal");
00128
00129 if( title.size() == 0 ) {
00130 title = "<blank subject>";
00131
00132 }
00133
00134 string body = GetAttr("DESCRIPTION", "/vjournal");
00135
00136
00137
00138
00139
00140
00141
00142
00143 Barry::Memo &rec = m_BarryMemo;
00144 rec.SetIds(Barry::Memo::GetDefaultRecType(), RecordId);
00145
00146 rec.Title = title;
00147 rec.Body = body;
00148 rec.Categories = GetValueVector("CATEGORIES","/vjournal");
00149
00150 std::ostringstream oss;
00151 m_BarryMemo.Dump(oss);
00152
00153 return m_BarryMemo;
00154 }
00155
00156
00157 char* vJournal::ExtractVJournal()
00158 {
00159 char *ret = m_gJournalData;
00160 m_gJournalData = 0;
00161 return ret;
00162 }
00163
00164 void vJournal::Clear()
00165 {
00166 vBase::Clear();
00167 m_vJournalData.clear();
00168 m_BarryMemo.Clear();
00169
00170 if( m_gJournalData ) {
00171 g_free(m_gJournalData);
00172 m_gJournalData = 0;
00173 }
00174 }
00175
00176 }}
00177