OUT-159: Move GetDownloadState logic to CryptableMailItem.ProcessAndGetRating.
authorThomas
Tue, 08 Nov 2016 09:57:47 +0100
changeset 14181694c5f4a2a2
parent 1417 97abe1c7eeff
child 1420 48a276f740ea
OUT-159: Move GetDownloadState logic to CryptableMailItem.ProcessAndGetRating.
CryptableMailItem.cs
UI/FormRegionPrivacyStatus.cs
     1.1 --- a/CryptableMailItem.cs	Mon Nov 07 13:17:03 2016 +0100
     1.2 +++ b/CryptableMailItem.cs	Tue Nov 08 09:57:47 2016 +0100
     1.3 @@ -99,13 +99,15 @@
     1.4          private BackgroundWorker                  processor;
     1.5          private BackgroundWorker                  mirrorLocator;
     1.6          private bool                              disposeAfterProcessing;
     1.7 -        private object                            mutexMailItem       = new object();
     1.8 -        private static object                     mutexMirror         = new object();
     1.9 -        private static object                     mutexConversation   = new object();
    1.10 -        private static object                     mutexDecrytionList  = new object();
    1.11 -        private static Dictionary<string, string> mirrorCache         = new Dictionary<string, string>();
    1.12 -        private static List<OriginalMailItem>     conversationCache   = new List<OriginalMailItem>();
    1.13 -        private static Dictionary<string, string> decryptionList      = new Dictionary<string, string>();
    1.14 +        private object                            mutexMailItem         = new object();
    1.15 +        private static object                     mutexMirror           = new object();
    1.16 +        private static object                     mutexConversation     = new object();
    1.17 +        private static object                     mutexDecryptionList   = new object();
    1.18 +        private static object                     mutexRepeatProcessing = new object();
    1.19 +        private static bool                       repeatProcessing      = false;
    1.20 +        private static Dictionary<string, string> mirrorCache           = new Dictionary<string, string>();
    1.21 +        private static List<OriginalMailItem>     conversationCache     = new List<OriginalMailItem>();
    1.22 +        private static Dictionary<string, string> decryptionList        = new Dictionary<string, string>();
    1.23  
    1.24          /**************************************************************
    1.25           * 
    1.26 @@ -717,11 +719,13 @@
    1.27                  bool isSubmitted;
    1.28                  bool specialCaseFound = false;
    1.29                  bool entryIDWasAdded = false;
    1.30 +                bool markForDownload = false;
    1.31                  bool mirrorCreated = false;
    1.32                  string entryId = this.internalMailItem.EntryID;
    1.33                  object propValue;
    1.34                  Outlook.MailItem mirrorMailItem = null;
    1.35                  Outlook.MailItem mailItem;
    1.36 +                Outlook.OlDownloadState dlState;
    1.37                  PEPMessage message = null;
    1.38                  PEPMessage mirror = null;
    1.39                  PEPMessage processedMessage;
    1.40 @@ -739,6 +743,51 @@
    1.41                  isDraft = this.internalMailItem.GetIsDraft();
    1.42                  isSubmitted = this.internalMailItem.GetIsSubmitted();
    1.43  
    1.44 +                // Attempt to get the download state
    1.45 +                do
    1.46 +                {
    1.47 +                    try
    1.48 +                    {
    1.49 +                        dlState = this.internalMailItem.DownloadState;
    1.50 +                    }
    1.51 +                    catch (Exception ex)
    1.52 +                    {
    1.53 +                        Log.Warning("ProcessAndGetRating: Get DownloadState failed, " + ex.ToString());
    1.54 +
    1.55 +                        // Assume everything is downloaded, but try to download again as well
    1.56 +                        dlState = Outlook.OlDownloadState.olFullItem;
    1.57 +                        markForDownload = true;
    1.58 +                    }
    1.59 +
    1.60 +                    if (dlState != Outlook.OlDownloadState.olFullItem)
    1.61 +                    {
    1.62 +                        markForDownload = true;
    1.63 +                    }
    1.64 +                    else
    1.65 +                    {
    1.66 +                        markForDownload = false;
    1.67 +                    }
    1.68 +
    1.69 +                    if (markForDownload)
    1.70 +                    {
    1.71 +                        try
    1.72 +                        {
    1.73 +                            this.internalMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
    1.74 +                        }
    1.75 +                        catch (Exception ex)
    1.76 +                        {
    1.77 +                            Log.Warning("ProcessAndGetRating: MarkForDownload failed, " + ex.ToString());
    1.78 +                        }
    1.79 +
    1.80 +                        Thread.Sleep(sleepTime);
    1.81 +                        curWaitTime += sleepTime;
    1.82 +                    }
    1.83 +
    1.84 +                } while (markForDownload);
    1.85 +
    1.86 +                // Reset wait time to zero for next step
    1.87 +                curWaitTime = 0;
    1.88 +
    1.89                  // Make sure this mailitem is not already being decrypted
    1.90                  if (string.IsNullOrEmpty(entryId) == false)
    1.91                  {
    1.92 @@ -748,7 +797,7 @@
    1.93                           * If so, it is being decrypted already and we must wait
    1.94                           * If it isn't already being decrypted, then add it to the decryption list
    1.95                           */
    1.96 -                        lock (mutexDecrytionList)
    1.97 +                        lock (mutexDecryptionList)
    1.98                          {
    1.99                              if (decryptionList.ContainsKey(entryId))
   1.100                              {
   1.101 @@ -990,7 +1039,7 @@
   1.102                  }
   1.103  
   1.104                  // Release the decryption lock
   1.105 -                lock (mutexDecrytionList)
   1.106 +                lock (mutexDecryptionList)
   1.107                  {
   1.108                      if (entryIDWasAdded)
   1.109                      {
   1.110 @@ -1030,6 +1079,13 @@
   1.111                  this.processor.RunWorkerAsync();
   1.112                  return (true);
   1.113              }
   1.114 +            else
   1.115 +            {
   1.116 +                lock (mutexRepeatProcessing)
   1.117 +                {
   1.118 +                    repeatProcessing = true;
   1.119 +                }
   1.120 +            }
   1.121  
   1.122              return (false);
   1.123          }
   1.124 @@ -1048,14 +1104,30 @@
   1.125          /// </summary>
   1.126          private void Processor_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
   1.127          {
   1.128 +            bool processAgain = false;
   1.129 +
   1.130              // Raise complete event (only if dispose after decryption is not true since .this reference is returned)
   1.131              if (this.disposeAfterProcessing == false)
   1.132              {
   1.133                  this.ProcessingCompleted?.Invoke(this, new ProcessingCompletedEventArgs((pEpRating)e.Result));
   1.134              }
   1.135  
   1.136 +            // Check if another processing is needed
   1.137 +            lock (mutexRepeatProcessing)
   1.138 +            {
   1.139 +                if (repeatProcessing)
   1.140 +                {
   1.141 +                    processAgain = true;
   1.142 +                    repeatProcessing = false;
   1.143 +                }
   1.144 +            }
   1.145 +
   1.146 +            if (processAgain)
   1.147 +            {                
   1.148 +                this.StartProcessing();
   1.149 +            }
   1.150              // Dispose if necessary
   1.151 -            if (this.disposeAfterProcessing)
   1.152 +            else if (this.disposeAfterProcessing)
   1.153              {
   1.154                  this.Dispose(false);
   1.155  
     2.1 --- a/UI/FormRegionPrivacyStatus.cs	Mon Nov 07 13:17:03 2016 +0100
     2.2 +++ b/UI/FormRegionPrivacyStatus.cs	Tue Nov 08 09:57:47 2016 +0100
     2.3 @@ -102,7 +102,6 @@
     2.4          private bool                    isManagerFormEnabled   = true;
     2.5          private bool                    isStarted              = false;
     2.6          private FormManagePrivacyStatus managerForm            = null;
     2.7 -        private bool                    refreshOngoing         = false;
     2.8  
     2.9          /**************************************************************
    2.10           * 
    2.11 @@ -951,10 +950,7 @@
    2.12          /// </summary>
    2.13          private void TimerRefresh_Tick(object sender, EventArgs e)
    2.14          {
    2.15 -            bool tryAgain = false;
    2.16 -            bool markForDownload = false;
    2.17              this.TimerRefresh.Enabled = false; // Only once
    2.18 -            Outlook.OlDownloadState dlState;
    2.19  
    2.20              /* The Refresh/UI_Update process is a little more complicated here than other forms.
    2.21               * There are the following components:
    2.22 @@ -977,7 +973,7 @@
    2.23               *        be updated.
    2.24               * 
    2.25               *   4. MailItem_GetMirrorComplete
    2.26 -             *        This is the final step in updating the UI, after a mirror is located, it's contents
    2.27 +             *        This is the final step in updating the UI, after a mirror is located, its contents
    2.28               *        will be shown in the unencrypted preview.
    2.29               * 
    2.30               * The general calling sequence is as shown above 1->4 with each component calling the next.
    2.31 @@ -986,58 +982,9 @@
    2.32  
    2.33              // Ensure the tick method is not called more than once
    2.34              if ((this.isEnabled) &&
    2.35 -                (refreshOngoing == false))
    2.36 +                (this.cryptableMailItem != null))
    2.37              {
    2.38 -                this.refreshOngoing = true;
    2.39 -
    2.40 -                if (this.cryptableMailItem != null)
    2.41 -                {
    2.42 -                    // Attempt to get the download state
    2.43 -                    try
    2.44 -                    {
    2.45 -                        dlState = this.cryptableMailItem.DownloadState;
    2.46 -                    }
    2.47 -                    catch (Exception ex)
    2.48 -                    {
    2.49 -                        Log.Warning("TimerRefresh_Tick: Get DownloadState failed, " + ex.ToString());
    2.50 -
    2.51 -                        // Assume everything is downloaded, but try to download again as well
    2.52 -                        dlState = Outlook.OlDownloadState.olFullItem;
    2.53 -                        markForDownload = true;
    2.54 -                    }
    2.55 -
    2.56 -                    if (dlState == Outlook.OlDownloadState.olFullItem)
    2.57 -                    {
    2.58 -                        this.ImmediateRatingAndUIUpdate();
    2.59 -                    }
    2.60 -                    else
    2.61 -                    {
    2.62 -                        markForDownload = true;
    2.63 -                    }
    2.64 -
    2.65 -                    if (markForDownload)
    2.66 -                    {
    2.67 -                        // Try to mark the message for full download
    2.68 -                        try
    2.69 -                        {
    2.70 -                            this.cryptableMailItem.MarkForDownload = Outlook.OlRemoteStatus.olMarkedForDownload;
    2.71 -                            tryAgain = true;
    2.72 -                        }
    2.73 -                        catch (Exception ex)
    2.74 -                        {
    2.75 -                            Log.Warning("TimerRefresh_Tick: MarkForDownload failed, " + ex.ToString());
    2.76 -                        }
    2.77 -                    }
    2.78 -                }
    2.79 -
    2.80 -                // Set the timer to refresh again later automatically
    2.81 -                if (tryAgain)
    2.82 -                {
    2.83 -                    this.TimerRefresh.Interval = 100;
    2.84 -                    this.TimerRefresh.Enabled = true;
    2.85 -                }
    2.86 -
    2.87 -                this.refreshOngoing = false;
    2.88 +                this.ImmediateRatingAndUIUpdate();
    2.89              }
    2.90  
    2.91              return;