/* * Copyright (c) 2009 Craig Sutherland * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ namespace FastForward.WinCore { using System; using System.ComponentModel; using System.Linq; using System.Windows.Forms; using ThoughtWorks.CruiseControl.Remote; using ThoughtWorks.CruiseControl.Remote.Monitor; /// /// A that contains details. /// public class ProjectListViewItem : ListViewItem { #region Private fields private Project project; #endregion #region Constructors /// /// Initialise a new . /// /// public ProjectListViewItem(Project project) : base(new string[8]) { this.project = project; Text = project.Server.Name; SubItems[1].Text = project.Name; SubItems[2].Text = project.Category; SubItems[3].Text = RetrieveActivity(); SubItems[4].Text = RetrieveMessages(); SubItems[5].Text = project.LastSuccessfulBuildLabel; SubItems[6].Text = project.LastBuildDate.ToString(); SubItems[7].Text = project.Status.ToString(); ImageKey = ProjectUtils.RetrieveImage(project); project.PropertyChanged += project_PropertyChanged; } #endregion #region Public properties #region Project /// /// The underlying project. /// public Project Project { get { return project; } } #endregion #region ProgressCompleted /// /// Gets or sets the progress completed. /// public double ProgressCompleted { get; set; } #endregion #endregion #region Private methods #region RetrieveActivity() /// /// Retrieve the text for the activity column. /// /// private string RetrieveActivity() { var activity = project.Activity.Type; if (project.Status == ProjectIntegratorState.Stopped) activity = "Stopped"; return activity; } #endregion #region project_PropertyChanged() /// /// Handle a change in the underlying project. /// /// /// private void project_PropertyChanged(object sender, PropertyChangedEventArgs e) { ListView.SafeInvoke(() => { switch (e.PropertyName) { case "Category": SubItems[2].Text = project.Category; break; case "BuildStatus": ImageKey = ProjectUtils.RetrieveImage(project); break; case "Activity": SubItems[3].Text = RetrieveActivity(); SubItems[4].Text = RetrieveMessages(); ImageKey = ProjectUtils.RetrieveImage(project); break; case "Status": SubItems[3].Text = RetrieveActivity(); SubItems[4].Text = RetrieveMessages(); SubItems[7].Text = project.Status.ToString(); ImageKey = ProjectUtils.RetrieveImage(project); break; case "Messages": case "NextBuildTime": SubItems[4].Text = RetrieveMessages(); break; case "LastSuccessfulBuildLabel": SubItems[5].Text = project.LastSuccessfulBuildLabel; break; case "LastBuildDate": SubItems[6].Text = project.LastBuildDate.ToString(); break; case "Exception": ImageKey = ProjectUtils.RetrieveImage(project); SubItems[4].Text = RetrieveMessages(); if (project.Exception == null) { SubItems[3].Text = RetrieveActivity(); SubItems[7].Text = project.Status.ToString(); SubItems[5].Text = project.LastSuccessfulBuildLabel; SubItems[6].Text = project.LastBuildDate.ToString(); } else { SubItems[3].Text = string.Empty; SubItems[7].Text = string.Empty; SubItems[5].Text = string.Empty; SubItems[6].Text = string.Empty; } break; } }); } #endregion #region RetrieveMessages() /// /// Retrieve the text for the messages column. /// /// private string RetrieveMessages() { if (project.Exception == null) { var details = string.Join(",", (from message in project.Messages select message.Text).ToArray()); if (project.Activity.IsBuilding()) { details = "Unknown progress"; try { var status = project.RetrieveCurrentStatus(); if (status.ChildItems.Count > 0) { var completed = (from item in status.ChildItems where (item.Status != ItemBuildStatus.Pending) && (item.Status != ItemBuildStatus.Running) select item).Count(); ProgressCompleted = completed * 100 / (double)status.ChildItems.Count; details = string.Format("{0}% completed", ProgressCompleted); } } catch { // The server does not allow this functionality } } else if (project.Activity.IsSleeping() && (project.Status != ProjectIntegratorState.Stopped) && (project.NextBuildTime != DateTime.MaxValue)) { details = string.Format("Next build check: {0}{1}", project.NextBuildTime, details.Length > 0 ? " - " + details : string.Empty); } return details; } else { return string.Format("Unable to connect: {0}", project.Exception.Message); } } #endregion #endregion } }