// Scan files and call for icons to be generated public void generateFromFolder(MainDisplay me, bool onlyDisplayIfDownloaded) { this.quickpics.Clear(); this.fileNames.Clear(); if (me.syncbutton.InvokeRequired) { me.syncbutton.BeginInvoke((MethodInvoker) delegate() { me.syncbutton.Enabled = false; }); } else { me.syncbutton.Enabled = false; } DirectoryInfo d = new DirectoryInfo(Properties.Settings.Default.runpath); FileInfo[] Files = d.GetFiles(); Array.Sort(Files, delegate(FileInfo x, FileInfo y) { return(x.Name.Substring(0, x.Name.LastIndexOf(".")).CompareTo(y.Name.Substring(0, y.Name.LastIndexOf(".")))); }); Array.Reverse(Files); foreach (FileInfo file in Files) { me.AggiornaContatore2(Files.Count()); generateQuickPics(file, onlyDisplayIfDownloaded); } this.generateTable(me); }
// Add new lables to display public void generateTable(MainDisplay me) { int cols = this.paneltable.Width / 110; int offset = this.paneltable.Width - (110 * cols); int spacing = offset / (cols + 1); this.paneltable.Margin = new System.Windows.Forms.Padding(spacing, 0, spacing, 0); this.paneltable.RowCount = (int)Math.Ceiling((double)quickpics.Count / (double)cols); this.paneltable.ColumnCount = cols; for (int i = 0; i < cols; i++) { this.paneltable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 110)); } for (int i = 0; i < this.paneltable.RowCount; i++) { this.paneltable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 140)); } int counter = 0; for (int ai = 0; ai < this.paneltable.RowCount; ai++) { for (int i = 0; i < cols; i++) { if (counter < quickpics.Count) { Label toAdd = new Label(); toAdd.BackColor = Color.Transparent; toAdd.Parent = quickpics.ElementAt(counter); toAdd.Text = fileNames.ElementAt(counter); toAdd.AutoSize = false; toAdd.TextAlign = ContentAlignment.MiddleCenter; toAdd.Height = 15; toAdd.Location = new Point(0, 95); me.AggiornaContatore(quickpics.ElementAt(counter), i, ai); counter = counter + 1; } } } if (me.syncbutton.InvokeRequired) { me.syncbutton.BeginInvoke((MethodInvoker) delegate() { me.syncbutton.Enabled = true; me.syncbutton.BackgroundImage = Properties.Resources.sync; }); } else { me.syncbutton.Enabled = true; me.syncbutton.BackgroundImage = Properties.Resources.sync; } }
// Used to download multiple files in list. Useful for threading (IE thread1 should download items 1-3, thread2 should download items 4-6, etc.) private static void downloadPattern(List <Uri> filesFromServer, int startIndex, int endIndex, CustomProgressBar bar, GridMaker displayController, MainDisplay me) { if (STOP) { return; } for (int i = startIndex; i < endIndex; i = i + 1) { if (i < filesFromServer.Count) { String file = filesFromServer.ElementAt(i).ToString(); String fileExtension = file.Substring(file.LastIndexOf("/") + 1); try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(filesFromServer.ElementAt(i)); req.Timeout = 120000; req.ReadWriteTimeout = 120000; var w = (HttpWebResponse)req.GetResponse(); using (Stream filer = File.OpenWrite(Properties.Settings.Default.runpath + "\\" + fileExtension)) { w.GetResponseStream().CopyTo(filer); } } catch (System.Net.WebException e) { MessageBox.Show("Please check your internet connection. Heres what we know:\n" + e.Message, "An error was incurred", MessageBoxButtons.OK, MessageBoxIcon.Error); } Debug.WriteLine("file"); if (STOP) { FileInfo tfile = new FileInfo(Properties.Settings.Default.runpath + "\\" + fileExtension); Debug.WriteLine(tfile.Exists); tfile.Delete(); return; } if (bar.InvokeRequired) { bar.BeginInvoke((MethodInvoker) delegate() { bar.Step = 1; }); bar.BeginInvoke((MethodInvoker) delegate() { bar.PerformStep(); }); } else { bar.BeginInvoke((MethodInvoker) delegate() { bar.Step = 1; }); bar.BeginInvoke((MethodInvoker) delegate() { bar.PerformStep(); }); } if (bar.Value + 1 >= bar.Maximum) { finishFile(bar, displayController, me, false); } } } }
// Fired when sync ends... either when complete or canceled public static void finishFile(CustomProgressBar bar, GridMaker displayController, MainDisplay me, bool onlyDisplayDownloaded) { Debug.WriteLine("done syncing!"); me.cancel = false; System.Threading.Thread.Sleep(1000); if (bar.InvokeRequired) { bar.BeginInvoke((MethodInvoker) delegate() { bar.Value = 0; }); bar.BeginInvoke((MethodInvoker) delegate() { bar.UpdateText(); }); bar.BeginInvoke((MethodInvoker) delegate() { bar.Refresh(); }); bar.BeginInvoke((MethodInvoker) delegate() { bar.UpdateText(); }); } else { bar.Value = 0; bar.UpdateText(); bar.Refresh(); bar.UpdateText(); } displayController.generateFromFolder(me, onlyDisplayDownloaded); isSyncing = false; }
// Delete all other files, split up downloads into new threads and start them public static void sync(CustomProgressBar progbar, GridMaker displayController, MainDisplay me) { if (STOP || isSyncing) { return; } isSyncing = true; try { DirectoryInfo di = new DirectoryInfo(Properties.Settings.Default.runpath); foreach (FileInfo file in di.GetFiles()) { file.Delete(); } foreach (DirectoryInfo dir in di.GetDirectories()) { dir.Delete(true); } } catch (IOException) { Thread.CurrentThread.Abort(); } AisUriProvider api = new AisUriProvider(); List <Uri> filesFromServer = api.Get().ToList(); if (progbar.InvokeRequired) { progbar.BeginInvoke((MethodInvoker) delegate() { progbar.Maximum = filesFromServer.Count(); }); } else { progbar.Maximum = filesFromServer.Count(); } for (int i = 0; i < filesFromServer.Count; i = i + (filesFromServer.Count / Properties.Settings.Default.n)) { int lowerBound = i; int upperBound = i + (filesFromServer.Count / Properties.Settings.Default.n); Thread t3 = new Thread(() => downloadPattern(filesFromServer, lowerBound, upperBound, progbar, displayController, me)); t3.Start(); } }
private void mainForm_FormClosing(object sender, System.ComponentModel.CancelEventArgs e, MainDisplay me) { if (me.cancel == true) { if (MessageBox.Show("A sync is in progress. Are you sure you would like to exit?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes) { e.Cancel = false; this.Activate(); } else { e.Cancel = true; this.Activate(); } } else { e.Cancel = false; this.Activate(); } }