lib: added GetPath() to GlobalConfigPath class
[barry.git] / tools / btool.cc
blobc5dccd02b137bfbcd3a4802577980459ccc09777
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-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 <barry/barry.h>
23 #ifdef __BARRY_SYNC_MODE__
24 #include <barry/barrysync.h>
25 #include "mimedump.h"
26 #endif
27 #ifdef __BARRY_BACKUP_MODE__
28 #include <barry/barrybackup.h>
29 #endif
31 #include <iomanip>
32 #include <iostream>
33 #include <fstream>
34 #include <sstream>
35 #include <vector>
36 #include <string>
37 #include <algorithm>
38 #include <stdlib.h>
39 #include <tr1/memory>
40 #include "i18n.h"
42 #include "barrygetopt.h"
44 using namespace std;
45 using namespace std::tr1;
46 using namespace Barry;
48 void Usage()
50 int logical, major, minor;
51 const char *Version = Barry::Version(logical, major, minor);
53 cerr
54 << "btool - Command line USB Blackberry Test Tool\n"
55 << " Copyright 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)\n"
56 << " Using: " << Version << "\n"
57 << " Compiled "
58 #ifdef __BARRY_BOOST_MODE__
59 << "with"
60 #else
61 << "without"
62 #endif
63 << " Boost support\n"
64 << "\n"
65 << " -b file Filename to save or load a Barry Backup to (tar.gz)\n"
66 << " -B bus Specify which USB bus to search on\n"
67 << " -N dev Specify which system device, using system specific string\n"
68 << "\n"
69 << " -a db Erase / clear database 'db' FROM device, deleting all\n"
70 << " its records. Can be used multiple times to clear more\n"
71 << " than one DB.\n"
72 << " -c dn Convert address book database to LDIF format, using the\n"
73 << " specified baseDN\n"
74 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
75 << " Defaults to 'cn'\n"
76 << " -d db Load database 'db' FROM device and dump to screen\n"
77 << " Can be used multiple times to fetch more than one DB\n"
78 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
79 << " string separated by a comma, holding the read,write\n"
80 << " endpoint pair. Example: -e 83,5\n"
81 << " Note: Endpoints are specified in hex.\n"
82 << " You should never need to use this option.\n"
83 #ifdef __BARRY_BOOST_MODE__
84 << " -f file Filename to save or load handheld data to/from\n"
85 #endif
86 << " -h This help\n"
87 << " -i cs International charset for string conversions\n"
88 << " Valid values here are available with 'iconv --list'\n"
89 << " -I Sort records before output\n"
90 << " -l List devices\n"
91 << " -L List Contact field names\n"
92 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
93 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
94 << " Unmap: ldif name alone\n"
95 << " -M List current LDIF mapping\n"
96 << " -n Use null parser on all databases.\n"
97 << " -p pin PIN of device to talk with\n"
98 << " If only one device is plugged in, this flag is optional\n"
99 << " -P pass Simplistic method to specify device password\n"
100 << " -s db Save database 'db' TO device from data loaded from -f file\n"
101 << " -S Show list of supported database parsers\n"
102 << " -t Show database database table\n"
103 << " -T db Show record state table for given database\n"
104 << " -v Dump protocol data during operation\n"
105 #ifdef __BARRY_SYNC_MODE__
106 << " -V Dump records using MIME vformats where possible\n"
107 #endif
108 << " -X Reset device\n"
109 << " -z Use non-threaded sockets\n"
110 << " -Z Use threaded socket router (default)\n"
111 << "\n"
112 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
113 << "\n"
114 << " -r # Record index number as seen in the -T state table.\n"
115 << " This overrides the default -d behaviour, and only\n"
116 << " downloads the one specified record, sending to stdout.\n"
117 << " -R # Same as -r, but also clears the record's dirty flags.\n"
118 << " -D # Record index number as seen in the -T state table,\n"
119 << " which indicates the record to delete. Used with the -d\n"
120 << " command to specify the database.\n"
121 << endl;
124 class Contact2Ldif
126 public:
127 Barry::ContactLdif &ldif;
129 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
131 void operator()(const Contact &rec)
133 ldif.DumpLdif(cout, rec);
137 template <class Record>
138 struct Store
140 std::vector<Record> records;
141 mutable typename std::vector<Record>::const_iterator rec_it;
142 std::string filename;
143 bool load;
144 bool immediate_display;
145 bool vformat_mode;
146 int from_device_count;
147 mutable int to_device_count;
149 Store(const string &filename, bool load, bool immediate_display,
150 bool vformat_mode)
151 : rec_it(records.end()),
152 filename(filename),
153 load(load),
154 immediate_display(immediate_display),
155 vformat_mode(vformat_mode),
156 from_device_count(0),
157 to_device_count(0)
159 #ifdef __BARRY_BOOST_MODE__
160 try {
162 if( load && filename.size() ) {
163 // filename is available, attempt to load
164 cout << "Loading: " << filename << endl;
165 ifstream ifs(filename.c_str());
166 std::string dbName;
167 getline(ifs, dbName);
168 boost::archive::text_iarchive ia(ifs);
169 ia >> records;
170 cout << records.size()
171 << " records loaded from '"
172 << filename << "'" << endl;
173 sort(records.begin(), records.end());
174 rec_it = records.begin();
176 // debugging aid
177 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
178 for( ; beg != end; beg++ ) {
179 cout << (*beg) << endl;
183 } catch( boost::archive::archive_exception &ae ) {
184 cerr << "Archive exception in ~Store(): "
185 << ae.what() << endl;
187 #endif
190 ~Store()
192 if( !immediate_display ) {
193 // not dumped yet, sort then dump
194 sort(records.begin(), records.end());
195 DumpAll();
198 cout << "Store counted " << dec << from_device_count << " records read from device, and " << dec << to_device_count << " records written to device." << endl;
199 #ifdef __BARRY_BOOST_MODE__
200 try {
202 if( !load && filename.size() ) {
203 // filename is available, attempt to save
204 cout << "Saving: " << filename << endl;
205 const std::vector<Record> &r = records;
206 ofstream ofs(filename.c_str());
207 ofs << Record::GetDBName() << endl;
208 boost::archive::text_oarchive oa(ofs);
209 oa << r;
210 cout << dec << r.size() << " records saved to '"
211 << filename << "'" << endl;
214 } catch( boost::archive::archive_exception &ae ) {
215 cerr << "Archive exception in ~Store(): "
216 << ae.what() << endl;
218 #endif
221 void DumpAll()
223 typename vector<Record>::const_iterator i = records.begin();
224 for( ; i != records.end(); ++i ) {
225 Dump(*i);
229 void Dump(const Record &rec)
231 if( vformat_mode ) {
232 #ifdef __BARRY_SYNC_MODE__
233 MimeDump<Record> md;
234 md.Dump(cout, rec);
235 #endif
237 else {
238 cout << rec << endl;
242 // storage operator
243 void operator()(const Record &rec)
245 from_device_count++;
246 if( immediate_display )
247 Dump(rec);
248 records.push_back(rec);
251 // retrieval operator
252 bool operator()(Record &rec, Builder &builder) const
254 if( rec_it == records.end() )
255 return false;
256 to_device_count++;
257 rec = *rec_it;
258 rec_it++;
259 return true;
263 shared_ptr<Parser> GetParser(const string &name,
264 const string &filename,
265 bool null_parser,
266 bool immediate_display,
267 bool vformat_mode,
268 bool bbackup_mode)
270 bool dnow = immediate_display;
271 bool vmode = vformat_mode;
273 if( null_parser ) {
274 // use null parser
275 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
277 else if( bbackup_mode ) {
278 #ifdef __BARRY_BACKUP_MODE__
279 // Only one backup file per run
280 static shared_ptr<Parser> backup;
281 if( !backup.get() ) {
282 backup.reset( new Backup(filename) );
284 return backup;
285 #else
286 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
287 #endif
289 // check for recognized database names
290 else if( name == Contact::GetDBName() ) {
291 return shared_ptr<Parser>(
292 new RecordParser<Contact, Store<Contact> > (
293 new Store<Contact>(filename, false, dnow, vmode)));
295 else if( name == Message::GetDBName() ) {
296 return shared_ptr<Parser>(
297 new RecordParser<Message, Store<Message> > (
298 new Store<Message>(filename, false, dnow, vmode)));
300 else if( name == Calendar::GetDBName() ) {
301 return shared_ptr<Parser>(
302 new RecordParser<Calendar, Store<Calendar> > (
303 new Store<Calendar>(filename, false, dnow, vmode)));
305 else if( name == CalendarAll::GetDBName() ) {
306 return shared_ptr<Parser>(
307 new RecordParser<CalendarAll, Store<CalendarAll> > (
308 new Store<CalendarAll>(filename, false, dnow, vmode)));
310 else if( name == CallLog::GetDBName() ) {
311 return shared_ptr<Parser>(
312 new RecordParser<CallLog, Store<CallLog> > (
313 new Store<CallLog>(filename, false, dnow, vmode)));
315 else if( name == Bookmark::GetDBName() ) {
316 return shared_ptr<Parser>(
317 new RecordParser<Bookmark, Store<Bookmark> > (
318 new Store<Bookmark>(filename, false, dnow, vmode)));
320 else if( name == ServiceBook::GetDBName() ) {
321 return shared_ptr<Parser>(
322 new RecordParser<ServiceBook, Store<ServiceBook> > (
323 new Store<ServiceBook>(filename, false, dnow, vmode)));
326 else if( name == Memo::GetDBName() ) {
327 return shared_ptr<Parser>(
328 new RecordParser<Memo, Store<Memo> > (
329 new Store<Memo>(filename, false, dnow, vmode)));
331 else if( name == Task::GetDBName() ) {
332 return shared_ptr<Parser>(
333 new RecordParser<Task, Store<Task> > (
334 new Store<Task>(filename, false, dnow, vmode)));
336 else if( name == PINMessage::GetDBName() ) {
337 return shared_ptr<Parser>(
338 new RecordParser<PINMessage, Store<PINMessage> > (
339 new Store<PINMessage>(filename, false, dnow, vmode)));
341 else if( name == SavedMessage::GetDBName() ) {
342 return shared_ptr<Parser>(
343 new RecordParser<SavedMessage, Store<SavedMessage> > (
344 new Store<SavedMessage>(filename, false, dnow, vmode)));
346 else if( name == Sms::GetDBName() ) {
347 return shared_ptr<Parser>(
348 new RecordParser<Sms, Store<Sms> > (
349 new Store<Sms>(filename, false, dnow, vmode)));
351 else if( name == Folder::GetDBName() ) {
352 return shared_ptr<Parser>(
353 new RecordParser<Folder, Store<Folder> > (
354 new Store<Folder>(filename, false, dnow, vmode)));
356 else if( name == Timezone::GetDBName() ) {
357 return shared_ptr<Parser>(
358 new RecordParser<Timezone, Store<Timezone> > (
359 new Store<Timezone>(filename, false, dnow, vmode)));
361 else if( name == HandheldAgent::GetDBName() ) {
362 return shared_ptr<Parser>(
363 new RecordParser<HandheldAgent, Store<HandheldAgent> > (
364 new Store<HandheldAgent>(filename, false, dnow, vmode)));
366 else {
367 // unknown database, use null parser
368 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
372 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
374 // check for recognized database names
375 if( name == Contact::GetDBName() ) {
376 return shared_ptr<Builder>(
377 new RecordBuilder<Contact, Store<Contact> > (
378 new Store<Contact>(filename, true, true, false)));
380 else if( name == Calendar::GetDBName() ) {
381 return shared_ptr<Builder>(
382 new RecordBuilder<Calendar, Store<Calendar> > (
383 new Store<Calendar>(filename, true, true, false)));
385 else if( name == CalendarAll::GetDBName() ) {
386 return shared_ptr<Builder>(
387 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
388 new Store<CalendarAll>(filename, true, true, false)));
390 else if( name == Memo::GetDBName() ) {
391 return shared_ptr<Builder>(
392 new RecordBuilder<Memo, Store<Memo> > (
393 new Store<Memo>(filename, true, true, false)));
395 else if( name == Task::GetDBName() ) {
396 return shared_ptr<Builder>(
397 new RecordBuilder<Task, Store<Task> > (
398 new Store<Task>(filename, true, true, false)));
401 else if( name == "Messages" ) {
402 return shared_ptr<Parser>(
403 new RecordParser<Message, Store<Message> > (
404 new Store<Message>(filename, true, true, false)));
406 else if( name == "Service Book" ) {
407 return shared_ptr<Parser>(
408 new RecordParser<ServiceBook, Store<ServiceBook> > (
409 new Store<ServiceBook>(filename, true, true, false)));
412 else {
413 throw std::runtime_error("No Builder available for database");
417 void ShowParsers()
419 cout << "Supported Database parsers:\n"
420 #undef HANDLE_PARSER
421 #ifdef __BARRY_SYNC_MODE__
422 << " (* = can display in vformat MIME mode)\n"
423 #define HANDLE_PARSER(tname) << " " << tname::GetDBName() << (MimeDump<tname>::Supported() ? " *" : "") << "\n"
425 #else
426 #define HANDLE_PARSER(tname) << " " << tname::GetDBName() << "\n"
428 #endif
429 ALL_KNOWN_PARSER_TYPES
431 << "\n"
433 << "Supported Database builders:\n"
434 #undef HANDLE_BUILDER
435 #define HANDLE_BUILDER(tname) << " " << tname::GetDBName() << "\n"
436 ALL_KNOWN_BUILDER_TYPES
437 << endl;
440 struct StateTableCommand
442 char flag;
443 bool clear;
444 unsigned int index;
446 StateTableCommand(char f, bool c, unsigned int i)
447 : flag(f), clear(c), index(i) {}
450 bool SplitMap(const string &map, string &ldif, string &read, string &write)
452 string::size_type a = map.find(',');
453 if( a == string::npos )
454 return false;
456 string::size_type b = map.find(',', a+1);
457 if( b == string::npos )
458 return false;
460 ldif.assign(map, 0, a);
461 read.assign(map, a + 1, b - a - 1);
462 write.assign(map, b + 1, map.size() - b - 1);
464 return ldif.size() && read.size() && write.size();
467 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
469 for( vector<string>::const_iterator i = mapCommands.begin();
470 i != mapCommands.end();
471 ++i )
473 // single names mean unmapping
474 if( i->find(',') == string::npos ) {
475 // unmap
476 cerr << "Unmapping: " << *i << endl;
477 ldif.Unmap(*i);
479 else {
480 cerr << "Mapping: " << *i << endl;
482 // map... extract ldif/read/write names
483 string ldifname, read, write;
484 if( SplitMap(*i, ldifname, read, write) ) {
485 if( !ldif.Map(ldifname, read, write) ) {
486 cerr << "Read/Write name unknown: " << *i << endl;
489 else {
490 cerr << "Invalid map format: " << *i << endl;
496 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
498 int read, write;
499 char comma;
500 istringstream iss(arg);
501 iss >> hex >> read >> comma >> write;
502 if( !iss )
503 return false;
504 epp->read = read;
505 epp->write = write;
506 return true;
509 int main(int argc, char *argv[])
511 INIT_I18N(PACKAGE);
513 cout.sync_with_stdio(true); // leave this on, since libusb uses
514 // stdio for debug messages
516 try {
518 uint32_t pin = 0;
519 bool list_only = false,
520 show_dbdb = false,
521 ldif_contacts = false,
522 data_dump = false,
523 vformat_mode = false,
524 reset_device = false,
525 list_contact_fields = false,
526 list_ldif_map = false,
527 epp_override = false,
528 threaded_sockets = true,
529 record_state_table = false,
530 clear_database = false,
531 null_parser = false,
532 bbackup_mode = false,
533 sort_records = false;
534 string ldifBaseDN, ldifDnAttr;
535 string filename;
536 string password;
537 string busname;
538 string devname;
539 string iconvCharset;
540 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
541 vector<StateTableCommand> stCommands;
542 Usb::EndpointPair epOverride;
544 // process command line options
545 for(;;) {
546 int cmd = getopt(argc, argv, "a:b:B:c:C:d:D:e:f:hi:IlLm:MnN:p:P:r:R:Ss:tT:vVXzZ");
547 if( cmd == -1 )
548 break;
550 switch( cmd )
552 case 'a': // Clear Database
553 clear_database = true;
554 clearDbNames.push_back(string(optarg));
555 break;
557 case 'b': // Barry backup filename (tar.gz)
558 #ifdef __BARRY_BACKUP_MODE__
559 if( filename.size() == 0 ) {
560 filename = optarg;
561 bbackup_mode = true;
563 else {
564 cerr << "Do not use -f with -b\n";
565 return 1;
567 #else
568 cerr << "-b option not supported - no Barry "
569 "Backup library support available\n";
570 return 1;
571 #endif
572 break;
574 case 'B': // busname
575 busname = optarg;
576 break;
578 case 'c': // contacts to ldap ldif
579 ldif_contacts = true;
580 ldifBaseDN = optarg;
581 break;
583 case 'C': // DN Attribute for FQDN
584 ldifDnAttr = optarg;
585 break;
587 case 'd': // show dbname
588 dbNames.push_back(string(optarg));
589 break;
591 case 'D': // delete record
592 stCommands.push_back(
593 StateTableCommand('D', false, atoi(optarg)));
594 break;
596 case 'e': // endpoint override
597 if( !ParseEpOverride(optarg, &epOverride) ) {
598 Usage();
599 return 1;
601 epp_override = true;
602 break;
604 case 'f': // filename
605 #ifdef __BARRY_BOOST_MODE__
606 if( !bbackup_mode && filename.size() == 0 ) {
607 filename = optarg;
609 else {
610 cerr << "Do not use -f with -b\n";
611 return 1;
613 #else
614 cerr << "-f option not supported - no Boost "
615 "serialization support available\n";
616 return 1;
617 #endif
618 break;
620 case 'i': // international charset (iconv)
621 iconvCharset = optarg;
622 break;
624 case 'I': // sort before dump
625 sort_records = true;
626 break;
628 case 'l': // list only
629 list_only = true;
630 break;
632 case 'L': // List Contact field names
633 list_contact_fields = true;
634 break;
636 case 'm': // Map / Unmap
637 mapCommands.push_back(string(optarg));
638 break;
640 case 'M': // List LDIF map
641 list_ldif_map = true;
642 break;
644 case 'n': // use null parser
645 null_parser = true;
646 break;
648 case 'N': // Devname
649 devname = optarg;
650 break;
652 case 'p': // Blackberry PIN
653 pin = strtoul(optarg, NULL, 16);
654 break;
656 case 'P': // Device password
657 password = optarg;
658 break;
660 case 'r': // get specific record index
661 stCommands.push_back(
662 StateTableCommand('r', false, atoi(optarg)));
663 break;
665 case 'R': // same as 'r', and clears dirty
666 stCommands.push_back(
667 StateTableCommand('r', true, atoi(optarg)));
668 break;
670 case 's': // save dbname
671 saveDbNames.push_back(string(optarg));
672 break;
674 case 'S': // show supported databases
675 ShowParsers();
676 return 0;
678 case 't': // display database database
679 show_dbdb = true;
680 break;
682 case 'T': // show RecordStateTable
683 record_state_table = true;
684 dbNames.push_back(string(optarg));
685 break;
687 case 'v': // data dump on
688 data_dump = true;
689 break;
691 case 'V': // vformat MIME mode
692 #ifdef __BARRY_SYNC_MODE__
693 vformat_mode = true;
694 #else
695 cerr << "-V option not supported - no Sync "
696 "library support available\n";
697 return 1;
698 #endif
699 break;
701 case 'X': // reset device
702 reset_device = true;
703 break;
705 case 'z': // non-threaded sockets
706 threaded_sockets = false;
707 break;
709 case 'Z': // threaded socket router
710 threaded_sockets = true;
711 break;
713 case 'h': // help
714 default:
715 Usage();
716 return 0;
720 // Initialize the barry library. Must be called before
721 // anything else.
722 Barry::Init(data_dump);
723 if( data_dump ) {
724 int logical, major, minor;
725 const char *Version = Barry::Version(logical, major, minor);
726 cout << Version << endl;
729 // Create an IConverter object if needed
730 auto_ptr<IConverter> ic;
731 if( iconvCharset.size() ) {
732 ic.reset( new IConverter(iconvCharset.c_str(), true) );
735 // LDIF class... only needed if ldif output turned on
736 ContactLdif ldif(ldifBaseDN);
737 DoMapping(ldif, mapCommands);
738 if( ldifDnAttr.size() ) {
739 if( !ldif.SetDNAttr(ldifDnAttr) ) {
740 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
744 // Probe the USB bus for Blackberry devices and display.
745 // If user has specified a PIN, search for it in the
746 // available device list here as well
747 Barry::Probe probe(busname.c_str(), devname.c_str(),
748 epp_override ? &epOverride : 0);
749 int activeDevice = -1;
751 // show any errors during probe first
752 if( probe.GetFailCount() ) {
753 if( ldif_contacts )
754 cout << "# ";
755 cout << "Blackberry device errors with errors during probe:" << endl;
756 for( int i = 0; i < probe.GetFailCount(); i++ ) {
757 if( ldif_contacts )
758 cout << "# ";
759 cout << probe.GetFailMsg(i) << endl;
763 // show all successfully found devices
764 if( ldif_contacts )
765 cout << "# ";
766 cout << "Blackberry devices found:" << endl;
767 for( int i = 0; i < probe.GetCount(); i++ ) {
768 if( ldif_contacts )
769 cout << "# ";
770 if( data_dump )
771 probe.Get(i).DumpAll(cout);
772 else
773 cout << probe.Get(i);
774 cout << endl;
775 if( probe.Get(i).m_pin == pin )
776 activeDevice = i;
779 if( list_only )
780 return 0; // done
782 if( activeDevice == -1 ) {
783 if( pin == 0 ) {
784 // can we default to single device?
785 if( probe.GetCount() == 1 )
786 activeDevice = 0;
787 else {
788 cerr << "No device selected" << endl;
789 return 1;
792 else {
793 cerr << "PIN " << setbase(16) << pin
794 << " not found" << endl;
795 return 1;
799 if( ldif_contacts )
800 cout << "# ";
801 cout << "Using device (PIN): "
802 << probe.Get(activeDevice).m_pin.Str() << endl;
804 if( reset_device ) {
805 Usb::Device dev(probe.Get(activeDevice).m_dev);
806 dev.Reset();
807 return 0;
810 // Override device endpoints if user asks
811 Barry::ProbeResult device = probe.Get(activeDevice);
812 if( epp_override ) {
813 device.m_ep.read = epOverride.read;
814 device.m_ep.write = epOverride.write;
815 // FIXME - override this too?
816 device.m_ep.type = Usb::EndpointDescriptor::BulkType;
817 cout << "Endpoint pair (read,write) overridden with: "
818 << hex
819 << (unsigned int) device.m_ep.read << ","
820 << (unsigned int) device.m_ep.write << endl;
824 // execute each mode that was turned on
828 // Dump current LDIF mapping
829 if( list_ldif_map ) {
830 cout << ldif << endl;
833 // Dump list of Contact field names
834 if( list_contact_fields ) {
835 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
836 cout.fill(' ');
837 cout << " " << left << setw(20) << n->name << ": "
838 << n->description << endl;
842 // Check if Desktop access is needed
843 if( !( show_dbdb ||
844 ldif_contacts ||
845 record_state_table ||
846 clear_database ||
847 stCommands.size() ||
848 dbNames.size() ||
849 saveDbNames.size() ) )
850 return 0; // done
853 // Create our controller object
855 // Order is important in the following auto_ptr<> objects,
856 // since Controller must get destroyed before router.
857 // Normally you'd pick one method, and not bother
858 // with auto_ptr<> and so the normal C++ constructor
859 // rules would guarantee this safety for you, but
860 // here we want the user to pick.
862 auto_ptr<SocketRoutingQueue> router;
863 if( threaded_sockets ) {
864 router.reset( new SocketRoutingQueue );
865 router->SpinoffSimpleReadThread();
868 DesktopConnector connector(password.c_str(),
869 iconvCharset, device, router.get());
870 if( !connector.Connect() ) {
871 // bad password (default action is not to prompt)
872 cerr << connector.GetBadPassword().what() << endl;
873 return 1;
876 Barry::Mode::Desktop &desktop = connector.GetDesktop();
878 // Dump list of all databases to stdout
879 if( show_dbdb ) {
880 // open desktop mode socket
881 cout << desktop.GetDBDB() << endl;
884 // Dump list of contacts to an LDAP LDIF file
885 // This uses the Controller convenience templates
886 if( ldif_contacts ) {
887 // create a storage functor object that accepts
888 // Barry::Contact objects as input
889 Contact2Ldif storage(ldif);
891 // load all the Contact records into storage
892 desktop.LoadDatabaseByType<Barry::Contact>(storage);
895 // Dump record state table to stdout
896 if( record_state_table ) {
897 if( dbNames.size() == 0 ) {
898 cout << "No db names to process" << endl;
899 return 1;
902 vector<string>::iterator b = dbNames.begin();
903 for( ; b != dbNames.end(); b++ ) {
904 unsigned int id = desktop.GetDBID(*b);
905 RecordStateTable state;
906 desktop.GetRecordStateTable(id, state);
907 cout << "Record state table for: " << *b << endl;
908 cout << state;
910 return 0;
913 // Get Record mode overrides the default name mode
914 if( stCommands.size() ) {
915 if( dbNames.size() != 1 ) {
916 cout << "Must have 1 db name to process" << endl;
917 return 1;
920 unsigned int id = desktop.GetDBID(dbNames[0]);
921 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
922 null_parser, true, vformat_mode, bbackup_mode);
924 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
925 desktop.GetRecord(id, stCommands[i].index, *parse.get());
927 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
928 cout << "Clearing record's dirty flags..." << endl;
929 desktop.ClearDirty(id, stCommands[i].index);
932 if( stCommands[i].flag == 'D' ) {
933 desktop.DeleteRecord(id, stCommands[i].index);
937 return 0;
940 // Dump contents of selected databases to stdout, or
941 // to file if specified.
942 // This is retrieving data from the Blackberry.
943 if( dbNames.size() ) {
944 vector<string>::iterator b = dbNames.begin();
946 for( ; b != dbNames.end(); b++ ) {
947 shared_ptr<Parser> parse = GetParser(*b,
948 filename, null_parser, !sort_records,
949 vformat_mode, bbackup_mode);
950 unsigned int id = desktop.GetDBID(*b);
951 desktop.LoadDatabase(id, *parse.get());
955 // Clear databases
956 if( clear_database ) {
957 if( clearDbNames.size() == 0 ) {
958 cout << "No db names to erase" << endl;
959 return 1;
962 vector<string>::iterator b = clearDbNames.begin();
964 for( ; b != clearDbNames.end(); b++ ) {
965 unsigned int id = desktop.GetDBID(*b);
966 cout << "Deleting all records from " << (*b) << "..." << endl;
967 desktop.ClearDatabase(id);
970 return 0;
973 // Save contents of file to specified databases
974 // This is writing data to the Blackberry.
975 if( saveDbNames.size() ) {
976 vector<string>::iterator b = saveDbNames.begin();
978 for( ; b != saveDbNames.end(); b++ ) {
979 shared_ptr<Builder> build = GetBuilder(*b,
980 filename);
981 unsigned int id = desktop.GetDBID(*b);
982 desktop.SaveDatabase(id, *build);
987 catch( Usb::Error &ue ) {
988 std::cerr << "Usb::Error caught: " << ue.what() << endl;
989 return 1;
991 catch( Barry::Error &se ) {
992 std::cerr << "Barry::Error caught: " << se.what() << endl;
993 return 1;
995 catch( std::exception &e ) {
996 std::cerr << "std::exception caught: " << e.what() << endl;
997 return 1;
1000 return 0;