#endif
this.ds = new ServiceDataSource();
// grid binding
InitializeGrid();
BuildTrayMenu();
System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
statusstrip_statuslabel.Text = string.Concat("v", ass.GetName().Version.ToString());
}
/// <summary>
/// Initializes the grid programmatically (set alternating row style and so on..)
/// </summary>
private void InitializeGrid()
{
servicegrid.AlternatingRowsDefaultCellStyle.BackColor = servicegrid.DefaultCellStyle.BackColor.ChangeBrightness(-25);
BindServiceData(true);
this.servicegrid.Rows[0].Selected = true;
this.servicegrid.Focus();
}
/// <summary>
/// Builds the tray menu, adds the selected favorite services.
/// </summary>
private void BuildTrayMenu()
{
var favorites = (from sw in ds.GetServices() where sw.IsFavorite orderby sw.DisplayName descending select sw).ToList<ServiceWrapper>();
while (this.trayicon_menu.Items.Count > 6)
{
this.trayicon_menu.Items.RemoveAt(2);
}
if (favorites.Count < 1)
{
return;
}
foreach (ServiceWrapper sw in favorites)
{
var item_service = new ToolStripMenuItem();
item_service.Text = GetServiceMenuItemText(sw);
item_service.Tag = sw;
item_service.Checked = sw.Status == ServiceControllerStatus.Running;
item_service.ToolTipText = Properties.Resources.NotifyIcon_Menu_FavoriteServicesLabel_Tooltip;
item_service.Click += delegate(object sender, EventArgs e)
{
var i = sender as ToolStripMenuItem;
var swi = i.Tag as ServiceWrapper;
if (item_service.Checked)
{
swi.Stop();
if (ServiceManagerForm.IsElevated)
{
SetStatusUpdateNotificationTimer(swi, ServiceControllerStatus.Stopped, item_service);
}
}
else
{
swi.Start();
if (ServiceManagerForm.IsElevated)
{
SetStatusUpdateNotificationTimer(swi, ServiceControllerStatus.Running, item_service);
}
}
};
this.trayicon_menu.Items.Insert(1, item_service);
}
this.trayicon_menu.Items.Insert(1, new ToolStripSeparator());
var favorite_label = new ToolStripLabel(Properties.Resources.NotifyIcon_Menu_FavoriteServicesLabel_Text);
favorite_label.Font = new Font(System.Drawing.SystemFonts.IconTitleFont, FontStyle.Bold);
favorite_label.ForeColor = System.Drawing.SystemColors.InfoText;
this.trayicon_menu.Items.Insert(2, favorite_label);
}
/// <summary>
/// The service menu item string like [service name] - [service state]
/// </summary>
/// <param name="sw"></param>
/// <returns></returns>
private static string GetServiceMenuItemText(ServiceWrapper sw)
{
var msg = string.Format(Properties.Resources.NotifyIcon_Menu_Service, sw.DisplayName, sw.Status.ToString());
return msg;
}
/// <summary>
/// Starts a timer for the specified service
/// and after the services reached the desired status it refreshes the grid.
/// </summary>
/// <param name="sw"></param>
/// <param name="statusToWaitFor"></param>
private void SetStatusUpdateNotificationTimer(ServiceWrapper sw, ServiceControllerStatus statusToWaitFor, ToolStripMenuItem item_service)
{
if (item_service != null)
{
item_service.Enabled = false;
}
var refresh = new Timer();
refresh.Interval = 200;
refresh.Tick += delegate(object sender2, EventArgs e2)
{
sw.ServiceController.Refresh();
if (sw.ServiceController.Status == statusToWaitFor)
{
if (item_service != null)
{
item_service.Checked = (sw.ServiceController.Status == ServiceControllerStatus.Running);
item_service.Text = GetServiceMenuItemText(sw);
item_service.Enabled = true;
}
ShowServiceState(sw);
BindServiceData(false);
BuildTrayMenu();
refresh.Stop();
}
};
refresh.Start();
}
/// <summary>
/// Displays the service state as a balloon.
/// </summary>
/// <param name="sw"></param>
private void ShowServiceState(ServiceWrapper sw)
{
var msg = string.Format(CultureInfo.InvariantCulture, Properties.Resources.NotifyIcon_Baloon_Service_State, sw.DisplayName, sw.Status.ToString().ToLowerInvariant());
trayicon.ShowBalloonTip(2000, Properties.Resources.NotifyIcon_Baloon_Header, msg, ToolTipIcon.Info);
}
/// <summary>
/// Refreshes the services info and binds the data to the grid.
/// </summary>
/// <param name="refresh_services">If TRUE the list of services is refreshed as well.</param>
private void BindServiceData(bool refresh_services)
{
string selected_service_name = this.instance_selected_service_name;
if (selected_service_name == null)
{
selected_service_name = GetSelectedServiceName();
}
var list = ds.GetServicesSearched(refresh_services);
DataGridViewColumn sorted_column = servicegrid.SortedColumn;
if (sorted_column != null)
{
Properties.Settings.Default.SortColumnName = sorted_column.DataPropertyName;
Properties.Settings.Default.SortOrder = servicegrid.SortOrder;
}
else
{
sorted_column = (from DataGridViewColumn c in servicegrid.Columns where c.DataPropertyName == Properties.Settings.Default.SortColumnName select c).Single<DataGridViewColumn>();
}
var sorting_list = new SortableBindingList<ServiceWrapper>(list);
this.servicegrid.DataSource = sorting_list;
if (sorted_column != null)
{
servicegrid.Sort(sorted_column, Properties.Settings.Default.SortOrder == SortOrder.Descending ? ListSortDirection.Descending : ListSortDirection.Ascending);
}
if (selected_service_name != null)
{
servicegrid.ClearSelection();
var row = (from DataGridViewRow r in servicegrid.Rows where (r.DataBoundItem as ServiceWrapper).DisplayName == selected_service_name select r).SingleOrDefault<DataGridViewRow>();
if (row != null)
{
row.Selected = true;
servicegrid.FirstDisplayedScrollingRowIndex = row.Index;
}
}
servicegrid.Update();
}
/// <summary>
/// Gets the name of the selected service.
/// </summary>
/// <returns></returns>
private string GetSelectedServiceName()
{
string selected_service_name = null;
if (servicegrid.SelectedRows.Count > 0)
{
selected_service_name = (servicegrid.SelectedRows[0].DataBoundItem as ServiceWrapper).DisplayName;
}
return selected_service_name;
}
/// <summary>
/// If the window is minimized it will hidden in the tray.
/// </summary>
private void SetTaskbarState()
{
if (FormWindowState.Minimized == this.WindowState)
{
this.ShowInTaskbar = false;
if (!Properties.Settings.Default.TrayBalloonShown)
{
this.trayicon.ShowBalloonTip(5000, Properties.Resources.NotifyIcon_Baloon_Header, Properties.Resources.NotifyIcon_Baloon_Hide, ToolTipIcon.Info);
Properties.Settings.Default.TrayBalloonShown = true;
}
}
else
{
this.ShowInTaskbar = true;
}
}
public static bool IsElevated
{
get
{
WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
return hasAdministrativeRight;
}
}
public static void RequestElevatation(String serviceName, bool falseIsToStopAndTrueIsToStart)
{
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.Verb = "runas";
processInfo.FileName = Application.ExecutablePath;
processInfo.Arguments = serviceName + " " + falseIsToStopAndTrueIsToStart.ToString();
try
{
Process.Start(processInfo);
}
catch (Win32Exception)
{
return;
}
Application.Exit();
}
#endregion
#region Commands
/// <summary>
/// Displays the form.
/// </summary>
private void ShowForm()
{
this.WindowState = FormWindowState.Normal;
}
/// <summary>
/// Saves the settings and quits.
/// </summary>
public void QuitForm()
{
SaveSettings();