示例#1
0
文件: Program.cs 项目: jasonsch/cli
        private static bool FindCalendarEvent(CalendarService service, string calendarID, EventInformation eventInfo)
        {
            IEnumerable <Tuple <EventDateTime, EventDateTime> > events = eventInfo.GetEventTimes();

            foreach (var t in events)
            {
                if (FindCalendarEventWorker(service, calendarID, eventInfo, t.Item1))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#2
0
文件: Program.cs 项目: jasonsch/cli
 public static void AddCalendarEvent(CalendarService service, string CalendarID, EventInformation EventInfo)
 {
     if (!FindCalendarEvent(service, CalendarID, EventInfo))
     {
         foreach (var time in EventInfo.GetEventTimes())
         {
             InsertCalendarEvent(service, CalendarID, EventInfo, time.Item1, time.Item2);
         }
     }
     else
     {
         Console.WriteLine("Found an existing event that matches so ignoring add.");
     }
 }
示例#3
0
文件: Program.cs 项目: jasonsch/cli
        static void Main(string[] args)
        {
            UserCredential   credential;
            string           CalendarID      = "primary";
            EventInformation EventInfo       = new EventInformation();
            string           EventUrl        = null;
            bool             ParseOnly       = false; // If this is true we parse the event URL and display its info but don't add it to the calendar.
            List <string>    NakedParameters = null;
            bool             FlagsPassed     = false; // If we don't see any arguments then we act like the classic "cal" command

            // TODO -- https://www.twilio.com/blog/2018/05/user-secrets-in-a-net-core-console-app.html
            using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) // TODO -- Make this a resource
            {
                string credPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart.json"); // TODO

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
            }

            // Create Google Calendar API service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            // TODO -- Check for required arguments
            // TODO -- Do some sanity checking (end >= start, end not specified if all-day, ...)
            // TODO -- A date string like "2018-01-28" will get round-tripped (through DateTime.Parse()) as having an explicit time of 12AM
            OptionSet options = new OptionSet();

            options.Add("?|h|help", value => { PrintUsage(); });
            options.Add("c|calendar=", value => { CalendarID = FindCalendarByName(service, value); if (CalendarID == null)
                                                  {
                                                      PrintUsage("Couldn't find specified calendar!");
                                                  }
                                                  FlagsPassed = true; });
            options.Add("d|description=", value => { EventInfo.Description = value; FlagsPassed = true; });
            options.Add("e|end=", value => { EventInfo.SetEndDate(value); FlagsPassed = true; });
            options.Add("p|parse-only", value => { ParseOnly = true; FlagsPassed = true; });
            options.Add("r|reminder=", value => { EventInfo.AddReminder(value); FlagsPassed = true; });
            options.Add("s|start=", value => { EventInfo.SetStartDate(value); FlagsPassed = true; });
            options.Add("t|title=", value => { EventInfo.Title = value; FlagsPassed = true; });
            options.Add("u|url=", value => { EventUrl = value; FlagsPassed = true; });
            options.Add("w|where=", value => { EventInfo.Location = value; FlagsPassed = true; });

            NakedParameters = options.Parse(args);

            if (FlagsPassed)
            {
                if (!String.IsNullOrEmpty(EventUrl))
                {
                    HandleEventURL(service, CalendarID, EventUrl, ParseOnly);
                }
                else
                {
                    //
                    // The event start and title are required, everything else is optional.
                    //
                    if (EventInfo.GetEventTimes().Count == 0)
                    {
                        PrintUsage("Start date is required!");
                    }
                    else if (String.IsNullOrEmpty(EventInfo.Title))
                    {
                        PrintUsage("A title is required!");
                    }

                    AddCalendarEvent(service, CalendarID, EventInfo);
                }
            }
            else
            {
                if (NakedParameters.Count == 0)
                {
                    ShowCalendar();
                }
                else if (NakedParameters.Count == 1)
                {
                    ShowCalendar(Int32.Parse(NakedParameters[0]));
                }
                else if (NakedParameters.Count == 2)
                {
                    ShowCalendar(Int32.Parse(NakedParameters[0]), Int32.Parse(NakedParameters[1]));
                }
                else
                {
                    PrintUsage();
                }
            }
        }