| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183 |
- #include "stdafx.h"
- #include "CP_Main.h"
- #include "MainFrm.h"
- #include "Misc.h"
- #include ".\cp_main.h"
- #include "server.h"
- #include "Client.h"
- #include "InternetUpdate.h"
- #include <io.h>
- #include "Path.h"
- #include "Clip_ImportExport.h"
- #include "HyperLink.h"
- #include "OptionsSheet.h"
- #include "DittoCopyBuffer.h"
- #include "SendKeys.h"
- #include "MainTableFunctions.h"
- #include "ShowTaskBarIcon.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- class DittoCommandLineInfo : public CCommandLineInfo
- {
- public:
- DittoCommandLineInfo()
- {
- m_bDisconnect = FALSE;
- m_bConnect = FALSE;
- m_bU3 = FALSE;
- m_bU3Stop = FALSE;
- m_bU3Install = FALSE;
- m_uacPID = 0;
- m_bOpenWindow = FALSE;
- m_bCloseWindow = FALSE;
- }
- virtual void ParseParam(const TCHAR* pszParam, BOOL bFlag, BOOL bLast)
- {
- if(bFlag)
- {
- if(STRICMP(pszParam, _T("Connect")) == 0)
- {
- m_bConnect = TRUE;
- }
- else if(STRICMP(pszParam, _T("Disconnect")) == 0)
- {
- m_bDisconnect = TRUE;
- }
- else if(STRICMP(pszParam, _T("U3")) == 0)
- {
- m_bU3 = TRUE;
- }
- else if(STRICMP(pszParam, _T("U3appStop")) == 0)
- {
- m_bU3Stop = TRUE;
- }
- else if(STRICMP(pszParam, _T("U3Install")) == 0)
- {
- m_bU3Install = TRUE;
- }
- else if(wcsncmp(pszParam, _T("uacpaste"), 8) == 0)
- {
- CString pidCommand(pszParam);
- long sep = pidCommand.ReverseFind(':');
- if(sep > -1)
- {
- CString pid = pidCommand.Right(pidCommand.GetLength() - sep - 1);
- m_uacPID = ATOI(pid);
- }
- }
- else if(STRICMP(pszParam, _T("open")) == 0)
- {
- m_bOpenWindow = TRUE;
- }
- else if(STRICMP(pszParam, _T("close")) == 0)
- {
- m_bCloseWindow = TRUE;
- }
- }
-
- CCommandLineInfo::ParseParam(pszParam, bFlag, bLast);
- }
- BOOL m_bDisconnect;
- BOOL m_bConnect;
- BOOL m_bU3;
- BOOL m_bU3Stop;
- BOOL m_bU3Install;
- int m_uacPID;
- BOOL m_bCloseWindow;
- BOOL m_bOpenWindow;
- };
- CCP_MainApp theApp;
- BEGIN_MESSAGE_MAP(CCP_MainApp, CWinApp)
- //{{AFX_MSG_MAP(CCP_MainApp)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- // DO NOT EDIT what you see in these blocks of generated code!
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- CCP_MainApp::CCP_MainApp()
- {
- theApp.m_activeWnd.TrackActiveWnd(false);
- m_copyReason = CopyReasonEnum::COPY_TO_UNKOWN;
- m_copyReasonStartTime = 0;
- m_activeGroupId = -1;
- m_activeGroupStartTime = 0;
- m_pUacPasteThread = NULL;
- m_bAppRunning = false;
- m_bAppExiting = false;
- m_connectOnStartup = -1;
- m_MainhWnd = NULL;
- m_pMainFrame = NULL;
- BOOL set = ::AllowSetForegroundWindow(ASFW_ANY);
- m_bShowingQuickPaste = false;
- m_IC_bCopy = false;
- m_GroupDefaultID = 0;
- m_GroupID = -1;
- m_GroupParentID = 0;
- m_GroupText = "History";
- m_FocusID = -1;
- ClearOldGroupState();
- m_bAsynchronousRefreshView = true;
- m_lClipsSent = 0;
- m_lClipsRecieved = 0;
- m_oldtStartUp = COleDateTime::GetCurrentTime();
- m_bExitServerThread = false;
- m_lLastGoodIndexForNextworkPassword = -2;
- m_RTFFormat = ::RegisterClipboardFormat(_T("Rich Text Format"));
- m_HTML_Format = ::RegisterClipboardFormat(_T("HTML Format"));
- m_PingFormat = ::RegisterClipboardFormat(_T("Ditto Ping Format"));
- m_cfIgnoreClipboard = ::RegisterClipboardFormat(_T("Clipboard Viewer Ignore"));
- m_cfDelaySavingData = ::RegisterClipboardFormat(_T("Ditto Delay Saving Data"));
- m_RemoteCF_HDROP = ::RegisterClipboardFormat(_T("Ditto Remote CF_HDROP"));
- }
- CCP_MainApp::~CCP_MainApp()
- {
-
- }
- BOOL CCP_MainApp::InitInstance()
- {
- LoadLibrary(TEXT("riched20.dll"));
- AfxEnableControlContainer();
- AfxOleInit();
- AfxInitRichEditEx();
- afxAmbientActCtx = FALSE;
- Gdiplus::GdiplusStartupInput gdiplusStartupInput;
- Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
- DittoCommandLineInfo cmdInfo;
- ParseCommandLine(cmdInfo);
- //if starting from a u3 device we will pass in -U3Start
- if(cmdInfo.m_bU3)
- g_Opt.m_bU3 = cmdInfo.m_bU3 ? TRUE : FALSE;
- g_Opt.LoadSettings();
- if(cmdInfo.m_uacPID > 0)
- {
- Log(StrF(_T("Startup up ditto as admin to paste to admin windows, parent process id: %d"), cmdInfo.m_uacPID));
- CString mutex;
- mutex.Format(_T("DittoAdminPaste_%d"), cmdInfo.m_uacPID);
- m_adminPasteMutex = CreateMutex(NULL, FALSE, mutex);
- m_pUacPasteThread = new CUAC_Thread(cmdInfo.m_uacPID);
- m_pUacPasteThread->Start();
- m_pUacPasteThread->WaitForThreadToExit(INT_MAX);
- return FALSE;
- }
- if(cmdInfo.m_strFileName.IsEmpty() == FALSE)
- {
- try
- {
- g_Opt.m_bEnableDebugLogging = g_Opt.GetEnableDebugLogging();
- CClip_ImportExport Clip;
- CppSQLite3DB db;
- db.open(cmdInfo.m_strFileName);
- CClip_ImportExport clip;
- if(clip.ImportFromSqliteDB(db, false, true))
- {
- ShowCommandLineError("Ditto", theApp.m_Language.GetString("Importing_Good", "Clip placed on clipboard"));
- }
- else
- {
- ShowCommandLineError("Ditto", theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"));
- }
- }
- catch (CppSQLite3Exception& e)
- {
- ASSERT(FALSE);
- CString csError;
- csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
- ShowCommandLineError("Ditto", csError);
- }
- return FALSE;
- }
- else if(cmdInfo.m_bConnect || cmdInfo.m_bDisconnect)
- {
- //First get the saved hwnd and send it a message
- //If ditt is running then this will return 1, meening the running ditto process
- //handled this message
- //If it didn't handle the message(ditto is not running) then startup this processes of ditto
- //disconnected from the clipboard
- LRESULT ret = 0;
- HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
- if(hWnd)
- {
- ret = ::SendMessage(hWnd, WM_SET_CONNECTED, cmdInfo.m_bConnect, cmdInfo.m_bDisconnect);
- }
- //passed off to the running instance of ditto, exit this instance
- if(ret == 1)
- {
- return FALSE;
- }
-
- if(cmdInfo.m_bConnect)
- {
- m_connectOnStartup = TRUE;
- }
- else if(cmdInfo.m_bDisconnect)
- {
- m_connectOnStartup = FALSE;
- }
- }
- else if(cmdInfo.m_bOpenWindow || cmdInfo.m_bCloseWindow)
- {
- //First get the saved hwnd and send it a message
- //If ditt is running then this will return 1, meening the running ditto process
- //handled this message
- //If it didn't handle the message(ditto is not running) then startup this processes of ditto
- //disconnected from the clipboard
- LRESULT ret = 0;
- HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
- if(hWnd)
- {
- ret = ::SendMessage(hWnd, WM_OPEN_CLOSE_WINDWOW, cmdInfo.m_bOpenWindow, cmdInfo.m_bCloseWindow);
- }
- return FALSE;
- }
- CInternetUpdate update;
- long lRunningVersion = update.GetRunningVersion();
- CString cs = update.GetVersionString(lRunningVersion);
- cs.Insert(0, _T("InitInstance - Running Version - "));
- Log(cs);
- CString csMutex("Ditto Is Now Running");
- if(g_Opt.m_bU3)
- {
- //If running from a U3 device then allow other ditto's to run
- //only prevent Ditto from running from the same device
- csMutex += " ";
- csMutex += GETENV(_T("U3_DEVICE_SERIAL"));
- }
- else if(g_Opt.GetIsPortableDitto())
- {
- csMutex += " ";
- csMutex += g_Opt.GetExeFileName();
- }
- m_hMutex = CreateMutex(NULL, FALSE, csMutex);
- DWORD dwError = GetLastError();
- if(dwError == ERROR_ALREADY_EXISTS)
- {
- HWND hWnd = (HWND)CGetSetOptions::GetMainHWND();
- if(hWnd)
- ::SendMessage(hWnd, WM_SHOW_TRAY_ICON, TRUE, TRUE);
- return TRUE;
- }
- CString csFile = CGetSetOptions::GetLanguageFile();
- if(m_Language.LoadLanguageFile(csFile) == false)
- {
- CString cs;
- cs.Format(_T("Error loading language file - %s - \n\n%s"), csFile, m_Language.m_csLastError);
- Log(cs);
- m_Language.LoadLanguageFile(_T("English.xml"));
- }
- //The first time we run Ditto on U3 show a web page about ditto
- if(g_Opt.m_bU3)
- {
- if(FileExists(CGetSetOptions::GetDBPath()) == FALSE)
- {
- CString csFile = CGetSetOptions::GetPath(PATH_HELP);
- csFile += "U3_Install.htm";
- CHyperLink::GotoURL(csFile, SW_SHOW);
- }
- }
- int nRet = CheckDBExists(CGetSetOptions::GetDBPath());
- if(nRet == FALSE)
- {
- AfxMessageBox(theApp.m_Language.GetString("Error_Opening_Database", "Error Opening Database."));
- return FALSE;
- }
- CMainFrame* pFrame = new CMainFrame;
- m_pMainWnd = m_pMainFrame = pFrame;
- pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
- pFrame->ShowWindow(SW_SHOW);
- pFrame->UpdateWindow();
- return TRUE;
- }
- void CCP_MainApp::AfterMainCreate()
- {
- m_MainhWnd = m_pMainFrame->m_hWnd;
- ASSERT( ::IsWindow(m_MainhWnd) );
- g_Opt.SetMainHWND((long)m_MainhWnd);
- //Save the HWND so the stop app can send us a close message
- if(g_Opt.m_bU3)
- {
- CGetSetOptions::WriteU3Hwnd(m_MainhWnd);
- }
- g_HotKeys.Init(m_MainhWnd);
- // create hotkeys here. They are automatically deleted on exit
- m_pDittoHotKey = new CHotKey(CString("DittoHotKey"), 704); //704 is ctrl-tilda
- m_pPosOne = new CHotKey("Position1", 0, true);
- m_pPosTwo = new CHotKey("Position2", 0, true);
- m_pPosThree = new CHotKey("Position3", 0, true);
- m_pPosFour = new CHotKey("Position4", 0, true);
- m_pPosFive = new CHotKey("Position5", 0, true);
- m_pPosSix = new CHotKey("Position6", 0, true);
- m_pPosSeven = new CHotKey("Position7", 0, true);
- m_pPosEight = new CHotKey("Position8", 0, true);
- m_pPosNine = new CHotKey("Position9", 0, true);
- m_pPosTen = new CHotKey("Position10", 0, true);
- m_pCopyBuffer1 = new CHotKey("CopyBufferCopyHotKey_0", 0, true);
- m_pPasteBuffer1 = new CHotKey("CopyBufferPasteHotKey_0", 0, true);
- m_pCutBuffer1 = new CHotKey("CopyBufferCutHotKey_0", 0, true);
-
- m_pCopyBuffer2 = new CHotKey("CopyBufferCopyHotKey_1", 0, true);
- m_pPasteBuffer2 = new CHotKey("CopyBufferPasteHotKey_1", 0, true);
- m_pCutBuffer2 = new CHotKey("CopyBufferCutHotKey_1", 0, true);
- m_pCopyBuffer3 = new CHotKey("CopyBufferCopyHotKey_2", 0, true);
- m_pPasteBuffer3 = new CHotKey("CopyBufferPasteHotKey_2", 0, true);
- m_pCutBuffer3 = new CHotKey("CopyBufferCutHotKey_2", 0, true);
- m_pTextOnlyPaste = new CHotKey("TextOnlyPaste", 0, true);
- m_pSaveClipboard = new CHotKey("SaveClipboard", 0, false);
- LoadGlobalClips();
- g_HotKeys.RegisterAll();
- StartCopyThread();
- StartStopServerThread();
- #ifdef UNICODE
- m_Addins.LoadAll();
- #endif
-
- m_bAppRunning = true;
- }
- void CCP_MainApp::LoadGlobalClips()
- {
- try
- {
- {
- CppSQLite3Query q = m_db.execQuery(_T("SELECT lID, lShortCut, mText FROM Main WHERE lShortCut > 0 AND globalShortCut = 1"));
- while(q.eof() == false)
- {
- int id = q.getIntField(_T("lID"));
- int shortcut = q.getIntField(_T("lShortCut"));
- CString desc = q.getStringField(_T("mText"));
- //Constructor will add to a global list and free
- CHotKey* globalHotKey = new CHotKey(desc, shortcut, true, CHotKey::PASTE_OPEN_CLIP);
- if(globalHotKey != NULL)
- {
- globalHotKey->m_clipId = id;
- }
- q.nextRow();
- }
- }
- {
- CppSQLite3Query q2 = m_db.execQuery(_T("SELECT lID, MoveToGroupShortCut, mText FROM Main WHERE MoveToGroupShortCut > 0 AND GlobalMoveToGroupShortCut = 1"));
- while(q2.eof() == false)
- {
- int id = q2.getIntField(_T("lID"));
- int shortcut = q2.getIntField(_T("MoveToGroupShortCut"));
- CString desc = q2.getStringField(_T("mText"));
- //Constructor will add to a global list and free
- CHotKey* globalHotKey = new CHotKey(desc, shortcut, true, CHotKey::MOVE_TO_GROUP);
- if(globalHotKey != NULL)
- {
- globalHotKey->m_clipId = id;
- }
- q2.nextRow();
- }
- }
- }
- CATCH_SQLITE_EXCEPTION
- }
- void CCP_MainApp::StartStopServerThread()
- {
- if(CGetSetOptions::GetDisableRecieve() == FALSE && g_Opt.GetAllowFriends())
- {
- AfxBeginThread(MTServerThread, m_MainhWnd);
- }
- else
- {
- m_bExitServerThread = true;
- closesocket(theApp.m_sSocket);
- }
- }
- void CCP_MainApp::StopServerThread()
- {
- m_bExitServerThread = true;
- closesocket(theApp.m_sSocket);
- }
- void CCP_MainApp::BeforeMainClose()
- {
- ASSERT( m_bAppRunning && !m_bAppExiting );
- m_bAppRunning = false;
- m_bAppExiting = true;
- g_HotKeys.UnregisterAll();
- StopServerThread();
- StopCopyThread();
- }
- void CCP_MainApp::StartCopyThread()
- {
- ASSERT( m_MainhWnd );
- CClipTypes* pTypes = LoadTypesFromDB();
- // initialize to:
- // - m_MainhWnd = send WM_CLIPBOARD_COPIED messages to m_MainhWnd
- // - true = use Asynchronous communication (PostMessage)
- // - true = enable copying on clipboard changes
- // - pTypes = the supported types to use
- m_CopyThread.Init(CCopyConfig(m_MainhWnd, true, true, pTypes));
-
- if(m_connectOnStartup == FALSE || g_Opt.GetConnectedToClipboard() == FALSE)
- {
- m_CopyThread.m_connectOnStartup = false;
- Log(StrF(_T("Starting Ditto up disconnected from the clipboard, commandLine: %d, saved value: %d"), m_connectOnStartup, g_Opt.GetConnectedToClipboard()));
- SetConnectCV(false);
- }
- else if(m_connectOnStartup == TRUE)
- {
- SetConnectCV(true);
- Log(_T("Starting Ditto up connected from the clipboard, passed in true from command line to start connected"));
- }
- VERIFY(m_CopyThread.CreateThread(CREATE_SUSPENDED));
- m_CopyThread.ResumeThread();
- }
- void CCP_MainApp::StopCopyThread()
- {
- EnableCbCopy(false);
- m_CopyThread.Quit();
- }
- // returns the current Clipboard Viewer Connect state (though it might not yet
- // be actually connected -- check IsClipboardViewerConnected())
- bool CCP_MainApp::ToggleConnectCV()
- {
- bool bConnect = !GetConnectCV();
- SetConnectCV(bConnect);
- return bConnect;
- }
- // Sets a menu entry according to the current Clipboard Viewer Connection status
- // - the menu text indicates the available command (opposite the current state)
- // - a check mark appears in the rare cases that the menu text actually represents
- // the current state, e.g. if we are supposed to be connected, but we somehow
- // lose that connection, "Disconnect from Clipboard" will have a check next to it.
- void CCP_MainApp::UpdateMenuConnectCV(CMenu* pMenu, UINT nMenuID)
- {
- if(pMenu == NULL)
- return;
- bool bConnect = theApp.GetConnectCV();
- CString cs;
- if(bConnect)
- {
- cs = theApp.m_Language.GetString("Disconnect_Clipboard", "Disconnect from Clipboard.");
- pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
- }
- else
- {
- cs = theApp.m_Language.GetString("Connect_Clipboard", "Connect to Clipboard.");
- pMenu->ModifyMenu(nMenuID, MF_BYCOMMAND, nMenuID, cs);
- }
- }
- // Allocates a new CClipTypes
- CClipTypes* CCP_MainApp::LoadTypesFromDB()
- {
- CClipTypes* pTypes = new CClipTypes;
- try
- {
- CppSQLite3Query q = theApp.m_db.execQuery(_T("SELECT TypeText FROM Types"));
- while(q.eof() == false)
- {
- pTypes->Add(GetFormatID(q.getStringField(_T("TypeText"))));
- q.nextRow();
- }
- }
- CATCH_SQLITE_EXCEPTION
- if(pTypes->GetSize() <= 0)
- {
- pTypes->Add(CF_TEXT);
- pTypes->Add(RegisterClipboardFormat(CF_RTF));
- pTypes->Add(CF_UNICODETEXT);
- pTypes->Add(CF_HDROP);
- if(g_Opt.m_bU3 == false)
- {
- pTypes->Add(CF_DIB);
- }
- }
- return pTypes;
- }
- void CCP_MainApp::ReloadTypes()
- {
- CClipTypes* pTypes = LoadTypesFromDB();
- if(pTypes)
- {
- m_CopyThread.SetSupportedTypes(pTypes);
- }
- }
- void CCP_MainApp::RefreshView()
- {
- CQPasteWnd *pWnd = QPasteWnd();
- if(pWnd)
- {
- if(m_bAsynchronousRefreshView)
- {
- pWnd->PostMessage(WM_REFRESH_VIEW);
- }
- else
- {
- pWnd->SendMessage(WM_REFRESH_VIEW);
- }
- }
- }
- void CCP_MainApp::RefreshClipAfterPaste(int clipId)
- {
- CQPasteWnd *pWnd = QPasteWnd();
- if(pWnd)
- {
- if(m_bAsynchronousRefreshView)
- {
- pWnd->PostMessage(WM_RELOAD_CLIP_AFTER_PASTE, clipId, 0);
- }
- else
- {
- pWnd->SendMessage(WM_RELOAD_CLIP_AFTER_PASTE, clipId, 0);
- }
- }
- }
- void CCP_MainApp::OnPasteCompleted()
- {
- }
- void CCP_MainApp::OnCopyCompleted(long lLastID, int count)
- {
- if(count <= 0)
- {
- return;
- }
- // update copy statistics
- CGetSetOptions::SetTripCopyCount(-count);
- CGetSetOptions::SetTotalCopyCount(-count);
- if(m_CopyBuffer.Active())
- {
- m_CopyBuffer.EndCopy(lLastID);
- }
- RefreshView();
- }
- // Internal Clipboard for cut/copy/paste items between Groups
- // if NULL, this uses the current QPaste selection
- void CCP_MainApp::IC_Cut(ARRAY* pIDs)
- {
- if(pIDs == NULL)
- {
- if(QPasteWnd())
- {
- QPasteWnd()->m_lstHeader.GetSelectionItemData(m_IC_IDs);
- }
- else
- {
- m_IC_IDs.SetSize(0);
- }
- }
- else
- {
- m_IC_IDs.Copy(*pIDs);
- }
- m_IC_bCopy = false;
- if(QPasteWnd())
- {
- QPasteWnd()->UpdateStatus();
- }
- }
- // if NULL, this uses the current QPaste selection
- void CCP_MainApp::IC_Copy(ARRAY* pIDs)
- {
- if(pIDs == NULL)
- {
- if(QPasteWnd())
- {
- QPasteWnd()->m_lstHeader.GetSelectionItemData(m_IC_IDs);
- }
- else
- {
- m_IC_IDs.SetSize(0);
- }
- }
- else
- {
- m_IC_IDs.Copy(*pIDs);
- }
- m_IC_bCopy = true;
- RefreshView();
- }
- void CCP_MainApp::IC_Paste()
- {
- if(m_IC_IDs.GetSize() <= 0)
- {
- return;
- }
- if(m_IC_bCopy)
- {
- m_IC_IDs.CopyTo(GetValidGroupID());
- }
- else // Move
- {
- m_IC_IDs.MoveTo(GetValidGroupID());
- }
- // don't process the same items twice.
- m_IC_IDs.SetSize(0);
- RefreshView();
- }
- // Groups
- void CCP_MainApp::SaveCurrentGroupState()
- {
- m_oldGroupID = m_GroupID;
- m_oldGroupParentID = m_GroupParentID;
- m_oldGroupText = m_GroupText;
- }
- void CCP_MainApp::ClearOldGroupState()
- {
- m_oldGroupID = -2;
- m_oldGroupParentID = -2;
- m_oldGroupText = _T("");
- }
- BOOL CCP_MainApp::TryEnterOldGroupState()
- {
- BOOL enteredGroup = FALSE;
- if(m_oldGroupID > -2)
- {
- m_GroupID = m_oldGroupID;
- m_GroupParentID = m_oldGroupParentID;
- m_GroupText = m_oldGroupText;
- ClearOldGroupState();
- theApp.RefreshView();
- if(QPasteWnd())
- QPasteWnd()->UpdateStatus(true);
- enteredGroup = TRUE;
- }
- return enteredGroup;
- }
- BOOL CCP_MainApp::EnterGroupID(long lID, BOOL clearOldGroupState/* = TRUE*/, BOOL saveCurrentGroupState/* = FALSE*/)
- {
- BOOL bResult = FALSE;
- if(m_GroupID == lID)
- return TRUE;
- DWORD startTick = GetTickCount();
- if(clearOldGroupState)
- {
- ClearOldGroupState();
- }
- if(saveCurrentGroupState)
- {
- SaveCurrentGroupState();
- }
- // if we are switching to the parent, focus on the previous group
- if(m_GroupParentID == lID && m_GroupID > 0)
- m_FocusID = m_GroupID;
- switch(lID)
- {
- case -1:
- m_FocusID = -1;
- m_GroupID = -1;
- m_GroupParentID = -1;
- m_GroupText = "History";
- bResult = TRUE;
- break;
- default: // Normal Group
- try
- {
- CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lParentID, mText, bIsGroup FROM Main WHERE lID = %d"), lID);
- if(q.eof() == false)
- {
- if(q.getIntField(_T("bIsGroup")) > 0)
- {
- m_GroupID = lID;
- m_GroupParentID = q.getIntField(_T("lParentID"));
- m_GroupText = q.getStringField(_T("mText"));
- bResult = TRUE;
- }
- }
- }
- CATCH_SQLITE_EXCEPTION
- break;
- }
- if(bResult)
- {
- theApp.RefreshView();
- if(QPasteWnd())
- QPasteWnd()->UpdateStatus(true);
- }
- DWORD endTick = GetTickCount();
- if((endTick-startTick) > 150)
- Log(StrF(_T("Paste Timing EnterParentId: %d"), endTick-startTick));
- return bResult;
- }
- // returns a usable group id (not negative)
- long CCP_MainApp::GetValidGroupID()
- {
- return m_GroupID;
- }
- // sets a valid id
- void CCP_MainApp::SetGroupDefaultID(long lID)
- {
- if(m_GroupDefaultID == lID)
- {
- return;
- }
- if(lID <= 0)
- {
- m_GroupDefaultID = 0;
- }
- else
- {
- m_GroupDefaultID = lID;
- }
- if(QPasteWnd())
- {
- QPasteWnd()->UpdateStatus();
- }
- }
- void CCP_MainApp::SetStatus(const TCHAR* status, bool bRepaintImmediately)
- {
- m_Status = status;
- if(QPasteWnd())
- {
- QPasteWnd()->UpdateStatus(bRepaintImmediately);
- }
- }
- void CCP_MainApp::ShowPersistent(bool bVal)
- {
- g_Opt.SetShowPersistent(bVal);
- // give some visual indication
- if(m_bShowingQuickPaste)
- {
- ASSERT(QPasteWnd());
- QPasteWnd()->SetCaptionColorActive(g_Opt.m_bShowPersistent, theApp.GetConnectCV());
- QPasteWnd()->RefreshNc();
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CCP_MainApp message handlers
- int CCP_MainApp::ExitInstance()
- {
- Log(_T("ExitInstance"));
- m_db.close();
- if(m_pUacPasteThread != NULL)
- {
- if(m_pUacPasteThread->ThreadWasStarted() == false)
- {
- m_pUacPasteThread->FireExit();
- }
- delete m_pUacPasteThread;
- }
- Gdiplus::GdiplusShutdown(m_gdiplusToken);
- return CWinApp::ExitInstance();
- }
- // return TRUE if there is more idle processing to do
- BOOL CCP_MainApp::OnIdle(LONG lCount)
- {
- // let winapp handle its idle processing
- if(CWinApp::OnIdle(lCount))
- return TRUE;
- return FALSE;
- }
- void CCP_MainApp::SetConnectCV(bool bConnect)
- {
- m_CopyThread.SetConnectCV(bConnect);
- g_Opt.SetConnectedToClipboard(bConnect == true);
- if(bConnect)
- {
- m_pMainFrame->m_TrayIcon.SetIcon(IDR_MAINFRAME);
- }
- else
- {
- m_pMainFrame->m_TrayIcon.SetIcon(IDI_DITTO_NOCOPYCB);
- }
- if(QPasteWnd())
- {
- QPasteWnd()->SetCaptionColorActive(g_Opt.m_bShowPersistent, theApp.GetConnectCV());
- QPasteWnd()->RefreshNc();
- }
- }
- void CCP_MainApp::OnDeleteID(long lID)
- {
- if(QPasteWnd())
- {
- QPasteWnd()->PostMessage(NM_ITEM_DELETED, lID, 0);
- }
- }
- bool CCP_MainApp::ImportClips(HWND hWnd)
- {
- OPENFILENAME FileName;
- TCHAR szFileName[400];
- TCHAR szDir[400];
- memset(&FileName, 0, sizeof(FileName));
- memset(szFileName, 0, sizeof(szFileName));
- memset(&szDir, 0, sizeof(szDir));
- CString csInitialDir = CGetSetOptions::GetLastImportDir();
- STRCPY(szDir, csInitialDir);
- FileName.lStructSize = sizeof(FileName);
- FileName.lpstrTitle = _T("Import Clips");
- FileName.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
- FileName.nMaxFile = 400;
- FileName.lpstrFile = szFileName;
- FileName.lpstrInitialDir = szDir;
- FileName.lpstrFilter = _T("Exported Ditto Clips (.dto)\0*.dto\0\0");
- FileName.lpstrDefExt = _T("dto");
- if(GetOpenFileName(&FileName) == 0)
- {
- return false;
- }
- using namespace nsPath;
- CPath path(FileName.lpstrFile);
- CString csPath = path.GetPath();
- CGetSetOptions::SetLastImportDir(csPath);
-
- try
- {
- CppSQLite3DB db;
- db.open(FileName.lpstrFile);
- CClip_ImportExport clip;
- if(clip.ImportFromSqliteDB(db, true, false))
- {
- CShowTaskBarIcon show;
- CString cs;
-
- cs.Format(_T("%s %d "), theApp.m_Language.GetString("Import_Successfully", "Successfully imported"), clip.m_importCount);
- if(clip.m_importCount = 1)
- cs += theApp.m_Language.GetString("Clip", "clip");
- else
- cs += theApp.m_Language.GetString("Clips", "clips");
- MessageBox(hWnd, cs, _T("Ditto"), MB_OK);
- }
- else
- {
- CShowTaskBarIcon show;
- MessageBox(hWnd, theApp.m_Language.GetString("Error_Importing", "Error importing exported clip"), _T("Ditto"), MB_OK);
- }
- }
- catch (CppSQLite3Exception& e)
- {
- ASSERT(FALSE);
- CString csError;
- csError.Format(_T("%s - Exception - %d - %s"), theApp.m_Language.GetString("Error_Parsing", "Error parsing exported clip"), e.errorCode(), e.errorMessage());
- MessageBox(hWnd, csError, _T("Ditto"), MB_OK);
- }
- return true;
- }
- void CCP_MainApp::ShowCommandLineError(CString csTitle, CString csMessage)
- {
- Log(StrF(_T("ShowCommandLineError %s - %s"), csTitle, csMessage));
- CToolTipEx *pErrorWnd = new CToolTipEx;
- pErrorWnd->Create(NULL);
- pErrorWnd->SetToolTipText(csTitle + "\n\n" + csMessage);
- CPoint pt;
- CRect rcScreen;
- GetMonitorRect(0, &rcScreen);
- pt = rcScreen.BottomRight();
- CRect cr = pErrorWnd->GetBoundsRect();
- pt.x -= max(cr.Width()+50, 150);
- pt.y -= max(cr.Height()+50, 150);
- pErrorWnd->Show(pt);
-
- Sleep(4000);
- pErrorWnd->DestroyWindow();
- }
- BOOL CCP_MainApp::GetClipData(long parentId, CClipFormat &Clip)
- {
- BOOL bRet = FALSE;
- try
- {
- CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT ooData FROM Data WHERE lParentID = %d AND strClipboardFormat = '%s'"), parentId, GetFormatName(Clip.m_cfType));
- if(q.eof() == false)
- {
- int nDataLen = 0;
- const unsigned char *cData = q.getBlobField(_T("ooData"), nDataLen);
- if(cData != NULL)
- {
- Clip.m_hgData = NewGlobal(nDataLen);
- ::CopyToGlobalHP(Clip.m_hgData, (LPVOID)cData, nDataLen);
- bRet = TRUE;
- }
- }
- }
- CATCH_SQLITE_EXCEPTION
- return bRet;
- }
- bool CCP_MainApp::EditItems(CClipIDs &Ids, bool bShowError)
- {
- m_pMainFrame->ShowEditWnd(Ids);
- return true;
- }
- void CCP_MainApp::PumpMessageEx(HWND hWnd)
- {
- MSG KeyboardMsg;
- while (::PeekMessage(&KeyboardMsg, hWnd, 0, 0, PM_REMOVE))
- {
- ::TranslateMessage(&KeyboardMsg);
- ::DispatchMessage(&KeyboardMsg);
- }
- }
- HWND CCP_MainApp::QPastehWnd()
- {
- if(m_pMainFrame != NULL)
- {
- if(m_pMainFrame->m_quickPaste.m_pwndPaste != NULL)
- {
- return m_pMainFrame->m_quickPaste.m_pwndPaste->GetSafeHwnd();
- }
- }
- return NULL;
- }
- CQPasteWnd* CCP_MainApp::QPasteWnd()
- {
- if(m_pMainFrame != NULL)
- {
- return m_pMainFrame->m_quickPaste.m_pwndPaste;
- }
- return NULL;
- }
- bool CCP_MainApp::UACPaste()
- {
- if(m_pUacPasteThread == NULL)
- {
- m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
- }
- return m_pUacPasteThread->UACPaste();
- }
- bool CCP_MainApp::UACCopy()
- {
- if(m_pUacPasteThread == NULL)
- {
- m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
- }
- return m_pUacPasteThread->UACCopy();
- }
- bool CCP_MainApp::UACCut()
- {
- if(m_pUacPasteThread == NULL)
- {
- m_pUacPasteThread = new CUAC_Thread(GetCurrentProcessId());
- }
- return m_pUacPasteThread->UACCut();
- }
- bool CCP_MainApp::UACThreadRunning()
- {
- if(m_pUacPasteThread != NULL)
- {
- return m_pUacPasteThread->IsRunning();
- }
- return false;
- }
- void CCP_MainApp::RefreshShowInTaskBar()
- {
- if(m_pMainFrame != NULL)
- {
- m_pMainFrame->RefreshShowInTaskBar();
- }
- }
- void CCP_MainApp::SetActiveGroupId(int groupId)
- {
- m_activeGroupId = groupId;
- m_activeGroupStartTime = GetTickCount();
- }
- int CCP_MainApp::GetActiveGroupId()
- {
- int ret = -1;
- DWORD maxDiff = CGetSetOptions::GetSaveToGroupTimeoutMS();
- DWORD diff = GetTickCount() - m_activeGroupStartTime;
- if(m_activeGroupId > -1 &&
- diff < maxDiff)
- {
- ret = m_activeGroupId;
- }
- m_activeGroupId = -1;
- m_activeGroupStartTime = 0;
- return ret;
- }
- void CCP_MainApp::SetCopyReason(CopyReasonEnum::CopyReason copyReason)
- {
- m_copyReason = copyReason;
- m_copyReasonStartTime = GetTickCount();
- }
- CopyReasonEnum::CopyReason CCP_MainApp::GetCopyReason()
- {
- CopyReasonEnum::CopyReason ret = CopyReasonEnum::COPY_TO_UNKOWN;
- DWORD maxDiff = CGetSetOptions::GetCopyReasonTimeoutMS();
- DWORD diff = GetTickCount() - m_copyReasonStartTime;
- if(m_copyReason != CopyReasonEnum::COPY_TO_UNKOWN &&
- diff < maxDiff)
- {
- ret = m_copyReason;
- }
- m_copyReason = CopyReasonEnum::COPY_TO_UNKOWN;
- m_copyReasonStartTime = 0;
- return ret;
- }
|