-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsettingsdialog.cpp
191 lines (172 loc) · 6.96 KB
/
settingsdialog.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
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include <QDebug>
#include "globalvar.h"
#include "downloaderui.h"
#include "mainwindow.h"
#include <QWebView>
#include <QWebElement>
#include <QWebFrame>
#include <QUrl>
#include <QSettings>
#include <QDesktopServices>
SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
this->setWindowIcon(QPixmap(":/icons/OIVLE.png"));
this->setWindowTitle(" Offline IVLE");
this->setAttribute(Qt::WA_QuitOnClose,false);
this->setWindowFlags((Qt::WindowFlags) (Qt::Window | Qt::WindowStaysOnTopHint | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint & (~Qt::WindowFullscreenButtonHint)) );
webviewDialog = new QDialog(this);
webviewDialog->setLayout(new QBoxLayout(QBoxLayout::LeftToRight));
webviewDialog->setAttribute(Qt::WA_QuitOnClose,false);
webView = new QWebView(webviewDialog);
webviewDialog->layout()->addWidget(webView);
webviewDialog->layout()->setMargin(0);
connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(onWebviewLoaded()));
adialog = new AdvancedDialog(this);
connect(adialog, &AdvancedDialog::configSaved, this, &SettingsDialog::configSaved);
}
SettingsDialog::~SettingsDialog()
{
delete ui;
}
void SettingsDialog::onWebviewLoaded(){
QUrl url = webView->url();
if(url.host() == QString("ivle.nus.edu.sg")&&
url.path() == QString("/api/login/login_result.ashx")&&
QUrlQuery(url).queryItemValue("r") == QString("0")){
//qDebug()<<"OK!";
webviewDialog->close();
emit gottenToken(webView->page()->mainFrame()->toPlainText());
}
}
void SettingsDialog::on_pushButton_clicked()
{
webView->setUrl(QString("https://ivle.nus.edu.sg/api/login/?apikey=%1").arg(APIKEY));
//webviewDialog->setWindowModality(Qt::ApplicationModal);
//setting modality cause the cursor to disappear in textbox...
webviewDialog->show();
}
void SettingsDialog::setLabelText(const QString &t){
ui->label->setText(t);
}
void SettingsDialog::setDlText(const QString &t){
ui->label_2->setText(t);
}
void SettingsDialog::on_pushButton_2_clicked()
{
QString path = QFileDialog::getExistingDirectory(this,"Choose a download destination");
if(!path.isEmpty()){
ui->label_2->setText(QString("Download to: %1").arg(path));
ui->verticalLayout->update();
emit updateDirectory(path);
}
}
void SettingsDialog::setDisplayedSettings(QVariantMap m)
{
ui->spinBox->setValue( (int)(m["maxFileSize"].toDouble() / 1024 / 1024) );
ui->notifyCheckBox->setCheckState( m["notify"].toBool() ? Qt::Checked : Qt::Unchecked );
ui->igUpCheckBox->setCheckState( m["ignoreUploadable"].toBool() ? Qt::Checked : Qt::Unchecked );
ui->notifyAnmCheckBox->setCheckState( m["notifyAnm"].toBool() ? Qt::Checked : Qt::Unchecked );
adialog->setConfigContent(m["jsonConfig"].toString());
}
bool SettingsDialog::event(QEvent *e)
{
if(e->type() == QEvent::WindowActivate){
this->setWindowFlags(this->windowFlags() & (~Qt::WindowStaysOnTopHint));
this->show();
}
return QDialog::event(e);
}
void SettingsDialog::closeEvent(QCloseEvent *e){
QVariantMap m;
m["maxFileSize"] = ui->spinBox->value();
m["ignoreUploadable"] = ui->igUpCheckBox->checkState() == Qt::Checked;
m["notify"] = ui->notifyCheckBox->checkState() == Qt::Checked;
m["notifyAnm"] = ui->notifyAnmCheckBox->checkState() == Qt::Checked;
emit closedWithSettings(m);
QDialog::closeEvent(e);
}
void SettingsDialog::on_pushButton_3_clicked()
{
adialog->show();
}
//Offline IVLE button
void SettingsDialog::on_pushButton_4_clicked()
{
if(checkCon()==0){
QSettings* regSett;
regSett = new QSettings("Organization-name","Project-name");
if(regSett->value("KEY","failed").toString().length()==21)
{
DownloaderUI UI;
this->close();
UI.setModal(true);
UI.exec();
this->show();
}
else QMessageBox::warning(this,"Failed","APIKEY is invalid, do parse a correct APIKEY before using the downloader!");
}
//else QMessageBox::information(this,"No connection","You do not have a internet connection. This app needs to have an internect connection");
}
//Resets everything about the application!
void SettingsDialog::on_pushButton_5_clicked()
{
webviewDialog2 = new QDialog(this);// an object that is shown in the UI, code below will set the settings of the Dialog box seen
webviewDialog2->setLayout(new QBoxLayout(QBoxLayout::LeftToRight));
webviewDialog2->setAttribute(Qt::WA_QuitOnClose,false);
webView2 = new QWebView(webviewDialog2);
webviewDialog2->layout()->addWidget(webView2);
webviewDialog2->layout()->setMargin(0);
webView2->setUrl(QString("https://ivle.nus.edu.sg/LAPI/default.aspx"));
webviewDialog2->show();
QSettings settings ("IVLE", "App");
settings.beginGroup("userinfo");
settings.setValue("tempUsername", "null");
settings.setValue("tempPassword", "null");
settings.endGroup();
getAPIkey();
}
void SettingsDialog::getAPIkey()
{
QUrl url("https://ivle.nus.edu.sg/LAPI/default.aspx");//url to obtain the APIKEY
webView2->setUrl(url);
//Signals and slot function that ensures that the webpage is loaded finish before executing the parse function
connect(webView2, SIGNAL(loadFinished(bool)), SLOT(parse(bool)));
}
//Parsing function that uses QSettings,QWebView,QWebFrame and QWebElement to extract the APIKEY from parsing HTML code on the webpage that generates the APIKEY
void SettingsDialog::parse(bool){
QWebFrame *frameInner = webView2->page()->mainFrame();
QWebElement doc = frameInner->documentElement();
QWebElement key = doc.findFirst("b");//function to find the first element in the HTML tag <b>
APIKEY = key.toPlainText();
//qDebug()<<APIKEY;
QSettings* regSett;
regSett = new QSettings("Organization-name","Project-name");
regSett->setValue("KEY",APIKEY); //Store value of key defined by user
//qDebug()<<regSett->value("KEY","does not exist");
}
void SettingsDialog::on_pushButton_6_clicked()
{
QDesktopServices::openUrl(QUrl("https://github.com/Geraldcdx/IVLEDownloader/blob/master/docs/Userguide.md"));
}
int SettingsDialog::checkCon()
{
//CODE TO CHECK INTERNET CONNECTION if not dont open
QNetworkAccessManager nam;
QNetworkRequest req(QUrl("http://www.google.com"));
QNetworkReply *reply = nam.get(req);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if(reply->bytesAvailable())return 0;
//QMessageBox::information(this, "Info", "You are connected to the internet :)");
else{
QMessageBox::critical(this, "Info","You are not connected to the internet! This app cannot be opened without an internet connection, if you want to view your files go to the directory you saved at. Please try again after you have a stable connection!");
return 1;
}
//============ENDS HERE============================================
}