Bumped copyright dates for 2013
[barry.git] / desktop / src / osbase.cc
blob3eb5cd574f86e0cd4f9d345565b11d7f8865ea7d
1 ///
2 /// \file osbase.cc
3 /// Base API class helpers
4 ///
6 /*
7 Copyright (C) 2009-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 "osbase.h"
23 #include "os22.h"
24 #include "os40.h"
25 #include "osprivatebase.h"
26 #include <iostream>
27 #include <sstream>
28 #include <iomanip>
29 #include "i18n.h"
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 << _C("Which entry do you want to use?\n[1-9] To select a side");
160 // Translator note: the menu letters must remain the same!
161 if( IsAbortSupported() )
162 oss << _C(", [A]bort");
164 oss << _C(", [D]uplicate");
166 if( IsIgnoreSupported() )
167 oss << _C(", [I]gnore");
169 if( IsKeepNewerSupported() )
170 oss << _C(", Keep [N]ewer");
172 return oss.str();
175 void SyncConflict::Select(int change_index)
177 m_conflict.Select(change_index);
180 void SyncConflict::Abort()
182 m_conflict.Abort();
185 void SyncConflict::Duplicate()
187 m_conflict.Duplicate();
190 void SyncConflict::Ignore()
192 m_conflict.Ignore();
195 void SyncConflict::KeepNewer()
197 m_conflict.KeepNewer();
200 std::ostream& SyncConflict::Dump(std::ostream &os) const
202 const_iterator b = begin(), e = end();
203 for( ; b != e; ++b ) {
204 os << "Entry " << (b->id+1) << ":\n"
205 << "Member: " << b->member_id << "(" << b->plugin_name << ")\n"
206 << "UID: " << b->uid << "\n"
207 << "Data: " << b->printable_data << "\n";
209 return os;
213 /////////////////////////////////////////////////////////////////////////////
214 // SyncSummary public members
216 SyncSummary::SyncSummary(SyncSummaryPrivateBase &summary)
217 : m_summary(summary)
221 SyncSummary::~SyncSummary()
225 void SyncSummary::Abort()
227 m_summary.Abort();
230 void SyncSummary::Continue()
232 m_summary.Continue();
235 std::ostream& SyncSummary::Dump(std::ostream &os) const
237 string objtype_name;
238 const_iterator b = begin(), e = end();
239 for( ; b != e; ++b ) {
240 if( b->objtype_name != objtype_name ) {
241 objtype_name = b->objtype_name;
242 os << "Objtype: " << b->objtype_name << "\n";
245 os << "\tMember " << b->id << "(" << b->member_id << ") "
246 << b->plugin_name
247 << ": Adding(" << b->added << ") "
248 << "Modifying(" << b->modified << ") "
249 << "Deleting(" << b->deleted << ")\n";
251 return os;
255 /////////////////////////////////////////////////////////////////////////////
256 // SyncStatus public members - default, CLI imeplementations
258 SyncStatus::~SyncStatus()
262 void SyncStatus::HandleConflict(SyncConflict &conflict)
264 bool again = true;
265 while( again ) {
266 again = false;
267 cout << _C("Conflicting items:") << "\n" << conflict << endl;
268 cout << conflict.GetMenu() << ": ";
269 string line;
270 getline(cin, line);
271 switch( line[0] )
273 case '1':
274 case '2':
275 case '3':
276 case '4':
277 case '5':
278 case '6':
279 case '7':
280 case '8':
281 case '9':
282 conflict.Select(atoi(line.c_str()) - 1);
283 break;
284 case 'A':
285 case 'a':
286 if( conflict.IsAbortSupported() )
287 conflict.Abort();
288 else
289 cout << _C("Abort not supported!") << endl;
290 break;
291 case 'D':
292 case 'd':
293 conflict.Duplicate();
294 break;
295 case 'I':
296 case 'i':
297 if( conflict.IsIgnoreSupported() )
298 conflict.Ignore();
299 else
300 cout << _C("Ignore not supported!") << endl;
301 break;
302 case 'N':
303 case 'n':
304 if( conflict.IsKeepNewerSupported() )
305 conflict.KeepNewer();
306 else
307 cout << _C("Keep Newer not supported!") << endl;
308 break;
309 default:
310 again = true;
311 break;
316 void SyncStatus::EntryStatus(const std::string &msg, bool error)
318 if( error )
319 cout << _C("ERROR: ");
320 cout << msg << endl;
323 void SyncStatus::MappingStatus(const std::string &msg, bool error)
325 if( error )
326 cout << _C("ERROR: ");
327 cout << msg << endl;
330 void SyncStatus::EngineStatus(const std::string &msg, bool error, bool slowsync)
332 if( error )
333 cout << _C("ERROR: ");
334 cout << msg << endl;
337 void SyncStatus::MemberStatus(long member_id,
338 const std::string &plugin_name,
339 const std::string &msg,
340 bool error)
342 if( error )
343 cout << _C("ERROR: ");
344 cout << msg << endl;
347 void SyncStatus::CheckSummary(SyncSummary &summary)
349 cout << "\n" << _C("Synchronization Forecast Summary:") << "\n";
350 cout << summary << endl;
352 cout << _C("Do you want to continue the synchronization? (N/y): ");
353 string line;
354 getline(cin, line);
356 // Abort if not got accepted with 'y'
357 if( line[0] != 'y') {
358 cout << "\n" << _C("Aborting! Synchronization got aborted by user!") << endl;
359 summary.Abort();
360 } else {
361 cout << "\n" << _C("OK! Completing synchronization!") << endl;
362 summary.Continue();
366 void SyncStatus::ReportError(const std::string &msg)
368 cout << _C("CALLBACK ERROR: ") << msg << endl;
372 /////////////////////////////////////////////////////////////////////////////
373 // OpenSyncAPISet public members
375 APISet::APISet()
377 // initialize gettext, only once, for libosyncwrap
378 static bool i18n_initialized = false;
379 if( !i18n_initialized ) {
380 INIT_I18N();
381 i18n_initialized = true;
385 APISet::~APISet()
387 iterator b = begin(), e = end();
388 for( ; b != e; ++b ) {
389 delete *b;
392 base_type::clear();
395 // throws if not all can be opened
396 void APISet::OpenAll()
398 push_back( new OpenSync40 );
399 push_back( new OpenSync22 );
402 // does not throw
403 int APISet::OpenAvailable()
405 int loaded = 0;
407 try {
408 API *p = new OpenSync40;
409 push_back(p);
410 loaded++;
412 catch( std::exception &e ) {
413 cerr << _C("Unable to load opensync 0.40: ") << e.what();
414 push_back(0);
417 try {
418 API *p = new OpenSync22;
419 push_back(p);
420 loaded++;
422 catch( std::exception &e ) {
423 cerr << _C("Unable to load opensync 0.22: ") << e.what();
424 push_back(0);
427 return loaded;
430 int APISet::GetAvailable() const
432 return ((*this)[0] ? 1 : 0) + ((*this)[1] ? 1 : 0);
435 API* APISet::os40()
437 return (*this)[0];
440 API* APISet::os22()
442 return (*this)[1];
445 } // namespace OpenSync