35 #include <akonadi/contact/contactsearchjob.h>
36 #include <kcombobox.h>
38 #include <kfiledialog.h>
39 #include <kglobalsettings.h>
41 #include <kio/netaccess.h>
43 #include <kmessagebox.h>
44 #include <kpimidentities/identity.h>
45 #include <kpimidentities/identitymanager.h>
46 #include "pimcommon/texteditor/plaintexteditor/plaintexteditor.h"
48 #include <messageviewer/header/kxface.h>
51 #include <QHBoxLayout>
53 #include <QPushButton>
54 #include <QStackedWidget>
55 #include <QVBoxLayout>
60 using namespace KMail;
61 using namespace MessageViewer;
65 XFaceConfigurator::XFaceConfigurator(
QWidget * parent )
75 QVBoxLayout * page_vlay;
76 QPushButton * mFromFileBtn;
77 QPushButton * mFromAddrbkBtn;
79 vlay =
new QVBoxLayout(
this );
80 vlay->setObjectName( QLatin1String(
"main layout") );
81 vlay->setSpacing( KDialog::spacingHint() );
83 hlay =
new QHBoxLayout();
84 vlay->addLayout( hlay );
87 mEnableCheck =
new QCheckBox( i18n(
"&Send picture with every message"),
this );
88 mEnableCheck->setWhatsThis(
89 i18n(
"Check this box if you want KMail to add a so-called X-Face header to messages "
90 "written with this identity. An X-Face is a small (48x48 pixels) black and "
91 "white image that some mail clients are able to display." ) );
92 hlay->addWidget( mEnableCheck, Qt::AlignLeft | Qt::AlignVCenter );
94 mXFaceLabel =
new QLabel(
this );
95 mXFaceLabel->setWhatsThis(
96 i18n(
"This is a preview of the picture selected/entered below." ) );
97 mXFaceLabel->setFixedSize(48, 48);
98 mXFaceLabel->setFrameShape( QFrame::Box );
99 hlay->addWidget( mXFaceLabel );
105 hlay =
new QHBoxLayout();
106 vlay->addLayout( hlay );
108 sourceCombo->setEditable(
false );
109 sourceCombo->setWhatsThis(
110 i18n(
"Click on the widgets below to obtain help on the input methods."));
111 sourceCombo->setEnabled(
false );
112 sourceCombo->addItems( QStringList()
113 << i18nc(
"continuation of \"obtain picture from\"",
115 << i18nc(
"continuation of \"obtain picture from\"",
116 "Input Field Below" ) );
117 label =
new QLabel( i18n(
"Obtain pic&ture from:"),
this );
118 label->setBuddy( sourceCombo );
119 label->setEnabled(
false );
120 hlay->addWidget( label );
121 hlay->addWidget( sourceCombo, 1 );
124 QStackedWidget * widgetStack =
new QStackedWidget(
this );
125 widgetStack->setEnabled(
false );
126 vlay->addWidget( widgetStack, 1 );
127 connect( sourceCombo, SIGNAL(highlighted(
int)),
128 widgetStack, SLOT(setCurrentIndex(
int)) );
129 connect( sourceCombo, SIGNAL(activated(
int)),
130 widgetStack, SLOT(setCurrentIndex(
int)) );
131 connect( mEnableCheck, SIGNAL(toggled(
bool)),
132 sourceCombo, SLOT(setEnabled(
bool)) );
133 connect( mEnableCheck, SIGNAL(toggled(
bool)),
134 widgetStack, SLOT(setEnabled(
bool)) );
135 connect( mEnableCheck, SIGNAL(toggled(
bool)),
136 label, SLOT(setEnabled(
bool)) );
138 connect( mEnableCheck, SIGNAL(clicked()),
139 mEnableCheck, SLOT(setFocus()) );
143 page =
new QWidget( widgetStack );
144 widgetStack->insertWidget( pageno, page );
145 page_vlay =
new QVBoxLayout( page );
146 page_vlay->setMargin( 0 );
147 page_vlay->setSpacing( KDialog::spacingHint() );
148 hlay =
new QHBoxLayout();
149 page_vlay->addLayout( hlay );
150 mFromFileBtn =
new QPushButton( i18n(
"Select File..."), page );
151 mFromFileBtn->setWhatsThis(
152 i18n(
"Use this to select an image file to create the picture from. "
153 "The image should be of high contrast and nearly quadratic shape. "
154 "A light background helps improve the result." ) );
155 mFromFileBtn->setAutoDefault(
false );
156 page_vlay->addWidget( mFromFileBtn, 1 );
157 connect( mFromFileBtn, SIGNAL(released()),
158 this, SLOT(slotSelectFile()) );
159 mFromAddrbkBtn =
new QPushButton( i18n(
"Set From Address Book"), page );
160 mFromAddrbkBtn->setWhatsThis(
161 i18n(
"You can use a scaled-down version of the picture "
162 "you have set in your address book entry." ) );
163 mFromAddrbkBtn->setAutoDefault(
false );
164 page_vlay->addWidget( mFromAddrbkBtn, 1 );
165 connect( mFromAddrbkBtn, SIGNAL(released()),
166 this, SLOT(slotSelectFromAddressbook()) );
167 label1 =
new QLabel( i18n(
"<qt>KMail can send a small (48x48 pixels), low-quality, "
168 "monochrome picture with every message. "
169 "For example, this could be a picture of you or a glyph. "
170 "It is shown in the recipient's mail client (if supported).</qt>" ), page );
171 label1->setAlignment( Qt::AlignVCenter );
172 label1->setWordWrap(
true );
173 page_vlay->addWidget( label1 );
174 page_vlay->addStretch();
175 widgetStack->setCurrentIndex( 0 );
179 page =
new QWidget( widgetStack );
180 widgetStack->insertWidget( pageno,page );
181 page_vlay =
new QVBoxLayout( page );
182 page_vlay->setMargin( 0 );
183 page_vlay->setSpacing( KDialog::spacingHint() );
184 mTextEdit =
new PimCommon::PlainTextEditor( page );
185 page_vlay->addWidget( mTextEdit );
186 mTextEdit->setWhatsThis( i18n(
"Use this field to enter an arbitrary X-Face string." ) );
187 mTextEdit->setFont( KGlobalSettings::fixedFont() );
188 mTextEdit->setWordWrapMode( QTextOption::WrapAnywhere);
189 mTextEdit->setSearchSupport(
false);
190 label2 =
new QLabel( i18n(
"Examples are available at <a "
191 "href=\"http://ace.home.xs4all.nl/X-Faces/\">"
192 "http://ace.home.xs4all.nl/X-Faces/</a>."), page );
193 label2->setOpenExternalLinks(
true);
194 label2->setTextInteractionFlags(Qt::TextBrowserInteraction);
196 page_vlay->addWidget( label2 );
199 connect(mTextEdit, SIGNAL(textChanged()),
this, SLOT(slotUpdateXFace()));
209 return mEnableCheck->isChecked();
214 mEnableCheck->setChecked( enable );
219 return mTextEdit->toPlainText();
224 mTextEdit->setPlainText( text );
227 void XFaceConfigurator::setXfaceFromFile(
const KUrl &url )
230 if (KIO::NetAccess::download( url, tmpFile,
this )) {
232 mTextEdit->setPlainText( xf.fromImage( QImage( tmpFile ) ) );
233 KIO::NetAccess::removeTempFile( tmpFile );
235 KMessageBox::error(
this, KIO::NetAccess::lastErrorString() );
239 void XFaceConfigurator::slotSelectFile()
241 const QStringList mimeTypes = KImageIO::mimeTypes (KImageIO::Reading);
242 const QString filter = mimeTypes.join (QLatin1String(
" "));
243 const KUrl url = KFileDialog::getOpenUrl( QString(), filter,
this, QString() );
244 if ( !url.isEmpty() )
245 setXfaceFromFile( url );
248 void XFaceConfigurator::slotSelectFromAddressbook()
250 using namespace KPIMIdentities;
252 IdentityManager manager(
true );
253 const Identity defaultIdentity = manager.defaultIdentity();
254 const QString
email = defaultIdentity.primaryEmailAddress().toLower();
256 Akonadi::ContactSearchJob *job =
new Akonadi::ContactSearchJob(
this );
258 job->setQuery( Akonadi::ContactSearchJob::Email,
email, Akonadi::ContactSearchJob::ExactMatch );
259 connect( job, SIGNAL(result(
KJob*)), SLOT(slotDelayedSelectFromAddressbook(
KJob*)) );
262 void XFaceConfigurator::slotDelayedSelectFromAddressbook(
KJob *job )
264 const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
266 if ( searchJob->contacts().isEmpty() ) {
267 KMessageBox::information(
this, i18n(
"You do not have your own contact defined in the address book."), i18n(
"No Picture") );
271 const Addressee contact = searchJob->contacts().first();
272 if ( contact.photo().isIntern() )
274 const QImage photo = contact.photo().data();
275 if ( !photo.isNull() ) {
277 mTextEdit->setPlainText( xf.fromImage( photo ) );
279 KMessageBox::information(
this, i18n(
"No picture set for your address book entry."), i18n(
"No Picture") );
285 const KUrl url = contact.photo().url();
287 setXfaceFromFile( url );
289 KMessageBox::information(
this, i18n(
"No picture set for your address book entry."), i18n(
"No Picture") );
293 void XFaceConfigurator::slotUpdateXFace()
295 QString str = mTextEdit->toPlainText();
297 if ( !str.isEmpty() ) {
298 if ( str.startsWith( QLatin1String(
"x-face:"), Qt::CaseInsensitive ) ) {
299 str = str.remove( QLatin1String(
"x-face:"), Qt::CaseInsensitive );
300 mTextEdit->setPlainText(str);
303 const QPixmap p = QPixmap::fromImage( xf.toImage(str) );
304 mXFaceLabel->setPixmap( p );
306 mXFaceLabel->clear();
312 #include "xfaceconfigurator.moc"
virtual QByteArray text(quint32 serialNumber) const =0
void setXFaceEnabled(bool enable)
void setXFace(const QString &text)
bool isXFaceEnabled() const