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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
/*
Copyright 2012-2014 Adam Reichold
Copyright 2012 MichaĆ Trybus
This file is part of qpdfview.
qpdfview 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.
qpdfview 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 qpdfview. If not, see <http://www.gnu.org/licenses/>.
*/
#include "database.h"
#include <QApplication>
#include <QCryptographicHash>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QStandardPaths>
#else
#include <QDesktopServices>
#endif // QT_VERSION
#ifdef WITH_SQL
#include <QSqlError>
#include <QSqlQuery>
#endif // WITH_SQL
#include "settings.h"
#include "documentview.h"
#include "bookmarkmenu.h"
namespace
{
class Transaction
{
public:
Transaction(QSqlDatabase& database) : m_committed(false), m_database(database)
{
m_database.transaction();
}
~Transaction()
{
if(!m_committed)
{
m_database.rollback();
}
}
void commit()
{
m_committed = m_database.commit();
}
private:
bool m_committed;
QSqlDatabase& m_database;
};
} // anonymous
namespace qpdfview
{
Database* Database::s_instance = 0;
Database* Database::instance()
{
if(s_instance == 0)
{
s_instance = new Database(qApp);
}
return s_instance;
}
Database::~Database()
{
s_instance = 0;
}
QStringList Database::loadInstanceNames()
{
QStringList instanceNames;
#ifdef WITH_SQL
if(m_database.isOpen())
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.exec("SELECT DISTINCT(instanceName) FROM tabs_v2");
while(query.next())
{
if(!query.isActive())
{
qDebug() << query.lastError();
return QStringList();
}
if(!query.value(0).toString().isEmpty())
{
instanceNames.append(query.value(0).toString());
}
}
transaction.commit();
}
#endif // WITH_SQL
return instanceNames;
}
void Database::restoreTabs()
{
#ifdef WITH_SQL
if(m_database.isOpen())
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.prepare("SELECT filePath,currentPage,continuousMode,layoutMode,scaleMode,scaleFactor,rotation FROM tabs_v2 WHERE instanceName==?");
query.bindValue(0, instanceName());
query.exec();
while(query.next())
{
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
emit tabRestored(query.value(0).toString(),
static_cast< bool >(query.value(2).toUInt()),
static_cast< LayoutMode >(query.value(3).toUInt()),
static_cast< ScaleMode >(query.value(4).toUInt()),
query.value(5).toReal(),
static_cast< Rotation >(query.value(6).toUInt()),
query.value(1).toInt());
}
transaction.commit();
}
#endif // WITH_SQL
}
void Database::saveTabs(const QList< const DocumentView* >& tabs)
{
#ifdef WITH_SQL
if(m_database.isOpen())
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
if(Settings::instance()->mainWindow().restoreTabs())
{
query.prepare("DELETE FROM tabs_v2 WHERE instanceName==?");
query.bindValue(0, instanceName());
query.exec();
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
query.prepare("INSERT INTO tabs_v2 "
"(filePath,instanceName,currentPage,continuousMode,layoutMode,scaleMode,scaleFactor,rotation)"
" VALUES (?,?,?,?,?,?,?,?)");
foreach(const DocumentView* tab, tabs)
{
query.bindValue(0, tab->fileInfo().absoluteFilePath());
query.bindValue(1, instanceName());
query.bindValue(2, tab->currentPage());
query.bindValue(3, static_cast< uint >(tab->continuousMode()));
query.bindValue(4, static_cast< uint >(tab->layoutMode()));
query.bindValue(5, static_cast< uint >(tab->scaleMode()));
query.bindValue(6, tab->scaleFactor());
query.bindValue(7, static_cast< uint >(tab->rotation()));
query.exec();
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
}
}
else
{
query.exec("DELETE FROM tabs_v2");
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
}
transaction.commit();
}
#else
Q_UNUSED(tabs);
#endif // WITH_SQL
}
void Database::restoreBookmarks()
{
#ifdef WITH_SQL
if(m_database.isOpen())
{
Transaction transaction(m_database);
QSqlQuery outerQuery(m_database);
outerQuery.exec("SELECT DISTINCT(filePath) FROM bookmarks_v2");
while(outerQuery.next())
{
if(!outerQuery.isActive())
{
qDebug() << outerQuery.lastError();
return;
}
const QString filePath = outerQuery.value(0).toString();
QList< QPair< int, QString > > jumps;
QSqlQuery innerQuery(m_database);
innerQuery.prepare("SELECT page,label FROM bookmarks_v2 WHERE filePath==?");
innerQuery.bindValue(0, filePath);
innerQuery.exec();
while(innerQuery.next())
{
if(!innerQuery.isActive())
{
qDebug() << innerQuery.lastError();
return;
}
const int page = innerQuery.value(0).toInt();
const QString label = innerQuery.value(1).toString();
jumps.append(qMakePair(page, label));
}
emit bookmarkRestored(filePath, jumps);
}
transaction.commit();
}
#endif // WITH_SQL
}
void Database::saveBookmarks(const QList< const BookmarkMenu* >& bookmarks)
{
#ifdef WITH_SQL
if(m_database.isOpen())
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.exec("DELETE FROM bookmarks_v2");
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
if(Settings::instance()->mainWindow().restoreBookmarks())
{
query.prepare("INSERT INTO bookmarks_v2 "
"(filePath,page,label)"
" VALUES (?,?,?)");
foreach(const BookmarkMenu* bookmark, bookmarks)
{
foreach(const Jump jump, bookmark->jumps())
{
query.bindValue(0, bookmark->absoluteFilePath());
query.bindValue(1, jump.first);
query.bindValue(2, jump.second);
query.exec();
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
}
}
}
transaction.commit();
}
#else
Q_UNUSED(bookmarks);
#endif // WITH_SQL
}
void Database::restorePerFileSettings(DocumentView* tab)
{
#ifdef WITH_SQL
if(Settings::instance()->mainWindow().restorePerFileSettings() && m_database.isOpen() && tab != 0)
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.prepare("SELECT currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation FROM perfilesettings_v2 WHERE filePath==?");
query.bindValue(0, QCryptographicHash::hash(tab->fileInfo().absoluteFilePath().toUtf8(), QCryptographicHash::Sha1).toBase64());
query.exec();
if(query.next())
{
tab->setContinuousMode(query.value(1).toBool());
tab->setLayoutMode(static_cast< LayoutMode >(query.value(2).toUInt()));
tab->setRightToLeftMode(query.value(3).toBool());
tab->setScaleMode(static_cast< ScaleMode >(query.value(4).toUInt()));
tab->setScaleFactor(query.value(5).toReal());
tab->setRotation(static_cast< Rotation >(query.value(6).toUInt()));
tab->jumpToPage(query.value(0).toInt(), false);
}
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
transaction.commit();
}
#else
Q_UNUSED(tab);
#endif // WITH_SQL
}
void Database::savePerFileSettings(const DocumentView* tab)
{
#ifdef WITH_SQL
if(Settings::instance()->mainWindow().restorePerFileSettings() && m_database.isOpen() && tab != 0)
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.prepare("INSERT OR REPLACE INTO perfilesettings_v2 "
"(lastUsed,filePath,currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation)"
" VALUES (?,?,?,?,?,?,?,?,?)");
query.bindValue(0, QDateTime::currentDateTime().toTime_t());
query.bindValue(1, QCryptographicHash::hash(tab->fileInfo().absoluteFilePath().toUtf8(), QCryptographicHash::Sha1).toBase64());
query.bindValue(2, tab->currentPage());
query.bindValue(3, static_cast< uint >(tab->continuousMode()));
query.bindValue(4, static_cast< uint >(tab->layoutMode()));
query.bindValue(5, static_cast< uint >(tab->rightToLeftMode()));
query.bindValue(6, static_cast< uint >(tab->scaleMode()));
query.bindValue(7, tab->scaleFactor());
query.bindValue(8, static_cast< uint >(tab->rotation()));
query.exec();
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
transaction.commit();
}
#else
Q_UNUSED(tab);
#endif // WITH_SQL
}
Database::Database(QObject* parent) : QObject(parent)
{
#ifdef WITH_SQL
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
#else
const QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
#endif // QT_VERSION
QDir().mkpath(path);
m_database = QSqlDatabase::addDatabase("QSQLITE");
m_database.setDatabaseName(QDir(path).filePath("database"));
m_database.open();
if(m_database.isOpen())
{
const QStringList tables = m_database.tables();
// tabs
if(!tables.contains("tabs_v2"))
{
if(prepareTabs_v2() && tables.contains("tabs_v1"))
{
migrateTabs_v1_v2();
}
}
// bookmarks
if(!tables.contains("bookmarks_v2"))
{
if(prepareBookmarks_v2() && tables.contains("bookmarks_v1"))
{
migrateBookmarks_v1_v2();
}
}
// per-file settings
if(!tables.contains("perfilesettings_v2"))
{
if(preparePerFileSettings_v2() && tables.contains("perfilesettings_v1"))
{
migratePerFileSettings_v1_v2();
}
}
}
else
{
qDebug() << m_database.lastError();
}
#endif // WITH_SQL
}
QString Database::instanceName()
{
return !qApp->objectName().isNull() ? qApp->objectName() : QString("");
}
#ifdef WITH_SQL
bool Database::prepareTabs_v2()
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.exec("CREATE TABLE tabs_v2 "
"(filePath TEXT"
",instanceName TEXT"
",currentPage INTEGER"
",continuousMode INTEGER"
",layoutMode INTEGER"
",scaleMode INTEGER"
",scaleFactor REAL"
",rotation INTEGER)");
if(!query.isActive())
{
qDebug() << query.lastError();
return false;
}
transaction.commit();
return true;
}
bool Database::prepareBookmarks_v2()
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.exec("CREATE TABLE bookmarks_v2 "
"(filePath TEXT"
",page INTEGER"
",label TEXT)");
if(!query.isActive())
{
qDebug() << query.lastError();
return false;
}
transaction.commit();
return true;
}
bool Database::preparePerFileSettings_v2()
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.exec("CREATE TABLE perfilesettings_v2 "
"(lastUsed INTEGER"
",filePath TEXT PRIMARY KEY"
",currentPage INTEGER"
",continuousMode INTEGER"
",layoutMode INTEGER"
",rightToLeftMode INTEGER"
",scaleMode INTEGER"
",scaleFactor REAL"
",rotation INTEGER)");
if(!query.isActive())
{
qDebug() << query.lastError();
return false;
}
if(Settings::instance()->mainWindow().restorePerFileSettings())
{
query.exec("DELETE FROM perfilesettings_v2 WHERE filePath IN (SELECT filePath FROM perfilesettings_v2 ORDER BY lastUsed DESC LIMIT -1 OFFSET 1000)");
}
else
{
query.exec("DELETE FROM perfilesettings_v2");
}
if(!query.isActive())
{
qDebug() << query.lastError();
return false;
}
transaction.commit();
return true;
}
void Database::migrateTabs_v1_v2()
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.prepare("INSERT INTO tabs_v2 "
"SELECT filePath,?,currentPage,continuousMode,layoutMode,scaleMode,scaleFactor,rotation "
"FROM tabs_v1");
query.bindValue(0, instanceName());
query.exec();
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
qWarning() << "Migrated tabs from v1 to v2, dropping v1.";
query.exec("DROP TABLE tabs_v1");
transaction.commit();
}
void Database::migrateBookmarks_v1_v2()
{
Transaction transaction(m_database);
QSqlQuery outerQuery(m_database);
outerQuery.exec("SELECT filePath,pages FROM bookmarks_v1");
while(outerQuery.next())
{
if(!outerQuery.isActive())
{
qDebug() << outerQuery.lastError();
return;
}
QSqlQuery innerQuery(m_database);
innerQuery.prepare("INSERT INTO bookmarks_v2 "
"(filePath,page,label)"
" VALUES (?,?,?)");
innerQuery.bindValue(0, outerQuery.value(0));
foreach(QString page, outerQuery.value(1).toString().split(",", QString::SkipEmptyParts))
{
innerQuery.bindValue(1, page);
innerQuery.bindValue(2, tr("Jump to page %1").arg(page));
innerQuery.exec();
if(!innerQuery.isActive())
{
qDebug() << innerQuery.lastError();
return;
}
}
}
if(!outerQuery.isActive())
{
qDebug() << outerQuery.lastError();
return;
}
qWarning() << "Migrated bookmarks from v1 to v2, dropping v1.";
outerQuery.exec("DROP TABLE bookmarks_v1");
transaction.commit();
}
void Database::migratePerFileSettings_v1_v2()
{
Transaction transaction(m_database);
QSqlQuery query(m_database);
query.prepare("INSERT INTO perfilesettings_v2 "
"SELECT lastUsed,filePath,currentPage,continuousMode,layoutMode,0,scaleMode,scaleFactor,rotation "
"FROM perfilesettings_v1");
query.exec();
if(!query.isActive())
{
qDebug() << query.lastError();
return;
}
qWarning() << "Migrated per-file settings from v1 to v2, dropping v1.";
query.exec("DROP TABLE perfilesettings_v1");
transaction.commit();
}
#endif // WITH_SQL
} // qpdfview
|