tools: added support for reoriented screens in bwatch
[barry/progweb.git] / tools / upldif.cc
blob36958d95e39545a5a3afa151df2e456f220011e7
1 ///
2 /// \file upldif.cc
3 /// LDIF contact uploader
4 ///
6 /*
7 Copyright (C) 2006-2011, 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 <stdlib.h>
28 #include <string>
29 #include <getopt.h>
30 #include "i18n.h"
32 using namespace std;
33 using namespace Barry;
35 void Usage()
37 cerr
38 << "upldif - Command line LDIF uploader\n"
39 << " Copyright 2006-2011, Net Direct Inc. (http://www.netdirect.ca/)\n\n"
40 << " -p pin PIN of device to talk with\n"
41 << " If only one device plugged in, this flag is optional\n"
42 << " -P pass Simplistic method to specify device password\n"
43 << " -u Do the upload. If not specified, only dumps parsed\n"
44 << " LDIF data to stdout.\n"
45 << " -v Dump protocol data during operation\n"
46 << " -h This help output\n"
47 << endl;
50 template <class Record>
51 struct Store
53 std::vector<Record> records;
54 mutable typename std::vector<Record>::const_iterator rec_it;
55 int count;
57 Barry::ContactLdif ldif;
59 // Store constructor -- reads LDIF records from the given
60 // stream object and stores them in memory.
61 Store(std::istream &is)
62 : count(0),
63 ldif("")
65 Record rec;
66 while( is ) {
67 if( ldif.ReadLdif(is, rec) ) {
68 count++;
69 records.push_back(rec);
73 rec_it = records.begin();
76 ~Store()
78 cout << "Store counted " << dec << count << " records." << endl;
81 // Retrieval operator -- called by Barry during the upload
82 // process to get the next object
83 bool operator()(Record &rec, Builder &builder) const
85 if( rec_it == records.end() )
86 return false;
87 rec = *rec_it;
88 rec_it++;
89 return true;
92 // For easy data display and debugging.
93 void Dump(std::ostream &os) const
95 typename std::vector<Record>::const_iterator b = records.begin();
96 for( ; b != records.end(); ++b ) {
97 os << *b << endl;
102 template <class Record>
103 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
105 store.Dump(os);
106 return os;
109 int main(int argc, char *argv[])
111 INIT_I18N(PACKAGE);
113 cout.sync_with_stdio(true); // leave this on, since libusb uses
114 // stdio for debug messages
116 try {
118 uint32_t pin = 0;
119 bool data_dump = false,
120 do_upload = false;
121 string password;
123 // process command line options
124 for(;;) {
125 int cmd = getopt(argc, argv, "hp:P:uv");
126 if( cmd == -1 )
127 break;
129 switch( cmd )
131 case 'p': // Blackberry PIN
132 pin = strtoul(optarg, NULL, 16);
133 break;
135 case 'P': // Device password
136 password = optarg;
137 break;
139 case 'u': // do upload
140 do_upload = true;
141 break;
143 case 'v': // data dump on
144 data_dump = true;
145 break;
147 case 'h': // help
148 default:
149 Usage();
150 return 0;
154 // Read all contacts from stdin
155 Store<Contact> contactStore(cin);
157 // Only dump to stdout if not uploading to device
158 if( !do_upload ) {
159 cout << contactStore << endl;
160 return 0;
163 // Initialize the barry library. Must be called before
164 // anything else.
165 Barry::Init(data_dump);
167 // Probe the USB bus for Blackberry devices
168 // If user has specified a PIN, search for it
169 Barry::Probe probe;
170 int activeDevice = probe.FindActive(pin);
171 if( activeDevice == -1 ) {
172 cerr << "Device not found, or not specified" << endl;
173 return 1;
176 // Create our controller object
177 Barry::Controller con(probe.Get(activeDevice));
179 // make sure we're in desktop mode
180 Barry::Mode::Desktop desktop(con);
181 desktop.Open(password.c_str());
183 // upload all records to device
184 desktop.SaveDatabaseByType<Barry::Contact>(contactStore);
187 catch( Usb::Error &ue) {
188 std::cerr << "Usb::Error caught: " << ue.what() << endl;
190 catch( Barry::Error &se ) {
191 std::cerr << "Barry::Error caught: " << se.what() << endl;
193 catch( std::exception &e ) {
194 std::cerr << "std::exception caught: " << e.what() << endl;
195 return 1;
198 return 0;