dialogs.cpp 12 KB

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