Example #1
0
        public bool sendEmailNotify(int newSaleItemCount, ref NotifyIcon notifyIcon)
        {
            //get any recent sales (item name and sold date)
            EnvatoAPI api = new EnvatoAPI();
            ArrayList salesData = api.getNewSalesItemName(EnvatoTrackerFrame.username, EnvatoTrackerFrame.apiKey, newSaleItemCount, ref notifyIcon);
            if (salesData.Count <= 0)
                return false;

            //start the email format
            string body = "Hello "+EnvatoTrackerFrame.username+",<br>" +
                          "Below are your new sales received:<br><br>";

            //loop through each new item and build the email body
            foreach (ArrayList arr in salesData)
            {
                body += "Item name: " + arr[0].ToString() + "<br>" +
                        "Sold at: " + arr[1].ToString() + "<br><br>";
            }

            body += "Thank-you,<br>EnvatoTracker";

            //send the email
            Email email = new Email();

            if (!email.sendEmail(EnvatoTrackerFrame.emailUsername, EnvatoTrackerFrame.emailPassword, EnvatoTrackerFrame.emailHost,
                EnvatoTrackerFrame.emailFrom, EnvatoTrackerFrame.emailTo, "EnvatoTracker - You have new sales!", body, EnvatoTrackerFrame.emailEnableSSL))
                return false;
            else
                return true;
        }
Example #2
0
        //check if the user has new sales by
        //comparing known sales against results from api
        //returns true on success or false on failure
        public bool hasNewSales(string username)
        {
            //get current known sales
            int currentSaleTotal = 0, newSaleTotal = 0;
            string value = "";

            if (!File.Exists("sales.txt"))
                return false;

            StreamReader sr = new StreamReader("sales.txt");
            value = sr.ReadLine();
            sr.Close();
            string[] split =  value.Split(':');
            currentSaleTotal = int.Parse(split[1].ToString());

            //get new total sales from api
            EnvatoAPI api = new EnvatoAPI();
            newSaleTotal = api.getTotalSales(username);

            if (newSaleTotal > currentSaleTotal)
                return true;
            else
                return false;
        }
Example #3
0
 //validates the user/key combo
 public bool isValidApiKey(string username, string apiKey)
 {
     EnvatoAPI api = new EnvatoAPI();
     return api.isValidApiKey(username, apiKey);
 }
        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();
        }
        private void updateButton_Click_1(object sender, EventArgs e)
        {
            //write user info to file
            username = usernameTextbox.Text;
            apiKey = apikeyTextbox.Text;
            emailTo = emailTextbox.Text;
            EnvatoAPI api = new EnvatoAPI();
            User user = new User();

            //validate fields
            if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(apiKey) || String.IsNullOrEmpty(emailTo))
                MessageBox.Show("Please fill in all details and resubmit");
            else if (!user.IsValidEmailAddress(emailTo))
                MessageBox.Show("Invalid email address");
            else
            {
                //check if user is valid, if so we notify that we're polling, otherwise display warning
                if (user.isValidApiKey(username, apiKey))
                {
                    //update config
                    Config config = new Config();
                    config.setUsername(username);
                    config.setApiKey(apiKey);
                    config.setEmailTo(emailTo);

                    //update total sales status label
                    int totalSales = api.getTotalSales(username);
                    totalSalesLabelTextValue.Text = totalSales.ToString();

                    //write sales total to file
                    StreamWriter sr2 = new StreamWriter("sales.txt");
                    sr2.WriteLine("TotalSales:" + totalSales);
                    sr2.Close();

                    //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;

                    appStatusValue.Text = "API polling active";
                    totalSalesLabelTextValue.Text = totalSales.ToString();
                }
                else
                    appStatusValue.Text = "API polling inactive - invalid key/username combo";
            }
        }
        // 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);
                }
            }
        }