File indexing completed on 2024-09-22 04:47:57

0001 /*
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "transportcombobox.h"
0008 #include "transportmanager.h"
0009 
0010 using namespace MailTransport;
0011 
0012 /**
0013  * Private class that helps to provide binary compatibility between releases.
0014  * @internal
0015  */
0016 class TransportComboBoxPrivate
0017 {
0018 public:
0019     QList<int> transports;
0020 };
0021 
0022 TransportComboBox::TransportComboBox(QWidget *parent)
0023     : QComboBox(parent)
0024     , d(new TransportComboBoxPrivate)
0025 {
0026     updateComboboxList();
0027     connect(TransportManager::self(), &TransportManager::transportsChanged, this, &TransportComboBox::updateComboboxList);
0028     connect(TransportManager::self(), &TransportManager::transportRemoved, this, &TransportComboBox::transportRemoved);
0029 }
0030 
0031 TransportComboBox::~TransportComboBox() = default;
0032 
0033 int TransportComboBox::currentTransportId() const
0034 {
0035     if (currentIndex() >= 0 && currentIndex() < d->transports.count()) {
0036         return d->transports.at(currentIndex());
0037     }
0038     return -1;
0039 }
0040 
0041 bool TransportComboBox::setCurrentTransport(int transportId)
0042 {
0043     const int i = d->transports.indexOf(transportId);
0044     if (i >= 0 && i < count()) {
0045         setCurrentIndex(i);
0046         return true;
0047     }
0048     return false;
0049 }
0050 
0051 QString TransportComboBox::transportType() const
0052 {
0053     return TransportManager::self()->transportById(currentTransportId())->identifier();
0054 }
0055 
0056 void TransportComboBox::updateComboboxList()
0057 {
0058     const int oldTransport = currentTransportId();
0059     clear();
0060 
0061     int defaultId = 0;
0062     if (!TransportManager::self()->isEmpty()) {
0063         const QStringList listNames = TransportManager::self()->transportNames();
0064         const QList<int> listIds = TransportManager::self()->transportIds();
0065         addItems(listNames);
0066         setTransportList(listIds);
0067         defaultId = TransportManager::self()->defaultTransportId();
0068     }
0069 
0070     if (oldTransport != -1) {
0071         setCurrentTransport(oldTransport);
0072     } else {
0073         setCurrentTransport(defaultId);
0074     }
0075 }
0076 
0077 void TransportComboBox::setTransportList(const QList<int> &transportList)
0078 {
0079     d->transports = transportList;
0080 }
0081 
0082 #include "moc_transportcombobox.cpp"