forked from sabrogden/Ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClipIds.cpp
481 lines (404 loc) · 10.3 KB
/
ClipIds.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include "stdafx.h"
#include "CP_Main.h"
#include "ClipIds.h"
#include "tinyxml\tinyxml.h"
#include "shared/TextConvert.h"
#include "Clip_ImportExport.h"
#include "CF_HDropAggregator.h"
#include "CF_UnicodeTextAggregator.h"
#include "CF_TextAggregator.h"
#include "richtextaggregator.h"
#include "htmlformataggregator.h"
#include "Popup.h"
// allocate an HGLOBAL of the given Format Type representing these Clip IDs.
HGLOBAL CClipIDs::Render(UINT cfType)
{
INT_PTR count = GetSize();
if(count <= 0)
{
return 0;
}
if(count == 1)
{
return CClip::LoadFormat(ElementAt(0), cfType);
}
CStringA SepA = CTextConvert::UnicodeToAnsi(g_Opt.GetMultiPasteSeparator());
CStringW SepW = g_Opt.GetMultiPasteSeparator();
if(cfType == CF_TEXT)
{
CCF_TextAggregator CFText(SepA);
if(AggregateData(CFText, CF_TEXT, g_Opt.m_bMultiPasteReverse, false))
{
return CFText.GetHGlobal();
}
}
else if(cfType == CF_UNICODETEXT)
{
CCF_UnicodeTextAggregator CFUnicodeText(SepW);
if(AggregateData(CFUnicodeText, CF_UNICODETEXT, g_Opt.m_bMultiPasteReverse, false))
{
return CFUnicodeText.GetHGlobal();
}
}
else if(cfType == CF_HDROP)
{
CCF_HDropAggregator HDrop;
if(AggregateData(HDrop, CF_HDROP, g_Opt.m_bMultiPasteReverse, false))
{
return HDrop.GetHGlobal();
}
}
else if(cfType == theApp.m_HTML_Format)
{
CHTMLFormatAggregator Html(SepA);
if(AggregateData(Html, theApp.m_HTML_Format, g_Opt.m_bMultiPasteReverse, false))
{
return Html.GetHGlobal();
}
}
else if(cfType == theApp.m_RTFFormat)
{
CRichTextAggregator RichText(SepA);
if(AggregateData(RichText, theApp.m_RTFFormat, g_Opt.m_bMultiPasteReverse, false))
{
return RichText.GetHGlobal();
}
}
return NULL;
}
void CClipIDs::GetTypes(CClipTypes& types)
{
INT_PTR count = GetSize();
types.RemoveAll();
if(count == 1)
{
CClip::LoadTypes(ElementAt(0), types);
}
else if(count > 1)
{
//Add the types that are common across all paste ids
long lCount;
CMap<CLIPFORMAT, CLIPFORMAT, long, long> RenderTypes;
for(int nIDPos = 0; nIDPos < count; nIDPos++)
{
CClipTypes CurrTypes;
CClip::LoadTypes(ElementAt(nIDPos), CurrTypes);
INT_PTR typeCount = CurrTypes.GetSize();
for(int type = 0; type < typeCount; type++)
{
lCount = 0;
if(nIDPos == 0 || RenderTypes.Lookup(CurrTypes[type], lCount) == TRUE)
{
lCount++;
RenderTypes.SetAt(CurrTypes[type], lCount);
}
}
}
CLIPFORMAT Format;
POSITION pos = RenderTypes.GetStartPosition();
while(pos)
{
RenderTypes.GetNextAssoc(pos, Format, lCount);
if(lCount == count)
{
types.Add(Format);
}
}
//If there were no common types add the first clip
if(types.GetSize() <= 0)
{
CClip::LoadTypes(ElementAt(0), types);
}
}
}
bool CClipIDs::AggregateData(IClipAggregator &Aggregator, UINT cfType, BOOL bReverse, bool textOnly)
{
CString csSQL;
LPWSTR Text = NULL;
int nTextSize = 0;
INT_PTR numIDs = GetSize();
bool bRet = false;
try
{
INT_PTR nIndex;
for(int i=0; i < numIDs; i++)
{
nIndex = i;
if(bReverse)
{
nIndex = numIDs - i - 1;
}
CString sqlCF_HDROP = _T("");
if (textOnly &&
cfType == CF_UNICODETEXT || cfType == CF_TEXT)
{
sqlCF_HDROP.Format(_T("OR Data.strClipBoardFormat = '%s'"), GetFormatName(CF_HDROP));
}
csSQL.Format(_T("SELECT * FROM Data ")
_T("INNER JOIN Main ON Main.lID = Data.lParentID ")
_T("WHERE (Data.strClipBoardFormat = '%s'")
_T(" %s) ")
_T("AND Main.lID = %d"),
GetFormatName(cfType),
sqlCF_HDROP,
ElementAt(nIndex));
CppSQLite3Query q = theApp.m_db.execQuery(csSQL);
if(q.eof() == false)
{
int nDataLen = 0;
LPVOID pData = (LPVOID)q.getBlobField(_T("ooData"), nDataLen);
if(pData == NULL)
{
continue;
}
if(Aggregator.AddClip(pData, nDataLen, (int)i, (int)numIDs, GetFormatID(q.getStringField(_T("strClipBoardFormat")))))
{
bRet |= true;
}
}
else
{
bRet |= false;
}
}
}
CATCH_SQLITE_EXCEPTION
catch(...)
{
}
return bRet;
}
// Blindly Moves IDs into the lParentID Group sequentially with the given order
BOOL CClipIDs::MoveTo(long lParentID, double dFirst, double dIncrement)
{
try
{
int count = (int)GetSize();
Log(StrF(_T("MoveTo, Start, Size: %d, ParentId: %d"), count, lParentID));
for(int i = count-1; i >= 0; i--)
{
CString sql;
if(lParentID > 0)
{
sql = StrF(_T("UPDATE Main SET lParentID = %d, clipGroupOrder = %f WHERE lID = %d AND lID <> %d;"),
lParentID,
CClip::GetNewOrder(lParentID, ElementAt(i)),
ElementAt(i),
lParentID);
}
else
{
sql = StrF(_T("UPDATE Main SET lParentID = %d WHERE lID = %d AND lID <> %d;"),
lParentID,
ElementAt(i),
lParentID);
}
int ret = theApp.m_db.execDMLEx(sql);
Log(StrF(_T("MoveTo, Sql Ret: %d, SQL: %s"), ret, sql));
}
}
CATCH_SQLITE_EXCEPTION
return (TRUE);
}
// Empties this array and fills it with the elements of the given group ID
BOOL CClipIDs::LoadElementsOf(int groupId)
{
SetSize(0);
try
{
CppSQLite3Query q = theApp.m_db.execQueryEx(_T("SELECT lID FROM Main WHERE lParentID = %d"), groupId);
while(q.eof() == false)
{
Add(q.getIntField(_T("lID")));
q.nextRow();
}
}
CATCH_SQLITE_EXCEPTION
return GetSize() > 0;
}
// Creates copies (duplicates) of all items in this array and assigns the
// lParentID of the copies to the given "lParentID" group.
// - if lParentID <= 0, then the copies have the same parent as the source
// - pCopies is filled with the corresponding duplicate IDs.
// - pAddNewTable and pSeekTable are used for more efficient recursion.
// - the primary overhead for recursion is one ID array per level deep.
// an alternative design would be to have one CMainTable per level deep,
// but I thought that might be too costly, so I implemented it this way.
BOOL CClipIDs::CopyTo(int parentId)
{
INT_PTR count = GetSize();
if(count == 0)
return TRUE;
try
{
theApp.m_db.execDML(_T("begin transaction;"));
for(int i = 0; i < count; i++)
{
int nID = ElementAt(i);
CClip clip;
if(clip.LoadMainTable(nID))
{
if(clip.LoadFormats(nID))
{
clip.MakeLatestOrder();
clip.m_shortCut = 0;
clip.m_parentId = parentId;
clip.m_csQuickPaste = "";
if(clip.AddToDB(false) == false)
{
Log(_T("failed to add copy to database"));
}
}
}
}
theApp.m_db.execDML(_T("commit transaction;"));
}
CATCH_SQLITE_EXCEPTION
return TRUE;
}
BOOL CClipIDs::DeleteIDs(bool fromClipWindow, CppSQLite3DB& db)
{
CPopup status(0, 0, ::GetForegroundWindow());
bool bAllowShow;
bAllowShow = IsAppWnd(::GetForegroundWindow());
BOOL bRet = TRUE;
INT_PTR count = GetSize();
int batchCount = 25;
Log(StrF(_T("Begin delete clips, Count: %d from Window: %d"), count, fromClipWindow));
if(count <= 0)
return FALSE;
try
{
CString sql = _T("DELETE FROM Main where lId in(");
CString sqlIn = _T("");
CString workingString = _T("Deleting clips, building query statement");
INT_PTR startIndex = 0;
INT_PTR index = 0;
if(bAllowShow)
{
status.Show(workingString);
}
for(index = 0; index < count; index++)
{
int clipId = ElementAt(index);
if(clipId <= 0)
continue;
Log(StrF(_T("Delete clip Id: %d"), clipId));
bool cont = false;
bool bGroup = false;
{
CppSQLite3Query q = db.execQueryEx(_T("SELECT bIsGroup FROM Main WHERE lId = %d"), clipId);
cont = !q.eof();
if(cont)
{
bGroup = q.getIntField(_T("bIsGroup")) > 0;
}
}
if(cont)
{
if(bGroup)
{
db.execDMLEx(_T("UPDATE Main SET lParentID = -1 WHERE lParentID = %d;"), clipId);
}
if(sqlIn.GetLength() > 0)
{
sqlIn += ", ";
}
sqlIn += StrF(_T("%d"), clipId);
}
if(index > 0 &&
(index % batchCount) == 0)
{
if(bAllowShow)
{
status.Show(StrF(_T("Deleting %d - %d of %d..."), startIndex+1, index, count));
}
startIndex = index;
db.execDMLEx(sql + sqlIn + _T(")"));
sqlIn = "";
bRet = TRUE;
if(bAllowShow)
{
status.Show(workingString);
}
}
if(fromClipWindow == false)
{
theApp.OnDeleteID(clipId);
}
}
if(sqlIn.GetLength() > 0)
{
if(bAllowShow)
{
status.Show(StrF(_T("Deleting %d - %d of %d..."), startIndex+1, index, count));
}
db.execDMLEx(sql + sqlIn + _T(")"));
bRet = TRUE;
}
}
CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
Log(StrF(_T("End delete clips, Count: %d"), count));
return bRet;
}
BOOL CClipIDs::CreateExportSqliteDB(CppSQLite3DB &db)
{
BOOL bRet = FALSE;
try
{
db.execDML(_T("CREATE TABLE Main(")
_T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
_T("lVersion INTEGER, ")
_T("mText TEXT);"));
db.execDML(_T("CREATE TABLE Data(")
_T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
_T("lParentID INTEGER, ")
_T("strClipBoardFormat TEXT, ")
_T("lOriginalSize INTEGER, ")
_T("ooData BLOB);"));
db.execDML(_T("CREATE UNIQUE INDEX Main_ID on Main(lID ASC)"));
db.execDML(_T("CREATE UNIQUE INDEX Data_ID on Data(lID ASC)"));
db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
_T("BEGIN\n")
_T("DELETE FROM Data WHERE lParentID = old.lID;\n")
_T("END\n"));
bRet = TRUE;
}
CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
return bRet;
}
BOOL CClipIDs::Export(CString csFilePath)
{
INT_PTR count = GetSize();
if(count == 0)
return TRUE;
BOOL bRet = FALSE;
if(FileExists(csFilePath) && DeleteFile(csFilePath) == FALSE)
{
Log(StrF(_T("Export::Error deleting the file %s"), csFilePath));
return FALSE;
}
try
{
CppSQLite3DB db;
db.open(csFilePath);
if(CreateExportSqliteDB(db) == FALSE)
return FALSE;
for(int i = 0; i < count; i++)
{
int nID = ElementAt(i);
CClip_ImportExport clip;
if(clip.LoadMainTable(nID))
{
if(clip.LoadFormats(nID))
{
clip.ExportToSqliteDB(db);
bRet = TRUE;
}
}
}
db.close();
}
CATCH_SQLITE_EXCEPTION
return bRet;
}