1 | /* |
2 | Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com> |
3 | |
4 | Based on KMail code by: |
5 | Copyright (c) 2002 Marc Mutz <mutz@kde.org> |
6 | Copyright (c) 2007 Mathias Soeken <msoeken@tzi.de> |
7 | |
8 | This library is free software; you can redistribute it and/or modify it |
9 | under the terms of the GNU Library General Public License as published by |
10 | the Free Software Foundation; either version 2 of the License, or (at your |
11 | option) any later version. |
12 | |
13 | This library is distributed in the hope that it will be useful, but WITHOUT |
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
16 | License for more details. |
17 | |
18 | You should have received a copy of the GNU Library General Public License |
19 | along with this library; see the file COPYING.LIB. If not, write to the |
20 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
21 | 02110-1301, USA. |
22 | */ |
23 | |
24 | #include "transportlistview.h" |
25 | #include "transport.h" |
26 | #include "transportmanager.h" |
27 | #include "transporttype.h" |
28 | |
29 | #include <QHeaderView> |
30 | #include <QLineEdit> |
31 | |
32 | #include <KDebug> |
33 | #include <KLocalizedString> |
34 | |
35 | using namespace MailTransport; |
36 | |
37 | TransportListView::TransportListView( QWidget *parent ) |
38 | : QTreeWidget( parent ) |
39 | { |
40 | setHeaderLabels( QStringList() |
41 | << i18nc( "@title:column email transport name" , "Name" ) |
42 | << i18nc( "@title:column email transport type" , "Type" ) ); |
43 | setRootIsDecorated( false ); |
44 | header()->setMovable( false ); |
45 | header()->setResizeMode( QHeaderView::ResizeToContents ); |
46 | setAllColumnsShowFocus( true ); |
47 | setAlternatingRowColors( true ); |
48 | setSortingEnabled( true ); |
49 | sortByColumn( 0, Qt::AscendingOrder ); |
50 | setSelectionMode( SingleSelection ); |
51 | |
52 | fillTransportList(); |
53 | connect( TransportManager::self(), SIGNAL(transportsChanged()), |
54 | this, SLOT(fillTransportList()) ); |
55 | } |
56 | |
57 | void TransportListView::editItem( QTreeWidgetItem *item, int column ) |
58 | { |
59 | // TODO: is there a nicer way to make only the 'name' column editable? |
60 | if ( column == 0 && item ) { |
61 | Qt::ItemFlags oldFlags = item->flags(); |
62 | item->setFlags( oldFlags | Qt::ItemIsEditable ); |
63 | QTreeWidget::editItem( item, 0 ); |
64 | item->setFlags( oldFlags ); |
65 | const int id = item->data( 0, Qt::UserRole ).toInt(); |
66 | Transport *t = TransportManager::self()->transportById( id ); |
67 | if ( !t ) { |
68 | kWarning() << "Transport" << id << "not known by manager." ; |
69 | return; |
70 | } |
71 | if ( TransportManager::self()->defaultTransportId() == t->id() ) { |
72 | item->setText( 0, t->name() ); |
73 | } |
74 | } |
75 | } |
76 | |
77 | void TransportListView::commitData( QWidget *editor ) |
78 | { |
79 | if ( selectedItems().isEmpty() ) { |
80 | // transport was deleted by someone else??? |
81 | kDebug() << "No selected item." ; |
82 | return; |
83 | } |
84 | QTreeWidgetItem *item = selectedItems().first(); |
85 | QLineEdit *edit = dynamic_cast<QLineEdit*>( editor ); // krazy:exclude=qclasses |
86 | Q_ASSERT( edit ); // original code had if |
87 | |
88 | const int id = item->data( 0, Qt::UserRole ).toInt(); |
89 | Transport *t = TransportManager::self()->transportById( id ); |
90 | if ( !t ) { |
91 | kWarning() << "Transport" << id << "not known by manager." ; |
92 | return; |
93 | } |
94 | kDebug() << "Renaming transport" << id << "to" << edit->text(); |
95 | t->setName( edit->text() ); |
96 | t->forceUniqueName(); |
97 | t->writeConfig(); |
98 | } |
99 | |
100 | void TransportListView::fillTransportList() |
101 | { |
102 | // try to preserve the selection |
103 | int selected = -1; |
104 | if ( currentItem() ) { |
105 | selected = currentItem()->data( 0, Qt::UserRole ).toInt(); |
106 | } |
107 | |
108 | clear(); |
109 | foreach ( Transport *t, TransportManager::self()->transports() ) { |
110 | QTreeWidgetItem *item = new QTreeWidgetItem( this ); |
111 | item->setData( 0, Qt::UserRole, t->id() ); |
112 | QString name = t->name(); |
113 | if ( TransportManager::self()->defaultTransportId() == t->id() ) { |
114 | name += i18nc( "@label the default mail transport" , " (Default)" ); |
115 | QFont font( item->font(0) ); |
116 | font.setBold( true ); |
117 | item->setFont( 0, font ); |
118 | } |
119 | item->setText( 0, name ); |
120 | item->setText( 1, t->transportType().name() ); |
121 | if ( t->id() == selected ) { |
122 | setCurrentItem( item ); |
123 | } |
124 | } |
125 | } |
126 | |