4 KMail, the KDE mail client.
5 Copyright (c) 2003 Ingo Kloecker <kloecker@kde.org>
6 Copyright (c) 2007 Thomas McGuire <Thomas.McGuire@gmx.net>
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License,
10 version 2.0, as published by the Free Software Foundation.
11 You should have received a copy of the GNU General Public License
12 along with this program; if not, write to the Free Software Foundation,
13 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
16 #include "attachmentlistview.h"
19 #include <QHeaderView>
20 #include <QContextMenuEvent>
24 #include <maillistdrag.h>
27 #include "kmmsgdict.h"
28 #include "kmcommands.h"
32 static const int encryptColumn
= 5;
36 AttachmentListView::AttachmentListView( KMail::Composer
* composer
,
38 : QTreeWidget( parent
),
41 setDragDropMode( QAbstractItemView::DragDrop
);
42 setObjectName( "attachment list view" );
43 setSelectionMode( QAbstractItemView::ExtendedSelection
);
45 setAllColumnsShowFocus( true );
46 QStringList headerNames
;
47 headerNames
<< i18nc("@title collumn attachment name.", "Name")
48 << i18nc("@title collumn attachment size.", "Size")
49 << i18nc("@title collumn attachment encoding.", "Encoding")
50 << i18nc("@title collumn attachment type.", "Type")
51 << i18nc("@title collumn attachment compression checkbox.", "Compress")
52 << i18nc("@title collumn attachment encryption checkbox.", "Encrypt")
53 << i18nc("@title collumn attachment signed checkbox.", "Sign");
54 setHeaderLabels( headerNames
);
56 header()->setResizeMode( QHeaderView::Interactive
);
57 header()->setResizeMode( 3, QHeaderView::Stretch
);
58 header()->setStretchLastSection( false );
59 setColumnWidth( 0, 200);
60 setColumnWidth( 1, 80 );
61 setColumnWidth( 2, 120 );
62 setColumnWidth( 3, 120 );
63 setColumnWidth( 4, 100 );
64 setColumnWidth( 5, 80 );
65 setColumnWidth( 6, 80 );
67 // We can not enable automatic sorting, because that would resort the list
68 // when the list is sorted by size and the user clicks the compress checkbox.
69 header()->setSortIndicatorShown( true );
70 header()->setClickable( true );
71 connect( header(), SIGNAL(sectionClicked(int)), this, SLOT(slotSort()) );
73 enableCryptoCBs( false );
75 // This is needed because QTreeWidget forgets to update when a column is
76 // moved. It updates correctly when a column is resized, so trick it into
77 // thinking a column was resized when it is moved.
78 connect ( header(), SIGNAL(sectionMoved(int,int,int)),
79 header(), SIGNAL(sectionResized(int,int,int)) );
82 //-----------------------------------------------------------------------------
84 AttachmentListView::~AttachmentListView()
88 //-----------------------------------------------------------------------------
90 void AttachmentListView::enableCryptoCBs( bool enable
)
92 bool enabledBefore
= areCryptoCBsEnabled();
94 // Show the crypto columns if they were hidden before
95 if ( enable
&& !enabledBefore
) {
97 // determine the total width of the columns
99 for ( int col
= 0; col
< encryptColumn
; col
++ )
100 totalWidth
+= header()->sectionSize( col
);
102 int reducedTotalWidth
= totalWidth
- mEncryptColWidth
- mSignColWidth
;
104 // reduce the width of all columns so that the encrypt and sign columns fit
106 for ( int col
= 0; col
< encryptColumn
- 1; col
++ ) {
107 int newWidth
= header()->sectionSize( col
) * reducedTotalWidth
/
109 setColumnWidth( col
, newWidth
);
110 usedWidth
+= newWidth
;
113 // the last column before the encrypt column gets the remaining space
114 // (because of rounding errors the width of this column isn't calculated
115 // the same way as the width of the other columns)
116 setColumnWidth( encryptColumn
- 1, reducedTotalWidth
- usedWidth
);
117 setColumnWidth( encryptColumn
, mEncryptColWidth
);
118 setColumnWidth( encryptColumn
+ 1, mSignColWidth
);
121 // Hide the crypto columns if they were shown before
122 if ( !enable
&& enabledBefore
) {
124 mEncryptColWidth
= columnWidth( encryptColumn
);
125 mSignColWidth
= columnWidth( encryptColumn
+ 1 );
127 // determine the total width of the columns
129 for( int col
= 0; col
< header()->count(); col
++ )
130 totalWidth
+= header()->sectionSize( col
);
132 int reducedTotalWidth
= totalWidth
- mEncryptColWidth
- mSignColWidth
;
134 // increase the width of all columns so that the visible columns take
135 // up the whole space
137 for( int col
= 0; col
< encryptColumn
- 1; col
++ ) {
138 int newWidth
= header()->sectionSize( col
) * totalWidth
/
140 setColumnWidth( col
, newWidth
);
141 usedWidth
+= newWidth
;
144 // the last column before the encrypt column gets the remaining space
145 // (because of rounding errors the width of this column isn't calculated
146 // the same way as the width of the other columns)
147 setColumnWidth( encryptColumn
- 1, totalWidth
- usedWidth
);
148 setColumnWidth( encryptColumn
, 0 );
149 setColumnWidth( encryptColumn
+ 1, 0 );
153 //-----------------------------------------------------------------------------
155 bool AttachmentListView::areCryptoCBsEnabled()
157 return ( header()->sectionSize( encryptColumn
) != 0 );
160 //-----------------------------------------------------------------------------
162 void AttachmentListView::dragEnterEvent( QDragEnterEvent
* e
)
164 // Ignore drags from ourself
165 if ( e
->source() == this )
168 KUrl::List uriList
= KUrl::List::fromMimeData( e
->mimeData() );
169 if( KPIM::MailList::canDecode( e
->mimeData() ) || !uriList
.isEmpty() )
170 e
->setAccepted( true );
172 QTreeWidget::dragEnterEvent( e
);
176 //-----------------------------------------------------------------------------
178 void AttachmentListView::dragMoveEvent( QDragMoveEvent
* e
)
180 KUrl::List uriList
= KUrl::List::fromMimeData( e
->mimeData() );
181 if( KPIM::MailList::canDecode( e
->mimeData() ) || !uriList
.isEmpty() )
182 e
->setAccepted( true );
184 QTreeWidget::dragMoveEvent( e
);
187 //-----------------------------------------------------------------------------
189 void AttachmentListView::dropEvent( QDropEvent
* e
)
191 const QMimeData
*md
= e
->mimeData();
192 if( KPIM::MailList::canDecode( md
) ) {
194 // Decode the list of serial numbers stored as the drag data
195 QByteArray serNums
= KPIM::MailList::serialsFromMimeData( md
);
196 QBuffer
serNumBuffer( &serNums
);
197 serNumBuffer
.open( QIODevice::ReadOnly
);
198 QDataStream
serNumStream( &serNumBuffer
);
200 KMFolder
*folder
= 0;
202 QList
<KMMsgBase
*> messageList
;
203 while( !serNumStream
.atEnd() ) {
204 KMMsgBase
*msgBase
= 0;
205 serNumStream
>> serNum
;
206 KMMsgDict::instance()->getLocation( serNum
, &folder
, &idx
);
208 msgBase
= folder
->getMsgBase( idx
);
210 messageList
.append( msgBase
);
212 serNumBuffer
.close();
213 uint identity
= folder
? folder
->identity() : 0;
214 KMCommand
*command
= new KMForwardAttachedCommand( mComposer
, messageList
,
215 identity
, mComposer
);
219 KUrl::List urlList
= KUrl::List::fromMimeData( md
);
220 if ( !urlList
.isEmpty() ) {
222 for( KUrl::List::Iterator it
= urlList
.begin();
223 it
!= urlList
.end(); ++it
) {
224 mComposer
->addAttach( *it
);
228 QTreeWidget::dropEvent( e
);
233 //-----------------------------------------------------------------------------
235 void AttachmentListView::keyPressEvent( QKeyEvent
* e
)
237 if ( e
->key() == Qt::Key_Delete
) {
238 emit
attachmentDeleted();
242 void AttachmentListView::startDrag( Qt::DropActions supportedActions
)
244 Q_UNUSED( supportedActions
);
248 void AttachmentListView::contextMenuEvent( QContextMenuEvent
* event
)
250 emit
rightButtonPressed( itemAt( event
->pos() ) );
253 //-----------------------------------------------------------------------------
255 void AttachmentListView::slotSort()
257 sortByColumn( header()->sortIndicatorSection(), header()->sortIndicatorOrder() );
263 #include "attachmentlistview.moc"