CP_Main.cpp 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "MainFrm.h"
  4. #include "Misc.h"
  5. #include ".\cp_main.h"
  6. #include "server.h"
  7. #include "Client.h"
  8. #include "InternetUpdate.h"
  9. #include <io.h>
  10. #include "Path.h"
  11. #include "Clip_ImportExport.h"
  12. #include "HyperLink.h"
  13. #include "OptionsSheet.h"
  14. #include "DittoCopyBuffer.h"
  15. #include "SendKeys.h"
  16. #include "MainTableFunctions.h"
  17. #include "ShowTaskBarIcon.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. class DittoCommandLineInfo : public CCommandLineInfo
  24. {
  25. public:
  26. DittoCommandLineInfo()
  27. {
  28. m_bDisconnect = FALSE;
  29. m_bConnect = FALSE;
  30. m_bU3 = FALSE;
  31. m_bU3Stop = FALSE;
  32. m_bU3Install = FALSE;
  33. m_uacPID = 0;
  34. }
  35. virtual void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast)
  36. {
  37. if(bFlag)
  38. {
  39. if(STRICMP(pszParam, _T("Connect")) == 0)
  40. {
  41. m_bConnect = TRUE;
  42. }
  43. else if(STRICMP(pszParam, _T("Disconnect")) == 0)
  44. {
  45. m_bDisconnect = TRUE;
  46. }
  47. else if(STRICMP(pszParam, _T("U3")) == 0)
  48. {
  49. m_bU3 = TRUE;
  50. }
  51. else if(STRICMP(pszParam, _T("U3appStop")) == 0)
  52. {
  53. m_bU3Stop = TRUE;
  54. }
  55. else if(STRICMP(pszParam, _T("U3Install")) == 0)
  56. {
  57. m_bU3Install = TRUE;
  58. }
  59. else if(wcsncmp(pszParam, _T("uacpaste"), 8) == 0)
  60. {
  61. CString pidCommand(pszParam);
  62. long sep = pidCommand.ReverseFind(':');
  63. if(sep > -1)
  64. {
  65. CString pid = pidCommand.Right(pidCommand.GetLength() - sep - 1);
  66. m_uacPID = ATOI(pid);
  67. }
  68. }
  69. }
  70. CCommandLineInfo::ParseParam(pszParam, bFlag, bLast);
  71. }
  72. BOOL m_bDisconnect;
  73. BOOL m_bConnect;
  74. BOOL m_bU3;
  75. BOOL m_bU3Stop;
  76. BOOL m_bU3Install;
  77. int m_uacPID;
  78. };
  79. CCP_MainApp theApp;
  80. BEGIN_MESSAGE_MAP(CCP_MainApp, CWinApp)
  81. //{{AFX_MSG_MAP(CCP_MainApp)
  82. // NOTE - the ClassWizard will add and remove mapping macros here.
  83. // DO NOT EDIT what you see in these blocks of generated code!
  84. //}}AFX_MSG_MAP
  85. END_MESSAGE_MAP()
  86. CCP_MainApp::CCP_MainApp()
  87. {
  88. theApp.m_activeWnd.TrackActiveWnd(false);
  89. m_pUacPasteThread = NULL;
  90. m_bAppRunning = false;
  91. m_bAppExiting = false;
  92. m_connectOnStartup = -1;
  93. m_MainhWnd = NULL;
  94. m_pMainFrame = NULL;
  95. BOOL set = ::AllowSetForegroundWindow(ASFW_ANY);
  96. m_bShowingQuickPaste = false;
  97. m_IC_bCopy = false;
  98. m_GroupDefaultID = 0;
  99. m_GroupID = -1;
  100. m_GroupParentID = 0;
  101. m_GroupText = "History";
  102. m_FocusID = -1;
  103. ClearOldGroupState();
  104. m_bAsynchronousRefreshView = true;
  105. m_lClipsSent = 0;
  106. m_lClipsRecieved = 0;
  107. m_oldtStartUp = COleDateTime::GetCurrentTime();
  108. m_bExitServerThread = false;
  109. m_lLastGoodIndexForNextworkPassword = -2;
  110. m_RTFFormat = ::RegisterClipboardFormat(_T("Rich Text Format"));
  111. m_HTML_Format = ::RegisterClipboardFormat(_T("HTML Format"));
  112. m_PingFormat = ::RegisterClipboardFormat(_T("Ditto Ping Format"));
  113. m_cfIgnoreClipboard = ::RegisterClipboardFormat(_T("Clipboard Viewer Ignore"));
  114. m_cfDelaySavingData = ::RegisterClipboardFormat(_T("Ditto Delay Saving Data"));
  115. m_RemoteCF_HDROP = ::RegisterClipboardFormat(_T("Ditto Remote CF_HDROP"));
  116. }
  117. CCP_MainApp::~CCP_MainApp()
  118. {
  119. }
  120. BOOL CCP_MainApp::InitInstance()
  121. {
  122. LoadLibrary(TEXT("riched20.dll"));
  123. AfxEnableControlContainer();
  124. AfxOleInit();
  125. AfxInitRichEditEx();
  126. afxAmbientActCtx = FALSE;
  127. Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  128. Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  129. DittoCommandLineInfo cmdInfo;
  130. ParseCommandLine(cmdInfo);
  131. //if starting from a u3 device we will pass in -U3Start
  132. if(cmdInfo.m_bU3)
  133. g_Opt.m_bU3 = cmdInfo.m_bU3 ? TRUE : FALSE;
  134. g_Opt.LoadSettings();
  135. if(cmdInfo.m_uacPID > 0)
  136. {
  137. Log(StrF(_T("Startup up ditto as admin to paste to admin windows, parent process id: %d"), cmdInfo.m_uacPID));
  138. CString mutex;
  139. mutex.Format(_T("DittoAdminPaste_%d"), cmdInfo.m_uacPID);
  140. m_adminPasteMutex = CreateMutex(NULL, FALSE, mutex);
  141. m_pUacPasteThread = new CUAC_Thread(cmdInfo.m_uacPID);
  142. m_pUacPasteThread->Start();
  143. m_pUacPasteThread->WaitForThreadToExit(INT_MAX);
  144. return FALSE;
  145. }
  146. if(cmdInfo.m_strFileName.IsEmpty() == FALSE)
  147. {
  148. try
  149. {
  150. g_Opt.m_bEnableDebugLogging = g_Opt.GetEnableDebugLogging();
  151. CClip_ImportExport Clip;
  152. CppSQLite3DB db;
  153. db.open(cmdInfo.m_strFileName);
  154. CClip_ImportExport clip;
  155. if(clip.ImportFromSqliteDB(db, false, true))
  156. {
  157. ShowCommandLineError("Ditto", theApp.m_Language.GetString("Importing_Good", "Clip placed on clipboard"));
  158. }
  159. else
  160. {
  161. ShowCommandLineError("Ditto", theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"));
  162. }
  163. }
  164. catch (CppSQLite3Exception& e)
  165. {
  166. ASSERT(FALSE);
  167. CString csError;
  168. csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
  169. ShowCommandLineError("Ditto", csError);
  170. }
  171. return FALSE;
  172. }
  173. else if(cmdInfo.m_bConnect || cmdInfo.m_bDisconnect)
  174. {
  175. //First get the saved hwnd and send it a message
  176. //If ditt is running then this will return 1, meening the running ditto process
  177. //handled this message
  178. //If it didn't handle the message(ditto is not running) then startup this processes of ditto
  179. //disconnected from the clipboard
  180. LRESULT ret = 0;
  181. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  182. if(hWnd)
  183. {
  184. ret = ::SendMessage(hWnd, WM_SET_CONNECTED, cmdInfo.m_bConnect, cmdInfo.m_bDisconnect);
  185. }
  186. //passed off to the running instance of ditto, exit this instance
  187. if(ret == 1)
  188. {
  189. return FALSE;
  190. }
  191. if(cmdInfo.m_bConnect)
  192. {
  193. m_connectOnStartup = TRUE;
  194. }
  195. else if(cmdInfo.m_bDisconnect)
  196. {
  197. m_connectOnStartup = FALSE;
  198. }
  199. }
  200. CInternetUpdate update;
  201. long lRunningVersion = update.GetRunningVersion();
  202. CString cs = update.GetVersionString(lRunningVersion);
  203. cs.Insert(0, _T("InitInstance - Running Version - "));
  204. Log(cs);
  205. CString csMutex("Ditto Is Now Running");
  206. if(g_Opt.m_bU3)
  207. {
  208. //If running from a U3 device then allow other ditto's to run
  209. //only prevent Ditto from running from the same device
  210. csMutex += " ";
  211. csMutex += GETENV(_T("U3_DEVICE_SERIAL"));
  212. }
  213. else if(g_Opt.GetIsPortableDitto())
  214. {
  215. csMutex += " ";
  216. csMutex += g_Opt.GetExeFileName();
  217. }
  218. m_hMutex = CreateMutex(NULL, FALSE, csMutex);
  219. DWORD dwError = GetLastError();
  220. if(dwError == ERROR_ALREADY_EXISTS)
  221. {
  222. HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
  223. if(hWnd)
  224. ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
  225. return TRUE;
  226. }
  227. CString csFile = CGetSetOptions::GetLanguageFile();
  228. if(m_Language.LoadLanguageFile(csFile) == false)
  229. {
  230. CString cs;
  231. cs.Format(_T("Error loading language file - %s - \n\n%s"), csFile, m_Language.m_csLastError);
  232. Log(cs);
  233. m_Language.LoadLanguageFile(_T("English.xml"));
  234. }
  235. //The first time we run Ditto on U3 show a web page about ditto
  236. if(g_Opt.m_bU3)
  237. {
  238. if(FileExists(CGetSetOptions::GetDBPath()) == FALSE)
  239. {
  240. CString csFile = CGetSetOptions::GetPath(PATH_HELP);
  241. csFile += "U3_Install.htm";
  242. CHyperLink::GotoURL(csFile, SW_SHOW);
  243. }
  244. }
  245. int nRet = CheckDBExists(CGetSetOptions::GetDBPath());
  246. if(nRet == FALSE)
  247. {
  248. AfxMessageBox(theApp.m_Language.GetString("Error_Opening_Database", "Error Opening Database."));
  249. return FALSE;
  250. }
  251. CMainFrame* pFrame = new CMainFrame;
  252. m_pMainWnd = m_pMainFrame = pFrame;
  253. pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
  254. pFrame->ShowWindow(SW_SHOW);
  255. pFrame->UpdateWindow();
  256. return TRUE;
  257. }
  258. void CCP_MainApp::AfterMainCreate()
  259. {
  260. m_MainhWnd = m_pMainFrame->m_hWnd;
  261. ASSERT( ::IsWindow(m_MainhWnd) );
  262. g_Opt.SetMainHWND((long)m_MainhWnd);
  263. //Save the HWND so the stop app can send us a close message
  264. if(g_Opt.m_bU3)
  265. {
  266. CGetSetOptions::WriteU3Hwnd(m_MainhWnd);
  267. }
  268. g_HotKeys.Init(m_MainhWnd);
  269. // create hotkeys here. They are automatically deleted on exit
  270. m_pDittoHotKey = new CHotKey(CString("DittoHotKey"), 704); //704 is ctrl-tilda
  271. m_pPosOne = new CHotKey("Position1", 0, true);
  272. m_pPosTwo = new CHotKey("Position2", 0, true);
  273. m_pPosThree = new CHotKey("Position3", 0, true);
  274. m_pPosFour = new CHotKey("Position4", 0, true);
  275. m_pPosFive = new CHotKey("Position5", 0, true);
  276. m_pPosSix = new CHotKey("Position6", 0, true);
  277. m_pPosSeven = new CHotKey("Position7", 0, true);
  278. m_pPosEight = new CHotKey("Position8", 0, true);
  279. m_pPosNine = new CHotKey("Position9", 0, true);
  280. m_pPosTen = new CHotKey("Position10", 0, true);
  281. m_pCopyBuffer1 = new CHotKey("CopyBufferCopyHotKey_0", 0, true);
  282. m_pPasteBuffer1 = new CHotKey("CopyBufferPasteHotKey_0", 0, true);
  283. m_pCutBuffer1 = new CHotKey("CopyBufferCutHotKey_0", 0, true);
  284. m_pCopyBuffer2 = new CHotKey("CopyBufferCopyHotKey_1", 0, true);
  285. m_pPasteBuffer2 = new CHotKey("CopyBufferPasteHotKey_1", 0, true);
  286. m_pCutBuffer2 = new CHotKey("CopyBufferCutHotKey_1", 0, true);
  287. m_pCopyBuffer3 = new CHotKey("CopyBufferCopyHotKey_2", 0, true);
  288. m_pPasteBuffer3 = new CHotKey("CopyBufferPasteHotKey_2", 0, true);
  289. m_pCutBuffer3 = new CHotKey("CopyBufferCutHotKey_2", 0, true);
  290. m_pTextOnlyPaste = new CHotKey("TextOnlyPaste", 0, true);
  291. LoadGlobalClips();
  292. g_HotKeys.RegisterAll();
  293. StartCopyThread();
  294. StartStopServerThread();
  295. #ifdef UNICODE
  296. m_Addins.LoadAll();
  297. #endif
  298. m_bAppRunning = true;
  299. }
  300. void CCP_MainApp::LoadGlobalClips()
  301. {
  302. try
  303. {
  304. CppSQLite3Query q = m_db.execQuery(_T("SELECT lID, lShortCut, mText FROM Main WHERE lShortCut > 0 AND globalShortCut = 1"));
  305. while(q.eof() == false)
  306. {
  307. int id = q.getIntField(_T("lID"));
  308. int shortcut = q.getIntField(_T("lShortCut"));
  309. CString desc = q.getStringField(_T("mText"));
  310. //Constructor will add to a global list and free
  311. CHotKey* globalHotKey = new CHotKey(desc, shortcut, true);
  312. if(globalHotKey != NULL)
  313. {
  314. globalHotKey->m_clipId = id;
  315. }
  316. q.nextRow();
  317. }
  318. }
  319. CATCH_SQLITE_EXCEPTION
  320. }
  321. void CCP_MainApp::StartStopServerThread()
  322. {
  323. if(CGetSetOptions::GetDisableRecieve() == FALSE && g_Opt.GetAllowFriends())
  324. {
  325. AfxBeginThread(MTServerThread, m_MainhWnd);
  326. }
  327. else
  328. {
  329. m_bExitServerThread = true;
  330. closesocket(theApp.m_sSocket);
  331. }
  332. }
  333. void CCP_MainApp::StopServerThread()
  334. {
  335. m_bExitServerThread = true;
  336. closesocket(theApp.m_sSocket);
  337. }
  338. void CCP_MainApp::BeforeMainClose()
  339. {
  340. ASSERT( m_bAppRunning && !m_bAppExiting );
  341. m_bAppRunning = false;
  342. m_bAppExiting = true;
  343. g_HotKeys.UnregisterAll();
  344. StopServerThread();
  345. StopCopyThread();
  346. }
  347. void CCP_MainApp::StartCopyThread()
  348. {
  349. ASSERT( m_MainhWnd );
  350. CClipTypes* pTypes = LoadTypesFromDB();
  351. // initialize to:
  352. // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
  353. // - true = use Asynchronous communication (PostMessage)
  354. // - true = enable copying on clipboard changes
  355. // - pTypes = the supported types to use
  356. m_CopyThread.Init(CCopyConfig(m_MainhWnd, true, true, pTypes));
  357. if(m_connectOnStartup == FALSE || g_Opt.GetConnectedToClipboard() == FALSE)
  358. {
  359. m_CopyThread.m_connectOnStartup = false;
  360. Log(StrF(_T("Starting Ditto up disconnected from the clipboard, commandLine: %d, saved value: %d"), m_connectOnStartup, g_Opt.GetConnectedToClipboard()));
  361. SetConnectCV(false);
  362. }
  363. else if(m_connectOnStartup == TRUE)
  364. {
  365. SetConnectCV(true);
  366. Log(_T("Starting Ditto up connected from the clipboard, passed in true from command line to start connected"));
  367. }
  368. VERIFY(m_CopyThread.CreateThread(CREATE_SUSPENDED));
  369. m_CopyThread.ResumeThread();
  370. }
  371. void CCP_MainApp::StopCopyThread()
  372. {
  373. EnableCbCopy(false);
  374. m_CopyThread.Quit();
  375. }
  376. // returns the current Clipboard Viewer Connect state (though it might not yet
  377. // be actually connected -- check IsClipboardViewerConnected())
  378. bool CCP_MainApp::ToggleConnectCV()
  379. {
  380. bool bConnect = !GetConnectCV();
  381. SetConnectCV(bConnect);
  382. return bConnect;
  383. }
  384. // Sets a menu entry according to the current Clipboard Viewer Connection status
  385. // - the menu text indicates the available command (opposite the current state)
  386. // - a check mark appears in the rare cases that the menu text actually represents
  387. // the current state, e.g. if we are supposed to be connected, but we somehow
  388. // lose that connection, "Disconnect from Clipboard" will have a check next to it.
  389. void CCP_MainApp::UpdateMenuConnectCV(CMenu* pMenu, UINT nMenuID)
  390. {
  391. if(pMenu == NULL)
  392. return;
  393. bool bConnect = theApp.GetConnectCV();
  394. CString cs;
  395. if(bConnect)
  396. {
  397. cs = theApp.m_Language.GetString("Disconnect_Clipboard", "Disconnect from Clipboard.");
  398. pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
  399. }
  400. else
  401. {
  402. cs = theApp.m_Language.GetString("Connect_Clipboard", "Connect to Clipboard.");
  403. pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
  404. }
  405. }
  406. // Allocates a new CClipTypes
  407. CClipTypes* CCP_MainApp::LoadTypesFromDB()
  408. {
  409. CClipTypes* pTypes = new CClipTypes;
  410. try
  411. {
  412. CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT TypeText FROM Types"));
  413. while(q.eof() == false)
  414. {
  415. pTypes->Add(GetFormatID(q.getStringField(_T("TypeText"))));
  416. q.nextRow();
  417. }
  418. }
  419. CATCH_SQLITE_EXCEPTION
  420. if(pTypes->GetSize() <= 0)
  421. {
  422. pTypes->Add(CF_TEXT);
  423. pTypes->Add(RegisterClipboardFormat(CF_RTF));
  424. pTypes->Add(CF_UNICODETEXT);
  425. pTypes->Add(CF_HDROP);
  426. if(g_Opt.m_bU3 == false)
  427. {
  428. pTypes->Add(CF_DIB);
  429. }
  430. }
  431. return pTypes;
  432. }
  433. void CCP_MainApp::ReloadTypes()
  434. {
  435. CClipTypes* pTypes = LoadTypesFromDB();
  436. if(pTypes)
  437. {
  438. m_CopyThread.SetSupportedTypes(pTypes);
  439. }
  440. }
  441. void CCP_MainApp::RefreshView()
  442. {
  443. CQPasteWnd *pWnd = QPasteWnd();
  444. if(pWnd)
  445. {
  446. if(m_bAsynchronousRefreshView)
  447. {
  448. pWnd->PostMessage(WM_REFRESH_VIEW);
  449. }
  450. else
  451. {
  452. pWnd->SendMessage(WM_REFRESH_VIEW);
  453. }
  454. }
  455. }
  456. void CCP_MainApp::RefreshClipOrder(int clipId)
  457. {
  458. CQPasteWnd *pWnd = QPasteWnd();
  459. if(pWnd)
  460. {
  461. if(m_bAsynchronousRefreshView)
  462. {
  463. pWnd->PostMessage(WM_RELOAD_CLIP_ORDER, clipId, 0);
  464. }
  465. else
  466. {
  467. pWnd->SendMessage(WM_RELOAD_CLIP_ORDER, clipId, 0);
  468. }
  469. }
  470. }
  471. void CCP_MainApp::OnPasteCompleted()
  472. {
  473. }
  474. void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
  475. {
  476. if(count <= 0)
  477. {
  478. return;
  479. }
  480. // update copy statistics
  481. CGetSetOptions::SetTripCopyCount(-count);
  482. CGetSetOptions::SetTotalCopyCount(-count);
  483. if(m_CopyBuffer.Active())
  484. {
  485. m_CopyBuffer.EndCopy(lLastID);
  486. }
  487. RefreshView();
  488. }
  489. // Internal Clipboard for cut/copy/paste items between Groups
  490. // if NULL, this uses the current QPaste selection
  491. void CCP_MainApp::IC_Cut(ARRAY* pIDs)
  492. {
  493. if(pIDs == NULL)
  494. {
  495. if(QPasteWnd())
  496. {
  497. QPasteWnd()->m_lstHeader.GetSelectionItemData(m_IC_IDs);
  498. }
  499. else
  500. {
  501. m_IC_IDs.SetSize(0);
  502. }
  503. }
  504. else
  505. {
  506. m_IC_IDs.Copy(*pIDs);
  507. }
  508. m_IC_bCopy = false;
  509. if(QPasteWnd())
  510. {
  511. QPasteWnd()->UpdateStatus();
  512. }
  513. }
  514. // if NULL, this uses the current QPaste selection
  515. void CCP_MainApp::IC_Copy(ARRAY* pIDs)
  516. {
  517. if(pIDs == NULL)
  518. {
  519. if(QPasteWnd())
  520. {
  521. QPasteWnd()->m_lstHeader.GetSelectionItemData(m_IC_IDs);
  522. }
  523. else
  524. {
  525. m_IC_IDs.SetSize(0);
  526. }
  527. }
  528. else
  529. {
  530. m_IC_IDs.Copy(*pIDs);
  531. }
  532. m_IC_bCopy = true;
  533. RefreshView();
  534. }
  535. void CCP_MainApp::IC_Paste()
  536. {
  537. if(m_IC_IDs.GetSize() <= 0)
  538. {
  539. return;
  540. }
  541. if(m_IC_bCopy)
  542. {
  543. m_IC_IDs.CopyTo(GetValidGroupID());
  544. }
  545. else // Move
  546. {
  547. m_IC_IDs.MoveTo(GetValidGroupID());
  548. }
  549. // don't process the same items twice.
  550. m_IC_IDs.SetSize(0);
  551. RefreshView();
  552. }
  553. // Groups
  554. void CCP_MainApp::SaveCurrentGroupState()
  555. {
  556. m_oldGroupID = m_GroupID;
  557. m_oldGroupParentID = m_GroupParentID;
  558. m_oldGroupText = m_GroupText;
  559. }
  560. void CCP_MainApp::ClearOldGroupState()
  561. {
  562. m_oldGroupID = -2;
  563. m_oldGroupParentID = -2;
  564. m_oldGroupText = _T("");
  565. }
  566. BOOL CCP_MainApp::TryEnterOldGroupState()
  567. {
  568. BOOL enteredGroup = FALSE;
  569. if(m_oldGroupID > -2)
  570. {
  571. m_GroupID = m_oldGroupID;
  572. m_GroupParentID = m_oldGroupParentID;
  573. m_GroupText = m_oldGroupText;
  574. ClearOldGroupState();
  575. theApp.RefreshView();
  576. if(QPasteWnd())
  577. QPasteWnd()->UpdateStatus(true);
  578. enteredGroup = TRUE;
  579. }
  580. return enteredGroup;
  581. }
  582. BOOL CCP_MainApp::EnterGroupID(long lID, BOOL clearOldGroupState/* = TRUE*/, BOOL saveCurrentGroupState/* = FALSE*/)
  583. {
  584. BOOL bResult = FALSE;
  585. if(m_GroupID == lID)
  586. return TRUE;
  587. DWORD startTick = GetTickCount();
  588. if(clearOldGroupState)
  589. {
  590. ClearOldGroupState();
  591. }
  592. if(saveCurrentGroupState)
  593. {
  594. SaveCurrentGroupState();
  595. }
  596. // if we are switching to the parent, focus on the previous group
  597. if(m_GroupParentID == lID && m_GroupID > 0)
  598. m_FocusID = m_GroupID;
  599. switch(lID)
  600. {
  601. case -1:
  602. m_FocusID = -1;
  603. m_GroupID = -1;
  604. m_GroupParentID = -1;
  605. m_GroupText = "History";
  606. bResult = TRUE;
  607. break;
  608. default: // Normal Group
  609. try
  610. {
  611. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lParentID, mText, bIsGroup FROM Main WHERE lID = %d"), lID);
  612. if(q.eof() == false)
  613. {
  614. if(q.getIntField(_T("bIsGroup")) > 0)
  615. {
  616. m_GroupID = lID;
  617. m_GroupParentID = q.getIntField(_T("lParentID"));
  618. m_GroupText = q.getStringField(_T("mText"));
  619. bResult = TRUE;
  620. }
  621. }
  622. }
  623. CATCH_SQLITE_EXCEPTION
  624. break;
  625. }
  626. if(bResult)
  627. {
  628. theApp.RefreshView();
  629. if(QPasteWnd())
  630. QPasteWnd()->UpdateStatus(true);
  631. }
  632. DWORD endTick = GetTickCount();
  633. if((endTick-startTick) > 150)
  634. Log(StrF(_T("Paste Timing EnterParentId: %d"), endTick-startTick));
  635. return bResult;
  636. }
  637. // returns a usable group id (not negative)
  638. long CCP_MainApp::GetValidGroupID()
  639. {
  640. return m_GroupID;
  641. }
  642. // sets a valid id
  643. void CCP_MainApp::SetGroupDefaultID(long lID)
  644. {
  645. if(m_GroupDefaultID == lID)
  646. {
  647. return;
  648. }
  649. if(lID <= 0)
  650. {
  651. m_GroupDefaultID = 0;
  652. }
  653. else
  654. {
  655. m_GroupDefaultID = lID;
  656. }
  657. if(QPasteWnd())
  658. {
  659. QPasteWnd()->UpdateStatus();
  660. }
  661. }
  662. void CCP_MainApp::SetStatus(const TCHAR* status, bool bRepaintImmediately)
  663. {
  664. m_Status = status;
  665. if(QPasteWnd())
  666. {
  667. QPasteWnd()->UpdateStatus(bRepaintImmediately);
  668. }
  669. }
  670. void CCP_MainApp::ShowPersistent(bool bVal)
  671. {
  672. g_Opt.SetShowPersistent(bVal);
  673. // give some visual indication
  674. if(m_bShowingQuickPaste)
  675. {
  676. ASSERT(QPasteWnd());
  677. QPasteWnd()->SetCaptionColorActive(g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  678. QPasteWnd()->RefreshNc();
  679. }
  680. }
  681. /////////////////////////////////////////////////////////////////////////////
  682. // CCP_MainApp message handlers
  683. int CCP_MainApp::ExitInstance()
  684. {
  685. Log(_T("ExitInstance"));
  686. m_db.close();
  687. if(m_pUacPasteThread != NULL)
  688. {
  689. if(m_pUacPasteThread->ThreadWasStarted() == false)
  690. {
  691. m_pUacPasteThread->FireExit();
  692. }
  693. delete m_pUacPasteThread;
  694. }
  695. Gdiplus::GdiplusShutdown(m_gdiplusToken);
  696. return CWinApp::ExitInstance();
  697. }
  698. // return TRUE if there is more idle processing to do
  699. BOOL CCP_MainApp::OnIdle(LONG lCount)
  700. {
  701. // let winapp handle its idle processing
  702. if(CWinApp::OnIdle(lCount))
  703. return TRUE;
  704. return FALSE;
  705. }
  706. void CCP_MainApp::SetConnectCV(bool bConnect)
  707. {
  708. m_CopyThread.SetConnectCV(bConnect);
  709. g_Opt.SetConnectedToClipboard(bConnect == true);
  710. if(bConnect)
  711. {
  712. m_pMainFrame->m_TrayIcon.SetIcon(IDR_MAINFRAME);
  713. }
  714. else
  715. {
  716. m_pMainFrame->m_TrayIcon.SetIcon(IDI_DITTO_NOCOPYCB);
  717. }
  718. if(QPasteWnd())
  719. {
  720. QPasteWnd()->SetCaptionColorActive(g_Opt.m_bShowPersistent, theApp.GetConnectCV());
  721. QPasteWnd()->RefreshNc();
  722. }
  723. }
  724. void CCP_MainApp::OnDeleteID(long lID)
  725. {
  726. if(QPasteWnd())
  727. {
  728. QPasteWnd()->PostMessage(NM_ITEM_DELETED, lID, 0);
  729. }
  730. }
  731. bool CCP_MainApp::ImportClips(HWND hWnd)
  732. {
  733. OPENFILENAME FileName;
  734. TCHAR szFileName[400];
  735. TCHAR szDir[400];
  736. memset(&FileName, 0, sizeof(FileName));
  737. memset(szFileName, 0, sizeof(szFileName));
  738. memset(&szDir, 0, sizeof(szDir));
  739. CString csInitialDir = CGetSetOptions::GetLastImportDir();
  740. STRCPY(szDir, csInitialDir);
  741. FileName.lStructSize = sizeof(FileName);
  742. FileName.lpstrTitle = _T("Import Clips");
  743. FileName.Flags = OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST;
  744. FileName.nMaxFile = 400;
  745. FileName.lpstrFile = szFileName;
  746. FileName.lpstrInitialDir = szDir;
  747. FileName.lpstrFilter = _T("Exported Ditto Clips (.dto)\0*.dto\0\0");
  748. FileName.lpstrDefExt = _T("dto");
  749. if(GetOpenFileName(&FileName) == 0)
  750. {
  751. return false;
  752. }
  753. using namespace nsPath;
  754. CPath path(FileName.lpstrFile);
  755. CString csPath = path.GetPath();
  756. CGetSetOptions::SetLastImportDir(csPath);
  757. try
  758. {
  759. CppSQLite3DB db;
  760. db.open(FileName.lpstrFile);
  761. CClip_ImportExport clip;
  762. if(clip.ImportFromSqliteDB(db, true, false))
  763. {
  764. CShowTaskBarIcon show;
  765. CString cs;
  766. cs.Format(_T("%s %d "), theApp.m_Language.GetString("Import_Successfully", "Successfully imported"), clip.m_importCount);
  767. if(clip.m_importCount = 1)
  768. cs += theApp.m_Language.GetString("Clip", "clip");
  769. else
  770. cs += theApp.m_Language.GetString("Clips", "clips");
  771. MessageBox(hWnd, cs, _T("Ditto"), MB_OK);
  772. }
  773. else
  774. {
  775. CShowTaskBarIcon show;
  776. MessageBox(hWnd, theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"), _T("Ditto"), MB_OK);
  777. }
  778. }
  779. catch (CppSQLite3Exception& e)
  780. {
  781. ASSERT(FALSE);
  782. CString csError;
  783. csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
  784. MessageBox(hWnd, csError, _T("Ditto"), MB_OK);
  785. }
  786. return true;
  787. }
  788. void CCP_MainApp::ShowCommandLineError(CString csTitle, CString csMessage)
  789. {
  790. Log(StrF(_T("ShowCommandLineError %s - %s"), csTitle, csMessage));
  791. CToolTipEx *pErrorWnd = new CToolTipEx;
  792. pErrorWnd->Create(NULL);
  793. pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
  794. CPoint pt;
  795. CRect rcScreen;
  796. GetMonitorRect(0, &rcScreen);
  797. pt = rcScreen.BottomRight();
  798. CRect cr = pErrorWnd->GetBoundsRect();
  799. pt.x -= max(cr.Width()+50, 150);
  800. pt.y -= max(cr.Height()+50, 150);
  801. pErrorWnd->Show(pt);
  802. Sleep(4000);
  803. pErrorWnd->DestroyWindow();
  804. }
  805. BOOL CCP_MainApp::GetClipData(long lID, CClipFormat &Clip)
  806. {
  807. BOOL bRet = FALSE;
  808. try
  809. {
  810. CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT ooData FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), lID, GetFormatName(Clip.m_cfType));
  811. if(q.eof() == false)
  812. {
  813. int nDataLen = 0;
  814. const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
  815. if(cData != NULL)
  816. {
  817. Clip.m_hgData = NewGlobal(nDataLen);
  818. ::CopyToGlobalHP(Clip.m_hgData, (LPVOID)cData, nDataLen);
  819. bRet = TRUE;
  820. }
  821. }
  822. }
  823. CATCH_SQLITE_EXCEPTION
  824. return bRet;
  825. }
  826. bool CCP_MainApp::EditItems(CClipIDs &Ids, bool bShowError)
  827. {
  828. m_pMainFrame->ShowEditWnd(Ids);
  829. return true;
  830. }
  831. void CCP_MainApp::PumpMessageEx(HWND hWnd)
  832. {
  833. MSG KeyboardMsg;
  834. while (::PeekMessage(&KeyboardMsg, hWnd, 0, 0, PM_REMOVE))
  835. {
  836. ::TranslateMessage(&KeyboardMsg);
  837. ::DispatchMessage(&KeyboardMsg);
  838. }
  839. }
  840. HWND CCP_MainApp::QPastehWnd()
  841. {
  842. if(m_pMainFrame != NULL)
  843. {
  844. if(m_pMainFrame->m_quickPaste.m_pwndPaste != NULL)
  845. {
  846. return m_pMainFrame->m_quickPaste.m_pwndPaste->GetSafeHwnd();
  847. }
  848. }
  849. return NULL;
  850. }
  851. CQPasteWnd* CCP_MainApp::QPasteWnd()
  852. {
  853. if(m_pMainFrame != NULL)
  854. {
  855. return m_pMainFrame->m_quickPaste.m_pwndPaste;
  856. }
  857. return NULL;
  858. }
  859. void CCP_MainApp::UACPaste()
  860. {
  861. if(m_pUacPasteThread == NULL)
  862. {
  863. m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
  864. }
  865. m_pUacPasteThread->UACPaste();
  866. }
  867. bool CCP_MainApp::UACThreadRunning()
  868. {
  869. if(m_pUacPasteThread != NULL)
  870. {
  871. return m_pUacPasteThread->IsRunning();
  872. }
  873. return false;
  874. }