• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kmail

  • sources
  • kde-4.14
  • kdepim
  • kmail
  • identity
xfaceconfigurator.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KMail.
3 
4  Copyright (c) 2004 Jakob Schr�er <js@camaya.net>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
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
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include "xfaceconfigurator.h"
34 
35 #include <akonadi/contact/contactsearchjob.h>
36 #include <kcombobox.h>
37 #include <kdialog.h>
38 #include <kfiledialog.h>
39 #include <kglobalsettings.h>
40 #include <kimageio.h>
41 #include <kio/netaccess.h>
42 #include <klocale.h>
43 #include <kmessagebox.h>
44 #include <kpimidentities/identity.h>
45 #include <kpimidentities/identitymanager.h>
46 #include "pimcommon/texteditor/plaintexteditor/plaintexteditor.h"
47 #include <kurl.h>
48 #include <messageviewer/header/kxface.h>
49 
50 #include <QCheckBox>
51 #include <QHBoxLayout>
52 #include <QLabel>
53 #include <QPushButton>
54 #include <QStackedWidget>
55 #include <QVBoxLayout>
56 
57 using namespace KABC;
58 using namespace KIO;
59 using namespace KMail;
60 using namespace MessageViewer;
61 
62 namespace KMail {
63 
64 XFaceConfigurator::XFaceConfigurator( QWidget * parent )
65  : QWidget( parent )
66 {
67  QVBoxLayout *vlay = new QVBoxLayout( this );
68  vlay->setObjectName( QLatin1String("main layout") );
69  vlay->setSpacing( KDialog::spacingHint() );
70  vlay->setMargin( 0 );
71  QHBoxLayout *hlay = new QHBoxLayout();
72  vlay->addLayout( hlay );
73 
74  // "enable X-Face" checkbox:
75  mEnableCheck = new QCheckBox( i18n("&Send picture with every message"), this );
76  mEnableCheck->setWhatsThis(
77  i18n( "Check this box if you want KMail to add a so-called X-Face header to messages "
78  "written with this identity. An X-Face is a small (48x48 pixels) black and "
79  "white image that some mail clients are able to display." ) );
80  hlay->addWidget( mEnableCheck, Qt::AlignLeft | Qt::AlignVCenter );
81 
82  mXFaceLabel = new QLabel( this );
83  mXFaceLabel->setWhatsThis(
84  i18n( "This is a preview of the picture selected/entered below." ) );
85  mXFaceLabel->setFixedSize(48, 48);
86  mXFaceLabel->setFrameShape( QFrame::Box );
87  hlay->addWidget( mXFaceLabel );
88 
89  // label1 = new QLabel( "X-Face:", this );
90  // vlay->addWidget( label1 );
91 
92  // "obtain X-Face from" combo and label:
93  hlay = new QHBoxLayout(); // inherits spacing
94  vlay->addLayout( hlay );
95  KComboBox *sourceCombo = new KComboBox( this );
96  sourceCombo->setEditable( false );
97  sourceCombo->setWhatsThis(
98  i18n("Click on the widgets below to obtain help on the input methods."));
99  sourceCombo->setEnabled( false ); // since !mEnableCheck->isChecked()
100  sourceCombo->addItems( QStringList()
101  << i18nc( "continuation of \"obtain picture from\"",
102  "External Source" )
103  << i18nc( "continuation of \"obtain picture from\"",
104  "Input Field Below" ) );
105  QLabel *label = new QLabel( i18n("Obtain pic&ture from:"), this );
106  label->setBuddy( sourceCombo );
107  label->setEnabled( false ); // since !mEnableCheck->isChecked()
108  hlay->addWidget( label );
109  hlay->addWidget( sourceCombo, 1 );
110 
111  // widget stack that is controlled by the source combo:
112  QStackedWidget * widgetStack = new QStackedWidget( this );
113  widgetStack->setEnabled( false ); // since !mEnableCheck->isChecked()
114  vlay->addWidget( widgetStack, 1 );
115  connect( sourceCombo, SIGNAL(highlighted(int)),
116  widgetStack, SLOT(setCurrentIndex(int)) );
117  connect( sourceCombo, SIGNAL(activated(int)),
118  widgetStack, SLOT(setCurrentIndex(int)) );
119  connect( mEnableCheck, SIGNAL(toggled(bool)),
120  sourceCombo, SLOT(setEnabled(bool)) );
121  connect( mEnableCheck, SIGNAL(toggled(bool)),
122  widgetStack, SLOT(setEnabled(bool)) );
123  connect( mEnableCheck, SIGNAL(toggled(bool)),
124  label, SLOT(setEnabled(bool)) );
125  // The focus might be still in the widget that is disabled
126  connect( mEnableCheck, SIGNAL(clicked()),
127  mEnableCheck, SLOT(setFocus()) );
128 
129  int pageno = 0;
130  // page 0: create X-Face from image file or address book entry
131  QWidget *page = new QWidget( widgetStack );
132  widgetStack->insertWidget( pageno, page ); // force sequential numbers (play safe)
133  QVBoxLayout *page_vlay = new QVBoxLayout( page );
134  page_vlay->setMargin( 0 );
135  page_vlay->setSpacing( KDialog::spacingHint() );
136  hlay = new QHBoxLayout(); // inherits spacing ??? FIXME really?
137  page_vlay->addLayout( hlay );
138  QPushButton *fromFileButton = new QPushButton( i18n("Select File..."), page );
139  fromFileButton->setWhatsThis(
140  i18n("Use this to select an image file to create the picture from. "
141  "The image should be of high contrast and nearly quadratic shape. "
142  "A light background helps improve the result." ) );
143  fromFileButton->setAutoDefault( false );
144  page_vlay->addWidget( fromFileButton, 1 );
145  connect( fromFileButton, SIGNAL(released()),
146  this, SLOT(slotSelectFile()) );
147  QPushButton *fromAddressBookButton = new QPushButton( i18n("Set From Address Book"), page );
148  fromAddressBookButton->setWhatsThis(
149  i18n( "You can use a scaled-down version of the picture "
150  "you have set in your address book entry." ) );
151  fromAddressBookButton->setAutoDefault( false );
152  page_vlay->addWidget( fromAddressBookButton, 1 );
153  connect( fromAddressBookButton, SIGNAL(released()),
154  this, SLOT(slotSelectFromAddressbook()) );
155  QLabel *label1 = new QLabel( i18n("<qt>KMail can send a small (48x48 pixels), low-quality, "
156  "monochrome picture with every message. "
157  "For example, this could be a picture of you or a glyph. "
158  "It is shown in the recipient's mail client (if supported).</qt>" ), page );
159  label1->setAlignment( Qt::AlignVCenter );
160  label1->setWordWrap( true );
161  page_vlay->addWidget( label1 );
162  page_vlay->addStretch();
163  widgetStack->setCurrentIndex( 0 ); // since sourceCombo->currentItem() == 0
164 
165  // page 1: input field for direct entering
166  ++pageno;
167  page = new QWidget( widgetStack );
168  widgetStack->insertWidget( pageno,page );
169  page_vlay = new QVBoxLayout( page );
170  page_vlay->setMargin( 0 );
171  page_vlay->setSpacing( KDialog::spacingHint() );
172  mTextEdit = new PimCommon::PlainTextEditor( page );
173  page_vlay->addWidget( mTextEdit );
174  mTextEdit->setWhatsThis( i18n( "Use this field to enter an arbitrary X-Face string." ) );
175  mTextEdit->setFont( KGlobalSettings::fixedFont() );
176  mTextEdit->setWordWrapMode( QTextOption::WrapAnywhere);
177  mTextEdit->setSearchSupport(false);
178  QLabel *label2 = new QLabel( i18n("Examples are available at <a "
179  "href=\"http://ace.home.xs4all.nl/X-Faces/\">"
180  "http://ace.home.xs4all.nl/X-Faces/</a>."), page );
181  label2->setOpenExternalLinks(true);
182  label2->setTextInteractionFlags(Qt::TextBrowserInteraction);
183 
184  page_vlay->addWidget( label2 );
185 
186 
187  connect(mTextEdit, SIGNAL(textChanged()), this, SLOT(slotUpdateXFace()));
188 }
189 
190 XFaceConfigurator::~XFaceConfigurator()
191 {
192 
193 }
194 
195 bool XFaceConfigurator::isXFaceEnabled() const
196 {
197  return mEnableCheck->isChecked();
198 }
199 
200 void XFaceConfigurator::setXFaceEnabled( bool enable )
201 {
202  mEnableCheck->setChecked( enable );
203 }
204 
205 QString XFaceConfigurator::xface() const
206 {
207  return mTextEdit->toPlainText();
208 }
209 
210 void XFaceConfigurator::setXFace( const QString & text )
211 {
212  mTextEdit->setPlainText( text );
213 }
214 
215 void XFaceConfigurator::setXfaceFromFile( const KUrl &url )
216 {
217  QString tmpFile;
218  if (KIO::NetAccess::download( url, tmpFile, this )) {
219  KXFace xf;
220  mTextEdit->setPlainText( xf.fromImage( QImage( tmpFile ) ) );
221  KIO::NetAccess::removeTempFile( tmpFile );
222  } else {
223  KMessageBox::error(this, KIO::NetAccess::lastErrorString() );
224  }
225 }
226 
227 void XFaceConfigurator::slotSelectFile()
228 {
229  const QStringList mimeTypes = KImageIO::mimeTypes (KImageIO::Reading);
230  const QString filter = mimeTypes.join (QLatin1String(" "));
231  const KUrl url = KFileDialog::getOpenUrl( QString(), filter, this, QString() );
232  if ( !url.isEmpty() )
233  setXfaceFromFile( url );
234 }
235 
236 void XFaceConfigurator::slotSelectFromAddressbook()
237 {
238  using namespace KPIMIdentities;
239 
240  IdentityManager manager( true );
241  const Identity defaultIdentity = manager.defaultIdentity();
242  const QString email = defaultIdentity.primaryEmailAddress().toLower();
243 
244  Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob( this );
245  job->setLimit( 1 );
246  job->setQuery( Akonadi::ContactSearchJob::Email, email, Akonadi::ContactSearchJob::ExactMatch );
247  connect( job, SIGNAL(result(KJob*)), SLOT(slotDelayedSelectFromAddressbook(KJob*)) );
248 }
249 
250 void XFaceConfigurator::slotDelayedSelectFromAddressbook( KJob *job )
251 {
252  const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
253 
254  if ( searchJob->contacts().isEmpty() ) {
255  KMessageBox::information( this, i18n("You do not have your own contact defined in the address book."), i18n("No Picture") );
256  return;
257  }
258 
259  const Addressee contact = searchJob->contacts().at(0);
260  if ( contact.photo().isIntern() )
261  {
262  const QImage photo = contact.photo().data();
263  if ( !photo.isNull() ) {
264  KXFace xf;
265  mTextEdit->setPlainText( xf.fromImage( photo ) );
266  } else {
267  KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") );
268  }
269 
270  }
271  else
272  {
273  const KUrl url = contact.photo().url();
274  if( !url.isEmpty() )
275  setXfaceFromFile( url );
276  else
277  KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") );
278  }
279 }
280 
281 void XFaceConfigurator::slotUpdateXFace()
282 {
283  QString str = mTextEdit->toPlainText();
284 
285  if ( !str.isEmpty() ) {
286  if ( str.startsWith( QLatin1String("x-face:"), Qt::CaseInsensitive ) ) {
287  str = str.remove( QLatin1String("x-face:"), Qt::CaseInsensitive );
288  mTextEdit->setPlainText(str);
289  }
290  KXFace xf;
291  const QPixmap p = QPixmap::fromImage( xf.toImage(str) );
292  mXFaceLabel->setPixmap( p );
293  } else {
294  mXFaceLabel->clear();
295  }
296 }
297 
298 } // namespace KMail
299 
QWidget
text
virtual QByteArray text(quint32 serialNumber) const =0
QFrame::setFrameShape
void setFrameShape(Shape)
email
const QString email
Definition: identitydialog.cpp:640
QLabel::setOpenExternalLinks
void setOpenExternalLinks(bool open)
QLabel::setPixmap
void setPixmap(const QPixmap &)
QHBoxLayout
QLabel::setAlignment
void setAlignment(QFlags< Qt::AlignmentFlag >)
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QLabel::clear
void clear()
QStringList::join
QString join(const QString &separator) const
QImage::isNull
bool isNull() const
QString::remove
QString & remove(int position, int n)
KMail::XFaceConfigurator::setXFaceEnabled
void setXFaceEnabled(bool enable)
Definition: xfaceconfigurator.cpp:200
KMail::XFaceConfigurator::setXFace
void setXFace(const QString &text)
Definition: xfaceconfigurator.cpp:210
QLabel::setBuddy
void setBuddy(QWidget *buddy)
QWidget::setEnabled
void setEnabled(bool)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QWidget::setFocus
void setFocus()
QCheckBox
QObject::setObjectName
void setObjectName(const QString &name)
QString::isEmpty
bool isEmpty() const
QStackedWidget::setCurrentIndex
void setCurrentIndex(int index)
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
KMail::XFaceConfigurator::isXFaceEnabled
bool isXFaceEnabled() const
Definition: xfaceconfigurator.cpp:195
QVBoxLayout
QStackedWidget
QString
QLayout::setMargin
void setMargin(int margin)
QStringList
KMail::XFaceConfigurator::~XFaceConfigurator
~XFaceConfigurator()
Definition: xfaceconfigurator.cpp:190
QPixmap
QString::toLower
QString toLower() const
QWidget::setFixedSize
void setFixedSize(const QSize &s)
QImage
QAbstractButton::isChecked
bool isChecked() const
QLabel::setTextInteractionFlags
void setTextInteractionFlags(QFlags< Qt::TextInteractionFlag > flags)
QWidget::setWhatsThis
void setWhatsThis(const QString &)
xfaceconfigurator.h
QStackedWidget::insertWidget
int insertWidget(int index, QWidget *widget)
QLatin1String
QBoxLayout::addStretch
void addStretch(int stretch)
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
KComboBox
QPushButton
QtConcurrent::filter
QFuture< void > filter(Sequence &sequence, FilterFunction filterFunction)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
KMail::XFaceConfigurator::xface
QString xface() const
Definition: xfaceconfigurator.cpp:205
KJob
QLabel::setWordWrap
void setWordWrap(bool on)
QBoxLayout::setSpacing
void setSpacing(int spacing)
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
QPushButton::setAutoDefault
void setAutoDefault(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kmail

Skip menu "kmail"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal