[1f4adec] | 1 | /* |
---|
| 2 | |
---|
| 3 | Copyright 2014 S. Razi Alavizadeh |
---|
| 4 | Copyright 2012-2015 Adam Reichold |
---|
| 5 | Copyright 2012 Michał Trybus |
---|
| 6 | |
---|
| 7 | This file is part of qpdfview. |
---|
| 8 | |
---|
| 9 | qpdfview is free software: you can redistribute it and/or modify |
---|
| 10 | it under the terms of the GNU General Public License as published by |
---|
| 11 | the Free Software Foundation, either version 2 of the License, or |
---|
| 12 | (at your option) any later version. |
---|
| 13 | |
---|
| 14 | qpdfview is distributed in the hope that it will be useful, |
---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | GNU General Public License for more details. |
---|
| 18 | |
---|
| 19 | You should have received a copy of the GNU General Public License |
---|
| 20 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include "database.h" |
---|
| 25 | |
---|
| 26 | #include <QApplication> |
---|
| 27 | #include <QCryptographicHash> |
---|
| 28 | #include <QDateTime> |
---|
| 29 | #include <QDebug> |
---|
| 30 | #include <QDir> |
---|
| 31 | |
---|
| 32 | #if QT_VERSION >= QT_VERSION_CHECK(5,0,0) |
---|
| 33 | |
---|
| 34 | #include <QStandardPaths> |
---|
| 35 | |
---|
| 36 | #else |
---|
| 37 | |
---|
| 38 | #include <QDesktopServices> |
---|
| 39 | |
---|
| 40 | #endif // QT_VERSION |
---|
| 41 | |
---|
| 42 | #ifdef WITH_SQL |
---|
| 43 | |
---|
| 44 | #include <QSqlError> |
---|
| 45 | #include <QSqlQuery> |
---|
| 46 | |
---|
| 47 | #endif // WITH_SQL |
---|
| 48 | |
---|
| 49 | #include "settings.h" |
---|
| 50 | #include "documentview.h" |
---|
| 51 | #include "bookmarkmodel.h" |
---|
| 52 | |
---|
| 53 | #ifdef WITH_SQL |
---|
| 54 | |
---|
| 55 | namespace |
---|
| 56 | { |
---|
| 57 | |
---|
| 58 | using namespace qpdfview; |
---|
| 59 | |
---|
| 60 | class Transaction |
---|
| 61 | { |
---|
| 62 | public: |
---|
| 63 | Transaction(QSqlDatabase& database) : |
---|
| 64 | m_database(database), |
---|
| 65 | m_committed(false) |
---|
| 66 | { |
---|
| 67 | if(s_current != 0) |
---|
| 68 | { |
---|
| 69 | return; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | if(!m_database.transaction()) |
---|
| 73 | { |
---|
| 74 | throw m_database.lastError(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | s_current = this; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | ~Transaction() throw() |
---|
| 81 | { |
---|
| 82 | if(s_current != this) |
---|
| 83 | { |
---|
| 84 | return; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | if(!m_committed) |
---|
| 88 | { |
---|
| 89 | m_database.rollback(); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | s_current = 0; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void commit() |
---|
| 96 | { |
---|
| 97 | if(s_current != this) |
---|
| 98 | { |
---|
| 99 | return; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if(!m_database.commit()) |
---|
| 103 | { |
---|
| 104 | throw m_database.lastError(); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | m_committed = true; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | private: |
---|
| 111 | Q_DISABLE_COPY(Transaction) |
---|
| 112 | |
---|
| 113 | QSqlDatabase& m_database; |
---|
| 114 | bool m_committed; |
---|
| 115 | |
---|
| 116 | static Transaction* s_current; |
---|
| 117 | |
---|
| 118 | }; |
---|
| 119 | |
---|
| 120 | Transaction* Transaction::s_current = 0; |
---|
| 121 | |
---|
| 122 | class Query |
---|
| 123 | { |
---|
| 124 | private: |
---|
| 125 | template< typename T > |
---|
| 126 | class BindValue |
---|
| 127 | { |
---|
| 128 | private: |
---|
| 129 | friend class Query; |
---|
| 130 | |
---|
| 131 | explicit BindValue(const T& value) : m_value(value) {} |
---|
| 132 | |
---|
| 133 | public: |
---|
| 134 | inline operator QVariant() const |
---|
| 135 | { |
---|
| 136 | return Conversion< T >::convert(m_value); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | private: |
---|
| 140 | const T& m_value; |
---|
| 141 | |
---|
| 142 | template< typename S, bool Defined = QMetaTypeId2< S >::Defined > |
---|
| 143 | struct Conversion |
---|
| 144 | { |
---|
| 145 | static inline QVariant convert(const S& value) |
---|
| 146 | { |
---|
| 147 | return QVariant(value); |
---|
| 148 | } |
---|
| 149 | }; |
---|
| 150 | |
---|
| 151 | template< typename S > |
---|
| 152 | struct Conversion< S, false > |
---|
| 153 | { |
---|
| 154 | static inline QVariant convert(const S& value) |
---|
| 155 | { |
---|
| 156 | return QVariant(static_cast< uint >(value)); |
---|
| 157 | } |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | }; |
---|
| 161 | |
---|
| 162 | public: |
---|
| 163 | class Value |
---|
| 164 | { |
---|
| 165 | private: |
---|
| 166 | friend class Query; |
---|
| 167 | |
---|
| 168 | explicit Value(const QVariant& value) : m_value(value) {} |
---|
| 169 | |
---|
| 170 | public: |
---|
| 171 | template< typename T > |
---|
| 172 | inline operator T() const |
---|
| 173 | { |
---|
| 174 | return Conversion< T >::convert(m_value); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | private: |
---|
| 178 | const QVariant m_value; |
---|
| 179 | |
---|
| 180 | template< typename T, bool Defined = QMetaTypeId2< T >::Defined > |
---|
| 181 | struct Conversion |
---|
| 182 | { |
---|
| 183 | static inline T convert(const QVariant& value) |
---|
| 184 | { |
---|
| 185 | return value.value< T >(); |
---|
| 186 | } |
---|
| 187 | }; |
---|
| 188 | |
---|
| 189 | template< typename T > |
---|
| 190 | struct Conversion< T, false > |
---|
| 191 | { |
---|
| 192 | static inline T convert(const QVariant& value) |
---|
| 193 | { |
---|
| 194 | return static_cast< T >(value.value< uint >()); |
---|
| 195 | } |
---|
| 196 | }; |
---|
| 197 | |
---|
| 198 | }; |
---|
| 199 | |
---|
| 200 | public: |
---|
| 201 | Query(QSqlDatabase& database) : |
---|
| 202 | m_query(database), |
---|
| 203 | m_bindValueIndex(0), |
---|
| 204 | m_valueIndex(0) |
---|
| 205 | { |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | void prepare(const QString& query) |
---|
| 209 | { |
---|
| 210 | if(!m_query.prepare(query)) |
---|
| 211 | { |
---|
| 212 | throw m_query.lastError(); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | m_bindValueIndex = 0; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | void exec() |
---|
| 219 | { |
---|
| 220 | if(!m_query.exec()) |
---|
| 221 | { |
---|
| 222 | throw m_query.lastError(); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | m_bindValueIndex = 0; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void exec(const QString& query) |
---|
| 229 | { |
---|
| 230 | if(!m_query.exec(query)) |
---|
| 231 | { |
---|
| 232 | throw m_query.lastError(); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | m_bindValueIndex = 0; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | Query& operator <<(const QVariant& value) |
---|
| 239 | { |
---|
| 240 | m_query.bindValue(m_bindValueIndex++, value); |
---|
| 241 | |
---|
| 242 | return *this; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | template< typename T > |
---|
| 246 | Query& operator <<(const T& value) |
---|
| 247 | { |
---|
| 248 | m_query.bindValue(m_bindValueIndex++, BindValue< T >(value)); |
---|
| 249 | |
---|
| 250 | return *this; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | bool nextRecord() |
---|
| 254 | { |
---|
| 255 | if(!m_query.isActive()) |
---|
| 256 | { |
---|
| 257 | throw m_query.lastError(); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | m_valueIndex = 0; |
---|
| 261 | |
---|
| 262 | return m_query.next(); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | Value nextValue() |
---|
| 266 | { |
---|
| 267 | return Value(m_query.value(m_valueIndex++)); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | private: |
---|
| 271 | Q_DISABLE_COPY(Query) |
---|
| 272 | |
---|
| 273 | QSqlQuery m_query; |
---|
| 274 | int m_bindValueIndex; |
---|
| 275 | int m_valueIndex; |
---|
| 276 | |
---|
| 277 | }; |
---|
| 278 | |
---|
| 279 | inline QByteArray hashFilePath(const QString& filePath) |
---|
| 280 | { |
---|
| 281 | return QCryptographicHash::hash(filePath.toUtf8(), QCryptographicHash::Sha1).toBase64(); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | } // anonymous |
---|
| 285 | |
---|
| 286 | #endif // WITH_SQL |
---|
| 287 | |
---|
| 288 | namespace qpdfview |
---|
| 289 | { |
---|
| 290 | |
---|
| 291 | Database* Database::s_instance = 0; |
---|
| 292 | |
---|
| 293 | Database* Database::instance() |
---|
| 294 | { |
---|
| 295 | if(s_instance == 0) |
---|
| 296 | { |
---|
| 297 | s_instance = new Database(qApp); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | return s_instance; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | Database::~Database() |
---|
| 304 | { |
---|
| 305 | s_instance = 0; |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | QStringList Database::loadInstanceNames() |
---|
| 309 | { |
---|
| 310 | QStringList instanceNames; |
---|
| 311 | |
---|
| 312 | #ifdef WITH_SQL |
---|
| 313 | |
---|
| 314 | try |
---|
| 315 | { |
---|
| 316 | Transaction transaction(m_database); |
---|
| 317 | Query query(m_database); |
---|
| 318 | |
---|
| 319 | query.exec("SELECT DISTINCT(instanceName) FROM tabs_v2"); |
---|
| 320 | |
---|
| 321 | while(query.nextRecord()) |
---|
| 322 | { |
---|
| 323 | const QString instanceName = query.nextValue(); |
---|
| 324 | |
---|
| 325 | if(!instanceName.isEmpty()) |
---|
| 326 | { |
---|
| 327 | instanceNames.append(instanceName); |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | transaction.commit(); |
---|
| 332 | } |
---|
| 333 | catch(QSqlError& error) |
---|
| 334 | { |
---|
| 335 | qDebug() << error; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | #endif // WITH_SQL |
---|
| 339 | |
---|
| 340 | return instanceNames; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | void Database::restoreTabs(const RestoreTab& restoreTab) |
---|
| 344 | { |
---|
| 345 | #ifdef WITH_SQL |
---|
| 346 | |
---|
| 347 | try |
---|
| 348 | { |
---|
| 349 | Transaction transaction(m_database); |
---|
| 350 | Query query(m_database); |
---|
| 351 | |
---|
| 352 | query.prepare("SELECT filePath,currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation,renderFlags,firstPage" |
---|
| 353 | " FROM tabs_v4 WHERE instanceName==?"); |
---|
| 354 | |
---|
| 355 | query << instanceName(); |
---|
| 356 | |
---|
| 357 | query.exec(); |
---|
| 358 | |
---|
| 359 | while(query.nextRecord()) |
---|
| 360 | { |
---|
| 361 | if(DocumentView* newTab = restoreTab(query.nextValue())) |
---|
| 362 | { |
---|
| 363 | const int page = query.nextValue(); |
---|
| 364 | |
---|
| 365 | newTab->setContinuousMode(query.nextValue()); |
---|
| 366 | newTab->setLayoutMode(query.nextValue()); |
---|
| 367 | newTab->setRightToLeftMode(query.nextValue()); |
---|
| 368 | |
---|
| 369 | newTab->setScaleMode(query.nextValue()); |
---|
| 370 | newTab->setScaleFactor(query.nextValue()); |
---|
| 371 | |
---|
| 372 | newTab->setRotation(query.nextValue()); |
---|
| 373 | newTab->setRenderFlags(query.nextValue()); |
---|
| 374 | |
---|
| 375 | newTab->setFirstPage(query.nextValue()); |
---|
| 376 | |
---|
| 377 | newTab->jumpToPage(page, false); |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | transaction.commit(); |
---|
| 382 | } |
---|
| 383 | catch(QSqlError& error) |
---|
| 384 | { |
---|
| 385 | qDebug() << error; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | #else |
---|
| 389 | |
---|
| 390 | Q_UNUSED(restoreTab); |
---|
| 391 | |
---|
| 392 | #endif // WITH_SQL |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | void Database::saveTabs(const QList< DocumentView* >& tabs) |
---|
| 396 | { |
---|
| 397 | #ifdef WITH_SQL |
---|
| 398 | |
---|
| 399 | try |
---|
| 400 | { |
---|
| 401 | Transaction transaction(m_database); |
---|
| 402 | Query query(m_database); |
---|
| 403 | |
---|
| 404 | query.prepare("DELETE FROM tabs_v4 WHERE instanceName==?"); |
---|
| 405 | |
---|
| 406 | query << instanceName(); |
---|
| 407 | |
---|
| 408 | query.exec(); |
---|
| 409 | |
---|
| 410 | query.prepare("INSERT INTO tabs_v4" |
---|
| 411 | " (filePath,instanceName,currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation,renderFlags,firstPage)" |
---|
| 412 | " VALUES (?,?,?,?,?,?,?,?,?,?,?)"); |
---|
| 413 | |
---|
| 414 | foreach(const DocumentView* tab, tabs) |
---|
| 415 | { |
---|
| 416 | query << tab->fileInfo().absoluteFilePath() |
---|
| 417 | << instanceName() |
---|
| 418 | << tab->currentPage() |
---|
| 419 | |
---|
| 420 | << tab->continuousMode() |
---|
| 421 | << tab->layoutMode() |
---|
| 422 | << tab->rightToLeftMode() |
---|
| 423 | |
---|
| 424 | << tab->scaleMode() |
---|
| 425 | << tab->scaleFactor() |
---|
| 426 | |
---|
| 427 | << tab->rotation() |
---|
| 428 | << tab->renderFlags() |
---|
| 429 | |
---|
| 430 | << tab->firstPage(); |
---|
| 431 | |
---|
| 432 | query.exec(); |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | transaction.commit(); |
---|
| 436 | } |
---|
| 437 | catch(QSqlError& error) |
---|
| 438 | { |
---|
| 439 | qDebug() << error; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | #else |
---|
| 443 | |
---|
| 444 | Q_UNUSED(tabs); |
---|
| 445 | |
---|
| 446 | #endif // WITH_SQL |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | void Database::clearTabs() |
---|
| 450 | { |
---|
| 451 | #ifdef WITH_SQL |
---|
| 452 | |
---|
| 453 | try |
---|
| 454 | { |
---|
| 455 | Transaction transaction(m_database); |
---|
| 456 | Query query(m_database); |
---|
| 457 | |
---|
| 458 | query.exec("DELETE FROM tabs_v4"); |
---|
| 459 | |
---|
| 460 | transaction.commit(); |
---|
| 461 | } |
---|
| 462 | catch(QSqlError& error) |
---|
| 463 | { |
---|
| 464 | qDebug() << error; |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | #endif // WITH_SQL |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | void Database::restoreBookmarks() |
---|
| 471 | { |
---|
| 472 | #ifdef WITH_SQL |
---|
| 473 | |
---|
| 474 | try |
---|
| 475 | { |
---|
| 476 | Transaction transaction(m_database); |
---|
| 477 | Query outerQuery(m_database); |
---|
| 478 | Query innerQuery(m_database); |
---|
| 479 | |
---|
| 480 | outerQuery.exec("SELECT DISTINCT(filePath) FROM bookmarks_v3"); |
---|
| 481 | |
---|
| 482 | innerQuery.prepare("SELECT page,label,comment,modified" |
---|
| 483 | " FROM bookmarks_v3 WHERE filePath==?"); |
---|
| 484 | |
---|
| 485 | while(outerQuery.nextRecord()) |
---|
| 486 | { |
---|
| 487 | const QString filePath = outerQuery.nextValue(); |
---|
| 488 | |
---|
| 489 | innerQuery << filePath; |
---|
| 490 | |
---|
| 491 | innerQuery.exec(); |
---|
| 492 | |
---|
| 493 | BookmarkModel* model = BookmarkModel::fromPath(filePath, true); |
---|
| 494 | |
---|
| 495 | while(innerQuery.nextRecord()) |
---|
| 496 | { |
---|
| 497 | const int page = innerQuery.nextValue(); |
---|
| 498 | const QString label = innerQuery.nextValue(); |
---|
| 499 | const QString comment = innerQuery.nextValue(); |
---|
| 500 | const QDateTime modified = innerQuery.nextValue(); |
---|
| 501 | |
---|
| 502 | model->addBookmark(BookmarkItem(page, label, comment, modified)); |
---|
| 503 | } |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | transaction.commit(); |
---|
| 507 | } |
---|
| 508 | catch(QSqlError& error) |
---|
| 509 | { |
---|
| 510 | qDebug() << error; |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | #endif // WITH_SQL |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | void Database::saveBookmarks() |
---|
| 517 | { |
---|
| 518 | #ifdef WITH_SQL |
---|
| 519 | |
---|
| 520 | try |
---|
| 521 | { |
---|
| 522 | Transaction transaction(m_database); |
---|
| 523 | Query query(m_database); |
---|
| 524 | |
---|
| 525 | query.exec("DELETE FROM bookmarks_v3"); |
---|
| 526 | |
---|
| 527 | if(Settings::instance()->mainWindow().restoreBookmarks()) |
---|
| 528 | { |
---|
| 529 | query.prepare("INSERT INTO bookmarks_v3" |
---|
| 530 | " (filePath,page,label,comment,modified)" |
---|
| 531 | " VALUES (?,?,?,?,?)"); |
---|
| 532 | |
---|
| 533 | foreach(const QString& filePath, BookmarkModel::knownPaths()) |
---|
| 534 | { |
---|
| 535 | const BookmarkModel* model = BookmarkModel::fromPath(filePath); |
---|
| 536 | |
---|
| 537 | for(int row = 0, rowCount = model->rowCount(); row < rowCount; ++row) |
---|
| 538 | { |
---|
| 539 | const QModelIndex index = model->index(row); |
---|
| 540 | |
---|
| 541 | query << filePath |
---|
| 542 | |
---|
| 543 | << index.data(BookmarkModel::PageRole) |
---|
| 544 | << index.data(BookmarkModel::LabelRole) |
---|
| 545 | << index.data(BookmarkModel::CommentRole) |
---|
| 546 | << index.data(BookmarkModel::ModifiedRole); |
---|
| 547 | |
---|
| 548 | query.exec(); |
---|
| 549 | } |
---|
| 550 | } |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | transaction.commit(); |
---|
| 554 | } |
---|
| 555 | catch(QSqlError& error) |
---|
| 556 | { |
---|
| 557 | qDebug() << error; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | #endif // WITH_SQL |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | void Database::clearBookmarks() |
---|
| 564 | { |
---|
| 565 | #ifdef WITH_SQL |
---|
| 566 | |
---|
| 567 | try |
---|
| 568 | { |
---|
| 569 | Transaction transaction(m_database); |
---|
| 570 | Query query(m_database); |
---|
| 571 | |
---|
| 572 | query.exec("DELETE FROM bookmarks_v3"); |
---|
| 573 | |
---|
| 574 | transaction.commit(); |
---|
| 575 | } |
---|
| 576 | catch(QSqlError& error) |
---|
| 577 | { |
---|
| 578 | qDebug() << error; |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | #endif // WITH_SQL |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | void Database::restorePerFileSettings(DocumentView* tab) |
---|
| 585 | { |
---|
| 586 | #ifdef WITH_SQL |
---|
| 587 | |
---|
| 588 | if(!Settings::instance()->mainWindow().restorePerFileSettings()) |
---|
| 589 | { |
---|
| 590 | return; |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | try |
---|
| 594 | { |
---|
| 595 | Transaction transaction(m_database); |
---|
| 596 | Query query(m_database); |
---|
| 597 | |
---|
| 598 | query.prepare("SELECT currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation,renderFlags,firstPage" |
---|
| 599 | " FROM perfilesettings_v4 WHERE filePath==?"); |
---|
| 600 | |
---|
| 601 | query << hashFilePath(tab->fileInfo().absoluteFilePath()); |
---|
| 602 | |
---|
| 603 | query.exec(); |
---|
| 604 | |
---|
| 605 | if(query.nextRecord()) |
---|
| 606 | { |
---|
| 607 | const int page = query.nextValue(); |
---|
| 608 | |
---|
| 609 | tab->setContinuousMode(query.nextValue()); |
---|
| 610 | tab->setLayoutMode(query.nextValue()); |
---|
| 611 | tab->setRightToLeftMode(query.nextValue()); |
---|
| 612 | |
---|
| 613 | tab->setScaleMode(query.nextValue()); |
---|
| 614 | tab->setScaleFactor(query.nextValue()); |
---|
| 615 | |
---|
| 616 | tab->setRotation(query.nextValue()); |
---|
| 617 | |
---|
| 618 | tab->setRenderFlags(query.nextValue()); |
---|
| 619 | |
---|
| 620 | tab->setFirstPage(query.nextValue()); |
---|
| 621 | |
---|
| 622 | tab->jumpToPage(page, false); |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | transaction.commit(); |
---|
| 626 | } |
---|
| 627 | catch(QSqlError& error) |
---|
| 628 | { |
---|
| 629 | qDebug() << error; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | #else |
---|
| 633 | |
---|
| 634 | Q_UNUSED(tab); |
---|
| 635 | |
---|
| 636 | #endif // WITH_SQL |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | void Database::savePerFileSettings(const DocumentView* tab) |
---|
| 640 | { |
---|
| 641 | #ifdef WITH_SQL |
---|
| 642 | |
---|
| 643 | if(!Settings::instance()->mainWindow().restorePerFileSettings()) |
---|
| 644 | { |
---|
| 645 | return; |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | try |
---|
| 649 | { |
---|
| 650 | Transaction transaction(m_database); |
---|
| 651 | Query query(m_database); |
---|
| 652 | |
---|
| 653 | query.prepare("INSERT OR REPLACE INTO perfilesettings_v4" |
---|
| 654 | " (lastUsed,filePath,currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation,renderFlags,firstPage)" |
---|
| 655 | " VALUES (strftime('%s','now'),?,?,?,?,?,?,?,?,?,?)"); |
---|
| 656 | |
---|
| 657 | query << hashFilePath(tab->fileInfo().absoluteFilePath()) |
---|
| 658 | << tab->currentPage() |
---|
| 659 | |
---|
| 660 | << tab->continuousMode() |
---|
| 661 | << tab->layoutMode() |
---|
| 662 | << tab->rightToLeftMode() |
---|
| 663 | |
---|
| 664 | << tab->scaleMode() |
---|
| 665 | << tab->scaleFactor() |
---|
| 666 | |
---|
| 667 | << tab->rotation() |
---|
| 668 | << tab->renderFlags() |
---|
| 669 | |
---|
| 670 | << tab->firstPage(); |
---|
| 671 | |
---|
| 672 | query.exec(); |
---|
| 673 | |
---|
| 674 | transaction.commit(); |
---|
| 675 | } |
---|
| 676 | catch(QSqlError& error) |
---|
| 677 | { |
---|
| 678 | qDebug() << error; |
---|
| 679 | } |
---|
| 680 | |
---|
| 681 | #else |
---|
| 682 | |
---|
| 683 | Q_UNUSED(tab); |
---|
| 684 | |
---|
| 685 | #endif // WITH_SQL |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | Database::Database(QObject* parent) : QObject(parent) |
---|
| 689 | { |
---|
| 690 | #ifdef WITH_SQL |
---|
| 691 | |
---|
| 692 | #if QT_VERSION >= QT_VERSION_CHECK(5,0,0) |
---|
| 693 | |
---|
| 694 | const QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); |
---|
| 695 | |
---|
| 696 | #else |
---|
| 697 | |
---|
| 698 | const QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation); |
---|
| 699 | |
---|
| 700 | #endif // QT_VERSION |
---|
| 701 | |
---|
| 702 | QDir().mkpath(path); |
---|
| 703 | |
---|
| 704 | m_database = QSqlDatabase::addDatabase("QSQLITE"); |
---|
| 705 | m_database.setDatabaseName(QDir(path).filePath("database")); |
---|
| 706 | |
---|
| 707 | if(!m_database.open()) |
---|
| 708 | { |
---|
| 709 | qDebug() << m_database.lastError(); |
---|
| 710 | return; |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | try |
---|
| 714 | { |
---|
| 715 | Query query(m_database); |
---|
| 716 | |
---|
| 717 | query.exec("PRAGMA synchronous = OFF"); |
---|
| 718 | query.exec("PRAGMA journal_mode = MEMORY"); |
---|
| 719 | } |
---|
| 720 | catch(QSqlError& error) |
---|
| 721 | { |
---|
| 722 | qDebug() << error; |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | const QStringList tables = m_database.tables(); |
---|
| 726 | |
---|
| 727 | // tabs |
---|
| 728 | |
---|
| 729 | if(!tables.contains("tabs_v4")) |
---|
| 730 | { |
---|
| 731 | if(prepareTabs_v4()) |
---|
| 732 | { |
---|
| 733 | if(tables.contains("tabs_v3")) |
---|
| 734 | { |
---|
| 735 | migrateTabs_v3_v4(); |
---|
| 736 | } |
---|
| 737 | else if(tables.contains("tabs_v2")) |
---|
| 738 | { |
---|
| 739 | migrateTabs_v2_v4(); |
---|
| 740 | } |
---|
| 741 | else if(tables.contains("tabs_v1")) |
---|
| 742 | { |
---|
| 743 | migrateTabs_v1_v4(); |
---|
| 744 | } |
---|
| 745 | } |
---|
| 746 | } |
---|
| 747 | |
---|
| 748 | // bookmarks |
---|
| 749 | |
---|
| 750 | if(!tables.contains("bookmarks_v3")) |
---|
| 751 | { |
---|
| 752 | if(prepareBookmarks_v3()) |
---|
| 753 | { |
---|
| 754 | if(tables.contains("bookmarks_v2")) |
---|
| 755 | { |
---|
| 756 | migrateBookmarks_v2_v3(); |
---|
| 757 | } |
---|
| 758 | else if(tables.contains("bookmarks_v1")) |
---|
| 759 | { |
---|
| 760 | migrateBookmarks_v1_v3(); |
---|
| 761 | } |
---|
| 762 | } |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | // per-file settings |
---|
| 766 | |
---|
| 767 | if(!tables.contains("perfilesettings_v4")) |
---|
| 768 | { |
---|
| 769 | if(preparePerFileSettings_v4()) |
---|
| 770 | { |
---|
| 771 | if(tables.contains("perfilesettings_v3")) |
---|
| 772 | { |
---|
| 773 | migratePerFileSettings_v3_v4(); |
---|
| 774 | } |
---|
| 775 | else if(tables.contains("perfilesettings_v2")) |
---|
| 776 | { |
---|
| 777 | migratePerFileSettings_v2_v4(); |
---|
| 778 | } |
---|
| 779 | else if(tables.contains("perfilesettings_v1")) |
---|
| 780 | { |
---|
| 781 | migratePerFileSettings_v1_v4(); |
---|
| 782 | } |
---|
| 783 | } |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | limitPerFileSettings(); |
---|
| 787 | |
---|
| 788 | #endif // WITH_SQL |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | QString Database::instanceName() |
---|
| 792 | { |
---|
| 793 | return !qApp->objectName().isNull() ? qApp->objectName() : QString(""); |
---|
| 794 | } |
---|
| 795 | |
---|
| 796 | #ifdef WITH_SQL |
---|
| 797 | |
---|
| 798 | bool Database::prepareTabs_v4() |
---|
| 799 | { |
---|
| 800 | return prepareTable("CREATE TABLE tabs_v4 (" |
---|
| 801 | " filePath TEXT" |
---|
| 802 | " ,instanceName TEXT" |
---|
| 803 | " ,currentPage INTEGER" |
---|
| 804 | " ,continuousMode INTEGER" |
---|
| 805 | " ,layoutMode INTEGER" |
---|
| 806 | " ,rightToLeftMode INTEGER" |
---|
| 807 | " ,scaleMode INTEGER" |
---|
| 808 | " ,scaleFactor REAL" |
---|
| 809 | " ,rotation INTEGER" |
---|
| 810 | " ,renderFlags INTEGER" |
---|
| 811 | " ,firstPage INTEGER" |
---|
| 812 | " )"); |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | bool Database::prepareBookmarks_v3() |
---|
| 816 | { |
---|
| 817 | return prepareTable("CREATE TABLE bookmarks_v3 (" |
---|
| 818 | " filePath TEXT" |
---|
| 819 | " ,page INTEGER" |
---|
| 820 | " ,label TEXT" |
---|
| 821 | " ,comment TEXT" |
---|
| 822 | " ,modified DATETIME" |
---|
| 823 | " )"); |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | bool Database::preparePerFileSettings_v4() |
---|
| 827 | { |
---|
| 828 | return prepareTable("CREATE TABLE perfilesettings_v4 (" |
---|
| 829 | " lastUsed INTEGER" |
---|
| 830 | " ,filePath TEXT PRIMARY KEY" |
---|
| 831 | " ,currentPage INTEGER" |
---|
| 832 | " ,continuousMode INTEGER" |
---|
| 833 | " ,layoutMode INTEGER" |
---|
| 834 | " ,rightToLeftMode INTEGER" |
---|
| 835 | " ,scaleMode INTEGER" |
---|
| 836 | " ,scaleFactor REAL" |
---|
| 837 | " ,rotation INTEGER" |
---|
| 838 | " ,renderFlags INTEGER" |
---|
| 839 | " ,firstPage INTEGER" |
---|
| 840 | " )"); |
---|
| 841 | } |
---|
| 842 | |
---|
| 843 | void Database::migrateTabs_v3_v4() |
---|
| 844 | { |
---|
| 845 | migrateTable("INSERT INTO tabs_v4" |
---|
| 846 | " SELECT filePath,instanceName,currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation,0,-1" |
---|
| 847 | " FROM tabs_v3", |
---|
| 848 | |
---|
| 849 | "DROP TABLE tabs_v3", |
---|
| 850 | |
---|
| 851 | "Migrated tabs from v3 to v4, dropping v3."); |
---|
| 852 | } |
---|
| 853 | |
---|
| 854 | void Database::migrateTabs_v2_v4() |
---|
| 855 | { |
---|
| 856 | migrateTable("INSERT INTO tabs_v4" |
---|
| 857 | " SELECT filePath,instanceName,currentPage,continuousMode,layoutMode,0,scaleMode,scaleFactor,rotation,0,-1" |
---|
| 858 | " FROM tabs_v2", |
---|
| 859 | |
---|
| 860 | "DROP TABLE tabs_v2" , |
---|
| 861 | |
---|
| 862 | "Migrated tabs from v2 to v4, dropping v2."); |
---|
| 863 | } |
---|
| 864 | |
---|
| 865 | void Database::migrateTabs_v1_v4() |
---|
| 866 | { |
---|
| 867 | migrateTable("INSERT INTO tabs_v4" |
---|
| 868 | " SELECT filePath,?,currentPage,continuousMode,layoutMode,0,scaleMode,scaleFactor,rotation,0,-1" |
---|
| 869 | " FROM tabs_v1", |
---|
| 870 | |
---|
| 871 | "DROP TABLE tabs_v1", |
---|
| 872 | |
---|
| 873 | "Migrated tabs from v1 to v4, dropping v1."); |
---|
| 874 | } |
---|
| 875 | |
---|
| 876 | void Database::migrateBookmarks_v2_v3() |
---|
| 877 | { |
---|
| 878 | migrateTable("INSERT INTO bookmarks_v3" |
---|
| 879 | " SELECT filePath,page,label,'',datetime('now')" |
---|
| 880 | " FROM bookmarks_v2", |
---|
| 881 | |
---|
| 882 | "DROP TABLE bookmarks_v2", |
---|
| 883 | |
---|
| 884 | "Migrated bookmarks from v2 to v3, dropping v2."); |
---|
| 885 | } |
---|
| 886 | |
---|
| 887 | void Database::migrateBookmarks_v1_v3() |
---|
| 888 | { |
---|
| 889 | try |
---|
| 890 | { |
---|
| 891 | Transaction transaction(m_database); |
---|
| 892 | Query outerQuery(m_database); |
---|
| 893 | Query innerQuery(m_database); |
---|
| 894 | |
---|
| 895 | outerQuery.exec("SELECT filePath,pages FROM bookmarks_v1"); |
---|
| 896 | |
---|
| 897 | innerQuery.prepare("INSERT INTO bookmarks_v3" |
---|
| 898 | " (filePath,page,label,comment,modified)" |
---|
| 899 | " VALUES (?,?,?,'',datetime('now'))"); |
---|
| 900 | |
---|
| 901 | while(outerQuery.nextRecord()) |
---|
| 902 | { |
---|
| 903 | const QString filePath = outerQuery.nextValue(); |
---|
| 904 | const QString pages = outerQuery.nextValue(); |
---|
| 905 | |
---|
| 906 | foreach(const QString& page, pages.split(",", QString::SkipEmptyParts)) |
---|
| 907 | { |
---|
| 908 | innerQuery << filePath |
---|
| 909 | << page |
---|
| 910 | << tr("Jump to page %1").arg(page); |
---|
| 911 | |
---|
| 912 | innerQuery.exec(); |
---|
| 913 | } |
---|
| 914 | } |
---|
| 915 | |
---|
| 916 | qWarning() << "Migrated bookmarks from v1 to v3, dropping v1."; |
---|
| 917 | |
---|
| 918 | outerQuery.exec("DROP TABLE bookmarks_v1"); |
---|
| 919 | |
---|
| 920 | transaction.commit(); |
---|
| 921 | } |
---|
| 922 | catch(QSqlError& error) |
---|
| 923 | { |
---|
| 924 | qDebug() << error; |
---|
| 925 | } |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | void Database::migratePerFileSettings_v3_v4() |
---|
| 929 | { |
---|
| 930 | migrateTable("INSERT INTO perfilesettings_v4" |
---|
| 931 | " SELECT lastUsed,filePath,currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation,0,firstPage" |
---|
| 932 | " FROM perfilesettings_v3", |
---|
| 933 | |
---|
| 934 | "DROP TABLE perfilesettings_v3", |
---|
| 935 | |
---|
| 936 | "Migrated per-file settings from v3 to v4, dropping v3."); |
---|
| 937 | } |
---|
| 938 | |
---|
| 939 | void Database::migratePerFileSettings_v2_v4() |
---|
| 940 | { |
---|
| 941 | migrateTable("INSERT INTO perfilesettings_v4" |
---|
| 942 | " SELECT lastUsed,filePath,currentPage,continuousMode,layoutMode,rightToLeftMode,scaleMode,scaleFactor,rotation,0,-1" |
---|
| 943 | " FROM perfilesettings_v2", |
---|
| 944 | |
---|
| 945 | "DROP TABLE perfilesettings_v2", |
---|
| 946 | |
---|
| 947 | "Migrated per-file settings from v2 to v4, dropping v2."); |
---|
| 948 | } |
---|
| 949 | |
---|
| 950 | void Database::migratePerFileSettings_v1_v4() |
---|
| 951 | { |
---|
| 952 | migrateTable("INSERT INTO perfilesettings_v4" |
---|
| 953 | " SELECT lastUsed,filePath,currentPage,continuousMode,layoutMode,0,scaleMode,scaleFactor,rotation,0,-1" |
---|
| 954 | " FROM perfilesettings_v1", |
---|
| 955 | |
---|
| 956 | "DROP TABLE perfilesettings_v1", |
---|
| 957 | |
---|
| 958 | "Migrated per-file settings from v1 to v4, dropping v1."); |
---|
| 959 | } |
---|
| 960 | |
---|
| 961 | bool Database::prepareTable(const QString& prepare) |
---|
| 962 | { |
---|
| 963 | try |
---|
| 964 | { |
---|
| 965 | Transaction transaction(m_database); |
---|
| 966 | Query query(m_database); |
---|
| 967 | |
---|
| 968 | query.exec(prepare); |
---|
| 969 | |
---|
| 970 | transaction.commit(); |
---|
| 971 | return true; |
---|
| 972 | } |
---|
| 973 | catch(QSqlError& error) |
---|
| 974 | { |
---|
| 975 | qDebug() << error; |
---|
| 976 | return false; |
---|
| 977 | } |
---|
| 978 | } |
---|
| 979 | |
---|
| 980 | void Database::migrateTable(const QString& migrate, const QString& prune, const QString& warning) |
---|
| 981 | { |
---|
| 982 | try |
---|
| 983 | { |
---|
| 984 | Transaction transaction(m_database); |
---|
| 985 | Query query(m_database); |
---|
| 986 | |
---|
| 987 | query.exec(migrate); |
---|
| 988 | |
---|
| 989 | qWarning() << warning; |
---|
| 990 | |
---|
| 991 | query.exec(prune); |
---|
| 992 | |
---|
| 993 | transaction.commit(); |
---|
| 994 | } |
---|
| 995 | catch(QSqlError& error) |
---|
| 996 | { |
---|
| 997 | qDebug() << error; |
---|
| 998 | } |
---|
| 999 | } |
---|
| 1000 | |
---|
| 1001 | void Database::limitPerFileSettings() |
---|
| 1002 | { |
---|
| 1003 | try |
---|
| 1004 | { |
---|
| 1005 | Transaction transaction(m_database); |
---|
| 1006 | Query query(m_database); |
---|
| 1007 | |
---|
| 1008 | if(Settings::instance()->mainWindow().restorePerFileSettings()) |
---|
| 1009 | { |
---|
| 1010 | query.prepare("DELETE FROM perfilesettings_v4" |
---|
| 1011 | " WHERE filePath NOT IN (" |
---|
| 1012 | " SELECT filePath FROM perfilesettings_v4" |
---|
| 1013 | " ORDER BY lastUsed DESC LIMIT ?" |
---|
| 1014 | " )"); |
---|
| 1015 | |
---|
| 1016 | query << Settings::instance()->mainWindow().perFileSettingsLimit(); |
---|
| 1017 | |
---|
| 1018 | query.exec(); |
---|
| 1019 | } |
---|
| 1020 | else |
---|
| 1021 | { |
---|
| 1022 | query.exec("DELETE FROM perfilesettings_v4"); |
---|
| 1023 | } |
---|
| 1024 | |
---|
| 1025 | transaction.commit(); |
---|
| 1026 | } |
---|
| 1027 | catch(QSqlError& error) |
---|
| 1028 | { |
---|
| 1029 | qDebug() << error; |
---|
| 1030 | } |
---|
| 1031 | } |
---|
| 1032 | |
---|
| 1033 | #endif // WITH_SQL |
---|
| 1034 | |
---|
| 1035 | } // qpdfview |
---|