TODO update
[barry.git] / tools / upldif.cc
blob8c08ee3ef7df7e3dbada3bba2d967c5f5886f096
1 ///
2 /// \file upldif.cc
3 /// LDIF contact uploader
4 ///
6 /*
7 Copyright (C) 2006-2007, 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 #include <iomanip>
24 #include <iostream>
25 #include <fstream>
26 #include <vector>
27 #include <string>
28 #include <getopt.h>
31 using namespace std;
32 using namespace Barry;
34 void Usage()
36 cerr
37 << "upldif - Command line LDIF uploader\n"
38 << " Copyright 2006-2007, Net Direct Inc. (http://www.netdirect.ca/)\n\n"
39 << " -p pin PIN of device to talk with\n"
40 << " If only one device plugged in, this flag is optional\n"
41 << " -u Do the upload. If not specified, only dumps parsed\n"
42 << " LDIF data to stdout.\n"
43 << " -v Dump protocol data during operation\n"
44 << endl;
47 class Ldif2Contact
49 public:
50 Ldif2Contact() {}
51 bool operator()(Contact &rec)
53 return rec.ReadLdif(cin);
57 template <class Record>
58 struct Store
60 std::vector<Record> records;
61 mutable typename std::vector<Record>::const_iterator rec_it;
62 int count;
64 // Store constructor -- reads LDIF records from the given
65 // stream object and stores them in memory.
66 Store(std::istream &is)
67 : count(0)
69 Record rec;
70 while( is ) {
71 if( rec.ReadLdif(is) ) {
72 count++;
73 records.push_back(rec);
77 rec_it = records.begin();
80 ~Store()
82 cout << "Store counted " << dec << count << " records." << endl;
85 // Retrieval operator -- called by Barry during the upload
86 // process to get the next object
87 bool operator()(Record &rec, unsigned int databaseId) const
89 if( rec_it == records.end() )
90 return false;
91 rec = *rec_it;
92 rec_it++;
93 return true;
96 // For easy data display and debugging.
97 void Dump(std::ostream &os) const
99 typename std::vector<Record>::const_iterator b = records.begin();
100 for( ; b != records.end(); ++b ) {
101 os << *b << endl;
106 template <class Record>
107 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
109 store.Dump(os);
110 return os;
113 int main(int argc, char *argv[])
115 cout.sync_with_stdio(true); // leave this on, since libusb uses
116 // stdio for debug messages
118 try {
120 uint32_t pin = 0;
121 bool data_dump = false,
122 do_upload = false;
124 // process command line options
125 for(;;) {
126 int cmd = getopt(argc, argv, "hp:uv");
127 if( cmd == -1 )
128 break;
130 switch( cmd )
132 case 'p': // Blackberry PIN
133 pin = strtoul(optarg, NULL, 16);
134 break;
136 case 'u': // do upload
137 do_upload = true;
138 break;
140 case 'v': // data dump on
141 data_dump = true;
142 break;
144 case 'h': // help
145 default:
146 Usage();
147 return 0;
151 // Read all contacts from stdin
152 Store<Contact> contactStore(cin);
154 // Only dump to stdout if not uploading to device
155 if( !do_upload ) {
156 cout << contactStore << endl;
157 return 0;
160 // Initialize the barry library. Must be called before
161 // anything else.
162 Barry::Init(data_dump);
164 // Probe the USB bus for Blackberry devices
165 // If user has specified a PIN, search for it
166 Barry::Probe probe;
167 int activeDevice = probe.FindActive(pin);
168 if( activeDevice == -1 ) {
169 cerr << "Device not found, or not specified" << endl;
170 return 1;
173 // Create our controller object
174 Barry::Controller con(probe.Get(activeDevice));
176 // make sure we're in desktop mode
177 con.OpenMode(Controller::Desktop);
179 // upload all records to device
180 con.SaveDatabaseByType<Barry::Contact>(contactStore);
183 catch( Barry::BError &se ) {
184 std::cerr << "BError caught: " << se.what() << endl;
186 catch( Usb::UsbError &ue) {
187 std::cerr << "UsbError caught: " << ue.what() << endl;
189 catch( std::runtime_error &re ) {
190 std::cerr << "std::runtime_error caught: " << re.what() << endl;
191 return 1;
193 catch( std::exception &e ) {
194 std::cerr << "std::exception caught: " << e.what() << endl;
195 return 1;
198 return 0;