forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainFrmThread.cpp
239 lines (183 loc) · 5.86 KB
/
MainFrmThread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "stdafx.h"
#include "MainFrmThread.h"
#include "DatabaseUtilities.h"
#include "Options.h"
#include "Misc.h"
#include "cp_main.h"
CMainFrmThread::CMainFrmThread(void)
{
m_threadName = "CMainFrmThread";
for(int eventEnum = 0; eventEnum < ECMAINFRMTHREADEVENTS_COUNT; eventEnum++)
{
AddEvent(eventEnum);
}
}
CMainFrmThread::~CMainFrmThread(void)
{
}
void CMainFrmThread::AddClipToSave(CClip *pClip)
{
ATL::CCritSecLock csLock(m_cs.m_sect);
Log(_T("Adding clip to thread for save to db"));
m_saveClips.AddTail(pClip);
FireEvent(SAVE_CLIPS);
}
void CMainFrmThread::AddRemoteClipToSave(CClipList *pClipList)
{
ATL::CCritSecLock csLock(m_cs.m_sect);
Log(_T("Adding REMOTE clip to thread for save to db"));
POSITION pos = pClipList->GetHeadPosition();
while(pos)
{
CClip *pClip = pClipList->GetNext(pos);
m_saveRemoteClips.AddTail(pClip);
}
//local cliplist now owns the clip memory
pClipList->RemoveAll();
FireEvent(SAVE_REMOTE_CLIPS);
}
void CMainFrmThread::OnEvent(int eventId, void *param)
{
switch((eCMainFrmThreadEvents)eventId)
{
case DELETE_ENTRIES:
OnDeleteEntries();
break;
case REMOVE_TEMP_FILES:
OnRemoveTempFiles();
break;
case SAVE_CLIPS:
OnSaveClips();
break;
case SAVE_REMOTE_CLIPS:
OnSaveRemoteClips();
break;
case READ_DB_FILE:
OnReadDbFile();
break;
}
}
//try and keep our db file in windows cache by randomly reading some data
//not sure if this does what i think it does but looking into issues with slow access on large dbs
void CMainFrmThread::OnReadDbFile()
{
double idle = IdleSeconds();
if (idle < CGetSetOptions::ReadRandomFileIdleMin())
{
CString dbFile = CGetSetOptions::GetDBPath();
__int64 dbSize = FileSize(dbFile);
srand((UINT)time(NULL));
int random = rand() % (dbSize - 1024) + 1;
CFile f;
if (f.Open(dbFile, CFile::modeRead | CFile::shareDenyNone))
{
f.Seek(random, 0);
char data[1024];
f.Read(&data, 1024);
f.Close();
}
}
}
void CMainFrmThread::OnDeleteEntries()
{
RemoveOldEntries(true);
}
void CMainFrmThread::OnRemoveTempFiles()
{
DeleteDittoTempFiles(TRUE);
}
void CMainFrmThread::OnSaveClips()
{
CClipList *pLocalClips = new CClipList();
CopyReasonEnum::CopyReason copyReason = CopyReasonEnum::COPY_TO_UNKOWN;
//Save the clips locally
{
ATL::CCritSecLock csLock(m_cs.m_sect);
POSITION pos;
CClip* pClip;
pos = m_saveClips.GetHeadPosition();
while(pos)
{
pClip = m_saveClips.GetNext(pos);
copyReason = pClip->m_copyReason;
pLocalClips->AddTail(pClip);
}
//pLocalClips now own, the clips
m_saveClips.RemoveAll();
}
Log(_T("SaveCopyClips Before AddToDb"));
int count = pLocalClips->AddToDB(true);
Log(StrF(_T("SaveCopyclips After AddToDb, Count: %d"), count));
if(count > 0)
{
int Id = pLocalClips->GetTail()->m_id;
Log(StrF(_T("SaveCopyclips After AddToDb, Id: %d Before OnCopyCopyCompleted"), Id));
theApp.OnCopyCompleted(Id, count, copyReason);
Log(StrF(_T("SaveCopyclips After AddToDb, Id: %d After OnCopyCopyCompleted"), Id));
if (pLocalClips->GetTail()->m_copyReason == CopyReasonEnum::COPY_TO_GROUP &&
CGetSetOptions::GetShowMsgWndOnCopyToGroup())
{
CString groupName;
CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT mText FROM Main WHERE lID = %d"), pLocalClips->GetTail()->m_parentId);
if (q.eof() == false)
{
groupName = q.getStringField(0);
}
CString *pMsg = new CString();
pMsg->Format(_T("Saved new clip \"%s\"\r\ndirectly to the group \"%s\""), pLocalClips->GetTail()->m_Desc.Left(35), groupName);
theApp.m_pMainFrame->PostMessageW(WM_SHOW_MSG_WINDOW, (WPARAM) pMsg, pLocalClips->GetTail()->m_parentId);
}
if(g_Opt.m_lAutoSendClientCount > 0)
{
m_sendToClientThread.FireSendToClient(pLocalClips);
}
}
delete pLocalClips;
}
void CMainFrmThread::OnSaveRemoteClips()
{
LogSendRecieveInfo("---------Start of OnSaveRemoteClips");
CClipList *pLocalClips = new CClipList();
//Save the clips locally
{
ATL::CCritSecLock csLock(m_cs.m_sect);
POSITION pos;
CClip* pClip;
pos = m_saveRemoteClips.GetHeadPosition();
while(pos)
{
pClip = m_saveRemoteClips.GetNext(pos);
pLocalClips->AddTail(pClip);
}
//pLocalClips now own, the clips
m_saveRemoteClips.RemoveAll();
}
LogSendRecieveInfo("---------OnSaveRemoteClips - Before AddToDB");
int count = pLocalClips->AddToDB(true);
LogSendRecieveInfo("---------OnSaveRemoteClips - After AddToDB");
//are we supposed to add this clip to the clipboard
CClip *pLastClip = pLocalClips->GetTail();
if (CGetSetOptions::GetShowMsgWhenReceivingManualSentClip())
{
if (pLastClip && (pLastClip->m_param1 & REMOTE_CLIP_MANUAL_SEND))
{
CString *pMsg = new CString();
//baloon message can only show 254 characters
pMsg->Format(_T("Received remote clip\r\n\r\n%s"), pLastClip->m_Desc.Left(225));
theApp.m_pMainFrame->PostMessageW(WM_SHOW_MSG_WINDOW, (WPARAM)pMsg, pLocalClips->GetTail()->m_parentId);
}
}
if(pLastClip && (pLastClip->m_param1 & REMOTE_CLIP_ADD_TO_CLIPBOARD))
{
LogSendRecieveInfo("---------OnSaveRemoteClips - Before Posting msg to main thread to set clipboard");
//set the clipboard on the main thread, i was having a problem with setting the clipboard on a thread
//guess it needs to be set on the main thread
//main window will clear this memory
PostMessage(theApp.m_MainhWnd, WM_LOAD_ClIP_ON_CLIPBOARD, (LPARAM)pLastClip, 0);
LogSendRecieveInfo("---------OnSaveRemoteClips - After Posting msg to main thread to set clipboard");
pLocalClips->RemoveTail();
}
theApp.RefreshView();
delete pLocalClips;
LogSendRecieveInfo("---------End of OnSaveRemoteClips");
}