-rw-r--r-- | libkcal/icalformat.cpp | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/libkcal/icalformat.cpp b/libkcal/icalformat.cpp index 6f3a799..3829bc1 100644 --- a/libkcal/icalformat.cpp +++ b/libkcal/icalformat.cpp | |||
@@ -1,242 +1,242 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libkcal. | 2 | This file is part of libkcal. |
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | 3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public | 6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. | 8 | version 2 of the License, or (at your option) any later version. |
9 | 9 | ||
10 | This library is distributed in the hope that it will be useful, | 10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. | 13 | Library General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to | 16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #include <qdatetime.h> | 21 | #include <qdatetime.h> |
22 | #include <qstring.h> | 22 | #include <qstring.h> |
23 | #include <qptrlist.h> | 23 | #include <q3ptrlist.h> |
24 | #include <qregexp.h> | 24 | #include <qregexp.h> |
25 | #include <qclipboard.h> | 25 | #include <qclipboard.h> |
26 | #include <qfile.h> | 26 | #include <qfile.h> |
27 | #include <qtextstream.h> | 27 | #include <q3textstream.h> |
28 | #include <qtextcodec.h> | 28 | #include <qtextcodec.h> |
29 | #include <stdlib.h> | 29 | #include <stdlib.h> |
30 | 30 | ||
31 | #include <kdebug.h> | 31 | #include <kdebug.h> |
32 | #include <kglobal.h> | 32 | #include <kglobal.h> |
33 | #include <klocale.h> | 33 | #include <klocale.h> |
34 | 34 | ||
35 | extern "C" { | 35 | extern "C" { |
36 | #include <ical.h> | 36 | #include <ical.h> |
37 | #include <icalss.h> | 37 | #include <icalss.h> |
38 | #include <icalparser.h> | 38 | #include <icalparser.h> |
39 | #include <icalrestriction.h> | 39 | #include <icalrestriction.h> |
40 | } | 40 | } |
41 | 41 | ||
42 | #include "calendar.h" | 42 | #include "calendar.h" |
43 | #include "calendarlocal.h" | 43 | #include "calendarlocal.h" |
44 | #include "journal.h" | 44 | #include "journal.h" |
45 | 45 | ||
46 | #include "icalformat.h" | 46 | #include "icalformat.h" |
47 | #include "icalformatimpl.h" | 47 | #include "icalformatimpl.h" |
48 | 48 | ||
49 | #define _ICAL_VERSION "2.0" | 49 | #define _ICAL_VERSION "2.0" |
50 | 50 | ||
51 | using namespace KCal; | 51 | using namespace KCal; |
52 | 52 | ||
53 | ICalFormat::ICalFormat( bool pe ) | 53 | ICalFormat::ICalFormat( bool pe ) |
54 | { | 54 | { |
55 | mProcessEvents = pe; | 55 | mProcessEvents = pe; |
56 | mImpl = new ICalFormatImpl( this ); | 56 | mImpl = new ICalFormatImpl( this ); |
57 | tzOffsetMin = 0; | 57 | tzOffsetMin = 0; |
58 | //qDebug("new ICalFormat() "); | 58 | //qDebug("new ICalFormat() "); |
59 | } | 59 | } |
60 | 60 | ||
61 | ICalFormat::~ICalFormat() | 61 | ICalFormat::~ICalFormat() |
62 | { | 62 | { |
63 | delete mImpl; | 63 | delete mImpl; |
64 | //qDebug("delete ICalFormat "); | 64 | //qDebug("delete ICalFormat "); |
65 | } | 65 | } |
66 | 66 | ||
67 | bool ICalFormat::load( Calendar *calendar, const QString &fileName) | 67 | bool ICalFormat::load( Calendar *calendar, const QString &fileName) |
68 | { | 68 | { |
69 | 69 | ||
70 | clearException(); | 70 | clearException(); |
71 | 71 | ||
72 | QFile file( fileName ); | 72 | QFile file( fileName ); |
73 | if (!file.open( IO_ReadOnly ) ) { | 73 | if (!file.open( QIODevice::ReadOnly ) ) { |
74 | setException(new ErrorFormat(ErrorFormat::LoadError)); | 74 | setException(new ErrorFormat(ErrorFormat::LoadError)); |
75 | return false; | 75 | return false; |
76 | } | 76 | } |
77 | QTextStream ts( &file ); | 77 | Q3TextStream ts( &file ); |
78 | QString text; | 78 | QString text; |
79 | 79 | ||
80 | ts.setEncoding( QTextStream::Latin1 ); | 80 | ts.setEncoding( Q3TextStream::Latin1 ); |
81 | text = ts.read(); | 81 | text = ts.read(); |
82 | file.close(); | 82 | file.close(); |
83 | 83 | ||
84 | return fromString( calendar, text ); | 84 | return fromString( calendar, text ); |
85 | } | 85 | } |
86 | 86 | ||
87 | //#include <qdatetime.h> | 87 | //#include <qdatetime.h> |
88 | bool ICalFormat::save( Calendar *calendar, const QString &fileName ) | 88 | bool ICalFormat::save( Calendar *calendar, const QString &fileName ) |
89 | { | 89 | { |
90 | //kdDebug(5800) << "ICalFormat::save(): " << fileName << endl; | 90 | //kdDebug(5800) << "ICalFormat::save(): " << fileName << endl; |
91 | //qDebug("ICalFormat::save "); | 91 | //qDebug("ICalFormat::save "); |
92 | clearException(); | 92 | clearException(); |
93 | QString text = toString( calendar ); | 93 | QString text = toString( calendar ); |
94 | //return false; | 94 | //return false; |
95 | // qDebug("to string takes ms: %d ",is.elapsed() ); | 95 | // qDebug("to string takes ms: %d ",is.elapsed() ); |
96 | if ( text.isNull() ) return false; | 96 | if ( text.isNull() ) return false; |
97 | 97 | ||
98 | // TODO: write backup file | 98 | // TODO: write backup file |
99 | //is.restart(); | 99 | //is.restart(); |
100 | QFile file( fileName ); | 100 | QFile file( fileName ); |
101 | if (!file.open( IO_WriteOnly ) ) { | 101 | if (!file.open( QIODevice::WriteOnly ) ) { |
102 | setException(new ErrorFormat(ErrorFormat::SaveError, | 102 | setException(new ErrorFormat(ErrorFormat::SaveError, |
103 | i18n("Could not open file '%1'").arg(fileName))); | 103 | i18n("Could not open file '%1'").arg(fileName))); |
104 | return false; | 104 | return false; |
105 | } | 105 | } |
106 | QTextStream ts( &file ); | 106 | Q3TextStream ts( &file ); |
107 | 107 | ||
108 | ts.setEncoding( QTextStream::Latin1 ); | 108 | ts.setEncoding( Q3TextStream::Latin1 ); |
109 | ts << text; | 109 | ts << text; |
110 | file.close(); | 110 | file.close(); |
111 | //qDebug("saving file takes ms: %d ", is.elapsed() ); | 111 | //qDebug("saving file takes ms: %d ", is.elapsed() ); |
112 | return true; | 112 | return true; |
113 | } | 113 | } |
114 | 114 | ||
115 | bool ICalFormat::fromString( Calendar *cal, const QString &text ) | 115 | bool ICalFormat::fromString( Calendar *cal, const QString &text ) |
116 | { | 116 | { |
117 | setTimeZone( cal->timeZoneId(), !cal->isLocalTime() ); | 117 | setTimeZone( cal->timeZoneId(), !cal->isLocalTime() ); |
118 | // qDebug("ICalFormat::fromString tz: %s ", cal->timeZoneId().latin1()); | 118 | // qDebug("ICalFormat::fromString tz: %s ", cal->timeZoneId().latin1()); |
119 | // Get first VCALENDAR component. | 119 | // Get first VCALENDAR component. |
120 | // TODO: Handle more than one VCALENDAR or non-VCALENDAR top components | 120 | // TODO: Handle more than one VCALENDAR or non-VCALENDAR top components |
121 | icalcomponent *calendar; | 121 | icalcomponent *calendar; |
122 | 122 | ||
123 | //calendar = icalcomponent_new_from_string( text.local8Bit().data()); | 123 | //calendar = icalcomponent_new_from_string( text.local8Bit().data()); |
124 | // good calendar = icalcomponent_new_from_string( text.utf8().data()); | 124 | // good calendar = icalcomponent_new_from_string( text.utf8().data()); |
125 | calendar = icalcomponent_new_from_string( (char*)text.latin1()); | 125 | calendar = icalcomponent_new_from_string( (char*)text.latin1()); |
126 | if (!calendar) { | 126 | if (!calendar) { |
127 | setException(new ErrorFormat(ErrorFormat::ParseErrorIcal)); | 127 | setException(new ErrorFormat(ErrorFormat::ParseErrorIcal)); |
128 | return false; | 128 | return false; |
129 | } | 129 | } |
130 | 130 | ||
131 | bool success = true; | 131 | bool success = true; |
132 | 132 | ||
133 | if (icalcomponent_isa(calendar) != ICAL_VCALENDAR_COMPONENT) { | 133 | if (icalcomponent_isa(calendar) != ICAL_VCALENDAR_COMPONENT) { |
134 | setException(new ErrorFormat(ErrorFormat::NoCalendar)); | 134 | setException(new ErrorFormat(ErrorFormat::NoCalendar)); |
135 | success = false; | 135 | success = false; |
136 | } else { | 136 | } else { |
137 | // put all objects into their proper places | 137 | // put all objects into their proper places |
138 | if ( !mImpl->populate( cal, calendar ) ) { | 138 | if ( !mImpl->populate( cal, calendar ) ) { |
139 | if ( !exception() ) { | 139 | if ( !exception() ) { |
140 | setException(new ErrorFormat(ErrorFormat::ParseErrorKcal)); | 140 | setException(new ErrorFormat(ErrorFormat::ParseErrorKcal)); |
141 | } | 141 | } |
142 | success = false; | 142 | success = false; |
143 | } else | 143 | } else |
144 | mLoadedProductId = mImpl->loadedProductId(); | 144 | mLoadedProductId = mImpl->loadedProductId(); |
145 | } | 145 | } |
146 | 146 | ||
147 | icalcomponent_free( calendar ); | 147 | icalcomponent_free( calendar ); |
148 | icalmemory_free_ring(); | 148 | icalmemory_free_ring(); |
149 | 149 | ||
150 | return success; | 150 | return success; |
151 | } | 151 | } |
152 | 152 | ||
153 | Incidence *ICalFormat::fromString( const QString &text ) | 153 | Incidence *ICalFormat::fromString( const QString &text ) |
154 | { | 154 | { |
155 | CalendarLocal cal( mTimeZoneId ); | 155 | CalendarLocal cal( mTimeZoneId ); |
156 | fromString(&cal, text); | 156 | fromString(&cal, text); |
157 | 157 | ||
158 | Incidence *ical = 0; | 158 | Incidence *ical = 0; |
159 | QPtrList<Event> elist = cal.events(); | 159 | Q3PtrList<Event> elist = cal.events(); |
160 | if ( elist.count() > 0 ) { | 160 | if ( elist.count() > 0 ) { |
161 | ical = elist.first(); | 161 | ical = elist.first(); |
162 | } else { | 162 | } else { |
163 | QPtrList<Todo> tlist = cal.todos(); | 163 | Q3PtrList<Todo> tlist = cal.todos(); |
164 | if ( tlist.count() > 0 ) { | 164 | if ( tlist.count() > 0 ) { |
165 | ical = tlist.first(); | 165 | ical = tlist.first(); |
166 | } else { | 166 | } else { |
167 | QPtrList<Journal> jlist = cal.journals(); | 167 | Q3PtrList<Journal> jlist = cal.journals(); |
168 | if ( jlist.count() > 0 ) { | 168 | if ( jlist.count() > 0 ) { |
169 | ical = jlist.first(); | 169 | ical = jlist.first(); |
170 | } | 170 | } |
171 | } | 171 | } |
172 | } | 172 | } |
173 | return ical; | 173 | return ical; |
174 | } | 174 | } |
175 | #include <qapp.h> | 175 | #include <qapplication.h> |
176 | 176 | ||
177 | QString ICalFormat::toString( Calendar *cal ) | 177 | QString ICalFormat::toString( Calendar *cal ) |
178 | { | 178 | { |
179 | 179 | ||
180 | setTimeZone( cal->timeZoneId(), !cal->isLocalTime() ); | 180 | setTimeZone( cal->timeZoneId(), !cal->isLocalTime() ); |
181 | 181 | ||
182 | icalcomponent *calendar = mImpl->createCalendarComponent(cal); | 182 | icalcomponent *calendar = mImpl->createCalendarComponent(cal); |
183 | 183 | ||
184 | icalcomponent *component; | 184 | icalcomponent *component; |
185 | 185 | ||
186 | // todos | 186 | // todos |
187 | QPtrList<Todo> todoList = cal->rawTodos(); | 187 | Q3PtrList<Todo> todoList = cal->rawTodos(); |
188 | QPtrListIterator<Todo> qlt(todoList); | 188 | Q3PtrListIterator<Todo> qlt(todoList); |
189 | for (; qlt.current(); ++qlt) { | 189 | for (; qlt.current(); ++qlt) { |
190 | component = mImpl->writeTodo(qlt.current()); | 190 | component = mImpl->writeTodo(qlt.current()); |
191 | icalcomponent_add_component(calendar,component); | 191 | icalcomponent_add_component(calendar,component); |
192 | //qDebug(" todos "); | 192 | //qDebug(" todos "); |
193 | if ( mProcessEvents ) { | 193 | if ( mProcessEvents ) { |
194 | //qDebug("mProcessEvents "); | 194 | //qDebug("mProcessEvents "); |
195 | qApp->processEvents(); | 195 | qApp->processEvents(); |
196 | } | 196 | } |
197 | } | 197 | } |
198 | // events | 198 | // events |
199 | QPtrList<Event> events = cal->rawEvents(); | 199 | Q3PtrList<Event> events = cal->rawEvents(); |
200 | Event *ev; | 200 | Event *ev; |
201 | for(ev=events.first();ev;ev=events.next()) { | 201 | for(ev=events.first();ev;ev=events.next()) { |
202 | component = mImpl->writeEvent(ev); | 202 | component = mImpl->writeEvent(ev); |
203 | icalcomponent_add_component(calendar,component); | 203 | icalcomponent_add_component(calendar,component); |
204 | //qDebug("events "); | 204 | //qDebug("events "); |
205 | if ( mProcessEvents ) | 205 | if ( mProcessEvents ) |
206 | qApp->processEvents(); | 206 | qApp->processEvents(); |
207 | } | 207 | } |
208 | 208 | ||
209 | // journals | 209 | // journals |
210 | QPtrList<Journal> journals = cal->journals(); | 210 | Q3PtrList<Journal> journals = cal->journals(); |
211 | Journal *j; | 211 | Journal *j; |
212 | for(j=journals.first();j;j=journals.next()) { | 212 | for(j=journals.first();j;j=journals.next()) { |
213 | component = mImpl->writeJournal(j); | 213 | component = mImpl->writeJournal(j); |
214 | icalcomponent_add_component(calendar,component); | 214 | icalcomponent_add_component(calendar,component); |
215 | //qDebug("journals "); | 215 | //qDebug("journals "); |
216 | if ( mProcessEvents ) | 216 | if ( mProcessEvents ) |
217 | qApp->processEvents(); | 217 | qApp->processEvents(); |
218 | } | 218 | } |
219 | const char *text; | 219 | const char *text; |
220 | QString ret =""; | 220 | QString ret =""; |
221 | text = icalcomponent_as_ical_string( calendar ); | 221 | text = icalcomponent_as_ical_string( calendar ); |
222 | if ( mProcessEvents ) | 222 | if ( mProcessEvents ) |
223 | qApp->processEvents(); | 223 | qApp->processEvents(); |
224 | 224 | ||
225 | // text = "BEGIN:VCALENDAR\nPRODID\n :-//K Desktop Environment//NONSGML libkcal 3.1//EN\nVERSION\n :2.0\nBEGIN:VEVENT\nDTSTAMP\n :20031231T213514Z\nORGANIZER\n :MAILTO:lutz@putz.de\nCREATED\n :20031231T213513Z\nUID\n :libkcal-1295166342.120\nSEQUENCE\n :0\nLAST-MODIFIED\n :20031231T213513Z\nSUMMARY\n :test1\nCLASS\n :PUBLIC\nPRIORITY\n :3\nDTSTART\n :20040101T090000Z\nDTEND\n :20040101T110000Z\nTRANSP\n :OPAQUE\nEND:VEVENT\nEND:VCALENDAR\n"; | 225 | // text = "BEGIN:VCALENDAR\nPRODID\n :-//K Desktop Environment//NONSGML libkcal 3.1//EN\nVERSION\n :2.0\nBEGIN:VEVENT\nDTSTAMP\n :20031231T213514Z\nORGANIZER\n :MAILTO:lutz@putz.de\nCREATED\n :20031231T213513Z\nUID\n :libkcal-1295166342.120\nSEQUENCE\n :0\nLAST-MODIFIED\n :20031231T213513Z\nSUMMARY\n :test1\nCLASS\n :PUBLIC\nPRIORITY\n :3\nDTSTART\n :20040101T090000Z\nDTEND\n :20040101T110000Z\nTRANSP\n :OPAQUE\nEND:VEVENT\nEND:VCALENDAR\n"; |
226 | 226 | ||
227 | 227 | ||
228 | if ( text ) { | 228 | if ( text ) { |
229 | ret = QString ( text ); | 229 | ret = QString ( text ); |
230 | } | 230 | } |
231 | icalcomponent_free( calendar ); | 231 | icalcomponent_free( calendar ); |
232 | 232 | ||
233 | if (!text) { | 233 | if (!text) { |
234 | setException(new ErrorFormat(ErrorFormat::SaveError, | 234 | setException(new ErrorFormat(ErrorFormat::SaveError, |
235 | i18n("libical error"))); | 235 | i18n("libical error"))); |
236 | icalmemory_free_ring(); | 236 | icalmemory_free_ring(); |
237 | return QString::null; | 237 | return QString::null; |
238 | } | 238 | } |
239 | icalmemory_free_ring(); | 239 | icalmemory_free_ring(); |
240 | return ret; | 240 | return ret; |
241 | } | 241 | } |
242 | 242 | ||