Exemple #1
0
        public MainWindow()
        {
            InitializeComponent();

            // Setup background worker to perform the bulk of the work.
            backgroundWorker                            = new BackgroundWorker();
            backgroundWorker.DoWork                    += backgroundWorker_DoWork;
            backgroundWorker.ProgressChanged           += backgroundWorker_ProgressChanged;
            backgroundWorker.WorkerReportsProgress      = true;
            backgroundWorker.WorkerSupportsCancellation = true;

            // Try to load credentials from a file, if successful, put them in the form.
            // If there's something wrong with them, delete them.
            winsSync = new WinsSync();
            //try
            //{
            //    winsSync.LoadLoginCreds();
            //    textBox.Text = winsSync.Username;
            //    passwordBox.Password = winsSync.ConvertToUnsecureString(winsSync.getSPassword());
            //}
            ////todo: fix the dll, throws null reference exception if the files don't exist at all.
            //// We do catch it and handle it (by doing nothing), but it's not a good implementation.
            //catch (CryptographicException)
            //{
            //    winsSync.DeleteLoginCreds();
            //}
            //catch(NullReferenceException)
            //{
            //    //do nothing
            //}
        }
        public MainWindow()
        {
            InitializeComponent();

            // Setup background worker to perform the bulk of the work.
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.WorkerSupportsCancellation = true;

            // Try to load credentials from a file, if successful, put them in the form.
            // If there's something wrong with them, delete them.
            winsSync = new WinsSync();
            //try
            //{
            //    winsSync.LoadLoginCreds();
            //    textBox.Text = winsSync.Username;
            //    passwordBox.Password = winsSync.ConvertToUnsecureString(winsSync.getSPassword());
            //}
            ////todo: fix the dll, throws null reference exception if the files don't exist at all.
            //// We do catch it and handle it (by doing nothing), but it's not a good implementation.
            //catch (CryptographicException)
            //{
            //    winsSync.DeleteLoginCreds();
            //}
            //catch(NullReferenceException)
            //{
            //    //do nothing
            //}
        }
        static void Main(string[] args)
        {
            WinsSync winssync = new WinsSync();

            //Setup our credentials.
            winssync.SetupGoogleCreds();
            try
            {
                winssync.LoadLoginCreds();
            }
            catch (CryptographicException)
            {
                // Saved credentials are somehow corrupt, let's just delete them and get them from the user.
                winssync.DeleteLoginCreds();
                winssync.LoadLoginCreds();
            }

            // Time to log in and get our schedule.
            // Retry loop in case javascript execution breaks. It happens often enough, and we need to log out, and back in to recover.
            int retries = 5;
            while (retries > 0)
            {
                try
                {
                    winssync.ScheduleGET();
                    break;
                }
                catch (ScheduleGETException ex)
                {
                    Console.WriteLine(ex.ToString());
                    if (--retries == 0)
                    {
                        Console.WriteLine("We tried getting your schedule five times and it didn't work.");
                        Console.WriteLine("Something must be up with your connection to the server.");
                        Console.WriteLine("Please check your connection and try again. Press any key to quit.");
                        if (winssync.Automate != "Automate")
                        {
                            Console.ReadKey();
                        }
                        Environment.Exit(0);
                    }
                }
            }

            // Parse the HTML for our schedule info.
            winssync.FindTable();
            winssync.ParseTable();
            winssync.ParseRows();

            // Let's let the user look at their schedule while we're busy uploading.
            winssync.DisplayResults();

            // Now we need to upload our schedule to Google.
            // Sometimes the upload request isn't processed correctly, just retrying fixes it almost every time.
            Console.WriteLine("Uploading your schedule now...");
            retries = 5;
            while (retries > 0)
            {
                try
                {
                    winssync.UploadResults().Wait();
                    break;
                }
                catch (Exception ex)
                {
                    if (--retries == 0)
                    {
                        Console.WriteLine("We tried uploading your schedule multiple times and it didn't work.");
                        Console.WriteLine("Here's a nasty error message to explain why:");
                        Console.Write(ex.ToString());
                    }
                }
            }
            Console.WriteLine("Upload Complete, Press Enter to exit.");

            //After a successful run with input, allow the user to enter "Automate" to remove all needed user input from future runs.
            //Don't prompt the user to use this mode, since it's a hassle to stop automated runs.
            if (winssync.Automate != "Automate")
            {
                if ((Console.ReadLine().Equals("Automate") && (winssync.Savedlogin == true)))
                {
                    winssync.SaveLoginCreds("Automate");
                    winssync.AutomateRun();
                    Console.WriteLine("Alright, all future runs will require no input.");
                    Console.WriteLine("To reset automation, manually delete the files at: ");
                    Console.WriteLine(string.Empty);
                    Console.WriteLine(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), ".credentials"));
                    Console.WriteLine(string.Empty);
                    Console.WriteLine("Press Any key to exit.");
                    Console.ReadKey();
                }
            }
        }
Exemple #4
0
        static void Main(string[] args)
        {
            WinsSync winssync = new WinsSync();

            //Setup our credentials.
            winssync.SetupGoogleCreds();
            try
            {
                winssync.LoadLoginCreds();
            }
            catch (CryptographicException)
            {
                // Saved credentials are somehow corrupt, let's just delete them and get them from the user.
                winssync.DeleteLoginCreds();
                winssync.LoadLoginCreds();
            }

            // Time to log in and get our schedule.
            // Retry loop in case javascript execution breaks. It happens often enough, and we need to log out, and back in to recover.
            int retries = 5;

            while (retries > 0)
            {
                try
                {
                    winssync.ScheduleGET();
                    break;
                }
                catch (ScheduleGETException ex)
                {
                    Console.WriteLine(ex.ToString());
                    if (--retries == 0)
                    {
                        Console.WriteLine("We tried getting your schedule five times and it didn't work.");
                        Console.WriteLine("Something must be up with your connection to the server.");
                        Console.WriteLine("Please check your connection and try again. Press any key to quit.");
                        if (winssync.Automate != "Automate")
                        {
                            Console.ReadKey();
                        }
                        Environment.Exit(0);
                    }
                }
            }

            // Parse the HTML for our schedule info.
            winssync.FindTable();
            winssync.ParseTable();
            winssync.ParseRows();

            // Let's let the user look at their schedule while we're busy uploading.
            winssync.DisplayResults();

            // Now we need to upload our schedule to Google.
            // Sometimes the upload request isn't processed correctly, just retrying fixes it almost every time.
            Console.WriteLine("Uploading your schedule now...");
            retries = 5;
            while (retries > 0)
            {
                try
                {
                    winssync.UploadResults().Wait();
                    break;
                }
                catch (Exception ex)
                {
                    if (--retries == 0)
                    {
                        Console.WriteLine("We tried uploading your schedule multiple times and it didn't work.");
                        Console.WriteLine("Here's a nasty error message to explain why:");
                        Console.Write(ex.ToString());
                    }
                }
            }
            Console.WriteLine("Upload Complete, Press Enter to exit.");

            //After a successful run with input, allow the user to enter "Automate" to remove all needed user input from future runs.
            //Don't prompt the user to use this mode, since it's a hassle to stop automated runs.
            if (winssync.Automate != "Automate")
            {
                if ((Console.ReadLine().Equals("Automate") && (winssync.Savedlogin == true)))
                {
                    winssync.SaveLoginCreds("Automate");
                    winssync.AutomateRun();
                    Console.WriteLine("Alright, all future runs will require no input.");
                    Console.WriteLine("To reset automation, manually delete the files at: ");
                    Console.WriteLine(string.Empty);
                    Console.WriteLine(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), ".credentials"));
                    Console.WriteLine(string.Empty);
                    Console.WriteLine("Press Any key to exit.");
                    Console.ReadKey();
                }
            }
        }