Bumped copyright dates for 2013
[barry.git] / opensync-plugin-0.4x / src / environment.cc
blob58c81e51b615bf1df0c31d192d2dbd82b95dd316
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 : m_dbId(0),
47 m_Sync(false),
48 m_Desc(description)
52 DatabaseSyncState::~DatabaseSyncState()
57 // Map2Uid
59 /// Converts record ID to string, since opensync 0.4x keeps the
60 /// same UID as we give it.
61 ///
62 std::string DatabaseSyncState::Map2Uid(uint32_t recordId) const
64 std::ostringstream oss;
65 oss << std::dec << recordId;
66 return oss.str();
71 //////////////////////////////////////////////////////////////////////////////
72 // BarryEnvironment Public API
74 BarryEnvironment::BarryEnvironment(OSyncPluginInfo *info):
75 info(info),
76 m_pin(-1),
77 m_DebugMode(false),
78 m_CalendarSync(info, "calendar"),
79 m_ContactsSync(info, "contacts"),
80 m_JournalSync(info, "note"),
81 m_TodoSync(info, "todo")
85 BarryEnvironment::~BarryEnvironment()
89 void BarryEnvironment::DoConnect()
91 if( !m_con.get() )
92 throw std::logic_error(_("Tried to use empty Connector"));
94 m_con->Connect();
96 // Save the DBIDs and DBNames of the databases we will work with
97 if( m_CalendarSync.m_Sync ) {
98 m_CalendarSync.m_dbName = Barry::Calendar::GetDBName();
99 m_CalendarSync.m_dbId = m_con->GetDesktop().GetDBID(Barry::Calendar::GetDBName());
102 if( m_ContactsSync.m_Sync ) {
103 m_ContactsSync.m_dbName = Barry::Contact::GetDBName();
104 m_ContactsSync.m_dbId = m_con->GetDesktop().GetDBID(Barry::Contact::GetDBName());
107 if( m_JournalSync.m_Sync ) {
108 m_JournalSync.m_dbName = Barry::Memo::GetDBName();
109 m_JournalSync.m_dbId = m_con->GetDesktop().GetDBID(Barry::Memo::GetDBName());
112 if( m_TodoSync.m_Sync ) {
113 m_TodoSync.m_dbName = Barry::Task::GetDBName();
114 m_TodoSync.m_dbId = m_con->GetDesktop().GetDBID(Barry::Task::GetDBName());
118 void BarryEnvironment::SetPassword(const std::string &password)
120 m_password = password;
121 if( m_con.get() )
122 m_con->SetPassword(password.c_str());
125 void BarryEnvironment::Connect(const Barry::ProbeResult &result)
127 m_con.reset(new DesktopConnector(m_password.c_str(), "UTF-8", result));
128 DoConnect();
131 void BarryEnvironment::Reconnect()
133 m_con->Reconnect(2);
136 void BarryEnvironment::Disconnect()
138 m_con->Disconnect();
141 bool BarryEnvironment::isConnected()
143 return m_con.get() && m_con->IsConnected();
146 void BarryEnvironment::ReconnectForDirtyFlags()
148 m_con->ReconnectForDirtyFlags();
151 void BarryEnvironment::RequireDirtyReconnect()
153 m_con->RequireDirtyReconnect();
156 void BarryEnvironment::ClearDirtyFlags(Barry::RecordStateTable &table,
157 const std::string &dbname)
159 Trace trace("ClearDirtyFlags");
161 unsigned int dbId = m_con->GetDesktop().GetDBID(dbname);
163 Barry::RecordStateTable::StateMapType::const_iterator i = table.StateMap.begin();
164 for( ; i != table.StateMap.end(); ++i ) {
165 if( i->second.Dirty ) {
166 trace.logf(_("Clearing dirty flag for db %u, index %u"),
167 dbId, i->first);
168 m_con->GetDesktop().ClearDirty(dbId, i->first);
173 void BarryEnvironment::ClearCalendarDirtyFlags()
175 Trace trace("ClearCalendarDirtyFlags");
176 ClearDirtyFlags(m_CalendarSync.m_Table, Barry::Calendar::GetDBName());
179 void BarryEnvironment::ClearContactsDirtyFlags()
181 Trace trace("ClearContactsDirtyFlags");
182 ClearDirtyFlags(m_ContactsSync.m_Table, Barry::Contact::GetDBName());
185 void BarryEnvironment::ClearJournalDirtyFlags()
187 Trace trace("ClearJournalDirtyFlags");
188 ClearDirtyFlags(m_JournalSync.m_Table, Barry::Memo::GetDBName());
191 void BarryEnvironment::ClearTodoDirtyFlags()
193 Trace trace("ClearTodoDirtyFlags");
194 ClearDirtyFlags(m_TodoSync.m_Table, Barry::Task::GetDBName());
197 DatabaseSyncState* BarryEnvironment::GetSyncObject(OSyncChange *change)
199 Trace trace("BarryEnvironment::GetSyncObject()");
201 const char *name = osync_change_get_objtype(change);
203 trace.logf(_("osync_change_get_objtype returns %s"), name);
205 if( strcmp(name, "event") == 0 ) {
206 trace.log(_("return calendar object"));
208 return &m_CalendarSync;
210 else if( strcmp(name, "contact") == 0 ) {
211 trace.log(_("return contact object"));
213 return &m_ContactsSync;
215 else if( strcmp(name, "note") == 0 ) {
216 trace.log(_("return journal object"));
218 return &m_JournalSync;
220 else if( strcmp(name, "todo") == 0 ) {
221 trace.log(_("return todo object"));
223 return &m_TodoSync;
226 trace.log(_("return none"));
228 return 0;