File: NotesStorage.cpp

package info (click to toggle)
buteo-sync-plugins 0.8.36-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,076 kB
  • sloc: cpp: 8,130; xml: 755; sh: 207; perl: 82; makefile: 11
file content (311 lines) | stat: -rw-r--r-- 9,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * This file is part of buteo-sync-plugins package
 *
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * Copyright (C) 2013 - 2021 Jolla Ltd.
 *
 * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "NotesStorage.h"

#include <QFile>
#include <QStringListIterator>

#include <buteosyncfw5/StorageItem.h>

#include "SyncMLPluginLogging.h"

#include "SyncMLCommon.h"
#include "SyncMLConfig.h"

// @todo: Because CalendarMaemo does not support batched operations ( or it does
//        but we can't use it as we cannot retrieve the id's of committed items ),
//        batched operations are currently done in series.

const char* CTCAPSFILENAME11        = "CTCaps_notes_11.xml";
const char* CTCAPSFILENAME12        = "CTCaps_notes_12.xml";

const char* STORAGE_NOTEBOOK_PROP   = "Notebook Name";

const char* DEFAULT_TYPE            = "text/plain";
const char* DEFAULT_TYPE_VERSION    = "1.0";
const char* DEFAULT_NOTEBOOK        = "Personal";
const char* DEFAULT_NOTEBOOK_NAME   = "myNotebook";


NotesStorage::NotesStorage( const QString& aPluginName ) : Buteo::StoragePlugin( aPluginName ), iCommitNow( true )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
}

NotesStorage::~NotesStorage()
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
}


bool NotesStorage::init( const QMap<QString, QString>& aProperties )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    iProperties                                = aProperties;
    iProperties[STORAGE_SYNCML_CTCAPS_PROP_11] = getCTCaps( CTCAPSFILENAME11 );
    iProperties[STORAGE_SYNCML_CTCAPS_PROP_12] = getCTCaps( CTCAPSFILENAME12 );

    // Use remote name (e.g. bt name) as notebook name.
    if(iProperties.contains(Buteo::KEY_REMOTE_NAME)) {
        qCDebug(lcSyncMLPlugin) << "Using remote name as notebook name";
        iProperties[STORAGE_NOTEBOOK_PROP] = iProperties.value(Buteo::KEY_REMOTE_NAME);
    }
    else if( iProperties.value( STORAGE_NOTEBOOK_PROP ).isEmpty() ) {
        qCWarning(lcSyncMLPlugin) << STORAGE_NOTEBOOK_PROP << " property not found" <<
                     "for notes storage, using default of" <<
                     DEFAULT_NOTEBOOK_NAME;
        iProperties[STORAGE_NOTEBOOK_PROP] = DEFAULT_NOTEBOOK_NAME;
    }
    qCDebug(lcSyncMLPlugin) << "Initializing notes, notebook name:" <<  iProperties[STORAGE_NOTEBOOK_PROP]; 


    if( iProperties.value( STORAGE_DEFAULT_MIME_PROP ).isEmpty() ) {
        qCWarning(lcSyncMLPlugin) << STORAGE_DEFAULT_MIME_PROP << "property not found"
                     << "for notes storage, using default of" << DEFAULT_TYPE;
        iProperties[STORAGE_DEFAULT_MIME_PROP] = DEFAULT_TYPE;
    }

    if( iProperties.value( STORAGE_DEFAULT_MIME_VERSION_PROP ).isEmpty() ) {
        qCWarning(lcSyncMLPlugin) << STORAGE_DEFAULT_MIME_VERSION_PROP << " property not found"
                     <<"for notes storage, using default of" << DEFAULT_TYPE_VERSION;
        iProperties[STORAGE_DEFAULT_MIME_VERSION_PROP] = DEFAULT_TYPE_VERSION;
    }

    if( iProperties.value( STORAGE_NOTEBOOK_PROP ).isEmpty() ) {
        qCWarning(lcSyncMLPlugin) << STORAGE_NOTEBOOK_PROP << " property not found"
                     << "for notes storage, using default of" << DEFAULT_NOTEBOOK;
        iProperties[STORAGE_NOTEBOOK_PROP] = DEFAULT_NOTEBOOK;
    }

    return iBackend.init( iProperties[STORAGE_NOTEBOOK_PROP], iProperties[Buteo::KEY_NOTES_UUID],
        iProperties[STORAGE_DEFAULT_MIME_PROP] );
}

bool NotesStorage::uninit()
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    return iBackend.uninit();
}

bool NotesStorage::getAllItems( QList<Buteo::StorageItem*>& aItems )
{
    return iBackend.getAllNotes( aItems );
}

bool NotesStorage::getAllItemIds( QList<QString>& aItemIds )
{
    return iBackend.getAllNoteIds( aItemIds );
}

bool NotesStorage::getNewItems( QList<Buteo::StorageItem*>& aNewItems, const QDateTime& aTime )
{
    return iBackend.getNewNotes( aNewItems, normalizeTime( aTime ) );
}

bool NotesStorage::getNewItemIds( QList<QString>& aNewItemIds, const QDateTime& aTime )
{
    return iBackend.getNewNoteIds( aNewItemIds, normalizeTime( aTime ) );
}

bool NotesStorage::getModifiedItems( QList<Buteo::StorageItem*>& aModifiedItems, const QDateTime& aTime )
{
    return iBackend.getModifiedNotes( aModifiedItems, normalizeTime( aTime ) );
}

bool NotesStorage::getModifiedItemIds( QList<QString>& aModifiedItemIds, const QDateTime& aTime )
{
    return iBackend.getModifiedNoteIds( aModifiedItemIds, normalizeTime( aTime ) );
}

bool NotesStorage::getDeletedItemIds( QList<QString>& aDeletedItemIds, const QDateTime& aTime )
{
    return iBackend.getDeletedNoteIds( aDeletedItemIds, normalizeTime( aTime ) );
}

Buteo::StorageItem* NotesStorage::newItem()
{
    return iBackend.newItem();
}

QList<Buteo::StorageItem*> NotesStorage::getItems( const QStringList& aItemIdList )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QList<Buteo::StorageItem*> items;
    QStringListIterator itr( aItemIdList );

    while( itr.hasNext() )
    {
        items.append( iBackend.getItem( itr.next() ) );
    }

    return items;
}

Buteo::StorageItem* NotesStorage::getItem( const QString& aItemId )
{
    return iBackend.getItem( aItemId );
}

Buteo::StoragePlugin::OperationStatus NotesStorage::addItem( Buteo::StorageItem& aItem )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    if( iBackend.addNote( aItem, iCommitNow ) ) {
        return STATUS_OK;
    }
    else {
        return STATUS_ERROR;
    }

}

QList<Buteo::StoragePlugin::OperationStatus> NotesStorage::addItems( const QList<Buteo::StorageItem*>& aItems )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QList<OperationStatus> results;

    //Commit once at the end of the batch update
    iCommitNow = false;

    for( int i = 0; i < aItems.count(); ++i ) {
        results.append( addItem( *aItems[i] ) );
    }

    iCommitNow = true;
    bool saved = iBackend.commitChanges();
    Q_UNUSED( saved );

    return results;
}

Buteo::StoragePlugin::OperationStatus NotesStorage::modifyItem( Buteo::StorageItem& aItem )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    if( iBackend.modifyNote( aItem, iCommitNow ) ) {
        return STATUS_OK;
    }
    else {
        return STATUS_ERROR;
    }

}

QList<Buteo::StoragePlugin::OperationStatus> NotesStorage::modifyItems( const QList<Buteo::StorageItem*>& aItems )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QList<OperationStatus> results;

    //Commit once at the end of the batch update
    iCommitNow = false;

    for( int i = 0; i < aItems.count(); ++i ) {
        results.append( modifyItem( *aItems[i] ) );
    }

    iCommitNow = true;
    bool saved = iBackend.commitChanges();
    Q_UNUSED( saved );

    return results;
}

Buteo::StoragePlugin::OperationStatus NotesStorage::deleteItem( const QString& aItemId )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    if( iBackend.deleteNote( aItemId, iCommitNow ) ) {
        return STATUS_OK;
    }
    else {
        return STATUS_ERROR;
    }

}

QList<Buteo::StoragePlugin::OperationStatus> NotesStorage::deleteItems( const QList<QString>& aItemIds )
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QList<OperationStatus> results;

    //Commit once at the end of the batch update
    iCommitNow = false;

    for( int i = 0; i < aItemIds.count(); ++i ) {
        results.append( deleteItem( aItemIds[i] ) );
    }

    iCommitNow = true;
    bool saved = iBackend.commitChanges();
    Q_UNUSED( saved );

    return results;
}

QDateTime NotesStorage::normalizeTime( const QDateTime& aTime ) const
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QDateTime normTime = aTime;

    QTime time = aTime.time();
    time.setHMS( time.hour(), time.minute(), time.second(), 0 );

    normTime.setTime( time );

    normTime = normTime.toUTC();

    return normTime;
}

QByteArray NotesStorage::getCTCaps( const QString& aFilename ) const
{
    FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);

    QFile ctCapsFile( SyncMLConfig::getXmlDataPath() + aFilename  );
    QByteArray ctCaps;

    if( ctCapsFile.open(QIODevice::ReadOnly)) {
       ctCaps = ctCapsFile.readAll();
       ctCapsFile.close();
    } else {
        qCWarning(lcSyncMLPlugin) << "Failed to open CTCaps file for notes storage:" << aFilename;
    }

    return ctCaps;

}


Buteo::StoragePlugin* NotesStoragePluginLoader::createPlugin(const QString& aPluginName)
{
    return new NotesStorage(aPluginName);
}