Exemplo n.º 1
0
        private async Task <bool> ZFetchToStream(cUID pUID, cSection pSection, eDecodingRequired pDecoding, string pTitle, Stream pStream)
        {
            frmProgress lProgress = null;

            try
            {
                lProgress = new frmProgress(pTitle);
                ZChildAdd(lProgress);
                await mClient.SelectedMailbox.UIDFetchAsync(pUID, pSection, pDecoding, pStream, new cBodyFetchConfiguration(lProgress.CancellationToken, lProgress.Increment));

                return(true);
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                if (!IsDisposed)
                {
                    MessageBox.Show(this, $"problem when fetching\n{ex}");
                }
            }
            finally
            {
                if (lProgress != null)
                {
                    lProgress.Complete();
                }
            }

            return(false);
        }
Exemplo n.º 2
0
 private void ZDownloadAdd(frmProgress pProgress)
 {
     mDownloads.Add(pProgress);
     pProgress.FormClosed += ZDownloadClosed;
     Program.Centre(pProgress, this, mDownloads);
     pProgress.Show();
 }
Exemplo n.º 3
0
 private void ZMessagesLoadingAdd(frmProgress pProgress)
 {
     mMessagesLoadingForms.Add(pProgress);
     pProgress.FormClosed += ZMessagesLoadingClosed;
     Program.Centre(pProgress, this, mMessagesLoadingForms);
     pProgress.Show();
 }
Exemplo n.º 4
0
        private async Task ZImageLoadAsync(int pQueryBodyStructureDetailEntryNumber, cSinglePartBody pPart, int pSize)
        {
            frmProgress  lProgress = null;
            MemoryStream lStream   = null;

            try
            {
                cBodyFetchConfiguration lConfiguration;

                if (mProgressBar)
                {
                    lProgress = new frmProgress("image " + pPart.Section.Part + " [" + pSize + " bytes]", pSize);
                    lProgress.ShowAndFocus(this);
                    lConfiguration     = new cBodyFetchConfiguration(lProgress.CancellationToken, lProgress.Increment);
                    mImageLoadProgress = lProgress; // so it can be cancelled from code
                }
                else
                {
                    lConfiguration = null;
                }

                lStream = new MemoryStream();

                await mMessage.FetchAsync(pPart, lStream, lConfiguration);

                if (IsDisposed || pQueryBodyStructureDetailEntryNumber != mQueryBodyStructureDetailEntryNumber)
                {
                    throw new Exception();
                }

                mImageStream = lStream; // so it can be closed
            }
            catch (Exception e)
            {
                if (lStream != null)
                {
                    lStream.Dispose();
                }
                if (IsDisposed || pQueryBodyStructureDetailEntryNumber != mQueryBodyStructureDetailEntryNumber)
                {
                    return;
                }
                rtxDecoded.Text = e.ToString();
                throw;
            }
            finally
            {
                if (lProgress != null)
                {
                    lProgress.Complete();
                }
            }
        }
Exemplo n.º 5
0
        private void ZImageLoadCancel()
        {
            if (mImageLoadProgress != null)
            {
                try { mImageLoadProgress.Cancel(); }
                catch { }

                mImageLoadProgress = null;
            }

            if (mImageStream != null)
            {
                try { mImageStream.Dispose(); }
                catch { }

                mImageStream = null;
            }
        }
Exemplo n.º 6
0
        private async void dgv_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var lData = dgv.Rows[e.RowIndex].DataBoundItem as cGridRowData;

            if (lData == null)
            {
                return;
            }

            cAttachment lAttachment = lData.Attachment;

            var lSaveFileDialog = new SaveFileDialog();

            lSaveFileDialog.FileName = lAttachment.FileName ?? lAttachment.Part.Section.Part + "." + lAttachment.Part.SubType;
            if (lSaveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            frmProgress lProgress = null;

            try
            {
                int lSize = lAttachment.SaveSizeInBytes();
                lProgress = new frmProgress("saving " + lSaveFileDialog.FileName, lSize);
                ZDownloadAdd(lProgress);
                await lAttachment.SaveAsAsync(lSaveFileDialog.FileName, new cBodyFetchConfiguration(lProgress.CancellationToken, lProgress.Increment));
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                if (!IsDisposed)
                {
                    MessageBox.Show(this, $"problem when saving '{lSaveFileDialog.FileName}'\n{ex}");
                }
            }
            finally
            {
                if (lProgress != null)
                {
                    lProgress.Complete();
                }
            }
        }
Exemplo n.º 7
0
        private async void ZDownloadRaw(cSection pSection, int pSize)
        {
            var lSaveFileDialog = new SaveFileDialog();

            lSaveFileDialog.FileName = ZDownloadFileName(pSection) + ".txt";
            if (lSaveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            frmProgress lProgress = null;

            try
            {
                lProgress = new frmProgress("saving " + lSaveFileDialog.FileName, pSize);
                ZDownloadAdd(lProgress);

                using (FileStream lStream = new FileStream(lSaveFileDialog.FileName, FileMode.Create))
                {
                    await mMessage.FetchAsync(pSection, eDecodingRequired.none, lStream, new cBodyFetchConfiguration(lProgress.CancellationToken, lProgress.Increment));
                }
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                if (!IsDisposed)
                {
                    MessageBox.Show(this, $"problem when saving '{lSaveFileDialog.FileName}'\n{ex}");
                }
            }
            finally
            {
                if (lProgress != null)
                {
                    lProgress.Complete();
                }
            }
        }
Exemplo n.º 8
0
        private async void ZQueryMessagesAsync()
        {
            // defend against re-entry during awaits
            int lQueryMessagesAsyncEntryNumber = ++mQueryMessagesAsyncEntryNumber;

            // terminate any outstanding message loading
            ZMessagesLoadingClose();

            dgvMessages.Enabled    = false;
            dgvMessages.DataSource = new BindingSource();

            if (mCurrentMailbox == null)
            {
                return;
            }

            // cautions;
            //  message delivery could arrive during the query
            //   therefore some of the messages could be on the grid already

            frmProgress     lProgress = null;
            List <cMessage> lMessages;

            try
            {
                if (mProgressBar)
                {
                    lProgress = new frmProgress("loading messages");
                    lProgress.ShowAndFocus(this);
                    ZMessagesLoadingAdd(lProgress); // so it can be cancelled from code
                }

                if (mCurrentMailbox.MessageCount > mMaxMessages)
                {
                    cMessageFetchConfiguration lMFConfiguration;

                    if (lProgress == null)
                    {
                        lMFConfiguration = null;
                    }
                    else
                    {
                        lMFConfiguration = new cMessageFetchConfiguration(lProgress.CancellationToken, null, null);  // the setcount and progress will never be used as we aren't asked for items to be cached
                    }
                    // first get the messages, sorted, but don't get the requested properties yet (as this would be wasteful if we are about to trim the set of messages we are going to display)
                    lMessages = await mCurrentMailbox.MessagesAsync(mFilter, mOverrideSort, cMessageCacheItems.Empty, lMFConfiguration);

                    if (IsDisposed || lQueryMessagesAsyncEntryNumber != mQueryMessagesAsyncEntryNumber)
                    {
                        return;
                    }

                    // remove any excess messages (the filter may have removed enough or the mailbox may have changed in the meantime)
                    if (lMessages.Count > mMaxMessages)
                    {
                        lMessages.RemoveRange(mMaxMessages, lMessages.Count - mMaxMessages);
                    }

                    cCacheItemFetchConfiguration lCIFConfiguration;

                    if (lProgress == null)
                    {
                        lCIFConfiguration = null;
                    }
                    else
                    {
                        lProgress.SetCount(lMessages.Count);
                        lCIFConfiguration = new cCacheItemFetchConfiguration(lProgress.CancellationToken, lProgress.Increment);
                    }

                    // get any missing cache items
                    await mClient.FetchAsync(lMessages, mClient.DefaultMessageCacheItems, lCIFConfiguration);
                }
                else if (mFilter != null || mOverrideSort != null || lProgress != null)
                {
                    cMessageFetchConfiguration lConfiguration;

                    if (lProgress == null)
                    {
                        lConfiguration = null;
                    }
                    else
                    {
                        lConfiguration = new cMessageFetchConfiguration(lProgress.CancellationToken, lProgress.SetCount, lProgress.Increment);
                    }

                    lMessages = await mCurrentMailbox.MessagesAsync(mFilter, mOverrideSort, null, lConfiguration); // demonstrate the full API (note that we could have specified non default message properties if required)
                }
                else
                {
                    lMessages = await mCurrentMailbox.MessagesAsync();  // show that getting the full set of messages in a mailbox is trivial if no restrictions are required and the defaults are set correctly
                }
            }

            /* this is commented out as it hides problems in the gating code
             * catch (OperationCanceledException e)
             * {
             *  if (lProgress != null && lProgress.CancellationToken.IsCancellationRequested) return; // ignore the cancellation if we cancelled it
             *  if (!IsDisposed) MessageBox.Show(this, $"a problem occurred: {e}");
             *  return;
             * } */
            catch (Exception e)
            {
                if (!IsDisposed)
                {
                    MessageBox.Show(this, $"a problem occurred: {e}");
                }
                return;
            }
            finally
            {
                if (lProgress != null)
                {
                    lProgress.Complete();
                }
            }

            // check that while getting the messages we haven't been closed or re-entered
            if (IsDisposed || lQueryMessagesAsyncEntryNumber != mQueryMessagesAsyncEntryNumber)
            {
                return;
            }

            // load the grid with data
            ZAddMessagesToGrid(lMessages);

            // enable
            dgvMessages.Enabled = true;

            // initialise unseen count
            if (mCurrentMailbox.UnseenUnknownCount > 0 && mTrackUnseen)
            {
                try { await mCurrentMailbox.SetUnseenCountAsync(); }
                catch (Exception ex)
                {
                    if (!IsDisposed)
                    {
                        MessageBox.Show(this, $"an error occurred while setting unseen: {ex}");
                    }
                }
            }
        }
Exemplo n.º 9
0
        private async void mCurrentMailbox_MessageDelivery(object sender, cMessageDeliveryEventArgs e)
        {
            // cautions;
            //  the event could be for the previously selected mailbox
            //  this could arrive during the query
            //   therefore some of the messages could be on the grid already
            //  we could be processing one of these when the next one arrives

            if (e.MessageHandles[0].MessageCache != mCurrentMessageCache)
            {
                return;
            }
            if (mFilter != null)
            {
                return;
            }

            var lBindingSource = dgvMessages.DataSource as BindingSource;

            if (lBindingSource == null)
            {
                return;
            }

            frmProgress     lProgress = null;
            List <cMessage> lMessages;

            try
            {
                if (mProgressBar)
                {
                    lProgress = new frmProgress("loading new messages", e.MessageHandles.Count);
                    lProgress.ShowAndFocus(this);
                    var lConfiguration = new cCacheItemFetchConfiguration(lProgress.CancellationToken, lProgress.Increment);
                    ZMessagesLoadingAdd(lProgress); // so it can be cancelled from code
                    lMessages = await mCurrentMailbox.MessagesAsync(e.MessageHandles, null, lConfiguration);
                }
                else
                {
                    lMessages = await mCurrentMailbox.MessagesAsync(e.MessageHandles);
                }
            }
            catch (OperationCanceledException) { return; }
            catch (Exception ex)
            {
                if (!IsDisposed)
                {
                    MessageBox.Show(this, $"a problem occurred: {ex}");
                }
                return;
            }
            finally
            {
                if (lProgress != null)
                {
                    lProgress.Complete();
                }
            }

            // check that while getting the messages we haven't been closed or the mailbox changed
            if (IsDisposed || e.MessageHandles[0].MessageCache != mCurrentMessageCache)
            {
                return;
            }

            // load the grid with data
            ZAddMessagesToGrid(lMessages);
        }