Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: C#  |  size: 15.82 KB  |  hits: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6. namespace Blurum
  7. {
  8.     /// <summary>
  9.     ///
  10.     /// </summary>
  11.     [Serializable]
  12.     public partial class BlurumCatalogue : UserControl
  13.     {
  14.         private readonly Timer _timer;
  15.         /// <summary>
  16.         ///
  17.         /// </summary>
  18.         public event ProductAddedHandler ProductAdded;
  19.         /// <summary>
  20.         ///
  21.         /// </summary>
  22.         /// <param name="catalogue"></param>
  23.         /// <param name="p"></param>
  24.         public delegate void ProductAddedHandler(BlurumCatalogue catalogue,Product p);
  25.         /// <summary>
  26.         ///
  27.         /// </summary>
  28.         public event ProductAddedToWishListHandler ProductAddedToWishList;
  29.         /// <summary>
  30.         ///
  31.         /// </summary>
  32.         /// <param name="catalogue"></param>
  33.         /// <param name="p"></param>
  34.         public delegate void ProductAddedToWishListHandler(BlurumCatalogue catalogue, Product p);
  35.         /// <summary>
  36.         ///
  37.         /// </summary>
  38.         public event ProductInWishListAvailableHandler ProductInWishListAvailable;
  39.         /// <summary>
  40.         ///
  41.         /// </summary>
  42.         /// <param name="catalogue"></param>
  43.         /// <param name="p"></param>
  44.         public delegate void ProductInWishListAvailableHandler(BlurumCatalogue catalogue, Product p);
  45.  
  46.         readonly private WebBrowser _web = new WebBrowser();
  47.         /// <summary>
  48.         /// WishList con i prodotti caricati in memoria.
  49.         /// </summary>
  50.         public List<Product> LoadedProducts = new List<Product>();
  51.         /// <summary>
  52.         /// WishList con i prodotti.
  53.         /// </summary>
  54.         public List<Product> WishList = new List<Product>();
  55.         private const string Link = @"https://www.blurum.it/Web/PointBox.aspx";
  56.         /// <summary>
  57.         /// Controlla se il webbrowser interno sta ancora caricando oppure no.
  58.         /// </summary>
  59.         public bool IsLoading { get {return _web.ReadyState!=WebBrowserReadyState.Complete; }  }
  60.         /// <summary>
  61.         /// Ritorna true se il timer della wishlist è attivo, false altrimenti.
  62.         /// </summary>
  63.         public bool IsCheckingWishList { get; set; }
  64.         /// <summary>
  65.         /// Costruttore principale della classe.
  66.         /// </summary>
  67.         public BlurumCatalogue()
  68.         {
  69.             InitializeComponent();
  70.             _web.ScriptErrorsSuppressed = true;
  71.             _web.Visible = false;
  72.             treeCatalogo.AfterSelect += AfterSelect;
  73.             treeCatalogo.BeforeExpand += BeforeExpand;
  74.             _timer = new Timer {Interval = 5000};
  75.             _timer.Tick += CheckWishListTick;
  76.         }
  77.         /// <summary>
  78.         /// Metodo principale che carica le categorie principali.
  79.         /// </summary>
  80.         public void LoadMainCategories()
  81.         {
  82.             //IsLoading = true;
  83.             treeCatalogo.Nodes.Clear();
  84.             _web.Navigate(Link);
  85.             while (_web.ReadyState != WebBrowserReadyState.Complete)
  86.             {
  87.                 Application.DoEvents();
  88.             }
  89.             var elements = _web.Document.GetElementsByTagName("li");
  90.             foreach (HtmlElement elem in elements)
  91.             {
  92.                 foreach (HtmlElement el2 in elem.GetElementsByTagName("a"))
  93.                 {
  94.                     if (el2.GetAttribute("href").Contains("catalog"))
  95.                     {
  96.                         TreeNode radice = new TreeNode(elem.GetElementsByTagName("img")[0].GetAttribute("alt"));
  97.                         radice.ToolTipText = el2.GetAttribute("href");
  98.                         radice.Tag = el2.GetAttribute("href");
  99.                         radice.Nodes.Add("cacca");
  100.                         treeCatalogo.Nodes.Add(radice);
  101.                     }
  102.                 }
  103.             }
  104.             //IsLoading = false;
  105.         }
  106.         /// <summary>
  107.         /// Setta l'intervallo del timer interno.
  108.         /// </summary>
  109.         /// <param name="ms">Tempo in millisecondi per l'intervallo del timer (consiglio di moltiplicare il numero di prodotti nella wishlist per 5)</param>
  110.         public void SetTimerIntervalForWishListCheck(int ms)
  111.         {
  112.             _timer.Interval = ms;
  113.         }
  114.         /// <summary>
  115.         /// Fa partire il timer interno per controllare se i prodotti nella wishlist sono disponibili.
  116.         /// </summary>
  117.         public void BeginCheckingWishList()
  118.         {
  119.             _timer.Start();
  120.             IsCheckingWishList = true;
  121.         }
  122.         /// <summary>
  123.         /// Ferma il timer interno che controlla se i prodotti nella wishlist sono disponibili.
  124.         /// </summary>
  125.         public void StopCheckingWishList()
  126.         {
  127.             _timer.Stop();
  128.             IsCheckingWishList = false;
  129.         }
  130.  
  131.  
  132.         private void BeforeExpand(object sender, TreeViewCancelEventArgs e)
  133.         {
  134.             TreeNode nodo = e.Node;
  135.             if (nodo.Nodes.Count == 1 && nodo.Nodes[0].Text == @"cacca")
  136.                 nodo.Nodes.RemoveAt(0);
  137.             if (nodo.Nodes.Count == 0)
  138.             {
  139.                 switch (nodo.Level)
  140.                 {
  141.                     case 0:
  142.                         //IsLoading = true;
  143.                         _web.Navigate(nodo.ToolTipText);
  144.                         while (_web.ReadyState != WebBrowserReadyState.Complete)
  145.                         {
  146.                             Application.DoEvents();
  147.                         }
  148.                         var elements = _web.Document.GetElementsByTagName("ul");
  149.                         foreach (HtmlElement element in elements)
  150.                         {
  151.  
  152.                             if (element.GetAttribute("className") == "listCompVertical catalogSectors")
  153.                             {
  154.                                 var elem2 = element.GetElementsByTagName("li");
  155.                                 foreach (HtmlElement el in elem2)
  156.                                 {
  157.                                     Application.DoEvents();
  158.                                     TreeNode newNodo = new TreeNode(el.GetElementsByTagName("a")[0].InnerHtml.Replace("&nbsp;", ""));
  159.                                     newNodo.ToolTipText = el.GetElementsByTagName("a")[0].GetAttribute("href");
  160.                                     newNodo.Tag = el.GetElementsByTagName("a")[0].GetAttribute("href");
  161.                                     //MessageBox.Show(newNodo.ToolTipText);
  162.                                     newNodo.Nodes.Add("cacca");
  163.                                     nodo.Nodes.Add(newNodo);
  164.                                 }
  165.                             }
  166.                         }
  167.                         //IsLoading = false;
  168.                         break;
  169.                     case 1: //sottocategoria
  170.                         //IsLoading = true;
  171.                         _web.Navigate(nodo.ToolTipText);
  172.                         while (_web.ReadyState != WebBrowserReadyState.Complete)
  173.                         {
  174.                             Application.DoEvents();
  175.                         }
  176.                         var elem = _web.Document.GetElementById("lev-2-0");
  177.                         foreach (HtmlElement listElement in elem.GetElementsByTagName("li"))
  178.                         {
  179.                             Application.DoEvents();
  180.                             TreeNode newNodo = new TreeNode(listElement.GetElementsByTagName("a")[0].InnerHtml.Replace("&nbsp;", ""));
  181.                             newNodo.ToolTipText = listElement.GetElementsByTagName("a")[0].GetAttribute("href").Replace("&page=1", "");
  182.                             newNodo.Tag = listElement.GetElementsByTagName("a")[0].GetAttribute("href").Replace("&page=1", "");
  183.                             newNodo.Nodes.Add("cacca");
  184.                             //MessageBox.Show(newNodo.ToolTipText);
  185.                             nodo.Nodes.Add(newNodo);
  186.                         }
  187.                         //IsLoading = false;
  188.                         break;
  189.                     case 2:
  190.                         string notFound = "La ricerca non ha prodotto risultati";
  191.                         int i = 1;
  192.                         while (true)
  193.                         {
  194.                             //IsLoading = true;
  195.                             _web.Navigate(nodo.ToolTipText + "&page=" + i);
  196.                             while (_web.ReadyState != WebBrowserReadyState.Complete)
  197.                             {
  198.                                 Application.DoEvents();
  199.                             }
  200.  
  201.                             if (_web.DocumentText.Contains(notFound)) break;
  202.  
  203.                             var elementi = _web.Document.GetElementsByTagName("ul");
  204.                             foreach (HtmlElement element in elementi)
  205.                             {
  206.                                 if (element.GetAttribute("className") == "compListHorizontal productSampler")
  207.                                 {
  208.                                     var elem2 = element.GetElementsByTagName("li");
  209.                                     foreach (HtmlElement el in elem2)
  210.                                     {
  211.                                         Application.DoEvents();
  212.                                         TreeNode newNodo = new TreeNode(el.GetElementsByTagName("a")[0].InnerHtml.Replace("&nbsp;", "")
  213.                                             + " - " + el.GetElementsByTagName("p")[0].InnerText);
  214.                                         newNodo.ToolTipText = el.GetElementsByTagName("a")[0].GetAttribute("href");
  215.                                         newNodo.Tag = el.GetElementsByTagName("a")[0].GetAttribute("href");
  216.                                         //MessageBox.Show(newNodo.ToolTipText);
  217.                                         nodo.Nodes.Add(newNodo);
  218.                                     }
  219.                                 }
  220.                             }
  221.                             i++;
  222.                             //IsLoading = false;
  223.                         }
  224.                         break;
  225.                 }
  226.             }
  227.         }
  228.         private void AfterSelect(object sender, TreeViewEventArgs e)
  229.         {
  230.             TreeNode nodo = e.Node;
  231.             if (nodo.Level == 3 && LoadedProducts.Where(p => p.Link == nodo.ToolTipText).ToList().Count == 0)
  232.             {
  233.                 Product p = new Product();
  234.                 p.Name = nodo.Text.Substring(0, nodo.Text.LastIndexOf('-') - 1);
  235.                 p.Value = nodo.Text.Substring(nodo.Text.LastIndexOf('-') + 1);
  236.                 p.Link = nodo.ToolTipText;
  237.                 //IsLoading = true;
  238.                 _web.Navigate(nodo.ToolTipText);
  239.                 while (_web.ReadyState != WebBrowserReadyState.Complete)
  240.                 {
  241.                     Application.DoEvents();
  242.                 }
  243.                
  244.                 var x = _web.Document.GetElementsByTagName("span");
  245.                 foreach (HtmlElement el in x)
  246.                 {
  247.                     if (el.Id == "availabilityValue")
  248.                     {
  249.                         //p.IsAvailable = el.InnerText != "in riassortimento";
  250.                         p.IsAvailable = !_web.DocumentText.Contains("in riassortimento");
  251.                     }
  252.                 }
  253.                 x = _web.Document.GetElementsByTagName("div");
  254.                 foreach (HtmlElement el in x)
  255.                 {
  256.                     if (el.GetAttribute("className") == "description")
  257.                     {
  258.                         p.Description = el.InnerText;
  259.                     }
  260.                 }
  261.                 //IsLoading = false;
  262.                 nodo.ContextMenuStrip = contextMenu;
  263.                 LoadedProducts.Add(p);
  264.                 if (ProductAdded != null)
  265.                     ProductAdded(this, p);
  266.             }
  267.         }
  268.         private void load(object sender, EventArgs e)
  269.         {
  270.             LoadMainCategories();
  271.         }
  272.         private void AddToWishListClick(object sender, EventArgs e)
  273.         {
  274.             if (LoadedProducts != null)
  275.             {
  276.                 Product x = LoadedProducts.Where(p => p.Link == treeCatalogo.SelectedNode.ToolTipText).ToList()[0];
  277.                 WishList.Add(x);
  278.                 if (ProductAddedToWishList != null)
  279.                     ProductAddedToWishList(this, x);
  280.             }
  281.         }
  282.         private void CheckWishListTick(object sender, EventArgs e)
  283.         {
  284.             for (int i = 0; i < WishList.Count; i++)
  285.             {
  286.                 if (!IsLoading)
  287.                 {
  288.                     //IsLoading = true;
  289.                     _web.Navigate(WishList[i].Link);
  290.                     while (_web.ReadyState != WebBrowserReadyState.Complete)
  291.                     {
  292.                         Application.DoEvents();
  293.                     }
  294.                     var x = _web.Document.GetElementsByTagName("span");
  295.                     foreach (HtmlElement el in x)
  296.                     {
  297.                         if (el.Id == "availabilityValue")
  298.                         {
  299.                             WishList[i].IsAvailable = !_web.DocumentText.Contains("in riassortimento");
  300.                         }
  301.                     }
  302.                     if (WishList[i].IsAvailable && ProductInWishListAvailable != null) ProductInWishListAvailable(this, WishList[i]);
  303.                 }
  304.             }
  305.         }
  306.     }
  307.     /// <summary>
  308.     /// Classe che rappresenta un prodotto di blurum.
  309.     /// </summary>
  310.     public class Product
  311.     {
  312.         private string _valore;
  313.         private string _nome;
  314.         private bool _isAvailable;
  315.         private string _description;
  316.         private string _link;
  317.         /// <summary>
  318.         /// Costruttore per un prodotto generico.
  319.         /// </summary>
  320.         /// <param name="nome">Nome del prodotto.</param>
  321.         /// <param name="isAvailable">Disponibilità del prodotto (true o false).</param>
  322.         /// <param name="description">Descrizione del prodotto.</param>
  323.         /// <param name="valore">Valore (in punti) del prodotto.</param>
  324.         /// <param name="link">Link al sito di blurum del prodotto.</param>
  325.         public Product(string nome, bool isAvailable, string description, string valore, string link)
  326.         {
  327.             _nome = nome;
  328.             _isAvailable = isAvailable;
  329.             _description = description;
  330.             _valore = valore;
  331.             _link = link;
  332.         }
  333.         /// <summary>
  334.         /// Costruttore di default.
  335.         /// </summary>
  336.         public Product()
  337.         {
  338.         }
  339.  
  340.         /// <summary>
  341.         /// Valore del prodotto in punti.
  342.         /// </summary>
  343.         public string Value
  344.         {
  345.             get { return _valore; }
  346.             set { _valore = value; }
  347.         }
  348.         /// <summary>
  349.         /// Nome del prodotto.
  350.         /// </summary>
  351.         public string Name
  352.         {
  353.             get { return _nome; }
  354.             set { _nome = value; }
  355.         }
  356.         /// <summary>
  357.         /// Indica se un prodotto è disponibile o no.
  358.         /// </summary>
  359.         public bool IsAvailable
  360.         {
  361.             get { return _isAvailable; }
  362.             set { _isAvailable = value; }
  363.         }
  364.         /// <summary>
  365.         /// Descrizione del prodotto.
  366.         /// </summary>
  367.         public string Description
  368.         {
  369.             get { return _description; }
  370.             set { _description = value; }
  371.         }
  372.         /// <summary>
  373.         /// Link al sito di blurum del prodotto.
  374.         /// </summary>
  375.         public string Link
  376.         {
  377.             get { return _link; }
  378.             set { _link = value; }
  379.         }
  380.         /// <summary>
  381.         ///
  382.         /// </summary>
  383.         /// <returns></returns>
  384.         public override string ToString()
  385.         {
  386.             return Name + "\n" + "Value: " + Value + "\nIs Available :" + _isAvailable.ToString() + "\nDescription: " + Description;
  387.         }
  388.     }
  389. }