• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.3 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 
193 }
194 
195 QDataStream &KPIMIdentities::operator>>
196 ( QDataStream &stream, KPIMIdentities::Identity &i )
197 {
198  quint32 uoid;
199  QString format;
200  stream
201  >> uoid
202  >> i.mPropertiesMap[s_identity]
203  >> i.mPropertiesMap[s_name]
204  >> i.mPropertiesMap[s_organization]
205  >> i.mPropertiesMap[s_pgps]
206  >> i.mPropertiesMap[s_pgpe]
207  >> i.mPropertiesMap[s_smimes]
208  >> i.mPropertiesMap[s_smimee]
209  >> i.mPropertiesMap[s_email]
210  >> i.mPropertiesMap[s_emailAliases]
211  >> i.mPropertiesMap[s_replyto]
212  >> i.mPropertiesMap[s_bcc]
213  >> i.mPropertiesMap[s_vcard]
214  >> i.mPropertiesMap[s_transport]
215  >> i.mPropertiesMap[s_fcc]
216  >> i.mPropertiesMap[s_drafts]
217  >> i.mPropertiesMap[s_templates]
218  >> i.mPropertiesMap[s_signature]
219  >> i.mPropertiesMap[s_dict]
220  >> i.mPropertiesMap[s_xface]
221  >> i.mPropertiesMap[s_prefcrypt]
222  >> i.mPropertiesMap[s_cc];
223 
224  i.setProperty( s_uoid, uoid );
225  return stream;
226 }
227 
228 bool Identity::operator< ( const Identity &other ) const
229 {
230  if ( isDefault() ) {
231  return true;
232  }
233  if ( other.isDefault() ) {
234  return false;
235  }
236  return identityName() < other.identityName();
237 }
238 
239 bool Identity::operator> ( const Identity &other ) const
240 {
241  if ( isDefault() ) {
242  return false;
243  }
244  if ( other.isDefault() ) {
245  return true;
246  }
247  return identityName() > other.identityName();
248 }
249 
250 bool Identity::operator<= ( const Identity &other ) const
251 {
252  return !operator> ( other );
253 }
254 
255 bool Identity::operator>= ( const Identity &other ) const
256 {
257  return !operator< ( other );
258 }
259 
260 bool Identity::operator== ( const Identity &other ) const
261 {
262  return mPropertiesMap == other.mPropertiesMap &&
263  mSignature == other.mSignature;
264 }
265 
266 bool Identity::operator!= ( const Identity &other ) const
267 {
268  return !operator== ( other );
269 }
270 
271 // --------------------- Getters -----------------------------//
272 
273 QVariant Identity::property( const QString &key ) const
274 {
275  return mPropertiesMap.value( key );
276 }
277 
278 QString Identity::fullEmailAddr( void ) const
279 {
280  const QString name = mPropertiesMap.value( s_name ).toString();
281  const QString mail = mPropertiesMap.value( s_email ).toString();
282 
283  if ( name.isEmpty() ) {
284  return mail;
285  }
286 
287  const QString specials( "()<>@,.;:[]" );
288 
289  QString result;
290 
291  // add DQUOTE's if necessary:
292  bool needsQuotes=false;
293  const int nameLength( name.length() );
294  for ( int i=0; i < nameLength; i++ ) {
295  if ( specials.contains( name[i] ) ) {
296  needsQuotes = true;
297  } else if ( name[i] == '\\' || name[i] == '"' ) {
298  needsQuotes = true;
299  result += '\\';
300  }
301  result += name[i];
302  }
303 
304  if ( needsQuotes ) {
305  result.insert( 0,'"' );
306  result += '"';
307  }
308 
309  result += " <" + mail + '>';
310 
311  return result;
312 }
313 
314 QString Identity::identityName() const
315 {
316  return property( QLatin1String( s_identity ) ).toString();
317 }
318 
319 QString Identity::signatureText( bool *ok ) const
320 {
321  return mSignature.withSeparator( ok );
322 }
323 
324 bool Identity::signatureIsInlinedHtml() const
325 {
326  return mSignature.isInlinedHtml();
327 }
328 
329 bool Identity::isDefault() const
330 {
331  return mIsDefault;
332 }
333 
334 uint Identity::uoid() const
335 {
336  return property( QLatin1String( s_uoid ) ).toInt();
337 }
338 
339 QString Identity::fullName() const
340 {
341  return property( QLatin1String( s_name ) ).toString();
342 }
343 
344 QString Identity::organization() const
345 {
346  return property( QLatin1String( s_organization ) ).toString();
347 }
348 
349 QByteArray Identity::pgpEncryptionKey() const
350 {
351  return property( QLatin1String( s_pgpe ) ).toByteArray();
352 }
353 
354 QByteArray Identity::pgpSigningKey() const
355 {
356  return property( QLatin1String( s_pgps ) ).toByteArray();
357 }
358 
359 QByteArray Identity::smimeEncryptionKey() const
360 {
361  return property( QLatin1String( s_smimee ) ).toByteArray();
362 }
363 
364 QByteArray Identity::smimeSigningKey() const
365 {
366  return property( QLatin1String( s_smimes ) ).toByteArray();
367 }
368 
369 QString Identity::preferredCryptoMessageFormat() const
370 {
371  return property( QLatin1String( s_prefcrypt ) ).toString();
372 }
373 
374 QString Identity::emailAddr() const
375 {
376  return primaryEmailAddress();
377 }
378 
379 QString Identity::primaryEmailAddress() const
380 {
381  return property( QLatin1String( s_email ) ).toString();
382 }
383 
384 const QStringList Identity::emailAliases() const
385 {
386  return property( QLatin1String( s_emailAliases ) ).toStringList();
387 }
388 
389 QString Identity::vCardFile() const
390 {
391  return property( QLatin1String( s_vcard ) ).toString();
392 }
393 
394 QString Identity::replyToAddr() const
395 {
396  return property( QLatin1String( s_replyto ) ).toString();
397 }
398 
399 QString Identity::bcc() const
400 {
401  return property( QLatin1String( s_bcc ) ).toString();
402 }
403 
404 QString Identity::cc() const
405 {
406  return property( QLatin1String( s_cc ) ).toString();
407 }
408 
409 Signature &Identity::signature()
410 {
411  return mSignature;
412 }
413 
414 bool Identity::isXFaceEnabled() const
415 {
416  return property( QLatin1String( s_xfaceenabled ) ).toBool();
417 }
418 
419 QString Identity::xface() const
420 {
421  return property( QLatin1String( s_xface ) ).toString();
422 }
423 
424 QString Identity::dictionary() const
425 {
426  return property( QLatin1String( s_dict ) ).toString();
427 }
428 
429 QString Identity::templates() const
430 {
431  const QString str = property( QLatin1String( s_templates ) ).toString();
432  return verifyAkonadiId(str);
433 }
434 
435 QString Identity::drafts() const
436 {
437  const QString str = property( QLatin1String( s_drafts ) ).toString();
438  return verifyAkonadiId(str);
439 }
440 
441 QString Identity::fcc() const
442 {
443  const QString str = property( QLatin1String( s_fcc ) ).toString();
444  return verifyAkonadiId(str);
445 }
446 
447 QString Identity::transport() const
448 {
449  return property( QLatin1String( s_transport ) ).toString();
450 }
451 
452 bool Identity::signatureIsCommand() const
453 {
454  return mSignature.type() == Signature::FromCommand;
455 }
456 
457 bool Identity::signatureIsPlainFile() const
458 {
459  return mSignature.type() == Signature::FromFile;
460 }
461 
462 bool Identity::signatureIsInline() const
463 {
464  return mSignature.type() == Signature::Inlined;
465 }
466 
467 bool Identity::useSignatureFile() const
468 {
469  return signatureIsPlainFile() || signatureIsCommand();
470 }
471 
472 QString Identity::signatureInlineText() const
473 {
474  return mSignature.text();
475 }
476 
477 QString Identity::signatureFile() const
478 {
479  return mSignature.url();
480 }
481 
482 // --------------------- Setters -----------------------------//
483 
484 void Identity::setProperty( const QString &key, const QVariant &value )
485 {
486  if ( value.isNull() ||
487  ( value.type() == QVariant::String && value.toString().isEmpty() ) ) {
488  mPropertiesMap.remove( key );
489  } else {
490  mPropertiesMap.insert( key, value );
491  }
492 }
493 
494 void Identity::setUoid( uint aUoid )
495 {
496  setProperty( s_uoid, aUoid );
497 }
498 
499 void Identity::setIdentityName( const QString &name )
500 {
501  setProperty( s_identity, name );
502 }
503 
504 void Identity::setFullName( const QString &str )
505 {
506  setProperty( s_name, str );
507 }
508 
509 void Identity::setOrganization( const QString &str )
510 {
511  setProperty( s_organization, str );
512 }
513 
514 void Identity::setPGPSigningKey( const QByteArray &str )
515 {
516  setProperty( s_pgps, QString( str ) );
517 }
518 
519 void Identity::setPGPEncryptionKey( const QByteArray &str )
520 {
521  setProperty( s_pgpe, QString( str ) );
522 }
523 
524 void Identity::setSMIMESigningKey( const QByteArray &str )
525 {
526  setProperty( s_smimes, QString( str ) );
527 }
528 
529 void Identity::setSMIMEEncryptionKey( const QByteArray &str )
530 {
531  setProperty( s_smimee, QString( str ) );
532 }
533 
534 void Identity::setEmailAddr( const QString &str )
535 {
536  setPrimaryEmailAddress( str );
537 }
538 
539 void Identity::setPrimaryEmailAddress( const QString & email )
540 {
541  setProperty( s_email, email );
542 }
543 
544 void Identity::setEmailAliases( const QStringList & aliases )
545 {
546  setProperty( s_emailAliases, aliases );
547 }
548 
549 void Identity::setVCardFile( const QString &str )
550 {
551  setProperty( s_vcard, str );
552 }
553 
554 void Identity::setReplyToAddr( const QString&str )
555 {
556  setProperty( s_replyto, str );
557 }
558 
559 void Identity::setSignatureFile( const QString &str )
560 {
561  mSignature.setUrl( str, signatureIsCommand() );
562 }
563 
564 void Identity::setSignatureInlineText( const QString &str )
565 {
566  mSignature.setText( str );
567 }
568 
569 void Identity::setTransport( const QString &str )
570 {
571  setProperty( s_transport, str );
572 }
573 
574 void Identity::setFcc( const QString &str )
575 {
576  setProperty( s_fcc, str );
577 }
578 
579 void Identity::setDrafts( const QString &str )
580 {
581  setProperty( s_drafts, str );
582 }
583 
584 void Identity::setTemplates( const QString &str )
585 {
586  setProperty( s_templates, str );
587 }
588 
589 void Identity::setDictionary( const QString &str )
590 {
591  setProperty( s_dict, str );
592 }
593 
594 void Identity::setBcc( const QString &str )
595 {
596  setProperty( s_bcc, str );
597 }
598 
599 void Identity::setCc( const QString &str )
600 {
601  setProperty( s_cc, str );
602 }
603 
604 
605 void Identity::setIsDefault( bool flag )
606 {
607  mIsDefault = flag;
608 }
609 
610 void Identity::setPreferredCryptoMessageFormat( const QString &str )
611 {
612  setProperty( s_prefcrypt, str );
613 }
614 
615 void Identity::setXFace( const QString &str )
616 {
617  QString strNew = str;
618  strNew.remove( ' ' );
619  strNew.remove( '\n' );
620  strNew.remove( '\r' );
621  setProperty( s_xface, strNew );
622 }
623 
624 void Identity::setXFaceEnabled( const bool on )
625 {
626  setProperty( s_xfaceenabled, on );
627 }
628 
629 void Identity::setSignature( const Signature &sig )
630 {
631  mSignature = sig;
632 }
633 
634 bool Identity::matchesEmailAddress( const QString & addr ) const
635 {
636  const QString addrSpec = KPIMUtils::extractEmailAddress( addr ).toLower();
637  if ( addrSpec == primaryEmailAddress().toLower() )
638  return true;
639 
640  foreach ( const QString &alias, emailAliases() ) {
641  if ( alias.toLower() == addrSpec )
642  return true;
643  }
644 
645  return false;
646 }
647 
648 QString Identity::verifyAkonadiId(const QString& str) const
649 {
650  if(str.isEmpty())
651  return str;
652  bool ok = false;
653  const qlonglong val = str.toLongLong(&ok);
654  Q_UNUSED(val);
655  if(ok) {
656  return str;
657  } else {
658  return QString();
659  }
660 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 22:01:07 by doxygen 1.8.1.2 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.9.3 API Reference

Skip menu "kdepimlibs-4.9.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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