2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-08-30 14:35:32 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Controls.MainWindowControls
|
|
|
|
|
{
|
|
|
|
|
class NotificationsView : FlickerFreeListBox
|
|
|
|
|
{
|
2022-06-08 16:10:06 +02:00
|
|
|
|
private const int IMG_LEFT_MARGIN = 16;
|
|
|
|
|
private const int IMG_RIGHT_MARGIN = 8;
|
|
|
|
|
|
2013-08-30 14:35:32 +02:00
|
|
|
|
[Browsable(true)]
|
2013-11-07 11:00:53 +01:00
|
|
|
|
public event Action<NotificationsSubModeItem> NotificationsSubModeChanged;
|
2013-08-30 14:35:32 +02:00
|
|
|
|
|
|
|
|
|
public NotificationsView()
|
|
|
|
|
{
|
|
|
|
|
Items.Add(new NotificationsSubModeItem(NotificationsSubMode.Alerts));
|
|
|
|
|
Items.Add(new NotificationsSubModeItem(NotificationsSubMode.Events));
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 16:42:47 +01:00
|
|
|
|
public int GetTotalEntries()
|
|
|
|
|
{
|
|
|
|
|
int total = 0;
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
{
|
2022-06-08 16:10:06 +02:00
|
|
|
|
if (item is NotificationsSubModeItem subModeItem)
|
2013-11-19 16:42:47 +01:00
|
|
|
|
total += subModeItem.UnreadEntries;
|
|
|
|
|
}
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 14:35:32 +02:00
|
|
|
|
public void UpdateEntries(NotificationsSubMode subMode, int entries)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
{
|
2022-06-08 16:10:06 +02:00
|
|
|
|
if (item is NotificationsSubModeItem subModeItem && subModeItem.SubMode == subMode)
|
2013-08-30 14:35:32 +02:00
|
|
|
|
{
|
|
|
|
|
subModeItem.UnreadEntries = entries;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SelectNotificationsSubMode(NotificationsSubMode subMode)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in Items)
|
|
|
|
|
{
|
2022-06-08 16:10:06 +02:00
|
|
|
|
if (item is NotificationsSubModeItem subModeItem && subModeItem.SubMode == subMode)
|
2013-08-30 14:35:32 +02:00
|
|
|
|
{
|
2013-11-19 16:42:47 +01:00
|
|
|
|
var lastSelected = SelectedItem;
|
2013-08-30 14:35:32 +02:00
|
|
|
|
SelectedItem = item;
|
2013-11-19 16:42:47 +01:00
|
|
|
|
|
|
|
|
|
if (lastSelected == SelectedItem)
|
|
|
|
|
OnSelectedIndexChanged(EventArgs.Empty);
|
|
|
|
|
|
2013-08-30 14:35:32 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnDrawItem(DrawItemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnDrawItem(e);
|
|
|
|
|
|
2022-06-08 16:10:06 +02:00
|
|
|
|
if (!(Items[e.Index] is NotificationsSubModeItem item))
|
2013-08-30 14:35:32 +02:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
int itemHeight = e.Bounds.Height;
|
|
|
|
|
int imgWidth = item.Image.Width;
|
|
|
|
|
int imgHeight = item.Image.Height;
|
|
|
|
|
|
|
|
|
|
e.DrawBackground();
|
|
|
|
|
e.Graphics.DrawImage(item.Image,
|
|
|
|
|
e.Bounds.Left + IMG_LEFT_MARGIN,
|
|
|
|
|
e.Bounds.Top + (itemHeight - imgHeight) / 2);
|
|
|
|
|
|
|
|
|
|
FontStyle style = item.SubMode == NotificationsSubMode.Events && item.UnreadEntries > 0
|
|
|
|
|
? FontStyle.Bold
|
|
|
|
|
: FontStyle.Regular;
|
|
|
|
|
|
|
|
|
|
using (Font font = new Font(Font, style))
|
|
|
|
|
{
|
|
|
|
|
int textLeft = e.Bounds.Left + IMG_LEFT_MARGIN + imgWidth + IMG_RIGHT_MARGIN;
|
|
|
|
|
|
|
|
|
|
var textRec = new Rectangle(textLeft, e.Bounds.Top,
|
|
|
|
|
e.Bounds.Right - textLeft, itemHeight);
|
|
|
|
|
|
2018-07-24 10:15:50 +02:00
|
|
|
|
Drawing.DrawText(e.Graphics, item.Text, font, textRec, e.ForeColor,
|
2013-08-30 14:35:32 +02:00
|
|
|
|
TextFormatFlags.VerticalCenter | TextFormatFlags.Left | TextFormatFlags.EndEllipsis);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnSelectedIndexChanged(EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnSelectedIndexChanged(e);
|
|
|
|
|
|
2022-06-08 16:10:06 +02:00
|
|
|
|
if (Items[SelectedIndex] is NotificationsSubModeItem item && NotificationsSubModeChanged != null)
|
2013-11-07 11:00:53 +01:00
|
|
|
|
NotificationsSubModeChanged(item);
|
2013-08-30 14:35:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 15:26:58 +02:00
|
|
|
|
public enum NotificationsSubMode { Alerts, Events }
|
2013-08-30 14:35:32 +02:00
|
|
|
|
|
2013-11-07 11:00:53 +01:00
|
|
|
|
public class NotificationsSubModeItem
|
2013-08-30 14:35:32 +02:00
|
|
|
|
{
|
|
|
|
|
public readonly NotificationsSubMode SubMode;
|
|
|
|
|
|
2022-06-08 16:10:06 +02:00
|
|
|
|
public NotificationsSubModeItem(NotificationsSubMode subMode)
|
2013-08-30 14:35:32 +02:00
|
|
|
|
{
|
2022-06-08 16:10:06 +02:00
|
|
|
|
SubMode = subMode;
|
2013-08-30 14:35:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-02 15:45:42 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// For the Alerts and the Updates this is the total number of entries,
|
|
|
|
|
/// while for the Events it is the number of error entries.
|
|
|
|
|
/// </summary>
|
2013-08-30 14:35:32 +02:00
|
|
|
|
public int UnreadEntries { get; set; }
|
|
|
|
|
|
2022-06-08 16:10:06 +02:00
|
|
|
|
public Image Image => GetImage(SubMode, UnreadEntries);
|
2013-11-19 16:42:47 +01:00
|
|
|
|
|
2022-06-08 16:10:06 +02:00
|
|
|
|
public string Text => GetText(SubMode, UnreadEntries);
|
2013-11-19 16:42:47 +01:00
|
|
|
|
|
2022-06-08 16:10:06 +02:00
|
|
|
|
public static Image GetImage(NotificationsSubMode subMode, int unreadEntries)
|
2013-11-19 16:42:47 +01:00
|
|
|
|
{
|
2022-06-08 16:10:06 +02:00
|
|
|
|
switch (subMode)
|
2013-08-30 14:35:32 +02:00
|
|
|
|
{
|
2013-11-19 16:42:47 +01:00
|
|
|
|
case NotificationsSubMode.Alerts:
|
2020-06-18 02:20:29 +02:00
|
|
|
|
return Images.StaticImages._000_Alert2_h32bit_16;
|
2013-11-19 16:42:47 +01:00
|
|
|
|
case NotificationsSubMode.Events:
|
|
|
|
|
return unreadEntries == 0
|
2020-06-18 02:20:29 +02:00
|
|
|
|
? Images.StaticImages.notif_events_16
|
|
|
|
|
: Images.StaticImages.notif_events_errors_16;
|
2013-11-19 16:42:47 +01:00
|
|
|
|
default:
|
|
|
|
|
return null;
|
2013-08-30 14:35:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-08 16:10:06 +02:00
|
|
|
|
public static string GetText(NotificationsSubMode subMode, int unreadEntries)
|
2013-08-30 14:35:32 +02:00
|
|
|
|
{
|
2022-06-08 16:10:06 +02:00
|
|
|
|
switch (subMode)
|
2013-08-30 14:35:32 +02:00
|
|
|
|
{
|
2013-11-19 16:42:47 +01:00
|
|
|
|
case NotificationsSubMode.Alerts:
|
|
|
|
|
return unreadEntries == 0
|
|
|
|
|
? Messages.NOTIFICATIONS_SUBMODE_ALERTS_READ
|
|
|
|
|
: string.Format(Messages.NOTIFICATIONS_SUBMODE_ALERTS_UNREAD, unreadEntries);
|
|
|
|
|
case NotificationsSubMode.Events:
|
|
|
|
|
if (unreadEntries == 0)
|
|
|
|
|
return Messages.NOTIFICATIONS_SUBMODE_EVENTS_READ;
|
2022-06-08 16:10:06 +02:00
|
|
|
|
if (unreadEntries == 1)
|
2013-11-19 16:42:47 +01:00
|
|
|
|
return Messages.NOTIFICATIONS_SUBMODE_EVENTS_UNREAD_ONE;
|
2022-06-08 16:10:06 +02:00
|
|
|
|
return string.Format(Messages.NOTIFICATIONS_SUBMODE_EVENTS_UNREAD_MANY, unreadEntries);
|
2013-11-19 16:42:47 +01:00
|
|
|
|
default:
|
2022-06-08 16:10:06 +02:00
|
|
|
|
return string.Empty;
|
2013-08-30 14:35:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|