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

kmail

  • sources
  • kde-4.12
  • 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 // #include <assert.h>
58 using namespace KABC;
59 using namespace KIO;
60 using namespace KMail;
61 using namespace MessageViewer;
62 
63 namespace KMail {
64 
65 XFaceConfigurator::XFaceConfigurator( QWidget * parent )
66  : QWidget( parent )
67 {
68  // tmp. vars:
69  QLabel * label;
70  QLabel * label1;
71  QLabel * label2;
72  QWidget * page;
73  QVBoxLayout * vlay;
74  QHBoxLayout * hlay;
75  QVBoxLayout * page_vlay;
76  QPushButton * mFromFileBtn;
77  QPushButton * mFromAddrbkBtn;
78 
79  vlay = new QVBoxLayout( this );
80  vlay->setObjectName( QLatin1String("main layout") );
81  vlay->setSpacing( KDialog::spacingHint() );
82  vlay->setMargin( 0 );
83  hlay = new QHBoxLayout();
84  vlay->addLayout( hlay );
85 
86  // "enable X-Face" checkbox:
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 );
93 
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 );
100 
101  // label1 = new QLabel( "X-Face:", this );
102  // vlay->addWidget( label1 );
103 
104  // "obtain X-Face from" combo and label:
105  hlay = new QHBoxLayout(); // inherits spacing
106  vlay->addLayout( hlay );
107  KComboBox *sourceCombo = new KComboBox( this );
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 ); // since !mEnableCheck->isChecked()
112  sourceCombo->addItems( QStringList()
113  << i18nc( "continuation of \"obtain picture from\"",
114  "External Source" )
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 ); // since !mEnableCheck->isChecked()
120  hlay->addWidget( label );
121  hlay->addWidget( sourceCombo, 1 );
122 
123  // widget stack that is controlled by the source combo:
124  QStackedWidget * widgetStack = new QStackedWidget( this );
125  widgetStack->setEnabled( false ); // since !mEnableCheck->isChecked()
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)) );
137  // The focus might be still in the widget that is disabled
138  connect( mEnableCheck, SIGNAL(clicked()),
139  mEnableCheck, SLOT(setFocus()) );
140 
141  int pageno = 0;
142  // page 0: create X-Face from image file or address book entry
143  page = new QWidget( widgetStack );
144  widgetStack->insertWidget( pageno, page ); // force sequential numbers (play safe)
145  page_vlay = new QVBoxLayout( page );
146  page_vlay->setMargin( 0 );
147  page_vlay->setSpacing( KDialog::spacingHint() );
148  hlay = new QHBoxLayout(); // inherits spacing ??? FIXME really?
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 ); // since sourceCombo->currentItem() == 0
176 
177  // page 1: input field for direct entering
178  ++pageno;
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);
195 
196  page_vlay->addWidget( label2 );
197 
198 
199  connect(mTextEdit, SIGNAL(textChanged()), this, SLOT(slotUpdateXFace()));
200 }
201 
202 XFaceConfigurator::~XFaceConfigurator()
203 {
204 
205 }
206 
207 bool XFaceConfigurator::isXFaceEnabled() const
208 {
209  return mEnableCheck->isChecked();
210 }
211 
212 void XFaceConfigurator::setXFaceEnabled( bool enable )
213 {
214  mEnableCheck->setChecked( enable );
215 }
216 
217 QString XFaceConfigurator::xface() const
218 {
219  return mTextEdit->toPlainText();
220 }
221 
222 void XFaceConfigurator::setXFace( const QString & text )
223 {
224  mTextEdit->setPlainText( text );
225 }
226 
227 void XFaceConfigurator::setXfaceFromFile( const KUrl &url )
228 {
229  QString tmpFile;
230  if (KIO::NetAccess::download( url, tmpFile, this )) {
231  KXFace xf;
232  mTextEdit->setPlainText( xf.fromImage( QImage( tmpFile ) ) );
233  KIO::NetAccess::removeTempFile( tmpFile );
234  } else {
235  KMessageBox::error(this, KIO::NetAccess::lastErrorString() );
236  }
237 }
238 
239 void XFaceConfigurator::slotSelectFile()
240 {
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 );
246 }
247 
248 void XFaceConfigurator::slotSelectFromAddressbook()
249 {
250  using namespace KPIMIdentities;
251 
252  IdentityManager manager( true );
253  const Identity defaultIdentity = manager.defaultIdentity();
254  const QString email = defaultIdentity.primaryEmailAddress().toLower();
255 
256  Akonadi::ContactSearchJob *job = new Akonadi::ContactSearchJob( this );
257  job->setLimit( 1 );
258  job->setQuery( Akonadi::ContactSearchJob::Email, email, Akonadi::ContactSearchJob::ExactMatch );
259  connect( job, SIGNAL(result(KJob*)), SLOT(slotDelayedSelectFromAddressbook(KJob*)) );
260 }
261 
262 void XFaceConfigurator::slotDelayedSelectFromAddressbook( KJob *job )
263 {
264  const Akonadi::ContactSearchJob *searchJob = qobject_cast<Akonadi::ContactSearchJob*>( job );
265 
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") );
268  return;
269  }
270 
271  const Addressee contact = searchJob->contacts().first();
272  if ( contact.photo().isIntern() )
273  {
274  const QImage photo = contact.photo().data();
275  if ( !photo.isNull() ) {
276  KXFace xf;
277  mTextEdit->setPlainText( xf.fromImage( photo ) );
278  } else {
279  KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") );
280  }
281 
282  }
283  else
284  {
285  const KUrl url = contact.photo().url();
286  if( !url.isEmpty() )
287  setXfaceFromFile( url );
288  else
289  KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") );
290  }
291 }
292 
293 void XFaceConfigurator::slotUpdateXFace()
294 {
295  QString str = mTextEdit->toPlainText();
296 
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);
301  }
302  KXFace xf;
303  const QPixmap p = QPixmap::fromImage( xf.toImage(str) );
304  mXFaceLabel->setPixmap( p );
305  } else {
306  mXFaceLabel->clear();
307  }
308 }
309 
310 } // namespace KMail
311 
312 #include "xfaceconfigurator.moc"
text
virtual QByteArray text(quint32 serialNumber) const =0
email
const QString email
Definition: identitydialog.cpp:611
QWidget
KMail::XFaceConfigurator::setXFaceEnabled
void setXFaceEnabled(bool enable)
Definition: xfaceconfigurator.cpp:212
KMail::XFaceConfigurator::setXFace
void setXFace(const QString &text)
Definition: xfaceconfigurator.cpp:222
KMail::XFaceConfigurator::isXFaceEnabled
bool isXFaceEnabled() const
Definition: xfaceconfigurator.cpp:207
KMail::XFaceConfigurator::~XFaceConfigurator
~XFaceConfigurator()
Definition: xfaceconfigurator.cpp:202
xfaceconfigurator.h
QLabel
KComboBox
KMail::XFaceConfigurator::xface
QString xface() const
Definition: xfaceconfigurator.cpp:217
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:52 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

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