File indexing completed on 2024-04-28 04:55:36

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 "twitterapidmessagedialog.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 "twitterapiaccount.h"
0026 #include "twitterapidebug.h"
0027 #include "twitterapimicroblog.h"
0028 
0029 class TwitterApiDMessageDialog::Private
0030 {
0031 public:
0032     Private(TwitterApiAccount *theAccount)
0033         : account(theAccount)
0034     {}
0035     QComboBox *comboFriendsList;
0036     Choqok::UI::TextEdit *editor;
0037     TwitterApiAccount *account;
0038     Choqok::Post *sentPost;
0039 };
0040 
0041 TwitterApiDMessageDialog::TwitterApiDMessageDialog(TwitterApiAccount *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(), "TwitterApi");
0049     resize(grp.readEntry("DMessageDialogSize", QSize(300, 200)));
0050     QStringList list = theAccount->followersList();
0051     if (list.isEmpty()) {
0052         reloadFriendslist();
0053     } else {
0054         list.sort(Qt::CaseInsensitive);
0055         d->comboFriendsList->addItems(list);
0056     }
0057 }
0058 
0059 TwitterApiDMessageDialog::~TwitterApiDMessageDialog()
0060 {
0061     KConfigGroup grp(KSharedConfig::openConfig(), "TwitterApi");
0062     grp.writeEntry("DMessageDialogSize", size());
0063     grp.sync();
0064     delete d;
0065 }
0066 
0067 void TwitterApiDMessageDialog::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, &TwitterApiDMessageDialog::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, &TwitterApiDMessageDialog::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, &TwitterApiDMessageDialog::accept);
0098     connect(buttonBox, &QDialogButtonBox::rejected, this, &TwitterApiDMessageDialog::reject);
0099     mainLayout->addWidget(buttonBox);
0100 }
0101 
0102 void TwitterApiDMessageDialog::setFriends(const QStringList friends)
0103 {
0104     d->comboFriendsList->clear();
0105     d->comboFriendsList->addItems(friends);
0106 }
0107 
0108 Choqok::UI::TextEdit *TwitterApiDMessageDialog::editor()
0109 {
0110     return d->editor;
0111 }
0112 
0113 TwitterApiAccount *TwitterApiDMessageDialog::account()
0114 {
0115     return d->account;
0116 }
0117 
0118 void TwitterApiDMessageDialog::reloadFriendslist()
0119 {
0120     d->comboFriendsList->clear();
0121     TwitterApiMicroBlog *blog = qobject_cast<TwitterApiMicroBlog *>(d->account->microblog());
0122     if (blog) {
0123         connect(blog, &TwitterApiMicroBlog::followersUsernameListed, this,
0124                 &TwitterApiDMessageDialog::followersUsernameListed);
0125         blog->listFollowersUsername(d->account);
0126         d->comboFriendsList->setCurrentText(i18n("Please wait..."));
0127     }
0128 }
0129 
0130 void TwitterApiDMessageDialog::accept()
0131 {
0132     submitPost(d->editor->toPlainText());
0133 }
0134 
0135 void TwitterApiDMessageDialog::submitPost(QString text)
0136 {
0137     if (d->account->friendsList().isEmpty() || text.isEmpty() || d->comboFriendsList->currentText().isEmpty()) {
0138         return;
0139     }
0140     hide();
0141     connect(d->account->microblog(), &Choqok::MicroBlog::errorPost, this,
0142             &TwitterApiDMessageDialog::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 TwitterApiDMessageDialog::followersUsernameListed(TwitterApiAccount *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 TwitterApiDMessageDialog::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 TwitterApiDMessageDialog::setTo(const QString &username)
0172 {
0173     d->comboFriendsList->setCurrentText(username);
0174 }
0175 
0176 #include "moc_twitterapidmessagedialog.cpp"