From 0c9a71f626e0e552cf670103b8dad8e61de1fb69 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sun, 6 Jun 2010 18:22:39 +0000 Subject: WinGui: - Moved all the services that handle parsing, scanning, encodes and the queue out into a separate library. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@3362 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- .../Functions/GrowlCommunicator.cs | 119 +++++++++++++++++ .../Functions/Main.cs | 83 ++++++++++++ .../Functions/Win32.cs | 143 +++++++++++++++++++++ 3 files changed, 345 insertions(+) create mode 100644 win/C#/HandBrake.ApplicationServices/Functions/GrowlCommunicator.cs create mode 100644 win/C#/HandBrake.ApplicationServices/Functions/Main.cs create mode 100644 win/C#/HandBrake.ApplicationServices/Functions/Win32.cs (limited to 'win/C#/HandBrake.ApplicationServices/Functions') diff --git a/win/C#/HandBrake.ApplicationServices/Functions/GrowlCommunicator.cs b/win/C#/HandBrake.ApplicationServices/Functions/GrowlCommunicator.cs new file mode 100644 index 000000000..579b92a46 --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Functions/GrowlCommunicator.cs @@ -0,0 +1,119 @@ +/* GrowlCommunicator.cs $ + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +namespace HandBrake.ApplicationServices.Functions +{ + using System; + + using Growl.Connector; + + /// + /// Provides all functionality for communicating with Growl for Windows. + /// + /// + /// This class is implemented as a static class because: + /// 1. It allows nearly all of the Growl-related code to be in one place + /// 2. It prevents the main form, queue handler, and any other part of Handbrake from having to declare + /// or track any new instance variables + /// + public static class GrowlCommunicator + { + /// + /// The that actually talks to Growl + /// + private static GrowlConnector growl; + + /// + /// The Handbrake application instance that is registered with Growl + /// + private static Application application; + + /// + /// Notification shown upon completion of encoding + /// + private static NotificationType encodeOrQueueCompleted = new NotificationType("EncodeOrQueue", "HandBrake Status"); + + /// + /// Checks to see if Growl is currently running on the local machine. + /// + /// + /// true if Growl is running; + /// false otherwise + /// + public static bool IsRunning() + { + Initialize(); + + return growl.IsGrowlRunning(); + } + + /// + /// Registers Handbrake with the local Growl instance + /// + /// + /// This should usually be called at application start-up + /// + public static void Register() + { + Initialize(); + growl.Register(application, new[] {encodeOrQueueCompleted}); + } + + /// + /// Sends a notification to Growl. (Since Handbrake currently only supports one type of notification with + /// static text, this is a shortcut method). + /// + /// + /// The title. + /// + /// + /// The text to display. + /// + public static void Notify(string title, string text) + { + Notification notification = new Notification(application.Name, encodeOrQueueCompleted.Name, String.Empty, + title, text); + growl.Notify(notification); + } + + /// + /// Sends a notification to Growl. (This is the more generic version that could be used in the future if + /// more notification types are implemented) + /// + /// The type of notification to send + /// The notification title + /// The notification text + /// The notification image as a url + public static void Notify(NotificationType notificationType, string title, string text, string imageUrl) + { + Notification notification = new Notification(application.Name, notificationType.Name, String.Empty, title, + text) + { + Icon = imageUrl + }; + + growl.Notify(notification); + } + + /// + /// Initializes the GrowlCommunicator + /// + private static void Initialize() + { + if (growl == null) + { + growl = new GrowlConnector + { + EncryptionAlgorithm = Cryptography.SymmetricAlgorithmType.PlainText + }; + + application = new Application("Handbrake") + { + Icon = Properties.Resources.logo64 + }; + } + } + } +} \ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Functions/Main.cs b/win/C#/HandBrake.ApplicationServices/Functions/Main.cs new file mode 100644 index 000000000..0696101c1 --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Functions/Main.cs @@ -0,0 +1,83 @@ +/* Main.cs $ + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +namespace HandBrake.ApplicationServices.Functions +{ + using System; + using System.Collections.Generic; + using System.Diagnostics; + + /// + /// Useful functions which various screens can use. + /// + public static class Main + { + /// + /// Get the Process ID of HandBrakeCLI for the current instance. + /// + /// List of processes before the new process was started + /// Int - Process ID + public static int GetCliProcess(Process[] before) + { + // This is a bit of a cludge. Maybe someone has a better idea on how to impliment this. + // Since we used CMD to start HandBrakeCLI, we don't get the process ID from hbProc. + // Instead we take the processes before and after, and get the ID of HandBrakeCLI.exe + // avoiding any previous instances of HandBrakeCLI.exe in before. + // Kill the current process. + + DateTime startTime = DateTime.Now; + TimeSpan duration; + + Process[] hbProcesses = Process.GetProcessesByName("HandBrakeCLI"); + while (hbProcesses.Length == 0) + { + hbProcesses = Process.GetProcessesByName("HandBrakeCLI"); + duration = DateTime.Now - startTime; + if (duration.Seconds > 5 && hbProcesses.Length == 0) + // Make sure we don't wait forever if the process doesn't start + return -1; + } + + Process hbProcess = null; + foreach (Process process in hbProcesses) + { + bool found = false; + // Check if the current CLI instance was running before we started the current one + foreach (Process bprocess in before) + { + if (process.Id == bprocess.Id) + found = true; + } + + // If it wasn't running before, we found the process we want. + if (!found) + { + hbProcess = process; + break; + } + } + if (hbProcess != null) + return hbProcess.Id; + + return -1; + } + + /// + /// Show the Exception Window + /// + /// + /// The short error. + /// + /// + /// The long error. + /// + public static void ShowExceptiowWindow(string shortError, string longError) + { + frmExceptionWindow exceptionWindow = new frmExceptionWindow(); + exceptionWindow.Setup(shortError, longError); + exceptionWindow.Show(); + } + } +} \ No newline at end of file diff --git a/win/C#/HandBrake.ApplicationServices/Functions/Win32.cs b/win/C#/HandBrake.ApplicationServices/Functions/Win32.cs new file mode 100644 index 000000000..a3caf38ab --- /dev/null +++ b/win/C#/HandBrake.ApplicationServices/Functions/Win32.cs @@ -0,0 +1,143 @@ +/* win32.cs $ + This file is part of the HandBrake source code. + Homepage: . + It may be used under the terms of the GNU General Public License. */ + +namespace HandBrake.ApplicationServices.Functions +{ + using System; + using System.Runtime.InteropServices; + + /// + /// Win32 API calls + /// + public class Win32 + { + /// + /// Set the Forground Window + /// + /// + /// The h wnd. + /// + /// + /// A Boolean true when complete. + /// + [DllImport("user32.dll")] + public static extern bool SetForegroundWindow(int hWnd); + + /// + /// Lock the workstation + /// + [DllImport("user32.dll")] + public static extern void LockWorkStation(); + + /// + /// Exit Windows + /// + /// + /// The u flags. + /// + /// + /// The dw reason. + /// + /// + /// an integer + /// + [DllImport("user32.dll")] + public static extern int ExitWindowsEx(int uFlags, int dwReason); + + /// + /// System Memory Status + /// + public struct MEMORYSTATUS // Unused var's are required here. + { + /// + /// Unknown + /// + public UInt32 dwLength; + + /// + /// Memory Load + /// + public UInt32 dwMemoryLoad; + + /// + /// Total Physical Memory + /// + public UInt32 dwTotalPhys; // Used + + /// + /// Available Physical Memory + /// + public UInt32 dwAvailPhys; + + /// + /// Total Page File + /// + public UInt32 dwTotalPageFile; + + /// + /// Available Page File + /// + public UInt32 dwAvailPageFile; + + /// + /// Total Virtual Memory + /// + public UInt32 dwTotalVirtual; + + /// + /// Available Virtual Memory + /// + public UInt32 dwAvailVirtual; + } + + /// + /// Global Memory Status + /// + /// + /// The lp buffer. + /// + [DllImport("kernel32.dll")] + public static extern void GlobalMemoryStatus + ( + ref MEMORYSTATUS lpBuffer + ); + + /// + /// Generate a Console Ctrl Event + /// + /// + /// The sigevent. + /// + /// + /// The dw process group id. + /// + /// + /// Bool true is sucess + /// + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent sigevent, int dwProcessGroupId); + + /// + /// Console Ctrl Event + /// + public enum ConsoleCtrlEvent + { + /// + /// Ctrl - C + /// + CTRL_C = 0, + + /// + /// Ctrl - Break + /// + CTRL_BREAK = 1, + + /// + /// Ctrl - Close + /// + CTRL_CLOSE = 2, + } + } +} -- cgit v1.2.3