1/*
2 Copyright (c) 2010 Laurent Montel <montel@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "identity.h"
21#include "transport.h"
22
23#include <kpimidentities/identitymanager.h>
24#include <kpimidentities/identity.h>
25
26#include <KLocalizedString>
27
28Identity::Identity( QObject *parent )
29 : SetupObject( parent ),
30 m_transport( 0 )
31{
32 m_manager = new KPIMIdentities::IdentityManager( false, this, "mIdentityManager" );
33 m_identity = &m_manager->newFromScratch( QString() );
34 Q_ASSERT( m_identity != 0 );
35}
36
37Identity::~Identity()
38{
39 delete m_manager;
40}
41
42void Identity::create()
43{
44 emit info( i18n( "Setting up identity..." ) );
45
46 // store identity information
47 // TODO now that we have the identity object around anyway we can probably get rid of most of the other members
48 m_identity->setIdentityName( identityName() );
49 m_identity->setFullName( m_realName );
50 m_identity->setPrimaryEmailAddress( m_email );
51 m_identity->setOrganization( m_organization );
52 if ( m_transport && m_transport->transportId() > 0 )
53 m_identity->setTransport( QString::number( m_transport->transportId() ) );
54 if ( !m_signature.isEmpty() ) {
55 const KPIMIdentities::Signature sig( m_signature );
56 m_identity->setSignature( sig );
57 }
58 if ( !m_prefCryptoFormat.isEmpty() )
59 m_identity->setPreferredCryptoMessageFormat( m_prefCryptoFormat );
60 if ( !m_xface.isEmpty() ) {
61 m_identity->setXFaceEnabled( true );
62 m_identity->setXFace( m_xface );
63 }
64 m_manager->setAsDefault( m_identity->uoid() );
65 m_manager->commit();
66
67 emit finished( i18n( "Identity set up." ) );
68}
69
70QString Identity::identityName() const
71{
72 // create identity name
73 QString name( m_identityName );
74 if ( name.isEmpty() ) {
75 name = i18nc( "Default name for new email accounts/identities.", "Unnamed" );
76
77 QString idName = m_email;
78 int pos = idName.indexOf( QLatin1Char('@') );
79 if ( pos != -1 ) {
80 name = idName.mid( 0, pos );
81 }
82
83 // Make the name a bit more human friendly
84 name.replace( QLatin1Char('.'), QLatin1Char(' ') );
85 pos = name.indexOf( QLatin1Char(' ') );
86 if ( pos != 0 ) {
87 name[ pos + 1 ] = name[ pos + 1 ].toUpper();
88 }
89 name[ 0 ] = name[ 0 ].toUpper();
90 }
91
92 if ( !m_manager->isUnique( name ) ) {
93 name = m_manager->makeUnique( name );
94 }
95 return name;
96}
97
98
99void Identity::destroy()
100{
101 m_manager->removeIdentityForced( m_identity->identityName() );
102 m_manager->commit();
103 m_identity = 0;
104 emit info( i18n( "Identity removed." ) );
105}
106
107void Identity::setIdentityName(const QString& name)
108{
109 m_identityName = name;
110}
111
112void Identity::setRealName( const QString &name )
113{
114 m_realName = name;
115}
116
117void Identity::setOrganization( const QString &org )
118{
119 m_organization = org;
120}
121
122void Identity::setEmail( const QString &email )
123{
124 m_email = email;
125}
126
127uint Identity::uoid() const
128{
129 return m_identity->uoid();
130}
131
132void Identity::setTransport(QObject* transport)
133{
134 m_transport = qobject_cast<Transport*>( transport );
135 setDependsOn( qobject_cast<SetupObject*>( transport ) );
136}
137
138void Identity::setSignature(const QString& sig)
139{
140 m_signature = sig;
141}
142
143void Identity::setPreferredCryptoMessageFormat(const QString& format)
144{
145 m_prefCryptoFormat = format;
146}
147
148void Identity::setXFace(const QString& xface)
149{
150 m_xface = xface;
151}
152
153