menu: added new Keywords tag to .desktop files
[barry.git] / src / vbase.h
blob06ce8c9e91cfc93fa576de16d957ee8f987cebc8
1 ///
2 /// \file vbase.h
3 /// Base class for vformat support
4 ///
6 /*
7 Copyright (C) 2006-2013, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_SYNC_VBASE_H__
23 #define __BARRY_SYNC_VBASE_H__
25 #include "dll.h"
26 #include "vsmartptr.h"
27 #include "vformat.h"
28 #include "error.h"
29 #include <vector>
31 namespace Barry {
32 class CategoryList;
35 namespace Barry { namespace Sync {
38 // vTimeConverter
40 /// A virtual base class that the plugins may override, to do
41 /// time related conversions. Default implementations for these
42 /// functions are provided, but may be overrided depending on need.
43 ///
44 /// We do this in a "callback" style, so that it doesn't matter what
45 /// version of the opensync library we link against, in case the
46 /// user wishes to use the opensync time functions.
47 ///
48 class BXEXPORT vTimeConverter
50 public:
51 virtual ~vTimeConverter() {}
53 /// Convert a time_t into an ISO timestamp string
54 /// Throws Barry::ConvertError on error, but these errors
55 /// must be rare.
56 virtual std::string unix2vtime(const time_t *timestamp);
58 /// Convert an ISO timestamp string into a time_t, using
59 /// the current system timezone if vtime is not in UTC.
60 /// Returns (time_t)-1 on error.
61 virtual time_t vtime2unix(const char *vtime);
63 /// Convert a VEVENT alarm duration string in the format
64 /// of "[+-]P.W.DT.H.M.S" where the periods represent numbers
65 /// and each letter besides P and T represent Week, Day,
66 /// Hour, Minute, and Second respectively.
67 virtual int alarmduration2sec(const char *alarm);
71 typedef Barry::vSmartPtr<b_VFormatAttribute, b_VFormatAttribute, &b_vformat_attribute_free> vAttrPtr;
72 typedef Barry::vSmartPtr<b_VFormatParam, b_VFormatParam, &b_vformat_attribute_param_free> vParamPtr;
73 typedef Barry::vSmartPtr<char, void, &g_free> gStringPtr;
77 // vAttr
79 /// Class for reading a b_VFormatAttribute. Reading does not require
80 /// memory management, so none is done.
81 ///
82 class BXEXPORT vAttr
84 b_VFormatAttribute *m_attr;
86 public:
87 vAttr()
88 : m_attr(0)
92 vAttr(b_VFormatAttribute *attr)
93 : m_attr(attr)
97 vAttr& operator=(b_VFormatAttribute *attr)
99 m_attr = attr;
100 return *this;
103 b_VFormatAttribute* Get() { return m_attr; }
105 // These functions do not throw an error if the value
106 // is NULL or does not exist (for example, if you ask for
107 // value #5 and there are only 4).
108 std::string GetName();
109 std::string GetValue(int nth = 0);
110 std::string GetDecodedValue();
111 std::string GetParam(const char *name, int nth = 0);
112 std::string GetAllParams(const char *name);
117 // vBase
119 /// Base class containing vformat helper API.
121 class BXEXPORT vBase
123 // internal data for managing the vformat
124 b_VFormat *m_format;
126 public:
127 protected:
128 vBase();
129 explicit vBase(b_VFormat *format);
130 virtual ~vBase();
132 b_VFormat* Format() { return m_format; }
133 const b_VFormat* Format() const { return m_format; }
134 void SetFormat(b_VFormat *format);
136 void Clear();
138 vAttrPtr NewAttr(const char *name);
139 vAttrPtr NewAttr(const char *name, const char *value);
140 void AddAttr(vAttrPtr attr);
141 void AddValue(vAttrPtr &attr, const char *value);
142 void AddEncodedValue(vAttrPtr &attr, b_VFormatEncoding encoding, const char *value, int len);
143 void AddParam(vAttrPtr &attr, const char *name, const char *value);
145 void AddCategories(const Barry::CategoryList &categories);
147 std::string GetAttr(const char *attrname, const char *block = 0);
148 std::vector<std::string> GetValueVector(const char *attrname, const char *block = 0);
149 vAttr GetAttrObj(const char *attrname, int nth = 0, const char *block = 0);
151 std::vector<std::string> Tokenize(const std::string &str, const char delim = ',');
152 std::string ToStringList(const std::vector<std::string> &list, const char delim = ',');
155 }} // namespace Barry::Sync
157 #endif