Allow specializations to decide when re-indexing is necessary.
[kdepim.git] / messagecore / attachmentpropertiesdialog.cpp
blob3c802a82ccdcc4de68391c96e6f9adf9398734b8
1 /*
2 Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
4 Based on KMail code by various authors (kmmsgpartdlg).
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
22 #include "attachmentpropertiesdialog.h"
24 #include "attachmentfrommimecontentjob.h"
25 #include "ui_attachmentpropertiesdialog.h"
27 #include <boost/shared_ptr.hpp>
29 #include <KAboutData>
30 #include <KComponentData>
31 #include <KDebug>
32 #include <KGlobal>
33 #include <KMimeType>
35 #include <kmime/kmime_content.h>
36 #include <kmime/kmime_headers.h>
37 #include <kmime/kmime_util.h>
39 using namespace KPIM;
41 class KPIM::AttachmentPropertiesDialog::Private
43 public:
44 Private( AttachmentPropertiesDialog *qq )
45 : q( qq )
46 , readOnly( false )
50 void init( AttachmentPart::Ptr part, bool readOnly );
51 void polishUi();
52 void mimeTypeChanged( const QString &type ); // slot
53 void populateEncodings();
54 void populateMimeTypes();
55 void populateWhatsThis();
56 void loadFromPart();
57 void saveToPart();
59 AttachmentPropertiesDialog *const q;
60 bool readOnly;
61 AttachmentPart::Ptr part;
62 Ui::AttachmentPropertiesDialog ui;
65 void AttachmentPropertiesDialog::Private::init( AttachmentPart::Ptr part, bool readOnly )
67 this->readOnly = readOnly;
68 this->part = part;
70 QWidget *widget = new QWidget( q );
71 q->setMainWidget( widget );
72 ui.setupUi( widget );
73 polishUi();
74 q->setModal( true );
75 q->showButtonSeparator( true );
76 q->setHelp( QString::fromLatin1( "attachments" ) );
78 loadFromPart();
81 void AttachmentPropertiesDialog::Private::polishUi()
83 // Update the icon when the selected mime type changes.
84 connect( ui.mimeType, SIGNAL( currentIndexChanged ( const QString & ) ),
85 q, SLOT( mimeTypeChanged( QString ) ) );
86 // Tweak the dialog, depending on whether it is read-only or not.
87 if( readOnly ) {
88 ui.mimeType->setEditable( false );
89 ui.name->setReadOnly( true );
90 ui.description->setReadOnly( true );
91 ui.encoding->setEnabled( false );
92 ui.autoDisplay->setEnabled( false );
93 ui.encrypt->setEnabled( false );
94 ui.sign->setEnabled( false );
96 q->setButtons( Ok|Help );
97 } else {
98 populateEncodings();
99 populateMimeTypes();
101 q->setButtons( Ok|Cancel|Help );
104 q->setDefaultButton( Ok );
105 populateWhatsThis();
108 void AttachmentPropertiesDialog::Private::mimeTypeChanged( const QString &type )
110 KMimeType::Ptr mimeType = KMimeType::mimeType( type, KMimeType::ResolveAliases );
111 if( !mimeType.isNull() ) {
112 ui.mimeIcon->setPixmap( KIconLoader::global()->loadMimeTypeIcon( mimeType->iconName(),
113 KIconLoader::Desktop ) );
114 } else {
115 ui.mimeIcon->setPixmap( DesktopIcon( "unknown" ) );
119 void AttachmentPropertiesDialog::Private::populateWhatsThis()
121 QString msg;
123 // FIXME These are such a mess... Make them straightforward and pretty.
125 msg = i18n( "<p>The <em>MIME type</em> of the file:</p>"
126 "<p>Normally, you do not need to touch this setting, since the "
127 "type of the file is automatically checked; but, sometimes, %1 "
128 "may not detect the type correctly -- here is where you can fix "
129 "that.</p>", KGlobal::mainComponent().aboutData()->programName() );
130 ui.mimeType->setWhatsThis( msg );
132 msg = i18n( "<p>The estimated size of the attachment:</p>"
133 "<p>Note that, in an email message, a binary file encoded with "
134 "base64 will take up four thirds the actual size of the file.</p>" );
135 ui.size->setWhatsThis( msg );
137 msg = i18n( "<p>The file name of the part:</p>"
138 "<p>Although this defaults to the name of the attached file, "
139 "it does not specify the file to be attached; rather, it "
140 "suggests a file name to be used by the recipient's mail agent "
141 "when saving the part to disk.</p>" );
142 ui.name->setWhatsThis( msg );
144 msg = i18n( "<p>A description of the part:</p>"
145 "<p>This is just an informational description of the part, "
146 "much like the Subject is for the whole message; most "
147 "mail agents will show this information in their message "
148 "previews alongside the attachment's icon.</p>" );
149 ui.description->setWhatsThis( msg );
151 msg = i18n( "<p>The transport encoding of this part:</p>"
152 "<p>Normally, you do not need to change this, since %1 will use "
153 "a decent default encoding, depending on the MIME type; yet, "
154 "sometimes, you can significantly reduce the size of the "
155 "resulting message, e.g. if a PostScript file does not contain "
156 "binary data, but consists of pure text -- in this case, choosing "
157 "\"quoted-printable\" over the default \"base64\" will save up "
158 "to 25% in resulting message size.</p>",
159 KGlobal::mainComponent().aboutData()->programName() );
160 ui.encoding->setWhatsThis( msg );
162 msg = i18n( "<p>Check this option if you want to suggest to the "
163 "recipient the automatic (inline) display of this part in the "
164 "message preview, instead of the default icon view;</p>"
165 "<p>Technically, this is carried out by setting this part's "
166 "<em>Content-Disposition</em> header field to \"inline\" "
167 "instead of the default \"attachment\".</p>" );
168 ui.autoDisplay->setWhatsThis( msg );
170 msg = i18n( "<p>Check this option if you want this message part to be "
171 "signed.</p>"
172 "<p>The signature will be made with the key that you associated "
173 "with the currently-selected identity.</p>" );
174 ui.sign->setWhatsThis( msg );
176 msg = i18n( "<p>Check this option if you want this message part to be "
177 "encrypted.</p>"
178 "<p>The part will be encrypted for the recipients of this "
179 "message.</p>" );
180 ui.encrypt->setWhatsThis( msg );
183 void AttachmentPropertiesDialog::Private::populateEncodings()
185 using namespace KMime;
186 using namespace KMime::Headers;
188 ui.encoding->clear();
189 ui.encoding->addItem( nameForEncoding( CE7Bit ), int( CE7Bit ) );
190 ui.encoding->addItem( nameForEncoding( CE8Bit ), int( CE8Bit ) );
191 ui.encoding->addItem( nameForEncoding( CEquPr ), int( CEquPr ) );
192 ui.encoding->addItem( nameForEncoding( CEbase64 ), int( CEbase64 ) );
194 // TODO 8bit should be disabled if it is disabled in Settings.
195 // Also, if it's a message/* part, base64 and qp should be disabled.
196 // But since this is a dialog for power users anyway, let them shoot
197 // themselves in the foot. (The AttachmentJob will fail when they
198 // try to compose the message.)
201 void AttachmentPropertiesDialog::Private::populateMimeTypes()
203 // TODO these are what the old KMMsgPartDialog used.
204 // Should we use KMimeType::allMimeTypes()?
206 QStringList list;
207 list << QString::fromLatin1( "text/html" )
208 << QString::fromLatin1( "text/plain" )
209 << QString::fromLatin1( "image/gif" )
210 << QString::fromLatin1( "image/jpeg" )
211 << QString::fromLatin1( "image/png" )
212 << QString::fromLatin1( "application/octet-stream" )
213 << QString::fromLatin1( "application/x-gunzip" )
214 << QString::fromLatin1( "application/zip" );
215 ui.mimeType->addItems( list );
218 void AttachmentPropertiesDialog::Private::loadFromPart()
220 Q_ASSERT( part );
222 ui.mimeType->setCurrentItem( part->mimeType(), true );
223 ui.size->setText( KGlobal::locale()->formatByteSize( part->size() ) );
224 ui.name->setText( part->name() );
225 ui.description->setText( part->description() );
226 ui.encoding->setCurrentIndex( int( part->encoding() ) );
227 ui.autoDisplay->setChecked( part->isInline() );
228 ui.encrypt->setChecked( part->isEncrypted() );
229 ui.sign->setChecked( part->isSigned() );
232 void AttachmentPropertiesDialog::Private::saveToPart()
234 Q_ASSERT( part );
235 Q_ASSERT( !readOnly );
237 part->setMimeType( ui.mimeType->currentText().toLatin1() );
238 part->setName( ui.name->text() );
239 // TODO what about fileName? Extra field??
240 part->setDescription( ui.description->text() );
241 part->setInline( ui.autoDisplay->isChecked() );
242 part->setSigned( ui.sign->isChecked() );
243 part->setEncrypted( ui.encrypt->isChecked() );
245 if( ui.mimeType->currentText().startsWith( QLatin1String( "message" ) ) &&
246 ui.encoding->itemData( ui.encoding->currentIndex() ) != KMime::Headers::CE7Bit &&
247 ui.encoding->itemData( ui.encoding->currentIndex() ) != KMime::Headers::CE8Bit ) {
248 kWarning() << "Encoding on message/rfc822 must be \"7bit\" or \"8bit\".";
250 part->setEncoding( KMime::Headers::contentEncoding(
251 ui.encoding->itemData( ui.encoding->currentIndex() ).toInt() ) );
254 AttachmentPropertiesDialog::AttachmentPropertiesDialog( AttachmentPart::Ptr part,
255 QWidget *parent, bool readOnly )
256 : KDialog( parent )
257 , d( new Private( this ) )
259 d->init( part, readOnly );
262 AttachmentPropertiesDialog::AttachmentPropertiesDialog( const KMime::Content *content,
263 QWidget *parent, bool readOnly )
264 : KDialog( parent )
265 , d( new Private( this ) )
267 if( !readOnly ) {
268 kFatal() << "Dialog must be read-only if loading from a KMime::Content.";
271 AttachmentFromMimeContentJob *ajob = new AttachmentFromMimeContentJob( content, this );
272 ajob->exec();
273 if( ajob->error() ) {
274 kError() << "AttachmentFromMimeContentJob failed.";
276 AttachmentPart::Ptr part = ajob->attachmentPart();
277 d->init( part, readOnly );
280 AttachmentPropertiesDialog::~AttachmentPropertiesDialog()
282 delete d;
285 KPIM::AttachmentPart::Ptr AttachmentPropertiesDialog::attachmentPart() const
287 return d->part;
290 bool AttachmentPropertiesDialog::isEncryptEnabled() const
292 return d->ui.encrypt->isEnabled();
295 void AttachmentPropertiesDialog::setEncryptEnabled( bool enabled )
297 return d->ui.encrypt->setEnabled( enabled );
300 bool AttachmentPropertiesDialog::isSignEnabled() const
302 return d->ui.sign->isEnabled();
305 void AttachmentPropertiesDialog::setSignEnabled( bool enabled )
307 return d->ui.sign->setEnabled( enabled );
310 void AttachmentPropertiesDialog::accept()
312 if( !d->readOnly ) {
313 d->saveToPart();
315 KDialog::accept();
318 #include "attachmentpropertiesdialog.moc"