argdialog.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <include/argdialog.h>
  2. #include <ui_argdialog.h>
  3. #include <algorithm>
  4. #include <QDebug>
  5. #include <QStringList>
  6. #include <QIODevice>
  7. #include <vector>
  8. #include <QTableWidgetItem>
  9. #include <QDateTime>
  10. #include <QCalendarWidget>
  11. #include <QMimeDatabase>
  12. ArgDialog::ArgDialog(QWidget *parent) :
  13. QDialog(parent),
  14. ui(new Ui::ArgDialog),
  15. m_task(Task{}),
  16. m_ig_post(IGPost{})
  17. {
  18. ui->setupUi(this);
  19. QObject::connect(ui->addFile, &QPushButton::clicked, this, [this]() {
  20. QFileDialog file_dialog;
  21. file_dialog.setStyleSheet("QFileDialog QWidget { color: white; font-weight: 700}");
  22. auto file_path = file_dialog.getOpenFileName(this,
  23. tr("Open File"), "~", tr("All Files (*.*)"), nullptr, QFileDialog::DontUseNativeDialog);
  24. qDebug() << "Selected file:" << file_path;
  25. if (file_path.size() > 0) {
  26. auto slash_index = file_path.lastIndexOf("/") + 1;
  27. QString file_name = file_path.right(file_path.size() - slash_index);
  28. QString dir = file_path.left(slash_index);
  29. qDebug() << "Dir is " << dir;
  30. QMimeDatabase db;
  31. auto is_video = db.mimeTypeForFile(file_path).name().contains("video");
  32. addItem(file_name, "file");
  33. m_ig_post.files.push_back(KFile{
  34. .name=file_name, .path=file_path, .type = is_video ? FileType::VIDEO : FileType::IMAGE
  35. });
  36. if (is_video) {
  37. qDebug() << "File discovered to be video";
  38. m_ig_post.is_video = true; // rename to "sending_video"
  39. QString preview_filename = FileUtils::generatePreview(file_path, file_name);
  40. // TODO: create some way of verifying preview generation was successful
  41. addFile("assets/previews/" + preview_filename);
  42. addItem(preview_filename, "file");
  43. addFile("assets/previews/" + preview_filename);
  44. m_ig_post.files.push_back(KFile{
  45. .name=preview_filename, .path=QCoreApplication::applicationDirPath() + "/assets/previews/" + preview_filename, .type = is_video ? FileType::VIDEO : FileType::IMAGE
  46. });
  47. } else {
  48. addFile(file_path);
  49. }
  50. }
  51. });
  52. ui->argList->setHorizontalHeaderLabels(QStringList{"Value", "Type"});
  53. ui->argList->setColumnWidth(0, 300);
  54. ui->argList->setColumnWidth(1, 40);
  55. ui->argList->setColumnWidth(2, 100);
  56. ui->argList->setColumnWidth(3, 30);
  57. ui->argList->verticalHeader()->setDefaultSectionSize(100);
  58. QObject::connect(ui->addArgument, &QPushButton::clicked, this, [this]() {
  59. QString text = ui->argInput->toPlainText();
  60. auto type = ui->argType->currentText();
  61. if (text.size() > 0) {
  62. if (type == Args::HASHTAG_TYPE) {
  63. addHashtag(text);
  64. } else if (type == Args::DESCRIPTION_TYPE) {
  65. addItem(text, type);
  66. m_ig_post.description = escapeText(text).toUtf8().constData();
  67. } else if (type == Args::PROMOTE_TYPE) {
  68. addOrReplaceInArgList(text, "promote/share");
  69. m_ig_post.promote_share = text.toUtf8().constData();
  70. } else if (type == Args::LINK_BIO_TYPE) {
  71. addOrReplaceInArgList(text, "link/bio");
  72. m_ig_post.link_in_bio = text.toUtf8().constData();
  73. } else if (type == Args::REQUESTED_BY_TYPE) {
  74. addRequestedBy(text);
  75. }
  76. ui->argInput->clear();
  77. }
  78. });
  79. QDateTime date_time = QDateTime::currentDateTime();
  80. // date_time.
  81. ui->dateTime->setDateTime(QDateTime::currentDateTime());
  82. QObject::connect(ui->dateTime, &QDateTimeEdit::dateTimeChanged, this, [this]() {
  83. auto date_time = ui->dateTime->dateTime();
  84. m_ig_post.datetime = std::string{std::to_string(date_time.toTime_t())};
  85. qDebug() << "Time changed to" << date_time;
  86. });
  87. QObject::connect(ui->argCommandButtons, static_cast<void (QDialogButtonBox::*)(QAbstractButton*)>(&QDialogButtonBox::clicked), this, [this](QAbstractButton* button) {
  88. if (button->text() == "Save") {
  89. if (m_ig_post.isReady()) {
  90. setTaskArguments();
  91. QVector<KFileData> k_file_v{};
  92. k_file_v.reserve(m_ig_post.files.size());
  93. for (const auto& kfile : m_ig_post.files) {
  94. QFile file(kfile.path);
  95. if (file.open(QIODevice::ReadOnly)) {
  96. k_file_v.push_back(KFileData{
  97. .type = kfile.type,
  98. .name = kfile.name,
  99. .bytes = file.readAll()
  100. });
  101. } else {
  102. QMessageBox::warning(
  103. this,
  104. tr("File Error"),
  105. tr("Unable to read file")
  106. );
  107. }
  108. }
  109. if (!k_file_v.empty()) {
  110. emit ArgDialog::uploadFiles(k_file_v);
  111. }
  112. emit ArgDialog::taskRequestReady(m_task, true);
  113. }
  114. defaultPost(); // reset m_ig_post to default values
  115. }
  116. });
  117. QObject::connect(ui->devTestButton, &QPushButton::clicked, this, [this]() {
  118. clearPost();
  119. m_ig_post = IGPost{
  120. .description = escapeText("My description yay!!!").toUtf8().constData(),
  121. .datetime = std::to_string(QDateTime::currentDateTime().toTime_t() + 12000),
  122. .promote_share = escapeText("If you enjoy the phrase, please like and share 🙋‍♀️").toUtf8().constData(),
  123. .link_in_bio = escapeText("Download a FREE PDF of 245 basic verbs (link 🔗 in bio 👆").toUtf8().constData(),
  124. .hashtags = {"love", "life"},
  125. .requested_by = {"unwillingagent"},
  126. .files = {{ .name = "holy.jpg", .path = "/data/c/ky_gui/assets/holy.jpg", .type = FileType::IMAGE }}
  127. };
  128. });
  129. QObject::connect(ui->clear, &QPushButton::clicked, this, [this]() {
  130. clearPost();
  131. ui->argList->setRowCount(0);
  132. qDebug() << "Task cleared";
  133. });
  134. QObject::connect(ui->default_args, &QPushButton::clicked, this, [this]() {
  135. defaultPost();
  136. ui->argList->setRowCount(0);
  137. qDebug() << "Task set to default values";
  138. });
  139. }
  140. void ArgDialog::setTaskArguments() {
  141. m_task.args.clear();
  142. std::string hashtags{};
  143. for (const auto & tag : m_ig_post.hashtags) {
  144. hashtags += "#" + tag + " ";
  145. }
  146. hashtags.pop_back();
  147. std::string requested_by{};
  148. for (const auto & name : m_ig_post.requested_by) {
  149. requested_by += "@" + name + "";
  150. }
  151. if (m_ig_post.requested_by.size() > 1) {
  152. requested_by.pop_back();
  153. }
  154. // m_task.args.push_back(m_ig_post.file.name.toUtf8().constData());
  155. m_task.args.push_back(m_ig_post.datetime);
  156. m_task.args.push_back(m_ig_post.description);
  157. m_task.args.push_back(hashtags);
  158. m_task.args.push_back(requested_by);
  159. m_task.args.push_back(m_ig_post.requested_by_phrase);
  160. m_task.args.push_back(m_ig_post.promote_share);
  161. m_task.args.push_back(m_ig_post.link_in_bio);
  162. m_task.args.push_back(std::to_string(m_ig_post.is_video));
  163. m_task.args.push_back(m_ig_post.header);
  164. }
  165. void ArgDialog::addItem(QString value, QString type) {
  166. QTableWidgetItem* item = new QTableWidgetItem(value);
  167. QTableWidgetItem* item2 = new QTableWidgetItem(type);
  168. auto row = ui->argList->rowCount();
  169. ui->argList->insertRow(row);
  170. QPushButton* q_pb = new QPushButton();
  171. q_pb->setText("Delete");
  172. q_pb->setIcon(std::move(QIcon(":/icons/icons/quit.png")));
  173. QObject::connect(q_pb, &QPushButton::clicked, this, [this, row]() {
  174. ui->argList->removeRow(row);
  175. });
  176. ui->argList->setItem(row, 0, item);
  177. ui->argList->setItem(row, 1, item2);
  178. ui->argList->setCellWidget(row, 2, q_pb);
  179. }
  180. void ArgDialog::addFile(QString path) {
  181. auto row_count = ui->argList->rowCount();
  182. QTableWidgetItem* file_item = new QTableWidgetItem();
  183. QPixmap pm{path};
  184. file_item->setData(
  185. Qt::DecorationRole,
  186. pm.scaledToHeight(ui->argList->rowHeight(0), Qt::TransformationMode::SmoothTransformation)
  187. );
  188. ui->argList->setItem(row_count - 1, 2, file_item);
  189. }
  190. void ArgDialog::clearPost() {
  191. m_ig_post.files.clear();
  192. m_ig_post.header = "";
  193. m_ig_post.datetime = "";
  194. m_ig_post.hashtags = {};
  195. m_ig_post.description = "";
  196. m_ig_post.link_in_bio = "";
  197. m_ig_post.requested_by = {};
  198. m_ig_post.promote_share = "";
  199. m_ig_post.requested_by_phrase = "";
  200. }
  201. void ArgDialog::defaultPost() {
  202. m_ig_post.files.clear();
  203. m_ig_post.header = "Learn to speak like native Korean speakers 🙆‍♀️🇰🇷";
  204. m_ig_post.datetime = "";
  205. m_ig_post.hashtags.clear();
  206. m_ig_post.description = "";
  207. m_ig_post.link_in_bio = "Subscribe to my YouTube channel (link 🔗in bio) to learn more about Korean language and culture ❤";
  208. m_ig_post.requested_by.clear();
  209. m_ig_post.promote_share = "Share the post through IG story if you enjoy the phrase 🙋‍♀️";
  210. m_ig_post.requested_by_phrase = "The phrase was requested by ";
  211. }
  212. void ArgDialog::clearTask() {
  213. m_task.args = {};
  214. m_task.mask = -1;
  215. }
  216. void ArgDialog::addRequestedBy(QString value) {
  217. if (std::find(m_ig_post.requested_by.begin(), m_ig_post.requested_by.end(), value.toUtf8().constData()) == m_ig_post.requested_by.end()) {
  218. m_ig_post.requested_by.push_back(value.toUtf8().constData());
  219. addToArgList(value, "requested_by");
  220. } else {
  221. const char* message = "You have already inputed this name under \"requested_by\"";
  222. qDebug() << message;
  223. QMessageBox::warning(
  224. this,
  225. tr("Requested By"),
  226. tr(message)
  227. );
  228. }
  229. }
  230. void ArgDialog::addToArgList(QString value, QString type) {
  231. for (int i = 0; i < ui->argList->rowCount(); i++) {
  232. auto item = ui->argList->item(i, 1);
  233. if (item) {
  234. if (QString::compare(item->text(), type) == 0) {
  235. auto text = ui->argList->item(i, 0)->text();
  236. text.append("\n");
  237. text += value;
  238. ui->argList->item(i, 0)->setText(text);
  239. return;
  240. }
  241. }
  242. }
  243. addItem(value, type);
  244. }
  245. void ArgDialog::addOrReplaceInArgList(QString value, QString type) {
  246. for (int i = 0; i < ui->argList->rowCount(); i++) {
  247. auto item = ui->argList->item(i, 1);
  248. if (item) {
  249. if (QString::compare(item->text(), type) == 0) {
  250. ui->argList->item(i, 0)->setText(value);
  251. return;
  252. }
  253. }
  254. }
  255. addItem(value, type);
  256. }
  257. void ArgDialog::addHashtag(QString tag) {
  258. QStringList tags = tag.split(" ");
  259. for (const auto& tag : tags) {
  260. if (std::find(m_ig_post.hashtags.begin(), m_ig_post.hashtags.end(), tag.toUtf8().constData()) == m_ig_post.hashtags.end()) {
  261. m_ig_post.hashtags.push_back(tag.toUtf8().constData());
  262. addToArgList(tag, "hashtag");
  263. } else {
  264. const char* message = "Can't add the same hashtag twice";
  265. qDebug() << message;
  266. QMessageBox::warning(
  267. this,
  268. tr("Hashtags"),
  269. tr(message)
  270. );
  271. }
  272. }
  273. }
  274. void ArgDialog::keyPressEvent(QKeyEvent *e) {
  275. if (Qt::ControlModifier) {
  276. if(e->key()==Qt::Key_Return || e->key()==Qt::Key_Enter) {
  277. ui->addArgument->clicked();
  278. auto idx = ui->argType->currentIndex();
  279. if (idx != (ui->argType->count() - 1)) {
  280. ui->argType->setCurrentIndex(idx + 1);
  281. }
  282. }
  283. }
  284. }
  285. ArgDialog::~ArgDialog()
  286. {
  287. delete ui;
  288. }