Debian Codegraph @

storageplugins/hnotes/NotesStorage.cpp

raw
1 /*
2 * This file is part of buteo-sync-plugins package
3 *
4 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 * Copyright (C) 2013 - 2021 Jolla Ltd.
6 *
7 * Contact: Sateesh Kavuri <[email protected]>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1 as published by the Free Software Foundation.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25 #include "NotesStorage.h"
26
27 #include <QFile>
28 #include <QStringListIterator>
29
30 #include <buteosyncfw5/StorageItem.h>
31
32 #include "SyncMLPluginLogging.h"
33
34 #include "SyncMLCommon.h"
35 #include "SyncMLConfig.h"
36
37 // @todo: Because CalendarMaemo does not support batched operations ( or it does
38 // but we can't use it as we cannot retrieve the id's of committed items ),
39 // batched operations are currently done in series.
40
41 const char* CTCAPSFILENAME11 = "CTCaps_notes_11.xml";
42 const char* CTCAPSFILENAME12 = "CTCaps_notes_12.xml";
43
44 const char* STORAGE_NOTEBOOK_PROP = "Notebook Name";
45
46 const char* DEFAULT_TYPE = "text/plain";
47 const char* DEFAULT_TYPE_VERSION = "1.0";
48 const char* DEFAULT_NOTEBOOK = "Personal";
49 const char* DEFAULT_NOTEBOOK_NAME = "myNotebook";
50
51
52 NotesStorage::NotesStorage( const QString& aPluginName ) : Buteo::StoragePlugin( aPluginName ), iCommitNow( true )
53 {
54 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
55 }
56
57 NotesStorage::~NotesStorage()
58 {
59 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
60 }
61
62
63 bool NotesStorage::init( const QMap<QString, QString>& aProperties )
64 {
65 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
66
67 iProperties = aProperties;
68 iProperties[STORAGE_SYNCML_CTCAPS_PROP_11] = getCTCaps( CTCAPSFILENAME11 );
69 iProperties[STORAGE_SYNCML_CTCAPS_PROP_12] = getCTCaps( CTCAPSFILENAME12 );
70
71 // Use remote name (e.g. bt name) as notebook name.
72 if(iProperties.contains(Buteo::KEY_REMOTE_NAME)) {
73 qCDebug(lcSyncMLPlugin) << "Using remote name as notebook name";
74 iProperties[STORAGE_NOTEBOOK_PROP] = iProperties.value(Buteo::KEY_REMOTE_NAME);
75 }
76 else if( iProperties.value( STORAGE_NOTEBOOK_PROP ).isEmpty() ) {
77 qCWarning(lcSyncMLPlugin) << STORAGE_NOTEBOOK_PROP << " property not found" <<
78 "for notes storage, using default of" <<
79 DEFAULT_NOTEBOOK_NAME;
80 iProperties[STORAGE_NOTEBOOK_PROP] = DEFAULT_NOTEBOOK_NAME;
81 }
82 qCDebug(lcSyncMLPlugin) << "Initializing notes, notebook name:" << iProperties[STORAGE_NOTEBOOK_PROP];
83
84
85 if( iProperties.value( STORAGE_DEFAULT_MIME_PROP ).isEmpty() ) {
86 qCWarning(lcSyncMLPlugin) << STORAGE_DEFAULT_MIME_PROP << "property not found"
87 << "for notes storage, using default of" << DEFAULT_TYPE;
88 iProperties[STORAGE_DEFAULT_MIME_PROP] = DEFAULT_TYPE;
89 }
90
91 if( iProperties.value( STORAGE_DEFAULT_MIME_VERSION_PROP ).isEmpty() ) {
92 qCWarning(lcSyncMLPlugin) << STORAGE_DEFAULT_MIME_VERSION_PROP << " property not found"
93 <<"for notes storage, using default of" << DEFAULT_TYPE_VERSION;
94 iProperties[STORAGE_DEFAULT_MIME_VERSION_PROP] = DEFAULT_TYPE_VERSION;
95 }
96
97 if( iProperties.value( STORAGE_NOTEBOOK_PROP ).isEmpty() ) {
98 qCWarning(lcSyncMLPlugin) << STORAGE_NOTEBOOK_PROP << " property not found"
99 << "for notes storage, using default of" << DEFAULT_NOTEBOOK;
100 iProperties[STORAGE_NOTEBOOK_PROP] = DEFAULT_NOTEBOOK;
101 }
102
103 return iBackend.init( iProperties[STORAGE_NOTEBOOK_PROP], iProperties[Buteo::KEY_NOTES_UUID],
104 iProperties[STORAGE_DEFAULT_MIME_PROP] );
105 }
106
107 bool NotesStorage::uninit()
108 {
109 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
110
111 return iBackend.uninit();
112 }
113
114 bool NotesStorage::getAllItems( QList<Buteo::StorageItem*>& aItems )
115 {
116 return iBackend.getAllNotes( aItems );
117 }
118
119 bool NotesStorage::getAllItemIds( QList<QString>& aItemIds )
120 {
121 return iBackend.getAllNoteIds( aItemIds );
122 }
123
124 bool NotesStorage::getNewItems( QList<Buteo::StorageItem*>& aNewItems, const QDateTime& aTime )
125 {
126 return iBackend.getNewNotes( aNewItems, normalizeTime( aTime ) );
127 }
128
129 bool NotesStorage::getNewItemIds( QList<QString>& aNewItemIds, const QDateTime& aTime )
130 {
131 return iBackend.getNewNoteIds( aNewItemIds, normalizeTime( aTime ) );
132 }
133
134 bool NotesStorage::getModifiedItems( QList<Buteo::StorageItem*>& aModifiedItems, const QDateTime& aTime )
135 {
136 return iBackend.getModifiedNotes( aModifiedItems, normalizeTime( aTime ) );
137 }
138
139 bool NotesStorage::getModifiedItemIds( QList<QString>& aModifiedItemIds, const QDateTime& aTime )
140 {
141 return iBackend.getModifiedNoteIds( aModifiedItemIds, normalizeTime( aTime ) );
142 }
143
144 bool NotesStorage::getDeletedItemIds( QList<QString>& aDeletedItemIds, const QDateTime& aTime )
145 {
146 return iBackend.getDeletedNoteIds( aDeletedItemIds, normalizeTime( aTime ) );
147 }
148
149 Buteo::StorageItem* NotesStorage::newItem()
150 {
151 return iBackend.newItem();
152 }
153
154 QList<Buteo::StorageItem*> NotesStorage::getItems( const QStringList& aItemIdList )
155 {
156 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
157
158 QList<Buteo::StorageItem*> items;
159 QStringListIterator itr( aItemIdList );
160
161 while( itr.hasNext() )
162 {
163 items.append( iBackend.getItem( itr.next() ) );
164 }
165
166 return items;
167 }
168
169 Buteo::StorageItem* NotesStorage::getItem( const QString& aItemId )
170 {
171 return iBackend.getItem( aItemId );
172 }
173
174 Buteo::StoragePlugin::OperationStatus NotesStorage::addItem( Buteo::StorageItem& aItem )
175 {
176 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
177
178 if( iBackend.addNote( aItem, iCommitNow ) ) {
179 return STATUS_OK;
180 }
181 else {
182 return STATUS_ERROR;
183 }
184
185 }
186
187 QList<Buteo::StoragePlugin::OperationStatus> NotesStorage::addItems( const QList<Buteo::StorageItem*>& aItems )
188 {
189 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
190
191 QList<OperationStatus> results;
192
193 //Commit once at the end of the batch update
194 iCommitNow = false;
195
196 for( int i = 0; i < aItems.count(); ++i ) {
197 results.append( addItem( *aItems[i] ) );
198 }
199
200 iCommitNow = true;
201 bool saved = iBackend.commitChanges();
202 Q_UNUSED( saved );
203
204 return results;
205 }
206
207 Buteo::StoragePlugin::OperationStatus NotesStorage::modifyItem( Buteo::StorageItem& aItem )
208 {
209 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
210
211 if( iBackend.modifyNote( aItem, iCommitNow ) ) {
212 return STATUS_OK;
213 }
214 else {
215 return STATUS_ERROR;
216 }
217
218 }
219
220 QList<Buteo::StoragePlugin::OperationStatus> NotesStorage::modifyItems( const QList<Buteo::StorageItem*>& aItems )
221 {
222 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
223
224 QList<OperationStatus> results;
225
226 //Commit once at the end of the batch update
227 iCommitNow = false;
228
229 for( int i = 0; i < aItems.count(); ++i ) {
230 results.append( modifyItem( *aItems[i] ) );
231 }
232
233 iCommitNow = true;
234 bool saved = iBackend.commitChanges();
235 Q_UNUSED( saved );
236
237 return results;
238 }
239
240 Buteo::StoragePlugin::OperationStatus NotesStorage::deleteItem( const QString& aItemId )
241 {
242 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
243
244 if( iBackend.deleteNote( aItemId, iCommitNow ) ) {
245 return STATUS_OK;
246 }
247 else {
248 return STATUS_ERROR;
249 }
250
251 }
252
253 QList<Buteo::StoragePlugin::OperationStatus> NotesStorage::deleteItems( const QList<QString>& aItemIds )
254 {
255 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
256
257 QList<OperationStatus> results;
258
259 //Commit once at the end of the batch update
260 iCommitNow = false;
261
262 for( int i = 0; i < aItemIds.count(); ++i ) {
263 results.append( deleteItem( aItemIds[i] ) );
264 }
265
266 iCommitNow = true;
267 bool saved = iBackend.commitChanges();
268 Q_UNUSED( saved );
269
270 return results;
271 }
272
273 QDateTime NotesStorage::normalizeTime( const QDateTime& aTime ) const
274 {
275 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
276
277 QDateTime normTime = aTime;
278
279 QTime time = aTime.time();
280 time.setHMS( time.hour(), time.minute(), time.second(), 0 );
281
282 normTime.setTime( time );
283
284 normTime = normTime.toUTC();
285
286 return normTime;
287 }
288
289 QByteArray NotesStorage::getCTCaps( const QString& aFilename ) const
290 {
291 FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
292
293 QFile ctCapsFile( SyncMLConfig::getXmlDataPath() + aFilename );
294 QByteArray ctCaps;
295
296 if( ctCapsFile.open(QIODevice::ReadOnly)) {
297 ctCaps = ctCapsFile.readAll();
298 ctCapsFile.close();
299 } else {
300 qCWarning(lcSyncMLPlugin) << "Failed to open CTCaps file for notes storage:" << aFilename;
301 }
302
303 return ctCaps;
304
305 }
306
307
308 Buteo::StoragePlugin* NotesStoragePluginLoader::createPlugin(const QString& aPluginName)
309 {
310 return new NotesStorage(aPluginName);
311 }