dialogs.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Project PhotoBunny
  3. * 2014 Gwilyn Saunders
  4. */
  5. #include <QtGui>
  6. #include <QtWidgets>
  7. #include <QWebFrame>
  8. #include "dialogs.h"
  9. #include "common.h"
  10. #include "fbApi.h"
  11. LoginDialog::LoginDialog(FbApi *fb, QWidget *parent)
  12. : QDialog(parent) {
  13. //housekeeping
  14. setWindowTitle("Login to Facebook | PhotoBunny");
  15. QFormLayout *layout = new QFormLayout(this);
  16. layout->setSizeConstraint(QLayout::SetFixedSize);
  17. setLayout(layout);
  18. //create form elements
  19. userEdit = new QLineEdit(this);
  20. userEdit->setFixedWidth(200);
  21. passEdit = new QLineEdit(this);
  22. timeEdit = new QSpinBox(this);
  23. hdEdit = new QCheckBox(this);
  24. cacheEdit = new QCheckBox(this);
  25. passEdit->setEchoMode(QLineEdit::Password);
  26. timeEdit->setMaximum(300);
  27. timeEdit->setMinimum(1);
  28. //load settings
  29. st = new QSettings(company_str, app_str);
  30. this->fb = fb;
  31. //permission dialog
  32. perms = new PermissionDialog(this);
  33. userEdit->setText(fb->user());
  34. hdEdit->setChecked(fb->highRes);
  35. cacheEdit->setChecked(fb->caching);
  36. timeEdit->setValue(st->value("timeout").toLongLong()/1000);
  37. //create buttons
  38. clearButton = new QPushButton("Clear Cache", this);
  39. clearButton->setMaximumWidth(90);
  40. clearButton->setFocusPolicy(Qt::NoFocus);
  41. okButton = new QPushButton("Login", this);
  42. exitButton = new QPushButton("Cancel", this);
  43. okButton->setMaximumWidth(70);
  44. exitButton->setMaximumWidth(70);
  45. //add buttons
  46. QHBoxLayout *buttons = new QHBoxLayout(this);
  47. buttons->addStretch();
  48. buttons->addWidget(okButton);
  49. buttons->addWidget(exitButton);
  50. //mix form
  51. layout->addRow("Username:", userEdit);
  52. layout->addRow("Password:", passEdit);
  53. layout->addRow("Timeout:", timeEdit);
  54. layout->addRow("High Res:", hdEdit);
  55. layout->addRow("Caching:", cacheEdit);
  56. layout->addWidget(clearButton);
  57. layout->addRow(buttons);
  58. connect(clearButton, SIGNAL(clicked()), this, SLOT(clearCache()));
  59. connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
  60. connect(fb, SIGNAL(loginResult(LoginStatus)), this, SLOT(loggedIn(LoginStatus)));
  61. connect(exitButton, SIGNAL(clicked()), this, SLOT(reject()));
  62. connect(perms, SIGNAL(accepted()), fb, SLOT(acceptPermissions()));
  63. connect(perms, SIGNAL(rejected()), this, SLOT(showDeclineBox()));
  64. }
  65. void LoginDialog::clearCache() {
  66. QDir tmpDir = QDir(fb->getTempPath());
  67. int count = tmpDir.count();
  68. qint64 size = 0;
  69. foreach(QFileInfo i, tmpDir.entryInfoList()) {
  70. size += i.size();
  71. }
  72. QString sizeStr;
  73. if (size <= 1024) {
  74. sizeStr = QString("%1 bytes").arg(size);
  75. } else if (size <= 1024*1024) {
  76. sizeStr = QString("%1 KiB").arg(size/1024);
  77. } else if (size <= 1024*1024*1024) {
  78. sizeStr = QString("%1 MiB").arg(size/1024/1024);
  79. } else {
  80. sizeStr = QString("%1 GiB").arg(size/1024/1024/1024);
  81. }
  82. int res = QMessageBox::question(this,
  83. "Confirm",
  84. QString("There are currently %1 images in the cache\n"
  85. "totalling %2 in disk space.\n"
  86. "Are you sure you want to delete the cached images?").arg(count).arg(sizeStr));
  87. if (res == QMessageBox::Yes) {
  88. qDebug() << "deleting cache files";
  89. foreach(QString f, tmpDir.entryList()) {
  90. tmpDir.remove(f);
  91. }
  92. }
  93. }
  94. void LoginDialog::okClicked() {
  95. okButton->setEnabled(false);
  96. userEdit->setEnabled(false);
  97. passEdit->setEnabled(false);
  98. timeEdit->setEnabled(false);
  99. hdEdit->setEnabled(false);
  100. cacheEdit->setEnabled(false);
  101. st->setValue("timeout", timeEdit->value()*1000);
  102. fb->caching = cacheEdit->isChecked();
  103. fb->highRes = hdEdit->isChecked();
  104. fb->authenticate(userEdit->text(), passEdit->text());
  105. }
  106. void LoginDialog::loggedIn(LoginStatus status) {
  107. switch(status) {
  108. case SUCCESS:
  109. accept();
  110. break;
  111. case BAD_PASS:
  112. QMessageBox::information(this,
  113. "Bad Login",
  114. "Password or email is incorrect.");
  115. okButton->setEnabled(true);
  116. userEdit->setEnabled(true);
  117. passEdit->setEnabled(true);
  118. timeEdit->setEnabled(true);
  119. hdEdit->setEnabled(true);
  120. cacheEdit->setEnabled(true);
  121. break;
  122. case BAD_PERM:
  123. perms->show();
  124. break;
  125. default:
  126. qDebug() << "Impossible switch case!";
  127. }
  128. }
  129. void LoginDialog::showDeclineBox() {
  130. QMessageBox::information(this,
  131. "No Permissions",
  132. "This app requires Facebook permissions, please try again.");
  133. okButton->setEnabled(true);
  134. userEdit->setEnabled(true);
  135. passEdit->setEnabled(true);
  136. timeEdit->setEnabled(true);
  137. hdEdit->setEnabled(true);
  138. cacheEdit->setEnabled(true);
  139. }
  140. AlbumDialog::AlbumDialog(FbApi *fb, QWidget *parent)
  141. : QDialog(parent) {
  142. //housekeeping
  143. setFixedHeight(500);
  144. setFixedWidth(350);
  145. setWindowTitle("Select an Album | PhotoBunny");
  146. QVBoxLayout *layout = new QVBoxLayout(this);
  147. setLayout(layout);
  148. st = new QSettings(company_str, app_str);
  149. this->fb = fb;
  150. //widgets
  151. list = new QListWidget(this);
  152. QPushButton *okButton = new QPushButton("Select", this);
  153. okButton->setMaximumWidth(70);
  154. QPushButton *cancelButton = new QPushButton("Cancel", this);
  155. cancelButton->setMaximumWidth(70);
  156. //mixing
  157. QHBoxLayout *buttons = new QHBoxLayout(this);
  158. buttons->addStretch();
  159. buttons->addWidget(okButton);
  160. buttons->addWidget(cancelButton);
  161. layout->addWidget(list);
  162. layout->addLayout(buttons);
  163. connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
  164. connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
  165. connect(fb, SIGNAL(albumsLoaded()), this, SLOT(loadAlbums()));
  166. if (fb->loggedIn()) {
  167. fb->loadAlbums();
  168. } else {
  169. QMessageBox::critical(this,
  170. "Login Error",
  171. "Not logged in!");
  172. }
  173. }
  174. void AlbumDialog::okClicked() {
  175. qDebug() << "registering albums";
  176. QStringList checked;
  177. for (int i=0; i<list->count(); i++) {
  178. QListWidgetItem *item = list->item(i);
  179. if (item->checkState() == Qt::Checked) {
  180. checked.append(item->data(Qt::UserRole).toString());
  181. qDebug() << "id: " << item->data(Qt::UserRole).toString();
  182. }
  183. }
  184. fb->selectAlbums(checked);
  185. accept();
  186. }
  187. void AlbumDialog::loadAlbums() {
  188. qDebug() << "album list loaded";
  189. foreach (QVariantMap album, fb->allAlbums()) {
  190. QListWidgetItem *item = new QListWidgetItem(album.value("name").toString());
  191. item->setData(Qt::UserRole, album.value("id").toString());
  192. item->setCheckState(Qt::Unchecked);
  193. list->addItem(item);
  194. }
  195. }
  196. ScreenDialog::ScreenDialog(QWidget *parent)
  197. : QDialog(parent) {
  198. //housekeeping
  199. setWindowTitle("Select a Screen | PhotoBunny");
  200. QVBoxLayout *layout = new QVBoxLayout(this);
  201. layout->setSizeConstraint(QLayout::SetFixedSize);
  202. setLayout(layout);
  203. st = new QSettings(company_str, app_str);
  204. //read screen sizes
  205. QMap<int, QRect> rects;
  206. int offsetX = 0, offsetY = 0;
  207. qDebug() << "screens";
  208. for (int i=0; i<desktop.screenCount(); i++) {
  209. QRect r = desktop.screenGeometry(i);
  210. rects.insert(i, r);
  211. if (r.x() < 0 && r.x() < offsetX) offsetX = r.x();
  212. if (r.y() < 0 && r.y() < offsetY) offsetY = r.y();
  213. qDebug() << i << ":" << r;
  214. }
  215. //adjust for positive integers
  216. for (int i=0; i<rects.size(); i++) {
  217. QRect r = rects.take(i).adjusted(-offsetX, -offsetY, -offsetX, -offsetY);
  218. rects.insert(i, r);
  219. }
  220. //create screen grid (relative to total screen size)
  221. QWidget *screenBox = new QWidget(this);
  222. screenBox->setFixedWidth(desktop.width()/box_scale + box_scale);
  223. screenBox->setFixedHeight(desktop.height()/box_scale + box_scale);
  224. QGridLayout *screenGrid = new QGridLayout(screenBox);
  225. screenBox->setLayout(screenGrid);
  226. //load screens into widgets
  227. foreach (int i, rects.keys()) {
  228. QRect r = rects.value(i);
  229. DesktopBox *box = new DesktopBox(i, r.width()/box_scale, r.height()/box_scale);
  230. box->setIdentifier(desktop.screenGeometry(i));
  231. boxes.append(box);
  232. screenGrid->addWidget(box, r.y(), r.x());
  233. connect(box, SIGNAL(clicked()), this, SLOT(clearHighlights()));
  234. connect(box, SIGNAL(clicked()), box, SLOT(highlight()));
  235. connect(box, SIGNAL(clicked(int)), this, SLOT(selectScreen(int)));
  236. connect(box, SIGNAL(doubleClicked()), box, SLOT(identify()));
  237. }
  238. //set a default screen value, incase we skip this dialog
  239. st->setValue("screen", 0);
  240. //button bar
  241. screenLabel = new QLabel("?", this);
  242. screenLabel->setFont(QFont("Arial", 12, QFont::Normal));
  243. okButton = new QPushButton("Select", this);
  244. okButton->setMaximumWidth(70);
  245. okButton->setEnabled(false);
  246. QPushButton *cancelButton = new QPushButton("Cancel", this);
  247. cancelButton->setMaximumWidth(70);
  248. //mix it up!
  249. QHBoxLayout *buttons = new QHBoxLayout(this);
  250. buttons->addStretch();
  251. buttons->addWidget(screenLabel);
  252. buttons->addWidget(okButton);
  253. buttons->addWidget(cancelButton);
  254. layout->addWidget(screenBox);
  255. layout->addLayout(buttons);
  256. //events
  257. connect(okButton, SIGNAL(clicked()), this, SLOT(okClicked()));
  258. connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
  259. }
  260. void ScreenDialog::okClicked() {
  261. st->setValue("screen", selectedScreen);
  262. accept();
  263. }
  264. void ScreenDialog::identify() {
  265. foreach(DesktopBox *d, boxes) {
  266. d->identify();
  267. }
  268. }
  269. void ScreenDialog::selectScreen(int screen) {
  270. selectedScreen = screen;
  271. okButton->setEnabled(true);
  272. okButton->setFocus();
  273. screenLabel->setText(QString::number(screen));
  274. }
  275. void ScreenDialog::clearHighlights() {
  276. foreach (DesktopBox *b, boxes) {
  277. b->highlight(false);
  278. }
  279. }
  280. PermissionDialog::PermissionDialog(QWidget *parent)
  281. : QDialog(parent) {
  282. //housekeeping
  283. setWindowTitle("Permissions | PhotoBunny");
  284. QVBoxLayout *layout = new QVBoxLayout(this);
  285. setFixedHeight(250);
  286. setFixedWidth(150);
  287. layout->setSizeConstraint(QLayout::SetFixedSize);
  288. setLayout(layout);
  289. QPushButton* okButton = new QPushButton("Accept", this);
  290. QPushButton* cancelButton = new QPushButton("Decline", this);
  291. QLabel* msgLabel = new QLabel("<strong>PhotoBunny will receive the following info: your public profile and Photos.</strong><br>"
  292. "Your public profile includes name, profile picture, age range, gender, language, country and other public info.<br>"
  293. "<i>(This does not let the app post to Facebook.)</i><br>"
  294. "<a href=\"http://www.gwillz.com.au/blog-privacy_policy\">Privacy Policy</a>");
  295. QHBoxLayout *buttons = new QHBoxLayout(this);
  296. buttons->addStretch();
  297. buttons->addWidget(okButton);
  298. buttons->addWidget(cancelButton);
  299. layout->addWidget(msgLabel);
  300. layout->addLayout(buttons);
  301. connect(okButton, SIGNAL(pressed()), this, SLOT(accept()));
  302. connect(cancelButton, SIGNAL(pressed()), this, SLOT(reject()));
  303. }