desktop: string conversion to support i18n
[barry.git] / desktop / src / osbase.cc
blob382bad9395cc13d89248fa39a7aa4942761c5e95
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>
30 #include "i18n.h"
32 using namespace std;
34 namespace OpenSync {
36 std::ostream& operator<< (std::ostream &os, const string_list_type &list)
38 string_list_type::const_iterator b = list.begin(), e = list.end();
39 for( ; b != e; ++b ) {
40 os << *b << endl;
42 return os;
45 std::ostream& operator<< (std::ostream &os, const Member &member)
47 os << "Member ID: 0x" << hex << member.id
48 << "\n Plugin Name: " << member.plugin_name;
49 os << "\n Friendly Name: ";
50 if( member.friendly_name.size() )
51 os << member.friendly_name;
52 else
53 os << "<not set>";
54 return os;
57 std::ostream& operator<< (std::ostream &os, const member_list_type &list)
59 member_list_type::const_iterator b = list.begin(), e = list.end();
60 for( ; b != e; ++b ) {
61 os << *b << endl;
63 return os;
66 std::ostream& operator<< (std::ostream &os, const Format &format)
68 os << "Format: " << format.name
69 << " (Object Type: " << format.object_type << ")";
70 return os;
73 std::ostream& operator<< (std::ostream &os, const format_list_type &list)
75 format_list_type::const_iterator b = list.begin(), e = list.end();
76 for( ; b != e; ++b ) {
77 os << *b << endl;
79 return os;
83 /////////////////////////////////////////////////////////////////////////////
84 // MemberSet public members
86 Member* MemberSet::Find(long id)
88 iterator b = begin(), e = end();
89 for( ; b != e; ++b ) {
90 if( b->id == id )
91 return &(*b);
93 return 0;
96 Member* MemberSet::Find(const char *plugin_name)
98 iterator b = begin(), e = end();
99 for( ; b != e; ++b ) {
100 if( b->plugin_name == plugin_name )
101 return &(*b);
103 return 0;
106 long MemberSet::FindId(const char *plugin_name)
108 iterator b = begin(), e = end();
109 for( ; b != e; ++b ) {
110 if( b->plugin_name == plugin_name )
111 return b->id;
113 return -1;
116 /////////////////////////////////////////////////////////////////////////////
117 // FormatSet public members
119 Format* FormatSet::Find(const char *name)
121 iterator b = begin(), e = end();
122 for( ; b != e; ++b ) {
123 if( b->name == name )
124 return &(*b);
126 return 0;
129 /////////////////////////////////////////////////////////////////////////////
130 // SyncConflict public members
132 SyncConflict::SyncConflict(SyncConflictPrivateBase &conflict)
133 : m_conflict(conflict)
137 SyncConflict::~SyncConflict()
141 bool SyncConflict::IsAbortSupported() const
143 return m_conflict.IsAbortSupported();
146 bool SyncConflict::IsIgnoreSupported() const
148 return m_conflict.IsIgnoreSupported();
151 bool SyncConflict::IsKeepNewerSupported() const
153 return m_conflict.IsKeepNewerSupported();
156 std::string SyncConflict::GetMenu() const
158 ostringstream oss;
159 oss << _C("Which entry do you want to use?\n[1-9] To select a side");
161 // Translator note: the menu letters must remain the same!
162 if( IsAbortSupported() )
163 oss << _C(", [A]bort");
165 oss << _C(", [D]uplicate");
167 if( IsIgnoreSupported() )
168 oss << _C(", [I]gnore");
170 if( IsKeepNewerSupported() )
171 oss << _C(", Keep [N]ewer");
173 return oss.str();
176 void SyncConflict::Select(int change_index)
178 m_conflict.Select(change_index);
181 void SyncConflict::Abort()
183 m_conflict.Abort();
186 void SyncConflict::Duplicate()
188 m_conflict.Duplicate();
191 void SyncConflict::Ignore()
193 m_conflict.Ignore();
196 void SyncConflict::KeepNewer()
198 m_conflict.KeepNewer();
201 std::ostream& SyncConflict::Dump(std::ostream &os) const
203 const_iterator b = begin(), e = end();
204 for( ; b != e; ++b ) {
205 os << "Entry " << (b->id+1) << ":\n"
206 << "Member: " << b->member_id << "(" << b->plugin_name << ")\n"
207 << "UID: " << b->uid << "\n"
208 << "Data: " << b->printable_data << "\n";
210 return os;
214 /////////////////////////////////////////////////////////////////////////////
215 // SyncSummary public members
217 SyncSummary::SyncSummary(SyncSummaryPrivateBase &summary)
218 : m_summary(summary)
222 SyncSummary::~SyncSummary()
226 void SyncSummary::Abort()
228 m_summary.Abort();
231 void SyncSummary::Continue()
233 m_summary.Continue();
236 std::ostream& SyncSummary::Dump(std::ostream &os) const
238 string objtype_name;
239 const_iterator b = begin(), e = end();
240 for( ; b != e; ++b ) {
241 if( b->objtype_name != objtype_name ) {
242 objtype_name = b->objtype_name;
243 os << "Objtype: " << b->objtype_name << "\n";
246 os << "\tMember " << b->id << "(" << b->member_id << ") "
247 << b->plugin_name
248 << ": Adding(" << b->added << ") "
249 << "Modifying(" << b->modified << ") "
250 << "Deleting(" << b->deleted << ")\n";
252 return os;
256 /////////////////////////////////////////////////////////////////////////////
257 // SyncStatus public members - default, CLI imeplementations
259 SyncStatus::~SyncStatus()
263 void SyncStatus::HandleConflict(SyncConflict &conflict)
265 bool again = true;
266 while( again ) {
267 again = false;
268 cout << _C("Conflicting items:") << "\n" << conflict << endl;
269 cout << conflict.GetMenu() << ": ";
270 string line;
271 getline(cin, line);
272 switch( line[0] )
274 case '1':
275 case '2':
276 case '3':
277 case '4':
278 case '5':
279 case '6':
280 case '7':
281 case '8':
282 case '9':
283 conflict.Select(atoi(line.c_str()) - 1);
284 break;
285 case 'A':
286 case 'a':
287 if( conflict.IsAbortSupported() )
288 conflict.Abort();
289 else
290 cout << _C("Abort not supported!") << endl;
291 break;
292 case 'D':
293 case 'd':
294 conflict.Duplicate();
295 break;
296 case 'I':
297 case 'i':
298 if( conflict.IsIgnoreSupported() )
299 conflict.Ignore();
300 else
301 cout << _C("Ignore not supported!") << endl;
302 break;
303 case 'N':
304 case 'n':
305 if( conflict.IsKeepNewerSupported() )
306 conflict.KeepNewer();
307 else
308 cout << _C("Keep Newer not supported!") << endl;
309 break;
310 default:
311 again = true;
312 break;
317 void SyncStatus::EntryStatus(const std::string &msg, bool error)
319 if( error )
320 cout << _C("ERROR: ");
321 cout << msg << endl;
324 void SyncStatus::MappingStatus(const std::string &msg, bool error)
326 if( error )
327 cout << _C("ERROR: ");
328 cout << msg << endl;
331 void SyncStatus::EngineStatus(const std::string &msg, bool error, bool slowsync)
333 if( error )
334 cout << _C("ERROR: ");
335 cout << msg << endl;
338 void SyncStatus::MemberStatus(long member_id,
339 const std::string &plugin_name,
340 const std::string &msg,
341 bool error)
343 if( error )
344 cout << _C("ERROR: ");
345 cout << msg << endl;
348 void SyncStatus::CheckSummary(SyncSummary &summary)
350 cout << "\n" << _C("Synchronization Forecast Summary:") << "\n";
351 cout << summary << endl;
353 cout << _C("Do you want to continue the synchronization? (N/y): ");
354 string line;
355 getline(cin, line);
357 // Abort if not got accepted with 'y'
358 if( line[0] != 'y') {
359 cout << "\n" << _C("Aborting! Synchronization got aborted by user!") << endl;
360 summary.Abort();
361 } else {
362 cout << "\n" << _C("OK! Completing synchronization!") << endl;
363 summary.Continue();
367 void SyncStatus::ReportError(const std::string &msg)
369 cout << _C("CALLBACK ERROR: ") << msg << endl;
373 /////////////////////////////////////////////////////////////////////////////
374 // OpenSyncAPISet public members
376 APISet::APISet()
380 APISet::~APISet()
382 iterator b = begin(), e = end();
383 for( ; b != e; ++b ) {
384 delete *b;
387 base_type::clear();
390 // throws if not all can be opened
391 void APISet::OpenAll()
393 push_back( new OpenSync40 );
394 push_back( new OpenSync22 );
397 // does not throw
398 int APISet::OpenAvailable()
400 int loaded = 0;
402 try {
403 API *p = new OpenSync40;
404 push_back(p);
405 loaded++;
407 catch( std::exception &e ) {
408 cerr << _C("Unable to load opensync 0.40: ") << e.what();
409 push_back(0);
412 try {
413 API *p = new OpenSync22;
414 push_back(p);
415 loaded++;
417 catch( std::exception &e ) {
418 cerr << _C("Unable to load opensync 0.22: ") << e.what();
419 push_back(0);
422 return loaded;
425 int APISet::GetAvailable() const
427 return ((*this)[0] ? 1 : 0) + ((*this)[1] ? 1 : 0);
430 API* APISet::os40()
432 return (*this)[0];
435 API* APISet::os22()
437 return (*this)[1];
440 } // namespace OpenSync