ReadOnlyFlag.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "StdAfx.h"
  2. #include "ReadOnlyFlag.h"
  3. #include "../../Shared/Tokenizer.h"
  4. #include "../../Shared/TextConvert.h"
  5. CReadOnlyFlag::CReadOnlyFlag(void)
  6. {
  7. }
  8. CReadOnlyFlag::~CReadOnlyFlag(void)
  9. {
  10. }
  11. bool CReadOnlyFlag::ResetReadOnlyFlag(const CDittoInfo &DittoInfo, IClip *pClip, bool resetFlag)
  12. {
  13. IClipFormats *pFormats = pClip->Clips();
  14. if(pFormats)
  15. {
  16. CStringArray lines;
  17. LoadHDropFiles(lines, pFormats);
  18. if(lines.GetSize() <= 0)
  19. {
  20. LoadUnicodeFiles(lines, pFormats);
  21. }
  22. if(lines.GetSize() <= 0)
  23. {
  24. LoadTextFiles(lines, pFormats);
  25. }
  26. for(int i = 0; i < lines.GetSize(); i++)
  27. {
  28. CString file = lines[i].TrimLeft(' ').TrimRight(' ').MakeLower();
  29. //Find the first occurance of a file // or \\ for a network file or a->z:\\ for a local files
  30. int pos = file.Find(_T("//"));
  31. if(pos >= 0)
  32. {
  33. file = file.Mid(pos);
  34. }
  35. else
  36. {
  37. pos = file.Find(_T("\\\\"));
  38. if(pos >= 0)
  39. {
  40. file = file.Mid(pos);
  41. }
  42. else
  43. {
  44. for(wchar_t drive = 'a'; drive <= 'z'; drive++)
  45. {
  46. CString csDrive(drive);
  47. csDrive += _T(":\\");
  48. pos = file.Find(csDrive);
  49. if(pos >= 0)
  50. {
  51. file = file.Mid(pos);
  52. break;
  53. }
  54. csDrive = drive;
  55. csDrive += _T(":/");
  56. pos = file.Find(csDrive);
  57. if(pos >= 0)
  58. {
  59. file = file.Mid(pos);
  60. break;
  61. }
  62. }
  63. }
  64. }
  65. BOOL success = FALSE;
  66. if(resetFlag)
  67. {
  68. success = ::SetFileAttributes(file, FILE_ATTRIBUTE_NORMAL);
  69. }
  70. else
  71. {
  72. success = ::SetFileAttributes(file, FILE_ATTRIBUTE_READONLY);
  73. }
  74. }
  75. }
  76. return true;
  77. }
  78. bool CReadOnlyFlag::LoadUnicodeFiles(CStringArray &lines, IClipFormats *pFormats)
  79. {
  80. IClipFormat *pFormat = pFormats->FindFormatEx(CF_UNICODETEXT);
  81. if(pFormat != NULL)
  82. {
  83. wchar_t *stringData = (wchar_t *)GlobalLock(pFormat->Data());
  84. if(stringData != NULL)
  85. {
  86. CString string(stringData);
  87. CString delim(_T("\r\n"));
  88. CTokenizer token(string, delim);
  89. CString line;
  90. while(token.Next(line))
  91. {
  92. lines.Add(line);
  93. }
  94. GlobalUnlock(pFormat->Data());
  95. }
  96. }
  97. return lines.GetSize() > 0;
  98. }
  99. bool CReadOnlyFlag::LoadTextFiles(CStringArray &lines, IClipFormats *pFormats)
  100. {
  101. IClipFormat *pFormat = pFormats->FindFormatEx(CF_TEXT);
  102. if(pFormat != NULL)
  103. {
  104. char *stringData = (char *)GlobalLock(pFormat->Data());
  105. if(stringData != NULL)
  106. {
  107. CStringA string(stringData);
  108. CStringW unicodeString(CTextConvert::AnsiToUnicode(string));
  109. CString delim(_T("\r\n"));
  110. CTokenizer token(unicodeString, delim);
  111. CString line;
  112. while(token.Next(line))
  113. {
  114. lines.Add(line);
  115. }
  116. GlobalUnlock(pFormat->Data());
  117. }
  118. }
  119. return lines.GetSize() > 0;
  120. }
  121. bool CReadOnlyFlag::LoadHDropFiles(CStringArray &lines, IClipFormats *pFormats)
  122. {
  123. IClipFormat *pFormat = pFormats->FindFormatEx(CF_HDROP);
  124. if(pFormat != NULL)
  125. {
  126. HDROP drop = (HDROP)GlobalLock(pFormat->Data());
  127. if(drop)
  128. {
  129. int nNumFiles = DragQueryFile(drop, -1, NULL, 0);
  130. TCHAR file[MAX_PATH];
  131. for(int nFile = 0; nFile < nNumFiles; nFile++)
  132. {
  133. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  134. {
  135. lines.Add(file);
  136. }
  137. }
  138. GlobalUnlock(pFormat->Data());
  139. }
  140. }
  141. return lines.GetSize() > 0;
  142. }