desktop: fixed ExecHelper usage, to use child exit code
[barry.git] / opensync-plugin-0.4x / src / environment.cc
blobb921a912995ce8cfc458c6745fc802dc7bcc3803
1 //
2 // \file environment.cc
3 // Container / environment class for the sync module.
4 //
6 /*
7 Copyright (C) 2006-2012, 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>
39 using namespace Barry;
41 //////////////////////////////////////////////////////////////////////////////
42 // DatabaseSyncState
44 DatabaseSyncState::DatabaseSyncState(OSyncPluginInfo *info, const char *description)
45 : m_dbId(0),
46 m_Sync(false),
47 m_Desc(description)
51 DatabaseSyncState::~DatabaseSyncState()
56 // Map2Uid
58 /// Converts record ID to string, since opensync 0.4x keeps the
59 /// same UID as we give it.
60 ///
61 std::string DatabaseSyncState::Map2Uid(uint32_t recordId) const
63 std::ostringstream oss;
64 oss << std::dec << recordId;
65 return oss.str();
70 //////////////////////////////////////////////////////////////////////////////
71 // BarryEnvironment Public API
73 BarryEnvironment::BarryEnvironment(OSyncPluginInfo *info):
74 info(info),
75 m_pin(-1),
76 m_DebugMode(false),
77 m_CalendarSync(info, "calendar"),
78 m_ContactsSync(info, "contacts"),
79 m_JournalSync(info, "note"),
80 m_TodoSync(info, "todo")
84 BarryEnvironment::~BarryEnvironment()
88 void BarryEnvironment::DoConnect()
90 if( !m_con.get() )
91 throw std::logic_error("Tried to use empty Connector");
93 m_con->Connect();
95 // Save the DBIDs and DBNames of the databases we will work with
96 if( m_CalendarSync.m_Sync ) {
97 m_CalendarSync.m_dbName = Barry::Calendar::GetDBName();
98 m_CalendarSync.m_dbId = m_con->GetDesktop().GetDBID(Barry::Calendar::GetDBName());
101 if( m_ContactsSync.m_Sync ) {
102 m_ContactsSync.m_dbName = Barry::Contact::GetDBName();
103 m_ContactsSync.m_dbId = m_con->GetDesktop().GetDBID(Barry::Contact::GetDBName());
106 if( m_JournalSync.m_Sync ) {
107 m_JournalSync.m_dbName = Barry::Memo::GetDBName();
108 m_JournalSync.m_dbId = m_con->GetDesktop().GetDBID(Barry::Memo::GetDBName());
111 if( m_TodoSync.m_Sync ) {
112 m_TodoSync.m_dbName = Barry::Task::GetDBName();
113 m_TodoSync.m_dbId = m_con->GetDesktop().GetDBID(Barry::Task::GetDBName());
117 void BarryEnvironment::SetPassword(const std::string &password)
119 m_password = password;
120 if( m_con.get() )
121 m_con->SetPassword(password.c_str());
124 void BarryEnvironment::Connect(const Barry::ProbeResult &result)
126 m_con.reset(new DesktopConnector(m_password.c_str(), "UTF-8", result));
127 DoConnect();
130 void BarryEnvironment::Reconnect()
132 m_con->Reconnect(2);
135 void BarryEnvironment::Disconnect()
137 m_con->Disconnect();
140 bool BarryEnvironment::isConnected()
142 return m_con.get() && m_con->IsConnected();
145 void BarryEnvironment::ReconnectForDirtyFlags()
147 m_con->ReconnectForDirtyFlags();
150 void BarryEnvironment::RequireDirtyReconnect()
152 m_con->RequireDirtyReconnect();
155 void BarryEnvironment::ClearDirtyFlags(Barry::RecordStateTable &table,
156 const std::string &dbname)
158 Trace trace("ClearDirtyFlags");
160 unsigned int dbId = m_con->GetDesktop().GetDBID(dbname);
162 Barry::RecordStateTable::StateMapType::const_iterator i = table.StateMap.begin();
163 for( ; i != table.StateMap.end(); ++i ) {
164 if( i->second.Dirty ) {
165 trace.logf("Clearing dirty flag for db %u, index %u",
166 dbId, i->first);
167 m_con->GetDesktop().ClearDirty(dbId, i->first);
172 void BarryEnvironment::ClearCalendarDirtyFlags()
174 Trace trace("ClearCalendarDirtyFlags");
175 ClearDirtyFlags(m_CalendarSync.m_Table, Barry::Calendar::GetDBName());
178 void BarryEnvironment::ClearContactsDirtyFlags()
180 Trace trace("ClearContactsDirtyFlags");
181 ClearDirtyFlags(m_ContactsSync.m_Table, Barry::Contact::GetDBName());
184 void BarryEnvironment::ClearJournalDirtyFlags()
186 Trace trace("ClearJournalDirtyFlags");
187 ClearDirtyFlags(m_JournalSync.m_Table, Barry::Memo::GetDBName());
190 void BarryEnvironment::ClearTodoDirtyFlags()
192 Trace trace("ClearTodoDirtyFlags");
193 ClearDirtyFlags(m_TodoSync.m_Table, Barry::Task::GetDBName());
196 DatabaseSyncState* BarryEnvironment::GetSyncObject(OSyncChange *change)
198 Trace trace("BarryEnvironment::GetSyncObject()");
200 const char *name = osync_change_get_objtype(change);
202 trace.logf("osync_change_get_objtype returns %s", name);
204 if( strcmp(name, "event") == 0 ) {
205 trace.log("return calendar object");
207 return &m_CalendarSync;
209 else if( strcmp(name, "contact") == 0 ) {
210 trace.log("return contact object");
212 return &m_ContactsSync;
214 else if( strcmp(name, "note") == 0 ) {
215 trace.log("return journal object");
217 return &m_JournalSync;
219 else if( strcmp(name, "todo") == 0 ) {
220 trace.log("return todo object");
222 return &m_TodoSync;
225 trace.log("return none");
227 return 0;