-
Notifications
You must be signed in to change notification settings - Fork 19
/
smartiodboxsingle.cpp
644 lines (521 loc) · 14.9 KB
/
smartiodboxsingle.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
Copyright (C) 2014 Sialan Labs
http://labs.sialan.org
Kaqaz 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 3 of the License, or
(at your option) any later version.
Kaqaz is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "kaqazmacros.h"
#define BEGIN_FNC \
if(p->started) return; \
BEGIN_FNC_DEBUG \
p->started = true; \
emit started(this);
#define END_FNC { \
p->started = false; \
emit finished(this); \
END_FNC_DEBUG \
return; }
#define NAME_OF(METHOD) #METHOD
#define WRITE_REPEAT_FAILED( FILE, DATA ) { \
if( DATA.isEmpty() ) \
END_FNC \
qint64 res = -1; \
bool flush = false; \
int counter = 0; \
do { \
if( FILE.open(QDropboxFile::WriteOnly) ) { \
res = FILE.write(DATA); \
flush = FILE.flush(); \
FILE.close(); \
if( !flush ) \
res = -1; \
} \
counter++; \
} while( res == -1 && counter <= 5 ); \
if( res == -1 && counter > 5 ) { \
smart_io_dbox_fatal = true; \
END_FNC } \
}
#define READ_REPEAT_FAILED( FILE, RES, FAILED_CMD ) \
QByteArray RES; { \
int counter = 0; \
do { \
if( FILE.open(QDropboxFile::ReadOnly) ) {\
RES = FILE.readAll(); \
FILE.close(); \
} \
counter++; \
} while( RES.isEmpty() && counter <= 5 ); \
if( RES.isEmpty() ) { \
FAILED_CMD; \
smart_io_dbox_fatal = true; \
END_FNC \
} \
}
#define CHECK_DATA( DATA, RES ) \
const QByteArray & RES = DATA; \
if( RES.isEmpty() ) \
END_FNC
#define PAPER_DATA_HEADER QString("KaqazPaper")
#define PAPER_DATA_VERSION QString("1.3")
#define GROUP_DATA_HEADER QString("KaqazGroups")
#define GROUP_DATA_VERSION QString("1.0")
#define CALL_CORE_VOID( METHOD ) SialanTools::call(p->core, NAME_OF(METHOD), Qt::QueuedConnection );
#define CALL_CORE( METHOD, ... ) SialanTools::call(p->core, NAME_OF(METHOD), Qt::QueuedConnection, __VA_ARGS__ );
#define ALLOC_DBOX(NAME) \
QDropbox NAME( DROPBOX_APP_KEY, DROPBOX_APP_SECRET, QDropbox::Plaintext, "api.dropbox.com", this ); \
NAME.setAuthMethod(QDropbox::Plaintext); \
NAME.setToken(p->token); \
NAME.setTokenSecret(p->tokenSecret);
#define ALLOC_DBOX_FILE(FILE,PATH) \
ALLOC_DBOX(dbox) \
QDropboxFile FILE(PATH,&dbox);
#include "smartiodboxsingle.h"
#include "kaqaz.h"
#include "database.h"
#include "sialantools/sialantools.h"
#include "kaqazmacros.h"
#include "qdropbox.h"
#include "qdropboxfile.h"
#include "qdropboxfileinfo.h"
#include "simpleqtcryptor/simpleqtcryptor.h"
#include <QFileInfo>
#include <QFile>
#include <QCoreApplication>
#include <QThread>
#include <QEventLoop>
#include <QMutex>
#include <QDebug>
#include <QBuffer>
#include <QDataStream>
#include <QGeoCoordinate>
bool smart_io_dbox_fatal = false;
class SmartIODBoxSinglePrivate
{
public:
QString password;
bool started;
QThread *thread;
QEventLoop *loop;
QString token;
QString tokenSecret;
SmartIODBoxSingleCore *core;
QByteArray cache;
QMutex cache_mutex;
};
SmartIODBoxSingle::SmartIODBoxSingle(QObject *parent) :
QObject(parent)
{
p = new SmartIODBoxSinglePrivate;
p->started = false;
p->loop = new QEventLoop(this);
p->core = new SmartIODBoxSingleCore();
p->core->moveToThread( Kaqaz::database()->thread() );
p->thread = new QThread();
moveToThread(p->thread);
p->thread->start();
connect( p->thread, SIGNAL(destroyed()) , SLOT(deleteLater()) , Qt::QueuedConnection );
connect( p->core , SIGNAL(paperIsReady(QByteArray)) , SLOT(cacheIsReady(QByteArray)), Qt::QueuedConnection );
connect( p->core , SIGNAL(groupsIsReady(QByteArray)), SLOT(cacheIsReady(QByteArray)), Qt::QueuedConnection );
connect( p->core , SIGNAL(finished()) , SLOT(coreFinished()) , Qt::QueuedConnection );
}
void SmartIODBoxSingle::setToken(const QString &t)
{
p->token = t;
}
QString SmartIODBoxSingle::token() const
{
return p->token;
}
void SmartIODBoxSingle::setTokenSecret(const QString &s)
{
p->tokenSecret = s;
}
QString SmartIODBoxSingle::tokenSecret() const
{
return p->tokenSecret;
}
void SmartIODBoxSingle::setPassword(const QString &pass)
{
if( p->password == pass )
return;
p->password = pass;
emit passwordChanged();
}
QString SmartIODBoxSingle::password() const
{
return p->password;
}
void SmartIODBoxSingle::pushFile(const QString &file, const QString & dest)
{
BEGIN_FNC
QFile src(file);
if( !src.open(QFile::ReadOnly) )
END_FNC
ALLOC_DBOX_FILE(dst,dest);
CHECK_DATA(src.readAll(),data);
const QByteArray & _d = encryptData(data);
WRITE_REPEAT_FAILED( dst, _d )
src.close();
END_FNC
}
void SmartIODBoxSingle::fetchFile(const QString &path, const QString & dest)
{
BEGIN_FNC
QFile ofile(dest);
ofile.remove();
if( !ofile.open(QFile::WriteOnly) )
END_FNC
ALLOC_DBOX_FILE(file,path)
READ_REPEAT_FAILED( file, _d, ofile.close();ofile.remove(); );
CHECK_DATA( decryptData(_d), data );
ofile.write(data);
ofile.flush();
ofile.close();
END_FNC
}
void SmartIODBoxSingle::pushPaper(const QString &uuid, qint64 revision, const QString & dest)
{
BEGIN_FNC
QString gid = uuid;
gid.remove("{").remove("}");
CALL_CORE(requestPaperToSync, uuid );
p->loop->exec();
const QByteArray cached_data = p->cache;
if( cached_data.isEmpty() )
END_FNC;
const QByteArray & _d = encryptData(cached_data);
ALLOC_DBOX_FILE(file,dest)
WRITE_REPEAT_FAILED( file, _d )
CALL_CORE(requestPaperToSync, uuid );
p->loop->exec();
if( p->cache != cached_data )
END_FNC
CALL_CORE( paperPushed, uuid, revision );
p->loop->exec();
emit revisionChanged( gid, revision );
END_FNC
}
void SmartIODBoxSingle::fetchPaper(const QString &uuid, qint64 revision, const QString & path)
{
BEGIN_FNC
QString gid = uuid;
gid.remove("{").remove("}");
ALLOC_DBOX_FILE(file,path)
READ_REPEAT_FAILED( file, _d, (void)path )
const QByteArray & data = decryptData(_d);
if( data.isEmpty() )
END_FNC
CALL_CORE( paperFetched, uuid, data, revision );
p->loop->exec();
emit revisionChanged( gid, revision );
END_FNC
}
void SmartIODBoxSingle::pushGroups(const QString & path, qint64 revision)
{
BEGIN_FNC
CALL_CORE_VOID( requestGroupsToSync );
p->loop->exec();
if( p->cache.isEmpty() )
END_FNC
const QByteArray & _d = encryptData(p->cache);
ALLOC_DBOX_FILE(file,path);
WRITE_REPEAT_FAILED( file, _d );
CALL_CORE( groupsPushed, revision );
p->loop->exec();
emit revisionChanged(GROUPS_SYNC_KEY,revision);
END_FNC
}
void SmartIODBoxSingle::fetchGroups(const QString & path, qint64 revision, qint64 current_revision)
{
BEGIN_FNC
ALLOC_DBOX_FILE(file,path);
READ_REPEAT_FAILED( file, _d, (void)path )
const QByteArray & data = decryptData(_d);
if( data.isEmpty() )
END_FNC
CALL_CORE( groupsFetched, data, revision, current_revision );
p->loop->exec();
emit revisionChanged( GROUPS_SYNC_KEY, revision );
END_FNC
}
void SmartIODBoxSingle::setDeleted(const QString &path)
{
BEGIN_FNC
ALLOC_DBOX_FILE(file,path);
WRITE_REPEAT_FAILED( file, QByteArray("DELETED") )
emit revisionChanged(path,-2);
END_FNC
}
void SmartIODBoxSingle::close()
{
BEGIN_FNC_DEBUG
p->thread->quit();
p->thread->wait();
p->thread->deleteLater();
END_FNC_DEBUG
}
QString SmartIODBoxSingle::cache() const
{
BEGIN_FNC_DEBUG
p->cache_mutex.lock();
QString result = p->cache;
p->cache_mutex.unlock();
END_FNC_DEBUG
return result;
}
bool SmartIODBoxSingle::fatalError()
{
return smart_io_dbox_fatal;
}
void SmartIODBoxSingle::cacheIsReady(const QByteArray &data)
{
BEGIN_FNC_DEBUG
p->cache_mutex.lock();
p->cache = data;
p->cache_mutex.unlock();
END_FNC_DEBUG
}
void SmartIODBoxSingle::coreFinished()
{
BEGIN_FNC_DEBUG
p->loop->exit();
END_FNC_DEBUG
}
QByteArray SmartIODBoxSingle::encryptData(const QByteArray &data)
{
BEGIN_FNC_DEBUG
QSharedPointer<SimpleQtCryptor::Key> gKey = QSharedPointer<SimpleQtCryptor::Key>(new SimpleQtCryptor::Key(p->password));
SimpleQtCryptor::Encryptor enc( gKey, SimpleQtCryptor::SERPENT_32, SimpleQtCryptor::ModeCFB, SimpleQtCryptor::NoChecksum );
QByteArray enc_new_data;
enc.encrypt( data, enc_new_data, true );
BEGIN_FNC_DEBUG
return enc_new_data;
}
QByteArray SmartIODBoxSingle::decryptData(const QByteArray &sdata)
{
BEGIN_FNC_DEBUG
QSharedPointer<SimpleQtCryptor::Key> gKey = QSharedPointer<SimpleQtCryptor::Key>(new SimpleQtCryptor::Key(p->password));
SimpleQtCryptor::Decryptor dec( gKey, SimpleQtCryptor::SERPENT_32, SimpleQtCryptor::ModeCFB );
QByteArray enc_code_dec;
if( dec.decrypt(sdata,enc_code_dec,true) == SimpleQtCryptor::ErrorInvalidKey )
{
END_FNC_DEBUG
return enc_code_dec;
}
END_FNC_DEBUG
return enc_code_dec;
}
SmartIODBoxSingle::~SmartIODBoxSingle()
{
BEGIN_FNC_DEBUG
p->core->deleteLater();
END_FNC_DEBUG
delete p;
}
void SmartIODBoxSingleCore::requestPaperToSync(const QString &uuid)
{
BEGIN_FNC_DEBUG
Database *db = Kaqaz::database();
int paperId = db->paperUuidId(uuid);
if( paperId == -1 )
{
emit finished();
END_FNC_DEBUG
return;
}
QByteArray d;
QBuffer buffer(&d);
buffer.open(QBuffer::WriteOnly);
QDataStream stream(&buffer);
stream << PAPER_DATA_HEADER;
stream << PAPER_DATA_VERSION;
stream << db->paperTitle(paperId);
stream << db->paperLocation(paperId);
stream << db->paperCreatedDate(paperId).toString();
stream << db->paperModifiedDate(paperId).toString();
stream << db->groupUuid( db->paperGroup(paperId) );
stream << db->paperFiles(paperId);
stream << db->paperText(paperId);
stream << db->paperType(paperId);
stream << db->paperWeather(paperId);
stream << db->paperTemperature(paperId);
buffer.close();
emit paperIsReady(d);
emit finished();
END_FNC_DEBUG
}
void SmartIODBoxSingleCore::requestGroupsToSync()
{
BEGIN_FNC_DEBUG
Database *db = Kaqaz::database();
QByteArray data;
QBuffer mainBuffer(&data);
mainBuffer.open(QBuffer::WriteOnly);
QDataStream mainStream(&mainBuffer);
mainStream << GROUP_DATA_HEADER;
mainStream << GROUP_DATA_VERSION;
const QList<int> & groups = db->groups();
foreach( int gr, groups )
{
QByteArray d;
QBuffer buffer(&d);
buffer.open(QBuffer::WriteOnly);
QDataStream stream(&buffer);
stream << db->groupUuid(gr);
stream << db->groupColor(gr).name();
stream << db->groupName(gr);
buffer.close();
mainStream << d;
}
mainBuffer.close();
emit groupsIsReady(data);
emit finished();
END_FNC_DEBUG
}
void SmartIODBoxSingleCore::paperPushed(const QString &id, quint64 revision)
{
BEGIN_FNC_DEBUG
Database *db = Kaqaz::database();
db->setSignalBlocker(true);
db->setRevision(id,revision);
db->setSignalBlocker(false);
emit finished();
END_FNC_DEBUG
}
void SmartIODBoxSingleCore::paperFetched(const QString &uuid, const QByteArray &d_const, quint64 revision)
{
BEGIN_FNC_DEBUG
if( d_const.isEmpty() )
{
emit finished();
END_FNC_DEBUG
return;
}
Database *db = Kaqaz::database();
db->setSignalBlocker(true);
QByteArray d = d_const;
QBuffer buffer(&d);
buffer.open(QBuffer::ReadOnly);
QDataStream stream(&buffer);
QString tmp_string;
QString header;
QString version;
QString title;
QGeoCoordinate location;
QString date;
QString mdate;
QString group;
QStringList files;
QString body;
int type = Enums::Normal;
QString weather;
int temperature = 0;
stream >> header;
stream >> version;
stream >> title;
if( version == "1.0" )
stream >> tmp_string;
else
stream >> location;
stream >> date;
stream >> mdate;
stream >> group;
stream >> files;
stream >> body;
if( version.toDouble() > 1.1 )
{
stream >> type;
}
if( version.toDouble() > 1.2 )
{
stream >> weather;
stream >> temperature;
}
db->setPaper( uuid, title, body, group, date, location, type, weather, temperature );
db->setRevision( uuid, revision );
int paperId = db->paperUuidId(uuid);
for( int i=files.count()-1; i>=0 ; i-- )
db->addFileToPaper( paperId, files[i] );
db->setSignalBlocker(false);
emit finished();
END_FNC_DEBUG
}
void SmartIODBoxSingleCore::groupsPushed(quint64 revision)
{
BEGIN_FNC_DEBUG
Database *db = Kaqaz::database();
db->setSignalBlocker(true);
db->setRevision( GROUPS_SYNC_KEY, revision );
db->setSignalBlocker(false);
emit finished();
END_FNC_DEBUG
}
void SmartIODBoxSingleCore::groupsFetched(const QByteArray &data, quint64 revision, qint64 current_revision)
{
BEGIN_FNC_DEBUG
if( data.isEmpty() )
{
emit finished();
END_FNC_DEBUG
return;
}
Database *db = Kaqaz::database();
db->setSignalBlocker(true);
QByteArray mainData = data;
QBuffer mainBuffer(&mainData);
mainBuffer.open(QBuffer::ReadOnly);
QDataStream mainStream(&mainBuffer);
QString header;
QString version;
mainStream >> header;
mainStream >> version;
while( !mainStream.atEnd() )
{
QByteArray record;
mainStream >> record;
QBuffer buffer(&record);
buffer.open(QBuffer::ReadOnly);
QDataStream stream(&buffer);
QString uuid;
QString color;
QString name;
stream >> uuid;
stream >> color;
stream >> name;
db->setGroup( uuid, name, color );
buffer.close();
}
mainBuffer.close();
db->setRevision( GROUPS_SYNC_KEY, revision );
if( current_revision == -1 )
db->setRevision( GROUPS_SYNC_KEY, -1 );
db->setSignalBlocker(false);
emit finished();
END_FNC_DEBUG
}
void SmartIODBoxSingleCore::paperDeleted(const QString &id)
{
BEGIN_FNC_DEBUG
Database *db = Kaqaz::database();
int paperId = db->paperUuidId(id);
if( paperId == -1 )
{
emit finished();
END_FNC_DEBUG
return;
}
db->setSignalBlocker(true);
db->deletePaper( paperId );
db->setSignalBlocker(false);
emit finished();
END_FNC_DEBUG
}