os4x: fixed bug in connect that would connect to a DB even if not enabled
[barry.git] / opensync-plugin-0.4x / src / environment.cc
blobac973273b4e22903118e550946cfa7bd7ce93232
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>
39 //////////////////////////////////////////////////////////////////////////////
40 // DatabaseSyncState
42 DatabaseSyncState::DatabaseSyncState(OSyncPluginInfo *info, const char *description)
43 : m_dbId(0),
44 m_Sync(false),
45 m_Desc(description)
49 DatabaseSyncState::~DatabaseSyncState()
54 // Map2Uid
56 /// Converts record ID to string, since opensync 0.4x keeps the
57 /// same UID as we give it.
58 ///
59 std::string DatabaseSyncState::Map2Uid(uint32_t recordId) const
61 std::ostringstream oss;
62 oss << std::dec << recordId;
63 return oss.str();
68 //////////////////////////////////////////////////////////////////////////////
69 // BarryEnvironment Public API
71 BarryEnvironment::BarryEnvironment(OSyncPluginInfo *info):
72 info(info),
73 m_pin(-1),
74 m_DebugMode(false),
75 m_password(""),
76 m_IConverter("UTF-8"),
77 m_pCon(0),
78 m_pDesktop(0),
79 m_CalendarSync(info, "calendar"),
80 m_ContactsSync(info, "contacts"),
81 m_JournalSync(info, "note"),
82 m_TodoSync(info, "todo")
86 BarryEnvironment::~BarryEnvironment()
88 delete m_pDesktop;
89 delete m_pCon;
92 void BarryEnvironment::DoConnect()
94 // Create controller
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)
123 Disconnect();
125 // save result in case we need to reconnect later
126 m_ProbeResult = result;
128 DoConnect();
131 void BarryEnvironment::Reconnect()
133 Disconnect();
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.
141 Barry::Probe probe;
142 int i = probe.FindActive(m_ProbeResult.m_pin);
143 if( i != -1 )
144 m_ProbeResult = probe.Get(i);
147 DoConnect();
150 void BarryEnvironment::Disconnect()
152 delete m_pDesktop;
153 m_pDesktop = 0;
155 delete m_pCon;
156 m_pCon = 0;
159 bool BarryEnvironment::isConnected()
161 if (m_pCon != 0)
162 return true;
164 return false;
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",
178 dbId, i->first);
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");
234 return &m_TodoSync;
237 trace.log("return none");
239 return 0;