Updated TODO
[barry.git] / tools / upldif.cc
bloba78c417ff818f752936d8dada7a8f97e764744f1
1 ///
2 /// \file upldif.cc
3 /// LDIF contact uploader
4 ///
6 /*
7 Copyright (C) 2006-2010, 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>
29 #include "i18n.h"
31 using namespace std;
32 using namespace Barry;
34 void Usage()
36 cerr
37 << "upldif - Command line LDIF uploader\n"
38 << " Copyright 2006-2010, 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 << " -P pass Simplistic method to specify device password\n"
42 << " -u Do the upload. If not specified, only dumps parsed\n"
43 << " LDIF data to stdout.\n"
44 << " -v Dump protocol data during operation\n"
45 << " -h This help output\n"
46 << endl;
49 template <class Record>
50 struct Store
52 std::vector<Record> records;
53 mutable typename std::vector<Record>::const_iterator rec_it;
54 int count;
56 Barry::ContactLdif ldif;
58 // Store constructor -- reads LDIF records from the given
59 // stream object and stores them in memory.
60 Store(std::istream &is)
61 : count(0),
62 ldif("")
64 Record rec;
65 while( is ) {
66 if( ldif.ReadLdif(is, rec) ) {
67 count++;
68 records.push_back(rec);
72 rec_it = records.begin();
75 ~Store()
77 cout << "Store counted " << dec << count << " records." << endl;
80 // Retrieval operator -- called by Barry during the upload
81 // process to get the next object
82 bool operator()(Record &rec, Builder &builder) const
84 if( rec_it == records.end() )
85 return false;
86 rec = *rec_it;
87 rec_it++;
88 return true;
91 // For easy data display and debugging.
92 void Dump(std::ostream &os) const
94 typename std::vector<Record>::const_iterator b = records.begin();
95 for( ; b != records.end(); ++b ) {
96 os << *b << endl;
101 template <class Record>
102 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
104 store.Dump(os);
105 return os;
108 int main(int argc, char *argv[])
110 INIT_I18N(PACKAGE);
112 cout.sync_with_stdio(true); // leave this on, since libusb uses
113 // stdio for debug messages
115 try {
117 uint32_t pin = 0;
118 bool data_dump = false,
119 do_upload = false;
120 string password;
122 // process command line options
123 for(;;) {
124 int cmd = getopt(argc, argv, "hp:P:uv");
125 if( cmd == -1 )
126 break;
128 switch( cmd )
130 case 'p': // Blackberry PIN
131 pin = strtoul(optarg, NULL, 16);
132 break;
134 case 'P': // Device password
135 password = optarg;
136 break;
138 case 'u': // do upload
139 do_upload = true;
140 break;
142 case 'v': // data dump on
143 data_dump = true;
144 break;
146 case 'h': // help
147 default:
148 Usage();
149 return 0;
153 // Read all contacts from stdin
154 Store<Contact> contactStore(cin);
156 // Only dump to stdout if not uploading to device
157 if( !do_upload ) {
158 cout << contactStore << endl;
159 return 0;
162 // Initialize the barry library. Must be called before
163 // anything else.
164 Barry::Init(data_dump);
166 // Probe the USB bus for Blackberry devices
167 // If user has specified a PIN, search for it
168 Barry::Probe probe;
169 int activeDevice = probe.FindActive(pin);
170 if( activeDevice == -1 ) {
171 cerr << "Device not found, or not specified" << endl;
172 return 1;
175 // Create our controller object
176 Barry::Controller con(probe.Get(activeDevice));
178 // make sure we're in desktop mode
179 Barry::Mode::Desktop desktop(con);
180 desktop.Open(password.c_str());
182 // upload all records to device
183 desktop.SaveDatabaseByType<Barry::Contact>(contactStore);
186 catch( Usb::Error &ue) {
187 std::cerr << "Usb::Error caught: " << ue.what() << endl;
189 catch( Barry::Error &se ) {
190 std::cerr << "Barry::Error caught: " << se.what() << endl;
192 catch( std::exception &e ) {
193 std::cerr << "std::exception caught: " << e.what() << endl;
194 return 1;
197 return 0;