File indexing completed on 2023-09-24 08:19:52

0001 /* ============================================================
0002  *
0003  * This file is a part of KDE project
0004  *
0005  *
0006  * Date        : 2015-16-05
0007  * Description : a kipi plugin to export images to Flickr web service
0008  *
0009  * Copyright (C) 2015 by Shourya Singh Gupta <shouryasgupta at gmail dot com>
0010  *
0011  * This program is free software; you can redistribute it
0012  * and/or modify it under the terms of the GNU General
0013  * Public License as published by the Free Software Foundation;
0014  * either version 2, or (at your option) any later version.
0015  *
0016  * This program is distributed in the hope that it will be useful,
0017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0019  * GNU General Public License for more details.
0020  *
0021  * ============================================================ */
0022 
0023 #include "selectuserdlg.h"
0024 
0025 // Qt includes
0026 
0027 #include <QPushButton>
0028 #include <QLabel>
0029 #include <QVBoxLayout>
0030 #include <QDialogButtonBox>
0031 #include <QIcon>
0032 
0033 // KDE includes
0034 
0035 #include <klocalizedstring.h>
0036 #include <kconfig.h>
0037 #include <kconfiggroup.h>
0038 
0039 namespace KIPIFlickrPlugin
0040 {
0041 
0042 SelectUserDlg::SelectUserDlg(QWidget* const parent, const QString& serviceName)
0043     : QDialog(parent)
0044 {
0045     m_serviceName = serviceName;
0046 
0047     setWindowTitle(i18n("Flickr Account Selector"));
0048     setModal(true);
0049 
0050     QDialogButtonBox* const buttonBox   = new QDialogButtonBox();
0051     QPushButton* const buttonNewAccount = new QPushButton(buttonBox);
0052     buttonNewAccount->setText(i18n("Add another account"));
0053     buttonNewAccount->setIcon(QIcon::fromTheme(QString::fromLatin1("network-workgroup")));
0054 
0055     buttonBox->addButton(buttonNewAccount, QDialogButtonBox::AcceptRole);
0056     buttonBox->addButton(QDialogButtonBox::Ok);
0057     buttonBox->addButton(QDialogButtonBox::Close);
0058 
0059     buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
0060 
0061     m_okButton = buttonBox->button(QDialogButtonBox::Ok);
0062 
0063     if (m_serviceName == QString::fromLatin1("23"))
0064     {
0065         setWindowIcon(QIcon::fromTheme(QString::fromLatin1("kipi-hq")));
0066     }
0067     else
0068     {
0069         setWindowIcon(QIcon::fromTheme(QString::fromLatin1("kipi-flickr")));
0070     }
0071 
0072     m_uname = QString();
0073 
0074     m_label = new QLabel(this);
0075     m_label->setText(i18n("Choose the %1 account to use for exporting images:", m_serviceName));
0076 
0077     m_userComboBox = new QComboBox(this);
0078 
0079     QVBoxLayout* const mainLayout = new QVBoxLayout(this);
0080     mainLayout->addWidget(m_label);
0081     mainLayout->addWidget(m_userComboBox);
0082     mainLayout->addWidget(buttonBox);
0083     setLayout(mainLayout);
0084 
0085     connect(buttonBox, SIGNAL(accepted()),
0086             this, SLOT(accept()));
0087 
0088     connect(buttonBox, SIGNAL(rejected()),
0089             this, SLOT(reject()));
0090 
0091     connect(buttonBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
0092             this, SLOT(slotOkClicked()));
0093 
0094     connect(buttonNewAccount, SIGNAL(clicked()),
0095             this, SLOT(slotNewAccountClicked()));
0096 }
0097 
0098 SelectUserDlg::~SelectUserDlg()
0099 {
0100     delete m_userComboBox;
0101     delete m_label;
0102 }
0103 
0104 void SelectUserDlg::reactivate()
0105 {
0106     KConfig config(QString::fromLatin1("kipirc"));
0107 
0108     m_userComboBox->clear();
0109 
0110     foreach(const QString& group, config.groupList())
0111     {
0112         if (!(group.contains(m_serviceName)))
0113             continue;
0114 
0115         KConfigGroup grp = config.group(group);
0116 
0117         if (QString::compare(grp.readEntry(QString::fromLatin1("username")), QString(), Qt::CaseInsensitive) == 0)
0118             continue;
0119 
0120         m_userComboBox->addItem(grp.readEntry(QString::fromLatin1("username")));
0121     }
0122 
0123     m_okButton->setEnabled(m_userComboBox->count() > 0);
0124 
0125     exec();
0126 }
0127 
0128 void SelectUserDlg::slotOkClicked()
0129 {
0130     m_uname = m_userComboBox->currentText();
0131 }
0132 
0133 void SelectUserDlg::slotNewAccountClicked()
0134 {
0135     m_uname = QString();
0136 }
0137 
0138 QString SelectUserDlg::getUname() const
0139 {
0140     return m_uname;
0141 }
0142 
0143 SelectUserDlg* SelectUserDlg::getDlg()
0144 {
0145     return this;
0146 }
0147 
0148 } // namespace KIPIFlickrPlugin
0149 
0150 #include "moc_selectuserdlg.cpp"