File indexing completed on 2024-04-28 04:55:44
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2017 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 "mastodondmessagedialog.h" 0010 0011 #include <QComboBox> 0012 #include <QDialogButtonBox> 0013 #include <QLabel> 0014 #include <QHBoxLayout> 0015 #include <QPushButton> 0016 #include <QVBoxLayout> 0017 0018 #include <KLocalizedString> 0019 #include <KSharedConfig> 0020 0021 #include "choqoktextedit.h" 0022 #include "microblog.h" 0023 #include "notifymanager.h" 0024 0025 #include "mastodonaccount.h" 0026 #include "mastodondebug.h" 0027 #include "mastodonmicroblog.h" 0028 0029 class MastodonDMessageDialog::Private 0030 { 0031 public: 0032 Private(MastodonAccount *theAccount) 0033 : account(theAccount) 0034 {} 0035 QComboBox *comboFriendsList; 0036 Choqok::UI::TextEdit *editor; 0037 MastodonAccount *account; 0038 Choqok::Post *sentPost; 0039 }; 0040 0041 MastodonDMessageDialog::MastodonDMessageDialog(MastodonAccount *theAccount, QWidget *parent, 0042 Qt::WindowFlags flags) 0043 : QDialog(parent, flags), d(new Private(theAccount)) 0044 { 0045 setWindowTitle(i18n("Send Private Message")); 0046 setAttribute(Qt::WA_DeleteOnClose); 0047 setupUi(this); 0048 KConfigGroup grp(KSharedConfig::openConfig(), "Mastodon"); 0049 resize(grp.readEntry("DMessageDialogSize", QSize(300, 200))); 0050 QStringList list = theAccount->followers(); 0051 if (list.isEmpty()) { 0052 reloadFriendslist(); 0053 } else { 0054 list.sort(Qt::CaseInsensitive); 0055 d->comboFriendsList->addItems(list); 0056 } 0057 } 0058 0059 MastodonDMessageDialog::~MastodonDMessageDialog() 0060 { 0061 KConfigGroup grp(KSharedConfig::openConfig(), "Mastodon"); 0062 grp.writeEntry("DMessageDialogSize", size()); 0063 grp.sync(); 0064 delete d; 0065 } 0066 0067 void MastodonDMessageDialog::setupUi(QWidget *mainWidget) 0068 { 0069 QLabel *lblTo = new QLabel(i18nc("Send message to", "To:"), this); 0070 d->comboFriendsList = new QComboBox(this); 0071 d->comboFriendsList->setDuplicatesEnabled(false); 0072 0073 QPushButton *btnReload = new QPushButton(this); 0074 btnReload->setToolTip(i18n("Reload friends list")); 0075 btnReload->setIcon(QIcon::fromTheme(QLatin1String("view-refresh"))); 0076 btnReload->setMaximumWidth(25); 0077 connect(btnReload, &QPushButton::clicked, this, &MastodonDMessageDialog::reloadFriendslist); 0078 0079 QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget); 0080 0081 QHBoxLayout *toLayout = new QHBoxLayout; 0082 toLayout->addWidget(lblTo); 0083 toLayout->addWidget(d->comboFriendsList); 0084 toLayout->addWidget(btnReload); 0085 mainLayout->addLayout(toLayout); 0086 0087 d->editor = new Choqok::UI::TextEdit(d->account->postCharLimit()); 0088 connect(d->editor, &Choqok::UI::TextEdit::returnPressed, this, &MastodonDMessageDialog::submitPost); 0089 mainLayout->addWidget(d->editor); 0090 d->editor->setFocus(); 0091 0092 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0093 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); 0094 okButton->setDefault(true); 0095 okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0096 okButton->setText(i18nc("Send private message", "Send")); 0097 connect(buttonBox, &QDialogButtonBox::accepted, this, &MastodonDMessageDialog::accept); 0098 connect(buttonBox, &QDialogButtonBox::rejected, this, &MastodonDMessageDialog::reject); 0099 mainLayout->addWidget(buttonBox); 0100 } 0101 0102 void MastodonDMessageDialog::setFriends(const QStringList friends) 0103 { 0104 d->comboFriendsList->clear(); 0105 d->comboFriendsList->addItems(friends); 0106 } 0107 0108 Choqok::UI::TextEdit *MastodonDMessageDialog::editor() 0109 { 0110 return d->editor; 0111 } 0112 0113 MastodonAccount *MastodonDMessageDialog::account() 0114 { 0115 return d->account; 0116 } 0117 0118 void MastodonDMessageDialog::reloadFriendslist() 0119 { 0120 d->comboFriendsList->clear(); 0121 MastodonMicroBlog *blog = qobject_cast<MastodonMicroBlog *>(d->account->microblog()); 0122 if (blog) { 0123 connect(blog, &MastodonMicroBlog::followersUsernameListed, 0124 this, &MastodonDMessageDialog::followersUsernameListed); 0125 blog->fetchFollowers(d->account, true); 0126 d->comboFriendsList->setCurrentText(i18n("Please wait...")); 0127 } 0128 } 0129 0130 void MastodonDMessageDialog::accept() 0131 { 0132 submitPost(d->editor->toPlainText()); 0133 } 0134 0135 void MastodonDMessageDialog::submitPost(QString text) 0136 { 0137 if (d->account->following().isEmpty() || text.isEmpty() || d->comboFriendsList->currentText().isEmpty()) { 0138 return; 0139 } 0140 hide(); 0141 connect(d->account->microblog(), &Choqok::MicroBlog::errorPost, 0142 this, &MastodonDMessageDialog::errorPost); 0143 connect(d->account->microblog(), SIGNAL(postCreated(Choqok::Account*,Choqok::Post*)), 0144 this, SLOT(postCreated(Choqok::Account*,Choqok::Post*))); 0145 d->sentPost = new Choqok::Post; 0146 d->sentPost->isPrivate = true; 0147 d->sentPost->replyToUser.userName = d->comboFriendsList->currentText(); 0148 d->sentPost->content = text; 0149 d->account->microblog()->createPost(d->account, d->sentPost); 0150 } 0151 0152 void MastodonDMessageDialog::followersUsernameListed(MastodonAccount *theAccount, QStringList list) 0153 { 0154 if (theAccount == d->account) { 0155 d->comboFriendsList->clear(); 0156 list.sort(Qt::CaseInsensitive); 0157 d->comboFriendsList->addItems(list); 0158 } 0159 } 0160 0161 void MastodonDMessageDialog::errorPost(Choqok::Account *theAccount, Choqok::Post *thePost, 0162 Choqok::MicroBlog::ErrorType , QString , 0163 Choqok::MicroBlog::ErrorLevel) 0164 { 0165 if (theAccount == d->account && thePost == d->sentPost) { 0166 qCDebug(CHOQOK); 0167 show(); 0168 } 0169 } 0170 0171 void MastodonDMessageDialog::setTo(const QString &username) 0172 { 0173 d->comboFriendsList->setCurrentText(username); 0174 } 0175 0176 #include "moc_mastodondmessagedialog.cpp"