| 1 |
/* |
| 2 |
* Copyright (C) 2017-2019 UBports |
| 3 |
* Copyright (C) 2014-2015 Canonical, Ltd. |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; version 3. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
import QtQuick 2.4 |
| 18 |
import QtQuick.LocalStorage 2.0 |
| 19 |
|
| 20 |
Item { |
| 21 |
property var calculationHistoryDatabase: null |
| 22 |
readonly property int lastDatabaseVersion: 1 |
| 23 |
|
| 24 |
ListModel { |
| 25 |
id: history |
| 26 |
|
| 27 |
Component.onCompleted: { |
| 28 |
getCalculations(function(calc) { |
| 29 |
history.append(calc); |
| 30 |
}); |
| 31 |
} |
| 32 |
|
| 33 |
ListElement { |
| 34 |
dbId: -1 |
| 35 |
formula: '' |
| 36 |
result: '' |
| 37 |
date: 0 |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
Timer { |
| 42 |
id: timer |
| 43 |
interval: 500 |
| 44 |
property var execute: [] |
| 45 |
onTriggered: { |
| 46 |
for (var i = 0; i < execute.length; i++) { |
| 47 |
execute[i](); |
| 48 |
} |
| 49 |
execute = []; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
function openDatabase() { |
| 54 |
// Check if the database was already opened |
| 55 |
if (calculationHistoryDatabase !== null) return; |
| 56 |
|
| 57 |
calculationHistoryDatabase = LocalStorage.openDatabaseSync( |
| 58 |
"calculator.ubports", "", "", 5000); |
| 59 |
|
| 60 |
// Update (or create) the database if needed |
| 61 |
if (calculationHistoryDatabase.version !== lastDatabaseVersion) { |
| 62 |
upgradeDatabase(calculationHistoryDatabase.version); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
function upgradeDatabase(currentDatabaseVersion) { |
| 67 |
// Array with all the SQL needed to create database and update versions |
| 68 |
var sqlcode = [ |
| 69 |
'CREATE TABLE IF NOT EXISTS Calculations( |
| 70 |
dbId INTEGER PRIMARY KEY, |
| 71 |
formula TEXT NOT NULL, |
| 72 |
result TEXT NOT NULL, |
| 73 |
date INTEGER NOT NULL DEFAULT 0 |
| 74 |
)' |
| 75 |
]; |
| 76 |
|
| 77 |
// Start the upgrade |
| 78 |
calculationHistoryDatabase.changeVersion(currentDatabaseVersion, |
| 79 |
lastDatabaseVersion, function(tx) { |
| 80 |
// Create the database |
| 81 |
if (currentDatabaseVersion < lastDatabaseVersion) { |
| 82 |
tx.executeSql(sqlcode[0]); |
| 83 |
console.log("Database upgraded to " + lastDatabaseVersion.toString()); |
| 84 |
} |
| 85 |
} |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
function getCalculations(callback) { |
| 90 |
openDatabase(); |
| 91 |
|
| 92 |
// Do a transaction to take all calculations |
| 93 |
calculationHistoryDatabase.transaction( |
| 94 |
function (tx) { |
| 95 |
var results = tx.executeSql('SELECT * FROM Calculations'); |
| 96 |
|
| 97 |
for (var i = 0; i < results.rows.length; i++) { |
| 98 |
callback(results.rows.item(i)); |
| 99 |
} |
| 100 |
} |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
function addCalculationToScreen(formula, result) { |
| 105 |
// The function add the last formula to the model, and leave to |
| 106 |
// addCalculationToDatabase the job to add it to the database |
| 107 |
// that is called only after the element has been added to the |
| 108 |
// model |
| 109 |
var date = Date.now(); |
| 110 |
history.append({"formula": formula, |
| 111 |
"result": result, |
| 112 |
"date": date, |
| 113 |
}); |
| 114 |
var index = history.count - 1; |
| 115 |
// TODO: move this function to a plave that retards the execution to |
| 116 |
// improve performances |
| 117 |
timer.execute.push(function() { |
| 118 |
calculationHistory.addCalculationToDatabase(formula, result, date, index); |
| 119 |
}); |
| 120 |
timer.start(); |
| 121 |
} |
| 122 |
|
| 123 |
function addCalculationToDatabase(formula, result, date, index) { |
| 124 |
openDatabase(); |
| 125 |
calculationHistoryDatabase.transaction( |
| 126 |
function (tx) { |
| 127 |
var results = tx.executeSql('INSERT INTO Calculations ( |
| 128 |
formula, result, date) VALUES( |
| 129 |
?, ?, ?)', |
| 130 |
[formula, result, date] |
| 131 |
); |
| 132 |
// we need to update the listmodel unless we would have dbId = 0 on the |
| 133 |
// last inserted item |
| 134 |
history.setProperty(index, "dbId", parseInt(results.insertId)); |
| 135 |
} |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
function getContents() { |
| 140 |
return history; |
| 141 |
} |
| 142 |
|
| 143 |
function deleteCalc(dbId, id) { |
| 144 |
openDatabase(); |
| 145 |
history.setProperty(id, "dbId", -1); |
| 146 |
|
| 147 |
timer.execute.push(function () { |
| 148 |
calculationHistoryDatabase.transaction( |
| 149 |
function (tx) { |
| 150 |
tx.executeSql('DELETE FROM Calculations WHERE dbId = ?', [dbId]); |
| 151 |
} |
| 152 |
); |
| 153 |
}); |
| 154 |
timer.start(); |
| 155 |
} |
| 156 |
} |