summaryrefslogtreecommitdiff
path: root/plugins/DbChecker/src/worker.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2021-06-06 14:25:08 +0300
committerGeorge Hazan <ghazan@miranda.im>2021-06-06 14:25:08 +0300
commita243ea9b99bfb820b7bfad322e6a97246d88c6c2 (patch)
tree81bc1a3cab152b3f599de0ffd6ae623a29ce022e /plugins/DbChecker/src/worker.cpp
parent3282476d92aa415a138697669e8000eb54b04d0c (diff)
Dbchecker: restored ability to fix utf-8 in old events
Diffstat (limited to 'plugins/DbChecker/src/worker.cpp')
-rw-r--r--plugins/DbChecker/src/worker.cpp54
1 files changed, 50 insertions, 4 deletions
diff --git a/plugins/DbChecker/src/worker.cpp b/plugins/DbChecker/src/worker.cpp
index a31d723eff..433f9ca386 100644
--- a/plugins/DbChecker/src/worker.cpp
+++ b/plugins/DbChecker/src/worker.cpp
@@ -21,6 +21,40 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
void ProcessingDone(void);
+static bool ConvertOldEvent(DBEVENTINFO &dbei)
+{
+ if (dbei.flags & DBEF_UTF)
+ return false;
+
+ int msglen = (int)mir_strlen((char *)dbei.pBlob) + 1, msglenW = 0;
+ if (msglen != (int)dbei.cbBlob) {
+ int count = ((dbei.cbBlob - msglen) / sizeof(WCHAR));
+ WCHAR *p = (WCHAR *)&dbei.pBlob[msglen];
+ for (int i = 0; i < count; i++) {
+ if (p[i] == 0) {
+ msglenW = i;
+ break;
+ }
+ }
+ }
+
+ char *utf8str;
+ if (msglenW > 0 && msglenW <= msglen)
+ utf8str = mir_utf8encodeW((WCHAR *)&dbei.pBlob[msglen]);
+ else
+ utf8str = mir_utf8encode((char *)dbei.pBlob);
+
+ if (utf8str == nullptr)
+ return false;
+
+ mir_free(dbei.pBlob);
+
+ dbei.flags |= DBEF_UTF;
+ dbei.cbBlob = (DWORD)mir_strlen(utf8str);
+ dbei.pBlob = (PBYTE)utf8str;
+ return true;
+}
+
void __cdecl WorkerThread(DbToolOptions *opts)
{
time_t ts = time(nullptr);
@@ -29,25 +63,37 @@ void __cdecl WorkerThread(DbToolOptions *opts)
DWORD sp = 0;
- if (opts->bMarkRead) {
- int nCount = 0;
+ if (opts->bMarkRead || opts->bCheckUtf) {
+ int nCount = 0, nUtfCount = 0;
for (auto &cc : Contacts()) {
DB::ECPTR pCursor(DB::Events(cc));
while (MEVENT hEvent = pCursor.FetchNext()) {
- DBEVENTINFO dbei = {};
+ DB::EventInfo dbei;
+ if (opts->bCheckUtf) // read also event's body
+ dbei.cbBlob = -1;
if (db_event_get(hEvent, &dbei))
continue;
- if (!dbei.markedRead()) {
+ if (opts->bMarkRead && !dbei.markedRead()) {
db_event_markRead(cc, hEvent);
nCount++;
}
+
+ if (opts->bCheckUtf && dbei.eventType == EVENTTYPE_MESSAGE) {
+ if (ConvertOldEvent(dbei)) {
+ db_event_edit(cc, hEvent, &dbei);
+ nUtfCount++;
+ }
+ }
}
}
if (nCount)
AddToStatus(STATUS_MESSAGE, TranslateT("%d events marked as read"), nCount);
+
+ if (nUtfCount)
+ AddToStatus(STATUS_MESSAGE, TranslateT("Utf-8 encoding fixed in %d events"), nUtfCount);
}
DBCHeckCallback callback;