3 /// Utility to dump tarball backup records to stdout.
7 Copyright (C) 2010, 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 <barry/barry.h>
23 #ifdef __BARRY_SYNC_MODE__
24 #include <barry/barrysync.h>
26 #include <barry/barrybackup.h>
32 using namespace Barry
;
37 const char *Version
= Barry::Version(major
, minor
);
40 << "btardump - Command line parser for Barry backup files\n"
41 << " Copyright 2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
42 << " Using: " << Version
<< "\n"
44 << " -d db Name of database to dump. Can be used multiple times\n"
45 << " to parse multiple databases at once. If not specified\n"
46 << " at all, all available databases from the backup are\n"
49 << " -i cs International charset for string conversions\n"
50 << " Valid values here are available with 'iconv --list'\n"
51 #ifdef __BARRY_SYNC_MODE__
52 << " -V Dump records using MIME vformats where possible\n"
55 << " [files...] Backup file(s), created by btool or the backup GUI.\n"
59 #ifdef __BARRY_SYNC_MODE__
60 template <class Record
>
64 void Dump(std::ostream
&os
, const Record
&rec
)
69 static bool Supported() { return false; }
73 class MimeDump
<Contact
>
76 void Dump(std::ostream
&os
, const Contact
&rec
)
79 os
<< vcard
.ToVCard(rec
) << endl
;
82 static bool Supported() { return true; }
86 class MyAllRecordDumpStore
: public AllRecordDumpStore
91 explicit MyAllRecordDumpStore(std::ostream
&os
, bool vformat_mode
=false)
92 : AllRecordDumpStore(os
)
93 , vformat_mode(vformat_mode
)
96 virtual void operator() (const Barry::Contact
&);
97 virtual void operator() (const Barry::Calendar
&);
98 virtual void operator() (const Barry::CalendarAll
&);
99 virtual void operator() (const Barry::Memo
&);
100 virtual void operator() (const Barry::Task
&);
103 void MyAllRecordDumpStore::operator() (const Barry::Contact
&rec
)
106 #ifdef __BARRY_SYNC_MODE__
108 m_os
<< vcard
.ToVCard(rec
) << endl
;
112 m_os
<< rec
<< std::endl
;
116 void MyAllRecordDumpStore::operator() (const Barry::Calendar
&rec
)
119 #ifdef __BARRY_SYNC_MODE__
120 Sync::vTimeConverter vtc
;
121 Sync::vCalendar
vcal(vtc
);
122 m_os
<< vcal
.ToVCal(rec
) << endl
;
126 m_os
<< rec
<< std::endl
;
130 void MyAllRecordDumpStore::operator() (const Barry::CalendarAll
&rec
)
133 #ifdef __BARRY_SYNC_MODE__
134 Sync::vTimeConverter vtc
;
135 Sync::vCalendar
vcal(vtc
);
136 m_os
<< vcal
.ToVCal(rec
) << endl
;
140 m_os
<< rec
<< std::endl
;
144 void MyAllRecordDumpStore::operator() (const Barry::Memo
&rec
)
147 #ifdef __BARRY_SYNC_MODE__
148 Sync::vJournal vjournal
;
149 m_os
<< vjournal
.ToMemo(rec
) << endl
;
153 m_os
<< rec
<< std::endl
;
157 void MyAllRecordDumpStore::operator() (const Barry::Task
&rec
)
160 #ifdef __BARRY_SYNC_MODE__
161 Sync::vTimeConverter vtc
;
162 Sync::vTodo
vtodo(vtc
);
163 m_os
<< vtodo
.ToTask(rec
) << endl
;
167 m_os
<< rec
<< std::endl
;
171 int main(int argc
, char *argv
[])
174 bool vformat_mode
= false;
176 vector
<string
> db_names
;
177 vector
<string
> backup_files
;
180 // process command line options
182 int cmd
= getopt(argc
, argv
, "d:hi:V");
188 case 'd': // show dbname
189 db_names
.push_back(string(optarg
));
192 case 'V': // vformat MIME mode
193 #ifdef __BARRY_SYNC_MODE__
196 cerr
<< "-V option not supported - no Sync "
197 "library support available\n";
202 case 'i': // international charset (iconv)
203 iconvCharset
= optarg
;
213 // grab all backup filenames
214 while( optind
< argc
) {
215 backup_files
.push_back(string(argv
[optind
++]));
218 if( backup_files
.size() == 0 ) {
227 // Create an IConverter object if needed
228 auto_ptr
<IConverter
> ic
;
229 if( iconvCharset
.size() ) {
230 ic
.reset( new IConverter(iconvCharset
.c_str(), true) );
233 // create the parser, and use stdout dump objects for output
234 AllRecordParser
parser(cout
,
235 new HexDumpParser(cout
),
236 new MyAllRecordDumpStore(cout
, vformat_mode
));
238 for( size_t i
= 0; i
< backup_files
.size(); i
++ ) {
240 cout
<< "Reading file: " << backup_files
[i
] << endl
;
242 Restore
builder(backup_files
[i
]);
244 // add desired database names
245 for( size_t j
= 0; j
< db_names
.size(); j
++ ) {
246 builder
.AddDB(db_names
[i
]);
249 // create the pipe to connect builder to parser and
252 pipe
.PumpFile(parser
, ic
.get());
256 catch( exception
&e
) {
257 cerr
<< e
.what() << endl
;