upldif.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       upldif.cc
00003 ///             LDIF contact uploader
00004 ///
00005 
00006 /*
00007     Copyright (C) 2006-2008, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include <barry/barry.h>
00023 #include <iomanip>
00024 #include <iostream>
00025 #include <fstream>
00026 #include <vector>
00027 #include <string>
00028 #include <getopt.h>
00029 
00030 
00031 using namespace std;
00032 using namespace Barry;
00033 
00034 void Usage()
00035 {
00036    cerr
00037    << "upldif - Command line LDIF uploader\n"
00038    << "         Copyright 2006-2008, Net Direct Inc. (http://www.netdirect.ca/)\n\n"
00039    << "   -p pin    PIN of device to talk with\n"
00040    << "             If only one device plugged in, this flag is optional\n"
00041    << "   -u        Do the upload.  If not specified, only dumps parsed\n"
00042    << "             LDIF data to stdout.\n"
00043    << "   -v        Dump protocol data during operation\n"
00044    << "   -h        This help output\n"
00045    << endl;
00046 }
00047 
00048 template <class Record>
00049 struct Store
00050 {
00051         std::vector<Record> records;
00052         mutable typename std::vector<Record>::const_iterator rec_it;
00053         int count;
00054 
00055         Barry::ContactLdif ldif;
00056 
00057         // Store constructor -- reads LDIF records from the given
00058         // stream object and stores them in memory.
00059         Store(std::istream &is)
00060                 : count(0),
00061                 ldif("")
00062         {
00063                 Record rec;
00064                 while( is ) {
00065                         if( ldif.ReadLdif(is, rec) ) {
00066                                 count++;
00067                                 records.push_back(rec);
00068                         }
00069                 }
00070 
00071                 rec_it = records.begin();
00072         }
00073 
00074         ~Store()
00075         {
00076                 cout << "Store counted " << dec << count << " records." << endl;
00077         }
00078 
00079         // Retrieval operator -- called by Barry during the upload
00080         // process to get the next object
00081         bool operator()(Record &rec, unsigned int databaseId) const
00082         {
00083                 if( rec_it == records.end() )
00084                         return false;
00085                 rec = *rec_it;
00086                 rec_it++;
00087                 return true;
00088         }
00089 
00090         // For easy data display and debugging.
00091         void Dump(std::ostream &os) const
00092         {
00093                 typename std::vector<Record>::const_iterator b = records.begin();
00094                 for( ; b != records.end(); ++b ) {
00095                         os << *b << endl;
00096                 }
00097         }
00098 };
00099 
00100 template <class Record>
00101 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
00102 {
00103         store.Dump(os);
00104         return os;
00105 }
00106 
00107 int main(int argc, char *argv[])
00108 {
00109         cout.sync_with_stdio(true);     // leave this on, since libusb uses
00110                                         // stdio for debug messages
00111 
00112         try {
00113 
00114                 uint32_t pin = 0;
00115                 bool    data_dump = false,
00116                         do_upload = false;
00117 
00118                 // process command line options
00119                 for(;;) {
00120                         int cmd = getopt(argc, argv, "hp:uv");
00121                         if( cmd == -1 )
00122                                 break;
00123 
00124                         switch( cmd )
00125                         {
00126                         case 'p':       // Blackberry PIN
00127                                 pin = strtoul(optarg, NULL, 16);
00128                                 break;
00129 
00130                         case 'u':       // do upload
00131                                 do_upload = true;
00132                                 break;
00133 
00134                         case 'v':       // data dump on
00135                                 data_dump = true;
00136                                 break;
00137 
00138                         case 'h':       // help
00139                         default:
00140                                 Usage();
00141                                 return 0;
00142                         }
00143                 }
00144 
00145                 // Read all contacts from stdin
00146                 Store<Contact> contactStore(cin);
00147 
00148                 // Only dump to stdout if not uploading to device
00149                 if( !do_upload ) {
00150                         cout << contactStore << endl;
00151                         return 0;
00152                 }
00153 
00154                 // Initialize the barry library.  Must be called before
00155                 // anything else.
00156                 Barry::Init(data_dump);
00157 
00158                 // Probe the USB bus for Blackberry devices
00159                 // If user has specified a PIN, search for it
00160                 Barry::Probe probe;
00161                 int activeDevice = probe.FindActive(pin);
00162                 if( activeDevice == -1 ) {
00163                         cerr << "Device not found, or not specified" << endl;
00164                         return 1;
00165                 }
00166 
00167                 // Create our controller object
00168                 Barry::Controller con(probe.Get(activeDevice));
00169 
00170                 // make sure we're in desktop mode
00171                 Barry::Mode::Desktop desktop(con);
00172                 desktop.Open();
00173 
00174                 // upload all records to device
00175                 desktop.SaveDatabaseByType<Barry::Contact>(contactStore);
00176 
00177         }
00178         catch( Usb::Error &ue) {
00179                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00180         }
00181         catch( Barry::Error &se ) {
00182                 std::cerr << "Barry::Error caught: " << se.what() << endl;
00183         }
00184         catch( std::exception &e ) {
00185                 std::cerr << "std::exception caught: " << e.what() << endl;
00186                 return 1;
00187         }
00188 
00189         return 0;
00190 }
00191 

Generated on Wed Sep 24 21:27:32 2008 for Barry by  doxygen 1.5.1