public LoginSSO()
        {
            // Login to Support CS SSO - just use a browser control to hit the SSO page and then
            // sniff for the Session Id and use that to login to the API

            // started with the WPF browser control but was very buggy! searching the internet said
            // the windows forms one was more stable, switched and seems to be fine

            // TODO - timeout detection - when salesforce timesout, there is a javascript error from the control
            // I think when SForce is trying to display the "You are about to be logged out due to inactivity"
            // this should be trapped and a message shown to the user with a better logout message/process

            InitializeComponent();
            Utility.setTheme(this);

            RadWindowInteropHelper.SetAllowTransparency(this, false);

            _d = Globals.ThisAddIn.getData();
            _settings = Globals.ThisAddIn.getSettings();
            _local = Globals.ThisAddIn.GetLocalSettings();

            // try the windows form browser
            _webBrowser1 = new System.Windows.Forms.WebBrowser();
            this.wfh1.Child = _webBrowser1;
            _webBrowser1.Navigated += _webBrowser1_Navigated;
            _webBrowser1.DocumentCompleted += _webBrowser1_DocumentCompleted;

            // stop the reminders - salesforce puts up a reminder window - it doesn't work though
            // we get an IE window with an error so just cancel it
            _webBrowser1.NewWindow += _webBrowser1_NewWindow;
        }
        public Login()
        {
            InitializeComponent();
            Utility.setTheme(this);

            _d = Globals.ThisAddIn.getData();
            _settings = Globals.ThisAddIn.getSettings();
            _local = Globals.ThisAddIn.GetLocalSettings();

            // depending on the setting hide the demo logins
            GetLogins(_local.ShowAllLogins);

            if (radComboDemoLogins.Items.Count > 0)
            {
                radComboDemoLogins.SelectedIndex = 0;
            }

            // hid the theme pcicker and the local dbase
            themepick1.Visibility = System.Windows.Visibility.Hidden;
            label4.Visibility = System.Windows.Visibility.Hidden;
            themepick1.SelectedIndex = 0;
            cbLocal.Visibility = System.Windows.Visibility.Hidden;

            tbUserName.Focus();
            tbUserName.SelectAll();
        }
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            // get the local settings and set the theme
            Logger.Log("get the local settings and set the theme");
            _localSettings = new LocalSettings();
            this.SetTheme();

            //Create the Salesforce connection
            _d = new Data();
            _p = new Processing();
            _p.Hide();

            if (_localSettings.SSOLogin)
            {
                // switch off normal login
                Globals.Ribbons.Ribbon1.btnLogin.Visible = false;

                if (_localSettings.ShowAllLogins)
                {
                    Globals.Ribbons.Ribbon1.btnLoginSSO.Visible = false;
                    Globals.Ribbons.Ribbon1.sbtnLoginSSO.Visible = true;
                }
                else
                {
                    Globals.Ribbons.Ribbon1.btnLoginSSO.Visible = true;
                    Globals.Ribbons.Ribbon1.sbtnLoginSSO.Visible = false;
                }
            }
            else
            {
                Globals.Ribbons.Ribbon1.btnLogin.Visible = true;
                Globals.Ribbons.Ribbon1.btnLoginSSO.Visible = false;
                Globals.Ribbons.Ribbon1.sbtnLoginSSO.Visible = false;
            }

            // set the name of the Ribbon button depending on the selected instances
            if (_localSettings.Inst != null && _localSettings.Inst != LocalSettings.Instances.Prod)
            {
                Globals.Ribbons.Ribbon1.btnLoginSSO.Label = _localSettings.Inst.ToString();
                Globals.Ribbons.Ribbon1.sbtnLoginSSO.Label = _localSettings.Inst.ToString();
            }

            if (_localSettings.Debug)
            {
                Globals.Ribbons.Ribbon1.gpDebug.Visible = true;

                // switch on normal login
                Globals.Ribbons.Ribbon1.btnLogin.Visible = true;
                Globals.Ribbons.Ribbon1.btnLoginSSO.Label = "SSO";

            }
            else
            {
                Globals.Ribbons.Ribbon1.gpDebug.Visible = false;
            }

            // ----------------------------------------------------------------------------------------------------------------------------------------------------
            // Change Nov : Auto Login
            bool autologin = false;

            if (autologin && _localSettings.Debug)
            {

                this.SetRibbon();
                //Globals.ThisAddIn.ProcessingStart("AutoLogin - remember to switch off!");

                // This can be used for testing so you don't have to login every time
                // set the autologin about to true
                // then add the details to the login call - should be username, password, sforce token, sforce url, login description to show in the about
                string rtn = _d.Login("[email protected]", "pass@word1", "LGZ0rTkNnuksEetJr1vrG0YS", "https://test.salesforce.com", "AutoLogin - Sales");

                if (rtn == "")
                {

                    // get the settings
                    bool gotSettings = true;

                    DataReturn settings = _d.GetStaticResource("RibbonSettings");
                    if (!settings.success || settings.strRtn == "")
                    {
                        // get the default settings
                        var uri = new Uri("pack://application:,,,/AxiomIRISRibbon;component/Resources/Settings.json");
                        byte[] bjson = AxiomIRISRibbon.Properties.Resources.Settings;
                        string sjson = Encoding.Default.GetString(bjson, 0, bjson.Length - 1);
                        try
                        {
                            _settings = new SForceEdit.Settings(sjson);
                            gotSettings = true;
                        }
                        catch (Exception eSet)
                        {
                            System.Windows.MessageBoxResult rslt = System.Windows.MessageBox.Show("Settings did not load : " + eSet.Message, "Axiom IRIS", System.Windows.MessageBoxButton.OK);
                        }
                    }
                    else
                    {
                        try
                        {
                            _settings = new SForceEdit.Settings(settings.strRtn);
                            gotSettings = true;
                        }
                        catch (Exception eSet)
                        {
                            System.Windows.MessageBoxResult rslt = System.Windows.MessageBox.Show("Settings did not load : " + eSet.Message, "Axiom IRIS", System.Windows.MessageBoxButton.OK);
                        }

                    }

                    if (!gotSettings) return;

                    DataReturn dr = Utility.HandleData(_d.LoadDefinitions());
                    Globals.Ribbons.Ribbon1.LoginOK();
                    Globals.Ribbons.Ribbon1.gpDebug.Visible = true;

                }
                //----------------------------------

            }
            else
            {

            }

             Globals.ThisAddIn.ProcessingStop("LoggedIN");

            // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

             _editWindows = new Dictionary<string, Edit>();
             _editZoomWindows = new Dictionary<string, Edit>();

            //Add in the Save handler
            this.Application.DocumentBeforeSave +=
             new Word.ApplicationEvents4_DocumentBeforeSaveEventHandler(Application_DocumentBeforeSave);

            //Add a loader
            this.Application.DocumentOpen += new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);

            //add a handler to tidy up sidebars
            this.Application.DocumentChange += Application_DocumentChange;
        }
 public void setSettings(SForceEdit.Settings s)
 {
     _settings = s;
 }
        void _webBrowser1_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e)
        {
            string url = e.Url.ToString();
            Globals.Ribbons.Ribbon1.SFDebug("Navigated", url);

            // lblLoginMessage.Text = "";

            System.Windows.Forms.HtmlDocument doc = (System.Windows.Forms.HtmlDocument)_webBrowser1.Document;

            if (doc.Url.ToString().StartsWith("res://ieframe.dll"))
            {
                bsyInd.IsBusy = false;
                lblLoginMessage.Text = "There has been a problem with the SSO login";
            }

            if (doc.Title.ToLower().Contains("error") || doc.Title.ToLower().Contains("if you can see this page"))
            {
                bsyInd.IsBusy = false;
                lblLoginMessage.Text = "There has been a problem with the SSO login - you need to be setup as a user in IRIS Core to access the ribbon.";
            }

            if (e.Url.AbsolutePath.EndsWith("home.jsp"))
            {
                // get the session id from the cookie
                string sid = "";
                string cookies = "";
                try
                {
                    cookies = Uri.UnescapeDataString(Application.GetCookie(new Uri(url)));
                }
                catch (Exception)
                {
                    cookies = "";
                }

                string[] cs = cookies.Split(';');
                foreach (string c in cs)
                {
                    string[] cpr = c.Split('=');
                    if (cpr.Length == 2 && cpr[0].Trim() == "sid")
                    {
                        sid = cpr[1];
                    }
                }

                if (sid != "")
                {
                    Globals.Ribbons.Ribbon1.SFDebug("SessionId", sid);

                    string u = _url;
                    string p = u + "/services/Soap/u/" + _soapversion + "/" + _orgid;
                    string m = u + "/services/Soap/m/" + _soapversion + "/" + _orgid;
                    string s = sid;
                    bool? local = false;
                    string rtn = _d.Login(s, p, m, (local == true ? "access" : "sf"),_instancename);

                    if (rtn == "")
                    {
                        // get the settings
                        bool gotSettings = false;
                        DataReturn settings = _d.GetStaticResource("RibbonSettings");
                        if (!settings.success || settings.strRtn == "")
                        {
                            // get the default settings
                            var uri = new Uri("pack://application:,,,/AxiomIRISRibbon;component/Resources/Settings.json");
                            //string sjson = File.ReadAllText(uri.LocalPath);
                            byte[] bjson = AxiomIRISRibbon.Properties.Resources.Settings;
                            string sjson = Encoding.Default.GetString(bjson, 0, bjson.Length - 1);
                            try
                            {
                                _settings = new SForceEdit.Settings(sjson);
                                gotSettings = true;
                            }
                            catch (Exception eSet)
                            {
                                System.Windows.MessageBoxResult rslt = System.Windows.MessageBox.Show("Settings did not load : " + eSet.Message, "Axiom IRIS", System.Windows.MessageBoxButton.OK);
                            }
                        }
                        else
                        {
                            try
                            {
                                _settings = new SForceEdit.Settings(settings.strRtn);
                                gotSettings = true;
                            }
                            catch (Exception eSet)
                            {
                                System.Windows.MessageBoxResult rslt = System.Windows.MessageBox.Show("Settings did not load : " + eSet.Message, "Axiom IRIS", System.Windows.MessageBoxButton.OK);
                            }
                        }

                        if (!gotSettings) return;
                        Globals.ThisAddIn.setSettings(_settings);
                        Utility.HandleData(_d.LoadDefinitions());
                        Globals.Ribbons.Ribbon1.LoginOK();

                        // If this is not Prod then change the label on the Login
                        Globals.Ribbons.Ribbon1.btnLoginSSO.Label = _instance.ToString();
                        Globals.Ribbons.Ribbon1.sbtnLoginSSO.Label = _instance.ToString();

                        // update the settings to make this instance the default one
                        _local.Inst = _instance;
                        Globals.ThisAddIn.SaveLocalSettings(_local);

                        bsyInd.IsBusy = false;
                        this.Close();
                    }
                    else
                    {
                        bsyInd.IsBusy = false;
                        lblLoginMessage.Text = rtn;
                    }
                }
                else
                {
                    bsyInd.IsBusy = false;
                    lblLoginMessage.Text = "Problem getting the Session Id";
                }
            }
        }
        void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            backgroundWorker.DoWork -= (obj, ev) => WorkerDoWork(obj, ev, "", "", "", "", false, "");
            backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;

            bsyInd.IsBusy = false;
            string rtn = (string)e.Result;

            if (rtn == "")
            {

                    // get the settings
                    bool gotSettings = false;
                    DataReturn settings = _d.GetStaticResource("RibbonSettings");
                    if (!settings.success || settings.strRtn == "")
                    {
                        // get the default settings
                        byte[] bjson = AxiomIRISRibbon.Properties.Resources.Settings;
                        string sjson = Encoding.Default.GetString(bjson, 0, bjson.Length - 1);
                        try
                        {
                            _settings = new SForceEdit.Settings(sjson);
                            gotSettings = true;
                        }
                        catch (Exception eSet)
                        {
                            System.Windows.MessageBoxResult rslt = System.Windows.MessageBox.Show("Settings did not load : " + eSet.Message, "Axiom IRIS", System.Windows.MessageBoxButton.OK);
                        }
                    }
                    else
                    {
                        try
                        {
                            _settings = new SForceEdit.Settings(settings.strRtn);
                            gotSettings = true;
                        }
                        catch (Exception eSet)
                        {
                            System.Windows.MessageBoxResult rslt = System.Windows.MessageBox.Show("Settings did not load : " + eSet.Message, "Axiom IRIS", System.Windows.MessageBoxButton.OK);
                        }
                    }

                    if (!gotSettings) return;
                    Globals.ThisAddIn.setSettings(_settings);

                Utility.HandleData(_d.LoadDefinitions());
                Globals.Ribbons.Ribbon1.LoginOK();
                btnOK.IsEnabled = true;
                this.Close();
            }
            else
            {
                lblLoginMessage.Text = rtn;
            }

            btnOK.IsEnabled = true;
        }