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 showUserCredentialsForm()
        {
            UserCredentialsForm userForm = new UserCredentialsForm();
            DialogResult        result   = userForm.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                if (tabControl1.SelectedTab == tabPendingJobs)
                {
                    lstSignedJobsRefresh();
                }
                else
                {
                    lstSignedJobsRefresh();
                }
                toolStripCurrentUser.Text = WSTools.GetServiceCurrentUser();
            }
        }
        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 JobListForm_Load(object sender, EventArgs e)
        {
            m_bLoading = true;

            /*try
             * {
             *  AppDomain myDomain = Thread.GetDomain();
             *
             *  myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
             *  WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
             *
             *  PrincipalPermission adminPermission = new PrincipalPermission(null, "SealSignSQS Admins");
             *  adminPermission.Demand();
             *  // Si el usuario es admin mostramos el menú de parámetros de firma
             *  mnuParameters.Visible = true;
             * }
             * catch
             * {
             * }*/

            m_lvwColumnSorter = new ListViewColumnSorter();
            lstPendingJobs.ListViewItemSorter = m_lvwColumnSorter;
            lstSignedJobs.ListViewItemSorter  = m_lvwColumnSorter;

            //this.WindowState = FormWindowState.Minimized;
            this.Hide();
            int interval = Convert.ToInt32(Tools.GetAppSettings("RefreshTimeout")) * 1000;

            if (interval == 0)
            {
                //timerRefresh.Interval = 60000;
                timerRefresh.Stop();
            }
            else
            {
                timerRefresh.Interval = interval;
                timerRefresh.Start();
            }
            lstPendingJobsRefresh(true);
            toolStripCurrentUser.Text = WSTools.GetServiceCurrentUser();
            m_bLoading = false;
        }
        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;
            }
        }
        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;
            }
        }