PasteImageAsHtmlImage.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = CTextConvert::UnicodeToUTF8(csIMG);
  84. pFormats->AddNew(DittoAddinHelpers::GetFormatID(_T("HTML Format")), DittoAddinHelpers::NewGlobalP(utf8.GetBuffer(), utf8.GetLength()));
  85. bRet = true;
  86. }
  87. }
  88. return bRet;
  89. }
  90. bool CPasteImageAsHtmlImage::WriteDataToFile(CString csPath, LPVOID data, ULONG size)
  91. {
  92. bool bRet = false;
  93. CFile file;
  94. CFileException ex;
  95. if(file.Open(csPath, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary, &ex))
  96. {
  97. BITMAPINFO *lpBI = (BITMAPINFO *)data;
  98. int nPaletteEntries = 1 << lpBI->bmiHeader.biBitCount;
  99. if(lpBI->bmiHeader.biBitCount > 8)
  100. nPaletteEntries = 0;
  101. else if( lpBI->bmiHeader.biClrUsed != 0 )
  102. nPaletteEntries = lpBI->bmiHeader.biClrUsed;
  103. BITMAPFILEHEADER BFH;
  104. memset(&BFH, 0, sizeof( BITMAPFILEHEADER));
  105. BFH.bfType = 'MB';
  106. BFH.bfSize = sizeof(BITMAPFILEHEADER) + size;
  107. BFH.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + nPaletteEntries * sizeof(RGBQUAD);
  108. file.Write(&BFH, sizeof(BITMAPFILEHEADER));
  109. file.Write(data, size);
  110. file.Close();
  111. bRet = true;
  112. }
  113. else
  114. {
  115. CString csError;
  116. TCHAR exError[250];
  117. ex.GetErrorMessage(exError, sizeof(exError));
  118. csError.Format(_T("OutLookExpress Addin - Failed to write CF_DIB to file: %s, Error: %s"), csPath, exError);
  119. OutputDebugString(csPath);
  120. }
  121. return bRet;
  122. }
  123. bool CPasteImageAsHtmlImage::CleanupPastedImages()
  124. {
  125. bool bRet = false;
  126. if(g_csDIBImagePath.IsEmpty())
  127. {
  128. CreateLocalPath(false);
  129. }
  130. CFileFind find;
  131. BOOL bCont = find.FindFile(g_csDIBImagePath + _T("\\*"));
  132. while(bCont)
  133. {
  134. bCont = find.FindNextFile();
  135. DeleteFile(find.GetFilePath());
  136. }
  137. find.Close();
  138. bRet = RemoveDirectory(g_csDIBImagePath) == TRUE;
  139. return false;;
  140. }
  141. void CPasteImageAsHtmlImage::CreateLocalPath(bool bCreateDir)
  142. {
  143. g_csDIBImagePath = _wgetenv(_T("TMP"));;
  144. g_csDIBImagePath += _T("\\ditto");
  145. if(bCreateDir)
  146. {
  147. CreateDirectory(g_csDIBImagePath, NULL);
  148. }
  149. }