-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.cpp
266 lines (217 loc) · 8.14 KB
/
form.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "form.h"
#include "ui_form.h"
#include <QDebug>
#include <QMessageBox>
#include "finddialog.h"
#include <QDesktopServices>
#include "reading_dialog.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
m_pOAuth2 = new OAuth2(this);
m_pOAuth2->setScope("https://www.googleapis.com/auth/books");
m_pOAuth2->setClientID("YOUR_CLIENT_ID_HERE");
m_pOAuth2->setRedirectURI("YOUR_REDIRECT_URI_HERE");
m_pOAuth2->setCompanyName("YOUR_COMPANY_NAME_HERE");
m_pOAuth2->setAppName("QtBooks");
connect(m_pOAuth2, SIGNAL(loginDone()), this, SLOT(loginDone()));
connect(ui->bookshelvesListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(loadVolumes()));
connect(ui->volumesListWidget, SIGNAL(currentRowChanged(int)), this, SLOT(showVolumeInfo()));
connect(ui->removeVolumeButton, SIGNAL(clicked()), this, SLOT(removeVolume()));
connect(ui->removeAllVolumesButton, SIGNAL(clicked()), this, SLOT(clearVolumes()));
connect(ui->readOnlineButton, SIGNAL(clicked()), this, SLOT(readBookOnline()));
connect(ui->readPdfButton, SIGNAL(clicked()), this, SLOT(readBookPdf()));
connect(&m_booksDataManager, SIGNAL(bookshelvesReady()), this, SLOT(getBookshelvesFromManager()));
connect(&m_booksDataManager, SIGNAL(volumesReady()), this, SLOT(getVolumesFromManager()));
connect(&m_booksDataManager, SIGNAL(errorOccured(QString)), this, SLOT(errorOccured(QString)));
connect(&m_booksDataManager, SIGNAL(volumeRemoved()), this, SLOT(loadVolumes()));
m_pFindDialog = new FindDialog(this);
m_pReadingDialog = new ReadingDialog(this);
}
Form::~Form()
{
delete ui;
}
void Form::startLogin(bool bForce)
{
//Now we allow to start logging in when m_oauth2.isAuthorized(). User can log in using another Google account.
//if(!m_oauth2.isAuthorized())
{
m_pOAuth2->startLogin(bForce); //this is a parent widget for a login dialog.
}
}
void Form::loginDone()
{
qDebug() << "Form" << __FUNCTION__;
//Now trying to get JSON string for bookshelves of the user.
//Clearing all the lists - to avoid getting volumes while updating the bookshelves (it might be another user's login).
ui->volumeInfoWebView->setHtml("");
ui->volumesListWidget->clear();
ui->bookshelvesListWidget->clear();
m_booksDataManager.getMyLibraryBookshelves(m_pOAuth2->accessToken());
}
void Form::loadVolumes()
{
ui->volumesListWidget->clear();
int shelf_index = ui->bookshelvesListWidget->currentRow();
if(shelf_index < 0)
{
return;
}
int shelf_id = m_bookshelves[shelf_index].toMap()["id"].toInt();
qDebug() << "shelf_id" << shelf_id;
m_booksDataManager.getMyLibraryVolumes(m_pOAuth2->accessToken(), shelf_id);
}
void Form::getBookshelvesFromManager()
{
qDebug() << "Form::getBookshelvesFromManager";
m_bookshelves = m_booksDataManager.getBookshelves();
QStringList lst;
for(int i = 0; i < m_bookshelves.count(); ++i)
{
lst << m_bookshelves[i].toMap()["title"].toString();
}
ui->bookshelvesListWidget->clear();
qDebug() << "Form::getBookshelvesFromManager, after clearBookshelves";
ui->bookshelvesListWidget->addItems(lst);
}
void Form::getVolumesFromManager()
{
m_volumes = m_booksDataManager.getVolumes();
QStringList lst;
for(int i = 0; i < m_volumes.count(); ++i)
{
lst << m_volumes[i].toMap()["volumeInfo"].toMap()["title"].toString();
}
ui->volumesListWidget->clear();
ui->volumesListWidget->addItems(lst);
}
void Form::showVolumeInfo()
{
ui->volumeInfoWebView->setHtml("");
int volume_index = ui->volumesListWidget->currentRow();
if(volume_index == -1)
{
return;
}
QString str;
str += "<html><body>\n";
QString strImage = m_volumes[volume_index].toMap()["volumeInfo"].toMap()["imageLinks"].toMap()["thumbnail"].toString();
str += QString("<img src=\"%1\" width=128 height=192 />").arg(strImage);
str += "<p><u>Title</u>: <b>" + m_volumes[volume_index].toMap()["volumeInfo"].toMap()["title"].toString() + "</b>\n\n";
QString authors;
QVariantList lst = m_volumes[volume_index].toMap()["volumeInfo"].toMap()["authors"].toList();
for(int i = 0; i < lst.count(); ++i)
{
authors += lst[i].toString();
if(i < lst.count()-1)
authors += ", ";
}
str += "<P><u>Authors</u>: <b>" + authors + "</b>\n\n";
str += "<P><u>Viewability</u>: <b>" + m_volumes[volume_index].toMap()["accessInfo"].toMap()["viewability"].toString() + "</b>\n\n";
str += "<P><u>SelfLink</u>: <i>" + m_volumes[volume_index].toMap()["selfLink"].toString() + "</i>\n\n";
str += "<P><u>Publisher</u>: " + m_volumes[volume_index].toMap()["volumeInfo"].toMap()["publisher"].toString() + "\n\n";
str += "<P><u>Published Date</u>: " + m_volumes[volume_index].toMap()["volumeInfo"].toMap()["publishedDate"].toString() + "\n\n";
str += "<P><u>Description</u>: " + m_volumes[volume_index].toMap()["volumeInfo"].toMap()["description"].toString() + "\n\n";
str += "</body></html>\n";
ui->volumeInfoWebView->setHtml(str);
QString pdfUrl = m_volumes[volume_index].toMap()["accessInfo"].toMap()["pdf"].toMap()["downloadLink"].toString();
if(pdfUrl.isEmpty())
{
ui->readPdfButton->setEnabled(false);
}
else
{
ui->readPdfButton->setEnabled(true);
}
}
void Form::removeVolume()
{
qDebug() << "removeVolume";
int volume_index = ui->volumesListWidget->currentRow();
if(volume_index == -1)
{
ui->volumeInfoWebView->setHtml("");
QMessageBox::warning(this, tr("Warning"), tr("No selected volume."));
return;
}
int shelf_index = ui->bookshelvesListWidget->currentRow();
if(shelf_index == -1)
{
QMessageBox::warning(this, tr("Warning"), tr("No selected bookshelf."));
return;
}
int shelf_id = m_bookshelves[shelf_index].toMap()["id"].toInt();
QString volume_id = m_volumes[volume_index].toMap()["id"].toString();
m_booksDataManager.removeVolumeFromMyLibrary(m_pOAuth2->accessToken(), shelf_id, volume_id);
}
void Form::clearVolumes()
{
qDebug() << "clearVolumes";
int shelf_index = ui->bookshelvesListWidget->currentRow();
if(shelf_index == -1)
{
QMessageBox::warning(this, tr("Warning"), tr("No selected bookshelf."));
return;
}
int shelf_id = m_bookshelves[shelf_index].toMap()["id"].toInt();
m_booksDataManager.clearVolumesFromMyLibrary(m_pOAuth2->accessToken(), shelf_id);
}
void Form::errorOccured(const QString& error)
{
if(error.indexOf("Token invalid") != -1)
{
startLogin(true);
}
else
{
QMessageBox::warning(this, tr("Error occured"), error);
}
}
void Form::findBook()
{
qDebug() << "findBook";
m_pFindDialog->setBookshelves(m_bookshelves);
m_pFindDialog->setBooksDataManager(&m_booksDataManager);
m_pFindDialog->setAccessToken(m_pOAuth2->accessToken());
m_pFindDialog->exec();
}
void Form::readBookOnline()
{
qDebug() << "readBook";
int volume_index = ui->volumesListWidget->currentRow();
if(volume_index == -1)
{
ui->volumeInfoWebView->setHtml("");
QMessageBox::warning(this, tr("Warning"), tr("No selected volume."));
return;
}
QString id = m_volumes[volume_index].toMap()["id"].toString();
QString s = QString("http://books.google.com/books?id=%1").arg(id);
qDebug() << "Before openUrl" << s;
//QDesktopServices::openUrl(QUrl(s));
m_pReadingDialog->openUrl(s);
m_pReadingDialog->show();
}
void Form::readBookPdf()
{
qDebug() << "readBookPdf";
int volume_index = ui->volumesListWidget->currentRow();
if(volume_index == -1)
{
ui->volumeInfoWebView->setHtml("");
QMessageBox::warning(this, tr("Warning"), tr("No selected volume."));
return;
}
QString pdfUrl = m_volumes[volume_index].toMap()["accessInfo"].toMap()["pdf"].toMap()["downloadLink"].toString();
if(pdfUrl.isEmpty())
{
QMessageBox::warning(this, tr("Warning"), tr("Cannot find PDF link."));
return;
}
QDesktopServices::openUrl(QUrl(pdfUrl));
//m_pReadingDialog->openUrl(pdfUrl);
//m_pReadingDialog->show();
}