2 // \file environment.cc
3 // Container / environment class for the sync module.
7 Copyright (C) 2006-2010, 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 #include <opensync/opensync.h>
23 #include <opensync/opensync-data.h>
24 #include <opensync/opensync-format.h>
25 #include <opensync/opensync-plugin.h>
26 #include <opensync/opensync-helper.h>
27 #include <opensync/opensync-version.h>
29 #include "environment.h"
39 //////////////////////////////////////////////////////////////////////////////
42 DatabaseSyncState::DatabaseSyncState(OSyncPluginInfo
*info
, const char *description
)
49 DatabaseSyncState::~DatabaseSyncState()
56 /// Converts record ID to string, since opensync 0.4x keeps the
57 /// same UID as we give it.
59 std::string
DatabaseSyncState::Map2Uid(uint32_t recordId
) const
61 std::ostringstream oss
;
62 oss
<< std::dec
<< recordId
;
68 //////////////////////////////////////////////////////////////////////////////
69 // BarryEnvironment Public API
71 BarryEnvironment::BarryEnvironment(OSyncPluginInfo
*info
):
76 m_IConverter("UTF-8"),
79 m_CalendarSync(info
, "calendar"),
80 m_ContactsSync(info
, "contacts"),
81 m_JournalSync(info
, "note"),
82 m_TodoSync(info
, "todo")
86 BarryEnvironment::~BarryEnvironment()
92 void BarryEnvironment::DoConnect()
95 m_pCon
= new Barry::Controller(m_ProbeResult
);
96 m_pDesktop
= new Barry::Mode::Desktop(*m_pCon
, m_IConverter
);
97 m_pDesktop
->Open(m_password
.c_str());
99 // Save the DBIDs and DBNames of the databases we will work with
100 if( m_CalendarSync
.m_Sync
) {
101 m_CalendarSync
.m_dbName
= Barry::Calendar::GetDBName();
102 m_CalendarSync
.m_dbId
= m_pDesktop
->GetDBID(Barry::Calendar::GetDBName());
105 if( m_ContactsSync
.m_Sync
) {
106 m_ContactsSync
.m_dbId
= m_pDesktop
->GetDBID(Barry::Contact::GetDBName());
107 m_ContactsSync
.m_dbName
= Barry::Contact::GetDBName();
110 if( m_JournalSync
.m_Sync
) {
111 m_JournalSync
.m_dbId
= m_pDesktop
->GetDBID(Barry::Memo::GetDBName());
112 m_JournalSync
.m_dbName
= Barry::Memo::GetDBName();
115 if( m_TodoSync
.m_Sync
) {
116 m_TodoSync
.m_dbId
= m_pDesktop
->GetDBID(Barry::Task::GetDBName());
117 m_TodoSync
.m_dbName
= Barry::Task::GetDBName();
121 void BarryEnvironment::Connect(const Barry::ProbeResult
&result
)
125 // save result in case we need to reconnect later
126 m_ProbeResult
= result
;
131 void BarryEnvironment::Reconnect()
135 // FIXME - temporary fix for odd reconnect message... without this
136 // probe, the reconnect will often fail on newer Blackberries
137 // due to an unexpected close socket message. It is unclear
138 // if this is really a message from the device, but until then,
139 // we add this probe.
142 int i
= probe
.FindActive(m_ProbeResult
.m_pin
);
144 m_ProbeResult
= probe
.Get(i
);
150 void BarryEnvironment::Disconnect()
159 bool BarryEnvironment::isConnected()
167 void BarryEnvironment::ClearDirtyFlags(Barry::RecordStateTable
&table
,
168 const std::string
&dbname
)
170 Trace
trace("ClearDirtyFlags");
172 unsigned int dbId
= m_pDesktop
->GetDBID(dbname
);
174 Barry::RecordStateTable::StateMapType::const_iterator i
= table
.StateMap
.begin();
175 for( ; i
!= table
.StateMap
.end(); ++i
) {
176 if( i
->second
.Dirty
) {
177 trace
.logf("Clearing dirty flag for db %u, index %u",
179 m_pDesktop
->ClearDirty(dbId
, i
->first
);
184 void BarryEnvironment::ClearCalendarDirtyFlags()
186 Trace
trace("ClearCalendarDirtyFlags");
187 ClearDirtyFlags(m_CalendarSync
.m_Table
, Barry::Calendar::GetDBName());
190 void BarryEnvironment::ClearContactsDirtyFlags()
192 Trace
trace("ClearContactsDirtyFlags");
193 ClearDirtyFlags(m_ContactsSync
.m_Table
, Barry::Contact::GetDBName());
196 void BarryEnvironment::ClearJournalDirtyFlags()
198 Trace
trace("ClearJournalDirtyFlags");
199 ClearDirtyFlags(m_JournalSync
.m_Table
, Barry::Memo::GetDBName());
202 void BarryEnvironment::ClearTodoDirtyFlags()
204 Trace
trace("ClearTodoDirtyFlags");
205 ClearDirtyFlags(m_TodoSync
.m_Table
, Barry::Task::GetDBName());
208 DatabaseSyncState
* BarryEnvironment::GetSyncObject(OSyncChange
*change
)
210 Trace
trace("BarryEnvironment::GetSyncObject()");
212 const char *name
= osync_change_get_objtype(change
);
214 trace
.logf("osync_change_get_objtype returns %s", name
);
216 if( strcmp(name
, "event") == 0 ) {
217 trace
.log("return calendar object");
219 return &m_CalendarSync
;
221 else if( strcmp(name
, "contact") == 0 ) {
222 trace
.log("return contact object");
224 return &m_ContactsSync
;
226 else if( strcmp(name
, "note") == 0 ) {
227 trace
.log("return journal object");
229 return &m_JournalSync
;
231 else if( strcmp(name
, "todo") == 0 ) {
232 trace
.log("return todo object");
237 trace
.log("return none");