private static bool _PauseResumePrintJobLocal(PrintJobTitle title, string methodName)
 {
     try
     {
         LogHelper.LogDebug("Local Need to " + methodName);
         bool isActionPerformed = false;
         ManagementObjectSearcher   searchPrintJobs   = new ManagementObjectSearcher(_searchQueryPrintJobs);
         ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
         LogHelper.LogDebug("Local Enumerate Jobs at " + Environment.MachineName + ", " + prntJobCollection.Count);
         foreach (ManagementObject prntJob in prntJobCollection)
         {
             PrintJobTitle _titleX = new PrintJobTitle(prntJob);
             if (_titleX.Equals(title))
             {
                 LogHelper.LogDebug("Local " + methodName.Substring(0, methodName.Length - 1) + "ing...");
                 prntJob.InvokeMethod(methodName, null);
                 isActionPerformed = true;
                 LogHelper.LogDebug(methodName);
                 break;
             }
         }
         return(isActionPerformed);
     }
     catch (Exception ex)
     {
         LogHelper.Log(ex);
         return(false);
     }
 }
 public static bool CancelAllPrintJobs(PrintJobTitle printJob, string host)
 {
     try
     {
         LogHelper.LogDebug("Need to Cancel at " + host);
         bool            isActionPerformed = false;
         ManagementScope scope             = new ManagementScope("\\\\" + host + "\\root\\cimv2");
         scope.Connect();
         ObjectQuery query = new ObjectQuery(_searchQueryPrintJobs);
         ManagementObjectSearcher   searchPrintJobs   = new ManagementObjectSearcher(scope, query);
         ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
         int cancelCount = 0;
         foreach (ManagementObject prntJob in prntJobCollection)
         {
             PrintJobTitle _titleX = new PrintJobTitle(prntJob);
             LogHelper.LogDebug(host + " has to Cancel " + _titleX + " | " + printJob);
             if (_titleX.Equals(printJob))
             {
                 LogHelper.LogDebug("Cancelling at " + host);
                 //performs an action similar to the cancel operation of windows print console
                 prntJob.Delete();
                 isActionPerformed = true;
                 cancelCount++;
             }
         }
         LogHelper.LogDebug("Cancelled " + cancelCount + " | " + host);
         return(isActionPerformed);
     }
     catch (Exception ex)
     {
         LogHelper.Log(ex);
         return(false);
     }
 }
        /// <summary>
        /// Constructor. Real Printer Mode.
        /// </summary>
        /// <param name="data">print job data</param>
        public PrintingControlModel(PrintJobData data)
        {
            IsBlockedMode = true;
            PSFileName    = null;
            PrintJobData  = data;
            Title         = PrintJobData.PrintJobTitle;
            if (Title == null)
            {
                Title = new PrintJobTitle(null, "unknown", null, null, -1);
            }

            NumberOfPages = Title.TotalPages;

            // set the chosen printer accordingly
            ChosenPrinter             = new Printer();
            ChosenPrinter.Name        = Title.PrinterName;
            ChosenPrinter.DisplayName = Title.PrinterName;
            IsControlsEnabled         = true;
            SelectedPrinterIndex      = -1;
            Printers = new Printers();
            //Printers.Add(ChosenPrinter);

            // initialize timer
            timerAllowedToPrint          = new System.Windows.Forms.Timer();
            timerAllowedToPrint.Enabled  = false;
            timerAllowedToPrint.Interval = ConfigData.Interval_TimerAllowedToResumeJob;
            timerAllowedToPrint.Tick    += timerAllowedToPrint_Tick;

            UpdateDataDevMode();
        }
        /// Init
        #region Init

        /// <summary>
        /// Constructor. Virtual Printer Mode.
        /// </summary>
        /// <param name="psFileName">postscript file name</param>
        public PrintingControlModel(string psFileName, PrintJobTitle title)
        {
            IsBlockedMode = false;
            PSFileName    = psFileName;
            Title         = title;
            if (Title == null)
            {
                Title = new PrintJobTitle(null, "unknown", null, null, -1);
            }

            if (Title.PrinterName.Equals(ConfigData.PrinterName2))
            {
                IsColorVirtualPrinterSelected = true;
            }

            ChosenPrinter        = new Printer();
            IsControlsEnabled    = false;
            SelectedPrinterIndex = 0;
            Printers             = new Printers();

            // initialize timer
            timerAllowedToPrint          = new System.Windows.Forms.Timer();
            timerAllowedToPrint.Enabled  = false;
            timerAllowedToPrint.Interval = ConfigData.Interval_TimerAllowedToPrint;
            timerAllowedToPrint.Tick    += timerAllowedToPrint_Tick;
        }
        /// <summary>
        /// Checks if the print queue of the specified host has the specified title
        /// </summary>
        /// <param name="host">host to be inspected</param>
        /// <param name="title">title to be found</param>
        /// <returns>true if the title is present; otherwise false</returns>
        public static bool HasPrintJob(string host, PrintJobTitle title)
        {
            if (String.IsNullOrWhiteSpace(host))
            {
                return(false);
            }

            if (title == null)
            {
                return(false);
            }

            try
            {
                foreach (ManagementObject item in GetPrintJobs(host, title.PrinterName))
                {
                    if (title.Equals(new PrintJobTitle(item)))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex);
            }

            return(false);
        }
        /// Print
        #region Print

        /// <summary>
        /// Prints file with specified printer and settings
        /// </summary>
        /// <param name="fileName">file name</param>
        /// <param name="printer">printer settings</param>
        public static void Print(string fileName, string documentName, PrinterSettings printer, PostScriptMetaData data)
        {
            if (String.IsNullOrWhiteSpace(fileName))
            {
                return;
            }

            if (printer == null)
            {
                return;
            }

            if (data == null)
            {
                return;
            }

            try
            {
                // check the existence of the file
                if (!File.Exists(fileName))
                {
                    return;
                }

                // get file format
                SupportedPrintFormat format = GetFileFormat(fileName);
                string newFileName          = fileName;
                // if Postscript file
                if (format == SupportedPrintFormat.PS)
                {
                    newFileName = fileName + ".pdf";
                    // convert to .pdf
                    GhostScriptHelper.ConvertPStoPDF(fileName, newFileName);
                }

                // create corresponding print job title
                PrintJobTitle title = new PrintJobTitle(printer.PrinterName, documentName, Environment.UserName, Environment.MachineName, -1);

                // skip this title
                SkipMethod(title);

                // print document
                if (PDFHelper.Print(printer, newFileName, documentName, Environment.UserName, Environment.MachineName))
                {
                    // add title watcher
                    AddWatcher(title);

                    // log printed data
                    LogPrint(ConfigData.FilePath_PrintLog, documentName, printer, data.NumberOfPages);
                }
            }
            catch (Exception ex)
            {
                WPFNotifier.DebugError(ex);
            }
        }
Exemple #7
0
        /// <summary>
        /// Launches print control window for the specified file. (Virtual printer method)
        /// </summary>
        /// <param name="eFileName">file name</param>
        /// <param name="copyFile">copy file flag</param>
        public void LaunchPrintControl(string eFileName, bool copyFile = true)
        {
            // check the file name
            if (string.IsNullOrWhiteSpace(eFileName))
            {
                return;
            }

            LogHelper.LogDebug("Launch UI For File " + eFileName);

            try
            {
                string newFileName = eFileName;
                if (copyFile)
                {
                    // create new file name
                    newFileName = Path.Combine(ConfigData.Path_Processing, Path.GetFileName(eFileName));

                    // if the file with new file name exists
                    if (File.Exists(newFileName))
                    {
                        // delete the file
                        File.Delete(newFileName);
                    }

                    // copy old file to the new one
                    File.Copy(eFileName, newFileName);
                }

                _dispatcher.BeginInvoke(new Action(() =>
                {
                    PrintJobTitle title = null;

                    // check the allowed printers
                    if (localPrintEventWatcher.AllowedPrintersTitles != null && localPrintEventWatcher.AllowedPrintersTitles.Count > 0)
                    {
                        // get the last title from the allowed printers (this needs for retrieving the correct print job title, for example)
                        title = localPrintEventWatcher.AllowedPrintersTitles.Last();
                    }

                    // show preparing progress
                    //MainController.Singleton.ShowProgressWindow("Preparing", "Your document is being prepared for printing, please wait...");

                    // set up an launch UI for the incoming file
                    PrintingControlWindow window = new PrintingControlWindow(newFileName, title);
                    window.Closed += WindowClosed;

                    // show UI
                    window.Show();
                }), DispatcherPriority.Background);
            }
            catch (Exception ex)
            {
                WPFNotifier.Error(ex);
            }
        }
        /// <summary>
        /// Skips specified title during watch process
        /// </summary>
        /// <param name="title">title to be skipped during watch process [during document print]</param>
        public void SkipDocument(PrintJobTitle title)
        {
            // check title
            if (title == null)
            {
                return;
            }

            lock (_titlesToSkip)
            {
                LogHelper.Log("SkipDocument : " + title.ToString());
                _titlesToSkip.Add(title);
            }
        }
Exemple #9
0
        public void AddWatcherX(PrintJobTitle data)
        {
            if (data == null)
            {
                return;
            }

            LogHelper.LogDebug(data.ToString());

            if (_watchers != null)
            {
                _watchers.AddWatcher(new PrintJobWatcher(data));
            }
        }
Exemple #10
0
        /// <summary>
        /// Adds watcher to the specified print job data
        /// </summary>
        /// <param name="data"></param>
        public void AddWatcher(PrintJobTitle data)
        {
            // check title
            if (data == null)
            {
                return;
            }

            // check watchers
            LogHelper.LogDebug(data.ToString());

            if (_watchers != null)
            {
                // add watcher for the specified title
                _watchers.AddWatcher(new PrintJobWatcher(data));
            }
        }
Exemple #11
0
        /// <summary>
        /// Skips the specified document during tracking print jobs
        /// </summary>
        /// <param name="title"></param>
        public void SkipDocument(PrintJobTitle title)
        {
            // check title
            if (title == null)
            {
                return;
            }

            LogHelper.LogDebug(title.MegaToString());

            // check local watcher
            if (localPrintEventWatcher != null)
            {
                // skip document
                localPrintEventWatcher.SkipDocument(title);
            }
        }
 public static void CancelPrintJobsByUser(string searchhost, string jobhost, string printerName, string owner)
 {
     try
     {
         foreach (ManagementObject job in GetPrintJobs(searchhost, printerName))
         {
             PrintJobTitle title = new PrintJobTitle(job);
             if (title.Host == jobhost && title.Owner == owner)
             {
                 CancelAllPrintJobs(title, searchhost);
             }
         }
     }
     catch (Exception ex)
     {
         LogHelper.Log(ex);
     }
 }
        /// Init
        #region Init

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="printJob"></param>
        public PrintJobWatcher(PrintJobTitle printJobTitle)
        {
            if (printJobTitle == null)
            {
                return;
            }

            /*
             * if (printJob == null)
             *  return;
             *
             * if (printJob.PrintJob == null)
             *  return;*/

            PrintJobTitle  = printJobTitle;
            _printerStatus = PrinterTrouble.None;

            // initialize timer
            timer          = new Timer();
            timer.Enabled  = false;
            timer.Interval = ConfigData.Interval_TimerPrintJobWatcher;
            timer.Elapsed += timer_Elapsed;
        }
        private static bool _PauseResumePrintJob(PrintJobTitle title, string methodName)
        {
            if (title.Host == Environment.MachineName)
            {
                return(_PauseResumePrintJobLocal(title, methodName));
            }

            try
            {
                LogHelper.LogDebug("Need to " + methodName + " at " + title.Host);
                bool            isActionPerformed = false;
                ManagementScope scope             = new ManagementScope("\\\\" + title.Host + "\\root\\cimv2");
                scope.Connect();
                ObjectQuery query = new ObjectQuery(_searchQueryPrintJobs);
                ManagementObjectSearcher   searchPrintJobs   = new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
                LogHelper.LogDebug("Enumerate Jobs at " + title.Host + ", " + prntJobCollection.Count);
                foreach (ManagementObject prntJob in prntJobCollection)
                {
                    PrintJobTitle _titleX = new PrintJobTitle(prntJob);
                    if (_titleX.Equals(title))
                    {
                        LogHelper.LogDebug(methodName.Substring(0, methodName.Length - 1) + "ing... " + title.Host);
                        prntJob.InvokeMethod(methodName, null);
                        isActionPerformed = true;
                        LogHelper.LogDebug(methodName + " " + title.Host);
                        //break;
                    }
                }
                return(isActionPerformed);
            }
            catch (Exception ex)
            {
                LogHelper.Log(ex);
                return(false);
            }
        }
        /// <summary>
        /// Updates data.
        /// </summary>
        /// <param name="data">updated data to be used</param>
        public void UpdateData(PrintJobData data)
        {
            // check data
            if (data == null)
            {
                LogHelper.LogDebug("Update data but nothing received");
                return;
            }

            try
            {
                LogHelper.LogDebug("Update data, pages " + data.PrintJobTitle.TotalPages);

                // update data if more pages found
                if (data.PrintJobTitle.TotalPages > PrintJobData.PrintJobTitle.TotalPages)
                {
                    LogHelper.LogDebug("Current pages " + PrintJobData.PrintJobTitle.TotalPages);

                    // set data
                    PrintJobData = data;

                    // update values
                    Title         = data.PrintJobTitle;
                    NumberOfPages = data.PrintJobTitle.TotalPages;

                    // set status
                    SetStatus("Ready to print");
                }
            }
            catch (Exception ex)
            {
                WPFNotifier.Error(ex);
            }

            UpdateDataDevMode();
        }
        /// Init
        #region Init

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="psFileName">postscript file name</param>
        /// <param name="title">print job title</param>
        public PrintingControlWindow(string psFileName, PrintJobTitle title)
            : this()
        {
            // create view model
            _viewModel = new PrintingControlViewModel(this, new PrintingControlModel(psFileName, title));
        }
Exemple #17
0
 public void SkipDocument(PrintJobTitle title)
 {
     SkipDocument(title.PrinterName, title.Document, title.Owner, title.Host, title.JobID);
 }