argdialog.cpp 11 KB

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