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
|
/*
* This file is part of buteo-sync-plugins package
*
* Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
*
* 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 "SyncMLStorageProvider.h"
#include <buteosyncfw5/Profile.h>
#include <buteosyncfw5/ProfileEngineDefs.h>
#include <buteosyncfw5/PluginCbInterface.h>
#include <buteosyncfw5/StoragePlugin.h>
#include <buteosyncml5/StoragePlugin.h>
#include <buteosyncml5/SessionHandler.h>
#include "SyncMLCommon.h"
#include "StorageAdapter.h"
#include "SyncMLPluginLogging.h"
SyncMLStorageProvider::SyncMLStorageProvider()
: iProfile( 0 ), iPlugin( 0 ), iCbInterface( 0 ), iRequestStorages( false )
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
}
SyncMLStorageProvider::~SyncMLStorageProvider()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
}
bool SyncMLStorageProvider::init( Buteo::Profile* aProfile,
Buteo::SyncPluginBase* aPlugin,
Buteo::PluginCbInterface* aCbInterface,
bool aRequestStorages )
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
if( !aProfile || !aPlugin || !aCbInterface ) {
qCCritical(lcSyncMLPlugin) << "NULL parameters passed to init()";
return false;
}
iProfile = aProfile;
iPlugin = aPlugin;
iCbInterface = aCbInterface;
iRequestStorages = aRequestStorages;
return true;
}
bool SyncMLStorageProvider::uninit()
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
return true;
}
QString SyncMLStorageProvider::getPreferredURINames( const QString &aURI )
{
Q_UNUSED(aURI);
// TODO : Handle possible multiple URI names in storage profiles
// One way to do this is to have separators for Local URI, like ./notes;./Notepad
return QString();
}
bool SyncMLStorageProvider::getStorageContentFormatInfo( const QString& aURI,
DataSync::StorageContentFormatInfo& aInfo )
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
const Buteo::Profile* storageProfile =
iProfile->subProfileByKeyValue( STORAGE_SOURCE_URI, aURI,
Buteo::Profile::TYPE_STORAGE, true );
if( !storageProfile ) {
qCDebug(lcSyncMLPlugin) << "Could not find storage for URI" << aURI;
return false;
}
QString preferredFormat = storageProfile->key( STORAGE_DEFAULT_MIME_PROP );
QString preferredVersion = storageProfile->key( STORAGE_DEFAULT_MIME_VERSION_PROP );
// Currently we support only one format per storage
DataSync::ContentFormat format;
format.iType = preferredFormat;
format.iVersion = preferredVersion;
aInfo.setPreferredRx( format );
aInfo.setPreferredTx( format );
aInfo.rx().append( format );
aInfo.tx().append( format );
return true;
}
DataSync::StoragePlugin* SyncMLStorageProvider::acquireStorageByURI( const QString& aURI )
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
qCDebug(lcSyncMLPlugin) << "Incoming request to acquire storage by URI:" << aURI;
#if 0
QString preferredURI = getPreferredURINames( aURI );
#endif
const Buteo::Profile* storageProfile =
iProfile->subProfileByKeyValue( STORAGE_SOURCE_URI, aURI,
Buteo::Profile::TYPE_STORAGE, true );
if( !storageProfile ) {
qCDebug(lcSyncMLPlugin) << "Could not find storage for URI" << aURI;
return NULL;
}
qCDebug(lcSyncMLPlugin) << "Found storage for URI" << aURI << ":" << storageProfile->name();
return acquireStorage( storageProfile );
}
DataSync::StoragePlugin* SyncMLStorageProvider::acquireStorageByMIME( const QString& aMIME )
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
qCDebug(lcSyncMLPlugin) << "Incoming request to acquire storage by MIME:" << aMIME;
const Buteo::Profile* storageProfile =
iProfile->subProfileByKeyValue( STORAGE_DEFAULT_MIME_PROP, aMIME,
Buteo::Profile::TYPE_STORAGE, true );
if( !storageProfile ) {
qCDebug(lcSyncMLPlugin) << "Could not find storage for MIME" << aMIME;
return NULL;
}
qCDebug(lcSyncMLPlugin) << "Found storage for MIME" << aMIME << ":" << storageProfile->name();
return acquireStorage( storageProfile );
}
void SyncMLStorageProvider::releaseStorage( DataSync::StoragePlugin* aStorage )
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
if (!aStorage)
return;
qCDebug(lcSyncMLPlugin) << "Incoming request to release storage with source URI" << aStorage->getSourceURI();
StorageAdapter* adapter = static_cast<StorageAdapter*>( aStorage );
if( !adapter->uninit() ) {
qCWarning(lcSyncMLPlugin) << "Storage adapter uninitialization failed";
}
Buteo::StoragePlugin* storage = adapter->getPlugin();
QString backend = storage->getProperty( Buteo::KEY_BACKEND );
if( !storage->uninit() ) {
qCWarning(lcSyncMLPlugin) << "Storage uninitialization failed";
}
iCbInterface->destroyStorage( storage );
if( iRequestStorages ) {
iCbInterface->releaseStorage( backend, iPlugin );
}
delete aStorage;
}
DataSync::StoragePlugin* SyncMLStorageProvider::acquireStorage( const Buteo::Profile* aProfile )
{
FUNCTION_CALL_TRACE(lcSyncMLPluginTrace);
if (!aProfile)
return NULL;
QString backend = aProfile->key( Buteo::KEY_BACKEND, aProfile->name() );
QString pluginName = aProfile->key( Buteo::KEY_PLUGIN, aProfile->name() );
QString uuid = iProfile->key(Buteo::KEY_UUID);
QString remoteName = iProfile->key(Buteo::KEY_REMOTE_NAME);
if(!uuid.isEmpty() && !remoteName.isEmpty())
{
qCDebug(lcSyncMLPlugin) << "uuid and remote name fetched from profile" << uuid << remoteName;
}
if(!iUUID.isEmpty() && !iRemoteName.isEmpty())
{
uuid = iUUID;
remoteName = iRemoteName;
qCDebug(lcSyncMLPlugin) << "uuid and remote name created on the fly" << uuid << remoteName;
}
if( iRequestStorages && !iCbInterface->requestStorage( backend, iPlugin ) ) {
qCCritical(lcSyncMLPlugin) << "Could not reserve storage backend:" << backend;
}
Buteo::StoragePlugin* storage = iCbInterface->createStorage( pluginName );
if( !storage ) {
iCbInterface->releaseStorage( backend, iPlugin );
qCDebug(lcSyncMLPlugin) << "Could not create storage:" << pluginName;
return NULL;
}
// Make sure that the backend name that was used in storage reservation is
// saved to the properties of storage plug-in. This name must be used again
// when the storage backend is released.
QMap<QString, QString> keys = aProfile->allKeys();
keys.insert( Buteo::KEY_BACKEND, backend );
if(!uuid.isEmpty())
{
keys.insert(Buteo::KEY_UUID, uuid);
}
if(!remoteName.isEmpty())
{
keys.insert(Buteo::KEY_REMOTE_NAME, remoteName);
}
// Configure the storage syncTarget and originId if known
if (!iProfile->key(Buteo::KEY_BT_ADDRESS).isEmpty()) {
keys.insert(STORAGE_SYNC_TARGET, STORAGE_SYNC_TARGET_BLUETOOTH);
keys.insert(STORAGE_ORIGIN_ID, iProfile->key(Buteo::KEY_BT_ADDRESS));
}
// If protocol version is not defined in the keys read from profile, try to
// read the version from the session handler and insert a corresponding key,
// so that storage plug-in knows which protocol version is in use.
if (!keys.contains(PROF_SYNC_PROTOCOL) && iSessionHandler != 0)
{
if (iSessionHandler->getProtocolVersion() == DataSync::SYNCML_1_1)
{
keys[PROF_SYNC_PROTOCOL] = SYNCML11;
}
else
{
keys[PROF_SYNC_PROTOCOL] = SYNCML12;
}
}
if( !storage->init( keys ) ) {
qCDebug(lcSyncMLPlugin) << "Could not initialize storage:" << pluginName;
iCbInterface->destroyStorage( storage );
iCbInterface->releaseStorage( backend, iPlugin );
return NULL;
}
StorageAdapter* adapter = new StorageAdapter( storage );
if( !adapter->init() ) {
qCDebug(lcSyncMLPlugin) << "Initialization of adapter for storage" << pluginName << "FAILED";
iCbInterface->destroyStorage( storage );
iCbInterface->releaseStorage( backend, iPlugin );
delete adapter;
return NULL;
}
return adapter;
}
void SyncMLStorageProvider::setRemoteName(const QString& aRemoteName)
{
iRemoteName = aRemoteName;
}
void SyncMLStorageProvider::setUUID(const QString& aUUID)
{
iUUID = aUUID;
}
|