i18n work in progress... desperately needs some housecleaning.
[barry.git] / examples / addcalendar.cc
blob3cb9a5c08960f068db7c4eac282dec70a98401ae
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 Mode::Desktop desktop(con);
85 desktop.Open();
86 unsigned int id = desktop.GetDBID("Calendar");
88 // find out what records are already there, and make new record ID
89 RecordStateTable table;
90 desktop.GetRecordStateTable(id, table);
91 uint32_t recordId = table.MakeNewRecordId();
93 // add it
94 desktop.AddRecordByType(recordId, cal);
95 cout << "Added successfully: " << endl << cal << endl;
98 int main(int argc, char *argv[])
100 try {
102 Barry::Init(true);
104 Barry::Probe probe;
105 if( probe.GetCount() == 0 ) {
106 cout << "No Blackberry found!" << endl;
107 return 1;
111 Barry::Calendar cal;
112 ReadInput(cal);
113 cout << "Just before upload: " << cal << endl;
114 Upload(probe.Get(0), cal);
117 catch( std::exception &e ) {
118 std::cerr << "Exception caught: " << e.what() << endl;
119 return 1;
122 return 0;