argdialog.cpp 11 KB

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