private void button11_Click(object sender, EventArgs e) { if (panel1.Enabled) { string message = ""; _cancel = false; BrowserScriptPocketQueriesPQSeries pqs = comboBox1.SelectedItem as BrowserScriptPocketQueriesPQSeries; BrowserScriptPocketQueriesPQSetting setting = comboBox2.SelectedItem as BrowserScriptPocketQueriesPQSetting; if (pqs != null && setting != null) { _init = true; listView1.Items.Clear(); pqs.Dates.Clear(); pqs.Dates.Add(dateTimePicker1.Value < dateTimePicker2.Value ? dateTimePicker1.Value : dateTimePicker2.Value); pqs.Dates.Add(dateTimePicker2.Value > dateTimePicker1.Value ? dateTimePicker2.Value : dateTimePicker1.Value); this.button5.Text = Utils.LanguageSupport.Instance.GetTranslation(STR_CANCEL); panel1.Enabled = false; this.ControlBox = false; _browserReady = new ManualResetEvent(false); _wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(_wb_DocumentCompleted); try { string baseUrl = string.Format("http://{0}", _wb.Url.Host); if (setToEnglish()) { if (gotoPage(string.Format("{0}/pocket/default.aspx", baseUrl))) { _wb.Document.GetElementById("uxCreateNewPQ").SetAttribute("Target", "_self"); _browserReady.Reset(); _wb.Document.GetElementById("uxCreateNewPQ").InvokeMember("Click"); if (waitForPageReady(string.Format("{0}/pocket/gcquery.aspx", baseUrl))) { //now, use this page to get the ranges int maxInPq = (int)numericUpDown1.Value; int maxInLastPq = (int)numericUpDown2.Value; int margin = (int)numericUpDown3.Value; DateTime lastPqEnd = pqs.Dates[1]; DateTime minOfInterval = pqs.Dates[0]; DateTime maxOfInterval = pqs.Dates[1]; DateTime lastAcceptableDate = DateTime.MinValue; //with every pq, start with begin date (is begin or end date of prev pq //and have end date //devide number of days by 2 and add to begin date if too many //until days=0 or number in pq < desired setting.PlacedDuring = "ctl00_ContentBody_rbPlacedBetween"; setting.Name = "_gcpqgen_ranges_"; setting.PlacedDuringFromDate = pqs.Dates[0]; setting.PlacedDuringToDate = pqs.Dates[1]; while (!_cancel && string.IsNullOrEmpty(message)) { setting.SetPQPage(_wb); _browserReady.Reset(); _wb.Document.GetElementById("ctl00_ContentBody_btnSubmit").InvokeMember("Click"); if (waitForPageReady(string.Format("{0}/pocket/gcquery.aspx", baseUrl))) { Thread.Sleep((int)numericUpDown4.Value); //check result int count = getNumberOfCachesInPQ(); if (count >= 0) { _core.DebugLog(Framework.Data.DebugLogLevel.Info, _plugin, null, string.Format("PQ => {0} - {1} ({2})", setting.PlacedDuringFromDate.ToString("d"), setting.PlacedDuringToDate.ToString("d"), count)); if (count > maxInPq) { //go between min and current end maxOfInterval = setting.PlacedDuringToDate; int days = (int)(maxOfInterval - minOfInterval).TotalDays / 2; if (days < 2) { if (lastAcceptableDate >= setting.PlacedDuringFromDate) { //take this //ready for this one! pqs.Dates.Add(lastAcceptableDate); //update ui ListViewItem lvi = new ListViewItem(lastAcceptableDate.ToString("d")); lvi.Tag = lastAcceptableDate; listView1.Items.Add(lvi); setting.PlacedDuringFromDate = lastAcceptableDate.AddDays(1); setting.PlacedDuringToDate = lastPqEnd; lastAcceptableDate = DateTime.MinValue; } else { message = STR_ERROR; break; } } else { setting.PlacedDuringToDate = minOfInterval.AddDays(days); } } else if (setting.PlacedDuringToDate >= lastPqEnd) { //ready! break; } else if (count <= maxInPq && count >= (maxInPq - margin)) { //ready for this one! pqs.Dates.Add(setting.PlacedDuringToDate); //update ui ListViewItem lvi = new ListViewItem(setting.PlacedDuringToDate.ToString("d")); lvi.Tag = setting.PlacedDuringToDate; listView1.Items.Add(lvi); setting.PlacedDuringFromDate = setting.PlacedDuringToDate.AddDays(1); setting.PlacedDuringToDate = lastPqEnd; lastAcceptableDate = DateTime.MinValue; } else //count too small { if (setting.PlacedDuringToDate > lastAcceptableDate) { lastAcceptableDate = setting.PlacedDuringToDate; } minOfInterval = setting.PlacedDuringToDate; int days = (int)(maxOfInterval - minOfInterval).TotalDays / 2; if (days < 2) { //take this //ready for this one! pqs.Dates.Add(setting.PlacedDuringToDate); //update ui ListViewItem lvi = new ListViewItem(setting.PlacedDuringToDate.ToString("d")); lvi.Tag = setting.PlacedDuringToDate; listView1.Items.Add(lvi); setting.PlacedDuringFromDate = setting.PlacedDuringToDate.AddDays(1); setting.PlacedDuringToDate = lastPqEnd; lastAcceptableDate = DateTime.MinValue; } else { setting.PlacedDuringToDate = minOfInterval.AddDays(days); } } } else { message = STR_ERROR; break; } } } } else { message = STR_ERROR; } } else { message = STR_ERROR; } } else { message = STR_ERROR; } } catch { message = STR_ERROR; } _wb.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(_wb_DocumentCompleted); _browserReady.Dispose(); _browserReady = null; this.ControlBox = true; panel1.Enabled = true; _init = false; comboBox1_SelectedIndexChanged(this, EventArgs.Empty); this.button5.Text = Utils.LanguageSupport.Instance.GetTranslation(STR_CREATEPQ); if (!string.IsNullOrEmpty(message)) { MessageBox.Show(Utils.LanguageSupport.Instance.GetTranslation(message), Utils.LanguageSupport.Instance.GetTranslation(STR_ERROR)); } } } }
private void button5_Click(object sender, EventArgs e) { if (panel1.Enabled) { string message = ""; string inArea = ""; string msgid = ""; _cancel = false; BrowserScriptPocketQueriesPQSeries pqs = comboBox1.SelectedItem as BrowserScriptPocketQueriesPQSeries; BrowserScriptPocketQueriesPQSetting setting = comboBox2.SelectedItem as BrowserScriptPocketQueriesPQSetting; if (pqs != null && pqs.Dates.Count > 1 && setting != null) { this.button5.Text = Utils.LanguageSupport.Instance.GetTranslation(STR_CANCEL); panel1.Enabled = false; this.ControlBox = false; _browserReady = new ManualResetEvent(false); _wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(_wb_DocumentCompleted); try { string baseUrl = string.Format("http://{0}", _wb.Url.Host); pqs.Dates.Sort(); if (setToEnglish()) { int pqIndex = 0; int retryCount = 0; int activationDay = 0; while (pqIndex < pqs.Dates.Count - 1) { message = ""; msgid = ""; try { inArea = "1"; if (gotoPage(string.Format("{0}/pocket/default.aspx", baseUrl))) { inArea = "2"; _wb.Document.GetElementById("uxCreateNewPQ").SetAttribute("Target", "_self"); _browserReady.Reset(); _wb.Document.GetElementById("uxCreateNewPQ").InvokeMember("Click"); if (waitForPageReady(string.Format("{0}/pocket/gcquery.aspx", baseUrl))) { inArea = "3"; if (pqIndex == 0) { setting.PlacedDuringFromDate = pqs.Dates[pqIndex]; } else { setting.PlacedDuringFromDate = pqs.Dates[pqIndex].AddDays(1); } setting.PlacedDuringToDate = pqs.Dates[pqIndex + 1]; setting.PlacedDuring = "ctl00_ContentBody_rbPlacedBetween"; setting.Name = pqs.NameTemplate.Replace("<index>", pqIndex.ToString("00")).Replace("<begindate>", setting.PlacedDuringFromDate.ToString("yyyyMMdd")).Replace("<enddate>", setting.PlacedDuringToDate.ToString("yyyyMMdd")); inArea = "4"; if (checkBox1.Checked) { setting.SetPQPage(_wb, activationDay); } else { setting.SetPQPage(_wb); } inArea = "5"; _browserReady.Reset(); _wb.Document.GetElementById("ctl00_ContentBody_btnSubmit").InvokeMember("Click"); inArea = "6"; if (waitForPageReady(string.Format("{0}/pocket/gcquery.aspx", baseUrl))) { inArea = "7"; if (!_wb.Document.Body.InnerHtml.Contains("Thanks! Your pocket query has been saved and currently results in")) { message = STR_ERROR; msgid = "1"; } } else { message = STR_ERROR; msgid = "2"; } } else { message = STR_ERROR; msgid = "3"; } if (_cancel) { break; } } else { msgid = "4"; message = STR_ERROR; } } catch (Exception ex) { msgid = string.Format("6i - {0}", inArea); message = STR_ERROR; } if (!string.IsNullOrEmpty(message)) { if (retryCount < 6) { _core.DebugLog(Framework.Data.DebugLogLevel.Error, _plugin, null, string.Format("ERROR: Setting PQ settings - retry ({0})", retryCount)); //check if PQ has been created: if (gotoPage(string.Format("{0}/pocket/default.aspx", baseUrl))) { if (_wb.Document.Body.InnerHtml.IndexOf(setting.Name) > 0) { _core.DebugLog(Framework.Data.DebugLogLevel.Error, _plugin, null, "But was created"); //created, goto next retryCount = 0; pqIndex++; activationDay++; if (activationDay > 6) { activationDay = 0; } } else { _core.DebugLog(Framework.Data.DebugLogLevel.Error, _plugin, null, "Was not created"); retryCount++; } } else { break; } } else { break; } } else { activationDay++; if (activationDay > 6) { activationDay = 0; } retryCount = 0; pqIndex++; } } } else { msgid = "5"; message = STR_ERROR; } } catch (Exception ex) { msgid = string.Format("6 - {0}", inArea); message = STR_ERROR; } _wb.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(_wb_DocumentCompleted); _browserReady.Dispose(); _browserReady = null; this.ControlBox = true; panel1.Enabled = true; this.button5.Text = Utils.LanguageSupport.Instance.GetTranslation(STR_CREATEPQ); if (!string.IsNullOrEmpty(message)) { MessageBox.Show(string.Format("{0} ({1} - {2})", Utils.LanguageSupport.Instance.GetTranslation(message), msgid, inArea), Utils.LanguageSupport.Instance.GetTranslation(STR_ERROR)); } } } else { _cancel = true; } }