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-2012, 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 <stdlib.h>
00028 #include <string>
00029 #include "i18n.h"
00030 
00031 #include "barrygetopt.h"
00032 
00033 using namespace std;
00034 using namespace Barry;
00035 
00036 void Usage()
00037 {
00038    cerr
00039    << "upldif - Command line LDIF uploader\n"
00040    << "         Copyright 2006-2012, Net Direct Inc. (http://www.netdirect.ca/)\n\n"
00041    << "   -p pin    PIN of device to talk with\n"
00042    << "             If only one device plugged in, this flag is optional\n"
00043    << "   -P pass   Simplistic method to specify device password\n"
00044    << "   -u        Do the upload.  If not specified, only dumps parsed\n"
00045    << "             LDIF data to stdout.\n"
00046    << "   -v        Dump protocol data during operation\n"
00047    << "   -h        This help output\n"
00048    << endl;
00049 }
00050 
00051 template <class Record>
00052 struct Store
00053 {
00054         std::vector<Record> records;
00055         mutable typename std::vector<Record>::const_iterator rec_it;
00056         int count;
00057 
00058         Barry::ContactLdif ldif;
00059 
00060         // Store constructor -- reads LDIF records from the given
00061         // stream object and stores them in memory.
00062         Store(std::istream &is)
00063                 : count(0),
00064                 ldif("")
00065         {
00066                 Record rec;
00067                 while( is ) {
00068                         if( ldif.ReadLdif(is, rec) ) {
00069                                 count++;
00070                                 records.push_back(rec);
00071                         }
00072                 }
00073 
00074                 rec_it = records.begin();
00075         }
00076 
00077         ~Store()
00078         {
00079                 cout << "Store counted " << dec << count << " records." << endl;
00080         }
00081 
00082         // Retrieval operator -- called by Barry during the upload
00083         // process to get the next object
00084         bool operator()(Record &rec, Builder &builder) const
00085         {
00086                 if( rec_it == records.end() )
00087                         return false;
00088                 rec = *rec_it;
00089                 rec_it++;
00090                 return true;
00091         }
00092 
00093         // For easy data display and debugging.
00094         void Dump(std::ostream &os) const
00095         {
00096                 typename std::vector<Record>::const_iterator b = records.begin();
00097                 for( ; b != records.end(); ++b ) {
00098                         os << *b << endl;
00099                 }
00100         }
00101 };
00102 
00103 template <class Record>
00104 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
00105 {
00106         store.Dump(os);
00107         return os;
00108 }
00109 
00110 int main(int argc, char *argv[])
00111 {
00112         INIT_I18N(PACKAGE);
00113 
00114         cout.sync_with_stdio(true);     // leave this on, since libusb uses
00115                                         // stdio for debug messages
00116 
00117         try {
00118 
00119                 uint32_t pin = 0;
00120                 bool    data_dump = false,
00121                         do_upload = false;
00122                 string password;
00123 
00124                 // process command line options
00125                 for(;;) {
00126                         int cmd = getopt(argc, argv, "hp:P:uv");
00127                         if( cmd == -1 )
00128                                 break;
00129 
00130                         switch( cmd )
00131                         {
00132                         case 'p':       // Blackberry PIN
00133                                 pin = strtoul(optarg, NULL, 16);
00134                                 break;
00135 
00136                         case 'P':       // Device password
00137                                 password = optarg;
00138                                 break;
00139 
00140                         case 'u':       // do upload
00141                                 do_upload = true;
00142                                 break;
00143 
00144                         case 'v':       // data dump on
00145                                 data_dump = true;
00146                                 break;
00147 
00148                         case 'h':       // help
00149                         default:
00150                                 Usage();
00151                                 return 0;
00152                         }
00153                 }
00154 
00155                 // Read all contacts from stdin
00156                 Store<Contact> contactStore(cin);
00157 
00158                 // Only dump to stdout if not uploading to device
00159                 if( !do_upload ) {
00160                         cout << contactStore << endl;
00161                         return 0;
00162                 }
00163 
00164                 // Initialize the barry library.  Must be called before
00165                 // anything else.
00166                 Barry::Init(data_dump);
00167 
00168                 // Probe the USB bus for Blackberry devices
00169                 // If user has specified a PIN, search for it
00170                 Barry::Probe probe;
00171                 int activeDevice = probe.FindActive(pin);
00172                 if( activeDevice == -1 ) {
00173                         cerr << "Device not found, or not specified" << endl;
00174                         return 1;
00175                 }
00176 
00177                 // Create our controller object
00178                 Barry::Controller con(probe.Get(activeDevice));
00179 
00180                 // make sure we're in desktop mode
00181                 Barry::Mode::Desktop desktop(con);
00182                 desktop.Open(password.c_str());
00183 
00184                 // upload all records to device
00185                 desktop.SaveDatabaseByType<Barry::Contact>(contactStore);
00186 
00187         }
00188         catch( Usb::Error &ue) {
00189                 std::cerr << "Usb::Error caught: " << ue.what() << endl;
00190         }
00191         catch( Barry::Error &se ) {
00192                 std::cerr << "Barry::Error caught: " << se.what() << endl;
00193         }
00194         catch( std::exception &e ) {
00195                 std::cerr << "std::exception caught: " << e.what() << endl;
00196                 return 1;
00197         }
00198 
00199         return 0;
00200 }
00201