-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_profile.cpp
221 lines (186 loc) · 8.96 KB
/
add_profile.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
#include "add_profile.h"
#include "ui_add_profile.h"
#include "global.h"
#include "mainwindow.h"
#include <QtSql>
#include <QMessageBox>
add_profile::add_profile(QWidget *parent) :
QWidget(parent),
ui(new Ui::add_profile)
{
ui->setupUi(this);
// hide ID field
ui->label_4->setHidden(true);
ui->txt_profile_id->setHidden(true);
}
add_profile::~add_profile()
{
delete ui;
}
void add_profile::on_btn_save_clicked() {
// check if we have a profile name set, otherwise sent warning and abort
if ( ui->txt_profile_name->text() !="" ) {
QString app_path = QApplication::applicationDirPath();
QString dbase_path = app_path + "/base.bz";
{
QSqlDatabase base = QSqlDatabase::addDatabase("QSQLITE", "create-new-profile");
base.setDatabaseName(dbase_path);
base.open();
if(base.isOpen() != true){
QMessageBox msgbox;
msgbox.setText("There was a problem with the database");
msgbox.setInformativeText("Due to unknown reason there was a problem with opening the database.\nThe problem accured during initial opening of the database.");
msgbox.exec();
}
else {
// the database is open
// scroll through profiles to determinate if this is really new profile or not
QSqlQuery sql_check(base);
sql_check.prepare("SELECT * FROM profile WHERE user LIKE '" + vApp->id_user() + "' AND profile_name LIKE '" + ui->txt_profile_name->text() + "'"
" AND id NOT LIKE '" + ui->txt_profile_id->text() + "'");
sql_check.exec();
if ( !sql_check.next() ) { // the profile name is unique
QSqlQuery sql_insert(base);
if ( ui->txt_profile_id->text() == "" ) { // insert new profile
sql_insert.prepare("INSERT INTO profile (user, profile_name, site_name, site_url) VALUES (?, ?, ?, ?)");
}
else { // update current profile
sql_insert.prepare("UPDATE profile SET user = ?, profile_name = ?, site_name= ?, site_url = ? "
"WHERE id LIKE '" + ui->txt_profile_id->text() + "'");
}
sql_insert.bindValue(0, vApp->id_user());
sql_insert.bindValue(1, ui->txt_profile_name->text());
sql_insert.bindValue(2, ui->txt_page_title->text());
sql_insert.bindValue(3, ui->txt_page_url->text());
sql_insert.exec();
// change global profile
QSqlQuery sql_get_id(base);
sql_get_id.prepare("SELECT * FROM profile WHERE user LIKE '" + vApp->id_user() + "' AND profile_name LIKE '" + ui->txt_profile_name->text() + "'");
sql_get_id.exec();
if ( sql_get_id.next() ) { // the profile was succesfully saved
QMessageBox msgbox;
msgbox.setText("Profile je bil uspesno shranjen!");
msgbox.exec();
vApp->set_id_profile(sql_get_id.value(sql_get_id.record().indexOf("id")).toString());
// send signal to the main window to refresh profile combo box
send("refresh-profiles");
}
else { // there was an error while saving the profile
QMessageBox msgbox;
msgbox.setText("Pri shranjevanju profila je prislo do napake!");
msgbox.exec();
}
}
else { // there already is profile with this name
QMessageBox msgbox;
msgbox.setText("Profil s tem imenom ze obstaja! Prosim, spremenite ga!");
msgbox.exec();
}
}
base.close();
}
QSqlDatabase::removeDatabase("create-new-profile");
}
else { // the profile name field is empty
QMessageBox msgbox;
msgbox.setText("Prosim vnesite ime profila!");
msgbox.exec();
}
}
void add_profile::fill_fields() {
// this function fills all the fields of the profile
QString app_path = QApplication::applicationDirPath();
QString dbase_path = app_path + "/base.bz";
{
QSqlDatabase base = QSqlDatabase::addDatabase("QSQLITE", "fill-fields");
base.setDatabaseName(dbase_path);
base.open();
if(base.isOpen() != true){
QMessageBox msgbox;
msgbox.setText("There was a problem with the database");
msgbox.setInformativeText("Due to unknown reason there was a problem with opening the database.\nThe problem accured during initial opening of the database.");
msgbox.exec();
}
else {
// the database is open
QSqlQuery sql_fill(base);
sql_fill.prepare("SELECT * FROM profile WHERE user LIKE '" + vApp->id_user() + "' AND id LIKE '" + ui->txt_profile_id->text() + "'");
sql_fill.exec();
if ( sql_fill.next() ) {
ui->txt_page_title->setText(sql_fill.value(sql_fill.record().indexOf("site_name")).toString());
ui->txt_page_url->setText(sql_fill.value(sql_fill.record().indexOf("site_url")).toString());
ui->txt_profile_name->setText(sql_fill.value(sql_fill.record().indexOf("profile_name")).toString());
}
}
base.close();
}
QSqlDatabase::removeDatabase("fill-fields");
}
void add_profile::on_btn_delete_clicked() {
// check if we have a profile name set, otherwise sent warning and abort
if ( ui->txt_profile_id->text() !="" ) {
QString app_path = QApplication::applicationDirPath();
QString dbase_path = app_path + "/base.bz";
{
QSqlDatabase base = QSqlDatabase::addDatabase("QSQLITE", "delete-profile");
base.setDatabaseName(dbase_path);
base.open();
if(base.isOpen() != true){
QMessageBox msgbox;
msgbox.setText("There was a problem with the database");
msgbox.setInformativeText("Due to unknown reason there was a problem with opening the database.\nThe problem accured during initial opening of the database.");
msgbox.exec();
}
else {
// the database is open
// delete selected profile
QSqlQuery sql_delete(base);
sql_delete.prepare("DELETE FROM sources WHERE user LIKE '" + vApp->id_user() + "' AND profile LIKE '" + ui->txt_profile_id->text() + "'");
sql_delete.exec();
sql_delete.clear();
sql_delete.prepare("DELETE FROM medium WHERE user LIKE '" + vApp->id_user() + "' AND profile LIKE '" + ui->txt_profile_id->text() + "'");
sql_delete.exec();
sql_delete.clear();
sql_delete.prepare("DELETE FROM data WHERE user LIKE '" + vApp->id_user() + "' AND profile LIKE '" + ui->txt_profile_id->text() + "'");
sql_delete.exec();
sql_delete.clear();
sql_delete.prepare("DELETE FROM campaign WHERE user LIKE '" + vApp->id_user() + "' AND profile LIKE '" + ui->txt_profile_id->text() + "'");
sql_delete.exec();
sql_delete.clear();
sql_delete.prepare("DELETE FROM profile WHERE user LIKE '" + vApp->id_user() + "' AND id LIKE '" + ui->txt_profile_id->text() + "'");
sql_delete.exec();
sql_delete.clear();
// set global profile id to null
vApp->set_id_profile("");
// clear form fields
ui->txt_page_title->setText("");
ui->txt_page_url->setText("");
ui->txt_profile_id->setText("");
ui->txt_profile_name->setText("");
// display message
QMessageBox msgbox;
msgbox.setText("Profil je bil uspesno izbrisan!");
msgbox.exec();
// send signal to the main window to refresh profile combo box
send("refresh-profiles");
}
base.close();
}
QSqlDatabase::removeDatabase("delete-profile");
}
}
void add_profile::recieve(QString state) {
// this function receives the signal send from main window and act acordingly
if ( state == "0" ) { // we input a new profile
ui->btn_save->setText("Vnesi");
ui->txt_page_title->setText("");
ui->txt_page_url->setText("");
ui->txt_profile_id->setText("");
ui->txt_profile_name->setText("");
}
else { // we have to fill the old profile and make it possible to change it
ui->btn_save->setText("Spremeni");
ui->txt_profile_id->setText(state);
fill_fields();
}
}