lib: added implicit ctor converter from DatabaseDatabase to DBListType
[barry/progweb.git] / desktop / src / osbase.cc
blob787ec8c0c19dcab6586c13c4789ab44920cd2b90
1 ///
2 /// \file osbase.cc
3 /// Base API class helpers
4 ///
6 /*
7 Copyright (C) 2009-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 "osbase.h"
23 #include "os22.h"
24 #include "os40.h"
25 #include "osprivatebase.h"
26 #include <stdlib.h>
27 #include <iostream>
28 #include <sstream>
29 #include <iomanip>
31 using namespace std;
33 namespace OpenSync {
35 std::ostream& operator<< (std::ostream &os, const string_list_type &list)
37 string_list_type::const_iterator b = list.begin(), e = list.end();
38 for( ; b != e; ++b ) {
39 os << *b << endl;
41 return os;
44 std::ostream& operator<< (std::ostream &os, const Member &member)
46 os << "Member ID: 0x" << hex << member.id
47 << "\n Plugin Name: " << member.plugin_name;
48 os << "\n Friendly Name: ";
49 if( member.friendly_name.size() )
50 os << member.friendly_name;
51 else
52 os << "<not set>";
53 return os;
56 std::ostream& operator<< (std::ostream &os, const member_list_type &list)
58 member_list_type::const_iterator b = list.begin(), e = list.end();
59 for( ; b != e; ++b ) {
60 os << *b << endl;
62 return os;
65 std::ostream& operator<< (std::ostream &os, const Format &format)
67 os << "Format: " << format.name
68 << " (Object Type: " << format.object_type << ")";
69 return os;
72 std::ostream& operator<< (std::ostream &os, const format_list_type &list)
74 format_list_type::const_iterator b = list.begin(), e = list.end();
75 for( ; b != e; ++b ) {
76 os << *b << endl;
78 return os;
82 /////////////////////////////////////////////////////////////////////////////
83 // MemberSet public members
85 Member* MemberSet::Find(long id)
87 iterator b = begin(), e = end();
88 for( ; b != e; ++b ) {
89 if( b->id == id )
90 return &(*b);
92 return 0;
95 Member* MemberSet::Find(const char *plugin_name)
97 iterator b = begin(), e = end();
98 for( ; b != e; ++b ) {
99 if( b->plugin_name == plugin_name )
100 return &(*b);
102 return 0;
105 long MemberSet::FindId(const char *plugin_name)
107 iterator b = begin(), e = end();
108 for( ; b != e; ++b ) {
109 if( b->plugin_name == plugin_name )
110 return b->id;
112 return -1;
115 /////////////////////////////////////////////////////////////////////////////
116 // FormatSet public members
118 Format* FormatSet::Find(const char *name)
120 iterator b = begin(), e = end();
121 for( ; b != e; ++b ) {
122 if( b->name == name )
123 return &(*b);
125 return 0;
128 /////////////////////////////////////////////////////////////////////////////
129 // SyncConflict public members
131 SyncConflict::SyncConflict(SyncConflictPrivateBase &conflict)
132 : m_conflict(conflict)
136 SyncConflict::~SyncConflict()
140 bool SyncConflict::IsAbortSupported() const
142 return m_conflict.IsAbortSupported();
145 bool SyncConflict::IsIgnoreSupported() const
147 return m_conflict.IsIgnoreSupported();
150 bool SyncConflict::IsKeepNewerSupported() const
152 return m_conflict.IsKeepNewerSupported();
155 std::string SyncConflict::GetMenu() const
157 ostringstream oss;
158 oss << "Which entry do you want to use?\n[1-9] To select a side";
160 if( IsAbortSupported() )
161 oss << ", [A]bort";
163 oss << ", [D]uplicate";
165 if( IsIgnoreSupported() )
166 oss << ", [I]gnore";
168 if( IsKeepNewerSupported() )
169 oss << ", Keep [N]ewer";
171 return oss.str();
174 void SyncConflict::Select(int change_index)
176 m_conflict.Select(change_index);
179 void SyncConflict::Abort()
181 m_conflict.Abort();
184 void SyncConflict::Duplicate()
186 m_conflict.Duplicate();
189 void SyncConflict::Ignore()
191 m_conflict.Ignore();
194 void SyncConflict::KeepNewer()
196 m_conflict.KeepNewer();
199 std::ostream& SyncConflict::Dump(std::ostream &os) const
201 const_iterator b = begin(), e = end();
202 for( ; b != e; ++b ) {
203 os << "Entry " << (b->id+1) << ":\n"
204 << "Member: " << b->member_id << "(" << b->plugin_name << ")\n"
205 << "UID: " << b->uid << "\n"
206 << "Data: " << b->printable_data << "\n";
208 return os;
212 /////////////////////////////////////////////////////////////////////////////
213 // SyncSummary public members
215 SyncSummary::SyncSummary(SyncSummaryPrivateBase &summary)
216 : m_summary(summary)
220 SyncSummary::~SyncSummary()
224 void SyncSummary::Abort()
226 m_summary.Abort();
229 void SyncSummary::Continue()
231 m_summary.Continue();
234 std::ostream& SyncSummary::Dump(std::ostream &os) const
236 string objtype_name;
237 const_iterator b = begin(), e = end();
238 for( ; b != e; ++b ) {
239 if( b->objtype_name != objtype_name ) {
240 objtype_name = b->objtype_name;
241 os << "Objtype: " << b->objtype_name << "\n";
244 os << "\tMember " << b->id << "(" << b->member_id << ") "
245 << b->plugin_name
246 << ": Adding(" << b->added << ") "
247 << "Modifying(" << b->modified << ") "
248 << "Deleting(" << b->deleted << ")\n";
250 return os;
254 /////////////////////////////////////////////////////////////////////////////
255 // SyncStatus public members - default, CLI imeplementations
257 SyncStatus::~SyncStatus()
261 void SyncStatus::HandleConflict(SyncConflict &conflict)
263 bool again = true;
264 while( again ) {
265 again = false;
266 cout << "Conflicting items:\n" << conflict << endl;
267 cout << conflict.GetMenu() << ": ";
268 string line;
269 getline(cin, line);
270 switch( line[0] )
272 case '1':
273 case '2':
274 case '3':
275 case '4':
276 case '5':
277 case '6':
278 case '7':
279 case '8':
280 case '9':
281 conflict.Select(atoi(line.c_str()) - 1);
282 break;
283 case 'A':
284 case 'a':
285 if( conflict.IsAbortSupported() )
286 conflict.Abort();
287 else
288 cout << "Abort not supported!" << endl;
289 break;
290 case 'D':
291 case 'd':
292 conflict.Duplicate();
293 break;
294 case 'I':
295 case 'i':
296 if( conflict.IsIgnoreSupported() )
297 conflict.Ignore();
298 else
299 cout << "Ignore not supported!" << endl;
300 break;
301 case 'N':
302 case 'n':
303 if( conflict.IsKeepNewerSupported() )
304 conflict.KeepNewer();
305 else
306 cout << "Keep Newer not supported!" << endl;
307 break;
308 default:
309 again = true;
310 break;
315 void SyncStatus::EntryStatus(const std::string &msg, bool error)
317 if( error )
318 cout << "ERROR: ";
319 cout << msg << endl;
322 void SyncStatus::MappingStatus(const std::string &msg, bool error)
324 if( error )
325 cout << "ERROR: ";
326 cout << msg << endl;
329 void SyncStatus::EngineStatus(const std::string &msg, bool error, bool slowsync)
331 if( error )
332 cout << "ERROR: ";
333 cout << msg << endl;
336 void SyncStatus::MemberStatus(long member_id,
337 const std::string &plugin_name,
338 const std::string &msg,
339 bool error)
341 if( error )
342 cout << "ERROR: ";
343 cout << msg << endl;
346 void SyncStatus::CheckSummary(SyncSummary &summary)
348 cout << "\nSynchronization Forecast Summary:\n";
349 cout << summary << endl;
351 cout << "Do you want to continue the synchronization? (N/y): ";
352 string line;
353 getline(cin, line);
355 // Abort if not got accepted with 'y'
356 if( line[0] != 'y') {
357 cout << "\nAborting! Synchronization got aborted by user!" << endl;
358 summary.Abort();
359 } else {
360 cout << "\nOK! Completing synchronization!" << endl;
361 summary.Continue();
365 void SyncStatus::ReportError(const std::string &msg)
367 cout << "CALLBACK ERROR: " << msg << endl;
371 /////////////////////////////////////////////////////////////////////////////
372 // OpenSyncAPISet public members
374 APISet::APISet()
378 APISet::~APISet()
380 iterator b = begin(), e = end();
381 for( ; b != e; ++b ) {
382 delete *b;
385 base_type::clear();
388 // throws if not all can be opened
389 void APISet::OpenAll()
391 push_back( new OpenSync40 );
392 push_back( new OpenSync22 );
395 // does not throw
396 int APISet::OpenAvailable()
398 int loaded = 0;
400 try {
401 API *p = new OpenSync40;
402 push_back(p);
403 loaded++;
405 catch( std::exception &e ) {
406 cerr << "Unable to load opensync 0.40: " << e.what();
407 push_back(0);
410 try {
411 API *p = new OpenSync22;
412 push_back(p);
413 loaded++;
415 catch( std::exception &e ) {
416 cerr << "Unable to load opensync 0.22: " << e.what();
417 push_back(0);
420 return loaded;
423 int APISet::GetAvailable() const
425 return ((*this)[0] ? 1 : 0) + ((*this)[1] ? 1 : 0);
428 API* APISet::os40()
430 return (*this)[0];
433 API* APISet::os22()
435 return (*this)[1];
438 } // namespace OpenSync