-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdjsearch.cpp
241 lines (184 loc) · 6.72 KB
/
tdjsearch.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
/*
The Daily Journal - A PIM program
Qt version
begin : 12 July 2013
copyright : (C) Kartik Patel
email : letapk@gmail.com
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
*/
//Last modified 19 June 2022
#include "tdj.h"
extern char iniVector[];//initialization vector
extern size_t blkLength;
extern size_t txtLength; // string plus termination
extern size_t txtLengthpadded; // string plus padded zero characters
extern int padding, tdremainder;
extern char *toencrypt, *encrypted, *todecrypt, *decrypted;
extern FILE *encf;
extern int aesenc (void);
extern int aesdec (void);
extern void crypt_error_notification (const char *errstr);
extern void set_decrypt_variables(void);
extern void free_dec_strings (void);
extern QMessageBox *msgBox;
extern size_t szt;
void MainWindow::search_data()
{
int inivecflag = 0;
//save_note();
get_appointment_items ();
write_journal_file(inivecflag);
write_appt_file(inivecflag);
srchresults->setPlainText(tr(""));
srchtxt = searchtxtbox->text();
if (srchtxt.isEmpty() == true) {
statustext->setText(tr("Please enter text in search box"));
return;
}
search_list_notes();
srchresults->append(tr("\n"));
search_list_appts();
statustext->setText(tr("Search complete"));
}
void MainWindow::search_list_notes()
{
QDir dir;
QString path;
QStringList fltr, result;
QFileInfo fileInfo;
QFileInfoList list;
fltr << tr("Notes*.tdj");
dir.setNameFilters(fltr);
dir.setFilter(QDir::Files);
path.append (Homepath);
dir.setPath(path);
list = dir.entryInfoList();//list of files
for (int i = 0; i < list.size(); ++i) {
fileInfo = list.at(i);//file at position i in the list
search_notes_file (fileInfo, &result);
}
}
void MainWindow::search_notes_file(QFileInfo fi, QStringList *result)
{
QTextDocument *doc;
QString s, s1, s2, s3, fname, Year, Month;
int i, j[32], k;
encf = fopen (fi.filePath().toUtf8().data(), "r");
if (encf == NULL) {
*result += tr("");
return;
}
for (i = 1; i <= 31; i++) {
j[i] = 0;
}
fname = fi.baseName();//filename without path and extension : "Notes-xxxx-xx"
Month = fname.remove (0, 11);//remove leading part : "Notes-xxxx-". Only "xx" remains
fname = fi.baseName();//filename without path and extension : "Notes-xxxx-xx"
Year = fname.remove(0, 6);//remove leading part of Notefilename : "Notes-". Only "xxxx-xx" remains
Year.truncate(4);//remove trailing part : "-xx". Only "xxxx" remains.
szt = fread (&iniVector, sizeof (char), 16, encf);
for (i = 1; i <= 31; i++) {//length of each note
szt = fread (&(j[i]), sizeof (int), 1, encf);
}
for (i = 1; i <= 31; i++) {
if (j[i] > 0) {
txtLength = j[i];
set_decrypt_variables ();
//read the data to be decrypted
szt = fread (todecrypt, txtLengthpadded, 1, encf);
//decrypt the data and put it in decrypted
k = aesdec ();
if (k == 1) {
crypt_error_notification ("Error in decryption of data.");
}
//put decrypted data into note for this day
s.clear();
s.append((const char *)decrypted);
//free the buffers
free_dec_strings ();
doc = new QTextDocument ();
doc->setHtml(s);
s = doc->toPlainText();
delete doc;
if (s.contains(&srchtxt, Qt::CaseInsensitive) == true) {
s1 = get_month_name(Month.toInt());
s2.setNum(i);
s3 = QString (tr("Found in the note for %1 %2, %3")).arg(s1).arg(s2).arg(Year);
result->append(s3);
srchresults->append(s3);
}
}
}
fclose (encf);
}
void MainWindow::search_list_appts()
{
QDir dir;
QString path;
QStringList fltr, result;
QFileInfo fileInfo;
QFileInfoList list;
fltr << tr("Appointments*.tdj");
dir.setNameFilters(fltr);
dir.setFilter(QDir::Files);
path.append (Homepath);
dir.setPath(path);
list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i) {
fileInfo = list.at(i);
search_appts_file (fileInfo, &result);
}
}
void MainWindow::search_appts_file(QFileInfo fi, QStringList *result)
{
QString s, s1, s2, s3, fname, Year, Month;
uint i, k, row;
encf = fopen (fi.filePath().toUtf8().data(), "r");
if (encf == NULL)
return;
fname = fi.baseName();//filename without path and extension : "Appointments-xxxx-xx"
Month = fname.remove (0, 18);//remove leading part : "Appointments-xxxx-"
fname = fi.baseName();//filename without path and extension : "Appointments-xxxx-xx".
Year = fname.remove(0, 13);//remove leading part of Apptfilename : "Appointments-". Only "xxxx-xx" remains
Year.truncate(4);//remove trailing part : "-xx". Only "xxxx" remains.
szt = fread (&iniVector, sizeof (char), 16, encf);
for (i = 1; i <= 31; i++) {
for(row = 0; row < 48; row++) {
//read and skip the time
//read the size of the time
szt = fread (&txtLength, sizeof (int), 1, encf);
set_decrypt_variables();
//read the encrypted time
szt = fread (todecrypt, txtLengthpadded, 1, encf);
//free the buffers
free_dec_strings ();
//read the size of the description
szt = fread (&txtLength, sizeof (int), 1, encf);
set_decrypt_variables();
//read the encrypted description
szt = fread (todecrypt, txtLengthpadded, 1, encf);
k = aesdec ();
if (k == 1) {
crypt_error_notification ("Error in decryption of appointments data.");
}
//assign the description to the appointment
s.clear();
s.append(decrypted);
//free the buffers
free_dec_strings ();
if (s.contains(&srchtxt, Qt::CaseInsensitive) == true) {
s1 = get_month_name(Month.toInt());
s2.setNum(i);
s3 = QString (tr("Found in the appointments for %1 %2, %3")).arg(s1).arg(s2).arg(Year);
result->append(s3);
srchresults->append(s3);
}
}
}
fclose(encf);
}