- added GCC visibility support, limiting what symbols are visible
[barry.git] / examples / addcalendar.cc
blobc4ea47ba500e08b65d189de226ab3b030baeb9ba
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-2008, 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 <barry/barry.h>
26 #include <iostream>
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 bool ReadBool(const char *prompt)
40 cout << prompt << "? (y/n) ";
41 string data;
42 getline(cin, data);
43 return data[0] == 'y' || data[0] == 'Y';
46 time_t ReadTime(const char *prompt)
48 string data;
49 char *unprocessed = 0;
50 struct tm tm;
51 memset(&tm, 0, sizeof(tm));
52 tm.tm_isdst = -1;
54 do {
55 cout << prompt << ": ";
56 getline(cin, data);
57 unprocessed = strptime(data.c_str(), "%Y/%m/%d %H:%M", &tm);
58 } while( !unprocessed );
60 return mktime(&tm);
63 void ReadInput(Barry::Calendar &cal)
65 cal.AllDayEvent = ReadBool("All Day Event");
67 cout << "Note: enter dates in the following format: YYYY/MM/DD hh:mm\n";
68 cout << "Time is in 24 hour format\n\n";
69 cal.StartTime = ReadTime("Start Time");
70 cal.EndTime = ReadTime("End Time");
71 cal.NotificationTime = ReadTime("Notification Time");
73 ReadLine("Subject", cal.Subject);
74 ReadLine("Notes", cal.Notes);
75 ReadLine("Location", cal.Location);
77 cal.Recurring = false;
80 void Upload(const Barry::ProbeResult &device, const Barry::Calendar &cal)
82 // connect to address book
83 Controller con(device);
84 con.OpenMode(Controller::Desktop);
85 unsigned int id = con.GetDBID("Calendar");
87 // find out what records are already there, and make new record ID
88 RecordStateTable table;
89 con.GetRecordStateTable(id, table);
90 uint32_t recordId = table.MakeNewRecordId();
92 // add it
93 con.AddRecordByType(recordId, cal);
94 cout << "Added successfully: " << endl << cal << endl;
97 int main(int argc, char *argv[])
99 try {
101 Barry::Init(true);
103 Barry::Probe probe;
104 if( probe.GetCount() == 0 ) {
105 cout << "No Blackberry found!" << endl;
106 return 1;
110 Barry::Calendar cal;
111 ReadInput(cal);
112 cout << "Just before upload: " << cal << endl;
113 Upload(probe.Get(0), cal);
116 catch( std::exception &e ) {
117 std::cerr << "Exception caught: " << e.what() << endl;
118 return 1;
121 return 0;