-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskManager.cpp
More file actions
206 lines (164 loc) · 7.13 KB
/
TaskManager.cpp
File metadata and controls
206 lines (164 loc) · 7.13 KB
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
#include <iostream>
#include <sqlite3.h>
#include <ctime>
#include <TaskManager.h>
#include <QVariantMap>
#include <QDebug>
using namespace std;
TaskManager::TaskManager(QObject *parent) : QObject(parent) {}
TaskManager::~TaskManager(){}
void TaskManager::loadTasksDB() {
sqlite3* db;
sqlite3_stmt* stmt;
if (sqlite3_open("tasks.db", &db) != SQLITE_OK) {
qDebug() << "Error opening database: " << sqlite3_errmsg(db);
return;
}
const char* sql = "SELECT id, taskName, taskDescription, taskDeadline, taskImportance, taskStateFinished FROM tasks;";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
qDebug() << "Error preparing statement: " << sqlite3_errmsg(db);
sqlite3_close(db);
return;
}
bool hasTasks = false; // Track if tasks exist
while (sqlite3_step(stmt) == SQLITE_ROW) {
hasTasks = true;
int id = sqlite3_column_int(stmt, 0);
QString taskName = QString::fromUtf8(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1)));
QString taskDescription = QString::fromUtf8(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2)));
QString taskDeadline = QString::fromUtf8(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)));
QString taskImportance = QString::fromUtf8(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4)));
int taskStateFinished = sqlite3_column_int(stmt, 5);
qDebug() << "data is: " << id << taskName << taskDescription << taskDeadline << taskImportance << taskStateFinished;
emit taskLoaded(id, taskName, taskDescription, taskDeadline, taskImportance, taskStateFinished);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
if (!hasTasks) {
qDebug() << "No tasks were loaded!";
}
}
bool TaskManager::removeTaskDB(int id)
{
sqlite3* db;
sqlite3_stmt* stmt;
// Open the database
if (sqlite3_open("tasks.db", &db) != SQLITE_OK) {
qDebug() << "Error opening database: " << sqlite3_errmsg(db);
return false;
}
// Prepare the SQL DELETE statement
const char* sql = "DELETE FROM tasks WHERE id = ?;";
// Prepare the statement for execution
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
qDebug() << "Error preparing statement: " << sqlite3_errmsg(db);
sqlite3_close(db);
return false;
}
// Bind the dateCreated value to the SQL query
sqlite3_bind_int(stmt, 1, id);
// Execute the DELETE statement
int result = sqlite3_step(stmt);
if (result != SQLITE_DONE) {
qDebug() << "Error executing delete: " << sqlite3_errmsg(db);
sqlite3_finalize(stmt);
sqlite3_close(db);
return false;
}
qDebug() << "Task successfully deleted with id:" << to_string(id);
// Finalize the statement and close the database
sqlite3_finalize(stmt);
sqlite3_close(db);
int rowsAffected = sqlite3_changes(db);
if (rowsAffected == 0) {
qDebug() << "No tasks were deleted. Task ID may not exist:" << id;
} else {
qDebug() << rowsAffected << "task(s) successfully deleted with id:" << id;
}
return true;
}
void TaskManager::cleanupBeforeExit() {
qDebug() << "Application is closing. Performing cleanup...";
// Example: Log to console or save data before exit
sqlite3* db;
if (sqlite3_open("tasks.db", &db) == SQLITE_OK) {
const char* sql = "UPDATE tasks SET taskImportance = 0 WHERE taskImportance IS NULL;";
char* errorMessage = nullptr;
if (sqlite3_exec(db, sql, nullptr, 0, &errorMessage) != SQLITE_OK) {
qDebug() << "Error updating database on exit:" << errorMessage;
sqlite3_free(errorMessage);
} else {
qDebug() << "Database updated successfully before exit.";
}
sqlite3_close(db);
} else {
qDebug() << "Failed to open database on exit.";
}
}
bool TaskManager::updateTaskDB(int id, const QString &name, const QString &description, const QString &deadline, const QString &priority, int taskStateFinished) {
sqlite3* db;
sqlite3_stmt* stmt;
if (sqlite3_open("tasks.db", &db) != SQLITE_OK) {
qDebug() << "Error opening database: " << sqlite3_errmsg(db);
return false;
}
const char* sql = "UPDATE tasks SET taskName = ?, taskDescription = ?, taskDeadline = ?, taskImportance = ?, taskStateFinished = ? WHERE id = ?;";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
qDebug() << "Error preparing update statement: " << sqlite3_errmsg(db);
sqlite3_close(db);
return false;
}
sqlite3_bind_text(stmt, 1, name.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, description.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, deadline.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 4, priority.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 5, taskStateFinished);
sqlite3_bind_int(stmt, 6, id);
int result = sqlite3_step(stmt);
if (result != SQLITE_DONE) {
qDebug() << "Error updating task: " << sqlite3_errmsg(db);
sqlite3_finalize(stmt);
sqlite3_close(db);
return false;
}
qDebug() << "Task updated successfully with ID:" << id;
sqlite3_finalize(stmt);
sqlite3_close(db);
return true;
}
void TaskManager::insertToTable(int id, const QString &taskName, const QString &taskDescription, const QString &taskDeadline, const QString &taskPriority, int taskStateFinished) {
sqlite3* db;
sqlite3_stmt* stmt;
if (sqlite3_open("tasks.db", &db) != SQLITE_OK) {
qDebug() << "Error opening database: " << sqlite3_errmsg(db);
return;
}
const char* sql = "INSERT INTO tasks (id, taskName, taskDescription, taskDeadline, taskImportance, taskStateFinished) VALUES (?, ?, ?, ?, ?, ?);";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr) != SQLITE_OK) {
qDebug() << "Error preparing statement: " << sqlite3_errmsg(db);
sqlite3_close(db);
return;
}
// gets current time
time_t now = time(nullptr);
tm localTime;
localtime_s(&localTime, &now);
char buffer[100];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &localTime);
string currentTime = buffer;
// Bind values
sqlite3_bind_int(stmt, 1, id);
sqlite3_bind_text(stmt, 2, taskName.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, taskDescription.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 4, taskDeadline.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 5, taskPriority.toUtf8().constData(), -1, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 6, taskStateFinished);
// Execute the statement
if (sqlite3_step(stmt) != SQLITE_DONE) {
qDebug() << "Error inserting data: " << sqlite3_errmsg(db);
} else {
qDebug() << "Task inserted successfully!" << id << " " << taskName << " " << taskDescription << " " << taskDeadline << " " << taskPriority << " " << taskStateFinished << " ";
}
sqlite3_finalize(stmt);
sqlite3_close(db);
}