lib: set ProbeResult's constructor to private
[barry.git] / opensync-plugin-0.4x / src / environment.cc
blob967eba8217ba1bf8f0c3d105adc193fd7698a7ff
1 //
2 // \file environment.cc
3 // Container / environment class for the sync module.
4 //
6 /*
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"
30 #include "trace.h"
31 #include <glib.h>
32 #include <string>
33 #include <fstream>
34 #include <sstream>
35 #include <iomanip>
36 #include <string.h>
37 #include <unistd.h>
40 //////////////////////////////////////////////////////////////////////////////
41 // DatabaseSyncState
43 DatabaseSyncState::DatabaseSyncState(OSyncPluginInfo *info, const char *description)
44 : m_dbId(0),
45 m_Sync(false),
46 m_Desc(description)
50 DatabaseSyncState::~DatabaseSyncState()
55 // Map2Uid
57 /// Converts record ID to string, since opensync 0.4x keeps the
58 /// same UID as we give it.
59 ///
60 std::string DatabaseSyncState::Map2Uid(uint32_t recordId) const
62 std::ostringstream oss;
63 oss << std::dec << recordId;
64 return oss.str();
69 //////////////////////////////////////////////////////////////////////////////
70 // BarryEnvironment Public API
72 BarryEnvironment::BarryEnvironment(OSyncPluginInfo *info):
73 info(info),
74 m_pin(-1),
75 m_DebugMode(false),
76 m_password(""),
77 m_IConverter("UTF-8"),
78 m_pCon(0),
79 m_pDesktop(0),
80 m_CalendarSync(info, "calendar"),
81 m_ContactsSync(info, "contacts"),
82 m_JournalSync(info, "note"),
83 m_TodoSync(info, "todo"),
84 m_NeedsReconnect(false)
88 BarryEnvironment::~BarryEnvironment()
90 delete m_pDesktop;
91 delete m_pCon;
94 void BarryEnvironment::DoConnect()
96 // Create controller
98 // Override the timeout due to a firmware issue... sometimes
99 // the firmware will hang during a Reconnect, and fail to
100 // respond to a Desktop::Open(). To work around this, we
101 // set the default timeout to 15 seconds so that we find this
102 // failure early enough to fix it within opensync's 30 second timeout.
103 // Then if we get such a timeout, we do the Reconnect again and
104 // hope for the best... this often fixes it.
106 if( !m_ProbeResult.get() )
107 throw std::logic_error("Tried to use empty ProbeResult");
108 m_pCon = new Barry::Controller(*m_ProbeResult, 15000);
109 m_pDesktop = new Barry::Mode::Desktop(*m_pCon, m_IConverter);
110 m_pDesktop->Open(m_password.c_str());
112 // Save the DBIDs and DBNames of the databases we will work with
113 if( m_CalendarSync.m_Sync ) {
114 m_CalendarSync.m_dbName = Barry::Calendar::GetDBName();
115 m_CalendarSync.m_dbId = m_pDesktop->GetDBID(Barry::Calendar::GetDBName());
118 if( m_ContactsSync.m_Sync ) {
119 m_ContactsSync.m_dbName = Barry::Contact::GetDBName();
120 m_ContactsSync.m_dbId = m_pDesktop->GetDBID(Barry::Contact::GetDBName());
123 if( m_JournalSync.m_Sync ) {
124 m_JournalSync.m_dbName = Barry::Memo::GetDBName();
125 m_JournalSync.m_dbId = m_pDesktop->GetDBID(Barry::Memo::GetDBName());
128 if( m_TodoSync.m_Sync ) {
129 m_TodoSync.m_dbName = Barry::Task::GetDBName();
130 m_TodoSync.m_dbId = m_pDesktop->GetDBID(Barry::Task::GetDBName());
134 void BarryEnvironment::Connect(const Barry::ProbeResult &result)
136 Disconnect();
138 // save result in case we need to reconnect later
139 m_ProbeResult.reset( new Barry::ProbeResult(result) );
141 DoConnect();
144 void BarryEnvironment::Reconnect()
146 int tries = 0;
148 while(1) try {
150 tries++;
152 Disconnect();
154 // let the device settle... this seems to help prevent the
155 // firmware hang, and therefore ultimately speeds up the sync
156 sleep(1);
158 // FIXME - temporary fix for odd reconnect message... without this
159 // probe, the reconnect will often fail on newer Blackberries
160 // due to an unexpected close socket message. It is unclear
161 // if this is really a message from the device, but until then,
162 // we add this probe.
164 Barry::Probe probe;
165 int i = probe.FindActive(m_ProbeResult->m_pin);
166 if( i != -1 )
167 m_ProbeResult.reset( new Barry::ProbeResult(probe.Get(i)) );
170 DoConnect();
172 return;
174 catch( Usb::Timeout & ) {
175 if( tries > 1 ) {
176 throw;
178 else {
179 std::cout << "Timeout in Reconnect()... trying again" << std::endl;
184 void BarryEnvironment::Disconnect()
186 delete m_pDesktop;
187 m_pDesktop = 0;
189 delete m_pCon;
190 m_pCon = 0;
192 m_NeedsReconnect = false;
195 bool BarryEnvironment::isConnected()
197 if (m_pCon != 0)
198 return true;
200 return false;
203 void BarryEnvironment::ReconnectForDirtyFlags()
205 if( m_NeedsReconnect ) {
206 // Reconnect resets the m_NeedsReconnect via Disconnect
207 Reconnect();
211 void BarryEnvironment::RequireDirtyReconnect()
213 m_NeedsReconnect = true;
216 void BarryEnvironment::ClearDirtyFlags(Barry::RecordStateTable &table,
217 const std::string &dbname)
219 Trace trace("ClearDirtyFlags");
221 unsigned int dbId = m_pDesktop->GetDBID(dbname);
223 Barry::RecordStateTable::StateMapType::const_iterator i = table.StateMap.begin();
224 for( ; i != table.StateMap.end(); ++i ) {
225 if( i->second.Dirty ) {
226 trace.logf("Clearing dirty flag for db %u, index %u",
227 dbId, i->first);
228 m_pDesktop->ClearDirty(dbId, i->first);
233 void BarryEnvironment::ClearCalendarDirtyFlags()
235 Trace trace("ClearCalendarDirtyFlags");
236 ClearDirtyFlags(m_CalendarSync.m_Table, Barry::Calendar::GetDBName());
239 void BarryEnvironment::ClearContactsDirtyFlags()
241 Trace trace("ClearContactsDirtyFlags");
242 ClearDirtyFlags(m_ContactsSync.m_Table, Barry::Contact::GetDBName());
245 void BarryEnvironment::ClearJournalDirtyFlags()
247 Trace trace("ClearJournalDirtyFlags");
248 ClearDirtyFlags(m_JournalSync.m_Table, Barry::Memo::GetDBName());
251 void BarryEnvironment::ClearTodoDirtyFlags()
253 Trace trace("ClearTodoDirtyFlags");
254 ClearDirtyFlags(m_TodoSync.m_Table, Barry::Task::GetDBName());
257 DatabaseSyncState* BarryEnvironment::GetSyncObject(OSyncChange *change)
259 Trace trace("BarryEnvironment::GetSyncObject()");
261 const char *name = osync_change_get_objtype(change);
263 trace.logf("osync_change_get_objtype returns %s", name);
265 if( strcmp(name, "event") == 0 ) {
266 trace.log("return calendar object");
268 return &m_CalendarSync;
270 else if( strcmp(name, "contact") == 0 ) {
271 trace.log("return contact object");
273 return &m_ContactsSync;
275 else if( strcmp(name, "note") == 0 ) {
276 trace.log("return journal object");
278 return &m_JournalSync;
280 else if( strcmp(name, "todo") == 0 ) {
281 trace.log("return todo object");
283 return &m_TodoSync;
286 trace.log("return none");
288 return 0;