Removed deprecated opensync header
[barry.git] / examples / addcalendar.cc
blobef491149ddcd4c98e4c7113fedf3ecf2b3933db0
1 ///
2 /// \file addcalendar.cc
3 /// Example code using the Barry library to add a calendar
4 /// entry to a Blackberry device.
5 ///
7 /*
8 Copyright (C) 2006-2009, 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 #define _XOPEN_SOURCE
24 #include <time.h>
25 #include <string.h>
26 #include <barry/barry.h>
27 #include <iostream>
29 using namespace std;
30 using namespace Barry;
33 void ReadLine(const char *prompt, std::string &data)
35 cout << prompt << ": ";
36 getline(cin, data);
39 bool ReadBool(const char *prompt)
41 cout << prompt << "? (y/n) ";
42 string data;
43 getline(cin, data);
44 return data[0] == 'y' || data[0] == 'Y';
47 time_t ReadTime(const char *prompt)
49 string data;
50 char *unprocessed = 0;
51 struct tm tm;
52 memset(&tm, 0, sizeof(tm));
53 tm.tm_isdst = -1;
55 do {
56 cout << prompt << ": ";
57 getline(cin, data);
58 unprocessed = strptime(data.c_str(), "%Y/%m/%d %H:%M", &tm);
59 } while( !unprocessed );
61 return mktime(&tm);
64 void ReadInput(Barry::Calendar &cal)
66 cal.AllDayEvent = ReadBool("All Day Event");
68 cout << "Note: enter dates in the following format: YYYY/MM/DD hh:mm\n";
69 cout << "Time is in 24 hour format\n\n";
70 cal.StartTime = ReadTime("Start Time");
71 cal.EndTime = ReadTime("End Time");
72 cal.NotificationTime = ReadTime("Notification Time");
74 ReadLine("Subject", cal.Subject);
75 ReadLine("Notes", cal.Notes);
76 ReadLine("Location", cal.Location);
78 cal.Recurring = false;
81 void Upload(const Barry::ProbeResult &device, const Barry::Calendar &cal)
83 // connect to address book
84 Controller con(device);
85 Mode::Desktop desktop(con);
86 desktop.Open();
87 unsigned int id = desktop.GetDBID("Calendar");
89 // find out what records are already there, and make new record ID
90 RecordStateTable table;
91 desktop.GetRecordStateTable(id, table);
92 uint32_t recordId = table.MakeNewRecordId();
94 // add it
95 desktop.AddRecordByType(recordId, cal);
96 cout << "Added successfully: " << endl << cal << endl;
99 int main(int argc, char *argv[])
101 try {
103 Barry::Init(true);
105 Barry::Probe probe;
106 if( probe.GetCount() == 0 ) {
107 cout << "No Blackberry found!" << endl;
108 return 1;
112 Barry::Calendar cal;
113 ReadInput(cal);
114 cout << "Just before upload: " << cal << endl;
115 Upload(probe.Get(0), cal);
118 catch( std::exception &e ) {
119 std::cerr << "Exception caught: " << e.what() << endl;
120 return 1;
123 return 0;