desktop: made it possible to run Detect() multiple times in EvoSources
[barry/progweb.git] / tools / btool.cc
blobcc4ef2b263ca296177232cdde5f7e42f6b65465d
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-2011, 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 <getopt.h>
39 #include <stdlib.h>
40 #include <tr1/memory>
41 #include "i18n.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-2011, 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 count;
148 Store(const string &filename, bool load, bool immediate_display,
149 bool vformat_mode)
150 : rec_it(records.end()),
151 filename(filename),
152 load(load),
153 immediate_display(immediate_display),
154 vformat_mode(vformat_mode),
155 count(0)
157 #ifdef __BARRY_BOOST_MODE__
158 try {
160 if( load && filename.size() ) {
161 // filename is available, attempt to load
162 cout << "Loading: " << filename << endl;
163 ifstream ifs(filename.c_str());
164 std::string dbName;
165 getline(ifs, dbName);
166 boost::archive::text_iarchive ia(ifs);
167 ia >> records;
168 cout << records.size()
169 << " records loaded from '"
170 << filename << "'" << endl;
171 sort(records.begin(), records.end());
172 rec_it = records.begin();
174 // debugging aid
175 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
176 for( ; beg != end; beg++ ) {
177 cout << (*beg) << endl;
181 } catch( boost::archive::archive_exception &ae ) {
182 cerr << "Archive exception in ~Store(): "
183 << ae.what() << endl;
185 #endif
188 ~Store()
190 if( !immediate_display ) {
191 // not dumped yet, sort then dump
192 sort(records.begin(), records.end());
193 DumpAll();
196 cout << "Store counted " << dec << count << " records." << endl;
197 #ifdef __BARRY_BOOST_MODE__
198 try {
200 if( !load && filename.size() ) {
201 // filename is available, attempt to save
202 cout << "Saving: " << filename << endl;
203 const std::vector<Record> &r = records;
204 ofstream ofs(filename.c_str());
205 ofs << Record::GetDBName() << endl;
206 boost::archive::text_oarchive oa(ofs);
207 oa << r;
208 cout << dec << r.size() << " records saved to '"
209 << filename << "'" << endl;
212 } catch( boost::archive::archive_exception &ae ) {
213 cerr << "Archive exception in ~Store(): "
214 << ae.what() << endl;
216 #endif
219 void DumpAll()
221 typename vector<Record>::const_iterator i = records.begin();
222 for( ; i != records.end(); ++i ) {
223 Dump(*i);
227 void Dump(const Record &rec)
229 if( vformat_mode ) {
230 #ifdef __BARRY_SYNC_MODE__
231 MimeDump<Record> md;
232 md.Dump(cout, rec);
233 #endif
235 else {
236 cout << rec << endl;
240 // storage operator
241 void operator()(const Record &rec)
243 count++;
244 if( immediate_display )
245 Dump(rec);
246 records.push_back(rec);
249 // retrieval operator
250 bool operator()(Record &rec, Builder &builder) const
252 if( rec_it == records.end() )
253 return false;
254 rec = *rec_it;
255 rec_it++;
256 return true;
260 shared_ptr<Parser> GetParser(const string &name,
261 const string &filename,
262 bool null_parser,
263 bool immediate_display,
264 bool vformat_mode,
265 bool bbackup_mode)
267 bool dnow = immediate_display;
268 bool vmode = vformat_mode;
270 if( null_parser ) {
271 // use null parser
272 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
274 else if( bbackup_mode ) {
275 #ifdef __BARRY_BACKUP_MODE__
276 // Only one backup file per run
277 static shared_ptr<Parser> backup;
278 if( !backup.get() ) {
279 backup.reset( new Backup(filename) );
281 return backup;
282 #else
283 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
284 #endif
286 // check for recognized database names
287 else if( name == Contact::GetDBName() ) {
288 return shared_ptr<Parser>(
289 new RecordParser<Contact, Store<Contact> > (
290 new Store<Contact>(filename, false, dnow, vmode)));
292 else if( name == Message::GetDBName() ) {
293 return shared_ptr<Parser>(
294 new RecordParser<Message, Store<Message> > (
295 new Store<Message>(filename, false, dnow, vmode)));
297 else if( name == Calendar::GetDBName() ) {
298 return shared_ptr<Parser>(
299 new RecordParser<Calendar, Store<Calendar> > (
300 new Store<Calendar>(filename, false, dnow, vmode)));
302 else if( name == CalendarAll::GetDBName() ) {
303 return shared_ptr<Parser>(
304 new RecordParser<CalendarAll, Store<CalendarAll> > (
305 new Store<CalendarAll>(filename, false, dnow, vmode)));
307 else if( name == CallLog::GetDBName() ) {
308 return shared_ptr<Parser>(
309 new RecordParser<CallLog, Store<CallLog> > (
310 new Store<CallLog>(filename, false, dnow, vmode)));
312 else if( name == Bookmark::GetDBName() ) {
313 return shared_ptr<Parser>(
314 new RecordParser<Bookmark, Store<Bookmark> > (
315 new Store<Bookmark>(filename, false, dnow, vmode)));
317 else if( name == ServiceBook::GetDBName() ) {
318 return shared_ptr<Parser>(
319 new RecordParser<ServiceBook, Store<ServiceBook> > (
320 new Store<ServiceBook>(filename, false, dnow, vmode)));
323 else if( name == Memo::GetDBName() ) {
324 return shared_ptr<Parser>(
325 new RecordParser<Memo, Store<Memo> > (
326 new Store<Memo>(filename, false, dnow, vmode)));
328 else if( name == Task::GetDBName() ) {
329 return shared_ptr<Parser>(
330 new RecordParser<Task, Store<Task> > (
331 new Store<Task>(filename, false, dnow, vmode)));
333 else if( name == PINMessage::GetDBName() ) {
334 return shared_ptr<Parser>(
335 new RecordParser<PINMessage, Store<PINMessage> > (
336 new Store<PINMessage>(filename, false, dnow, vmode)));
338 else if( name == SavedMessage::GetDBName() ) {
339 return shared_ptr<Parser>(
340 new RecordParser<SavedMessage, Store<SavedMessage> > (
341 new Store<SavedMessage>(filename, false, dnow, vmode)));
343 else if( name == Sms::GetDBName() ) {
344 return shared_ptr<Parser>(
345 new RecordParser<Sms, Store<Sms> > (
346 new Store<Sms>(filename, false, dnow, vmode)));
348 else if( name == Folder::GetDBName() ) {
349 return shared_ptr<Parser>(
350 new RecordParser<Folder, Store<Folder> > (
351 new Store<Folder>(filename, false, dnow, vmode)));
353 else if( name == Timezone::GetDBName() ) {
354 return shared_ptr<Parser>(
355 new RecordParser<Timezone, Store<Timezone> > (
356 new Store<Timezone>(filename, false, dnow, vmode)));
358 else if( name == HandheldAgent::GetDBName() ) {
359 return shared_ptr<Parser>(
360 new RecordParser<HandheldAgent, Store<HandheldAgent> > (
361 new Store<HandheldAgent>(filename, false, dnow, vmode)));
363 else {
364 // unknown database, use null parser
365 return shared_ptr<Parser>( new Barry::HexDumpParser(cout) );
369 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
371 // check for recognized database names
372 if( name == Contact::GetDBName() ) {
373 return shared_ptr<Builder>(
374 new RecordBuilder<Contact, Store<Contact> > (
375 new Store<Contact>(filename, true, true, false)));
377 else if( name == Calendar::GetDBName() ) {
378 return shared_ptr<Builder>(
379 new RecordBuilder<Calendar, Store<Calendar> > (
380 new Store<Calendar>(filename, true, true, false)));
382 else if( name == CalendarAll::GetDBName() ) {
383 return shared_ptr<Builder>(
384 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
385 new Store<CalendarAll>(filename, true, true, false)));
387 else if( name == Memo::GetDBName() ) {
388 return shared_ptr<Builder>(
389 new RecordBuilder<Memo, Store<Memo> > (
390 new Store<Memo>(filename, true, true, false)));
392 else if( name == Task::GetDBName() ) {
393 return shared_ptr<Builder>(
394 new RecordBuilder<Task, Store<Task> > (
395 new Store<Task>(filename, true, true, false)));
398 else if( name == "Messages" ) {
399 return shared_ptr<Parser>(
400 new RecordParser<Message, Store<Message> > (
401 new Store<Message>(filename, true, true, false)));
403 else if( name == "Service Book" ) {
404 return shared_ptr<Parser>(
405 new RecordParser<ServiceBook, Store<ServiceBook> > (
406 new Store<ServiceBook>(filename, true, true, false)));
409 else {
410 throw std::runtime_error("No Builder available for database");
414 void ShowParsers()
416 cout << "Supported Database parsers:\n"
417 #undef HANDLE_PARSER
418 #ifdef __BARRY_SYNC_MODE__
419 << " (* = can display in vformat MIME mode)\n"
420 #define HANDLE_PARSER(tname) << " " << tname::GetDBName() << (MimeDump<tname>::Supported() ? " *" : "") << "\n"
422 #else
423 #define HANDLE_PARSER(tname) << " " << tname::GetDBName() << "\n"
425 #endif
426 ALL_KNOWN_PARSER_TYPES
428 << "\n"
430 << "Supported Database builders:\n"
431 #undef HANDLE_BUILDER
432 #define HANDLE_BUILDER(tname) << " " << tname::GetDBName() << "\n"
433 ALL_KNOWN_BUILDER_TYPES
434 << endl;
437 struct StateTableCommand
439 char flag;
440 bool clear;
441 unsigned int index;
443 StateTableCommand(char f, bool c, unsigned int i)
444 : flag(f), clear(c), index(i) {}
447 bool SplitMap(const string &map, string &ldif, string &read, string &write)
449 string::size_type a = map.find(',');
450 if( a == string::npos )
451 return false;
453 string::size_type b = map.find(',', a+1);
454 if( b == string::npos )
455 return false;
457 ldif.assign(map, 0, a);
458 read.assign(map, a + 1, b - a - 1);
459 write.assign(map, b + 1, map.size() - b - 1);
461 return ldif.size() && read.size() && write.size();
464 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
466 for( vector<string>::const_iterator i = mapCommands.begin();
467 i != mapCommands.end();
468 ++i )
470 // single names mean unmapping
471 if( i->find(',') == string::npos ) {
472 // unmap
473 cerr << "Unmapping: " << *i << endl;
474 ldif.Unmap(*i);
476 else {
477 cerr << "Mapping: " << *i << endl;
479 // map... extract ldif/read/write names
480 string ldifname, read, write;
481 if( SplitMap(*i, ldifname, read, write) ) {
482 if( !ldif.Map(ldifname, read, write) ) {
483 cerr << "Read/Write name unknown: " << *i << endl;
486 else {
487 cerr << "Invalid map format: " << *i << endl;
493 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
495 int read, write;
496 char comma;
497 istringstream iss(arg);
498 iss >> hex >> read >> comma >> write;
499 if( !iss )
500 return false;
501 epp->read = read;
502 epp->write = write;
503 return true;
506 int main(int argc, char *argv[])
508 INIT_I18N(PACKAGE);
510 cout.sync_with_stdio(true); // leave this on, since libusb uses
511 // stdio for debug messages
513 try {
515 uint32_t pin = 0;
516 bool list_only = false,
517 show_dbdb = false,
518 ldif_contacts = false,
519 data_dump = false,
520 vformat_mode = false,
521 reset_device = false,
522 list_contact_fields = false,
523 list_ldif_map = false,
524 epp_override = false,
525 threaded_sockets = true,
526 record_state_table = false,
527 clear_database = false,
528 null_parser = false,
529 bbackup_mode = false,
530 sort_records = false;
531 string ldifBaseDN, ldifDnAttr;
532 string filename;
533 string password;
534 string busname;
535 string devname;
536 string iconvCharset;
537 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
538 vector<StateTableCommand> stCommands;
539 Usb::EndpointPair epOverride;
541 // process command line options
542 for(;;) {
543 int cmd = getopt(argc, argv, "a:b:B:c:C:d:D:e:f:hi:IlLm:MnN:p:P:r:R:Ss:tT:vVXzZ");
544 if( cmd == -1 )
545 break;
547 switch( cmd )
549 case 'a': // Clear Database
550 clear_database = true;
551 clearDbNames.push_back(string(optarg));
552 break;
554 case 'b': // Barry backup filename (tar.gz)
555 #ifdef __BARRY_BACKUP_MODE__
556 if( filename.size() == 0 ) {
557 filename = optarg;
558 bbackup_mode = true;
560 else {
561 cerr << "Do not use -f with -b\n";
562 return 1;
564 #else
565 cerr << "-b option not supported - no Barry "
566 "Backup library support available\n";
567 return 1;
568 #endif
569 break;
571 case 'B': // busname
572 busname = optarg;
573 break;
575 case 'c': // contacts to ldap ldif
576 ldif_contacts = true;
577 ldifBaseDN = optarg;
578 break;
580 case 'C': // DN Attribute for FQDN
581 ldifDnAttr = optarg;
582 break;
584 case 'd': // show dbname
585 dbNames.push_back(string(optarg));
586 break;
588 case 'D': // delete record
589 stCommands.push_back(
590 StateTableCommand('D', false, atoi(optarg)));
591 break;
593 case 'e': // endpoint override
594 if( !ParseEpOverride(optarg, &epOverride) ) {
595 Usage();
596 return 1;
598 epp_override = true;
599 break;
601 case 'f': // filename
602 #ifdef __BARRY_BOOST_MODE__
603 if( !bbackup_mode && filename.size() == 0 ) {
604 filename = optarg;
606 else {
607 cerr << "Do not use -f with -b\n";
608 return 1;
610 #else
611 cerr << "-f option not supported - no Boost "
612 "serialization support available\n";
613 return 1;
614 #endif
615 break;
617 case 'i': // international charset (iconv)
618 iconvCharset = optarg;
619 break;
621 case 'I': // sort before dump
622 sort_records = true;
623 break;
625 case 'l': // list only
626 list_only = true;
627 break;
629 case 'L': // List Contact field names
630 list_contact_fields = true;
631 break;
633 case 'm': // Map / Unmap
634 mapCommands.push_back(string(optarg));
635 break;
637 case 'M': // List LDIF map
638 list_ldif_map = true;
639 break;
641 case 'n': // use null parser
642 null_parser = true;
643 break;
645 case 'N': // Devname
646 devname = optarg;
647 break;
649 case 'p': // Blackberry PIN
650 pin = strtoul(optarg, NULL, 16);
651 break;
653 case 'P': // Device password
654 password = optarg;
655 break;
657 case 'r': // get specific record index
658 stCommands.push_back(
659 StateTableCommand('r', false, atoi(optarg)));
660 break;
662 case 'R': // same as 'r', and clears dirty
663 stCommands.push_back(
664 StateTableCommand('r', true, atoi(optarg)));
665 break;
667 case 's': // save dbname
668 saveDbNames.push_back(string(optarg));
669 break;
671 case 'S': // show supported databases
672 ShowParsers();
673 return 0;
675 case 't': // display database database
676 show_dbdb = true;
677 break;
679 case 'T': // show RecordStateTable
680 record_state_table = true;
681 dbNames.push_back(string(optarg));
682 break;
684 case 'v': // data dump on
685 data_dump = true;
686 break;
688 case 'V': // vformat MIME mode
689 #ifdef __BARRY_SYNC_MODE__
690 vformat_mode = true;
691 #else
692 cerr << "-V option not supported - no Sync "
693 "library support available\n";
694 return 1;
695 #endif
696 break;
698 case 'X': // reset device
699 reset_device = true;
700 break;
702 case 'z': // non-threaded sockets
703 threaded_sockets = false;
704 break;
706 case 'Z': // threaded socket router
707 threaded_sockets = true;
708 break;
710 case 'h': // help
711 default:
712 Usage();
713 return 0;
717 // Initialize the barry library. Must be called before
718 // anything else.
719 Barry::Init(data_dump);
720 if( data_dump ) {
721 int logical, major, minor;
722 const char *Version = Barry::Version(logical, major, minor);
723 cout << Version << endl;
726 // Create an IConverter object if needed
727 auto_ptr<IConverter> ic;
728 if( iconvCharset.size() ) {
729 ic.reset( new IConverter(iconvCharset.c_str(), true) );
732 // LDIF class... only needed if ldif output turned on
733 ContactLdif ldif(ldifBaseDN);
734 DoMapping(ldif, mapCommands);
735 if( ldifDnAttr.size() ) {
736 if( !ldif.SetDNAttr(ldifDnAttr) ) {
737 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
741 // Probe the USB bus for Blackberry devices and display.
742 // If user has specified a PIN, search for it in the
743 // available device list here as well
744 Barry::Probe probe(busname.c_str(), devname.c_str(),
745 epp_override ? &epOverride : 0);
746 int activeDevice = -1;
748 // show any errors during probe first
749 if( probe.GetFailCount() ) {
750 if( ldif_contacts )
751 cout << "# ";
752 cout << "Blackberry device errors with errors during probe:" << endl;
753 for( int i = 0; i < probe.GetFailCount(); i++ ) {
754 if( ldif_contacts )
755 cout << "# ";
756 cout << probe.GetFailMsg(i) << endl;
760 // show all successfully found devices
761 if( ldif_contacts )
762 cout << "# ";
763 cout << "Blackberry devices found:" << endl;
764 for( int i = 0; i < probe.GetCount(); i++ ) {
765 if( ldif_contacts )
766 cout << "# ";
767 if( data_dump )
768 probe.Get(i).DumpAll(cout);
769 else
770 cout << probe.Get(i);
771 cout << endl;
772 if( probe.Get(i).m_pin == pin )
773 activeDevice = i;
776 if( list_only )
777 return 0; // done
779 if( activeDevice == -1 ) {
780 if( pin == 0 ) {
781 // can we default to single device?
782 if( probe.GetCount() == 1 )
783 activeDevice = 0;
784 else {
785 cerr << "No device selected" << endl;
786 return 1;
789 else {
790 cerr << "PIN " << setbase(16) << pin
791 << " not found" << endl;
792 return 1;
796 if( ldif_contacts )
797 cout << "# ";
798 cout << "Using device (PIN): "
799 << probe.Get(activeDevice).m_pin.Str() << endl;
801 if( reset_device ) {
802 Usb::Device dev(probe.Get(activeDevice).m_dev);
803 dev.Reset();
804 return 0;
807 // Override device endpoints if user asks
808 Barry::ProbeResult device = probe.Get(activeDevice);
809 if( epp_override ) {
810 device.m_ep.read = epOverride.read;
811 device.m_ep.write = epOverride.write;
812 // FIXME - override this too?
813 device.m_ep.type = Usb::EndpointDescriptor::BulkType;
814 cout << "Endpoint pair (read,write) overridden with: "
815 << hex
816 << (unsigned int) device.m_ep.read << ","
817 << (unsigned int) device.m_ep.write << endl;
821 // execute each mode that was turned on
825 // Dump current LDIF mapping
826 if( list_ldif_map ) {
827 cout << ldif << endl;
830 // Dump list of Contact field names
831 if( list_contact_fields ) {
832 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
833 cout.fill(' ');
834 cout << " " << left << setw(20) << n->name << ": "
835 << n->description << endl;
839 // Check if Desktop access is needed
840 if( !( show_dbdb ||
841 ldif_contacts ||
842 record_state_table ||
843 clear_database ||
844 stCommands.size() ||
845 dbNames.size() ||
846 saveDbNames.size() ) )
847 return 0; // done
850 // Create our controller object
852 // Order is important in the following auto_ptr<> objects,
853 // since Controller must get destroyed before router.
854 // Normally you'd pick one method, and not bother
855 // with auto_ptr<> and so the normal C++ constructor
856 // rules would guarantee this safety for you, but
857 // here we want the user to pick.
859 auto_ptr<SocketRoutingQueue> router;
860 if( threaded_sockets ) {
861 router.reset( new SocketRoutingQueue );
862 router->SpinoffSimpleReadThread();
865 DesktopConnector connector(password.c_str(),
866 iconvCharset, device, router.get());
867 if( !connector.Connect() ) {
868 // bad password (default action is not to prompt)
869 cerr << connector.GetBadPassword().what() << endl;
870 return 1;
873 Barry::Mode::Desktop &desktop = connector.GetDesktop();
875 // Dump list of all databases to stdout
876 if( show_dbdb ) {
877 // open desktop mode socket
878 cout << desktop.GetDBDB() << endl;
881 // Dump list of contacts to an LDAP LDIF file
882 // This uses the Controller convenience templates
883 if( ldif_contacts ) {
884 // create a storage functor object that accepts
885 // Barry::Contact objects as input
886 Contact2Ldif storage(ldif);
888 // load all the Contact records into storage
889 desktop.LoadDatabaseByType<Barry::Contact>(storage);
892 // Dump record state table to stdout
893 if( record_state_table ) {
894 if( dbNames.size() == 0 ) {
895 cout << "No db names to process" << endl;
896 return 1;
899 vector<string>::iterator b = dbNames.begin();
900 for( ; b != dbNames.end(); b++ ) {
901 unsigned int id = desktop.GetDBID(*b);
902 RecordStateTable state;
903 desktop.GetRecordStateTable(id, state);
904 cout << "Record state table for: " << *b << endl;
905 cout << state;
907 return 0;
910 // Get Record mode overrides the default name mode
911 if( stCommands.size() ) {
912 if( dbNames.size() != 1 ) {
913 cout << "Must have 1 db name to process" << endl;
914 return 1;
917 unsigned int id = desktop.GetDBID(dbNames[0]);
918 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
919 null_parser, true, vformat_mode, bbackup_mode);
921 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
922 desktop.GetRecord(id, stCommands[i].index, *parse.get());
924 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
925 cout << "Clearing record's dirty flags..." << endl;
926 desktop.ClearDirty(id, stCommands[i].index);
929 if( stCommands[i].flag == 'D' ) {
930 desktop.DeleteRecord(id, stCommands[i].index);
934 return 0;
937 // Dump contents of selected databases to stdout, or
938 // to file if specified.
939 // This is retrieving data from the Blackberry.
940 if( dbNames.size() ) {
941 vector<string>::iterator b = dbNames.begin();
943 for( ; b != dbNames.end(); b++ ) {
944 shared_ptr<Parser> parse = GetParser(*b,
945 filename, null_parser, !sort_records,
946 vformat_mode, bbackup_mode);
947 unsigned int id = desktop.GetDBID(*b);
948 desktop.LoadDatabase(id, *parse.get());
952 // Clear databases
953 if( clear_database ) {
954 if( clearDbNames.size() == 0 ) {
955 cout << "No db names to erase" << endl;
956 return 1;
959 vector<string>::iterator b = clearDbNames.begin();
961 for( ; b != clearDbNames.end(); b++ ) {
962 unsigned int id = desktop.GetDBID(*b);
963 cout << "Deleting all records from " << (*b) << "..." << endl;
964 desktop.ClearDatabase(id);
967 return 0;
970 // Save contents of file to specified databases
971 // This is writing data to the Blackberry.
972 if( saveDbNames.size() ) {
973 vector<string>::iterator b = saveDbNames.begin();
975 for( ; b != saveDbNames.end(); b++ ) {
976 shared_ptr<Builder> build = GetBuilder(*b,
977 filename);
978 unsigned int id = desktop.GetDBID(*b);
979 desktop.SaveDatabase(id, *build);
984 catch( Usb::Error &ue ) {
985 std::cerr << "Usb::Error caught: " << ue.what() << endl;
986 return 1;
988 catch( Barry::Error &se ) {
989 std::cerr << "Barry::Error caught: " << se.what() << endl;
990 return 1;
992 catch( std::exception &e ) {
993 std::cerr << "std::exception caught: " << e.what() << endl;
994 return 1;
997 return 0;