/clients/cs/samples/calendar/console/CalendarDemo.cs

http://google-gdata.googlecode.com/ · C# · 371 lines · 206 code · 53 blank · 112 comment · 11 complexity · be1ad7e2243620cadd5cd97af558cbb2 MD5 · raw file

  1. using System;
  2. using System.Text;
  3. using Google.GData.AccessControl;
  4. using Google.GData.Calendar;
  5. using Google.GData.Client;
  6. using Google.GData.Extensions;
  7. namespace CalendarDemoConsoleApplication
  8. {
  9. class CalendarDemo
  10. {
  11. private static String userName, userPassword, feedUri;
  12. /// <summary>
  13. /// Prints a list of the user's calendars.
  14. /// </summary>
  15. /// <param name="service">The authenticated CalendarService object.</param>
  16. static void PrintUserCalendars(CalendarService service)
  17. {
  18. FeedQuery query = new FeedQuery();
  19. query.Uri = new Uri("http://www.google.com/calendar/feeds/default");
  20. // Tell the service to query:
  21. AtomFeed calFeed = service.Query(query);
  22. Console.WriteLine("Your calendars:");
  23. Console.WriteLine();
  24. for (int i = 0; i < calFeed.Entries.Count; i++)
  25. {
  26. Console.WriteLine(calFeed.Entries[i].Title.Text);
  27. }
  28. Console.WriteLine();
  29. }
  30. /// <summary>
  31. /// Prints the titles of all events on the specified calendar.
  32. /// </summary>
  33. /// <param name="service">The authenticated CalendarService object.</param>
  34. static void PrintAllEvents(CalendarService service)
  35. {
  36. EventQuery myQuery = new EventQuery(feedUri);
  37. EventFeed myResultsFeed = service.Query(myQuery) as EventFeed;
  38. Console.WriteLine("All events on your calendar:");
  39. Console.WriteLine();
  40. for (int i = 0; i < myResultsFeed.Entries.Count; i++)
  41. {
  42. Console.WriteLine(myResultsFeed.Entries[i].Title.Text);
  43. }
  44. Console.WriteLine();
  45. }
  46. /// <summary>
  47. /// Prints the titles of all events matching a full-text query.
  48. /// </summary>
  49. /// <param name="service">The authenticated CalendarService object.</param>
  50. /// <param name="queryString">The text for which to query.</param>
  51. static void FullTextQuery(CalendarService service, String queryString)
  52. {
  53. EventQuery myQuery = new EventQuery(feedUri);
  54. myQuery.Query = queryString;
  55. EventFeed myResultsFeed = service.Query(myQuery) as EventFeed;
  56. Console.WriteLine("Events matching \"{0}\":", queryString);
  57. Console.WriteLine();
  58. for (int i = 0; i < myResultsFeed.Entries.Count; i++)
  59. {
  60. Console.WriteLine(myResultsFeed.Entries[i].Title.Text);
  61. }
  62. Console.WriteLine();
  63. }
  64. /// <summary>
  65. /// Prints the titles of all events in a specified date/time range.
  66. /// </summary>
  67. /// <param name="service">The authenticated CalendarService object.</param>
  68. /// <param name="startTime">Start time (inclusive) of events to print.</param>
  69. /// <param name="endTime">End time (exclusive) of events to print.</param>
  70. static void DateRangeQuery(CalendarService service, DateTime startTime, DateTime endTime)
  71. {
  72. EventQuery myQuery = new EventQuery(feedUri);
  73. myQuery.StartTime = startTime;
  74. myQuery.EndTime = endTime;
  75. EventFeed myResultsFeed = service.Query(myQuery) as EventFeed;
  76. Console.WriteLine("Matching events from {0} to {1}:",
  77. startTime.ToShortDateString(),
  78. endTime.ToShortDateString());
  79. Console.WriteLine();
  80. for (int i = 0; i < myResultsFeed.Entries.Count; i++)
  81. {
  82. Console.WriteLine(myResultsFeed.Entries[i].Title.Text);
  83. }
  84. Console.WriteLine();
  85. }
  86. /// <summary>
  87. /// Helper method to create either single-instance or recurring events.
  88. /// For simplicity, some values that might normally be passed as parameters
  89. /// (such as author name, email, etc.) are hard-coded.
  90. /// </summary>
  91. /// <param name="service">The authenticated CalendarService object.</param>
  92. /// <param name="entryTitle">Title of the event to create.</param>
  93. /// <param name="recurData">Recurrence value for the event, or null for
  94. /// single-instance events.</param>
  95. /// <returns>The newly-created EventEntry on the calendar.</returns>
  96. static EventEntry CreateEvent(CalendarService service, String entryTitle,
  97. String recurData)
  98. {
  99. EventEntry entry = new EventEntry();
  100. // Set the title and content of the entry.
  101. entry.Title.Text = entryTitle;
  102. entry.Content.Content = "Meet for a quick lesson.";
  103. // Set a location for the event.
  104. Where eventLocation = new Where();
  105. eventLocation.ValueString = "South Tennis Courts";
  106. entry.Locations.Add(eventLocation);
  107. // If a recurrence was requested, add it. Otherwise, set the
  108. // time (the current date and time) and duration (30 minutes)
  109. // of the event.
  110. if (recurData == null) {
  111. When eventTime = new When();
  112. eventTime.StartTime = DateTime.Now;
  113. eventTime.EndTime = eventTime.StartTime.AddMinutes(30);
  114. entry.Times.Add(eventTime);
  115. } else {
  116. Recurrence recurrence = new Recurrence();
  117. recurrence.Value = recurData;
  118. entry.Recurrence = recurrence;
  119. }
  120. // Send the request and receive the response:
  121. Uri postUri = new Uri(feedUri);
  122. AtomEntry insertedEntry = service.Insert(postUri, entry);
  123. return (EventEntry)insertedEntry;
  124. }
  125. /// <summary>
  126. /// Creates a single-instance event on a calendar.
  127. /// </summary>
  128. /// <param name="service">The authenticated CalendarService object.</param>
  129. /// <param name="entryTitle">Title of the event to create.</param>
  130. /// <returns>The newly-created EventEntry on the calendar.</returns>
  131. static EventEntry CreateSingleEvent(CalendarService service, String entryTitle)
  132. {
  133. return CreateEvent(service, entryTitle, null);
  134. }
  135. /// <summary>
  136. /// Creates a recurring event on a calendar. In this example, the event
  137. /// occurs every Tuesday from May 1, 2007 through September 4, 2007. Note
  138. /// that we are using iCal (RFC 2445) syntax; see http://www.ietf.org/rfc/rfc2445.txt
  139. /// for more information.
  140. /// </summary>
  141. /// <param name="service">The authenticated CalendarService object.</param>
  142. /// <param name="entryTitle">Title of the event to create.</param>
  143. /// <returns>The newly-created EventEntry on the calendar.</returns>
  144. static EventEntry CreateRecurringEvent(CalendarService service, String entryTitle)
  145. {
  146. String recurData =
  147. "DTSTART;VALUE=DATE:20070501\r\n" +
  148. "DTEND;VALUE=DATE:20070502\r\n" +
  149. "RRULE:FREQ=WEEKLY;BYDAY=Tu;UNTIL=20070904\r\n";
  150. return CreateEvent(service, entryTitle, recurData);
  151. }
  152. /// <summary>
  153. /// Updates the title of an existing calendar event.
  154. /// </summary>
  155. /// <param name="entry">The event to update.</param>
  156. /// <param name="newTitle">The new title for this event.</param>
  157. /// <returns>The updated EventEntry object.</returns>
  158. static EventEntry UpdateTitle(EventEntry entry, String newTitle)
  159. {
  160. entry.Title.Text = newTitle;
  161. return (EventEntry)entry.Update();
  162. }
  163. /// <summary>
  164. /// Adds a reminder to a calendar event.
  165. /// </summary>
  166. /// <param name="entry">The event to update.</param>
  167. /// <param name="numMinutes">Reminder time, in minutes.</param>
  168. /// <returns>The updated EventEntry object.</returns>
  169. static EventEntry AddReminder(EventEntry entry, int numMinutes)
  170. {
  171. Reminder reminder = new Reminder();
  172. reminder.Minutes = numMinutes;
  173. entry.Reminder = reminder;
  174. return (EventEntry)entry.Update();
  175. }
  176. /// <summary>
  177. /// Adds an extended property to a calendar event.
  178. /// </summary>
  179. /// <param name="entry">The event to update.</param>
  180. /// <returns>The updated EventEntry object.</returns>
  181. static EventEntry AddExtendedProperty(EventEntry entry)
  182. {
  183. ExtendedProperty property = new ExtendedProperty();
  184. property.Name = "http://www.example.com/schemas/2005#mycal.id";
  185. property.Value = "1234";
  186. entry.ExtensionElements.Add(property);
  187. return (EventEntry)entry.Update();
  188. }
  189. /// <summary>
  190. /// Retrieves and prints the access control lists of all
  191. /// of the authenticated user's calendars.
  192. /// </summary>
  193. /// <param name="service">The authenticated CalendarService object.</param>
  194. static void RetrieveAcls(CalendarService service)
  195. {
  196. FeedQuery query = new FeedQuery();
  197. query.Uri = new Uri("http://www.google.com/calendar/feeds/default");
  198. AtomFeed calFeed = service.Query(query);
  199. Console.WriteLine();
  200. Console.WriteLine("Sharing permissions for your calendars:");
  201. // Retrieve the meta-feed of all calendars.
  202. foreach (AtomEntry calendarEntry in calFeed.Entries)
  203. {
  204. Console.WriteLine("Calendar: {0}", calendarEntry.Title.Text);
  205. AtomLink link = calendarEntry.Links.FindService(
  206. AclNameTable.LINK_REL_ACCESS_CONTROL_LIST, null);
  207. // For each calendar, retrieve its ACL feed.
  208. if (link != null)
  209. {
  210. AclFeed feed = service.Query(new AclQuery(link.HRef.ToString()));
  211. foreach (AclEntry aclEntry in feed.Entries)
  212. {
  213. Console.WriteLine("\tScope: Type={0} ({1})", aclEntry.Scope.Type,
  214. aclEntry.Scope.Value);
  215. Console.WriteLine("\tRole: {0}", aclEntry.Role.Value);
  216. }
  217. }
  218. }
  219. }
  220. /// <summary>
  221. /// Shares a calendar with the specified user. Note that this method
  222. /// will not run by default.
  223. /// </summary>
  224. /// <param name="service">The authenticated CalendarService object.</param>
  225. /// <param name="aclFeedUri">the ACL feed URI of the calendar being shared.</param>
  226. /// <param name="userEmail">The email address of the user with whom to share.</param>
  227. /// <param name="role">The role of the user with whom to share.</param>
  228. /// <returns>The AclEntry returned by the server.</returns>
  229. static AclEntry AddAccessControl(CalendarService service, string aclFeedUri,
  230. string userEmail, AclRole role)
  231. {
  232. AclEntry entry = new AclEntry();
  233. entry.Scope = new AclScope();
  234. entry.Scope.Type = AclScope.SCOPE_USER;
  235. entry.Scope.Value = userEmail;
  236. entry.Role = role;
  237. Uri aclUri =
  238. new Uri("http://www.google.com/calendar/feeds/gdata.ops.test@gmail.com/acl/full");
  239. AclEntry insertedEntry = service.Insert(aclUri, entry);
  240. Console.WriteLine("Added user {0}", insertedEntry.Scope.Value);
  241. return insertedEntry;
  242. }
  243. /// <summary>
  244. /// Updates a user to have new access permissions over a calendar.
  245. /// Note that this method will not run by default.
  246. /// </summary>
  247. /// <param name="entry">An existing AclEntry representing sharing permissions.</param>
  248. /// <param name="newRole">The new role (access permissions) for the user.</param>
  249. /// <returns>The updated AclEntry.</returns>
  250. static AclEntry UpdateEntry(AclEntry entry, AclRole newRole)
  251. {
  252. entry.Role = newRole;
  253. AclEntry updatedEntry = entry.Update() as AclEntry;
  254. Console.WriteLine("Updated {0} to have role {1}", updatedEntry.Scope.Value,
  255. entry.Role.Value);
  256. return updatedEntry;
  257. }
  258. /// <summary>
  259. /// Deletes a user from a calendar's access control list, preventing
  260. /// that user from accessing the calendar. Note that this method will
  261. /// not run by default.
  262. /// </summary>
  263. /// <param name="entry">An existing AclEntry representing sharing permissions.</param>
  264. static void DeleteEntry(AclEntry entry)
  265. {
  266. entry.Delete();
  267. }
  268. /// <summary>
  269. /// Runs the methods above to demonstrate usage of the .NET
  270. /// client library. The methods that add, update, or remove
  271. /// users on access control lists will not run by default.
  272. /// </summary>
  273. static void RunSample()
  274. {
  275. CalendarService service = new CalendarService("exampleCo-exampleApp-1");
  276. service.setUserCredentials(userName, userPassword);
  277. // Demonstrate retrieving a list of the user's calendars.
  278. PrintUserCalendars(service);
  279. // Demonstrate various feed queries.
  280. PrintAllEvents(service);
  281. FullTextQuery(service, "Tennis");
  282. DateRangeQuery(service, new DateTime(2007, 1, 5), new DateTime(2007, 1, 7));
  283. // Demonstrate creating a single-occurrence event.
  284. EventEntry singleEvent = CreateSingleEvent(service, "Tennis with Mike");
  285. Console.WriteLine("Successfully created event {0}", singleEvent.Title.Text);
  286. // Demonstrate creating a recurring event.
  287. AtomEntry recurringEvent = CreateRecurringEvent(service, "Tennis with Dan");
  288. Console.WriteLine("Successfully created recurring event {0}", recurringEvent.Title.Text);
  289. // Demonstrate updating the event's text.
  290. singleEvent = UpdateTitle(singleEvent, "Important meeting");
  291. Console.WriteLine("Event's new title is {0}", singleEvent.Title.Text);
  292. // Demonstrate adding a reminder. Note that this will only work on a primary
  293. // calendar.
  294. singleEvent = AddReminder(singleEvent, 15);
  295. Console.WriteLine("Set a {0}-minute reminder for the event.", singleEvent.Reminder.Minutes);
  296. // Demonstrate adding an extended property.
  297. AddExtendedProperty(singleEvent);
  298. // Demonstrate deleting the item.
  299. singleEvent.Delete();
  300. // Demonstrate retrieving access control lists for all calendars.
  301. RetrieveAcls(service);
  302. }
  303. static void Main(string[] args)
  304. {
  305. if (args.Length != 3)
  306. {
  307. Console.WriteLine("Usage: gcal_demo <username> <password> <feedUri>");
  308. }
  309. else
  310. {
  311. userName = args[0];
  312. userPassword = args[1];
  313. feedUri = args[2];
  314. RunSample();
  315. }
  316. }
  317. }
  318. }