PasteImageAsHtmlImage.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "StdAfx.h"
  2. #include ".\pasteimageashtmlimage.h"
  3. #include "../../shared/TextConvert.h"
  4. CString g_csDIBImagePath = _T("");
  5. int g_nDIBImageName = 1;
  6. CPasteImageAsHtmlImage::CPasteImageAsHtmlImage(void)
  7. {
  8. }
  9. CPasteImageAsHtmlImage::~CPasteImageAsHtmlImage(void)
  10. {
  11. }
  12. bool CPasteImageAsHtmlImage::ConvertPathToHtmlImageTag(const CDittoInfo &DittoInfo, IClip *pClip)
  13. {
  14. bool bRet = false;
  15. IClipFormats *pFormats = pClip->Clips();
  16. if(pFormats)
  17. {
  18. if(g_csDIBImagePath.IsEmpty())
  19. {
  20. CreateLocalPath(true);
  21. }
  22. CString csIMG = _T("");
  23. IClipFormat *pCF_DIB = pFormats->FindFormatEx(CF_DIB);
  24. if(pCF_DIB != NULL)
  25. {
  26. CString csFile;
  27. csFile.Format(_T("%s\\%d.bmp"), g_csDIBImagePath, g_nDIBImageName);
  28. g_nDIBImageName++;
  29. LPVOID pvData = GlobalLock(pCF_DIB->Data());
  30. ULONG size = (ULONG)GlobalSize(pCF_DIB->Data());
  31. if(WriteDataToFile(csFile, pvData, size))
  32. {
  33. GlobalUnlock(pCF_DIB->Data());
  34. csIMG.Format(_T("<IMG src=\"file:///%s\">"), csFile);
  35. }
  36. else
  37. {
  38. GlobalUnlock(pCF_DIB->Data());
  39. }
  40. }
  41. else
  42. {
  43. IClipFormat *pHDrop = pFormats->FindFormatEx(CF_HDROP);
  44. if(pHDrop)
  45. {
  46. HDROP drop = (HDROP)GlobalLock((HDROP)pHDrop->Data());
  47. int nNumFiles = DragQueryFile(drop, -1, NULL, 0);
  48. TCHAR file[MAX_PATH];
  49. for(int nFile = 0; nFile < nNumFiles; nFile++)
  50. {
  51. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  52. {
  53. CString csOrigfile(file);
  54. CString csFile(file);
  55. csFile = csFile.MakeLower();
  56. if(csFile.Find(_T(".bmp")) != -1 ||
  57. csFile.Find(_T(".dib")) != -1 ||
  58. csFile.Find(_T(".jpg")) != -1 ||
  59. csFile.Find(_T(".jpeg")) != -1 ||
  60. csFile.Find(_T(".jpe")) != -1 ||
  61. csFile.Find(_T(".jfif")) != -1 ||
  62. csFile.Find(_T(".gif")) != -1 ||
  63. csFile.Find(_T(".tif")) != -1 ||
  64. csFile.Find(_T(".tiff")) != -1 ||
  65. csFile.Find(_T(".png")) != -1)
  66. {
  67. CString csFormat;
  68. csFormat.Format(_T("<IMG src=\"file:///%s\">"), csOrigfile);
  69. if(nFile < nNumFiles-1)
  70. {
  71. csFormat += _T("<br>");
  72. }
  73. csIMG += csFormat;
  74. }
  75. }
  76. }
  77. GlobalUnlock(pHDrop->Data());
  78. }
  79. }
  80. if(csIMG.IsEmpty() == FALSE)
  81. {
  82. pFormats->DeleteAll();
  83. CStringA utf8;
  84. CTextConvert::ConvertToUTF8(csIMG, utf8);
  85. pFormats->AddNew(DittoAddinHelpers::GetFormatID(_T("HTML Format")), DittoAddinHelpers::NewGlobalP(utf8.GetBuffer(), utf8.GetLength()));
  86. bRet = true;
  87. }
  88. }
  89. return bRet;
  90. }
  91. bool CPasteImageAsHtmlImage::WriteDataToFile(CString csPath, LPVOID data, ULONG size)
  92. {
  93. bool bRet = false;
  94. CFile file;
  95. CFileException ex;
  96. if(file.Open(csPath, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary, &ex))
  97. {
  98. BITMAPINFO *lpBI = (BITMAPINFO *)data;
  99. int nPaletteEntries = 1 << lpBI->bmiHeader.biBitCount;
  100. if(lpBI->bmiHeader.biBitCount > 8)
  101. nPaletteEntries = 0;
  102. else if( lpBI->bmiHeader.biClrUsed != 0 )
  103. nPaletteEntries = lpBI->bmiHeader.biClrUsed;
  104. BITMAPFILEHEADER BFH;
  105. memset(&BFH, 0, sizeof( BITMAPFILEHEADER));
  106. BFH.bfType = 'MB';
  107. BFH.bfSize = sizeof(BITMAPFILEHEADER) + size;
  108. BFH.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteEntries * sizeof(RGBQUAD);
  109. file.Write(&BFH, sizeof(BITMAPFILEHEADER));
  110. file.Write(data, size);
  111. file.Close();
  112. bRet = true;
  113. }
  114. else
  115. {
  116. CString csError;
  117. TCHAR exError[250];
  118. ex.GetErrorMessage(exError, sizeof(exError));
  119. csError.Format(_T("OutLookExpress Addin - Failed to write CF_DIB to file: %s, Error: %s"), csPath, exError);
  120. OutputDebugString(csPath);
  121. }
  122. return bRet;
  123. }
  124. bool CPasteImageAsHtmlImage::CleanupPastedImages()
  125. {
  126. bool bRet = false;
  127. if(g_csDIBImagePath.IsEmpty())
  128. {
  129. CreateLocalPath(false);
  130. }
  131. CFileFind find;
  132. BOOL bCont = find.FindFile(g_csDIBImagePath + _T("\\*"));
  133. while(bCont)
  134. {
  135. bCont = find.FindNextFile();
  136. DeleteFile(find.GetFilePath());
  137. }
  138. find.Close();
  139. bRet = RemoveDirectory(g_csDIBImagePath) == TRUE;
  140. return false;;
  141. }
  142. void CPasteImageAsHtmlImage::CreateLocalPath(bool bCreateDir)
  143. {
  144. g_csDIBImagePath = _wgetenv(_T("TMP"));;
  145. g_csDIBImagePath += _T("\\ditto");
  146. if(bCreateDir)
  147. {
  148. CreateDirectory(g_csDIBImagePath, NULL);
  149. }
  150. }