Fixed C++ errors and warnings reported by cppcheck
[barry.git] / opensync-plugin-0.4x / src / environment.cc
blob1782fb6407a0481ba302a6c843d48d3179640d90
1 //
2 // \file environment.cc
3 // Container / environment class for the sync module.
4 //
6 /*
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"
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>
38 #include "i18n.h"
40 using namespace Barry;
42 //////////////////////////////////////////////////////////////////////////////
43 // DatabaseSyncState
45 DatabaseSyncState::DatabaseSyncState(OSyncPluginInfo *info, const char *description)
46 : sink(0),
47 m_dbId(0),
48 m_Sync(false),
49 m_Desc(description)
53 DatabaseSyncState::~DatabaseSyncState()
58 // Map2Uid
60 /// Converts record ID to string, since opensync 0.4x keeps the
61 /// same UID as we give it.
62 ///
63 std::string DatabaseSyncState::Map2Uid(uint32_t recordId) const
65 std::ostringstream oss;
66 oss << std::dec << recordId;
67 return oss.str();
72 //////////////////////////////////////////////////////////////////////////////
73 // BarryEnvironment Public API
75 BarryEnvironment::BarryEnvironment(OSyncPluginInfo *info):
76 member(0),
77 info(info),
78 m_pin(-1),
79 m_DebugMode(false),
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()
93 if( !m_con.get() )
94 throw std::logic_error(_("Tried to use empty Connector"));
96 m_con->Connect();
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;
123 if( m_con.get() )
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));
130 DoConnect();
133 void BarryEnvironment::Reconnect()
135 m_con->Reconnect(2);
138 void BarryEnvironment::Disconnect()
140 m_con->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"),
169 dbId, i->first);
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"));
225 return &m_TodoSync;
228 trace.log(_("return none"));
230 return 0;