public void GetFolderInformation(out string PrintJobsRootFolder, out string LabelDefinitionsRootFolder, out string PaperDefinitionsRootFolder)
        {
            GlobalDataStore.Logger.Debug("RemClientControlObject.GetFolderInformation");

            string AppPath = GlobalDataStore.AppPath;
            string RemotingConfigFilePath = AppPath + @"\ACALabelXClient.config.xml";

            lock (GlobalDataStore.LockClass)
            {
                if (!File.Exists(RemotingConfigFilePath))
                {
                    throw new LabelXRemClientControlOjectException(string.Format("Remoting configuration file doesn't exist: {0}", RemotingConfigFilePath));
                }
            }
            //not need all but fasted to use this for now...
            string             SettingsRootFolder;
            string             MachineName;
            int                PollFrequency;
            PrintGroupItemList PrintGroups = new PrintGroupItemList();

            Toolbox.Toolbox toolbox = new Toolbox.Toolbox();
            toolbox.GetGeneralClientConfiguratonEx(AppPath + @"\ACALabelXClient.config.xml",
                                                   out PrintJobsRootFolder,
                                                   out LabelDefinitionsRootFolder,
                                                   out PaperDefinitionsRootFolder,
                                                   out SettingsRootFolder,
                                                   out MachineName,
                                                   out PollFrequency,
                                                   ref PrintGroups);
        }
        public bool AddPrinterToPrintGroupItem(PrintGroupItem it, string PrinterName)
        {
            GlobalDataStore.Logger.Debug("RemClientControlObject.AddPrinterToPrintGroupItem");
            bool               bRet = false;
            string             PrintJobsRootFolder;
            string             LabelDefinitionsRootFolder;
            string             PaperDefinitionsRootFolder;
            string             SettingsRootFolder;
            string             MachineName;
            int                PollFrequency;
            PrintGroupItemList PrintGroups;

            PrintGroups = new PrintGroupItemList();
            string AppPath = GlobalDataStore.AppPath; // System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string RemotingConfigFilePath = AppPath + @"\ACALabelXClient.config.xml";

            lock (GlobalDataStore.LockClass)
            {
                if (!File.Exists(RemotingConfigFilePath))
                {
                    throw new LabelXRemClientControlOjectException(string.Format("Remoting configuration file doesn't exist: {0}", RemotingConfigFilePath));
                }
            }

            Toolbox.Toolbox toolbox = new Toolbox.Toolbox();
            toolbox.GetGeneralClientConfiguratonEx(AppPath + @"\ACALabelXClient.config.xml",
                                                   out PrintJobsRootFolder,
                                                   out LabelDefinitionsRootFolder,
                                                   out PaperDefinitionsRootFolder,
                                                   out SettingsRootFolder,
                                                   out MachineName,
                                                   out PollFrequency,
                                                   ref PrintGroups);

            //First check if the printgroupitem still exists
            foreach (PrintGroupItem item in PrintGroups)
            {
                if (item.Name.Equals(it.Name, StringComparison.OrdinalIgnoreCase))
                {
                    PrinterItem pi;
                    pi          = new PrinterItem();
                    pi.LongName = PrinterName;
                    pi.Enabled  = true;
                    pi.Trays    = new PrinterTrayItems();
                    bRet        = Toolbox.Toolbox.StorePrinter(AppPath + @"\ACALabelXClient.config.xml", item, pi);
                    break;
                }
            }
            return(bRet);
        }
        public StringCollection GetPaperTypes()
        {
            GlobalDataStore.Logger.Debug("RemClientControlObject.GetPaperTypes");

            string             PrintJobsRootFolder;
            string             LabelDefinitionsRootFolder;
            string             PaperDefinitionsRootFolder;
            string             SettingsRootFolder;
            string             MachineName;
            int                PollFrequency;
            PrintGroupItemList PrintGroups;

            PrintGroups = new PrintGroupItemList();
            string AppPath = GlobalDataStore.AppPath;// System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string RemotingConfigFilePath = AppPath + @"\ACALabelXClient.config.xml";

            lock (GlobalDataStore.LockClass)
            {
                if (!File.Exists(RemotingConfigFilePath))
                {
                    throw new LabelXRemClientControlOjectException(string.Format("Remoting configuration file doesn't exist: {0}", RemotingConfigFilePath));
                }
            }

            Toolbox.Toolbox toolbox = new Toolbox.Toolbox();
            toolbox.GetGeneralClientConfiguratonEx(AppPath + @"\ACALabelXClient.config.xml",
                                                   out PrintJobsRootFolder,
                                                   out LabelDefinitionsRootFolder,
                                                   out PaperDefinitionsRootFolder,
                                                   out SettingsRootFolder,
                                                   out MachineName,
                                                   out PollFrequency,
                                                   ref PrintGroups);

            StringCollection  theList;
            List <LabelXItem> itemList;

            itemList = new List <LabelXItem>();
            theList  = new StringCollection();
            Toolbox.Toolbox.GetItemsFromFolder(PaperDefinitionsRootFolder, ref itemList, string.Empty, LabelX.Toolbox.Toolbox.FileFilterXML);

            foreach (LabelXItem it in itemList)
            {
                theList.Add(it.Name);
            }
            return(theList);
        }
        public ACA.LabelX.Toolbox.PrintGroupItemList GetLabelPrintGroupsEx(out string Machine)
        {
            GlobalDataStore.Logger.Debug("RemClientControlObject.GetLabelPrintGroupsEx");
            ACA.LabelX.Toolbox.PrintGroupItemList PrintGroups;
            PrintGroups = new ACA.LabelX.Toolbox.PrintGroupItemList();

            string AppPath = GlobalDataStore.AppPath; // System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string RemotingConfigFilePath = AppPath + @"\ACALabelXClient.config.xml";

            lock (GlobalDataStore.LockClass)
            {
                if (!File.Exists(RemotingConfigFilePath))
                {
                    throw new LabelXRemClientControlOjectException(string.Format("Remoting configuration file doesn't exist: {0}", RemotingConfigFilePath));
                }
            }
            //not need all but faster to use this for now...
            string PrintJobsRootFolder;
            string LabelDefinitionsRootFolder;
            string PaperDefinitionsRootFolder;
            string SettingsRootFolder;
            string MachineName;
            int    PollFrequency;

            Toolbox.Toolbox toolbox = new Toolbox.Toolbox();
            toolbox.GetGeneralClientConfiguratonEx(AppPath + @"\ACALabelXClient.config.xml",
                                                   out PrintJobsRootFolder,
                                                   out LabelDefinitionsRootFolder,
                                                   out PaperDefinitionsRootFolder,
                                                   out SettingsRootFolder,
                                                   out MachineName,
                                                   out PollFrequency,
                                                   ref PrintGroups);
            Machine = MachineName;
            return(PrintGroups);
        }
        /*
         * What do we want to do?
         * We want to shedule printjobs which are available on the system to the printgroup.
         * So we collect the printgroups and the printjobs. Then we determine the desired
         * papertypes for each labeltype named in the printjob.
         * If a label has more than one suitable papertype, we only take the default for now.
         * It is the intention to let the user release the job to another papertype in a later stage.
         *
         * We then query all available printers which are part of the desired printgroup
         * and determine the windows printer queue length. We also determine if a printer
         * is online or offline.
         * We only consider online printers. If more than one printer with the same papertype
         * is available we take the one for which the windows queuelength is the smallest.
         */

        public bool Start()
        {
            int printLanguage;
            PrintGroupItemList PrintGroups;

            PrintGroups = new PrintGroupItemList();
            GlobalDataStore.Logger.Warning("Starting printing engine...");
            string AppPath = GlobalDataStore.AppPath; // System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string RemotingConfigFilePath = AppPath + @"\ACALabelXClient.config.xml";

            if (!File.Exists(RemotingConfigFilePath))
            {
                throw new LabelXRemClientControlOjectException(string.Format("Remoting configuration file doesn't exist: {0}", RemotingConfigFilePath));
            }

            //not need all but fasted to use this for now...

            string MachineName = string.Empty;
            int    PollFrequency;

            Toolbox.Toolbox toolbox = new Toolbox.Toolbox();

            //When a user changes the printlanguage, we print the total of the
            //current printjob in one language. We only check the variable when
            //handling a new job.
            string slang = toolbox.GetClientConfigurationLanguage(AppPath + @"\ACALabelXClient.config.xml");

            try
            {
                currentLanguage = int.Parse(slang);
            }
            catch (Exception)
            {
                currentLanguage = 1043;
            }

            printLanguage = currentLanguage;

            while (true)
            {
                if (moetstoppen)
                {
                    break;
                }
                //
                //Reread the printgroup XML... mayby not needed everytime...
                //
                if (PrintGroups.Count > 0)
                {
                    PrintGroups.Clear();
                }

                //Remark MVE:
                //  Hier lezen we de XML. De manager uppdate deze bevoorbeeld met een printer 'offline'
                //  maar in het loopje hier beneden, lezen we dit niet telkens opnieuw. D.w.z. dat het
                //  disabelen niet helpt zolang we in de verwerking van printjobs zijn. pas als alle printjobs
                //  klaar zijn gaan we hier uit en is de disable/enable pas definitief
                //  Zie plaats gemarkeerd met ***2 waar we de 'enabled' status van een printer opnieuw zouden moeten inlezen.
                try
                {
                    toolbox.GetGeneralClientConfiguratonEx(AppPath + @"\ACALabelXClient.config.xml",
                                                           out PrintJobsRootFolder,
                                                           out LabelDefinitionsRootFolder,
                                                           out PaperDefinitionsRootFolder,
                                                           out SettingsRootFolder,
                                                           out MachineName,
                                                           out PollFrequency,
                                                           ref PrintGroups);
                }
                catch (Exception e)
                {
                    GlobalDataStore.Logger.Error("Could not retrieve client configuration: " + e.Message);
                }

                bool writePrintgroupsToXml = false;
                //JBOS, 20-08-2013, Removing lock: It may cause an issue where this thread (PT) stops. It'll cause the program to stop printing until it is restarted.
                //lock (GlobalDataStore.LockClass)
                //{
                if ((GlobalDataStore.IsStandAlone) && (GlobalDataStore.MustWriteStandAlonePrintGroups))
                {
                    //write clients.xml file in SettingsRootFolder
                    writePrintgroupsToXml = true;
                    GlobalDataStore.MustWriteStandAlonePrintGroups = false;
                }
                //}
                if (writePrintgroupsToXml)
                {
                    try
                    {
                        WritePrintGroupsToLocalClientXML(PrintGroups, SettingsRootFolder + "Clients.xml", MachineName);
                    }
                    catch
                    {
                        GlobalDataStore.Logger.Info("Error writing printgroups to Clients.xml");
                        GlobalDataStore.MustWriteStandAlonePrintGroups = true;
                    }
                }
                SortedList <DateTime, PrintJobInfo> datelist = new SortedList <DateTime, PrintJobInfo>();
                foreach (PrintGroupItem it in PrintGroups)
                {
                    ///mve
                    /// **2 hier zouden we de status van de printer opnieuw moeten lezen
                    ///
                    // We may not handle disabled printgroupitems
                    if (!it.Enabled)
                    {
                        continue;
                    }

                    PrintJobInfos printjobs;
                    printjobs = new PrintJobInfos();
                    RemClientControlObject theObj;
                    theObj    = new RemClientControlObject();
                    printjobs = theObj.GetPrintjobsForPrintgroup(it);

                    //
                    //Now we have the name of all the printjobs for the group
                    //
                    foreach (PrintJobInfo jobinfo in printjobs)
                    {
                        ///
                        ///hier zouden we de printerstatus moeten opnieuw lezen en vullen.
                        ///
                        //Add dates and jobid to an array
                        if (jobinfo.LastPrinted != null && jobinfo.LastPrinted > new DateTime(1970, 1, 1))
                        {
                            try
                            {
                                datelist.Add(jobinfo.LastPrinted, jobinfo);
                            }
                            catch (ArgumentException)
                            {   //Make sure theres no key exception
                                bool notadded = true;
                                int  counter  = 1;
                                while (notadded)
                                {
                                    if (!(datelist.ContainsKey(jobinfo.LastPrinted.AddMilliseconds(counter))))
                                    {
                                        datelist.Add(jobinfo.LastPrinted.AddMilliseconds(counter), jobinfo);
                                        notadded = false;
                                    }
                                    else
                                    {
                                        counter++;
                                    }
                                }
                            }
                        }

                        //Printjobs which are not released may not be printed.
                        if (!jobinfo.AutoRelease)
                        {
                            continue;
                        }

                        if (moetstoppen)
                        {
                            break;
                        }

                        printLanguage = currentLanguage;

                        GlobalDataStore.Logger.Info("Attempting to print " + jobinfo.ID);
                        bool succes = HandlePrintJob(it, jobinfo, PrintJobsRootFolder, PaperDefinitionsRootFolder, LabelDefinitionsRootFolder, printLanguage);
                        if (succes)
                        {
                            jobinfo.AutoRelease = false;
                            GlobalDataStore.Logger.Info("Succesfully printed " + jobinfo.ID);
                        }
                    }
                    if (moetstoppen)
                    {
                        break;
                    }
                }

                //remove oldest if more than 5 in list
                //remove if older than 3 days

                List <PrintJobInfo> tempprintjobinfolist = new List <PrintJobInfo>();
                try
                {
                    foreach (KeyValuePair <DateTime, PrintJobInfo> pair in datelist)
                    {
                        if (pair.Key < DateTime.Now.AddDays(-3))
                        {
                            if (RemovePrintJob(pair.Value))
                            {
                                GlobalDataStore.Logger.Info("Deleted " + pair.Value.FullFilename);
                            }
                        }
                        else
                        {
                            tempprintjobinfolist.Add(pair.Value);
                        }
                    }
                }
                catch (Exception e)
                {
                    GlobalDataStore.Logger.Error("Unable to remove oldest printjobs (older than 3 days): " + e.Message);
                }

                try
                {
                    while (tempprintjobinfolist.Count > 5)
                    {
                        if (RemovePrintJob(tempprintjobinfolist[0]))
                        {
                            GlobalDataStore.Logger.Info("Deleted " + tempprintjobinfolist[0].FullFilename);
                        }
                        tempprintjobinfolist.RemoveAt(0);
                    }
                }
                catch (Exception e)
                {
                    GlobalDataStore.Logger.Error("Unable to remove oldest printjobs (more than 5 found): " + e.Message);
                }


                if (moetstoppen)
                {
                    break;
                }
                //Then we sleep a while (say 3 second)
                Thread.Sleep(3000);
            }
            return(true);
        }