desktop: added TimeZones parameter to CalendarEditDlg class
[barry/progweb.git] / tools / upldif.cc
blob580acb338548aee50b0336d92743d07fa449b1ee
1 ///
2 /// \file upldif.cc
3 /// LDIF contact uploader
4 ///
6 /*
7 Copyright (C) 2006-2012, 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 "i18n.h"
31 #include "barrygetopt.h"
33 using namespace std;
34 using namespace Barry;
36 void Usage()
38 cerr
39 << "upldif - Command line LDIF uploader\n"
40 << " Copyright 2006-2012, Net Direct Inc. (http://www.netdirect.ca/)\n\n"
41 << " -p pin PIN of device to talk with\n"
42 << " If only one device plugged in, this flag is optional\n"
43 << " -P pass Simplistic method to specify device password\n"
44 << " -u Do the upload. If not specified, only dumps parsed\n"
45 << " LDIF data to stdout.\n"
46 << " -v Dump protocol data during operation\n"
47 << " -h This help output\n"
48 << endl;
51 template <class Record>
52 struct Store
54 std::vector<Record> records;
55 mutable typename std::vector<Record>::const_iterator rec_it;
56 int count;
58 Barry::ContactLdif ldif;
60 // Store constructor -- reads LDIF records from the given
61 // stream object and stores them in memory.
62 Store(std::istream &is)
63 : count(0),
64 ldif("")
66 Record rec;
67 while( is ) {
68 if( ldif.ReadLdif(is, rec) ) {
69 count++;
70 records.push_back(rec);
74 rec_it = records.begin();
77 ~Store()
79 cout << "Store counted " << dec << count << " records." << endl;
82 // Retrieval operator -- called by Barry during the upload
83 // process to get the next object
84 bool operator()(Record &rec, Builder &builder) const
86 if( rec_it == records.end() )
87 return false;
88 rec = *rec_it;
89 rec_it++;
90 return true;
93 // For easy data display and debugging.
94 void Dump(std::ostream &os) const
96 typename std::vector<Record>::const_iterator b = records.begin();
97 for( ; b != records.end(); ++b ) {
98 os << *b << endl;
103 template <class Record>
104 std::ostream& operator<< (std::ostream &os, const Store<Record> &store)
106 store.Dump(os);
107 return os;
110 int main(int argc, char *argv[])
112 INIT_I18N(PACKAGE);
114 cout.sync_with_stdio(true); // leave this on, since libusb uses
115 // stdio for debug messages
117 try {
119 uint32_t pin = 0;
120 bool data_dump = false,
121 do_upload = false;
122 string password;
124 // process command line options
125 for(;;) {
126 int cmd = getopt(argc, argv, "hp:P: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 'P': // Device password
137 password = optarg;
138 break;
140 case 'u': // do upload
141 do_upload = true;
142 break;
144 case 'v': // data dump on
145 data_dump = true;
146 break;
148 case 'h': // help
149 default:
150 Usage();
151 return 0;
155 // Read all contacts from stdin
156 Store<Contact> contactStore(cin);
158 // Only dump to stdout if not uploading to device
159 if( !do_upload ) {
160 cout << contactStore << endl;
161 return 0;
164 // Initialize the barry library. Must be called before
165 // anything else.
166 Barry::Init(data_dump);
168 // Probe the USB bus for Blackberry devices
169 // If user has specified a PIN, search for it
170 Barry::Probe probe;
171 int activeDevice = probe.FindActive(pin);
172 if( activeDevice == -1 ) {
173 cerr << "Device not found, or not specified" << endl;
174 return 1;
177 // Create our controller object
178 Barry::Controller con(probe.Get(activeDevice));
180 // make sure we're in desktop mode
181 Barry::Mode::Desktop desktop(con);
182 desktop.Open(password.c_str());
184 // upload all records to device
185 desktop.SaveDatabaseByType<Barry::Contact>(contactStore);
188 catch( Usb::Error &ue) {
189 std::cerr << "Usb::Error caught: " << ue.what() << endl;
191 catch( Barry::Error &se ) {
192 std::cerr << "Barry::Error caught: " << se.what() << endl;
194 catch( std::exception &e ) {
195 std::cerr << "std::exception caught: " << e.what() << endl;
196 return 1;
199 return 0;