private void DocumentForm_Load(object sender, EventArgs e)
        {
            SignatureQueueServiceClient service = null;
            WaitingForm progressWindow          = null;

            try
            {
                this.Cursor    = Cursors.WaitCursor;
                progressWindow = new WaitingForm(Tools.GetLocalizedString("GettingJobInfo"));
                progressWindow.Show();
                service   = WSTools.GetSignatureQueueServiceClient();
                m_Job     = service.GetJob(m_JobId);
                this.Text = m_Job.jobReferenceEx.jobTitle;

                toolStripOwner.Text = String.Format(Tools.GetLocalizedString("Owner"), m_Job.jobReferenceEx.owner);
                if (m_Job.jobReferenceEx.processed)
                {
                    mnuSignDocument.Visible = false;
                    toolStripTime.Text      = String.Format(Tools.GetLocalizedString("SubmissionTime"), m_Job.jobReferenceEx.time.ToShortDateString(), m_Job.jobReferenceEx.time.ToLongTimeString());
                    toolStripComputer.Text  = String.Format(Tools.GetLocalizedString("SourceComputer"), m_Job.jobReferenceEx.computerName);
                }
                else
                {
                    mnuSignDocument.Visible = true;
                    toolStripTime.Text      = String.Format(Tools.GetLocalizedString("SigningTime"), m_Job.jobReferenceEx.time.ToShortDateString(), m_Job.jobReferenceEx.time.ToLongTimeString());
                    toolStripComputer.Text  = "";
                }

                m_tempFilename = System.IO.Path.GetTempFileName();
                FileStream f = new FileStream(m_tempFilename, FileMode.Create, FileAccess.Write);
                f.Write(m_Job.jobReferenceEx.blob, 0, m_Job.jobReferenceEx.blob.Length);
                f.Close();
                axAcroPDF1.LoadFile(m_tempFilename);
                axAcroPDF1.setShowToolbar(false);

                this.WindowState = FormWindowState.Maximized;
                this.Activate();
            }
            catch (Exception ex)
            {
                Tools.ShowUnexpectedError(this, ex);
            }
            finally
            {
                if (progressWindow != null)
                {
                    progressWindow.Close();
                }
                try
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
                catch { }
                this.Cursor = Cursors.Default;
            }
        }
        private void changeOwner()
        {
            SignatureQueueServiceClient service = null;

            try
            {
                if (lstPendingJobs.SelectedItems.Count == 0)
                {
                    return;
                }
                string newOwner = Microsoft.VisualBasic.Interaction.InputBox(
                    Tools.GetLocalizedString("ChangeOwnerText"),
                    Tools.GetLocalizedString("ChangeOwnerTitle"), System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                if (newOwner == "")
                {
                    // User press cancel button
                    return;
                }

                if (newOwner.IndexOf('\\') == -1)
                {
                    // Concat the name of the user with the domain
                    newOwner = SystemInformation.UserDomainName + "\\" + newOwner;
                }

                this.Cursor = Cursors.WaitCursor;
                service     = WSTools.GetSignatureQueueServiceClient();
                foreach (ListViewItem lstViewItem in lstPendingJobs.SelectedItems)
                {
                    service.ChangeJobOwner(Convert.ToInt32(lstViewItem.Tag), newOwner);
                }
                lstPendingJobsRefresh(true);
            }
            catch (Exception exc)
            {
                this.Cursor = Cursors.Default;
                this.Show();
                Tools.ShowUnexpectedError(this, exc);
            }
            finally
            {
                try
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
                catch { }
                this.Cursor = Cursors.Default;
            }
        }
        private void addJob(string[] newFiles)
        {
            SignatureQueueServiceClient service = null;
            Stream newJobStream = null;

            try
            {
                this.Cursor = Cursors.WaitCursor;

                service = WSTools.GetSignatureQueueServiceClient();
                foreach (string filePath in newFiles)
                {
                    if (System.IO.Path.GetExtension(filePath).ToUpper() == ".PDF")
                    {
                        newJobStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                        service.AddJob("SealSignSQSClient",
                                       0,
                                       System.IO.Path.GetFileName(filePath),
                                       Tools.StreamToByteArray(newJobStream),
                                       "");
                        newJobStream.Close();
                    }
                }
                if (service != null)
                {
                    service.Close();
                    service = null;
                }
                lstPendingJobsRefresh(true);
            }
            catch (Exception exc)
            {
                this.Cursor = Cursors.Default;
                this.Show();
                Tools.ShowUnexpectedError(this, exc);
            }
            finally
            {
                try
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
                catch { }
                this.Cursor = Cursors.Default;
            }
        }
        private void lstSignedJobsRefresh()
        {
            SignatureQueueServiceClient service = null;

            try
            {
                this.Cursor             = Cursors.WaitCursor;
                toolStripTotalJobs.Text = Tools.GetLocalizedString("RefreshingList");
                service = WSTools.GetSignatureQueueServiceClient();
                JobReference[] jobReferences = service.GetProcessedJobs(null, null, null);
                lstSignedJobs.Items.Clear();

                foreach (JobReference jobReference in jobReferences)
                {
                    ListViewItem lstViewItem = new ListViewItem();
                    lstViewItem.ImageIndex = 0;
                    lstViewItem.Tag        = jobReference.id.ToString();
                    lstViewItem.Text       = jobReference.jobTitle;
                    lstViewItem.SubItems.Add(jobReference.owner);
                    lstViewItem.SubItems.Add(jobReference.time.ToShortDateString() + ' ' + jobReference.time.ToLongTimeString());
                    lstSignedJobs.Items.Add(lstViewItem);
                }
                toolStripTotalJobs.Text = "" + jobReferences.Length + Tools.GetLocalizedString("SignedDocs");
                timerRefresh.Start();
            }
            catch (Exception exc)
            {
                timerRefresh.Stop();
                toolStripTotalJobs.Text   = "";
                toolStripCurrentUser.Text = "";
                this.Cursor = Cursors.Default;
                this.Show();
                Tools.ShowUnexpectedError(this, exc);
            }
            finally
            {
                try
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
                catch { }
                this.Cursor = Cursors.Default;
            }
        }
        private void lstDeleteSignedJobs()
        {
            SignatureQueueServiceClient service = null;

            try
            {
                if (lstSignedJobs.SelectedItems.Count == 0)
                {
                    return;
                }
                if (MessageBox.Show(this, Tools.GetLocalizedString("RemoveSelectedJobs"), Tools.GetLocalizedString("Title"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                this.Cursor = Cursors.WaitCursor;
                service     = WSTools.GetSignatureQueueServiceClient();
                foreach (ListViewItem lstViewItem in lstSignedJobs.SelectedItems)
                {
                    service.RemoveJob(Convert.ToInt32(lstViewItem.Tag));
                    lstSignedJobs.Items.Remove(lstViewItem);
                }

                toolStripTotalJobs.Text = "" + lstSignedJobs.Items.Count + Tools.GetLocalizedString("SignedDocs");
            }
            catch (Exception exc)
            {
                this.Cursor = Cursors.Default;
                this.Show();
                Tools.ShowUnexpectedError(this, exc);
            }
            finally
            {
                try
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
                catch { }
                this.Cursor = Cursors.Default;
            }
        }
        private void mnuDeleteDocument_Click(object sender, EventArgs e)
        {
            SignatureQueueServiceClient service = null;

            try
            {
                if (MessageBox.Show(this,
                                    m_Job.jobReferenceEx.processed ? Tools.GetLocalizedString("RemoveSignedJob") : Tools.GetLocalizedString("RemovePendingJob"),
                                    this.Text,
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                this.Cursor = Cursors.WaitCursor;
                service     = WSTools.GetSignatureQueueServiceClient();
                service.RemoveJob(m_JobId);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception exc)
            {
                this.Cursor = Cursors.Default;
                Tools.ShowUnexpectedError(this, exc);
            }
            finally
            {
                try
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
                catch { }
                this.Cursor = Cursors.Default;
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            SignatureQueueServiceClient service = null;

            try
            {
                m_progressWindow.Show(null, Tools.GetLocalizedString("Signing"));

                service = WSTools.GetSignatureQueueServiceClient();

                BiometricSignatureContext context = service.BeginBiometricSignatureProvider(
                    m_jobReferenceEx.id,
                    m_signatureIndex,
                    m_jobReferenceEx.queueName,
                    m_SignatureClientBehaviour.signatureId,
                    m_SignatureClientBehaviour.signatureAccount,
                    m_SignatureClientBehaviour.uri,
                    m_SignatureClientBehaviour.providerParameter,
                    null);
                if (context != null && context.instance != null)
                {
                    byte[] biometricFinalState = sealSignBSSPanel1.GetSignature(context.instance, context.biometricState);
                    if (biometricFinalState != null)
                    {
                        m_signedDocument = service.EndBiometricSignatureProvider(
                            context.instance,
                            biometricFinalState,
                            m_jobReferenceEx.id,
                            m_signatureIndex,
                            m_jobReferenceEx.queueName,
                            m_SignatureClientBehaviour.uri,
                            m_SignatureClientBehaviour.providerParameter);
                    }
                    else
                    {
                        service.Close();
                        return;
                    }
                }

                m_progressWindow.SetMessage(Tools.GetLocalizedString("SignatureSuccess"), 2000);
                this.DialogResult = DialogResult.OK;

                sealSignBSSPanel1.CleanSignature();
                m_progressWindow.Close();
                this.Close();
            }
            catch (Exception ex)
            {
                sealSignBSSPanel1.CleanSignature();
                m_progressWindow.Close();
                Tools.ShowUnexpectedError(this, ex);
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            finally
            {
                try
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
                catch { }
            }
        }
        private void lstPendingJobsRefresh(bool bForce)
        {
            SignatureQueueServiceClient service = null;

            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (tabControl1.SelectedTab == tabPendingJobs)
                {
                    toolStripTotalJobs.Text = Tools.GetLocalizedString("RefreshingList");
                }
                service = WSTools.GetSignatureQueueServiceClient();
                JobReference[] jobReferences = service.GetPendingJobs(null, null, null);
                if ((jobReferences.Length > lstPendingJobs.Items.Count) && !m_bLoading)
                {
                    // There are new pending jobs and is not the first loading of the list
                    if (this.Visible == false /*this.WindowState == FormWindowState.Minimized*/ || tabControl1.SelectedTab != tabPendingJobs)
                    {
                        notifyIcon1.ShowBalloonTip(10, Tools.GetLocalizedString("Title"), string.Format(Tools.GetLocalizedString("NewPendingDocs"), jobReferences.Length - lstPendingJobs.Items.Count), ToolTipIcon.Info);
                    }
                }

                if (!bForce)
                {
                    if (jobReferences.Length == lstPendingJobs.Items.Count)
                    {
                        // No hay nuevos trabajos ==> no actualizar la lista
                        if (tabControl1.SelectedTab == tabPendingJobs)
                        {
                            toolStripTotalJobs.Text = "" + jobReferences.Length + Tools.GetLocalizedString("PendingDocs");
                        }

                        timerRefresh.Start();
                        return;
                    }
                }

                lstPendingJobs.Items.Clear();

                foreach (JobReference jobReference in jobReferences)
                {
                    ListViewItem lstViewItem = new ListViewItem();
                    lstViewItem.ImageIndex = 0;
                    lstViewItem.Tag        = jobReference.id.ToString();
                    lstViewItem.Text       = jobReference.jobTitle;
                    lstViewItem.SubItems.Add(jobReference.owner);
                    lstViewItem.SubItems.Add(jobReference.time.ToShortDateString() + ' ' + jobReference.time.ToLongTimeString());
                    lstViewItem.SubItems.Add(jobReference.computerName);
                    lstPendingJobs.Items.Add(lstViewItem);
                }

                if (tabControl1.SelectedTab == tabPendingJobs)
                {
                    toolStripTotalJobs.Text = "" + jobReferences.Length + Tools.GetLocalizedString("PendingDocs");
                }
                notifyIcon1.Text = toolStripTotalJobs.Text;
                timerRefresh.Start();
            }
            catch (Exception exc)
            {
                toolStripTotalJobs.Text   = "";
                toolStripCurrentUser.Text = "";
                timerRefresh.Stop();
                this.Cursor = Cursors.Default;
                this.Show();
                Tools.ShowUnexpectedError(this, exc);
            }
            finally
            {
                try
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
                catch { }
                this.Cursor = Cursors.Default;
            }
        }
        public static SignatureQueueServiceClient GetSignatureQueueServiceClient()
        {
            bool   useWindowsCredentials = true;
            string userName   = "";
            string password   = "";
            string domainName = "";

            SignatureQueueServiceClient returnValue = new SignatureQueueServiceClient();
            string   UrlSQS         = "";
            TimeSpan SendTimeout    = new TimeSpan(0, 0, 60);
            TimeSpan OpenTimeout    = new TimeSpan(0, 0, 60);
            TimeSpan ReceiveTimeout = new TimeSpan(0, 0, 60);

            RegistryKey policiesKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Policies\SmartAccess\SealSignSQSClient\SignatureParameters", false);

            if (policiesKey == null)
            {
                policiesKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\SmartAccess\SealSignSQSClient\SignatureParameters", false);
            }
            if (policiesKey != null)
            {
                UrlSQS         = policiesKey.GetValue("SealSignSQSServiceURL", "").ToString();
                SendTimeout    = new TimeSpan(0, 0, Convert.ToInt32(policiesKey.GetValue("SendTimeout", 60)));
                OpenTimeout    = new TimeSpan(0, 0, Convert.ToInt32(policiesKey.GetValue("OpenTimeout", 60)));
                ReceiveTimeout = new TimeSpan(0, 0, Convert.ToInt32(policiesKey.GetValue("ReceiveTimeout", 60)));
                policiesKey.Close();
            }

            returnValue.Endpoint.Binding.OpenTimeout    = OpenTimeout;
            returnValue.Endpoint.Binding.SendTimeout    = SendTimeout;
            returnValue.Endpoint.Binding.ReceiveTimeout = ReceiveTimeout;

            ((WSHttpBinding)returnValue.Endpoint.Binding).MaxReceivedMessageSize      = Int32.MaxValue;
            ((WSHttpBinding)returnValue.Endpoint.Binding).MaxBufferPoolSize           = Int32.MaxValue;
            ((WSHttpBinding)returnValue.Endpoint.Binding).ReaderQuotas.MaxArrayLength = Int32.MaxValue;

            if (UrlSQS != "")
            {
                returnValue.Endpoint.Address = new System.ServiceModel.EndpointAddress(UrlSQS + "/SignatureQueueService.svc");
            }

            GetServiceUserCredentials(ref useWindowsCredentials, ref userName, ref password, ref domainName);

            if (!useWindowsCredentials)
            {
                if (((WSHttpBinding)returnValue.Endpoint.Binding).Security.Transport.ClientCredentialType == HttpClientCredentialType.Basic ||
                    ((WSHttpBinding)returnValue.Endpoint.Binding).Security.Message.ClientCredentialType == MessageCredentialType.UserName)
                {
                    returnValue.ClientCredentials.UserName.UserName = (string.IsNullOrEmpty(domainName)) ? userName : domainName + @"\" + userName;
                    returnValue.ClientCredentials.UserName.Password = password;
                }
                else
                {
                    returnValue.ClientCredentials.Windows.ClientCredential.Domain   = domainName;
                    returnValue.ClientCredentials.Windows.ClientCredential.UserName = userName;
                    returnValue.ClientCredentials.Windows.ClientCredential.Password = password;
                }
            }

            return(returnValue);
        }
        private void btnSign_Click(object sender, EventArgs e)
        {
            SignatureQueueServiceClient service = null;

            try
            {
                if (bSigning)
                {
                    return;
                }
                bSigning = true;

                sealSignBSSWacomSTUPanel1.Stop();

                m_otherProgressWindow = new ProgressWindow();
                m_otherProgressWindow.Show(null, Tools.GetLocalizedString("Signing"));

                service = WSTools.GetSignatureQueueServiceClient();

                BiometricSignatureContext context = service.BeginBiometricSignatureProvider(
                    m_jobReferenceEx.id,
                    m_signatureIndex,
                    m_jobReferenceEx.queueName,
                    m_SignatureClientBehaviour.signatureId,
                    m_SignatureClientBehaviour.signatureAccount,
                    m_SignatureClientBehaviour.uri,
                    m_SignatureClientBehaviour.providerParameter,
                    null);
                if (context != null && context.instance != null)
                {
                    byte[] biometricFinalState = sealSignBSSWacomSTUPanel1.GetSignature(context.instance, context.biometricState);
                    if (biometricFinalState != null)
                    {
                        m_signedDocument = service.EndBiometricSignatureProvider(
                            context.instance,
                            biometricFinalState,
                            m_jobReferenceEx.id,
                            m_signatureIndex,
                            m_jobReferenceEx.queueName,
                            m_SignatureClientBehaviour.uri,
                            m_SignatureClientBehaviour.providerParameter);
                    }
                    else
                    {
                        // Te error has already reported on the error handler
                        return;
                    }
                }

                m_otherProgressWindow.SetMessage(Tools.GetLocalizedString("SignatureSuccess"), 1500);

                m_otherProgressWindow.Close();

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                sealSignBSSWacomSTUPanel1.Stop();
                m_otherProgressWindow.Close();

                Tools.ShowUnexpectedError(this, ex);
            }
            finally
            {
                if (service != null)
                {
                    try
                    {
                        service.Close();
                    }
                    catch { }
                }

                this.CloseWindow();
                bSigning = false;
            }
        }