kmail

xfaceconfigurator.cpp
1 /*
2  This file is part of KMail.
3 
4  Copyright (c) 2004 Jakob Schröter <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 TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), 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  TQt. 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 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36 
37 #include "xfaceconfigurator.h"
38 
39 #include <kactivelabel.h>
40 #include <kdialog.h>
41 #include <tdefiledialog.h>
42 #include <tdeglobalsettings.h>
43 #include <kimageio.h>
44 #include <tdelocale.h>
45 #include <tdemessagebox.h>
46 #include <kurl.h>
47 #include <tdeio/netaccess.h>
48 using namespace TDEIO;
49 #include <kxface.h>
50 using namespace KPIM;
51 #include <tdeabc/stdaddressbook.h>
52 #include <tdeabc/addressee.h>
53 using namespace TDEABC;
54 
55 #include <tqbitmap.h>
56 #include <tqcheckbox.h>
57 #include <tqcombobox.h>
58 #include <tqimage.h>
59 #include <tqlabel.h>
60 #include <tqlayout.h>
61 #include <tqpushbutton.h>
62 #include <tqwhatsthis.h>
63 #include <tqwidgetstack.h>
64 
65 
66 // #include <assert.h>
67 
68 using namespace KMail;
69 using namespace KPIM;
70 
71 namespace KMail {
72 
73  XFaceConfigurator::XFaceConfigurator( TQWidget * parent, const char * name )
74  : TQWidget( parent, name )
75  {
76  // tmp. vars:
77  TQLabel * label;
78  TQLabel * label1;
79  KActiveLabel * label2;
80  TQWidget * page;
81  TQVBoxLayout * vlay;
82  TQHBoxLayout * hlay;
83  TQVBoxLayout * page_vlay;
84  TQPushButton * mFromFileBtn;
85  TQPushButton * mFromAddrbkBtn;
86 
87  vlay = new TQVBoxLayout( this, 0, KDialog::spacingHint(), "main layout" );
88  hlay = new TQHBoxLayout( vlay );
89 
90  // "enable X-Face" checkbox:
91  mEnableCheck = new TQCheckBox( i18n("&Send picture with every message"), this );
92  TQWhatsThis::add( mEnableCheck,
93  i18n( "Check this box if you want KMail to add a so-called X-Face header to messages "
94  "written with this identity. An X-Face is a small (48x48 pixels) black and "
95  "white image that some mail clients are able to display." ) );
96  hlay->addWidget( mEnableCheck, TQt::AlignLeft | TQt::AlignVCenter );
97 
98  mXFaceLabel = new TQLabel( this );
99  TQWhatsThis::add(mXFaceLabel,
100  i18n( "This is a preview of the picture selected/entered below." ) );
101  mXFaceLabel->setFixedSize(48, 48);
102  mXFaceLabel->setFrameShape( TQFrame::Box );
103  hlay->addWidget( mXFaceLabel );
104 
105 // label1 = new TQLabel( "X-Face:", this );
106 // vlay->addWidget( label1 );
107 
108  // "obtain X-Face from" combo and label:
109  hlay = new TQHBoxLayout( vlay ); // inherits spacing
110  mSourceCombo = new TQComboBox( false, this );
111  TQWhatsThis::add(mSourceCombo,
112  i18n("Click on the widgets below to obtain help on the input methods."));
113  mSourceCombo->setEnabled( false ); // since !mEnableCheck->isChecked()
114  mSourceCombo->insertStringList( TQStringList()
115  << i18n( "continuation of \"obtain picture from\"",
116  "External Source" )
117  << i18n( "continuation of \"obtain picture from\"",
118  "Input Field Below" ) );
119  label = new TQLabel( mSourceCombo,
120  i18n("Obtain pic&ture from:"), this );
121  label->setEnabled( false ); // since !mEnableCheck->isChecked()
122  hlay->addWidget( label );
123  hlay->addWidget( mSourceCombo, 1 );
124 
125  // widget stack that is controlled by the source combo:
126  TQWidgetStack * widgetStack = new TQWidgetStack( this );
127  widgetStack->setEnabled( false ); // since !mEnableCheck->isChecked()
128  vlay->addWidget( widgetStack, 1 );
129  connect( mSourceCombo, TQ_SIGNAL(highlighted(int)),
130  widgetStack, TQ_SLOT(raiseWidget(int)) );
131  connect( mEnableCheck, TQ_SIGNAL(toggled(bool)),
132  mSourceCombo, TQ_SLOT(setEnabled(bool)) );
133  connect( mEnableCheck, TQ_SIGNAL(toggled(bool)),
134  widgetStack, TQ_SLOT(setEnabled(bool)) );
135  connect( mEnableCheck, TQ_SIGNAL(toggled(bool)),
136  label, TQ_SLOT(setEnabled(bool)) );
137  // The focus might be still in the widget that is disabled
138  connect( mEnableCheck, TQ_SIGNAL(clicked()),
139  mEnableCheck, TQ_SLOT(setFocus()) );
140 
141  int pageno = 0;
142  // page 0: create X-Face from image file or address book entry
143  page = new TQWidget( widgetStack );
144  widgetStack->addWidget( page, pageno ); // force sequential numbers (play safe)
145  page_vlay = new TQVBoxLayout( page, 0, KDialog::spacingHint() );
146  hlay = new TQHBoxLayout( page_vlay ); // inherits spacing
147  mFromFileBtn = new TQPushButton( i18n("Select File..."), page );
148  TQWhatsThis::add( mFromFileBtn,
149  i18n("Use this to select an image file to create the picture from. "
150  "The image should be of high contrast and nearly quadratic shape. "
151  "A light background helps improve the result." ) );
152  mFromFileBtn->setAutoDefault( false );
153  page_vlay->addWidget( mFromFileBtn, 1 );
154  connect( mFromFileBtn, TQ_SIGNAL(released()),
155  this, TQ_SLOT(slotSelectFile()) );
156  mFromAddrbkBtn = new TQPushButton( i18n("Set From Address Book"), page );
157  TQWhatsThis::add( mFromAddrbkBtn,
158  i18n( "You can use a scaled-down version of the picture "
159  "you have set in your address book entry." ) );
160  mFromAddrbkBtn->setAutoDefault( false );
161  page_vlay->addWidget( mFromAddrbkBtn, 1 );
162  connect( mFromAddrbkBtn, TQ_SIGNAL(released()),
163  this, TQ_SLOT(slotSelectFromAddressbook()) );
164  label1 = new TQLabel( i18n("<qt>KMail can send a small (48x48 pixels), low-quality, "
165  "monochrome picture with every message. "
166  "For example, this could be a picture of you or a glyph. "
167  "It is shown in the recipient's mail client (if supported)." ), page );
168  label1->setAlignment( TQLabel::WordBreak | TQLabel::AlignVCenter );
169  page_vlay->addWidget( label1 );
170 
171  widgetStack->raiseWidget( 0 ); // since mSourceCombo->currentItem() == 0
172 
173  // page 1: input field for direct entering
174  ++pageno;
175  page = new TQWidget( widgetStack );
176  widgetStack->addWidget( page, pageno );
177  page_vlay = new TQVBoxLayout( page, 0, KDialog::spacingHint() );
178  mTextEdit = new TQTextEdit( page );
179  page_vlay->addWidget( mTextEdit );
180  TQWhatsThis::add( mTextEdit, i18n( "Use this field to enter an arbitrary X-Face string." ) );
181  mTextEdit->setFont( TDEGlobalSettings::fixedFont() );
182  mTextEdit->setWrapPolicy( TQTextEdit::Anywhere );
183  mTextEdit->setTextFormat( TQt::PlainText );
184  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 );
185  page_vlay->addWidget( label2 );
186 
187 
188  connect(mTextEdit, TQ_SIGNAL(textChanged()), this, TQ_SLOT(slotUpdateXFace()));
189  }
190 
191  XFaceConfigurator::~XFaceConfigurator() {
192 
193  }
194 
195  bool XFaceConfigurator::isXFaceEnabled() const {
196  return mEnableCheck->isChecked();
197  }
198 
199  void XFaceConfigurator::setXFaceEnabled( bool enable ) {
200  mEnableCheck->setChecked( enable );
201  }
202 
203  TQString XFaceConfigurator::xface() const {
204  return mTextEdit->text();
205  }
206 
207  void XFaceConfigurator::setXFace( const TQString & text ) {
208  mTextEdit->setText( text );
209  }
210 
211  void XFaceConfigurator::setXfaceFromFile( const KURL &url )
212  {
213  TQString tmpFile;
214  if( TDEIO::NetAccess::download( url, tmpFile, this ) )
215  {
216  KXFace xf;
217  mTextEdit->setText( xf.fromImage( TQImage( tmpFile ) ) );
218  TDEIO::NetAccess::removeTempFile( tmpFile );
219  } else {
220  KMessageBox::error(this, TDEIO::NetAccess::lastErrorString() );
221  }
222  }
223 
224  void XFaceConfigurator::slotSelectFile()
225  {
226  TQStringList mimeTypes = KImageIO::mimeTypes (KImageIO::Reading);
227  TQString filter = mimeTypes.join (" ");
228  KURL url = KFileDialog::getOpenURL( TQString(), filter, this, TQString() );
229  if ( !url.isEmpty() )
230  setXfaceFromFile( url );
231  }
232 
233  void XFaceConfigurator::slotSelectFromAddressbook()
234  {
235  StdAddressBook *ab = StdAddressBook::self( true );
236  Addressee me = ab->whoAmI();
237  if ( !me.isEmpty() )
238  {
239  if ( me.photo().isIntern() )
240  {
241  TQImage photo = me.photo().data();
242  if ( !photo.isNull() )
243  {
244  KXFace xf;
245  mTextEdit->setText( xf.fromImage( photo ) );
246  }
247  else
248  KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") );
249 
250  }
251  else
252  {
253  KURL url = me.photo().url();
254  if( !url.isEmpty() )
255  setXfaceFromFile( url );
256  else
257  KMessageBox::information( this, i18n("No picture set for your address book entry."), i18n("No Picture") );
258  }
259  }
260  else
261  KMessageBox::information( this, i18n("You do not have your own contact defined in the address book."), i18n("No Picture") );
262  }
263 
264  void XFaceConfigurator::slotUpdateXFace()
265  {
266  TQString str = mTextEdit->text();
267  if ( !str.isEmpty() )
268  {
269  if ( str.startsWith("x-face:", false) )
270  {
271  str = str.remove("x-face:", false);
272  mTextEdit->setText(str);
273  }
274  KXFace xf;
275  TQPixmap p( 48, 48, true );
276  p.convertFromImage( xf.toImage(str) );
277  mXFaceLabel->setPixmap( p );
278  }
279  else
280  mXFaceLabel->setPixmap( 0L );
281  }
282 
283 } // namespace KMail
284 
285 #include "xfaceconfigurator.moc"
folderdiaquotatab.h
Definition: aboutdata.cpp:40