tools: changed parser/builder auto_ptr to shared_ptr
[barry.git] / tools / btool.cc
blob3c017e4bf62c91d59b072e6aa07f4277ef8ec1c5
1 ///
2 /// \file btool.cc
3 /// Barry library tester
4 ///
6 /*
7 Copyright (C) 2005-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>
25 #endif
27 #include <iomanip>
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <vector>
32 #include <string>
33 #include <algorithm>
34 #include <getopt.h>
35 #include <tr1/memory>
36 #include "i18n.h"
39 using namespace std;
40 using namespace std::tr1;
41 using namespace Barry;
43 void Usage()
45 int major, minor;
46 const char *Version = Barry::Version(major, minor);
48 cerr
49 << "btool - Command line USB Blackberry Test Tool\n"
50 << " Copyright 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
51 << " Using: " << Version << "\n"
52 << " Compiled "
53 #ifdef __BARRY_BOOST_MODE__
54 << "with"
55 #else
56 << "without"
57 #endif
58 << " Boost support\n"
59 << "\n"
60 << " -B bus Specify which USB bus to search on\n"
61 << " -N dev Specify which system device, using system specific string\n"
62 << "\n"
63 << " -a db Erase / clear database 'db' FROM device, deleting all\n"
64 << " its records. Can be used multiple times to clear more\n"
65 << " than one DB.\n"
66 << " -c dn Convert address book database to LDIF format, using the\n"
67 << " specified baseDN\n"
68 << " -C dnattr LDIF attribute name to use when building the FQDN\n"
69 << " Defaults to 'cn'\n"
70 << " -d db Load database 'db' FROM device and dump to screen\n"
71 << " Can be used multiple times to fetch more than one DB\n"
72 << " -e epp Override endpoint pair detection. 'epp' is a single\n"
73 << " string separated by a comma, holding the read,write\n"
74 << " endpoint pair. Example: -e 83,5\n"
75 << " Note: Endpoints are specified in hex.\n"
76 << " You should never need to use this option.\n"
77 #ifdef __BARRY_BOOST_MODE__
78 << " -f file Filename to save or load handheld data to/from\n"
79 #endif
80 << " -h This help\n"
81 << " -i cs International charset for string conversions\n"
82 << " Valid values here are available with 'iconv --list'\n"
83 << " -I Sort records before output\n"
84 << " -l List devices\n"
85 << " -L List Contact field names\n"
86 << " -m Map LDIF name to Contact field / Unmap LDIF name\n"
87 << " Map: ldif,read,write - maps ldif to read/write Contact fields\n"
88 << " Unmap: ldif name alone\n"
89 << " -M List current LDIF mapping\n"
90 << " -n Use null parser on all databases.\n"
91 << " -p pin PIN of device to talk with\n"
92 << " If only one device is plugged in, this flag is optional\n"
93 << " -P pass Simplistic method to specify device password\n"
94 << " -s db Save database 'db' TO device from data loaded from -f file\n"
95 << " -S Show list of supported database parsers\n"
96 << " -t Show database database table\n"
97 << " -T db Show record state table for given database\n"
98 << " -v Dump protocol data during operation\n"
99 #ifdef __BARRY_SYNC_MODE__
100 << " -V Dump records using MIME vformats where possible\n"
101 #endif
102 << " -X Reset device\n"
103 << " -z Use non-threaded sockets\n"
104 << " -Z Use threaded socket router (default)\n"
105 << "\n"
106 << " -d Command modifiers: (can be used multiple times for more than 1 record)\n"
107 << "\n"
108 << " -r # Record index number as seen in the -T state table.\n"
109 << " This overrides the default -d behaviour, and only\n"
110 << " downloads the one specified record, sending to stdout.\n"
111 << " -R # Same as -r, but also clears the record's dirty flags.\n"
112 << " -D # Record index number as seen in the -T state table,\n"
113 << " which indicates the record to delete. Used with the -d\n"
114 << " command to specify the database.\n"
115 << endl;
118 class Contact2Ldif
120 public:
121 Barry::ContactLdif &ldif;
123 Contact2Ldif(Barry::ContactLdif &ldif) : ldif(ldif) {}
125 void operator()(const Contact &rec)
127 ldif.DumpLdif(cout, rec);
131 #ifdef __BARRY_SYNC_MODE__
132 template <class Record>
133 class MimeDump
135 public:
136 void Dump(std::ostream &os, const Record &rec)
138 os << rec << endl;
141 static bool Supported() { return false; }
144 template <>
145 class MimeDump<Contact>
147 public:
148 void Dump(std::ostream &os, const Contact &rec)
150 Sync::vCard vcard;
151 os << vcard.ToVCard(rec) << endl;
154 static bool Supported() { return true; }
157 template <>
158 class MimeDump<Calendar>
160 public:
161 void Dump(std::ostream &os, const Calendar &rec)
163 Sync::vTimeConverter vtc;
164 Sync::vCalendar vcal(vtc);
165 os << vcal.ToVCal(rec) << endl;
168 static bool Supported() { return true; }
171 template <>
172 class MimeDump<Memo>
174 public:
175 void Dump(std::ostream &os, const Memo &rec)
177 Sync::vJournal vjournal;
178 os << vjournal.ToMemo(rec) << endl;
181 static bool Supported() { return true; }
184 template <>
185 class MimeDump<Task>
187 public:
188 void Dump(std::ostream &os, const Task &rec)
190 Sync::vTimeConverter vtc;
191 Sync::vTodo vtodo(vtc);
192 os << vtodo.ToTask(rec) << endl;
195 static bool Supported() { return true; }
197 #endif
199 template <class Record>
200 struct Store
202 std::vector<Record> records;
203 mutable typename std::vector<Record>::const_iterator rec_it;
204 std::string filename;
205 bool load;
206 bool immediate_display;
207 bool vformat_mode;
208 int count;
210 Store(const string &filename, bool load, bool immediate_display,
211 bool vformat_mode)
212 : rec_it(records.end()),
213 filename(filename),
214 load(load),
215 immediate_display(immediate_display),
216 vformat_mode(vformat_mode),
217 count(0)
219 #ifdef __BARRY_BOOST_MODE__
220 try {
222 if( load && filename.size() ) {
223 // filename is available, attempt to load
224 cout << "Loading: " << filename << endl;
225 ifstream ifs(filename.c_str());
226 std::string dbName;
227 getline(ifs, dbName);
228 boost::archive::text_iarchive ia(ifs);
229 ia >> records;
230 cout << records.size()
231 << " records loaded from '"
232 << filename << "'" << endl;
233 sort(records.begin(), records.end());
234 rec_it = records.begin();
236 // debugging aid
237 typename std::vector<Record>::const_iterator beg = records.begin(), end = records.end();
238 for( ; beg != end; beg++ ) {
239 cout << (*beg) << endl;
243 } catch( boost::archive::archive_exception &ae ) {
244 cerr << "Archive exception in ~Store(): "
245 << ae.what() << endl;
247 #endif
250 ~Store()
252 if( !immediate_display ) {
253 // not dumped yet, sort then dump
254 sort(records.begin(), records.end());
255 DumpAll();
258 cout << "Store counted " << dec << count << " records." << endl;
259 #ifdef __BARRY_BOOST_MODE__
260 try {
262 if( !load && filename.size() ) {
263 // filename is available, attempt to save
264 cout << "Saving: " << filename << endl;
265 const std::vector<Record> &r = records;
266 ofstream ofs(filename.c_str());
267 ofs << Record::GetDBName() << endl;
268 boost::archive::text_oarchive oa(ofs);
269 oa << r;
270 cout << dec << r.size() << " records saved to '"
271 << filename << "'" << endl;
274 } catch( boost::archive::archive_exception &ae ) {
275 cerr << "Archive exception in ~Store(): "
276 << ae.what() << endl;
278 #endif
281 void DumpAll()
283 typename vector<Record>::const_iterator i = records.begin();
284 for( ; i != records.end(); ++i ) {
285 Dump(*i);
289 void Dump(const Record &rec)
291 if( vformat_mode ) {
292 #ifdef __BARRY_SYNC_MODE__
293 MimeDump<Record> md;
294 md.Dump(cout, rec);
295 #endif
297 else {
298 cout << rec << endl;
302 // storage operator
303 void operator()(const Record &rec)
305 count++;
306 if( immediate_display )
307 Dump(rec);
308 records.push_back(rec);
311 // retrieval operator
312 bool operator()(Record &rec, Builder &builder) const
314 if( rec_it == records.end() )
315 return false;
316 rec = *rec_it;
317 rec_it++;
318 return true;
322 class DataDumpParser : public Barry::Parser
324 uint32_t m_id;
326 public:
327 virtual void Clear() {}
329 virtual void SetIds(const std::string &DbName,
330 uint8_t RecType, uint32_t UniqueId)
332 m_id = UniqueId;
335 virtual void ParseHeader(const Data &, size_t &) {}
337 virtual void ParseFields(const Barry::Data &data, size_t &offset,
338 const IConverter *ic)
340 std::cout << "Raw record dump for record: "
341 << std::hex << m_id << std::endl;
342 std::cout << data << std::endl;
345 virtual void Store() {}
348 shared_ptr<Parser> GetParser(const string &name,
349 const string &filename,
350 bool null_parser,
351 bool immediate_display,
352 bool vformat_mode)
354 bool dnow = immediate_display;
355 bool vmode = vformat_mode;
357 if( null_parser ) {
358 // use null parser
359 return shared_ptr<Parser>( new DataDumpParser );
361 // check for recognized database names
362 else if( name == Contact::GetDBName() ) {
363 return shared_ptr<Parser>(
364 new RecordParser<Contact, Store<Contact> > (
365 new Store<Contact>(filename, false, dnow, vmode)));
367 else if( name == Message::GetDBName() ) {
368 return shared_ptr<Parser>(
369 new RecordParser<Message, Store<Message> > (
370 new Store<Message>(filename, false, dnow, vmode)));
372 else if( name == Calendar::GetDBName() ) {
373 return shared_ptr<Parser>(
374 new RecordParser<Calendar, Store<Calendar> > (
375 new Store<Calendar>(filename, false, dnow, vmode)));
377 else if( name == CalendarAll::GetDBName() ) {
378 return shared_ptr<Parser>(
379 new RecordParser<CalendarAll, Store<CalendarAll> > (
380 new Store<CalendarAll>(filename, false, dnow, vmode)));
382 else if( name == CallLog::GetDBName() ) {
383 return shared_ptr<Parser>(
384 new RecordParser<CallLog, Store<CallLog> > (
385 new Store<CallLog>(filename, false, dnow, vmode)));
387 else if( name == ServiceBook::GetDBName() ) {
388 return shared_ptr<Parser>(
389 new RecordParser<ServiceBook, Store<ServiceBook> > (
390 new Store<ServiceBook>(filename, false, dnow, vmode)));
393 else if( name == Memo::GetDBName() ) {
394 return shared_ptr<Parser>(
395 new RecordParser<Memo, Store<Memo> > (
396 new Store<Memo>(filename, false, dnow, vmode)));
398 else if( name == Task::GetDBName() ) {
399 return shared_ptr<Parser>(
400 new RecordParser<Task, Store<Task> > (
401 new Store<Task>(filename, false, dnow, vmode)));
403 else if( name == PINMessage::GetDBName() ) {
404 return shared_ptr<Parser>(
405 new RecordParser<PINMessage, Store<PINMessage> > (
406 new Store<PINMessage>(filename, false, dnow, vmode)));
408 else if( name == SavedMessage::GetDBName() ) {
409 return shared_ptr<Parser>(
410 new RecordParser<SavedMessage, Store<SavedMessage> > (
411 new Store<SavedMessage>(filename, false, dnow, vmode)));
413 else if( name == Sms::GetDBName() ) {
414 return shared_ptr<Parser>(
415 new RecordParser<Sms, Store<Sms> > (
416 new Store<Sms>(filename, false, dnow, vmode)));
418 else if( name == Folder::GetDBName() ) {
419 return shared_ptr<Parser>(
420 new RecordParser<Folder, Store<Folder> > (
421 new Store<Folder>(filename, false, dnow, vmode)));
423 else if( name == Timezone::GetDBName() ) {
424 return shared_ptr<Parser>(
425 new RecordParser<Timezone, Store<Timezone> > (
426 new Store<Timezone>(filename, false, dnow, vmode)));
428 else {
429 // unknown database, use null parser
430 return shared_ptr<Parser>( new DataDumpParser );
434 shared_ptr<Builder> GetBuilder(const string &name, const string &filename)
436 // check for recognized database names
437 if( name == Contact::GetDBName() ) {
438 return shared_ptr<Builder>(
439 new RecordBuilder<Contact, Store<Contact> > (
440 new Store<Contact>(filename, true, true, false)));
442 else if( name == Calendar::GetDBName() ) {
443 return shared_ptr<Builder>(
444 new RecordBuilder<Calendar, Store<Calendar> > (
445 new Store<Calendar>(filename, true, true, false)));
447 else if( name == CalendarAll::GetDBName() ) {
448 return shared_ptr<Builder>(
449 new RecordBuilder<CalendarAll, Store<CalendarAll> > (
450 new Store<CalendarAll>(filename, true, true, false)));
452 else if( name == Memo::GetDBName() ) {
453 return shared_ptr<Builder>(
454 new RecordBuilder<Memo, Store<Memo> > (
455 new Store<Memo>(filename, true, true, false)));
457 else if( name == Task::GetDBName() ) {
458 return shared_ptr<Builder>(
459 new RecordBuilder<Task, Store<Task> > (
460 new Store<Task>(filename, true, true, false)));
463 else if( name == "Messages" ) {
464 return shared_ptr<Parser>(
465 new RecordParser<Message, Store<Message> > (
466 new Store<Message>(filename, true, true, false)));
468 else if( name == "Service Book" ) {
469 return shared_ptr<Parser>(
470 new RecordParser<ServiceBook, Store<ServiceBook> > (
471 new Store<ServiceBook>(filename, true, true, false)));
474 else {
475 throw std::runtime_error("No Builder available for database");
479 void ShowParsers()
481 cout << "Supported Database parsers:\n"
482 << " (* = can display in vformat MIME mode)\n"
483 << " Address Book *\n"
484 << " Messages\n"
485 << " Calendar *\n"
486 << " Calendar - All *\n"
487 << " Phone Call Logs\n"
488 << " Service Book\n"
489 << " Memos *\n"
490 << " Tasks *\n"
491 << " PIN Messages\n"
492 << " Saved Email Messages\n"
493 << " SMS Messages\n"
494 << " Folders\n"
495 << " Time Zones (read only)\n"
496 << "\n"
497 << "Supported Database builders:\n"
498 << " Address Book\n"
499 << " Calendar\n"
500 << " Calendar - All\n"
501 << " Memo\n"
502 << " Task\n"
503 << endl;
506 struct StateTableCommand
508 char flag;
509 bool clear;
510 unsigned int index;
512 StateTableCommand(char f, bool c, unsigned int i)
513 : flag(f), clear(c), index(i) {}
516 bool SplitMap(const string &map, string &ldif, string &read, string &write)
518 string::size_type a = map.find(',');
519 if( a == string::npos )
520 return false;
522 string::size_type b = map.find(',', a+1);
523 if( b == string::npos )
524 return false;
526 ldif.assign(map, 0, a);
527 read.assign(map, a + 1, b - a - 1);
528 write.assign(map, b + 1, map.size() - b - 1);
530 return ldif.size() && read.size() && write.size();
533 void DoMapping(ContactLdif &ldif, const vector<string> &mapCommands)
535 for( vector<string>::const_iterator i = mapCommands.begin();
536 i != mapCommands.end();
537 ++i )
539 // single names mean unmapping
540 if( i->find(',') == string::npos ) {
541 // unmap
542 cerr << "Unmapping: " << *i << endl;
543 ldif.Unmap(*i);
545 else {
546 cerr << "Mapping: " << *i << endl;
548 // map... extract ldif/read/write names
549 string ldifname, read, write;
550 if( SplitMap(*i, ldifname, read, write) ) {
551 if( !ldif.Map(ldifname, read, write) ) {
552 cerr << "Read/Write name unknown: " << *i << endl;
555 else {
556 cerr << "Invalid map format: " << *i << endl;
562 bool ParseEpOverride(const char *arg, Usb::EndpointPair *epp)
564 int read, write;
565 char comma;
566 istringstream iss(arg);
567 iss >> hex >> read >> comma >> write;
568 if( !iss )
569 return false;
570 epp->read = read;
571 epp->write = write;
572 return true;
575 int main(int argc, char *argv[])
577 INIT_I18N(PACKAGE);
579 cout.sync_with_stdio(true); // leave this on, since libusb uses
580 // stdio for debug messages
582 try {
584 uint32_t pin = 0;
585 bool list_only = false,
586 show_dbdb = false,
587 ldif_contacts = false,
588 data_dump = false,
589 vformat_mode = false,
590 reset_device = false,
591 list_contact_fields = false,
592 list_ldif_map = false,
593 epp_override = false,
594 threaded_sockets = true,
595 record_state = false,
596 clear_database = false,
597 null_parser = false,
598 sort_records = false;
599 string ldifBaseDN, ldifDnAttr;
600 string filename;
601 string password;
602 string busname;
603 string devname;
604 string iconvCharset;
605 vector<string> dbNames, saveDbNames, mapCommands, clearDbNames;
606 vector<StateTableCommand> stCommands;
607 Usb::EndpointPair epOverride;
609 // process command line options
610 for(;;) {
611 int cmd = getopt(argc, argv, "a:B:c:C:d:D:e:f:hi:IlLm:MnN:p:P:r:R:Ss:tT:vVXzZ");
612 if( cmd == -1 )
613 break;
615 switch( cmd )
617 case 'a': // Clear Database
618 clear_database = true;
619 clearDbNames.push_back(string(optarg));
620 break;
622 case 'B': // busname
623 busname = optarg;
624 break;
626 case 'c': // contacts to ldap ldif
627 ldif_contacts = true;
628 ldifBaseDN = optarg;
629 break;
631 case 'C': // DN Attribute for FQDN
632 ldifDnAttr = optarg;
633 break;
635 case 'd': // show dbname
636 dbNames.push_back(string(optarg));
637 break;
639 case 'D': // delete record
640 stCommands.push_back(
641 StateTableCommand('D', false, atoi(optarg)));
642 break;
644 case 'e': // endpoint override
645 if( !ParseEpOverride(optarg, &epOverride) ) {
646 Usage();
647 return 1;
649 epp_override = true;
650 break;
652 case 'f': // filename
653 #ifdef __BARRY_BOOST_MODE__
654 filename = optarg;
655 #else
656 cerr << "-f option not supported - no Boost "
657 "serialization support available\n";
658 return 1;
659 #endif
660 break;
662 case 'i': // international charset (iconv)
663 iconvCharset = optarg;
664 break;
666 case 'I': // sort before dump
667 sort_records = true;
668 break;
670 case 'l': // list only
671 list_only = true;
672 break;
674 case 'L': // List Contact field names
675 list_contact_fields = true;
676 break;
678 case 'm': // Map / Unmap
679 mapCommands.push_back(string(optarg));
680 break;
682 case 'M': // List LDIF map
683 list_ldif_map = true;
684 break;
686 case 'n': // use null parser
687 null_parser = true;
688 break;
690 case 'N': // Devname
691 devname = optarg;
692 break;
694 case 'p': // Blackberry PIN
695 pin = strtoul(optarg, NULL, 16);
696 break;
698 case 'P': // Device password
699 password = optarg;
700 break;
702 case 'r': // get specific record index
703 stCommands.push_back(
704 StateTableCommand('r', false, atoi(optarg)));
705 break;
707 case 'R': // same as 'r', and clears dirty
708 stCommands.push_back(
709 StateTableCommand('r', true, atoi(optarg)));
710 break;
712 case 's': // save dbname
713 saveDbNames.push_back(string(optarg));
714 break;
716 case 'S': // show supported databases
717 ShowParsers();
718 return 0;
720 case 't': // display database database
721 show_dbdb = true;
722 break;
724 case 'T': // show RecordStateTable
725 record_state = true;
726 dbNames.push_back(string(optarg));
727 break;
729 case 'v': // data dump on
730 data_dump = true;
731 break;
733 case 'V': // vformat MIME mode
734 #ifdef __BARRY_SYNC_MODE__
735 vformat_mode = true;
736 #else
737 cerr << "-V option not supported - no Sync "
738 "library support available\n";
739 return 1;
740 #endif
741 break;
743 case 'X': // reset device
744 reset_device = true;
745 break;
747 case 'z': // non-threaded sockets
748 threaded_sockets = false;
749 break;
751 case 'Z': // threaded socket router
752 threaded_sockets = true;
753 break;
755 case 'h': // help
756 default:
757 Usage();
758 return 0;
762 // Initialize the barry library. Must be called before
763 // anything else.
764 Barry::Init(data_dump);
765 if( data_dump ) {
766 int major, minor;
767 const char *Version = Barry::Version(major, minor);
768 cout << Version << endl;
771 // Create an IConverter object if needed
772 auto_ptr<IConverter> ic;
773 if( iconvCharset.size() ) {
774 ic.reset( new IConverter(iconvCharset.c_str(), true) );
777 // LDIF class... only needed if ldif output turned on
778 ContactLdif ldif(ldifBaseDN);
779 DoMapping(ldif, mapCommands);
780 if( ldifDnAttr.size() ) {
781 if( !ldif.SetDNAttr(ldifDnAttr) ) {
782 cerr << "Unable to set DN Attr: " << ldifDnAttr << endl;
786 // Probe the USB bus for Blackberry devices and display.
787 // If user has specified a PIN, search for it in the
788 // available device list here as well
789 Barry::Probe probe(busname.c_str(), devname.c_str(),
790 epp_override ? &epOverride : 0);
791 int activeDevice = -1;
793 // show any errors during probe first
794 if( probe.GetFailCount() ) {
795 if( ldif_contacts )
796 cout << "# ";
797 cout << "Blackberry device errors with errors during probe:" << endl;
798 for( int i = 0; i < probe.GetFailCount(); i++ ) {
799 if( ldif_contacts )
800 cout << "# ";
801 cout << probe.GetFailMsg(i) << endl;
805 // show all successfully found devices
806 if( ldif_contacts )
807 cout << "# ";
808 cout << "Blackberry devices found:" << endl;
809 for( int i = 0; i < probe.GetCount(); i++ ) {
810 if( ldif_contacts )
811 cout << "# ";
812 if( data_dump )
813 probe.Get(i).DumpAll(cout);
814 else
815 cout << probe.Get(i);
816 cout << endl;
817 if( probe.Get(i).m_pin == pin )
818 activeDevice = i;
821 if( list_only )
822 return 0; // done
824 if( activeDevice == -1 ) {
825 if( pin == 0 ) {
826 // can we default to single device?
827 if( probe.GetCount() == 1 )
828 activeDevice = 0;
829 else {
830 cerr << "No device selected" << endl;
831 return 1;
834 else {
835 cerr << "PIN " << setbase(16) << pin
836 << " not found" << endl;
837 return 1;
841 if( ldif_contacts )
842 cout << "# ";
843 cout << "Using device (PIN): "
844 << probe.Get(activeDevice).m_pin.str() << endl;
846 if( reset_device ) {
847 Usb::Device dev(probe.Get(activeDevice).m_dev);
848 dev.Reset();
849 return 0;
852 // Override device endpoints if user asks
853 Barry::ProbeResult device = probe.Get(activeDevice);
854 if( epp_override ) {
855 device.m_ep.read = epOverride.read;
856 device.m_ep.write = epOverride.write;
857 device.m_ep.type = 2; // FIXME - override this too?
858 cout << "Endpoint pair (read,write) overridden with: "
859 << hex
860 << (unsigned int) device.m_ep.read << ","
861 << (unsigned int) device.m_ep.write << endl;
865 // Create our controller object
867 // Order is important in the following auto_ptr<> objects,
868 // since Controller must get destroyed before router.
869 // Normally you'd pick one method, and not bother
870 // with auto_ptr<> and so the normal C++ constructor
871 // rules would guarantee this safety for you, but
872 // here we want the user to pick.
874 auto_ptr<SocketRoutingQueue> router;
875 auto_ptr<Barry::Controller> pcon;
876 if( threaded_sockets ) {
877 router.reset( new SocketRoutingQueue );
878 router->SpinoffSimpleReadThread();
879 pcon.reset( new Barry::Controller(device, *router) );
881 else {
882 pcon.reset( new Barry::Controller(device) );
885 Barry::Controller &con = *pcon;
886 Barry::Mode::Desktop desktop(con, *ic);
889 // execute each mode that was turned on
893 // Dump list of all databases to stdout
894 if( show_dbdb ) {
895 // open desktop mode socket
896 desktop.Open(password.c_str());
897 cout << desktop.GetDBDB() << endl;
900 // Dump list of Contact field names
901 if( list_contact_fields ) {
902 for( const ContactLdif::NameToFunc *n = ldif.GetFieldNames(); n->name; n++ ) {
903 cout.fill(' ');
904 cout << " " << left << setw(20) << n->name << ": "
905 << n->description << endl;
909 // Dump current LDIF mapping
910 if( list_ldif_map ) {
911 cout << ldif << endl;
914 // Dump list of contacts to an LDAP LDIF file
915 // This uses the Controller convenience templates
916 if( ldif_contacts ) {
917 // make sure we're in desktop mode
918 desktop.Open(password.c_str());
920 // create a storage functor object that accepts
921 // Barry::Contact objects as input
922 Contact2Ldif storage(ldif);
924 // load all the Contact records into storage
925 desktop.LoadDatabaseByType<Barry::Contact>(storage);
928 // Dump record state table to stdout
929 if( record_state ) {
930 if( dbNames.size() == 0 ) {
931 cout << "No db names to process" << endl;
932 return 1;
935 desktop.Open(password.c_str());
937 vector<string>::iterator b = dbNames.begin();
938 for( ; b != dbNames.end(); b++ ) {
939 unsigned int id = desktop.GetDBID(*b);
940 RecordStateTable state;
941 desktop.GetRecordStateTable(id, state);
942 cout << "Record state table for: " << *b << endl;
943 cout << state;
945 return 0;
948 // Get Record mode overrides the default name mode
949 if( stCommands.size() ) {
950 if( dbNames.size() != 1 ) {
951 cout << "Must have 1 db name to process" << endl;
952 return 1;
955 desktop.Open(password.c_str());
956 unsigned int id = desktop.GetDBID(dbNames[0]);
957 shared_ptr<Parser> parse = GetParser(dbNames[0],filename,
958 null_parser, true, vformat_mode);
960 for( unsigned int i = 0; i < stCommands.size(); i++ ) {
961 desktop.GetRecord(id, stCommands[i].index, *parse.get());
963 if( stCommands[i].flag == 'r' && stCommands[i].clear ) {
964 cout << "Clearing record's dirty flags..." << endl;
965 desktop.ClearDirty(id, stCommands[i].index);
968 if( stCommands[i].flag == 'D' ) {
969 desktop.DeleteRecord(id, stCommands[i].index);
973 return 0;
976 // Clear databases
977 if (clear_database) {
978 if( clearDbNames.size() == 0 ) {
979 cout << "No db names to erase" << endl;
980 return 1;
983 vector<string>::iterator b = clearDbNames.begin();
985 desktop.Open(password.c_str());
987 for( ; b != clearDbNames.end(); b++ ) {
988 unsigned int id = desktop.GetDBID(*b);
989 cout << "Deleting all records from " << (*b) << "..." << endl;
990 desktop.ClearDatabase(id);
993 return 0;
996 // Dump contents of selected databases to stdout, or
997 // to file if specified.
998 // This is retrieving data from the Blackberry.
999 if( dbNames.size() ) {
1000 vector<string>::iterator b = dbNames.begin();
1002 desktop.Open(password.c_str());
1003 for( ; b != dbNames.end(); b++ ) {
1004 shared_ptr<Parser> parse = GetParser(*b,filename,
1005 null_parser, !sort_records,
1006 vformat_mode);
1007 unsigned int id = desktop.GetDBID(*b);
1008 desktop.LoadDatabase(id, *parse.get());
1012 // Save contents of file to specified databases
1013 // This is writing data to the Blackberry.
1014 if( saveDbNames.size() ) {
1015 vector<string>::iterator b = saveDbNames.begin();
1017 desktop.Open(password.c_str());
1018 for( ; b != saveDbNames.end(); b++ ) {
1019 shared_ptr<Builder> build =
1020 GetBuilder(*b, filename);
1021 unsigned int id = desktop.GetDBID(*b);
1022 desktop.SaveDatabase(id, *build);
1027 catch( Usb::Error &ue) {
1028 std::cerr << "Usb::Error caught: " << ue.what() << endl;
1029 return 1;
1031 catch( Barry::Error &se ) {
1032 std::cerr << "Barry::Error caught: " << se.what() << endl;
1033 return 1;
1035 catch( std::exception &e ) {
1036 std::cerr << "std::exception caught: " << e.what() << endl;
1037 return 1;
1040 return 0;