private bool closing = false; //track what is initiating the close (x'ing the form or shutdown) #endregion Fields #region Constructors public EnvatoTrackerFrame() { InitializeComponent(); User user = new User(); EnvatoAPI api = new EnvatoAPI(); //get ini config data, or create default file if not exists Config config = new Config(); if (config.initializeConfig() < 5) MessageBox.Show("Please verify you have set all configuration parameters and relaunch"); //load all window elements from config data usernameTextbox.Text = username; apikeyTextbox.Text = apiKey; emailTextbox.Text = emailTo; notifyIntervalCombo.Text = notifyInterval.ToString(); switch (notifyType) { case 1: notifyOptionsCombo.Text = "Email Only"; break; case 2: notifyOptionsCombo.Text = "Popup Only"; break; case 3: notifyOptionsCombo.Text = "Email & Popup"; break; default: notifyOptionsCombo.Text = "Popup Only"; break; } //remove from task bar on startup (should onlyl show in system tray) this.ShowInTaskbar = false; //display a popup that shows we're minimized and to make sure things are configured notifyIcon.ShowBalloonTip(18000000, "EnvatoTracker", "Remember to set your configuration information\nor you won't get any updates. (you should see your sales total if configured properly)", ToolTipIcon.None); //lets check if configured user is valid, if not we shouldn't do anything, otherwise lets initialize //set status (user is configured so we're polling)only if the username/key are successful if (user.isValidApiKey(username, apiKey)) { //get total sales int totalSales = api.getTotalSales(username); //update account balance & show balance if hover over tray icon accountBalance = api.getAccountBalance(username, apiKey, ref notifyIcon); if (accountBalance == "") accountBalance = "0.0"; notifyIcon.Text = "EnvatoTracker\nAccount Balance: $" + accountBalance + "\nTotal Sales: " + totalSales; //check if sales file exists, if it does we should compare the total to the number returned from the //api if (user.hasNewSales(username)) { notifyIcon.ShowBalloonTip(18000000, "New sales!", "You have received new sales since last time!\nAccount Balance:" + accountBalance, ToolTipIcon.None); //update the sales total in sales.txt user.setSalesTotal(totalSales); } appStatusValue.Text = "API polling active"; totalSalesLabelTextValue.Text = totalSales.ToString(); } else appStatusValue.Text = "API polling inactive"; //and finally we kick off the scheduler schedulerWorker.RunWorkerAsync(); }
// What to happen when the timer event is raised private void OnTimedEvent(object source, ElapsedEventArgs e) { /* * TASKS: * 1. Connect to api (if username/apikey are valid) * 2. Get total sales count * 3. Compare count to known sales count * 4. If new count is greater we should notify the user with details of the sales * (ie subtract new sales from known sales and get the last x amount of sales */ User user = new User(); EnvatoAPI api = new EnvatoAPI(); Notify notify = new Notify(); if (user.isValidApiKey(username, apiKey)) { if (user.hasNewSales(username)) { //check the notify type and send notification with info //we should find out the details of the new sales and either pop'm up or email em int newSalesTotal = api.getTotalSales(username); int currentSalesTotal = user.getCurrentKnownSales(); if (currentSalesTotal != -1) { int salesChange = newSalesTotal - currentSalesTotal; //update account balance accountBalance = api.getAccountBalance(username, apiKey, ref notifyIcon); switch (notifyType) { case 2: //popup only notifyIcon.ShowBalloonTip(18000000, "New sales!", "You have " + salesChange + " new sale(s)!\nAccount Balance:" + accountBalance, ToolTipIcon.None); break; case 1: //email only if (!notify.sendEmailNotify(salesChange, ref notifyIcon)) notifyIcon.ShowBalloonTip(18000000, "EnvatoTracker Error", "Failed to send email with new sales - verify your email config", ToolTipIcon.None); break; case 3: //popup and email if (!notify.sendEmailNotify(salesChange, ref notifyIcon)) notifyIcon.ShowBalloonTip(18000000, "EnvatoTracker Error", "Failed to send email with new sales - verify your email config", ToolTipIcon.None); notifyIcon.ShowBalloonTip(18000000, "New sales!", "You have " + salesChange + " new sale(s)!", ToolTipIcon.None); break; default: //default is just a popup notifyIcon.ShowBalloonTip(18000000, "New sales!", "You have " + salesChange + " new sale(s)!\nAccount Balance:" + accountBalance, ToolTipIcon.None); break; } //now update known sales total (to keep from sending out notifications over and over) user.setSalesTotal(newSalesTotal); //update the ui with new sales total totalSalesLabelTextValue.Invoke(new updateSalesTotalUICallback(this.updateSalesTotalUI), new object[] { newSalesTotal }); //update balloon hover text notifyIcon.Text = "EnvatoTracker\nAccount Balance: $" + accountBalance + "\nTotal Sales: " + newSalesTotal; //play the kaching sound - cause we're cool like that if (File.Exists(@wavFile)) { System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(); myPlayer.SoundLocation = @wavFile; myPlayer.Play(); } } else notifyIcon.ShowBalloonTip(18000000, "EnvatoTracker Error!", "Could not get sales change for some reason.", ToolTipIcon.None); } } }