2 // \file environment.cc
3 // Container / environment class for the sync module.
7 Copyright (C) 2006-2013, 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"
40 using namespace Barry
;
42 //////////////////////////////////////////////////////////////////////////////
45 DatabaseSyncState::DatabaseSyncState(OSyncPluginInfo
*info
, const char *description
)
53 DatabaseSyncState::~DatabaseSyncState()
60 /// Converts record ID to string, since opensync 0.4x keeps the
61 /// same UID as we give it.
63 std::string
DatabaseSyncState::Map2Uid(uint32_t recordId
) const
65 std::ostringstream oss
;
66 oss
<< std::dec
<< recordId
;
72 //////////////////////////////////////////////////////////////////////////////
73 // BarryEnvironment Public API
75 BarryEnvironment::BarryEnvironment(OSyncPluginInfo
*info
):
80 m_CalendarSync(info
, "calendar"),
81 m_ContactsSync(info
, "contacts"),
82 m_JournalSync(info
, "note"),
83 m_TodoSync(info
, "todo")
87 BarryEnvironment::~BarryEnvironment()
91 void BarryEnvironment::DoConnect()
94 throw std::logic_error(_("Tried to use empty Connector"));
98 // Save the DBIDs and DBNames of the databases we will work with
99 if( m_CalendarSync
.m_Sync
) {
100 m_CalendarSync
.m_dbName
= Barry::Calendar::GetDBName();
101 m_CalendarSync
.m_dbId
= m_con
->GetDesktop().GetDBID(Barry::Calendar::GetDBName());
104 if( m_ContactsSync
.m_Sync
) {
105 m_ContactsSync
.m_dbName
= Barry::Contact::GetDBName();
106 m_ContactsSync
.m_dbId
= m_con
->GetDesktop().GetDBID(Barry::Contact::GetDBName());
109 if( m_JournalSync
.m_Sync
) {
110 m_JournalSync
.m_dbName
= Barry::Memo::GetDBName();
111 m_JournalSync
.m_dbId
= m_con
->GetDesktop().GetDBID(Barry::Memo::GetDBName());
114 if( m_TodoSync
.m_Sync
) {
115 m_TodoSync
.m_dbName
= Barry::Task::GetDBName();
116 m_TodoSync
.m_dbId
= m_con
->GetDesktop().GetDBID(Barry::Task::GetDBName());
120 void BarryEnvironment::SetPassword(const std::string
&password
)
122 m_password
= password
;
124 m_con
->SetPassword(password
.c_str());
127 void BarryEnvironment::Connect(const Barry::ProbeResult
&result
)
129 m_con
.reset(new DesktopConnector(m_password
.c_str(), "UTF-8", result
));
133 void BarryEnvironment::Reconnect()
138 void BarryEnvironment::Disconnect()
143 bool BarryEnvironment::isConnected()
145 return m_con
.get() && m_con
->IsConnected();
148 void BarryEnvironment::ReconnectForDirtyFlags()
150 m_con
->ReconnectForDirtyFlags();
153 void BarryEnvironment::RequireDirtyReconnect()
155 m_con
->RequireDirtyReconnect();
158 void BarryEnvironment::ClearDirtyFlags(Barry::RecordStateTable
&table
,
159 const std::string
&dbname
)
161 Trace
trace("ClearDirtyFlags");
163 unsigned int dbId
= m_con
->GetDesktop().GetDBID(dbname
);
165 Barry::RecordStateTable::StateMapType::const_iterator i
= table
.StateMap
.begin();
166 for( ; i
!= table
.StateMap
.end(); ++i
) {
167 if( i
->second
.Dirty
) {
168 trace
.logf(_("Clearing dirty flag for db %u, index %u"),
170 m_con
->GetDesktop().ClearDirty(dbId
, i
->first
);
175 void BarryEnvironment::ClearCalendarDirtyFlags()
177 Trace
trace("ClearCalendarDirtyFlags");
178 ClearDirtyFlags(m_CalendarSync
.m_Table
, Barry::Calendar::GetDBName());
181 void BarryEnvironment::ClearContactsDirtyFlags()
183 Trace
trace("ClearContactsDirtyFlags");
184 ClearDirtyFlags(m_ContactsSync
.m_Table
, Barry::Contact::GetDBName());
187 void BarryEnvironment::ClearJournalDirtyFlags()
189 Trace
trace("ClearJournalDirtyFlags");
190 ClearDirtyFlags(m_JournalSync
.m_Table
, Barry::Memo::GetDBName());
193 void BarryEnvironment::ClearTodoDirtyFlags()
195 Trace
trace("ClearTodoDirtyFlags");
196 ClearDirtyFlags(m_TodoSync
.m_Table
, Barry::Task::GetDBName());
199 DatabaseSyncState
* BarryEnvironment::GetSyncObject(OSyncChange
*change
)
201 Trace
trace("BarryEnvironment::GetSyncObject()");
203 const char *name
= osync_change_get_objtype(change
);
205 trace
.logf(_("osync_change_get_objtype returns %s"), name
);
207 if( strcmp(name
, "event") == 0 ) {
208 trace
.log(_("return calendar object"));
210 return &m_CalendarSync
;
212 else if( strcmp(name
, "contact") == 0 ) {
213 trace
.log(_("return contact object"));
215 return &m_ContactsSync
;
217 else if( strcmp(name
, "note") == 0 ) {
218 trace
.log(_("return journal object"));
220 return &m_JournalSync
;
222 else if( strcmp(name
, "todo") == 0 ) {
223 trace
.log(_("return todo object"));
228 trace
.log(_("return none"));