Misc.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "Misc.h"
  4. #include "OptionsSheet.h"
  5. #include "shared/TextConvert.h"
  6. #include "AlphaBlend.h"
  7. #include "Tlhelp32.h"
  8. CString GetIPAddress()
  9. {
  10. WORD wVersionRequested;
  11. WSADATA wsaData;
  12. char name[255];
  13. CString IP;
  14. PHOSTENT hostinfo;
  15. wVersionRequested = MAKEWORD(2,0);
  16. if (WSAStartup(wVersionRequested, &wsaData)==0)
  17. {
  18. if(gethostname(name, sizeof(name))==0)
  19. {
  20. if((hostinfo=gethostbyname(name)) != NULL)
  21. {
  22. IP = inet_ntoa(*(struct in_addr*)* hostinfo->h_addr_list);
  23. }
  24. }
  25. WSACleanup();
  26. }
  27. IP.MakeUpper();
  28. return IP;
  29. }
  30. CString GetComputerName()
  31. {
  32. TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH+1] = _T("");
  33. DWORD Size=MAX_COMPUTERNAME_LENGTH+1;
  34. GetComputerName(ComputerName, &Size);
  35. CString cs(ComputerName);
  36. cs.MakeUpper();
  37. return cs;
  38. }
  39. void AppendToFile(const TCHAR* fn, const TCHAR* msg)
  40. {
  41. #ifdef _UNICODE
  42. FILE *file = _wfopen(fn, _T("a"));
  43. #else
  44. FILE *file = fopen(fn, _T("a"));
  45. #endif
  46. ASSERT( file );
  47. if(file != NULL)
  48. {
  49. #ifdef _UNICODE
  50. fwprintf(file, _T("%s"), msg);
  51. #else
  52. fprintf(file, _T("%s"),msg);
  53. #endif
  54. fclose(file);
  55. }
  56. }
  57. void log(const TCHAR* msg, bool bFromSendRecieve, CString csFile, long lLine)
  58. {
  59. ASSERT(AfxIsValidString(msg));
  60. SYSTEMTIME st;
  61. GetSystemTime(&st);
  62. CString csText;
  63. csText.Format(_T("[%d/%d/%d %02d:%02d:%02d.%03d - "), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  64. CString csFileLine;
  65. csFile = GetFileName(csFile);
  66. csFileLine.Format(_T("%s %d] "), csFile, lLine);
  67. csText += csFileLine;
  68. csText += msg;
  69. csText += "\n";
  70. #ifndef _DEBUG
  71. if(CGetSetOptions::m_bOutputDebugString)
  72. #endif
  73. {
  74. OutputDebugString(csText);
  75. }
  76. #ifndef _DEBUG
  77. if(!bFromSendRecieve)
  78. {
  79. if(!g_Opt.m_bEnableDebugLogging)
  80. return;
  81. }
  82. #endif
  83. CString csExeFile = CGetSetOptions::GetPath(PATH_LOG_FILE);
  84. csExeFile += "Ditto.log";
  85. AppendToFile(csExeFile, csText);
  86. }
  87. void logsendrecieveinfo(CString cs, CString csFile, long lLine)
  88. {
  89. if(g_Opt.m_bLogSendReceiveErrors)
  90. log(cs, true, csFile, lLine);
  91. }
  92. CString GetErrorString( int err )
  93. {
  94. CString str;
  95. LPVOID lpMsgBuf;
  96. ::FormatMessage(
  97. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  98. NULL,
  99. err,
  100. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  101. (LPTSTR) &lpMsgBuf,
  102. 0,
  103. NULL
  104. );
  105. str = (LPCTSTR) lpMsgBuf;
  106. // Display the string.
  107. // ::MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
  108. ::LocalFree( lpMsgBuf );
  109. return str;
  110. }
  111. CString StrF(const TCHAR * pszFormat, ...)
  112. {
  113. ASSERT( AfxIsValidString( pszFormat ) );
  114. CString str;
  115. va_list argList;
  116. va_start( argList, pszFormat );
  117. str.FormatV( pszFormat, argList );
  118. va_end( argList );
  119. return str;
  120. }
  121. BYTE GetEscapeChar( BYTE ch )
  122. {
  123. switch(ch)
  124. {
  125. case '\'': return '\''; // Single quotation mark (') = 39 or 0x27
  126. case '\"': return '\"'; // Double quotation mark (") = 34 or 0x22
  127. case '?': return '\?'; // Question mark (?) = 63 or 0x3f
  128. case '\\': return '\\'; // Backslash (\) = 92 or 0x5c
  129. case 'a': return '\a'; // Alert (BEL) = 7
  130. case 'b': return '\b'; // Backspace (BS) = 8
  131. case 'f': return '\f'; // Formfeed (FF) = 12 or 0x0c
  132. case 'n': return '\n'; // Newline (NL or LF) = 10 or 0x0a
  133. case 'r': return '\r'; // Carriage Return (CR) = 13 or 0x0d
  134. case 't': return '\t'; // Horizontal tab (HT) = 9
  135. case 'v': return '\v'; // Vertical tab (VT) = 11 or 0x0b
  136. case '0': return '\0'; // Null character (NUL) = 0
  137. }
  138. return 0; // invalid
  139. }
  140. CString RemoveEscapes( const TCHAR* str )
  141. {
  142. ASSERT( str );
  143. CString ret;
  144. TCHAR* pSrc = (TCHAR*) str;
  145. TCHAR* pDest = ret.GetBuffer((int)STRLEN(pSrc));
  146. TCHAR* pStart = pDest;
  147. while( *pSrc != '\0' )
  148. {
  149. if( *pSrc == '\\' )
  150. {
  151. pSrc++;
  152. *pDest = GetEscapeChar((BYTE)pSrc );
  153. }
  154. else
  155. *pDest = *pSrc;
  156. pSrc++;
  157. pDest++;
  158. }
  159. ret.ReleaseBuffer((int)(pDest - pStart));
  160. return ret;
  161. }
  162. CString GetWndText( HWND hWnd )
  163. {
  164. CString text;
  165. if( !IsWindow(hWnd) )
  166. return "! NOT A VALID WINDOW !";
  167. CWnd* pWnd = CWnd::FromHandle(hWnd);
  168. pWnd->GetWindowText(text);
  169. return text;
  170. }
  171. bool IsAppWnd( HWND hWnd )
  172. {
  173. DWORD dwMyPID = ::GetCurrentProcessId();
  174. DWORD dwTestPID;
  175. ::GetWindowThreadProcessId( hWnd, &dwTestPID );
  176. return dwMyPID == dwTestPID;
  177. }
  178. /*----------------------------------------------------------------------------*\
  179. Global Memory Helper Functions
  180. \*----------------------------------------------------------------------------*/
  181. // make sure the given HGLOBAL is valid.
  182. BOOL IsValid(HGLOBAL hGlobal)
  183. {
  184. void* pvData = ::GlobalLock(hGlobal);
  185. ::GlobalUnlock(hGlobal);
  186. return (pvData != NULL);
  187. }
  188. // asserts if hDest isn't big enough
  189. void CopyToGlobalHP(HGLOBAL hDest, LPVOID pBuf, SIZE_T ulBufLen)
  190. {
  191. ASSERT(hDest && pBuf && ulBufLen);
  192. LPVOID pvData = GlobalLock(hDest);
  193. ASSERT(pvData);
  194. SIZE_T size = GlobalSize(hDest);
  195. ASSERT(size >= ulBufLen); // assert if hDest isn't big enough
  196. memcpy(pvData, pBuf, ulBufLen);
  197. GlobalUnlock(hDest);
  198. }
  199. void CopyToGlobalHH(HGLOBAL hDest, HGLOBAL hSource, SIZE_T ulBufLen)
  200. {
  201. ASSERT(hDest && hSource && ulBufLen);
  202. LPVOID pvData = GlobalLock(hSource);
  203. ASSERT(pvData );
  204. SIZE_T size = GlobalSize(hSource);
  205. ASSERT(size >= ulBufLen); // assert if hSource isn't big enough
  206. CopyToGlobalHP(hDest, pvData, ulBufLen);
  207. GlobalUnlock(hSource);
  208. }
  209. HGLOBAL NewGlobalP(LPVOID pBuf, SIZE_T nLen)
  210. {
  211. ASSERT(pBuf && nLen);
  212. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  213. ASSERT(hDest );
  214. CopyToGlobalHP(hDest, pBuf, nLen);
  215. return hDest;
  216. }
  217. HGLOBAL NewGlobal(SIZE_T nLen)
  218. {
  219. ASSERT(nLen);
  220. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  221. return hDest;
  222. }
  223. HGLOBAL NewGlobalH(HGLOBAL hSource, SIZE_T nLen)
  224. {
  225. ASSERT(hSource && nLen);
  226. LPVOID pvData = GlobalLock(hSource);
  227. HGLOBAL hDest = NewGlobalP(pvData, nLen);
  228. GlobalUnlock(hSource);
  229. return hDest;
  230. }
  231. int CompareGlobalHP(HGLOBAL hLeft, LPVOID pBuf, SIZE_T ulBufLen)
  232. {
  233. ASSERT(hLeft && pBuf && ulBufLen);
  234. LPVOID pvData = GlobalLock(hLeft);
  235. ASSERT(pvData);
  236. ASSERT(ulBufLen <= GlobalSize(hLeft));
  237. int result = memcmp(pvData, pBuf, ulBufLen);
  238. GlobalUnlock(hLeft);
  239. return result;
  240. }
  241. int CompareGlobalHH( HGLOBAL hLeft, HGLOBAL hRight, SIZE_T ulBufLen)
  242. {
  243. ASSERT(hLeft && hRight && ulBufLen);
  244. ASSERT(ulBufLen <= GlobalSize(hRight));
  245. LPVOID pvData = GlobalLock(hRight);
  246. ASSERT(pvData);
  247. int result = CompareGlobalHP(hLeft, pvData, ulBufLen);
  248. GlobalUnlock(hLeft);
  249. return result;
  250. }
  251. //Do not change these these are stored in the database
  252. CLIPFORMAT GetFormatID(LPCTSTR cbName)
  253. {
  254. if(STRCMP(cbName, _T("CF_TEXT")) == 0)
  255. return CF_TEXT;
  256. else if(STRCMP(cbName, _T("CF_METAFILEPICT")) == 0)
  257. return CF_METAFILEPICT;
  258. else if(STRCMP(cbName, _T("CF_SYLK")) == 0)
  259. return CF_SYLK;
  260. else if(STRCMP(cbName, _T("CF_DIF")) == 0)
  261. return CF_DIF;
  262. else if(STRCMP(cbName, _T("CF_TIFF")) == 0)
  263. return CF_TIFF;
  264. else if(STRCMP(cbName, _T("CF_OEMTEXT")) == 0)
  265. return CF_OEMTEXT;
  266. else if(STRCMP(cbName, _T("CF_DIB")) == 0)
  267. return CF_DIB;
  268. else if(STRCMP(cbName, _T("CF_PALETTE")) == 0)
  269. return CF_PALETTE;
  270. else if(STRCMP(cbName, _T("CF_PENDATA")) == 0)
  271. return CF_PENDATA;
  272. else if(STRCMP(cbName, _T("CF_RIFF")) == 0)
  273. return CF_RIFF;
  274. else if(STRCMP(cbName, _T("CF_WAVE")) == 0)
  275. return CF_WAVE;
  276. else if(STRCMP(cbName, _T("CF_UNICODETEXT")) == 0)
  277. return CF_UNICODETEXT;
  278. else if(STRCMP(cbName, _T("CF_ENHMETAFILE")) == 0)
  279. return CF_ENHMETAFILE;
  280. else if(STRCMP(cbName, _T("CF_HDROP")) == 0)
  281. return CF_HDROP;
  282. else if(STRCMP(cbName, _T("CF_LOCALE")) == 0)
  283. return CF_LOCALE;
  284. else if(STRCMP(cbName, _T("CF_OWNERDISPLAY")) == 0)
  285. return CF_OWNERDISPLAY;
  286. else if(STRCMP(cbName, _T("CF_DSPTEXT")) == 0)
  287. return CF_DSPTEXT;
  288. else if(STRCMP(cbName, _T("CF_DSPBITMAP")) == 0)
  289. return CF_DSPBITMAP;
  290. else if(STRCMP(cbName, _T("CF_DSPMETAFILEPICT")) == 0)
  291. return CF_DSPMETAFILEPICT;
  292. else if(STRCMP(cbName, _T("CF_DSPENHMETAFILE")) == 0)
  293. return CF_DSPENHMETAFILE;
  294. return ::RegisterClipboardFormat(cbName);
  295. }
  296. //Do not change these these are stored in the database
  297. CString GetFormatName(CLIPFORMAT cbType)
  298. {
  299. switch(cbType)
  300. {
  301. case CF_TEXT:
  302. return _T("CF_TEXT");
  303. case CF_BITMAP:
  304. return _T("CF_BITMAP");
  305. case CF_METAFILEPICT:
  306. return _T("CF_METAFILEPICT");
  307. case CF_SYLK:
  308. return _T("CF_SYLK");
  309. case CF_DIF:
  310. return _T("CF_DIF");
  311. case CF_TIFF:
  312. return _T("CF_TIFF");
  313. case CF_OEMTEXT:
  314. return _T("CF_OEMTEXT");
  315. case CF_DIB:
  316. return _T("CF_DIB");
  317. case CF_PALETTE:
  318. return _T("CF_PALETTE");
  319. case CF_PENDATA:
  320. return _T("CF_PENDATA");
  321. case CF_RIFF:
  322. return _T("CF_RIFF");
  323. case CF_WAVE:
  324. return _T("CF_WAVE");
  325. case CF_UNICODETEXT:
  326. return _T("CF_UNICODETEXT");
  327. case CF_ENHMETAFILE:
  328. return _T("CF_ENHMETAFILE");
  329. case CF_HDROP:
  330. return _T("CF_HDROP");
  331. case CF_LOCALE:
  332. return _T("CF_LOCALE");
  333. case CF_OWNERDISPLAY:
  334. return _T("CF_OWNERDISPLAY");
  335. case CF_DSPTEXT:
  336. return _T("CF_DSPTEXT");
  337. case CF_DSPBITMAP:
  338. return _T("CF_DSPBITMAP");
  339. case CF_DSPMETAFILEPICT:
  340. return _T("CF_DSPMETAFILEPICT");
  341. case CF_DSPENHMETAFILE:
  342. return _T("CF_DSPENHMETAFILE");
  343. default:
  344. //Not a default type get the name from the clipboard
  345. if (cbType != 0)
  346. {
  347. TCHAR szFormat[256];
  348. GetClipboardFormatName(cbType, szFormat, 256);
  349. return szFormat;
  350. }
  351. break;
  352. }
  353. return "ERROR";
  354. }
  355. CString GetFilePath(CString csFileName)
  356. {
  357. long lSlash = csFileName.ReverseFind('\\');
  358. if(lSlash > -1)
  359. {
  360. csFileName = csFileName.Left(lSlash + 1);
  361. }
  362. return csFileName;
  363. }
  364. CString GetFileName(CString csFileName)
  365. {
  366. long lSlash = csFileName.ReverseFind('\\');
  367. if(lSlash > -1)
  368. {
  369. csFileName = csFileName.Right(csFileName.GetLength() - lSlash - 1);
  370. }
  371. return csFileName;
  372. }
  373. /****************************************************************************************************
  374. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  375. ***************************************************************************************************/
  376. typedef struct
  377. {
  378. long lFlags; // Flags
  379. LPRECT pVirtualRect; // Ptr to rect that receives the results, or the src of the monitor search method
  380. int iMonitor; // Ndx to the mointor to look at, -1 for all, -or- result of the monitor search method
  381. int nMonitorCount; // Total number of monitors found, -1 for monitor search method
  382. } MONITOR_ENUM_PARAM;
  383. #define MONITOR_SEARCH_METOHD 0x00000001
  384. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  385. {
  386. // Typecast param
  387. MONITOR_ENUM_PARAM* pParam = (MONITOR_ENUM_PARAM*)dwData;
  388. if(pParam)
  389. {
  390. // If a dest rect was passed
  391. if(pParam->pVirtualRect)
  392. {
  393. // If MONITOR_SEARCH_METOHD then we are being asked for the index of the monitor
  394. // that the rect falls inside of
  395. if(pParam->lFlags & MONITOR_SEARCH_METOHD)
  396. {
  397. if( (pParam->pVirtualRect->right < lprcMonitor->left) ||
  398. (pParam->pVirtualRect->left > lprcMonitor->right) ||
  399. (pParam->pVirtualRect->bottom < lprcMonitor->top) ||
  400. (pParam->pVirtualRect->top > lprcMonitor->bottom))
  401. {
  402. // Nothing
  403. }
  404. else
  405. {
  406. // This is the one
  407. pParam->iMonitor = pParam->nMonitorCount;
  408. // Stop the enumeration
  409. return FALSE;
  410. }
  411. }
  412. else
  413. {
  414. if(pParam->iMonitor == pParam->nMonitorCount)
  415. {
  416. *pParam->pVirtualRect = *lprcMonitor;
  417. }
  418. else
  419. if(pParam->iMonitor == -1)
  420. {
  421. pParam->pVirtualRect->left = min(pParam->pVirtualRect->left, lprcMonitor->left);
  422. pParam->pVirtualRect->top = min(pParam->pVirtualRect->top, lprcMonitor->top);
  423. pParam->pVirtualRect->right = max(pParam->pVirtualRect->right, lprcMonitor->right);
  424. pParam->pVirtualRect->bottom = max(pParam->pVirtualRect->bottom, lprcMonitor->bottom);
  425. }
  426. }
  427. }
  428. // Up the count if necessary
  429. pParam->nMonitorCount++;
  430. }
  431. return TRUE;
  432. }
  433. int GetScreenWidth(void)
  434. {
  435. OSVERSIONINFO OS_Version_Info;
  436. DWORD dwPlatform = 0;
  437. if(GetVersionEx(&OS_Version_Info) != 0)
  438. {
  439. dwPlatform = OS_Version_Info.dwPlatformId;
  440. }
  441. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  442. {
  443. int width, height;
  444. width = GetSystemMetrics(SM_CXSCREEN);
  445. height = GetSystemMetrics(SM_CYSCREEN);
  446. switch(width)
  447. {
  448. default:
  449. case 640:
  450. case 800:
  451. case 1024:
  452. return(width);
  453. case 1280:
  454. if(height == 480)
  455. {
  456. return(width / 2);
  457. }
  458. return(width);
  459. case 1600:
  460. if(height == 600)
  461. {
  462. return(width / 2);
  463. }
  464. return(width);
  465. case 2048:
  466. if(height == 768)
  467. {
  468. return(width / 2);
  469. }
  470. return(width);
  471. }
  472. }
  473. else
  474. {
  475. return(GetSystemMetrics(SM_CXSCREEN));
  476. }
  477. }
  478. int GetScreenHeight(void)
  479. {
  480. OSVERSIONINFO OS_Version_Info;
  481. DWORD dwPlatform = 0;
  482. if(GetVersionEx(&OS_Version_Info) != 0)
  483. {
  484. dwPlatform = OS_Version_Info.dwPlatformId;
  485. }
  486. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  487. {
  488. int width, height;
  489. width = GetSystemMetrics(SM_CXSCREEN);
  490. height = GetSystemMetrics(SM_CYSCREEN);
  491. switch(height)
  492. {
  493. default:
  494. case 480:
  495. case 600:
  496. case 768:
  497. return(height);
  498. case 960:
  499. if(width == 640)
  500. {
  501. return(height / 2);
  502. }
  503. return(height);
  504. case 1200:
  505. if(width == 800)
  506. {
  507. return(height / 2);
  508. }
  509. return(height);
  510. case 1536:
  511. if(width == 1024)
  512. {
  513. return(height / 2);
  514. }
  515. return(height);
  516. }
  517. }
  518. else
  519. {
  520. return(GetSystemMetrics(SM_CYSCREEN));
  521. }
  522. }
  523. int GetMonitorFromRect(LPRECT lpMonitorRect)
  524. {
  525. // Build up the param
  526. MONITOR_ENUM_PARAM EnumParam;
  527. ZeroMemory(&EnumParam, sizeof(EnumParam));
  528. EnumParam.lFlags = MONITOR_SEARCH_METOHD;
  529. EnumParam.pVirtualRect = lpMonitorRect;
  530. EnumParam.iMonitor = -1;
  531. // Enum Displays
  532. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  533. // Return the result
  534. return EnumParam.iMonitor;
  535. }
  536. void GetMonitorRect(int iMonitor, LPRECT lpDestRect)
  537. {
  538. // Build up the param
  539. MONITOR_ENUM_PARAM EnumParam;
  540. ZeroMemory(&EnumParam, sizeof(EnumParam));
  541. EnumParam.iMonitor = iMonitor;
  542. EnumParam.pVirtualRect = lpDestRect;
  543. // Zero out dest rect
  544. lpDestRect->bottom = lpDestRect->left = lpDestRect->right = lpDestRect->top = 0;
  545. // Enum Displays
  546. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  547. // If not successful, default to the screen dimentions
  548. if(lpDestRect->right == 0 || lpDestRect->bottom == 0)
  549. {
  550. lpDestRect->right = GetScreenWidth();
  551. lpDestRect->bottom = GetScreenHeight();
  552. }
  553. }
  554. /*------------------------------------------------------------------*\
  555. ID based Globals
  556. \*------------------------------------------------------------------*/
  557. long NewGroupID(int parentID, CString text)
  558. {
  559. long lID=0;
  560. CTime time;
  561. time = CTime::GetCurrentTime();
  562. try
  563. {
  564. //sqlite doesn't like single quotes ' replace them with double ''
  565. if(text.IsEmpty())
  566. text = time.Format("NewGroup %y/%m/%d %H:%M:%S");
  567. text.Replace(_T("'"), _T("''"));
  568. CString cs;
  569. cs.Format(_T("insert into Main values(NULL, %d, '%s', 0, %d, 0, 1, %d, '');"),
  570. (long)time.GetTime(),
  571. text,
  572. (long)time.GetTime(),
  573. parentID);
  574. theApp.m_db.execDML(cs);
  575. lID = (long)theApp.m_db.lastRowId();
  576. }
  577. CATCH_SQLITE_EXCEPTION_AND_RETURN(0)
  578. return lID;
  579. }
  580. BOOL DeleteAllIDs()
  581. {
  582. try
  583. {
  584. theApp.m_db.execDML(_T("DELETE FROM Data;"));
  585. theApp.m_db.execDML(_T("DELETE FROM Main;"));
  586. }
  587. CATCH_SQLITE_EXCEPTION
  588. return TRUE;
  589. }
  590. BOOL DeleteFormats(int parentID, ARRAY& formatIDs)
  591. {
  592. if(formatIDs.GetSize() <= 0)
  593. return TRUE;
  594. try
  595. {
  596. //Delete the requested data formats
  597. INT_PTR count = formatIDs.GetSize();
  598. for(int i = 0; i < count; i++)
  599. {
  600. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lID = %d;"), formatIDs[i]);
  601. }
  602. CClip clip;
  603. if(clip.LoadFormats(parentID))
  604. {
  605. DWORD CRC = clip.GenerateCRC();
  606. //Update the main table with new size
  607. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, parentID);
  608. }
  609. }
  610. CATCH_SQLITE_EXCEPTION
  611. return TRUE;
  612. }
  613. BOOL EnsureWindowVisible(CRect *pcrRect)
  614. {
  615. int nMonitor = GetMonitorFromRect(pcrRect);
  616. if(nMonitor < 0)
  617. {
  618. GetMonitorRect(0, pcrRect);
  619. pcrRect->right = pcrRect->left + 300;
  620. pcrRect->bottom = pcrRect->top + 300;
  621. return TRUE;
  622. }
  623. CRect crMonitor;
  624. GetMonitorRect(nMonitor, crMonitor);
  625. //Validate the left
  626. long lDiff = pcrRect->left - crMonitor.left;
  627. if(lDiff < 0)
  628. {
  629. pcrRect->left += abs(lDiff);
  630. pcrRect->right += abs(lDiff);
  631. }
  632. //Right side
  633. lDiff = pcrRect->right - crMonitor.right;
  634. if(lDiff > 0)
  635. {
  636. pcrRect->left -= abs(lDiff);
  637. pcrRect->right -= abs(lDiff);
  638. }
  639. //Top
  640. lDiff = pcrRect->top - crMonitor.top;
  641. if(lDiff < 0)
  642. {
  643. pcrRect->top += abs(lDiff);
  644. pcrRect->bottom += abs(lDiff);
  645. }
  646. //Bottom
  647. lDiff = pcrRect->bottom - crMonitor.bottom;
  648. if(lDiff > 0)
  649. {
  650. pcrRect->top -= abs(lDiff);
  651. pcrRect->bottom -= abs(lDiff);
  652. }
  653. return TRUE;
  654. }
  655. __int64 GetLastWriteTime(const CString &csFile)
  656. {
  657. __int64 nLastWrite = 0;
  658. CFileFind finder;
  659. BOOL bResult = finder.FindFile(csFile);
  660. if (bResult)
  661. {
  662. finder.FindNextFile();
  663. FILETIME ft;
  664. finder.GetLastWriteTime(&ft);
  665. memcpy(&nLastWrite, &ft, sizeof(ft));
  666. }
  667. return nLastWrite;
  668. }
  669. CString GetProcessName(HWND hWnd)
  670. {
  671. DWORD Id;
  672. GetWindowThreadProcessId(hWnd, &Id);
  673. PROCESSENTRY32 processEntry = { 0 };
  674. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  675. processEntry.dwSize = sizeof(PROCESSENTRY32);
  676. if (Process32First(hSnapShot, &processEntry))
  677. {
  678. do
  679. {
  680. if (processEntry.th32ProcessID == Id)
  681. {
  682. return processEntry.szExeFile;
  683. }
  684. } while(Process32Next(hSnapShot, &processEntry));
  685. }
  686. CloseHandle(hSnapShot);
  687. return "";
  688. }
  689. BOOL IsVista()
  690. {
  691. OSVERSIONINFO osver;
  692. osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  693. if (::GetVersionEx( &osver ) &&
  694. osver.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  695. (osver.dwMajorVersion >= 6 ) )
  696. {
  697. return TRUE;
  698. }
  699. return FALSE;
  700. }
  701. bool IsRunningLimited()
  702. {
  703. LPCTSTR pszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  704. LPCTSTR pszValue = _T("EnableLUA");
  705. DWORD dwType = 0;
  706. DWORD dwValue = 0;
  707. DWORD dwValueSize = sizeof(DWORD);
  708. if(ERROR_SUCCESS != SHGetValue(HKEY_LOCAL_MACHINE, pszSubKey, pszValue, &dwType, &dwValue, &dwValueSize))
  709. {
  710. //failed to read the reg key, either it's not there or we don't have access to the registry
  711. //If we are vista then assume we don't have access and we are running as a limited app
  712. //otherwise we are xp and the reg key probably doesn't exist and we are not a limited running app
  713. if(IsVista())
  714. {
  715. OutputDebugString(_T("Ditto - Failed to read registry entry finding UAC, Running as limited application"));
  716. return true;
  717. }
  718. }
  719. if(dwValue == 1)
  720. {
  721. OutputDebugString(_T("Ditto - UAC ENABLED, Running as limited application"));
  722. return true;
  723. }
  724. OutputDebugString(_T("Ditto - Running as standard application"));
  725. return false;
  726. }
  727. void DeleteReceivedFiles(CString csDir)
  728. {
  729. if(csDir.Find(_T("\\ReceivedFiles\\")) == -1)
  730. return;
  731. FIX_CSTRING_PATH(csDir);
  732. CTime ctOld = CTime::GetCurrentTime();
  733. CTime ctFile;
  734. ctOld -= CTimeSpan(0, 0, 0, 1);
  735. CFileFind Find;
  736. CString csFindString;
  737. csFindString.Format(_T("%s*.*"), csDir);
  738. BOOL bFound = Find.FindFile(csFindString);
  739. while(bFound)
  740. {
  741. bFound = Find.FindNextFile();
  742. if(Find.IsDots())
  743. continue;
  744. if(Find.IsDirectory())
  745. {
  746. CString csDir(Find.GetFilePath());
  747. DeleteReceivedFiles(csDir);
  748. RemoveDirectory(csDir);
  749. }
  750. if(Find.GetLastAccessTime(ctFile))
  751. {
  752. //Delete the remote copied file if it has'nt been used for the last day
  753. if(ctFile < ctOld)
  754. {
  755. DeleteFile(Find.GetFilePath());
  756. }
  757. }
  758. else
  759. {
  760. DeleteFile(Find.GetFilePath());
  761. }
  762. }
  763. }