kmail

xfaceconfigurator.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of KMail.
00003 
00004     Copyright (c) 2004 Jakob Schr�er <js@camaya.net>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
00031 */
00032 
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036 
00037 #include "xfaceconfigurator.h"
00038 
00039 #include <kactivelabel.h>
00040 #include <kdialog.h>
00041 #include <kfiledialog.h>
00042 #include <kglobalsettings.h>
00043 #include <kimageio.h>
00044 #include <klocale.h>
00045 #include <kmessagebox.h>
00046 #include <kurl.h>
00047 #include <kio/netaccess.h>
00048 using namespace KIO;
00049 #include <kxface.h>
00050 using namespace KPIM;
00051 #include <kabc/stdaddressbook.h>
00052 #include <kabc/addressee.h>
00053 using namespace KABC;
00054 
00055 #include <qbitmap.h>
00056 #include <qcheckbox.h>
00057 #include <qcombobox.h>
00058 #include <qimage.h>
00059 #include <qlabel.h>
00060 #include <qlayout.h>
00061 #include <qpushbutton.h>
00062 #include <qwhatsthis.h>
00063 #include <qwidgetstack.h>
00064 
00065 
00066 // #include <assert.h>
00067 
00068 using namespace KMail;
00069 using namespace KPIM;
00070 
00071 namespace KMail {
00072 
00073   XFaceConfigurator::XFaceConfigurator( QWidget * parent, const char * name )
00074     : QWidget( parent, name )
00075   {
00076     // tmp. vars:
00077     QLabel * label;
00078     QLabel * label1;
00079     KActiveLabel * label2;
00080     QWidget * page;
00081     QVBoxLayout * vlay;
00082     QHBoxLayout * hlay;
00083     QVBoxLayout * page_vlay;
00084     QPushButton * mFromFileBtn;
00085     QPushButton * mFromAddrbkBtn;
00086 
00087     vlay = new QVBoxLayout( this, 0, KDialog::spacingHint(), "main layout" );
00088     hlay = new QHBoxLayout( vlay );
00089 
00090     // "enable X-Face" checkbox:
00091     mEnableCheck = new QCheckBox( i18n("&Send picture with every message"), this );
00092     QWhatsThis::add( mEnableCheck,
00093         i18n( "Check this box if you want KMail to add a so-called X-Face header to messages "
00094             "written with this identity. An X-Face is a small (48x48 pixels) black and "
00095             "white image that some mail clients are able to display." ) );
00096     hlay->addWidget( mEnableCheck, Qt::AlignLeft | Qt::AlignVCenter );
00097 
00098     mXFaceLabel = new QLabel( this );
00099     QWhatsThis::add(mXFaceLabel,
00100                     i18n( "This is a preview of the picture selected/entered below." ) );
00101     mXFaceLabel->setFixedSize(48, 48);
00102     mXFaceLabel->setFrameShape( QFrame::Box );
00103     hlay->addWidget( mXFaceLabel );
00104 
00105 //     label1 = new QLabel( "X-Face:", this );
00106 //     vlay->addWidget( label1 );
00107 
00108     // "obtain X-Face from" combo and label:
00109     hlay = new QHBoxLayout( vlay ); // inherits spacing
00110     mSourceCombo = new QComboBox( false, this );
00111     QWhatsThis::add(mSourceCombo,
00112                     i18n("Click on the widgets below to obtain help on the input methods."));
00113     mSourceCombo->setEnabled( false ); // since !mEnableCheck->isChecked()
00114     mSourceCombo->insertStringList( QStringList()
00115         << i18n( "continuation of \"obtain picture from\"",
00116                  "External Source" )
00117         << i18n( "continuation of \"obtain picture from\"",
00118                  "Input Field Below" ) );
00119     label = new QLabel( mSourceCombo,
00120                         i18n("Obtain pic&ture from:"), this );
00121     label->setEnabled( false ); // since !mEnableCheck->isChecked()
00122     hlay->addWidget( label );
00123     hlay->addWidget( mSourceCombo, 1 );
00124 
00125     // widget stack that is controlled by the source combo:
00126     QWidgetStack * widgetStack = new QWidgetStack( this );
00127     widgetStack->setEnabled( false ); // since !mEnableCheck->isChecked()
00128     vlay->addWidget( widgetStack, 1 );
00129     connect( mSourceCombo, SIGNAL(highlighted(int)),
00130              widgetStack, SLOT(raiseWidget(int)) );
00131     connect( mEnableCheck, SIGNAL(toggled(bool)),
00132              mSourceCombo, SLOT(setEnabled(bool)) );
00133     connect( mEnableCheck, SIGNAL(toggled(bool)),
00134              widgetStack, SLOT(setEnabled(bool)) );
00135     connect( mEnableCheck, SIGNAL(toggled(bool)),
00136              label, SLOT(setEnabled(bool)) );
00137     // The focus might be still in the widget that is disabled
00138     connect( mEnableCheck, SIGNAL(clicked()),
00139              mEnableCheck, SLOT(setFocus()) );
00140 
00141     int pageno = 0;
00142     // page 0: create X-Face from image file or address book entry
00143     page = new QWidget( widgetStack );
00144     widgetStack->addWidget( page, pageno ); // force sequential numbers (play safe)
00145     page_vlay = new QVBoxLayout( page, 0, KDialog::spacingHint() );
00146     hlay = new QHBoxLayout( page_vlay ); // inherits spacing
00147     mFromFileBtn = new QPushButton( i18n("Select File..."), page );
00148     QWhatsThis::add( mFromFileBtn,
00149                      i18n("Use this to select an image file to create the picture from. "
00150                          "The image should be of high contrast and nearly quadratic shape. "
00151                          "A light background helps improve the result." ) );
00152     mFromFileBtn->setAutoDefault( false );
00153     page_vlay->addWidget( mFromFileBtn, 1 );
00154     connect( mFromFileBtn, SIGNAL(released()),
00155              this, SLOT(slotSelectFile()) );
00156     mFromAddrbkBtn = new QPushButton( i18n("Set From Address Book"), page );
00157     QWhatsThis::add( mFromAddrbkBtn,
00158                      i18n( "You can use a scaled-down version of the picture "
00159                          "you have set in your address book entry." ) );
00160     mFromAddrbkBtn->setAutoDefault( false );
00161     page_vlay->addWidget( mFromAddrbkBtn, 1 );
00162     connect( mFromAddrbkBtn, SIGNAL(released()),
00163              this, SLOT(slotSelectFromAddressbook()) );
00164     label1 = new QLabel( i18n("<qt>KMail can send a small (48x48 pixels), low-quality, "
00165         "monochrome picture with every message. "
00166         "For example, this could be a picture of you or a glyph. "
00167         "It is shown in the recipient's mail client (if supported)." ), page );
00168     label1->setAlignment( QLabel::WordBreak | QLabel::AlignVCenter );
00169     page_vlay->addWidget( label1 );
00170 
00171     widgetStack->raiseWidget( 0 ); // since mSourceCombo->currentItem() == 0
00172 
00173     // page 1: input field for direct entering
00174     ++pageno;
00175     page = new QWidget( widgetStack );
00176     widgetStack->addWidget( page, pageno );
00177     page_vlay = new QVBoxLayout( page, 0, KDialog::spacingHint() );
00178     mTextEdit = new QTextEdit( page );
00179     page_vlay->addWidget( mTextEdit );
00180     QWhatsThis::add( mTextEdit, i18n( "Use this field to enter an arbitrary X-Face string." ) );
00181     mTextEdit->setFont( KGlobalSettings::fixedFont() );
00182     mTextEdit->setWrapPolicy( QTextEdit::Anywhere );
00183     mTextEdit->setTextFormat( Qt::PlainText );
00184     label2 = new KActiveLabel( i18n("Examples are available at <a href=\"http://www.xs4all.nl/~ace/X-Faces/\">http://www.xs4all.nl/~ace/X-Faces/</a>."), page );
00185     page_vlay->addWidget( label2 );
00186 
00187 
00188     connect(mTextEdit, SIGNAL(textChanged()), this, SLOT(slotUpdateXFace()));
00189   }
00190 
00191   XFaceConfigurator::~XFaceConfigurator() {
00192 
00193   }
00194 
00195   bool XFaceConfigurator::isXFaceEnabled() const {
00196     return mEnableCheck->isChecked();
00197   }
00198 
00199   void XFaceConfigurator::setXFaceEnabled( bool enable ) {
00200     mEnableCheck->setChecked( enable );
00201   }
00202 
00203   QString XFaceConfigurator::xface() const {
00204     return mTextEdit->text();
00205   }
00206 
00207   void XFaceConfigurator::setXFace( const QString & text ) {
00208     mTextEdit->setText( text );
00209   }
00210 
00211   void XFaceConfigurator::setXfaceFromFile( const KURL &url )
00212   {
00213     QString tmpFile;
00214     if( KIO::NetAccess::download( url, tmpFile, this ) )
00215     {
00216       KXFace xf;
00217       mTextEdit->setText( xf.fromImage( QImage( tmpFile ) ) );
00218       KIO::NetAccess::removeTempFile( tmpFile );
00219     } else {
00220       KMessageBox::error(this, KIO::NetAccess::lastErrorString() );
00221     }
00222   }
00223 
00224   void XFaceConfigurator::slotSelectFile()
00225   {
00226     QStringList mimeTypes = KImageIO::mimeTypes (KImageIO::Reading);
00227     QString filter = mimeTypes.join (" ");
00228     KURL url = KFileDialog::getOpenURL( QString::null, filter, this, QString::null );
00229     if ( !url.isEmpty() )
00230       setXfaceFromFile( url );
00231   }
00232 
00233   void XFaceConfigurator::slotSelectFromAddressbook()
00234   {
00235     StdAddressBook *ab = StdAddressBook::self( true );
00236     Addressee me = ab->whoAmI();
00237     if ( !me.isEmpty() )
00238     {
00239       if ( me.photo().isIntern() )
00240       {
00241         QImage photo = me.photo().data();
00242         if ( !photo.isNull() )
00243         {
00244           KXFace xf;
00245           mTextEdit->setText( xf.fromImage( photo ) );
00246         }
00247         else
00248           KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") );
00249 
00250       }
00251       else
00252       {
00253         KURL url = me.photo().url();
00254         if( !url.isEmpty() )
00255           setXfaceFromFile( url );
00256         else
00257           KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") );
00258       }
00259     }
00260     else
00261       KMessageBox::information( this, i18n("You do not have your own contact defined in the address book."), i18n("No Picture") );
00262   }
00263 
00264   void XFaceConfigurator::slotUpdateXFace()
00265   {
00266     QString str = mTextEdit->text();
00267     if ( !str.isEmpty() )
00268     {
00269       if ( str.startsWith("x-face:", false) )
00270       {
00271         str = str.remove("x-face:", false);
00272         mTextEdit->setText(str);
00273       }
00274       KXFace xf;
00275       QPixmap p( 48, 48, true );
00276       p.convertFromImage( xf.toImage(str) );
00277       mXFaceLabel->setPixmap( p );
00278     }
00279     else
00280       mXFaceLabel->setPixmap( 0L );
00281   }
00282 
00283 } // namespace KMail
00284 
00285 #include "xfaceconfigurator.moc"