- split the maintenance scripts into release and test, so
[barry.git] / tools / upldif.cc
blob98bb3b17a8b51e71a721efe187739deb3459dd08
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 template <class Record>
48 struct Store
50 std::vector<Record> records;
51 mutable typename std::vector<Record>::const_iterator rec_it;
52 int count;
54 Barry::ContactLdif ldif;
56 // Store constructor -- reads LDIF records from the given
57 // stream object and stores them in memory.
58 Store(std::istream &is)
59 : count(0),
60 ldif("")
62 Record rec;
63 while( is ) {
64 if( ldif.ReadLdif(is, rec) ) {
65 count++;
66 records.push_back(rec);
70 rec_it = records.begin();
73 ~Store()
75 cout << "Store counted " << dec << count << " records." << endl;
78 // Retrieval operator -- called by Barry during the upload
79 // process to get the next object
80 bool operator()(Record &rec, unsigned int databaseId) const
82 if( rec_it == records.end() )
83 return false;
84 rec = *rec_it;
85 rec_it++;
86 return true;
89 // For easy data display and debugging.
90 void Dump(std::ostream &os) const
92 typename std::vector<Record>::const_iterator b = records.begin();
93 for( ; b != records.end(); ++b ) {
94 os << *b << endl;
99 template <class Record>
100 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
102 store.Dump(os);
103 return os;
106 int main(int argc, char *argv[])
108 cout.sync_with_stdio(true); // leave this on, since libusb uses
109 // stdio for debug messages
111 try {
113 uint32_t pin = 0;
114 bool data_dump = false,
115 do_upload = false;
117 // process command line options
118 for(;;) {
119 int cmd = getopt(argc, argv, "hp:uv");
120 if( cmd == -1 )
121 break;
123 switch( cmd )
125 case 'p': // Blackberry PIN
126 pin = strtoul(optarg, NULL, 16);
127 break;
129 case 'u': // do upload
130 do_upload = true;
131 break;
133 case 'v': // data dump on
134 data_dump = true;
135 break;
137 case 'h': // help
138 default:
139 Usage();
140 return 0;
144 // Read all contacts from stdin
145 Store<Contact> contactStore(cin);
147 // Only dump to stdout if not uploading to device
148 if( !do_upload ) {
149 cout << contactStore << endl;
150 return 0;
153 // Initialize the barry library. Must be called before
154 // anything else.
155 Barry::Init(data_dump);
157 // Probe the USB bus for Blackberry devices
158 // If user has specified a PIN, search for it
159 Barry::Probe probe;
160 int activeDevice = probe.FindActive(pin);
161 if( activeDevice == -1 ) {
162 cerr << "Device not found, or not specified" << endl;
163 return 1;
166 // Create our controller object
167 Barry::Controller con(probe.Get(activeDevice));
169 // make sure we're in desktop mode
170 con.OpenMode(Controller::Desktop);
172 // upload all records to device
173 con.SaveDatabaseByType<Barry::Contact>(contactStore);
176 catch( Usb::Error &ue) {
177 std::cerr << "Usb::Error caught: " << ue.what() << endl;
179 catch( Barry::Error &se ) {
180 std::cerr << "Barry::Error caught: " << se.what() << endl;
182 catch( std::exception &e ) {
183 std::cerr << "std::exception caught: " << e.what() << endl;
184 return 1;
187 return 0;