argdialog.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. }
  111. });
  112. QObject::connect(ui->devTestButton, &QPushButton::clicked, this, [this]() {
  113. clearPost();
  114. m_ig_post = IGPost{
  115. .description = escapeText("My description yay!!!").toUtf8().constData(),
  116. .datetime = std::to_string(QDateTime::currentDateTime().toTime_t() + 12000),
  117. .promote_share = escapeText("If you enjoy the phrase, please like and share 🙋‍♀️").toUtf8().constData(),
  118. .link_in_bio = escapeText("Download a FREE PDF of 245 basic verbs (link 🔗 in bio 👆").toUtf8().constData(),
  119. .hashtags = {"love", "life"},
  120. .requested_by = {"unwillingagent"},
  121. .files = {{ .name = "holy.jpg", .path = "/data/c/ky_gui/assets/holy.jpg", .type = FileType::IMAGE }}
  122. };
  123. });
  124. QObject::connect(ui->clear, &QPushButton::clicked, this, [this]() {
  125. m_task.args.clear();
  126. m_ig_post.files.clear();
  127. m_ig_post.datetime = "";
  128. m_ig_post.hashtags.clear();
  129. m_ig_post.description = "";
  130. m_ig_post.link_in_bio = "";
  131. m_ig_post.requested_by.clear();
  132. m_ig_post.promote_share = "";
  133. m_ig_post.is_video = false;
  134. ui->argList->clear();
  135. ui->argList->setRowCount(0);
  136. qDebug() << "Task cleared";
  137. });
  138. }
  139. void ArgDialog::setTaskArguments() {
  140. m_task.args.clear();
  141. std::string hashtags{};
  142. for (const auto & tag : m_ig_post.hashtags) {
  143. hashtags += "#" + tag + " ";
  144. }
  145. hashtags.pop_back();
  146. std::string requested_by{};
  147. for (const auto & name : m_ig_post.requested_by) {
  148. requested_by += "@" + name + "";
  149. }
  150. if (m_ig_post.requested_by.size() > 1) {
  151. requested_by.pop_back();
  152. }
  153. // m_task.args.push_back(m_ig_post.file.name.toUtf8().constData());
  154. m_task.args.push_back(m_ig_post.datetime);
  155. m_task.args.push_back(m_ig_post.description);
  156. m_task.args.push_back(hashtags);
  157. m_task.args.push_back(requested_by);
  158. m_task.args.push_back(m_ig_post.requested_by_phrase);
  159. m_task.args.push_back(m_ig_post.promote_share);
  160. m_task.args.push_back(m_ig_post.link_in_bio);
  161. m_task.args.push_back(std::to_string(m_ig_post.is_video));
  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. ui->argList->setItem(row, 0, item);
  169. ui->argList->setItem(row, 1, item2);
  170. }
  171. void ArgDialog::addFile(QString path) {
  172. auto row_count = ui->argList->rowCount();
  173. QTableWidgetItem* file_item = new QTableWidgetItem();
  174. QPixmap pm{path};
  175. file_item->setData(
  176. Qt::DecorationRole,
  177. pm.scaledToHeight(ui->argList->rowHeight(0), Qt::TransformationMode::SmoothTransformation)
  178. );
  179. ui->argList->setItem(row_count - 1, 2, file_item);
  180. }
  181. void ArgDialog::clearPost() {
  182. m_ig_post.files.clear();
  183. m_ig_post.datetime = "";
  184. m_ig_post.hashtags = {};
  185. m_ig_post.description = "";
  186. m_ig_post.link_in_bio = "";
  187. m_ig_post.requested_by = {};
  188. m_ig_post.promote_share = "";
  189. m_ig_post.requested_by_phrase = "";
  190. }
  191. void ArgDialog::clearTask() {
  192. m_task.args = {};
  193. m_task.mask = -1;
  194. }
  195. void ArgDialog::addRequestedBy(QString value) {
  196. if (std::find(m_ig_post.requested_by.begin(), m_ig_post.requested_by.end(), value.toUtf8().constData()) == m_ig_post.requested_by.end()) {
  197. m_ig_post.requested_by.push_back(value.toUtf8().constData());
  198. addToArgList(value, "requested_by");
  199. } else {
  200. const char* message = "You have already inputed this name under \"requested_by\"";
  201. qDebug() << message;
  202. QMessageBox::warning(
  203. this,
  204. tr("Requested By"),
  205. tr(message)
  206. );
  207. }
  208. }
  209. void ArgDialog::addToArgList(QString value, QString type) {
  210. for (int i = 0; i < ui->argList->rowCount(); i++) {
  211. auto item = ui->argList->item(i, 1);
  212. if (item) {
  213. if (QString::compare(item->text(), type) == 0) {
  214. auto text = ui->argList->item(i, 0)->text();
  215. text.append("\n");
  216. text += value;
  217. ui->argList->item(i, 0)->setText(text);
  218. return;
  219. }
  220. }
  221. }
  222. addItem(value, type);
  223. }
  224. void ArgDialog::addOrReplaceInArgList(QString value, QString type) {
  225. for (int i = 0; i < ui->argList->rowCount(); i++) {
  226. auto item = ui->argList->item(i, 1);
  227. if (item) {
  228. if (QString::compare(item->text(), type) == 0) {
  229. ui->argList->item(i, 0)->setText(value);
  230. return;
  231. }
  232. }
  233. }
  234. addItem(value, type);
  235. }
  236. void ArgDialog::addHashtag(QString tag) {
  237. QStringList tags = tag.split(" ");
  238. for (const auto& tag : tags) {
  239. if (std::find(m_ig_post.hashtags.begin(), m_ig_post.hashtags.end(), tag.toUtf8().constData()) == m_ig_post.hashtags.end()) {
  240. m_ig_post.hashtags.push_back(tag.toUtf8().constData());
  241. addToArgList(tag, "hashtag");
  242. } else {
  243. const char* message = "Can't add the same hashtag twice";
  244. qDebug() << message;
  245. QMessageBox::warning(
  246. this,
  247. tr("Hashtags"),
  248. tr(message)
  249. );
  250. }
  251. }
  252. }
  253. ArgDialog::~ArgDialog()
  254. {
  255. delete ui;
  256. }