/// <summary>
        /// Constructor
        /// </summary>
        /// <param name="session">The session to use for all communication with the webservice.
        /// The user name and password are used from their to provide such details to the
        /// webservice with each call without exposing such detail to the user of the 
        /// library.</param>
        public Request(Session session)
        {
            this.session = session;

            BasicHttpBinding binding;

            if (this.session.NetworkCredential != null)
            {
                binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            }
            else
            {
                binding = new BasicHttpBinding();
            }

            if (this.session.Url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                binding.Security.Mode = BasicHttpSecurityMode.Transport;
                binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
            }

            var endpoint = new EndpointAddress(this.session.Url);
            this.mc = new MantisConnectWebservice.MantisConnectPortTypeClient(binding, endpoint);

            if (this.session.NetworkCredential != null)
            {
                this.mc.ClientCredentials.UserName.UserName = this.session.NetworkCredential.UserName;
                this.mc.ClientCredentials.UserName.Password = this.session.NetworkCredential.Password;
            }
        }
        static private Session LoginToMantis(string ALoginURL, string AUsername, string APassword)
        {
            Session session = new Session(ALoginURL, AUsername, APassword, null);

            session.Connect();

            return session;
        }
        static private void UpdateVersionsOfProject(Session ASession, int AProjectID,
            string AVersionReleased, string AVersionDev, string AVersionNext)
        {
            DateTime DateReleased = DateTime.Today;

            Console.WriteLine(AProjectID.ToString() + " " + AVersionReleased);

            ProjectVersion[] ProjectVersions = ASession.Request.ProjectGetVersions(AProjectID);

            foreach (var element in ProjectVersions)
            {
                if (element.Name == AVersionReleased)
                {
                    element.IsReleased = true;
                    element.DateOrder = DateReleased;
                    element.Description = "";

                    ASession.Request.ProjectVersionUpdate(element);
                }
                else if (element.Name == AVersionDev)
                {
                    AVersionDev = string.Empty;
                }
                else if (element.Name == AVersionNext)
                {
                    AVersionNext = string.Empty;
                }
            }

            // add a new development version
            if (AVersionDev != string.Empty)
            {
                Console.WriteLine("adding version " + AVersionDev);
                ProjectVersion v = new ProjectVersion();
                v.ProjectId = AProjectID;
                v.IsReleased = false;
                v.Name = AVersionDev;
                v.DateOrder = new DateTime(DateReleased.Year, DateReleased.Month, DateReleased.Day, 0, 1, 0);
                v.Description = "for fixing development bugs";
                ASession.Request.ProjectVersionAdd(v);
            }

            // add a new future release version
            if (AVersionNext != string.Empty)
            {
                Console.WriteLine("adding version " + AVersionNext);
                ProjectVersion v = new ProjectVersion();
                v.ProjectId = AProjectID;
                v.IsReleased = false;
                v.Name = AVersionNext;
                v.DateOrder = DateReleased.AddMonths(1);
                v.Description = "next planned release";
                ASession.Request.ProjectVersionAdd(v);
            }
        }
        static private SortedList <int, string>GetAllProjects(Session ASession)
        {
            SortedList <int, string>result = new SortedList <int, string>();
            ProjectData[] projects = ASession.Request.UserGetDetailedAccessibleProjects();

            foreach (ProjectData p in projects)
            {
                result.Add(Convert.ToInt32(p.id), p.name);

                foreach (ProjectData sp in p.subprojects)
                {
                    result.Add(Convert.ToInt32(sp.id), sp.name);
                }
            }

            return result;
        }
        public void ProcessImage(int issueId, string fullSizePath, string fileName, string note)
        {
            // get file name from the user as input to save on the server.
            try
            {
                Session session = new Session(this.PluginSettings.Url, this.PluginSettings.UserName, this.PluginSettings.Password, null);
                session.Request.IssueAttachmentAdd(issueId, fullSizePath, fileName);

                if (note.Trim().Length > 0)
                {
                    IssueNote issueNote = new IssueNote();
                    issueNote.Text = note;
                    session.Request.IssueNoteAdd(issueId, issueNote);
                }

                string message = "Snapshot successfully uploaded";
                MessageBox.Show(message, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (SoapException ex)
            {
                MessageBox.Show(ex.Message, "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
 /// <summary>
 /// Connects to Mantis installation and creates a session which can be accessed through the
 /// <see cref="Session"/> property.
 /// </summary>
 protected void Connect()
 {
     session = new Session( Url, Username, Password, null );
     session.Connect();
 }
        private void MantisFiltersForm_Load(object sender, System.EventArgs e)
        {
            NetworkCredential nc = null;

            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            string basicHttpAuthUserName = appSettings["BasicHttpAuthUserName"];
            string basicHttpAuthPassword = appSettings["BasicHttpAuthPassword"];
            if (!String.IsNullOrEmpty(basicHttpAuthUserName) && basicHttpAuthPassword != null)
            {
                nc = new NetworkCredential(basicHttpAuthUserName, basicHttpAuthPassword);
            }

            string mantisConnectUrl = appSettings["MantisConnectUrl"];
            string mantisUserName = appSettings["MantisUserName"];
            string mantisPassword = appSettings["MantisPassword"];

            session = new Session(mantisConnectUrl, mantisUserName, mantisPassword, nc);
            session.Connect();

            populating = true;
            filtersComboBox.DataSource = session.Request.UserGetFilters( 0 );
            filtersComboBox.DisplayMember = "Name";
            filtersComboBox.ValueMember = "Id";
            populating = false;

            UpdateGrid();
        }
        private void SubmitIssue_Load(object sender, System.EventArgs e)
        {
            try
            {
                NetworkCredential nc = null;

                NameValueCollection appSettings = ConfigurationManager.AppSettings;

                string basicHttpAuthUserName = appSettings["BasicHttpAuthUserName"];
                string basicHttpAuthPassword = appSettings["BasicHttpAuthPassword"];
                if (!String.IsNullOrEmpty(basicHttpAuthUserName) && basicHttpAuthPassword != null)
                {
                    nc = new NetworkCredential(basicHttpAuthUserName, basicHttpAuthPassword);
                }

                string mantisConnectUrl = appSettings["MantisConnectUrl"];
                string mantisUserName = appSettings["MantisUserName"];
                string mantisPassword = appSettings["MantisPassword"];

                session = new Session(mantisConnectUrl, mantisUserName, mantisPassword, nc);

                session.Connect();

                populating = true;
                int i = 0;
                foreach (Project project in session.Request.UserGetAccessibleProjects())
                {
                    TreeNode Node = new TreeNode(project.Name);
                    Node.Tag = project.Id;
                    treeView1.Nodes.Add(Node);
                    if (project.Subprojects.Count > 0)
                    {
                        TreeNode customerNode = new TreeNode(project.Name);
                        customerNode.Tag = project.Id;
                        walkNode(project.Subprojects, ref customerNode);
                        treeView1.Nodes[i].Nodes.Add(customerNode);
                     }
                    if (i == 1)
                        treeView1.SelectedNode=Node;
                    i++;
                }

                this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.TreeView1_AfterSelect);
                populating = false;

                PopulateProjectDependentFields();

                priorityComboBox.DataSource = session.Config.PriorityEnum.GetLabels();
                severityComboBox.DataSource = session.Config.SeverityEnum.GetLabels();
                reproducibilityComboBox.DataSource = session.Config.ReproducibilityEnum.GetLabels();
            }
            catch( Exception ex )
            {
                MessageBox.Show( ex.Message, "Webservice Error", MessageBoxButtons.OK, MessageBoxIcon.Stop );
            }
        }
        private bool Connect()
        {
            if ( session == null )
            {
                try
                {
                    NetworkCredential nc = null;

                    NameValueCollection appSettings = ConfigurationManager.AppSettings;
                    string basicHttpAuthUserName = appSettings["BasicHttpAuthUserName"];
                    string basicHttpAuthPassword = appSettings["BasicHttpAuthPassword"];
                    if (!String.IsNullOrEmpty(basicHttpAuthUserName) && basicHttpAuthPassword != null)
                    {
                        nc = new NetworkCredential(basicHttpAuthUserName, basicHttpAuthPassword);
                    }

                    string mantisConnectUrl = appSettings["MantisConnectUrl"];
                    string mantisUserName = appSettings["MantisUserName"];
                    string mantisPassword = appSettings["MantisPassword"];

                    session = new Session( mantisConnectUrl, mantisUserName, mantisPassword, nc );
                    session.Connect();

                    notifyIcon.Text = "Mantis Notifier";
                }
                catch( System.Net.WebException )
                {
                    Disconnect();
                }
            }

            return session != null;
        }
        private void SubmitIssue_Load(object sender, System.EventArgs e)
        {
            try
            {
                NetworkCredential nc = null;

                NameValueCollection appSettings = ConfigurationManager.AppSettings;

                string basicHttpAuthUserName = appSettings["BasicHttpAuthUserName"];
                string basicHttpAuthPassword = appSettings["BasicHttpAuthPassword"];
                if (!String.IsNullOrEmpty(basicHttpAuthUserName) && basicHttpAuthPassword != null)
                {
                    nc = new NetworkCredential(basicHttpAuthUserName, basicHttpAuthPassword);
                }

                string mantisConnectUrl = appSettings["MantisConnectUrl"];
                string mantisUserName = appSettings["MantisUserName"];
                string mantisPassword = appSettings["MantisPassword"];

                session = new Session(mantisConnectUrl, mantisUserName, mantisPassword, nc);

                session.Connect();

                populating = true;
                projectComboBox.DataSource = session.Request.UserGetAccessibleProjects();
                projectComboBox.DisplayMember = "Name";
                projectComboBox.ValueMember = "Id";
                populating = false;

                projectComboBox.SelectedIndex = 0;
                PopulateProjectDependentFields();

                priorityComboBox.DataSource = session.Config.PriorityEnum.GetLabels();
                severityComboBox.DataSource = session.Config.SeverityEnum.GetLabels();
                reproducibilityComboBox.DataSource = session.Config.ReproducibilityEnum.GetLabels();
            }
            catch( Exception ex )
            {
                MessageBox.Show( ex.Message, "Webservice Error", MessageBoxButtons.OK, MessageBoxIcon.Stop );
            }
        }
        static private void SetVersionFixedInForResolvedBug(Session ASession, int bugid, string AVersionFixedIn)
        {
            Issue issue = ASession.Request.IssueGet(bugid);

            if (issue.Resolution.Id != 20) // 20 means fixed
            {
                TLogging.Log("*resolution* is not a fix, so we are not setting *version fixed in* for bug " + bugid.ToString());
                return;
            }

            issue.FixedInVersion = AVersionFixedIn;
            ASession.Request.IssueUpdate(issue);
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="session">Session to be used to retrieve the configs.</param>
 public Config(Session session)
 {
     this.session = session;
 }
        public string GetIssueSummary(int issueId)
        {
            try
            {
                Session session = new Session(this.PluginSettings.Url, this.PluginSettings.UserName, this.PluginSettings.Password, null);
                Issue issue = session.Request.IssueGet(issueId);
                if (issue == null)
                {
                    return "<issue doesn't exist>";
                }

                return issue.Summary;
            }
            catch (Exception ex)
            {
                return "<" + ex.Message + ">";
            }
        }