private void openCodeFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.CheckFileExists = true; ofd.Filter = "All Files|*.*"; if (cppRadioButton.Checked) { ofd.Filter = "CPP Files|*.cpp|Header Files|*.h*|All Files|*.*"; } if (ansiCradioButton.Checked) { ofd.Filter = "C Files|*.c|Header Files|*.h*|All Files|*.*"; } if (JavaRadioButton.Checked) { ofd.Filter = "Java Files|*.java|All Files|*.*"; } if (PascalRadioButton.Checked) { ofd.Filter = "Pascal Files|*.pascal|All Files|*.*"; } if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (LocalDirectory.GetFileSize(ofd.FileName) < 1024 * 512) { codeTextBox.OpenFile(ofd.FileName); } else { MessageBox.Show("File is too big. Please select a valid file"); } } }
/// <summary> /// Parse HTML file and find all local contents to download. /// </summary> /// <param name="pnum">Problem number</param> /// <param name="replace">True, if you want to replace old files.</param> /// <returns>List of files to download</returns> public static List <DownloadTask> ProcessHtmlContent(long pnum, bool replace) { try { string external = string.Format("http://uva.onlinejudge.org/external/{0}/", pnum / 100); string filepath = LocalDirectory.GetProblemHtml(pnum); if (!File.Exists(filepath)) { return(new List <DownloadTask>()); } List <string> urls = new List <string>(); List <DownloadTask> tasks = new List <DownloadTask>(); HtmlAgilityPack.HtmlDocument htdoc = new HtmlAgilityPack.HtmlDocument(); htdoc.Load(filepath); DFS(htdoc.DocumentNode, urls); htdoc.Save(filepath); foreach (string str in urls) { string url = str.StartsWith("./") ? str.Remove(0, 2) : str; while (url.StartsWith("/")) { url = url.Remove(0, 1); } string file = url.Replace('/', Path.DirectorySeparatorChar); file = LocalDirectory.GetProblemContent(pnum, file); if (replace || LocalDirectory.GetFileSize(file) < 10) { tasks.Add(new DownloadTask(external + url, file, pnum)); } } urls.Clear(); return(tasks); } catch (Exception ex) { Logger.Add(ex.Message, "Internet"); return(new List <DownloadTask>()); } }
public static bool UpdateAll() { const double PROBLEM_ALIVE_DAY = 1; const double USER_SUB_ALIVE_DAY = 0.5; bool result = true; //if database file is too old redownload string file = LocalDirectory.GetProblemInfoFile(); if (LocalDirectory.GetFileSize(file) < 100 || (new TimeSpan( DateTime.Now.Ticks - new FileInfo(file).LastWriteTime.Ticks ).TotalDays > PROBLEM_ALIVE_DAY)) { UVA_Arena.Internet.Downloader.DownloadProblemDatabase(); result = false; } //update user submissions if not available if (LocalDatabase.ContainsUser(RegistryAccess.DefaultUsername)) { file = LocalDirectory.GetUserSubPath(RegistryAccess.DefaultUsername); if (LocalDirectory.GetFileSize(file) < 50 || (new TimeSpan( DateTime.Now.Ticks - new FileInfo(file).LastWriteTime.Ticks ).TotalDays > USER_SUB_ALIVE_DAY)) { long sid = 0; if (LocalDatabase.DefaultUser != null) { sid = LocalDatabase.DefaultUser.LastSID; } UVA_Arena.Internet.Downloader.DownloadDefaultUserInfo(sid); } } //download category index if too old UVA_Arena.Internet.Downloader.DownloadCategoryIndex(); return(result); }
private string DownloadFile(string url, string file, int minSiz, int tryCount = 2) { try { string tmp = Path.GetTempFileName(); webClient.DownloadFile(url, tmp); LocalDirectory.CreateFile(file); if (LocalDirectory.GetFileSize(tmp) >= minSiz) { File.Copy(tmp, file, true); File.Delete(tmp); return("Success."); } else { File.Delete(tmp); return("File doesn't have desired length."); } } catch (Exception ex) { if (tryCount > 0) { System.Threading.Thread.Sleep(300); return(DownloadFile(url, file, minSiz, tryCount - 1)); } //if couldn't be downloaded if (File.Exists(file) && LocalDirectory.GetFileSize(file) < minSiz) { File.Delete(file); } Logger.Add(ex.Message, this.Name); return("Download Failed."); } }
private void DelayInitialize(object background) { //run in background if ((bool)background) { this.Cursor = Cursors.AppStarting; System.Threading.ThreadPool.QueueUserWorkItem(DelayInitialize, false); return; } //load problem database LocalDatabase.RunLoadAsync(false); //load controls bool _initialized = false; this.BeginInvoke((MethodInvoker) delegate { //add controls AddControls(); //add buttons to the top right beside control buttons //AddActiveButtons(); _initialized = true; this.Cursor = Cursors.Default; Logger.Add("Initialized all controls", "Main Form"); loadingPanel.Visible = false; }); //update problem database if not available if (LocalDirectory.GetFileSize(LocalDirectory.GetProblemInfoFile()) < 100) { while (!_initialized) { System.Threading.Thread.Sleep(1000); } System.Threading.Thread.Sleep(2000); this.BeginInvoke((MethodInvoker) delegate { UVA_Arena.Internet.Downloader.DownloadProblemDatabase(); }); } //update user submissions if not available if (LocalDatabase.ContainsUser(RegistryAccess.DefaultUsername)) { string file = LocalDirectory.GetUserSubPath(RegistryAccess.DefaultUsername); if (LocalDirectory.GetFileSize(file) < 50) { System.Threading.Thread.Sleep(1000); this.BeginInvoke((MethodInvoker) delegate { Interactivity.userstat.DownloadUserSubs(RegistryAccess.DefaultUsername); }); } } //check for updates System.Threading.Thread.Sleep(10000); UpdateCheck.CheckForUpdate(); }