File indexing completed on 2024-04-28 04:55:46
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2013-2014 Andrea Scarpino <scarpino@kde.org> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #include "pumpiomessagedialog.h" 0010 0011 #include <QDialogButtonBox> 0012 #include <QFileDialog> 0013 #include <QPointer> 0014 #include <QPushButton> 0015 0016 #include "pumpioaccount.h" 0017 #include "pumpiodebug.h" 0018 #include "pumpiomicroblog.h" 0019 #include "pumpiopost.h" 0020 0021 class PumpIOMessageDialog::Private 0022 { 0023 public: 0024 Choqok::Account *account; 0025 QString mediumToAttach; 0026 QPointer<QLabel> mediumName; 0027 QPointer<QPushButton> btnCancel; 0028 }; 0029 0030 PumpIOMessageDialog::PumpIOMessageDialog(Choqok::Account *theAccount, QWidget *parent, 0031 Qt::WindowFlags flags) 0032 : QDialog(parent, flags) 0033 , d(new Private) 0034 { 0035 d->account = theAccount; 0036 0037 setupUi(this); 0038 0039 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0040 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0041 okButton->setDefault(true); 0042 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0043 connect(buttonBox, &QDialogButtonBox::accepted, this, &PumpIOMessageDialog::accept); 0044 connect(buttonBox, &QDialogButtonBox::rejected, this, &PumpIOMessageDialog::reject); 0045 verticalLayout->addWidget(buttonBox); 0046 0047 PumpIOAccount *acc = qobject_cast<PumpIOAccount *>(theAccount); 0048 if (acc) { 0049 for (const QVariant &list: acc->lists()) { 0050 QVariantMap l = list.toMap(); 0051 QListWidgetItem *item = new QListWidgetItem; 0052 item->setText(l.value(QLatin1String("name")).toString()); 0053 item->setData(Qt::UserRole, l.value(QLatin1String("id")).toString()); 0054 toList->addItem(item); 0055 ccList->addItem(item->clone()); 0056 } 0057 //Lists are not sorted 0058 toList->sortItems(); 0059 ccList->sortItems(); 0060 0061 for (const QString &username: acc->following()) { 0062 QListWidgetItem *item = new QListWidgetItem; 0063 item->setText(PumpIOMicroBlog::userNameFromAcct(username)); 0064 item->setData(Qt::UserRole, username); 0065 toList->addItem(item); 0066 ccList->addItem(item->clone()); 0067 } 0068 } 0069 0070 connect(btnReload, &QPushButton::clicked, this, &PumpIOMessageDialog::fetchFollowing); 0071 connect(btnAttach, &QPushButton::clicked, this, &PumpIOMessageDialog::attachMedia); 0072 } 0073 0074 PumpIOMessageDialog::~PumpIOMessageDialog() 0075 { 0076 delete d; 0077 } 0078 0079 void PumpIOMessageDialog::fetchFollowing() 0080 { 0081 qCDebug(CHOQOK); 0082 toList->clear(); 0083 ccList->clear(); 0084 PumpIOMicroBlog *microblog = qobject_cast<PumpIOMicroBlog *>(d->account->microblog()); 0085 if (microblog) { 0086 microblog->fetchFollowing(d->account); 0087 connect(microblog, &PumpIOMicroBlog::followingFetched, this, 0088 &PumpIOMessageDialog::slotFetchFollowing); 0089 } 0090 } 0091 0092 void PumpIOMessageDialog::slotFetchFollowing(Choqok::Account *theAccount) 0093 { 0094 qCDebug(CHOQOK); 0095 if (theAccount == d->account) { 0096 PumpIOAccount *acc = qobject_cast<PumpIOAccount *>(theAccount); 0097 if (acc) { 0098 for (const QVariant &list: acc->lists()) { 0099 QVariantMap l = list.toMap(); 0100 QListWidgetItem *item = new QListWidgetItem; 0101 item->setText(l.value(QLatin1String("name")).toString()); 0102 item->setData(Qt::UserRole, l.value(QLatin1String("id")).toString()); 0103 toList->addItem(item); 0104 ccList->addItem(item->clone()); 0105 } 0106 toList->sortItems(); 0107 ccList->sortItems(); 0108 0109 for (const QString &username: acc->following()) { 0110 QListWidgetItem *item = new QListWidgetItem; 0111 item->setText(PumpIOMicroBlog::userNameFromAcct(username)); 0112 item->setData(Qt::UserRole, username); 0113 toList->addItem(item); 0114 ccList->addItem(item->clone()); 0115 } 0116 } 0117 } 0118 } 0119 0120 void PumpIOMessageDialog::accept() 0121 { 0122 qCDebug(CHOQOK); 0123 PumpIOAccount *acc = qobject_cast<PumpIOAccount *>(d->account); 0124 if (acc) { 0125 if (acc->following().isEmpty() || txtMessage->toPlainText().isEmpty() 0126 || (toList->selectedItems().isEmpty() && ccList->selectedItems().isEmpty())) { 0127 return; 0128 } 0129 hide(); 0130 PumpIOMicroBlog *microblog = qobject_cast<PumpIOMicroBlog *>(d->account->microblog()); 0131 if (microblog) { 0132 PumpIOPost *post = new PumpIOPost; 0133 post->content = txtMessage->toPlainText(); 0134 0135 QVariantList to; 0136 for (QListWidgetItem *item: toList->selectedItems()) { 0137 QVariantMap user; 0138 QString id = item->data(Qt::UserRole).toString(); 0139 if (id.contains(QLatin1String("acct:"))) { 0140 user.insert(QLatin1String("objectType"), QLatin1String("person")); 0141 } else { 0142 user.insert(QLatin1String("objectType"), QLatin1String("collection")); 0143 } 0144 user.insert(QLatin1String("id"), id); 0145 to.append(user); 0146 } 0147 0148 QVariantList cc; 0149 for (QListWidgetItem *item: ccList->selectedItems()) { 0150 QVariantMap user; 0151 QString id = item->data(Qt::UserRole).toString(); 0152 if (id.contains(QLatin1String("acct:"))) { 0153 user.insert(QLatin1String("objectType"), QLatin1String("person")); 0154 } else { 0155 user.insert(QLatin1String("objectType"), QLatin1String("collection")); 0156 } 0157 user.insert(QLatin1String("id"), id); 0158 cc.append(user); 0159 } 0160 0161 microblog->createPost(acc, post, to, cc); 0162 } 0163 } 0164 } 0165 0166 void PumpIOMessageDialog::attachMedia() 0167 { 0168 qCDebug(CHOQOK); 0169 d->mediumToAttach = QFileDialog::getOpenFileName(this, i18n("Select Media to Upload"), 0170 QString(), QStringLiteral("Images")); 0171 if (d->mediumToAttach.isEmpty()) { 0172 qCDebug(CHOQOK) << "No file selected"; 0173 return; 0174 } 0175 const QString fileName = QUrl(d->mediumToAttach).fileName(); 0176 if (!d->mediumName) { 0177 d->mediumName = new QLabel(this); 0178 d->btnCancel = new QPushButton(this); 0179 d->btnCancel->setIcon(QIcon::fromTheme(QLatin1String("list-remove"))); 0180 d->btnCancel->setToolTip(i18n("Discard Attachment")); 0181 d->btnCancel->setMaximumWidth(d->btnCancel->height()); 0182 connect(d->btnCancel, &QPushButton::clicked, this, &PumpIOMessageDialog::cancelAttach); 0183 0184 horizontalLayout->insertWidget(1, d->mediumName); 0185 horizontalLayout->insertWidget(2, d->btnCancel); 0186 } 0187 d->mediumName->setText(i18n("Attaching <b>%1</b>", fileName)); 0188 txtMessage->setFocus(); 0189 } 0190 0191 void PumpIOMessageDialog::cancelAttach() 0192 { 0193 qCDebug(CHOQOK); 0194 delete d->mediumName; 0195 d->mediumName = nullptr; 0196 delete d->btnCancel; 0197 d->btnCancel = nullptr; 0198 d->mediumToAttach.clear(); 0199 } 0200 0201 #include "moc_pumpiomessagedialog.cpp"