• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

kpimidentities

  • kpimidentities
identity.cpp
1 /*
2  Copyright (c) 2002-2004 Marc Mutz <mutz@kde.org>
3  Copyright (c) 2007 Tom Albers <tomalbers@kde.nl>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "identity.h"
22 #include "signature.h"
23 
24 #include <kdeversion.h>
25 #include <sonnet/globals.h>
26 #include <kdebug.h>
27 #include <klocale.h>
28 #include <kmessagebox.h>
29 #include <kconfiggroup.h>
30 #include <kurl.h>
31 #include <kprocess.h>
32 #include <kpimutils/kfileio.h>
33 #include <kpimutils/email.h>
34 
35 #include <QFileInfo>
36 #include <QMimeData>
37 #include <QByteArray>
38 
39 #include <sys/types.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #include <assert.h>
44 
45 using namespace KPIMIdentities;
46 
47 // TODO: should use a kstaticdeleter?
48 static Identity *identityNull = 0;
49 
50 Identity::Identity( const QString &id, const QString &fullName,
51  const QString &emailAddr, const QString &organization,
52  const QString &replyToAddr )
53  : mIsDefault( false )
54 {
55  setProperty( s_uoid, 0 );
56  setProperty( s_identity, id );
57  setProperty( s_name, fullName );
58  setProperty( s_email, emailAddr );
59  setProperty( s_organization, organization );
60  setProperty( s_replyto, replyToAddr );
61  setDictionary( Sonnet::defaultLanguageName() );
62 }
63 
64 Identity::~Identity()
65 {}
66 
67 const Identity &Identity::null()
68 {
69  if ( !identityNull ) {
70  identityNull = new Identity;
71  }
72  return *identityNull;
73 }
74 
75 bool Identity::isNull() const
76 {
77  bool empty = true;
78  QHash<QString, QVariant>::const_iterator i = mPropertiesMap.constBegin();
79  while ( i != mPropertiesMap.constEnd() ) {
80 
81  // Take into account that the dictionary for a null identity is not empty
82  if ( i.key() == s_dict ) {
83  ++i;
84  continue;
85  }
86 
87  // The uoid is 0 by default, so ignore this
88  if ( !( i.key() == s_uoid && i.value().toUInt() == 0 ) ) {
89  if ( !i.value().isNull() ||
90  ( i.value().type() == QVariant::String && !i.value().toString().isEmpty() ) ) {
91  empty = false;
92  }
93  }
94  ++i;
95  }
96  return empty;
97 }
98 
99 void Identity::readConfig( const KConfigGroup &config )
100 {
101  // get all keys and convert them to our QHash.
102  QMap<QString, QString> entries = config.entryMap();
103  QMap<QString, QString>::const_iterator i = entries.constBegin();
104  QMap<QString, QString>::const_iterator end = entries.constEnd();
105  while ( i != end ) {
106  if ( i.key() == s_emailAliases ) {
107  // HACK: Read s_emailAliases as a stringlist
108  mPropertiesMap.insert( i.key(), config.readEntry( i.key(), QStringList() ) );
109  } else {
110  mPropertiesMap.insert( i.key(), config.readEntry( i.key() ) );
111  }
112  ++i;
113  }
114  mSignature.readConfig( config );
115 }
116 
117 void Identity::writeConfig( KConfigGroup &config ) const
118 {
119  QHash<QString, QVariant>::const_iterator i = mPropertiesMap.constBegin();
120  QHash<QString, QVariant>::const_iterator end = mPropertiesMap.constEnd();
121  while ( i != end ) {
122  config.writeEntry( i.key(), i.value() );
123  kDebug( 5325 ) << "Store:" << i.key() << ":" << i.value();
124  ++i;
125  }
126  mSignature.writeConfig( config );
127 }
128 
129 bool Identity::mailingAllowed() const
130 {
131  return !property( s_email ).toString().isEmpty();
132 }
133 
134 QString Identity::mimeDataType()
135 {
136  return "application/x-kmail-identity-drag";
137 }
138 
139 bool Identity::canDecode( const QMimeData*md )
140 {
141  return md->hasFormat( mimeDataType() );
142 }
143 
144 void Identity::populateMimeData( QMimeData*md )
145 {
146  QByteArray a;
147  {
148  QDataStream s( &a, QIODevice::WriteOnly );
149  s << this;
150  }
151  md->setData( mimeDataType(), a );
152 }
153 
154 Identity Identity::fromMimeData( const QMimeData*md )
155 {
156  Identity i;
157  if ( canDecode( md ) ) {
158  QByteArray ba = md->data( mimeDataType() );
159  QDataStream s( &ba, QIODevice::ReadOnly );
160  s >> i;
161  }
162  return i;
163 }
164 
165 // ------------------ Operators --------------------------//
166 
167 QDataStream &KPIMIdentities::operator<<
168 ( QDataStream &stream, const KPIMIdentities::Identity &i )
169 {
170  return stream << static_cast<quint32>( i.uoid() )
171  << i.identityName()
172  << i.fullName()
173  << i.organization()
174  << i.pgpSigningKey()
175  << i.pgpEncryptionKey()
176  << i.smimeSigningKey()
177  << i.smimeEncryptionKey()
178  << i.primaryEmailAddress()
179  << i.emailAliases()
180  << i.replyToAddr()
181  << i.bcc()
182  << i.vCardFile()
183  << i.transport()
184  << i.fcc()
185  << i.drafts()
186  << i.templates()
187  << i.mPropertiesMap[s_signature]
188  << i.dictionary()
189  << i.xface()
190  << i.preferredCryptoMessageFormat()
191  << i.cc()
192  << i.attachVcard()
193  << i.autocorrectionLanguage();
194 
195 
196 }
197 
198 QDataStream &KPIMIdentities::operator>>
199 ( QDataStream &stream, KPIMIdentities::Identity &i )
200 {
201  quint32 uoid;
202  stream
203  >> uoid
204  >> i.mPropertiesMap[s_identity]
205  >> i.mPropertiesMap[s_name]
206  >> i.mPropertiesMap[s_organization]
207  >> i.mPropertiesMap[s_pgps]
208  >> i.mPropertiesMap[s_pgpe]
209  >> i.mPropertiesMap[s_smimes]
210  >> i.mPropertiesMap[s_smimee]
211  >> i.mPropertiesMap[s_email]
212  >> i.mPropertiesMap[s_emailAliases]
213  >> i.mPropertiesMap[s_replyto]
214  >> i.mPropertiesMap[s_bcc]
215  >> i.mPropertiesMap[s_vcard]
216  >> i.mPropertiesMap[s_transport]
217  >> i.mPropertiesMap[s_fcc]
218  >> i.mPropertiesMap[s_drafts]
219  >> i.mPropertiesMap[s_templates]
220  >> i.mPropertiesMap[s_signature]
221  >> i.mPropertiesMap[s_dict]
222  >> i.mPropertiesMap[s_xface]
223  >> i.mPropertiesMap[s_prefcrypt]
224  >> i.mPropertiesMap[s_cc]
225  >> i.mPropertiesMap[s_attachVcard]
226  >> i.mPropertiesMap[s_autocorrectionLanguage];
227 
228  i.setProperty( s_uoid, uoid );
229  return stream;
230 }
231 
232 bool Identity::operator< ( const Identity &other ) const
233 {
234  if ( isDefault() ) {
235  return true;
236  }
237  if ( other.isDefault() ) {
238  return false;
239  }
240  return identityName() < other.identityName();
241 }
242 
243 bool Identity::operator> ( const Identity &other ) const
244 {
245  if ( isDefault() ) {
246  return false;
247  }
248  if ( other.isDefault() ) {
249  return true;
250  }
251  return identityName() > other.identityName();
252 }
253 
254 bool Identity::operator<= ( const Identity &other ) const
255 {
256  return !operator> ( other );
257 }
258 
259 bool Identity::operator>= ( const Identity &other ) const
260 {
261  return !operator< ( other );
262 }
263 
264 bool Identity::operator== ( const Identity &other ) const
265 {
266  return mPropertiesMap == other.mPropertiesMap &&
267  mSignature == other.mSignature;
268 }
269 
270 bool Identity::operator!= ( const Identity &other ) const
271 {
272  return !operator== ( other );
273 }
274 
275 // --------------------- Getters -----------------------------//
276 
277 QVariant Identity::property( const QString &key ) const
278 {
279  return mPropertiesMap.value( key );
280 }
281 
282 QString Identity::fullEmailAddr( void ) const
283 {
284  const QString name = mPropertiesMap.value( s_name ).toString();
285  const QString mail = mPropertiesMap.value( s_email ).toString();
286 
287  if ( name.isEmpty() ) {
288  return mail;
289  }
290 
291  const QString specials( "()<>@,.;:[]" );
292 
293  QString result;
294 
295  // add DQUOTE's if necessary:
296  bool needsQuotes=false;
297  const int nameLength( name.length() );
298  for ( int i=0; i < nameLength; i++ ) {
299  if ( specials.contains( name[i] ) ) {
300  needsQuotes = true;
301  } else if ( name[i] == '\\' || name[i] == '"' ) {
302  needsQuotes = true;
303  result += '\\';
304  }
305  result += name[i];
306  }
307 
308  if ( needsQuotes ) {
309  result.insert( 0, '"' );
310  result += '"';
311  }
312 
313  result += " <" + mail + '>';
314 
315  return result;
316 }
317 
318 QString Identity::identityName() const
319 {
320  return property( QLatin1String( s_identity ) ).toString();
321 }
322 
323 QString Identity::signatureText( bool *ok ) const
324 {
325  return mSignature.withSeparator( ok );
326 }
327 
328 bool Identity::signatureIsInlinedHtml() const
329 {
330  return mSignature.isInlinedHtml();
331 }
332 
333 bool Identity::isDefault() const
334 {
335  return mIsDefault;
336 }
337 
338 uint Identity::uoid() const
339 {
340  return property( QLatin1String( s_uoid ) ).toInt();
341 }
342 
343 QString Identity::fullName() const
344 {
345  return property( QLatin1String( s_name ) ).toString();
346 }
347 
348 QString Identity::organization() const
349 {
350  return property( QLatin1String( s_organization ) ).toString();
351 }
352 
353 QByteArray Identity::pgpEncryptionKey() const
354 {
355  return property( QLatin1String( s_pgpe ) ).toByteArray();
356 }
357 
358 QByteArray Identity::pgpSigningKey() const
359 {
360  return property( QLatin1String( s_pgps ) ).toByteArray();
361 }
362 
363 QByteArray Identity::smimeEncryptionKey() const
364 {
365  return property( QLatin1String( s_smimee ) ).toByteArray();
366 }
367 
368 QByteArray Identity::smimeSigningKey() const
369 {
370  return property( QLatin1String( s_smimes ) ).toByteArray();
371 }
372 
373 QString Identity::preferredCryptoMessageFormat() const
374 {
375  return property( QLatin1String( s_prefcrypt ) ).toString();
376 }
377 
378 QString Identity::emailAddr() const
379 {
380  return primaryEmailAddress();
381 }
382 
383 QString Identity::primaryEmailAddress() const
384 {
385  return property( QLatin1String( s_email ) ).toString();
386 }
387 
388 const QStringList Identity::emailAliases() const
389 {
390  return property( QLatin1String( s_emailAliases ) ).toStringList();
391 }
392 
393 QString Identity::vCardFile() const
394 {
395  return property( QLatin1String( s_vcard ) ).toString();
396 }
397 
398 bool Identity::attachVcard() const
399 {
400  return property( QLatin1String( s_attachVcard ) ).toBool();
401 }
402 
403 QString Identity::replyToAddr() const
404 {
405  return property( QLatin1String( s_replyto ) ).toString();
406 }
407 
408 QString Identity::bcc() const
409 {
410  return property( QLatin1String( s_bcc ) ).toString();
411 }
412 
413 QString Identity::cc() const
414 {
415  return property( QLatin1String( s_cc ) ).toString();
416 }
417 
418 Signature &Identity::signature()
419 {
420  return mSignature;
421 }
422 
423 bool Identity::isXFaceEnabled() const
424 {
425  return property( QLatin1String( s_xfaceenabled ) ).toBool();
426 }
427 
428 QString Identity::xface() const
429 {
430  return property( QLatin1String( s_xface ) ).toString();
431 }
432 
433 QString Identity::dictionary() const
434 {
435  return property( QLatin1String( s_dict ) ).toString();
436 }
437 
438 QString Identity::templates() const
439 {
440  const QString str = property( QLatin1String( s_templates ) ).toString();
441  return verifyAkonadiId(str);
442 }
443 
444 QString Identity::drafts() const
445 {
446  const QString str = property( QLatin1String( s_drafts ) ).toString();
447  return verifyAkonadiId(str);
448 }
449 
450 QString Identity::fcc() const
451 {
452  const QString str = property( QLatin1String( s_fcc ) ).toString();
453  return verifyAkonadiId(str);
454 }
455 
456 QString Identity::transport() const
457 {
458  return property( QLatin1String( s_transport ) ).toString();
459 }
460 
461 bool Identity::signatureIsCommand() const
462 {
463  return mSignature.type() == Signature::FromCommand;
464 }
465 
466 bool Identity::signatureIsPlainFile() const
467 {
468  return mSignature.type() == Signature::FromFile;
469 }
470 
471 bool Identity::signatureIsInline() const
472 {
473  return mSignature.type() == Signature::Inlined;
474 }
475 
476 bool Identity::useSignatureFile() const
477 {
478  return signatureIsPlainFile() || signatureIsCommand();
479 }
480 
481 QString Identity::signatureInlineText() const
482 {
483  return mSignature.text();
484 }
485 
486 QString Identity::signatureFile() const
487 {
488  return mSignature.url();
489 }
490 
491 QString Identity::autocorrectionLanguage() const
492 {
493  return property( QLatin1String( s_autocorrectionLanguage ) ).toString();
494 }
495 
496 // --------------------- Setters -----------------------------//
497 
498 void Identity::setProperty( const QString &key, const QVariant &value )
499 {
500  if ( value.isNull() ||
501  ( value.type() == QVariant::String && value.toString().isEmpty() ) ) {
502  mPropertiesMap.remove( key );
503  } else {
504  mPropertiesMap.insert( key, value );
505  }
506 }
507 
508 void Identity::setUoid( uint aUoid )
509 {
510  setProperty( s_uoid, aUoid );
511 }
512 
513 void Identity::setIdentityName( const QString &name )
514 {
515  setProperty( s_identity, name );
516 }
517 
518 void Identity::setFullName( const QString &str )
519 {
520  setProperty( s_name, str );
521 }
522 
523 void Identity::setOrganization( const QString &str )
524 {
525  setProperty( s_organization, str );
526 }
527 
528 void Identity::setPGPSigningKey( const QByteArray &str )
529 {
530  setProperty( s_pgps, QString( str ) );
531 }
532 
533 void Identity::setPGPEncryptionKey( const QByteArray &str )
534 {
535  setProperty( s_pgpe, QString( str ) );
536 }
537 
538 void Identity::setSMIMESigningKey( const QByteArray &str )
539 {
540  setProperty( s_smimes, QString( str ) );
541 }
542 
543 void Identity::setSMIMEEncryptionKey( const QByteArray &str )
544 {
545  setProperty( s_smimee, QString( str ) );
546 }
547 
548 void Identity::setEmailAddr( const QString &str )
549 {
550  setPrimaryEmailAddress( str );
551 }
552 
553 void Identity::setPrimaryEmailAddress( const QString & email )
554 {
555  setProperty( s_email, email );
556 }
557 
558 void Identity::setEmailAliases( const QStringList & aliases )
559 {
560  setProperty( s_emailAliases, aliases );
561 }
562 
563 void Identity::setVCardFile( const QString &str )
564 {
565  setProperty( s_vcard, str );
566 }
567 
568 void Identity::setAttachVcard(bool attachment)
569 {
570  setProperty( s_attachVcard, attachment );
571 }
572 
573 void Identity::setReplyToAddr( const QString&str )
574 {
575  setProperty( s_replyto, str );
576 }
577 
578 void Identity::setSignatureFile( const QString &str )
579 {
580  mSignature.setUrl( str, signatureIsCommand() );
581 }
582 
583 void Identity::setSignatureInlineText( const QString &str )
584 {
585  mSignature.setText( str );
586 }
587 
588 void Identity::setTransport( const QString &str )
589 {
590  setProperty( s_transport, str );
591 }
592 
593 void Identity::setFcc( const QString &str )
594 {
595  setProperty( s_fcc, str );
596 }
597 
598 void Identity::setDrafts( const QString &str )
599 {
600  setProperty( s_drafts, str );
601 }
602 
603 void Identity::setTemplates( const QString &str )
604 {
605  setProperty( s_templates, str );
606 }
607 
608 void Identity::setDictionary( const QString &str )
609 {
610  setProperty( s_dict, str );
611 }
612 
613 void Identity::setBcc( const QString &str )
614 {
615  setProperty( s_bcc, str );
616 }
617 
618 void Identity::setCc( const QString &str )
619 {
620  setProperty( s_cc, str );
621 }
622 
623 void Identity::setIsDefault( bool flag )
624 {
625  mIsDefault = flag;
626 }
627 
628 void Identity::setPreferredCryptoMessageFormat( const QString &str )
629 {
630  setProperty( s_prefcrypt, str );
631 }
632 
633 void Identity::setXFace( const QString &str )
634 {
635  QString strNew = str;
636  strNew.remove( ' ' );
637  strNew.remove( '\n' );
638  strNew.remove( '\r' );
639  setProperty( s_xface, strNew );
640 }
641 
642 void Identity::setXFaceEnabled( const bool on )
643 {
644  setProperty( s_xfaceenabled, on );
645 }
646 
647 void Identity::setSignature( const Signature &sig )
648 {
649  mSignature = sig;
650 }
651 
652 bool Identity::matchesEmailAddress( const QString & addr ) const
653 {
654  const QString addrSpec = KPIMUtils::extractEmailAddress( addr ).toLower();
655  if ( addrSpec == primaryEmailAddress().toLower() ) {
656  return true;
657  }
658 
659  foreach ( const QString &alias, emailAliases() ) {
660  if ( alias.toLower() == addrSpec ) {
661  return true;
662  }
663  }
664 
665  return false;
666 }
667 
668 QString Identity::verifyAkonadiId(const QString& str) const
669 {
670  if(str.isEmpty())
671  return str;
672  bool ok = false;
673  const qlonglong val = str.toLongLong(&ok);
674  Q_UNUSED(val);
675  if(ok) {
676  return str;
677  } else {
678  return QString();
679  }
680 }
681 
682 void Identity::setAutocorrectionLanguage(const QString& language)
683 {
684  setProperty( s_autocorrectionLanguage, language );
685 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:30:05 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kpimidentities

Skip menu "kpimidentities"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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