argdialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include <include/ui/argdialog.h>
  2. #include <ui_argdialog.h>
  3. #include <QCalendarWidget>
  4. #include <QDateTime>
  5. #include <QDebug>
  6. #include <QIODevice>
  7. #include <QMimeDatabase>
  8. #include <QStringList>
  9. #include <QTableWidgetItem>
  10. #include <algorithm>
  11. #include <vector>
  12. ArgDialog::ArgDialog(QWidget *parent) : QDialog(parent), ui(new Ui::ArgDialog), m_task(nullptr), m_ig_post(IGPost{}) {
  13. ui->setupUi(this);
  14. ui->argCommandButtons->button(QDialogButtonBox::Close)
  15. ->setStyleSheet(QString("background:%1").arg("#2f535f"));
  16. QObject::connect(ui->addFile, &QPushButton::clicked, this, [this]() {
  17. KFileDialog file_dialog{};
  18. auto file_path = file_dialog.openFileDialog(m_file_path);
  19. qDebug() << "Selected file:" << file_path;
  20. if (file_path.size() > 0) {
  21. auto slash_index = file_path.lastIndexOf("/") + 1;
  22. QString file_name = file_path.right(file_path.size() - slash_index);
  23. QString dir = file_path.left(slash_index);
  24. QFile file(file_path);
  25. if (file.open(QIODevice::ReadOnly)) {
  26. QMimeDatabase db;
  27. auto is_video = db.mimeTypeForFile(file_path).name().contains("video");
  28. addItem(file_name, "file");
  29. m_task->setArgument("files", Scheduler::KFileData{
  30. .name = file_name,
  31. .type = is_video ? FileType::VIDEO : FileType::IMAGE,
  32. .path = file_path,
  33. .bytes = file.readAll()});
  34. if (is_video) {
  35. qDebug() << "File discovered to be video";
  36. m_task->setArgument("is_video", true);
  37. QString preview_filename = FileUtils::generatePreview(file_path, file_name);
  38. QString preview_file_path = QCoreApplication::applicationDirPath()
  39. + "/assets/previews/" + preview_filename;
  40. file.setFileName(preview_file_path);
  41. if (file.open(QIODevice::ReadOnly)) {
  42. // TODO: create some way of verifying preview generation was successful
  43. addFile("assets/previews/" + preview_filename);
  44. addItem(preview_filename, "file");
  45. addFile("assets/previews/" + preview_filename);
  46. m_task->setArgument("files", Scheduler::KFileData{
  47. .name = preview_filename,
  48. .type = is_video ? FileType::VIDEO : FileType::IMAGE,
  49. .path = preview_file_path,
  50. .bytes = file.readAll()});
  51. } else {
  52. qDebug() << "Could not add preview image for video";
  53. QMessageBox::warning(this, tr("File Error"), tr("Could not add preview image for video"));
  54. }
  55. } else {
  56. addFile(file_path);
  57. }
  58. } else {
  59. qDebug() << "Unable to open selected file";
  60. QMessageBox::warning(this, tr("File Error"), tr("Unable to open selected file"));
  61. }
  62. } else {
  63. qDebug() << "Could not read the file path";
  64. QMessageBox::warning(this, tr("File Error"), tr("Could not read the file path"));
  65. }
  66. });
  67. QObject::connect(ui->user, &QComboBox::currentTextChanged, this,
  68. [this](const QString &text) {
  69. m_ig_post.user = text.toUtf8().constData();
  70. });
  71. ui->argList->setHorizontalHeaderLabels(
  72. QStringList{"Type", "Value", "Preview", "Delete"});
  73. ui->argList->setColumnWidth(0, 40);
  74. ui->argList->setColumnWidth(1, 520);
  75. ui->argList->setColumnWidth(2, 220);
  76. ui->argList->setColumnWidth(3, 30);
  77. ui->argList->verticalHeader()->setDefaultSectionSize(100);
  78. QObject::connect(ui->addArgument, &QPushButton::clicked, this, [this]() {
  79. QString text = ui->argInput->toPlainText();
  80. // TODO: argType values need to be set by configuration
  81. // Can this somehow be known via the flatbuffer schema? I think not
  82. // handling of type needs to be abstracted by a class which can be
  83. // subclassed for various types of task: Instagram, etc
  84. auto type = ui->argType->currentText();
  85. if (text.size() > 0) {
  86. if (type == Args::HASHTAG_TYPE) {
  87. addHashtag(text);
  88. } else if (type == Args::DESCRIPTION_TYPE) {
  89. addItem(text, type);
  90. m_ig_post.description = escapeText(text).toUtf8().constData();
  91. } else if (type == Args::PROMOTE_TYPE) {
  92. addOrReplaceInArgList(text, "promote/share");
  93. m_ig_post.promote_share = text.toUtf8().constData();
  94. } else if (type == Args::LINK_BIO_TYPE) {
  95. addOrReplaceInArgList(text, "link/bio");
  96. m_ig_post.link_in_bio = text.toUtf8().constData();
  97. } else if (type == Args::REQUESTED_BY_TYPE) {
  98. addRequestedBy(text);
  99. }
  100. ui->argInput->clear();
  101. }
  102. });
  103. ui->dateTime->setDateTime(QDateTime::currentDateTime());
  104. QObject::connect(ui->dateTime, &QDateTimeEdit::dateTimeChanged, this, [this]() {
  105. auto date_time = ui->dateTime->dateTime();
  106. m_ig_post.datetime = std::string{std::to_string(date_time.toTime_t())};
  107. qDebug() << "Time changed to" << date_time;
  108. });
  109. QObject::connect(ui->argCommandButtons,
  110. static_cast<void (QDialogButtonBox::*)(QAbstractButton *)>(
  111. &QDialogButtonBox::clicked),
  112. this, [this](QAbstractButton *button) {
  113. if (button->text() == "Save") {
  114. if (m_task->isReady()) {
  115. setTaskArguments();
  116. emit ArgDialog::taskRequestReady(m_task);
  117. }
  118. clearPost(); // reset m_ig_post to default values
  119. }
  120. });
  121. QObject::connect(ui->clear, &QPushButton::clicked, this, [this]() {
  122. clearPost();
  123. ui->argList->setRowCount(0);
  124. qDebug() << "Task cleared and restored to default values";
  125. });
  126. }
  127. void ArgDialog::setTaskArguments() {
  128. QString hashtags{};
  129. for (const auto &tag : std::get<VariantIndex::STRVEC>(m_task->getTaskArgument("hashtags"))) {
  130. hashtags += "#" + tag + " ";
  131. }
  132. hashtags.chop(1);
  133. QString requested_by{};
  134. for (const auto &name : std::get<VariantIndex::STRVEC>(m_task->getTaskArgument("requested_by"))) {
  135. requested_by += "@" + name + "";
  136. }
  137. if (m_ig_post.requested_by.size() > 1) {
  138. requested_by.chop(1);
  139. }
  140. m_task->setArgument("hashtags", hashtags);
  141. m_task->setArgument("requested_by", requested_by);
  142. }
  143. void ArgDialog::addItem(QString value, QString type) {
  144. QTableWidgetItem *item = new QTableWidgetItem(type);
  145. QTableWidgetItem *item2 = new QTableWidgetItem(value);
  146. auto row = ui->argList->rowCount();
  147. ui->argList->insertRow(row);
  148. QPushButton *q_pb = new QPushButton();
  149. q_pb->setText("Delete");
  150. q_pb->setIcon(std::move(QIcon(":/icons/icons/quit.png")));
  151. QObject::connect(q_pb, &QPushButton::clicked, this, [this]() {
  152. auto row_index = ui->argList->currentRow();
  153. // If deleted item is a file, we need to remove it from the task
  154. auto type = ui->argList->item(row_index, 0);
  155. if (type->text() == "file") {
  156. auto value = ui->argList->item(row_index, 1);
  157. if (!value->text().isEmpty()) {
  158. auto file_it = std::find_if(m_ig_post.files.begin(), m_ig_post.files.end(),
  159. [value](const KFile &file) { return file.name == value->text(); });
  160. if (file_it != m_ig_post.files.end()) { // If file was matched
  161. qDebug() << "Removing file from task arguments";
  162. m_ig_post.files.erase(file_it);
  163. }
  164. }
  165. }
  166. ui->argList->removeRow(row_index);
  167. });
  168. ui->argList->setItem(row, 0, item);
  169. ui->argList->setItem(row, 1, item2);
  170. ui->argList->setCellWidget(row, 3, q_pb);
  171. }
  172. void ArgDialog::addFile(QString path) {
  173. auto row_count = ui->argList->rowCount();
  174. QTableWidgetItem *file_item = new QTableWidgetItem();
  175. QPixmap pm{path};
  176. file_item->setData(
  177. Qt::DecorationRole,
  178. pm.scaledToHeight(ui->argList->rowHeight(0),
  179. Qt::TransformationMode::SmoothTransformation));
  180. ui->argList->setItem(row_count - 1, 2, file_item);
  181. }
  182. void ArgDialog::clearPost() {
  183. QDateTime date_time = QDateTime::currentDateTime();
  184. ui->dateTime->setDateTime(date_time);
  185. m_task->clear();
  186. m_task->setDefaultValues();
  187. m_task->setArgument("datetime", date_time.toString());
  188. m_task->setArgument("user", ui->user->currentText());
  189. ui->argType->setCurrentIndex(0);
  190. ui->argList->setRowCount(0);
  191. }
  192. void ArgDialog::clearTask() { m_task->clear(); }
  193. void ArgDialog::addRequestedBy(QString value) {
  194. QStringList names = value.split(" ");
  195. for (const auto &name : names) {
  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(name.toUtf8().constData());
  198. addToArgList(name, "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. }
  210. void ArgDialog::addToArgList(QString value, QString type) {
  211. for (int i = 0; i < ui->argList->rowCount(); i++) {
  212. auto item = ui->argList->item(i, 0);
  213. if (item) {
  214. if (QString::compare(item->text(), type) == 0) {
  215. auto text = ui->argList->item(i, 1)->text();
  216. text.append("\n");
  217. text += value;
  218. ui->argList->item(i, 1)->setText(text);
  219. return;
  220. }
  221. }
  222. }
  223. addItem(value, type);
  224. }
  225. void ArgDialog::addOrReplaceInArgList(QString value, QString type) {
  226. for (int i = 0; i < ui->argList->rowCount(); i++) {
  227. auto item = ui->argList->item(i, 1);
  228. if (item) {
  229. if (QString::compare(item->text(), type) == 0) {
  230. ui->argList->item(i, 1)->setText(value);
  231. return;
  232. }
  233. }
  234. }
  235. addItem(value, type);
  236. }
  237. void ArgDialog::addHashtag(QString tag) {
  238. QStringList tags = tag.split(" ");
  239. for (const auto& tag : tags) {
  240. if (std::find(m_ig_post.hashtags.begin(), m_ig_post.hashtags.end(), tag.toUtf8().constData()) == m_ig_post.hashtags.end()) {
  241. m_ig_post.hashtags.push_back(tag.toUtf8().constData());
  242. addToArgList(tag, "hashtag");
  243. } else {
  244. const char* message = "Can't add the same hashtag twice";
  245. qDebug() << message;
  246. QMessageBox::warning(
  247. this,
  248. tr("Hashtags"),
  249. tr(message)
  250. );
  251. }
  252. }
  253. }
  254. void ArgDialog::keyPressEvent(QKeyEvent *e) {
  255. if (Qt::ControlModifier) {
  256. if(e->key()==Qt::Key_Return || e->key()==Qt::Key_Enter) {
  257. ui->addArgument->clicked();
  258. auto idx = ui->argType->currentIndex();
  259. if (idx != (ui->argType->count() - 1)) {
  260. ui->argType->setCurrentIndex(idx + 1);
  261. }
  262. }
  263. }
  264. }
  265. void ArgDialog::setFilePath(QString path) { m_file_path = path; }
  266. void ArgDialog::setConfig(QString config_string) {
  267. m_config_string = config_string;
  268. ui->user->addItems(getValueArgs(m_config_string.toUtf8(), "users"));
  269. if (ui->user->count() > 0) {
  270. m_ig_post.user = ui->user->itemText(0).toUtf8().constData();
  271. }
  272. }
  273. ArgDialog::~ArgDialog()
  274. {
  275. delete ui;
  276. }
  277. void ArgDialog::accept() { qDebug() << "Sending request to schedule a task.."; }
  278. void ArgDialog::setArgTypes() {
  279. ui->argType->clear();
  280. for (const auto &arg : m_task->getTaskArguments()) {
  281. ui->argType->addItem(arg->text());
  282. }
  283. }