3 /// Base API class helpers
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.
25 #include "osprivatebase.h"
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
) {
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
;
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
) {
65 std::ostream
& operator<< (std::ostream
&os
, const Format
&format
)
67 os
<< "Format: " << format
.name
68 << " (Object Type: " << format
.object_type
<< ")";
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
) {
82 /////////////////////////////////////////////////////////////////////////////
83 // MemberSet public members
85 Member
* MemberSet::Find(long id
)
87 iterator b
= begin(), e
= end();
88 for( ; b
!= e
; ++b
) {
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
)
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
)
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
)
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
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");
175 void SyncConflict::Select(int change_index
)
177 m_conflict
.Select(change_index
);
180 void SyncConflict::Abort()
185 void SyncConflict::Duplicate()
187 m_conflict
.Duplicate();
190 void SyncConflict::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";
213 /////////////////////////////////////////////////////////////////////////////
214 // SyncSummary public members
216 SyncSummary::SyncSummary(SyncSummaryPrivateBase
&summary
)
221 SyncSummary::~SyncSummary()
225 void SyncSummary::Abort()
230 void SyncSummary::Continue()
232 m_summary
.Continue();
235 std::ostream
& SyncSummary::Dump(std::ostream
&os
) const
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
<< ") "
247 << ": Adding(" << b
->added
<< ") "
248 << "Modifying(" << b
->modified
<< ") "
249 << "Deleting(" << b
->deleted
<< ")\n";
255 /////////////////////////////////////////////////////////////////////////////
256 // SyncStatus public members - default, CLI imeplementations
258 SyncStatus::~SyncStatus()
262 void SyncStatus::HandleConflict(SyncConflict
&conflict
)
267 cout
<< _C("Conflicting items:") << "\n" << conflict
<< endl
;
268 cout
<< conflict
.GetMenu() << ": ";
282 conflict
.Select(atoi(line
.c_str()) - 1);
286 if( conflict
.IsAbortSupported() )
289 cout
<< _C("Abort not supported!") << endl
;
293 conflict
.Duplicate();
297 if( conflict
.IsIgnoreSupported() )
300 cout
<< _C("Ignore not supported!") << endl
;
304 if( conflict
.IsKeepNewerSupported() )
305 conflict
.KeepNewer();
307 cout
<< _C("Keep Newer not supported!") << endl
;
316 void SyncStatus::EntryStatus(const std::string
&msg
, bool error
)
319 cout
<< _C("ERROR: ");
323 void SyncStatus::MappingStatus(const std::string
&msg
, bool error
)
326 cout
<< _C("ERROR: ");
330 void SyncStatus::EngineStatus(const std::string
&msg
, bool error
, bool slowsync
)
333 cout
<< _C("ERROR: ");
337 void SyncStatus::MemberStatus(long member_id
,
338 const std::string
&plugin_name
,
339 const std::string
&msg
,
343 cout
<< _C("ERROR: ");
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): ");
356 // Abort if not got accepted with 'y'
357 if( line
[0] != 'y') {
358 cout
<< "\n" << _C("Aborting! Synchronization got aborted by user!") << endl
;
361 cout
<< "\n" << _C("OK! Completing synchronization!") << endl
;
366 void SyncStatus::ReportError(const std::string
&msg
)
368 cout
<< _C("CALLBACK ERROR: ") << msg
<< endl
;
372 /////////////////////////////////////////////////////////////////////////////
373 // OpenSyncAPISet public members
377 // initialize gettext, only once, for libosyncwrap
378 static bool i18n_initialized
= false;
379 if( !i18n_initialized
) {
381 i18n_initialized
= true;
387 iterator b
= begin(), e
= end();
388 for( ; b
!= e
; ++b
) {
395 // throws if not all can be opened
396 void APISet::OpenAll()
398 push_back( new OpenSync40
);
399 push_back( new OpenSync22
);
403 int APISet::OpenAvailable()
408 API
*p
= new OpenSync40
;
412 catch( std::exception
&e
) {
413 cerr
<< _C("Unable to load opensync 0.40: ") << e
.what();
418 API
*p
= new OpenSync22
;
422 catch( std::exception
&e
) {
423 cerr
<< _C("Unable to load opensync 0.22: ") << e
.what();
430 int APISet::GetAvailable() const
432 return ((*this)[0] ? 1 : 0) + ((*this)[1] ? 1 : 0);
445 } // namespace OpenSync