Guest User

Sourcecode MainWindow

a guest
Dec 15th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.IO;
  16. using System.Net;
  17. using System.Xml;
  18.  
  19. namespace HackerplaceWidget
  20. {
  21.     /// <summary>
  22.     /// Interaktionslogik für MainWindow.xaml
  23.     /// </summary>
  24.     public partial class MainWindow : Window
  25.     {
  26.         public MainWindow()
  27.         {
  28.             InitializeComponent();
  29.  
  30.             trayicon.BalloonTipText = "Das Widget wurde minimiert. Klicke hier um es wie anzuzeigen.";
  31.             trayicon.BalloonTipTitle = "Hackerplace-Widget";
  32.             trayicon.Text = "Hackerplace-Widget";
  33.             trayicon.Icon = Properties.Resources.favicon;
  34.             trayicon.Click += trayicon_Click;
  35.  
  36.             System.Threading.Thread t = new System.Threading.Thread(Start);
  37.             t.Start();
  38.         }
  39.  
  40.         void trayicon_Click(object sender, EventArgs e)
  41.         {
  42.             this.Show();
  43.             window_state = WindowState;
  44.         }
  45.  
  46.         System.Windows.Forms.NotifyIcon trayicon = new System.Windows.Forms.NotifyIcon();
  47.         bool run = false;
  48.         System.Timers.Timer tim;
  49.         WindowState window_state = WindowState.Normal;
  50.  
  51.         private void Start()
  52.         {
  53.                 tim = new System.Timers.Timer(HackerplaceWidget.Properties.Settings.Default.refreshinterval);
  54.                 tim.AutoReset = false;
  55.                 tim.Elapsed += t_Elapsed;
  56.                 tim.Start();
  57.         }
  58.  
  59.         void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  60.         {
  61.             tim.Stop();
  62.             PerformRun();
  63.             tim.Start();
  64.         }
  65.  
  66.         private void PerformRun()
  67.         {
  68.             if (run)
  69.             {
  70.                 if (FetchData() == false)
  71.                 {
  72.                     MessageBox.Show("Es ist ein Fehler aufgetreten. Bitte überprüfe deine Logindaten!");
  73.                     ShowSettings();
  74.                 }
  75.             }
  76.         }
  77.  
  78.         private void ShowSettings()
  79.         {
  80.             SettingsWindow sw = new SettingsWindow();
  81.             sw.ShowDialog();
  82.             tim.Interval = HackerplaceWidget.Properties.Settings.Default.refreshinterval;
  83.             Topmost = Properties.Settings.Default.show_top;
  84.         }
  85.  
  86.         private bool FetchData()
  87.         {
  88.             try
  89.             {
  90.                 StringBuilder stb = new StringBuilder();
  91.                 stb.Append("http://hackerplace.de/gadget/gadget1_0.php");
  92.                 stb.Append("?username=");
  93.                 stb.Append(HackerplaceWidget.Properties.Settings.Default.username_md5);
  94.                 stb.Append("&password=");
  95.                 stb.Append(HackerplaceWidget.Properties.Settings.Default.password_and_pwstring_md5);
  96.                 stb.Append("&temp=");
  97.                 stb.Append(DateTime.Now.Millisecond);
  98.  
  99.                 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(stb.ToString());
  100.                 req.Method = WebRequestMethods.Http.Get;
  101.                 req.UserAgent = "HPWidget by Sarius Crawler";
  102.  
  103.                 HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  104.                 StreamReader str = new StreamReader(resp.GetResponseStream());
  105.  
  106.                 string resp_string = str.ReadToEnd();
  107.  
  108.                 str.Close();
  109.                 req = null;
  110.                 resp = null;
  111.                 stb = null;
  112.  
  113.                 return ParseData(resp_string);
  114.             }
  115.             catch(TimeoutException te)
  116.             {
  117.                 return false;
  118.             }
  119.             catch
  120.             {
  121.                 return false;
  122.             }
  123.         }
  124.  
  125.         private bool ParseData(string resp_string)
  126.         {
  127.             try
  128.             {
  129.                 XmlDocument xdoc = new XmlDocument();
  130.                 xdoc.LoadXml(resp_string);
  131.  
  132.                 Dictionary<string, string> basic_informations = new Dictionary<string, string>();
  133.                 Dictionary<string, string> clan_informations = new Dictionary<string, string>();
  134.                 Dictionary<string, float> stock_informations = new Dictionary<string, float>();
  135.  
  136.                 basic_informations.Add("Username", xdoc.GetElementsByTagName("username")[0].InnerText);
  137.                 basic_informations.Add("Place", xdoc.GetElementsByTagName("accountplace")[0].InnerText);
  138.                 basic_informations.Add("OldPlace", xdoc.GetElementsByTagName("accountoldplace")[0].InnerText);
  139.                 basic_informations.Add("Score", xdoc.GetElementsByTagName("accountscore")[0].InnerText);
  140.                 basic_informations.Add("MessageCount", xdoc.GetElementsByTagName("accountmessages")[0].InnerText);
  141.  
  142.                 clan_informations.Add("ClanName", xdoc.GetElementsByTagName("clanname")[0].InnerText);
  143.                 clan_informations.Add("ClanPlace", xdoc.GetElementsByTagName("clanplace")[0].InnerText);
  144.                 clan_informations.Add("ClanScore", xdoc.GetElementsByTagName("clanscore")[0].InnerText);
  145.                 clan_informations.Add("ClanMembers", xdoc.GetElementsByTagName("clanusers")[0].InnerText);
  146.  
  147.                 stock_informations.Add("StockPrice", Convert.ToInt32(xdoc.GetElementsByTagName("accountboersemoney")[0].InnerText.Replace("." , "")));
  148.                 stock_informations.Add("StockChange", Convert.ToSingle(xdoc.GetElementsByTagName("accountboerse")[0].InnerText.Replace("+" , "")));
  149.  
  150.                 SetData(basic_informations, clan_informations, stock_informations);
  151.             }
  152.             catch
  153.             {
  154.                 return false;
  155.             }
  156.             return true;
  157.         }
  158.  
  159.         private void SetData(Dictionary<string, string> basic_informations, Dictionary<string, string> clan_informations, Dictionary<string, float> stock_informations)
  160.         {
  161.             this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(delegate() {
  162.                 username_lbl.Content = basic_informations["Username"];
  163.                 place_value_lbl.Content = basic_informations["Place"] + " / " + basic_informations["OldPlace"];
  164.                 score_value_lbl.Content = basic_informations["Score"];
  165.                 messages_value_lbl.Content = basic_informations["MessageCount"];
  166.  
  167.                 clan_name_value_lbl.Content = clan_informations["ClanName"];
  168.                 clan_place_value_lbl.Content = clan_informations["ClanPlace"];
  169.                 clan_score_value_lbl.Content = clan_informations["ClanScore"];
  170.                 clan_members_value_lbl.Content = clan_informations["ClanMembers"];
  171.  
  172.                 stock_price_value_lbl.Content = stock_informations["StockPrice"].ToString();
  173.                 stock_change_value_lbl.Content = stock_informations["StockChange"].ToString();
  174.             }));  
  175.  
  176.             CheckNotify(basic_informations, clan_informations, stock_informations);
  177.         }
  178.  
  179.         private void CheckNotify(Dictionary<string, string> basic_informations, Dictionary<string, string> clan_informations, Dictionary<string, float> stock_informations)
  180.         {
  181.             if (HackerplaceWidget.Properties.Settings.Default.notify_below_percent_stock && HackerplaceWidget.Properties.Settings.Default.notify_below_percent_stock_value <= stock_informations["StockChange"])
  182.             {
  183.                 MessageBox.Show("Die Börse hat sich um " + stock_informations["StockChange"].ToString() + "% verändert.");
  184.             }
  185.             if (HackerplaceWidget.Properties.Settings.Default.notify_over_percent_stock && HackerplaceWidget.Properties.Settings.Default.notify_over_percent_stock_value >= stock_informations["StockChange"])
  186.             {
  187.                 MessageBox.Show("Die Börse hat sich um " + stock_informations["StockChange"].ToString() + "% verändert.");
  188.             }
  189.             if (HackerplaceWidget.Properties.Settings.Default.notify_below_stock && HackerplaceWidget.Properties.Settings.Default.notify_below_stock_value > stock_informations["StockPrice"])
  190.             {
  191.                 MessageBox.Show("Die Börse ist bei " + stock_informations["StockPrice"].ToString() + " Credits/Aktie.");
  192.             }
  193.             if (HackerplaceWidget.Properties.Settings.Default.notify_over_stock && HackerplaceWidget.Properties.Settings.Default.notify_over_stock_value < stock_informations["StockPrice"])
  194.             {
  195.                 MessageBox.Show("Die Börse ist bei " + stock_informations["StockPrice"].ToString() + " Credits/Aktie.");
  196.             }
  197.             if (HackerplaceWidget.Properties.Settings.Default.notify_new_message && 0 < Convert.ToInt32(basic_informations["MessageCount"]))
  198.             {
  199.                 MessageBox.Show("Du hast " + basic_informations["MessageCount"] + " neue Nachricht(en).");
  200.             }
  201.         }
  202.  
  203.         private void open_settings_btn_Click(object sender, RoutedEventArgs e)
  204.         {
  205.             ShowSettings();
  206.         }
  207.  
  208.         private void start_pause_btn_Click(object sender, RoutedEventArgs e)
  209.         {
  210.             run = !run;
  211.             System.Threading.Thread t = new System.Threading.Thread(PerformRun);
  212.             t.Start();
  213.         }
  214.  
  215.         private void Window_Closed_1(object sender, EventArgs e)
  216.         {
  217.             trayicon.Dispose();
  218.             trayicon = null;
  219.             Environment.Exit(0);
  220.         }
  221.  
  222.         private void Window_IsVisibleChanged_1(object sender, DependencyPropertyChangedEventArgs e)
  223.         {
  224.             ShowTrayIcon(!IsVisible);
  225.         }
  226.  
  227.         private void Window_StateChanged_1(object sender, EventArgs e)
  228.         {
  229.             if (WindowState == WindowState.Minimized)
  230.             {
  231.                 Hide();
  232.                 if (trayicon != null)
  233.                     trayicon.ShowBalloonTip(10000);
  234.             }
  235.             else
  236.                 window_state = WindowState;
  237.         }
  238.         void ShowTrayIcon(bool show)
  239.         {
  240.             if (trayicon != null)
  241.                 trayicon.Visible = show;
  242.         }
  243.  
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment