2 Copyright (c) 2012-2013 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License, version 2, as
6 published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #include "importwizardutil.h"
19 #include <KSharedConfig>
20 #include <KConfigGroup>
23 #include <AkonadiCore/Tag>
24 #include <AkonadiCore/TagAttribute>
25 #include <AkonadiCore/TagCreateJob>
27 void ImportWizardUtil::mergeLdap(const ldapStruct
&ldap
)
29 KSharedConfigPtr ldapConfig
= KSharedConfig::openConfig(QLatin1String("kabldaprc"));
30 int numberOfLdapSelected
= 0;
32 if (ldapConfig
->hasGroup(QLatin1String("LDAP"))) {
33 grp
= ldapConfig
->group(QLatin1String("LDAP"));
34 numberOfLdapSelected
= grp
.readEntry(QLatin1String("NumSelectedHosts"), 0);
35 grp
.writeEntry(QLatin1String("NumSelectedHosts"), (numberOfLdapSelected
+ 1));
37 grp
= ldapConfig
->group(QLatin1String("LDAP"));
38 grp
.writeEntry(QLatin1String("NumSelectedHosts"), 1);
40 KConfigGroup ldapSeach
= ldapConfig
->group(QLatin1String("LDAPSearch"));
41 ldapSeach
.writeEntry(QLatin1String("SearchType"), 0);
43 const int port
= ldap
.port
;
45 grp
.writeEntry(QString::fromLatin1("SelectedPort%1").arg(numberOfLdapSelected
), port
);
47 grp
.writeEntry(QString::fromLatin1("SelectedHost%1").arg(numberOfLdapSelected
), ldap
.ldapUrl
.host());
49 grp
.writeEntry(QString::fromLatin1("SelectedSecurity%1").arg(numberOfLdapSelected
), QString::fromLatin1("SSL"));
50 } else if (ldap
.useTLS
) {
51 grp
.writeEntry(QString::fromLatin1("SelectedSecurity%1").arg(numberOfLdapSelected
), QString::fromLatin1("TLS"));
53 grp
.writeEntry(QString::fromLatin1("SelectedSecurity%1").arg(numberOfLdapSelected
), QString::fromLatin1("None"));
56 if (ldap
.saslMech
== QLatin1String("GSSAPI")) {
57 grp
.writeEntry(QString::fromLatin1("SelectedMech%1").arg(numberOfLdapSelected
), QString::fromLatin1("GSSAPI"));
58 grp
.writeEntry(QString::fromLatin1("SelectedAuth%1").arg(numberOfLdapSelected
), QString::fromLatin1("SASL"));
59 } else if (ldap
.saslMech
.isEmpty()) {
60 grp
.writeEntry(QString::fromLatin1("SelectedMech%1").arg(numberOfLdapSelected
), QString::fromLatin1("PLAIN"));
61 grp
.writeEntry(QString::fromLatin1("SelectedAuth%1").arg(numberOfLdapSelected
), QString::fromLatin1("Simple"));
63 qDebug() << " Mech SASL undefined" << ldap
.saslMech
;
65 grp
.writeEntry(QString::fromLatin1("SelectedVersion%1").arg(numberOfLdapSelected
), QString::number(3));
66 grp
.writeEntry(QString::fromLatin1("SelectedBind%1").arg(numberOfLdapSelected
), ldap
.dn
);
67 //TODO: Verify selectedbase
68 grp
.writeEntry(QString::fromLatin1("SelectedBase%1").arg(numberOfLdapSelected
), ldap
.ldapUrl
.path());
69 if (ldap
.timeout
!= -1) {
70 grp
.writeEntry(QString::fromLatin1("SelectedTimeLimit%1").arg(numberOfLdapSelected
), ldap
.timeout
);
72 if (ldap
.limit
!= -1) {
73 grp
.writeEntry(QString::fromLatin1("SelectedSizeLimit%1").arg(numberOfLdapSelected
), ldap
.limit
);
75 if (!ldap
.password
.isEmpty()) {
76 storeInKWallet(QString::fromLatin1("SelectedPwdBind%1").arg(numberOfLdapSelected
), ImportWizardUtil::Ldap
, ldap
.password
);
81 void ImportWizardUtil::addAkonadiTag(const QList
<tagStruct
> &tagList
)
83 for (int i
= 0; i
< tagList
.size(); ++i
) {
84 Akonadi::Tag
tag(tagList
.at(i
).name
);
85 if (tagList
.at(i
).color
.isValid()) {
86 tag
.attribute
<Akonadi::TagAttribute
>(Akonadi::AttributeEntity::AddIfMissing
)->setTextColor(tagList
.at(i
).color
);
88 new Akonadi::TagCreateJob(tag
);
92 void ImportWizardUtil::storeInKWallet(const QString
&name
, ImportWizardUtil::ResourceType type
, const QString
&password
)
94 KWallet::Wallet
*wallet
= 0;
97 wallet
= KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), 0);
98 if (wallet
&& wallet
->isOpen()) {
99 if (!wallet
->hasFolder(QLatin1String("imap"))) {
100 wallet
->createFolder(QLatin1String("imap"));
102 wallet
->setFolder(QLatin1String("imap"));
103 wallet
->writePassword(name
+ QLatin1String("rc"), password
);
107 wallet
= KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(), 0);
108 if (wallet
&& wallet
->isOpen()) {
109 if (!wallet
->hasFolder(QLatin1String("pop3"))) {
110 wallet
->createFolder(QLatin1String("pop3"));
112 wallet
->setFolder(QLatin1String("pop3"));
113 wallet
->writePassword(name
, password
);
117 wallet
= KWallet::Wallet::openWallet(KWallet::Wallet::LocalWallet(), 0);
118 if (wallet
&& wallet
->isOpen()) {
119 if (!wallet
->hasFolder(QLatin1String("ldapclient"))) {
120 wallet
->createFolder(QLatin1String("ldapclient"));
122 wallet
->setFolder(QLatin1String("ldapclient"));
123 wallet
->writePassword(name
, password
);