1 | /* |
2 | Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "transportcombobox.h" |
21 | #include "transport.h" |
22 | #include "transportmanager.h" |
23 | |
24 | #include <KDebug> |
25 | |
26 | using namespace MailTransport; |
27 | |
28 | /** |
29 | * Private class that helps to provide binary compatibility between releases. |
30 | * @internal |
31 | */ |
32 | class TransportComboBoxPrivate |
33 | { |
34 | public: |
35 | QList<int> transports; |
36 | }; |
37 | |
38 | TransportComboBox::TransportComboBox( QWidget *parent ) |
39 | : KComboBox( parent ), d( new TransportComboBoxPrivate ) |
40 | { |
41 | QMetaObject::invokeMethod(this, "updateComboboxList" ); |
42 | connect( TransportManager::self(), SIGNAL(transportsChanged()), |
43 | SLOT(updateComboboxList()) ); |
44 | } |
45 | |
46 | TransportComboBox::~TransportComboBox() |
47 | { |
48 | delete d; |
49 | } |
50 | |
51 | int TransportComboBox::currentTransportId() const |
52 | { |
53 | if ( currentIndex() >= 0 && currentIndex() < d->transports.count() ) { |
54 | return d->transports.at( currentIndex() ); |
55 | } |
56 | return -1; |
57 | } |
58 | |
59 | void TransportComboBox::setCurrentTransport( int transportId ) |
60 | { |
61 | const int i = d->transports.indexOf( transportId ); |
62 | if ( i >= 0 && i < count() ) { |
63 | setCurrentIndex( i ); |
64 | } |
65 | } |
66 | |
67 | TransportBase::EnumType::type TransportComboBox::transportType() const |
68 | { |
69 | int transtype = TransportManager::self()->transportById( currentTransportId() )->type(); |
70 | return static_cast<TransportBase::EnumType::type>( transtype ); |
71 | } |
72 | |
73 | void TransportComboBox::updateComboboxList() |
74 | { |
75 | fillComboBox(); |
76 | } |
77 | |
78 | void TransportComboBox::fillComboBox() |
79 | { |
80 | const int oldTransport = currentTransportId(); |
81 | clear(); |
82 | |
83 | int defaultId = 0; |
84 | if ( !TransportManager::self()->isEmpty() ) { |
85 | const QStringList listNames = TransportManager::self()->transportNames(); |
86 | const QList<int> listIds = TransportManager::self()->transportIds(); |
87 | addItems( listNames ); |
88 | setTransportList(listIds); |
89 | defaultId = TransportManager::self()->defaultTransportId(); |
90 | } |
91 | |
92 | if ( oldTransport != -1 ) { |
93 | setCurrentTransport( oldTransport ); |
94 | } else { |
95 | setCurrentTransport( defaultId ); |
96 | } |
97 | } |
98 | |
99 | void TransportComboBox::setTransportList(const QList<int> &transportList) |
100 | { |
101 | d->transports = transportList; |
102 | } |
103 | |