Bumped copyright dates for 2013
[barry.git] / examples / addcontact.cc
blob291d39599180caeaa47f8d8e5b49e4a6c17df273
1 ///
2 /// \file addcontact.cc
3 /// Example code using the Barry library to add a contact
4 /// to a Blackberry device.
5 ///
7 /*
8 Copyright (C) 2006-2013, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <barry/barry.h>
24 #include <iostream>
25 #include <fstream>
26 #include <iomanip>
28 using namespace std;
29 using namespace Barry;
32 void ReadLine(const char *prompt, std::string &data)
34 cout << prompt << ": ";
35 getline(cin, data);
38 void ReadDate(const char *prompt, Barry::Date &date)
40 string datestr;
42 for( ;; ) {
43 ReadLine(prompt, datestr);
44 if( !datestr.size() ) {
45 // blank
46 date.Clear();
47 break;
50 if( date.FromYYYYMMDD(datestr) )
51 break;
53 cout << "Unable to parse date, try again" << endl;
57 void ReadInput(Barry::Contact &contact)
59 ReadLine("First Name", contact.FirstName);
60 ReadLine("Last Name", contact.LastName);
61 ReadLine("Job Title", contact.JobTitle);
63 string email;
64 do {
65 ReadLine("Email Address (blank to end)", email);
66 if( email.size() )
67 contact.EmailAddresses.push_back(email);
68 } while( email.size() );
70 ReadLine("Main Phone Number", contact.Phone);
71 ReadLine("Home Phone Number", contact.HomePhone);
72 ReadLine("Work Phone Number", contact.WorkPhone);
73 ReadLine("Fax Number", contact.Fax);
74 ReadLine("Cell Number", contact.MobilePhone);
75 ReadLine("Pager Number", contact.Pager);
76 ReadLine("Company", contact.Company);
77 ReadLine("Address Line 1", contact.WorkAddress.Address1);
78 ReadLine("Address Line 2", contact.WorkAddress.Address2);
79 ReadLine("Address Line 3", contact.WorkAddress.Address3);
80 ReadLine("City", contact.WorkAddress.City);
81 ReadLine("Province / State", contact.WorkAddress.Province);
82 ReadLine("Country", contact.WorkAddress.Country);
83 ReadLine("Postal / Zip Code", contact.WorkAddress.PostalCode);
84 ReadLine("Notes", contact.Notes);
86 ReadDate("Birthday (YYYYMMDD format)", contact.Birthday);
87 ReadDate("Anniversary (YYYYMMDD format)", contact.Anniversary);
89 string categories;
90 ReadLine("Categories", categories);
91 contact.Categories.CategoryStr2List(categories);
93 string image_path;
94 ReadLine("Photo path", image_path);
95 if( image_path.size() ) {
96 ifstream in(image_path.c_str());
97 char block[4096];
99 contact.Image.clear();
100 while( in.read(block, sizeof(block)) ) {
101 contact.Image.append(block, in.gcount());
104 else {
105 cout << "Can't open: " << image_path << endl;
109 void Upload(const Barry::ProbeResult &device, const Barry::Contact &contact)
111 // connect to address book
112 Controller con(device);
113 Mode::Desktop desktop(con);
114 desktop.Open();
115 unsigned int id = desktop.GetDBID("Address Book");
117 // find out what records are already there, and make new record ID
118 RecordStateTable table;
119 desktop.GetRecordStateTable(id, table);
120 uint32_t recordId = table.MakeNewRecordId();
122 // add it
123 desktop.AddRecordByType(recordId, contact);
124 cout << "Added successfully." << endl;
127 int main(int argc, char *argv[])
129 try {
131 Barry::Init();
133 Barry::Probe probe;
134 if( probe.GetCount() == 0 ) {
135 cout << "No Blackberry found!" << endl;
136 return 1;
138 else {
139 cout << "Using PIN: "
140 << probe.Get(0).m_pin.Str() << endl;
144 Barry::Contact contact;
145 ReadInput(contact);
146 Upload(probe.Get(0), contact);
149 catch( std::exception &e ) {
150 std::cerr << "Exception caught: " << e.what() << endl;
151 return 1;
154 return 0;