Add more logging and exceptions. Plus cosmetics. Redesign_Handshake_Dialog
authorThomas
Thu, 15 Jun 2017 13:18:35 +0200
branchRedesign_Handshake_Dialog
changeset 170419246dabd85b
parent 1703 d0199974ad59
child 1706 994383a99113
Add more logging and exceptions. Plus cosmetics.
UI/FormControlPreviewMessage.xaml.cs
     1.1 --- a/UI/FormControlPreviewMessage.xaml.cs	Wed Jun 14 17:48:22 2017 +0200
     1.2 +++ b/UI/FormControlPreviewMessage.xaml.cs	Thu Jun 15 13:18:35 2017 +0200
     1.3 @@ -48,21 +48,27 @@
     1.4          private const string RtfPictureTag = "pichgoal";
     1.5          private const string RtfPicturePlaceHolder = "__RTF_PICTURE_PLACE_HOLDER__";
     1.6  
     1.7 +        // Mapping mode used for the GdipEmfToWmfBits() method. See: https://msdn.microsoft.com/en-us/library/windows/desktop/dd162980(v=vs.85).aspx
     1.8          private static int MM_ANISOTROPIC = 8;
     1.9  
    1.10          // Temporary folder name for temporary images files         
    1.11 -        private string _tempFolder;        
    1.12 +        private string _TempFolder;
    1.13          private string TempFolder
    1.14          {
    1.15 -            get {
    1.16 -                if (string.IsNullOrEmpty(_tempFolder))
    1.17 -                    {
    1.18 -                        _tempFolder = GetTempFileFolder();
    1.19 -                    }
    1.20 -                    return _tempFolder;                    
    1.21 -                } 
    1.22 -        } 
    1.23 +            get
    1.24 +            {
    1.25 +                if (string.IsNullOrEmpty(_TempFolder))
    1.26 +                {
    1.27 +                    _TempFolder = GetTempFileFolder();
    1.28 +                }
    1.29  
    1.30 +                return _TempFolder;
    1.31 +            }
    1.32 +        }
    1.33 +
    1.34 +        /// <summary>
    1.35 +        /// Specifies options for the GdipEmfToWmfBits() method, which converts an Enhanced Metafile (EMF) metafile to a Windows Metafile Format (WMF) metafile.
    1.36 +        /// </summary>
    1.37          [Flags]
    1.38          private enum EmfToWmfBitsFlags
    1.39          {
    1.40 @@ -72,15 +78,44 @@
    1.41              EmfToWmfBitsFlagsNoXORClip = 0x00000004
    1.42          }
    1.43  
    1.44 +        /// <summary>
    1.45 +        /// Uses the EmfToWmfBits function in the GDI+ specification to convert a
    1.46 +        /// Enhanced Metafile to a Windows Metafile.
    1.47 +        /// See: http://www.pinvoke.net/default.aspx/gdiplus.gdipemftowmfbits
    1.48 +        /// </summary>
    1.49 +        /// <param name="hEmf">handle to the Enhanced Metafile to be converted</param>
    1.50 +        /// <param name="bufferSize">The size of the buffer used to store the Windows Metafile bits returned</param>
    1.51 +        /// <param name="buffer">An array of bytes used to hold the Windows Metafile bits returned</param>
    1.52 +        /// <param name="mappingMode">The mapping mode of the image. This control uses MM_ANISOTROPIC.</param>
    1.53 +        /// <param name="flags">Flags used to specify the format of the Windows Metafile returned</param>
    1.54          [DllImport("gdiplus.dll")]
    1.55 -        private static extern uint GdipEmfToWmfBits(IntPtr _hEmf, uint _bufferSize, byte[] _buffer, int _mappingMode, EmfToWmfBitsFlags _flags);
    1.56 +        private static extern uint GdipEmfToWmfBits(IntPtr hEmf, uint bufferSize, byte[] buffer, int mappingMode, EmfToWmfBitsFlags flags);
    1.57  
    1.58 +        /// <summary>
    1.59 +        /// Deletes an enhanced-format metafile or an enhanced-format metafile handle.
    1.60 +        /// See: http://www.pinvoke.net/default.aspx/gdi32.deleteenhmetafile
    1.61 +        /// </summary>
    1.62 +        /// <param name="hEmf">A handle to an enhanced metafile.</param>
    1.63 +        /// <returns>True if the method succeeds, otherwise false.</returns>
    1.64          [DllImport("gdi32.dll")]
    1.65          private static extern bool DeleteEnhMetaFile(IntPtr hEmf);
    1.66  
    1.67 +        /// <summary>
    1.68 +        /// Gets the handle of the window that has the clipboard open.
    1.69 +        /// See: http://www.pinvoke.net/default.aspx/user32/GetOpenClipboardWindow.html
    1.70 +        /// </summary>
    1.71 +        /// <returns>If the function succeeds, the handle to the window that has the clipboard open.</returns>
    1.72          [DllImport("user32.dll")]
    1.73          static extern IntPtr GetOpenClipboardWindow();
    1.74  
    1.75 +        /// <summary>
    1.76 +        /// Copies the specified window's title bar (if it has one) into a StringBuilder.
    1.77 +        /// See: http://www.pinvoke.net/default.aspx/user32/Getwindowtext.html
    1.78 +        /// </summary>
    1.79 +        /// <param name="hwnd">A handle to the window or control containing the text.</param>
    1.80 +        /// <param name="text">The StringBuilder that will receive the text.</param>
    1.81 +        /// <param name="count">The maximum number of characters to copy to the StringBuilder. If the text exceeds this limit, it is truncated.</param>
    1.82 +        /// <returns>If the function succeeds, the return value is the length, in characters, of the copied text.</returns>
    1.83          [DllImport("user32.dll")]
    1.84          static extern int GetWindowText(int hwnd, StringBuilder text, int count);
    1.85  
    1.86 @@ -104,7 +139,7 @@
    1.87              // connecting event handler where all controls and fields disposed in case this.IsVisible == false
    1.88              this.IsVisibleChanged += DisposeOfAllComponents;
    1.89          }
    1.90 -                
    1.91 +
    1.92          /**************************************************************
    1.93           * 
    1.94           * Property Accessors
    1.95 @@ -154,11 +189,11 @@
    1.96                  this.GridLayoutRoot = null;
    1.97  
    1.98                  // Delete temp directory for temporary images files
    1.99 -                if (!string.IsNullOrEmpty(_tempFolder))
   1.100 +                if (!string.IsNullOrEmpty(_TempFolder))
   1.101                  {
   1.102                      try
   1.103                      {
   1.104 -                        Directory.Delete(_tempFolder, true);
   1.105 +                        Directory.Delete(_TempFolder, true);
   1.106                      }
   1.107                      catch (Exception ex)
   1.108                      {
   1.109 @@ -169,14 +204,14 @@
   1.110          }
   1.111  
   1.112          /// <summary>
   1.113 -        /// Sets the rich text box text in the UI from the display state message.
   1.114 +        /// Sets the rich text box content in the UI from the display state message.
   1.115          /// </summary>
   1.116 -        private void SetRichTextBoxText()
   1.117 +        private void SetRichTextBoxContent()
   1.118          {
   1.119              // Set properties difficult to set in XAML
   1.120              this.RichTextBoxMessageBody.BackColor = System.Drawing.SystemColors.Window;
   1.121  
   1.122 -            // Set body text
   1.123 +            // If RTF content is not found, set plain text in RTF control
   1.124              if (string.IsNullOrEmpty(this.displayState?.Message?.LongMsgFormattedRtf))
   1.125              {
   1.126                  this.RichTextBoxMessageBody.Text = this.displayState?.Message?.LongMsg;
   1.127 @@ -200,8 +235,9 @@
   1.128                          5. If operation does not succeed only text without images is shown
   1.129          
   1.130                  */
   1.131 -                // html content
   1.132 -                string html = this.displayState?.Message?.LongMsgFormattedHtml; 
   1.133 +
   1.134 +                // If HTML content is not found, set RTF text without images and return
   1.135 +                string html = this.displayState?.Message?.LongMsgFormattedHtml;
   1.136                  if (string.IsNullOrEmpty(html))
   1.137                  {
   1.138                      this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.139 @@ -210,76 +246,101 @@
   1.140  
   1.141                  // Take Images names from HTML to seek them in Attachments 
   1.142                  List<string> imagesNames = new List<string>();
   1.143 -                foreach (Match m in Regex.Matches(html, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase | RegexOptions.Multiline))
   1.144 +                try
   1.145                  {
   1.146 -                    if (m.Groups?.Count > 1)
   1.147 +                    foreach (Match m in Regex.Matches(html, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase | RegexOptions.Multiline))
   1.148                      {
   1.149 -                        string src = m.Groups[1].Value;
   1.150 -                        if (string.IsNullOrEmpty(src) == false)
   1.151 +                        if (m.Groups?.Count > 1)
   1.152                          {
   1.153 -                            imagesNames.Add(src.Replace("cid:", ""));
   1.154 +                            string src = m.Groups[1].Value;
   1.155 +                            if (string.IsNullOrEmpty(src) == false)
   1.156 +                            {
   1.157 +                                imagesNames.Add(src.Replace("cid:", ""));
   1.158 +                            }
   1.159                          }
   1.160                      }
   1.161                  }
   1.162 -                // If no Images found in HTML assign just RTF with text and exit
   1.163 +                catch (Exception e)
   1.164 +                {
   1.165 +                    Log.Error("SetRichTextBoxContent: Error searching for attached images. " + e.Message);
   1.166 +                }
   1.167 +
   1.168 +                // If no images were found in HTML, set RTF text without images and return
   1.169                  if (imagesNames.Count < 1)
   1.170                  {
   1.171                      this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.172                      return;
   1.173                  }
   1.174  
   1.175 -                // Look for  attachments with images names found in HTML in previous step.
   1.176 -                // If image is found convert it to Bitmap and add it to List<Bitmap> 
   1.177 -                // If no images found assign just RTF with text and exit
   1.178 +                // Look for attachments with image names found in HTML in previous step.
   1.179 +                // If image is found, convert it to bitmap and add it to List<Bitmap> 
   1.180 +                List<Bitmap> images = new List<Bitmap>();
   1.181 +                try
   1.182 +                {
   1.183 +                    foreach (var attachmentImage in this.displayState?.Message?.Attachments?.Where(i => imagesNames.Contains(i.FileName)))
   1.184 +                    {
   1.185 +                        Bitmap img = null;
   1.186  
   1.187 -                List<Bitmap> images = new List<Bitmap>();
   1.188 -                foreach (var attachmentImage in this.displayState.Message.Attachments.Where(i => imagesNames.Contains(i.FileName)))
   1.189 -                {
   1.190 -                    Bitmap img = null;
   1.191 +                        try
   1.192 +                        {
   1.193 +                            img = (new ImageConverter()).ConvertFrom(attachmentImage.Data) as Bitmap;
   1.194 +                        }
   1.195 +                        catch (Exception e)
   1.196 +                        {
   1.197 +                            img = null;
   1.198 +                            Log.Verbose("SetRichTextBoxContent: Error converting attachment image to Bitmap. " + e.Message);
   1.199 +                        }
   1.200  
   1.201 -                    try
   1.202 -                    {
   1.203 -                        img = (new ImageConverter()).ConvertFrom(attachmentImage.Data) as Bitmap;
   1.204 -                    }
   1.205 -                    catch (Exception e)
   1.206 -                    {
   1.207 -                        img = null;
   1.208 -                        Log.Verbose("SetRichTextBoxText: Error converting attachment image to Bitmap. " + e.Message);
   1.209 -                    }
   1.210 -
   1.211 -                    if (img != null)
   1.212 -                    {
   1.213 -                        images.Add(img);
   1.214 +                        if (img != null)
   1.215 +                        {
   1.216 +                            images.Add(img);
   1.217 +                        }
   1.218                      }
   1.219                  }
   1.220 +                catch (Exception e)
   1.221 +                {
   1.222 +                    Log.Error("SetRichTextBoxContent: Error searching for attached images. " + e.Message);
   1.223 +                }
   1.224 +
   1.225 +                // If no images were found, set RTF text without images and return
   1.226                  if (images.Count < 1)
   1.227                  {
   1.228                      this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.229                      return;
   1.230                  }
   1.231  
   1.232 -
   1.233                  /* 
   1.234 -                        1. SaveMetafile function embeddes Bitmap image in WMF format using C++ functions from gdiplus.dll, gdi32.dll and converts it to  byte[] array 
   1.235 -                        2. byte[] array is saved to hard drive in system Temp folder.
   1.236 -                        3. SaveMetafile function returns path+file name to temporary stored on hard drive WMF image file in byte[] array
   1.237 +                        1. SaveMetafile function embeds bitmap image in WMF format using C++ functions from gdiplus.dll, gdi32.dll and converts it to byte[] 
   1.238 +                        2. Byte array is saved to hard drive in local Temp folder.
   1.239 +                        3. SaveMetafile function returns path+file name to temporary stored on hard drive WMF image file in byte array
   1.240                   */
   1.241                  List<string> wmfFileNames = new List<string>();
   1.242                  foreach (Bitmap img in images)
   1.243 -                {                    
   1.244 +                {
   1.245                      try
   1.246                      {
   1.247 -                        wmfFileNames.Add(SaveMetafile(img));
   1.248 +                        string wmfFileName = SaveMetafile(img);
   1.249 +
   1.250 +                        if (string.IsNullOrEmpty(wmfFileName) == false)
   1.251 +                        {
   1.252 +                            wmfFileNames.Add(wmfFileName);
   1.253 +                        }
   1.254                      }
   1.255                      catch (Exception ex)
   1.256                      {
   1.257 -                        Log.Error("Error during converting Bitmap image: " + ex.Message);
   1.258 -                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.259 -                        return;
   1.260 +                        Log.Error("SetRichTextBoxContent: Error converting Bitmap image: " + ex.Message);
   1.261                      }
   1.262                  }
   1.263  
   1.264 -                // Copy HTML to WebBrowser class 
   1.265 +                // If none of the images could be converted, set RTF text without images and return
   1.266 +                if (wmfFileNames.Count < 1)
   1.267 +                {
   1.268 +                    Log.Verbose("SetRichTextBoxContent: No images in WMF format available.");
   1.269 +                    this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.270 +                    return;
   1.271 +                }
   1.272 +
   1.273 +                // Create WebBrowser and copy HTML to it
   1.274                  using (System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser())
   1.275                  {
   1.276                      try
   1.277 @@ -287,13 +348,14 @@
   1.278                          wb.Navigate("about:blank");
   1.279                          wb.Document.Write(html);
   1.280                      }
   1.281 -                    catch
   1.282 +                    catch (Exception e)
   1.283                      {
   1.284 +                        Log.Error("SetRichTextBoxContent: Error writing HTML to web browser. " + e.Message);
   1.285                          this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.286                          return;
   1.287                      }
   1.288  
   1.289 -                    // back up data from clipboard
   1.290 +                    // Back up data from clipboard
   1.291                      object rtfFromClipboard = null;
   1.292                      string textFromClipboard = null;
   1.293                      BitmapSource imageFromClipboard = null;
   1.294 @@ -316,20 +378,31 @@
   1.295                              imageFromClipboard = Clipboard.GetImage();
   1.296                          }
   1.297                      }
   1.298 -                    catch
   1.299 +                    catch (Exception e)
   1.300                      {
   1.301 +                        Log.Error("SetRichTextBoxContent: Error backing up clipboard. " + e.Message);
   1.302                          this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.303                          return;
   1.304                      }
   1.305 -                    // Copy from WebBrowser class to Clipboard
   1.306 -                    wb.Document.ExecCommand("SelectAll", false, null);
   1.307 -                    wb.Document.ExecCommand("Copy", false, null);
   1.308  
   1.309 -                    string rtf= string.Empty;
   1.310 +                    // Copy data from WebBrowser to Clipboard
   1.311 +                    try
   1.312 +                    {
   1.313 +                        wb.Document.ExecCommand("SelectAll", false, null);
   1.314 +                        wb.Document.ExecCommand("Copy", false, null);
   1.315 +                    }
   1.316 +                    catch (Exception e)
   1.317 +                    {
   1.318 +                        Log.Error("SetRichTextBoxContent: Error copying from web browser to clipboard. " + e.Message);
   1.319 +                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.320 +                        return;
   1.321 +                    }
   1.322 +
   1.323 +                    string rtf = string.Empty;
   1.324  
   1.325                      try
   1.326                      {
   1.327 -                        // Copy from Clipboard to "rtf" variable 
   1.328 +                        // Get RTF from Clipboard
   1.329                          rtf = System.Windows.Forms.Clipboard.GetData(DataFormats.Rtf) as string;
   1.330  
   1.331                          // Restore Clipboard backup
   1.332 @@ -349,8 +422,9 @@
   1.333                              }
   1.334                          }
   1.335                      }
   1.336 -                    catch (Exception ex)
   1.337 +                    catch (Exception e)
   1.338                      {
   1.339 +                        Log.Error("SetRichTextBoxContent: Error getting rtf from clipboard. " + e.Message);
   1.340                          this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.341  
   1.342                          // If Exception was caused by another application which uses clipboard than GetOpenClipboardWindowText() will return information of this application. Uses user32.dll 
   1.343 @@ -364,41 +438,34 @@
   1.344                          // Replace WMF picture headers with RtfPicturePlaceHolder  
   1.345                          rtf = ReplaceWmfHeaders(rtf);
   1.346                      }
   1.347 -                    catch (System.OutOfMemoryException ex)
   1.348 +                    catch (Exception e)
   1.349                      {
   1.350 +                        Log.Error("SetRichTextBoxContent: Error replacing WMF headers. " + e.Message);
   1.351                          this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.352                          return;
   1.353                      }
   1.354 -                    catch (Exception ex)
   1.355 -                    {
   1.356 -                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.357 -                        return;
   1.358 -                    }
   1.359 -                    
   1.360 +
   1.361                      try
   1.362                      {
   1.363                          //  Replace RtfPicturePlaceHolder with images hex strings taken from files                        
   1.364                          for (int i = 0; i < wmfFileNames.Count; i++)
   1.365 -                        {                            
   1.366 -                            ReplaceFirstFromFile(ref rtf, RtfPicturePlaceHolder, wmfFileNames[i]);                            
   1.367 +                        {
   1.368 +                            ReplaceFirstFromFile(ref rtf, RtfPicturePlaceHolder, wmfFileNames[i]);
   1.369                          }
   1.370                      }
   1.371 -                    catch (System.OutOfMemoryException ex)
   1.372 +                    catch (Exception e)
   1.373                      {
   1.374 +                        Log.Error("SetRichTextBoxContent: Error replacing RtfPicturePlaceHolder. " + e.Message);
   1.375                          this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.376                          return;
   1.377                      }
   1.378 -                    catch (Exception ex)
   1.379 -                    {
   1.380 -                        this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.381 -                        return;
   1.382 -                    }
   1.383 -                    
   1.384 +
   1.385                      if (string.IsNullOrEmpty(rtf))
   1.386                      {
   1.387                          this.RichTextBoxMessageBody.Rtf = this.displayState?.Message?.LongMsgFormattedRtf;
   1.388                          return;
   1.389                      }
   1.390 +
   1.391                      // Assign RichTextBoxMessageBody.Rtf with rtf
   1.392                      this.RichTextBoxMessageBody.Rtf = rtf;
   1.393                  }
   1.394 @@ -422,17 +489,17 @@
   1.395                  if (index == -1)
   1.396                  {
   1.397                      break;
   1.398 -                }                   
   1.399 +                }
   1.400                  picturesStartIndexes.Add(index);
   1.401              }
   1.402  
   1.403              List<string> stringsToReplace = new List<string>();
   1.404  
   1.405 -            foreach(int startIndex in picturesStartIndexes)
   1.406 +            foreach (int startIndex in picturesStartIndexes)
   1.407              {
   1.408                  int pictureStartIndex = rtf.IndexOf("010009000003", startIndex);
   1.409                  int PictureEndIndex = rtf.IndexOf("}", pictureStartIndex);
   1.410 -                if(pictureStartIndex<0 || PictureEndIndex < 0)
   1.411 +                if (pictureStartIndex < 0 || PictureEndIndex < 0)
   1.412                  {
   1.413                      continue;
   1.414                  }
   1.415 @@ -471,19 +538,19 @@
   1.416                      return;
   1.417                  }
   1.418                  StringBuilder sb = new StringBuilder(text.Substring(0, pos));
   1.419 -                
   1.420 +
   1.421                  // Read WMF image from file and convert it to hex strings which is inserted in RTF
   1.422 -                SoapHexBinary shb = new SoapHexBinary(File.ReadAllBytes(fileName));                                
   1.423 +                SoapHexBinary shb = new SoapHexBinary(File.ReadAllBytes(fileName));
   1.424                  sb.Append(shb.ToString());
   1.425                  shb = null;
   1.426                  sb.Append(text.Substring(pos + search.Length));
   1.427                  text = null;
   1.428 -                text = sb.ToString(); 
   1.429 +                text = sb.ToString();
   1.430                  sb = null;
   1.431              }
   1.432 -            catch (System.OutOfMemoryException ex)
   1.433 +            catch (Exception e)
   1.434              {
   1.435 -
   1.436 +                Log.Error("ReplaceFirstFromFile: Error. " + e.Message);
   1.437              }
   1.438          }
   1.439  
   1.440 @@ -491,7 +558,7 @@
   1.441          /// Make Bitmap image embedded in Metafile format and save it to temporary folder
   1.442          /// GDI C++ libraries used. 
   1.443          /// </summary>
   1.444 -        private string SaveMetafile (Bitmap image)
   1.445 +        private string SaveMetafile(Bitmap image)
   1.446          {
   1.447              Metafile metafile = null;
   1.448  
   1.449 @@ -521,37 +588,38 @@
   1.450                  g.DrawImage(image, 0, 0);
   1.451              }
   1.452  
   1.453 -            IntPtr _hEmf = metafile.GetHenhmetafile();
   1.454 -            uint _bufferSize = GdipEmfToWmfBits(_hEmf, 0, null, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
   1.455 -            byte[] _buffer = new byte[_bufferSize];
   1.456 -            GdipEmfToWmfBits(_hEmf, _bufferSize, _buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
   1.457 -            DeleteEnhMetaFile(_hEmf);
   1.458 -            
   1.459 -            string fileName = Guid.NewGuid().ToString();            
   1.460 +            IntPtr hEmf = metafile.GetHenhmetafile();
   1.461 +            uint bufferSize = GdipEmfToWmfBits(hEmf, 0, null, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
   1.462 +            byte[] buffer = new byte[bufferSize];
   1.463 +            GdipEmfToWmfBits(hEmf, bufferSize, buffer, MM_ANISOTROPIC, EmfToWmfBitsFlags.EmfToWmfBitsFlagsDefault);
   1.464 +            DeleteEnhMetaFile(hEmf);
   1.465 +
   1.466 +            string fileName = Guid.NewGuid().ToString();
   1.467              string tempFolderAndFileName = Path.Combine(this.TempFolder, fileName);
   1.468              try
   1.469              {
   1.470 -                File.WriteAllBytes(tempFolderAndFileName, _buffer);
   1.471 -                _buffer = null;               
   1.472 +                File.WriteAllBytes(tempFolderAndFileName, buffer);
   1.473 +                buffer = null;
   1.474              }
   1.475 -            catch(Exception ex)
   1.476 +            catch (Exception e)
   1.477              {
   1.478 +                Log.Error("SaveMetaFile: Error writing to disk. " + e.Message);
   1.479                  return null;
   1.480              }
   1.481  
   1.482              return tempFolderAndFileName;
   1.483          }
   1.484  
   1.485 -
   1.486          /// <summary>
   1.487          ///  Creates temporary folder using Path.GetTempFileName
   1.488          /// </summary>
   1.489 -        private static string GetTempFileFolder()
   1.490 +        private string GetTempFileFolder()
   1.491          {
   1.492              // Create temp directory
   1.493              string tempFileName = Path.GetTempFileName();
   1.494              string tempDir = tempFileName + ".dir";
   1.495 -            Directory.CreateDirectory(tempDir);            
   1.496 +            Directory.CreateDirectory(tempDir);
   1.497 +
   1.498              // GetTempFileName() creates an empty file which is not needed
   1.499              try
   1.500              {
   1.501 @@ -561,8 +629,10 @@
   1.502              {
   1.503                  Log.Error("Could not delete temp file. " + e.Message);
   1.504              }
   1.505 +
   1.506              return tempDir;
   1.507          }
   1.508 +
   1.509          /**************************************************************
   1.510           * 
   1.511           * Event Handling
   1.512 @@ -580,7 +650,7 @@
   1.513                   * Any changes to the LongMsgFormattedRtf property directly are ignored.
   1.514                   * However, setting the Message is the only use case in code.
   1.515                   */
   1.516 -                this.SetRichTextBoxText();
   1.517 +                this.SetRichTextBoxContent();
   1.518              }
   1.519  
   1.520              this.PropertyChanged?.Invoke(this, e);
   1.521 @@ -1028,13 +1098,7 @@
   1.522                  this.CalcDependentProperties();
   1.523  
   1.524                  return;
   1.525 -            }            
   1.526 +            }
   1.527          }
   1.528 -
   1.529 -        private void RichTextBoxMessageBody_Disposed(object sender, EventArgs e)
   1.530 -        {
   1.531 -           
   1.532 -        }
   1.533 -
   1.534      }
   1.535  }