File indexing completed on 2024-04-28 04:55:41
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #include "quickpost.h" 0010 0011 #include <QCheckBox> 0012 #include <QComboBox> 0013 #include <QHBoxLayout> 0014 #include <QPointer> 0015 #include <QPushButton> 0016 0017 #include <KLocalizedString> 0018 #include <KMessageBox> 0019 0020 #include "accountmanager.h" 0021 #include "choqokbehaviorsettings.h" 0022 #include "choqoktextedit.h" 0023 #include "libchoqokdebug.h" 0024 #include "microblog.h" 0025 #include "notifymanager.h" 0026 #include "shortenmanager.h" 0027 #include "uploadmediadialog.h" 0028 0029 using namespace Choqok::UI; 0030 using namespace Choqok; 0031 0032 class QuickPost::Private 0033 { 0034 public: 0035 Private() 0036 : submittedPost(nullptr), isPostSubmitted(false) 0037 {} 0038 QCheckBox *all; 0039 QComboBox *comboAccounts; 0040 TextEdit *txtPost; 0041 0042 QHash< QString, Account * > accountsList; 0043 Post *submittedPost; 0044 QList<Account *> submittedAccounts; 0045 bool isPostSubmitted; 0046 QPushButton *attach; 0047 // QString replyToId; 0048 }; 0049 0050 QuickPost::QuickPost(QWidget *parent) 0051 : QDialog(parent), d(new Private) 0052 { 0053 qCDebug(CHOQOK); 0054 setupUi(); 0055 loadAccounts(); 0056 connect(d->comboAccounts, (void (QComboBox::*)(int))&QComboBox::currentIndexChanged, 0057 this, &QuickPost::slotCurrentAccountChanged); 0058 connect(d->txtPost, &TextEdit::returnPressed, this, &QuickPost::submitPost); 0059 connect(d->all, &QCheckBox::toggled, this, &QuickPost::checkAll); 0060 connect(AccountManager::self(), &AccountManager::accountAdded, this, &QuickPost::addAccount); 0061 connect(AccountManager::self(), &AccountManager::accountRemoved, this, &QuickPost::removeAccount); 0062 connect(d->attach, &QPushButton::clicked, this, &QuickPost::slotAttachMedium); 0063 0064 d->all->setChecked(Choqok::BehaviorSettings::all()); 0065 slotCurrentAccountChanged(d->comboAccounts->currentIndex()); 0066 } 0067 0068 void QuickPost::setupUi() 0069 { 0070 resize(Choqok::BehaviorSettings::quickPostDialogSize()); 0071 d->all = new QCheckBox(i18nc("All accounts", "All"), this); 0072 d->comboAccounts = new QComboBox(this); 0073 d->attach = new QPushButton(QIcon::fromTheme(QLatin1String("mail-attachment")), QString(), this); 0074 d->attach->setMaximumWidth(d->attach->height()); 0075 d->attach->setToolTip(i18n("Attach a file")); 0076 QVBoxLayout *mainLayout = new QVBoxLayout(this); 0077 QHBoxLayout *hLayout = new QHBoxLayout; 0078 hLayout->addWidget(d->all); 0079 hLayout->addWidget(d->comboAccounts); 0080 hLayout->addWidget(d->attach); 0081 mainLayout->addLayout(hLayout); 0082 d->txtPost = new TextEdit(0, this); 0083 d->txtPost->setTabChangesFocus(true); 0084 mainLayout->addWidget(d->txtPost); 0085 0086 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0087 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0088 okButton->setDefault(true); 0089 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0090 okButton->setText(i18nc("Submit post", "Submit")); 0091 connect(buttonBox, &QDialogButtonBox::accepted, this, &QuickPost::accept); 0092 connect(buttonBox, &QDialogButtonBox::rejected, this, &QuickPost::reject); 0093 mainLayout->addWidget(buttonBox); 0094 0095 setLayout(mainLayout); 0096 0097 d->txtPost->setFocus(Qt::OtherFocusReason); 0098 setWindowTitle(i18n("Quick Post")); 0099 } 0100 0101 QuickPost::~QuickPost() 0102 { 0103 BehaviorSettings::setAll(d->all->isChecked()); 0104 BehaviorSettings::setQuickPostDialogSize(this->size()); 0105 BehaviorSettings::self()->save(); 0106 delete d; 0107 qCDebug(CHOQOK); 0108 } 0109 0110 void QuickPost::show() 0111 { 0112 d->txtPost->setFocus(Qt::OtherFocusReason); 0113 QDialog::show(); 0114 } 0115 0116 void QuickPost::slotSubmitPost(Account *a, Post *post) 0117 { 0118 if (post == d->submittedPost && d->submittedAccounts.removeOne(a)) { 0119 Q_EMIT newPostSubmitted(Success, d->submittedPost); 0120 } 0121 if (d->isPostSubmitted && d->submittedAccounts.isEmpty()) { 0122 d->txtPost->setEnabled(true); 0123 d->txtPost->clear(); 0124 delete d->submittedPost; 0125 d->submittedPost = nullptr; 0126 d->isPostSubmitted = false; 0127 } 0128 } 0129 0130 void QuickPost::postError(Account *a, Choqok::Post *post, 0131 Choqok::MicroBlog::ErrorType , const QString &) 0132 { 0133 if (post == d->submittedPost && d->submittedAccounts.contains(a)) { 0134 d->txtPost->setEnabled(true); 0135 Q_EMIT newPostSubmitted(Fail, post); //Choqok crashes without post :( 0136 show(); 0137 } 0138 if (d->submittedAccounts.isEmpty()) { 0139 d->txtPost->setEnabled(true); 0140 delete d->submittedPost; 0141 d->submittedPost = nullptr; 0142 } 0143 } 0144 0145 void QuickPost::submitPost(const QString &txt) 0146 { 0147 qCDebug(CHOQOK); 0148 if (txt.isEmpty()) { 0149 KMessageBox::error(choqokMainWindow, i18n("Cannot create a post without any text.")); 0150 return; 0151 } 0152 Choqok::Account *currentAccount = d->accountsList.value(d->comboAccounts->currentText()); 0153 if (!currentAccount) { 0154 KMessageBox::error(choqokMainWindow, i18n("Please configure at least one account to be included in \"Quick Post\".\nSettings -> Configure Choqok... -> Accounts")); 0155 return; 0156 } 0157 this->hide(); 0158 d->submittedAccounts.clear(); 0159 QString newPost = txt; 0160 if (currentAccount->postCharLimit() && 0161 txt.size() > (int)currentAccount->postCharLimit()) { 0162 newPost = Choqok::ShortenManager::self()->parseText(txt); 0163 } 0164 delete d->submittedPost; 0165 if (d->all->isChecked()) { 0166 d->submittedPost = new Post; 0167 d->submittedPost->content = newPost; 0168 d->submittedPost->isPrivate = false; 0169 for (Account *acc: d->accountsList) { 0170 acc->microblog()->createPost(acc, d->submittedPost); 0171 d->submittedAccounts << acc; 0172 } 0173 } else { 0174 d->submittedPost = new Post; 0175 d->submittedPost->content = newPost; 0176 d->submittedPost->isPrivate = false; 0177 d->submittedAccounts << currentAccount; 0178 currentAccount->microblog()->createPost(d->accountsList.value(d->comboAccounts->currentText()), 0179 d->submittedPost); 0180 } 0181 d->isPostSubmitted = true; 0182 } 0183 0184 void QuickPost::accept() 0185 { 0186 qCDebug(CHOQOK); 0187 submitPost(d->txtPost->toPlainText()); 0188 } 0189 0190 void QuickPost::loadAccounts() 0191 { 0192 qCDebug(CHOQOK); 0193 for (Choqok::Account *ac: AccountManager::self()->accounts()) { 0194 addAccount(ac); 0195 } 0196 } 0197 0198 void QuickPost::addAccount(Choqok::Account *account) 0199 { 0200 qCDebug(CHOQOK); 0201 connect(account, &Account::modified, this, &QuickPost::accountModified); //Added for later changes 0202 if (!account->isEnabled() || account->isReadOnly() || !account->showInQuickPost()) { 0203 return; 0204 } 0205 d->accountsList.insert(account->alias(), account); 0206 d->comboAccounts->addItem(QIcon::fromTheme(account->microblog()->pluginIcon()), account->alias()); 0207 connect(account->microblog(), &MicroBlog::postCreated, this, &QuickPost::slotSubmitPost); 0208 connect(account->microblog(), &MicroBlog::errorPost, this, &QuickPost::postError); 0209 } 0210 0211 void QuickPost::removeAccount(const QString &alias) 0212 { 0213 qCDebug(CHOQOK); 0214 d->accountsList.remove(alias); 0215 d->comboAccounts->removeItem(d->comboAccounts->findText(alias)); 0216 } 0217 0218 void QuickPost::checkAll(bool isAll) 0219 { 0220 d->comboAccounts->setEnabled(!isAll); 0221 } 0222 0223 void QuickPost::setText(const QString &text) 0224 { 0225 d->txtPost->setPlainText(text); 0226 this->show(); 0227 // if(account) 0228 // d->comboAccounts->setCurrentItem(account->alias()); 0229 // if(!replyToId.isEmpty()) 0230 // d->replyToId = replyToId; 0231 } 0232 0233 void QuickPost::appendText(const QString &text) 0234 { 0235 d->txtPost->appendText(text); 0236 this->show(); 0237 } 0238 0239 void QuickPost::slotCurrentAccountChanged(int index) 0240 { 0241 Q_UNUSED(index) 0242 if (!d->accountsList.isEmpty()) { 0243 d->txtPost->setCharLimit(d->accountsList.value(d->comboAccounts->currentText())->postCharLimit()); 0244 } 0245 } 0246 0247 void QuickPost::accountModified(Account *theAccount) 0248 { 0249 qCDebug(CHOQOK); 0250 if (theAccount->isEnabled() && !theAccount->isReadOnly() && theAccount->showInQuickPost()) { 0251 if (!d->accountsList.contains(theAccount->alias())) { 0252 addAccount(theAccount); 0253 } 0254 } else if (d->accountsList.contains(theAccount->alias())) { 0255 removeAccount(theAccount->alias()); 0256 } 0257 } 0258 0259 void QuickPost::slotAttachMedium() 0260 { 0261 KMessageBox::information(this, i18n("Link to uploaded medium will be added here after uploading process succeed."), 0262 QString(), QLatin1String("quickPostAttachMedium")); 0263 QPointer<UploadMediaDialog> dlg = new UploadMediaDialog(this); 0264 dlg->show(); 0265 } 0266 0267 #include "moc_quickpost.cpp"