Beispiel #1
0
        /// <summary>
        /// Checks and installs a shortcut file in Start menu.
        /// </summary>
        /// <param name="request">Toast request</param>
        private static async Task CheckInstallShortcut(ToastRequest request)
        {
            var shortcutFilePath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.StartMenu),                 // Not CommonStartMenu
                "Programs",
                request.ShortcutFileName);

            var shortcut = new Shortcut();

            if (!shortcut.CheckShortcut(
                    shortcutPath: shortcutFilePath,
                    targetPath: request.ShortcutTargetFilePath,
                    arguments: request.ShortcutArguments,
                    comment: request.ShortcutComment,
                    workingFolder: request.ShortcutWorkingFolder,
                    windowState: request.ShortcutWindowState,
                    iconPath: request.ShortcutIconFilePath,
                    appId: request.AppId,
                    activatorId: request.ActivatorId))
            {
                shortcut.InstallShortcut(
                    shortcutPath: shortcutFilePath,
                    targetPath: request.ShortcutTargetFilePath,
                    arguments: request.ShortcutArguments,
                    comment: request.ShortcutComment,
                    workingFolder: request.ShortcutWorkingFolder,
                    windowState: request.ShortcutWindowState,
                    iconPath: request.ShortcutIconFilePath,
                    appId: request.AppId,
                    activatorId: request.ActivatorId);

                await Task.Delay((TimeSpan.Zero < request.WaitingDuration)?request.WaitingDuration : _waitingDuration);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Shows a toast.
        /// </summary>
        /// <param name="request">Toast request</param>
        /// <returns>Result of showing a toast</returns>
        public static async Task <ToastResult> ShowAsync(ToastRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (!OsVersion.IsEightOrNewer)
            {
                return(ToastResult.Unavailable);
            }

            if (request.IsShortcutValid)
            {
                await CheckInstallShortcut(request);
            }

            if (!request.IsToastValid)
            {
                return(ToastResult.Invalid);
            }

            var document = PrepareToastDocument(request);

            if (document == null)
            {
                return(ToastResult.Invalid);
            }

            return(await ShowBaseAsync(document, request.AppId));
        }
Beispiel #3
0
        /// <summary>
        /// Prepares a toast document.
        /// </summary>
        /// <param name="request">Toast request</param>
        /// <returns>Toast document</returns>
        private XmlDocument PrepareToastDocument(ToastRequest request)
        {
            XmlDocument document;

            if (!string.IsNullOrWhiteSpace(request.ToastXml))
            {
                // Load a toast document from XML.
                document = new XmlDocument();
                try
                {
                    document.LoadXml(request.ToastXml);
                }
                catch
                {
                    return(null);
                }
            }
            else
            {
                // Compose a toast document.
                document = OsVersion.IsTenOrNewer
                                        ? ComposeVisualForWin10(request)
                                        : ComposeVisualForWin8(request);

                document = AddAudio(document, request);
            }

            logger.Log(LogLevel.Debug, document.GetXml());

            return(document);
        }
Beispiel #4
0
        private static ToastTemplateType GetTemplateType(ToastRequest request)
        {
            if (!string.IsNullOrWhiteSpace(request.ToastLogoFilePath))
            {
                if (string.IsNullOrWhiteSpace(request.ToastTitle))
                {
                    return(ToastTemplateType.ToastImageAndText01);
                }

                return((request.ToastBodyList.Count < 2)
                                        ? ToastTemplateType.ToastImageAndText02
                                        : ToastTemplateType.ToastImageAndText04);

                // ToastTemplateType.ToastImageAndText03 will not be used.
            }
            else
            {
                if (string.IsNullOrWhiteSpace(request.ToastTitle))
                {
                    return(ToastTemplateType.ToastText01);
                }

                return((request.ToastBodyList.Count < 2)
                                        ? ToastTemplateType.ToastText02
                                        : ToastTemplateType.ToastText04);

                // ToastTemplateType.ToastText03 will not be used.
            }
        }
Beispiel #5
0
        /// <summary>
        /// Shows a toast.
        /// </summary>
        /// <param name="request">Toast request</param>
        /// <returns>Result of showing a toast</returns>
        private async Task <ToastResult> ShowBaseAsync(ToastRequest request)
        {
            var document = PrepareToastDocument(request);

            if (document == null)
            {
                return(ToastResult.Invalid);
            }

            return(await ShowBaseAsync(document, request.AppId, request.MaximumDuration));
        }
Beispiel #6
0
		/// <summary>
		/// Show a toast using JSON format.
		/// </summary>
		/// <param name="requestJson">Toast request in JSON format</param>
		/// <returns>Result of showing a toast</returns>
		public static async Task<ToastResult> ShowAsync(string requestJson)
		{
			ToastRequest request;
			try
			{
				request = new ToastRequest(requestJson);
			}
			catch
			{
				return ToastResult.Invalid;
			}

			return await ShowAsync(request);
		}
Beispiel #7
0
        private static XmlDocument ComposeVisualForWin10(ToastRequest request)
        {
            var document = new XmlDocument();

            var toastElement = document.CreateElement("toast");

            if (!string.IsNullOrWhiteSpace(request.Duration))
            {
                toastElement.SetAttribute("duration", request.Duration);
            }
            if (!string.IsNullOrWhiteSpace(request.Scenario))
            {
                toastElement.SetAttribute("scenario", request.Scenario);
            }
            document.AppendChild(toastElement);

            var visualElement = document.CreateElement("visual");

            document.DocumentElement.AppendChild(visualElement);

            var bindingElement = document.CreateElement("binding");

            bindingElement.SetAttribute("template", "ToastGeneric");
            visualElement.AppendChild(bindingElement);

            if (!string.IsNullOrWhiteSpace(request.Title))
            {
                var title = document.CreateElement("text");
                title.AppendChild(document.CreateTextNode(request.Title));
                bindingElement.AppendChild(title);
            }

            foreach (string body in request.BodyList)
            {
                var toastBody = document.CreateElement("text");
                toastBody.AppendChild(document.CreateTextNode(body));
                bindingElement.AppendChild(toastBody);
            }

            if (!string.IsNullOrWhiteSpace(request.LogoFilePath))
            {
                var appLogo = document.CreateElement("image");
                appLogo.SetAttribute("placement", "appLogoOverride");
                appLogo.SetAttribute("src", request.LogoFilePath);
                bindingElement.AppendChild(appLogo);
            }

            return(document);
        }
Beispiel #8
0
        /// <summary>
        /// Shows a toast using JSON format.
        /// </summary>
        /// <param name="requestJson">Toast request in JSON format</param>
        /// <returns>Result of showing a toast</returns>
        public static async Task <ToastResult> ShowAsync(string requestJson)
        {
            ToastRequest request;

            try
            {
                request = new ToastRequest(requestJson);
            }
            catch
            {
                return(ToastResult.Invalid);
            }

            return(await ShowAsync(request));
        }
Beispiel #9
0
 public Toast(string header, string body)
 {
     this.request = new ToastRequest
     {
         ToastHeadline = header,
         ToastBody = body,
     #if DEBUG
         ShortcutFileName = "提督業も忙しい! (debug).lnk",
     #else
         ShortcutFileName = "提督業も忙しい!.lnk",
     #endif
         ShortcutTargetFilePath = Assembly.GetEntryAssembly().Location,
         AppId = AppId,
     };
 }
Beispiel #10
0
        /// <summary>
        /// 创建一个Toast Notification并显示
        /// </summary>
        /// <param name="Body">第一行文本</param>
        /// <param name="BodyExtra">第二行文本</param>
        public static void CreateToastNotification(string Body, string BodyExtra = "")
        {
            var request = new ToastRequest
            {
                ToastHeadline = "EPIC",
                ToastBody = Body,
                ToastBodyExtra = BodyExtra,
                ToastAudio = ToastAudio.Default,
                ShortcutFileName = "EPIC.lnk",
                ShortcutTargetFilePath = Assembly.GetExecutingAssembly().Location,
                AppId = "pw.baka.epic",
            };

            var result = ToastManager.ShowAsync(request);
        }
Beispiel #11
0
        /// <summary>
        /// Shows a toast using JSON format.
        /// </summary>
        /// <param name="requestJson">Toast request in JSON format</param>
        /// <returns>Result of showing a toast</returns>
        public async Task <ToastResult> ShowAsync(string requestJson)
        {
            ToastRequest request;

            try
            {
                request = ToastRequest.FromJsonString(requestJson);
            }
            catch
            {
                return(ToastResult.Invalid);
            }

            return(await ShowAsync(request));
        }
Beispiel #12
0
        private static async Task <ToastResult> ShowBaseAsync(XmlDocument document, ToastRequest request)
        {
            var toast = new ToastNotification(document);

            if (request.Group != null)
            {
                toast.Group = request.Group;
            }
            if (request.Tag != null)
            {
                toast.Tag = request.Tag;
            }

            toast.SuppressPopup = request.SuppressPopup;

            return(await ShowBaseAsync(toast, request.AppId));
        }
Beispiel #13
0
		/// <summary>
		/// Show a toast.
		/// </summary>
		/// <param name="request">Toast request</param>
		/// <returns>Result of showing a toast</returns>
		public static async Task<ToastResult> ShowAsync(ToastRequest request)
		{
			if (!OsVersion.IsEightOrNewer)
				return ToastResult.Unavailable;

			if (request == null)
				throw new ArgumentNullException("request");

			if (request.IsShortcutValid)
				await CheckInstallShortcut(request);

			if (!request.IsToastValid)
				return ToastResult.Invalid;

			var document = PrepareToastDocument(request);

			return await ShowBaseAsync(document, request.AppId);
		}
Beispiel #14
0
        private static XmlDocument ComposeVisualForWin8(ToastRequest request)
        {
            var templateType = GetTemplateType(request);

            // Get a toast template.
            var document = ToastNotificationManager.GetTemplateContent(templateType);

            // Fill in image element.
            switch (templateType)
            {
            case ToastTemplateType.ToastImageAndText01:
            case ToastTemplateType.ToastImageAndText02:
            case ToastTemplateType.ToastImageAndText04:
                var imageElements = document.GetElementsByTagName("image");
                imageElements[0].Attributes.GetNamedItem("src").NodeValue = request.ToastLogoFilePath;
                break;
            }

            // Fill in text elements.
            var textElements = document.GetElementsByTagName("text");

            switch (templateType)
            {
            case ToastTemplateType.ToastImageAndText01:
            case ToastTemplateType.ToastText01:
                textElements[0].AppendChild(document.CreateTextNode(request.ToastBodyList[0]));
                break;

            case ToastTemplateType.ToastImageAndText02:
            case ToastTemplateType.ToastText02:
                textElements[0].AppendChild(document.CreateTextNode(request.ToastTitle));
                textElements[1].AppendChild(document.CreateTextNode(request.ToastBodyList[0]));
                break;

            case ToastTemplateType.ToastImageAndText04:
            case ToastTemplateType.ToastText04:
                textElements[0].AppendChild(document.CreateTextNode(request.ToastTitle));
                textElements[1].AppendChild(document.CreateTextNode(request.ToastBodyList[0]));
                textElements[2].AppendChild(document.CreateTextNode(request.ToastBodyList[1]));
                break;
            }

            return(document);
        }
Beispiel #15
0
        private static XmlDocument AddAudio(XmlDocument document, ToastRequest request)
        {
            var option = CheckAudio(request.ToastAudio);

            if (option == AudioOption.Long)
            {
                document.DocumentElement.SetAttribute("duration", "long");
            }

            var audioElement = document.CreateElement("audio");

            if (option == AudioOption.Silent)
            {
                audioElement.SetAttribute("silent", "true");
            }
            else
            {
                audioElement.SetAttribute("src", $"ms-winsoundevent:Notification.{request.ToastAudio.ToString().ToCamelWithSeparator('.')}");
                audioElement.SetAttribute("loop", (option == AudioOption.Long) ? "true" : "false");
            }
            document.DocumentElement.AppendChild(audioElement);

            return(document);
        }
Beispiel #16
0
		private static ToastTemplateType GetTemplateType(ToastRequest request)
		{
			if (!String.IsNullOrWhiteSpace(request.ToastImageFilePath))
			{
				if (String.IsNullOrWhiteSpace(request.ToastHeadline))
					return ToastTemplateType.ToastImageAndText01;

				if (request.ToastHeadlineWrapsTwoLines)
					return ToastTemplateType.ToastImageAndText03;

				return String.IsNullOrWhiteSpace(request.ToastBodyExtra)
					? ToastTemplateType.ToastImageAndText02
					: ToastTemplateType.ToastImageAndText04;
			}
			else
			{
				if (String.IsNullOrWhiteSpace(request.ToastHeadline))
					return ToastTemplateType.ToastText01;

				if (request.ToastHeadlineWrapsTwoLines)
					return ToastTemplateType.ToastText03;

				return String.IsNullOrWhiteSpace(request.ToastBodyExtra)
					? ToastTemplateType.ToastText02
					: ToastTemplateType.ToastText04;
			}
		}
Beispiel #17
0
		/// <summary>
		/// Check and install a shortcut file in Start menu.
		/// </summary>
		/// <param name="request">Toast request</param>
		private static async Task CheckInstallShortcut(ToastRequest request)
		{
			var shortcutFilePath = Path.Combine(
				Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), // Not CommonStartMenu
				"Programs",
				request.ShortcutFileName);

			var shortcut = new Shortcut();

			if (!shortcut.CheckShortcut(
				shortcutPath: shortcutFilePath,
				targetPath: request.ShortcutTargetFilePath,
				arguments: request.ShortcutArguments,
				comment: request.ShortcutComment,
				workingFolder: request.ShortcutWorkingFolder,
				windowState: request.ShortcutWindowState,
				iconPath: request.ShortcutIconFilePath,
				appId: request.AppId))
			{
				shortcut.InstallShortcut(
					shortcutPath: shortcutFilePath,
					targetPath: request.ShortcutTargetFilePath,
					arguments: request.ShortcutArguments,
					comment: request.ShortcutComment,
					workingFolder: request.ShortcutWorkingFolder,
					windowState: request.ShortcutWindowState,
					iconPath: request.ShortcutIconFilePath,
					appId: request.AppId);

				await Task.Delay((TimeSpan.Zero < request.WaitingDuration) ? request.WaitingDuration : _waitingDuration);
			}
		}
Beispiel #18
0
		/// <summary>
		/// Prepare a toast document.
		/// </summary>
		/// <param name="request">Toast request</param>
		/// <returns>Toast document</returns>
		private static XmlDocument PrepareToastDocument(ToastRequest request)
		{
			var templateType = GetTemplateType(request);

			// Get a toast template.
			var document = ToastNotificationManager.GetTemplateContent(templateType);

			// Fill in image element.
			switch (templateType)
			{
				case ToastTemplateType.ToastImageAndText01:
				case ToastTemplateType.ToastImageAndText02:
				case ToastTemplateType.ToastImageAndText03:
				case ToastTemplateType.ToastImageAndText04:
					var imageElements = document.GetElementsByTagName("image");
					imageElements[0].Attributes.GetNamedItem("src").NodeValue = request.ToastImageFilePath;
					break;
			}

			// Fill in text elements.
			var textElements = document.GetElementsByTagName("text");
			switch (templateType)
			{
				case ToastTemplateType.ToastImageAndText01:
				case ToastTemplateType.ToastText01:
					textElements[0].AppendChild(document.CreateTextNode(request.ToastBody));
					break;

				case ToastTemplateType.ToastImageAndText02:
				case ToastTemplateType.ToastImageAndText03:
				case ToastTemplateType.ToastText02:
				case ToastTemplateType.ToastText03:
					textElements[0].AppendChild(document.CreateTextNode(request.ToastHeadline));
					textElements[1].AppendChild(document.CreateTextNode(request.ToastBody));
					break;

				case ToastTemplateType.ToastImageAndText04:
				case ToastTemplateType.ToastText04:
					textElements[0].AppendChild(document.CreateTextNode(request.ToastHeadline));
					textElements[1].AppendChild(document.CreateTextNode(request.ToastBody));
					textElements[2].AppendChild(document.CreateTextNode(request.ToastBodyExtra));
					break;
			}

			// Set audio element.
			var option = CheckAudio(request.ToastAudio);
			if (option == AudioOption.Long)
				document.DocumentElement.SetAttribute("duration", "long");

			var audioElement = document.CreateElement("audio");
			if (option == AudioOption.Silent)
			{
				audioElement.SetAttribute("silent", "true");
			}
			else
			{
				audioElement.SetAttribute("src", GetAudio(request.ToastAudio));
				audioElement.SetAttribute("loop", (option == AudioOption.Long) ? "true" : "false");
			}
			document.DocumentElement.AppendChild(audioElement);

			Debug.WriteLine(document.GetXml());

			return document;
		}
Beispiel #19
0
		/// <summary>
		/// Show a toast to notify completion of copying.
		/// </summary>
		private async Task ShowToastAsync()
		{
			if (!OsVersion.IsEightOrNewer || (_copyFileCount <= 0) || (DateTime.Now - CopyStartTime < _copyToastShortestDuration))
				return;

			var request = new ToastRequest
			{
				ToastHeadline = Resources.ToastHeadline_CopyCompleted,
				ToastBody = Resources.ToastBody_CopyCompleted,
				ToastBodyExtra = String.Format(Resources.ToastBodyExtra_CopyCompleted, _copyFileCount, (int)(DateTime.Now - CopyStartTime).TotalSeconds),
				ShortcutFileName = Properties.Settings.Default.ShortcutFileName,
				ShortcutTargetFilePath = Assembly.GetExecutingAssembly().Location,
				AppId = Properties.Settings.Default.AppId
			};

			var result = await ToastManager.ShowAsync(request);

			if (result == ToastResult.Activated)
				IsWindowActivateRequested = true; // Activating Window is requested.
		}
 /// <summary>
 /// Shows a toast.
 /// </summary>
 /// <param name="request">Toast request</param>
 /// <returns>Result of showing a toast</returns>
 public static Task <ToastResult> ShowAsync(ToastRequest request)
 {
     return(ShowAsync(request, CancellationToken.None));
 }
Beispiel #21
0
        private async Task<string> ShowToastAsync(string HeadLine, string Body)
        {
            var request = new ToastRequest
            {
                ToastHeadline = HeadLine,
                ToastBody = Body,
                ShortcutFileName = "DejizoToaster.lnk",
                ShortcutTargetFilePath = Assembly.GetExecutingAssembly().Location,
                AppId = "DejizoToaster.WinForms",
                ToastAudio = ToastAudio.Silent
            };

            var result = await ToastManager.ShowAsync(request);

            return result.ToString();
        }