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

Generated on 29 Mar 2010 for Barry by  doxygen 1.6.1