~ubuntu-branches/ubuntu/raring/kdepim/raring-proposed

« back to all changes in this revision

Viewing changes to importwizard/sylpheed/sylpheedsettings.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-07 07:56:38 UTC
  • mfrom: (0.2.27)
  • Revision ID: package-import@ubuntu.com-20120607075638-0luhdq11z7sgvs4m
Tags: 4:4.8.80-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2012 Montel Laurent <montel@kde.org>
 
3
 
 
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.
 
7
 
 
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.
 
12
 
 
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
 
16
*/
 
17
 
 
18
#include "sylpheedsettings.h"
 
19
#include <mailtransport/transportmanager.h>
 
20
 
 
21
#include <kpimidentities/identity.h>
 
22
#include <kpimidentities/signature.h>
 
23
 
 
24
#include <KConfig>
 
25
#include <KConfigGroup>
 
26
 
 
27
#include <QRegExp>
 
28
#include <QStringList>
 
29
#include <QFile>
 
30
 
 
31
SylpheedSettings::SylpheedSettings( const QString& filename, const QString& sylpheedrc, ImportWizard *parent )
 
32
    :AbstractSettings( parent )
 
33
{
 
34
  bool checkMailOnStartup = true;
 
35
  if(QFile( sylpheedrc ).exists()) {
 
36
    KConfig configCommon( sylpheedrc );
 
37
    if(configCommon.hasGroup("Common")) {
 
38
      KConfigGroup common = configCommon.group("Common");
 
39
      checkMailOnStartup = ( common.readEntry("check_on_startup",1) == 1 );
 
40
      readGlobalSettings(common);
 
41
    }
 
42
  }
 
43
  KConfig config( filename );
 
44
  const QStringList accountList = config.groupList().filter( QRegExp( "Account: \\d+" ) );
 
45
  const QStringList::const_iterator end( accountList.constEnd() );
 
46
  for ( QStringList::const_iterator it = accountList.constBegin(); it!=end; ++it )
 
47
  {
 
48
    KConfigGroup group = config.group( *it );
 
49
    readAccount( group, checkMailOnStartup );
 
50
    readIdentity( group );
 
51
  }
 
52
}
 
53
 
 
54
SylpheedSettings::~SylpheedSettings()
 
55
{
 
56
}
 
57
 
 
58
void SylpheedSettings::readGlobalSettings(const KConfigGroup& group)
 
59
{
 
60
  const bool showTrayIcon = (group.readEntry("show_trayicon", 0) == 1 );
 
61
  addKmailConfig(QLatin1String("General"), QLatin1String("SystemTrayEnabled"), showTrayIcon);
 
62
 
 
63
  const bool cleanTrashOnExit = (group.readEntry("clean_trash_on_exit", 0) == 1 );
 
64
  addKmailConfig(QLatin1String("General"), QLatin1String("empty-trash-on-exit"), cleanTrashOnExit);
 
65
 
 
66
  const bool alwaysMarkReadOnShowMsg = (group.readEntry("always_mark_read_on_show_msg", 0) == 1 );
 
67
  if(alwaysMarkReadOnShowMsg) {
 
68
    addKmailConfig(QLatin1String("Behaviour"), QLatin1String("DelayedMarkAsRead"), true);
 
69
    addKmailConfig(QLatin1String("Behaviour"), QLatin1String("DelayedMarkTime"), 0);
 
70
  }
 
71
 
 
72
  if(group.readEntry("enable_autosave", 0) == 1 ) {
 
73
    const int autosaveInterval = group.readEntry("autosave_interval",5);
 
74
    addKmailConfig(QLatin1String("Composer"), QLatin1String("autosave"), autosaveInterval);
 
75
  }
 
76
  const bool checkAttach = (group.readEntry("check_attach", 0) == 1 );
 
77
  addKmailConfig(QLatin1String("Composer"), QLatin1String("showForgottenAttachmentWarning"), checkAttach);
 
78
 
 
79
  const QString attachStr = group.readEntry("check_attach_str");
 
80
  if(!attachStr.isEmpty()) {
 
81
    addKmailConfig(QLatin1String("Composer"), QLatin1String("attachment-keywords"), attachStr);
 
82
  }
 
83
 
 
84
  const int lineWrap = group.readEntry("linewrap_length", 80);
 
85
  addKmailConfig(QLatin1String("Composer"), QLatin1String("break-at"), lineWrap);
 
86
  addKmailConfig(QLatin1String("Composer"), QLatin1String("word-wrap"), true);
 
87
 
 
88
  const bool enableColor = group.readEntry("enable_color", false);
 
89
  if(enableColor) {
 
90
    const int colorLevel1 = group.readEntry("quote_level1_color", -1);
 
91
    if(colorLevel1!=-1) {
 
92
      //[Reader]  QuotedText1
 
93
    }
 
94
    const int colorLevel2 = group.readEntry("quote_level2_color", -1);
 
95
    if(colorLevel2!=-1) {
 
96
      //[Reader]  QuotedText2
 
97
 
 
98
    }
 
99
    const int colorLevel3 = group.readEntry("quote_level3_color", -1);
 
100
    if(colorLevel3!=-1) {
 
101
      //[Reader]  QuotedText3
 
102
 
 
103
    }
 
104
 
 
105
  }
 
106
}
 
107
 
 
108
void SylpheedSettings::readSignature( const KConfigGroup& accountConfig, KPIMIdentities::Identity* identity )
 
109
{
 
110
  KPIMIdentities::Signature signature;
 
111
  const int signatureType = accountConfig.readEntry("signature_type", 0 );
 
112
  switch( signatureType ) {
 
113
  case 0: //File
 
114
    signature.setType( KPIMIdentities::Signature::FromFile );
 
115
    signature.setUrl( accountConfig.readEntry("signature_path" ),false );
 
116
    break;
 
117
  case 1: //Output
 
118
    signature.setType( KPIMIdentities::Signature::FromCommand );
 
119
    signature.setUrl( accountConfig.readEntry("signature_path" ),true );
 
120
    break;
 
121
  case 2: //Text
 
122
    signature.setType( KPIMIdentities::Signature::Inlined );
 
123
    signature.setText( accountConfig.readEntry("signature_text" ) );
 
124
    break;
 
125
  default:
 
126
    qDebug()<<" signature type unknow :"<<signatureType;
 
127
  }
 
128
  //TODO  const bool signatureBeforeQuote = ( accountConfig.readEntry( "signature_before_quote", 0 ) == 1 ); not implemented in kmail
 
129
 
 
130
  identity->setSignature( signature );
 
131
}
 
132
 
 
133
bool SylpheedSettings::readConfig( const QString& key, const KConfigGroup& accountConfig, int& value, bool remove_underscore )
 
134
{
 
135
  QString cleanedKey( key );
 
136
  if ( remove_underscore )
 
137
    cleanedKey.remove( QLatin1Char( '_' ) );
 
138
  const QString useKey = QLatin1String( "set_" )+ cleanedKey;
 
139
  if ( accountConfig.hasKey( useKey ) && ( accountConfig.readEntry( useKey, 0 ) == 1 ) ) {
 
140
    value = accountConfig.readEntry( key,0 );
 
141
    return true;
 
142
  }
 
143
  return false;
 
144
}
 
145
 
 
146
 
 
147
bool SylpheedSettings::readConfig( const QString& key, const KConfigGroup& accountConfig, QString& value, bool remove_underscore )
 
148
{
 
149
  QString cleanedKey( key );
 
150
  if ( remove_underscore )
 
151
    cleanedKey.remove( QLatin1Char( '_' ) );
 
152
  const QString useKey = QLatin1String( "set_" )+ cleanedKey;
 
153
  if ( accountConfig.hasKey( useKey ) && ( accountConfig.readEntry( useKey, 0 ) == 1 ) ) {
 
154
    value = accountConfig.readEntry( key );
 
155
    return true;
 
156
  }
 
157
  return false;
 
158
}
 
159
 
 
160
void SylpheedSettings::readPop3Account( const KConfigGroup& accountConfig, bool checkMailOnStartup )
 
161
{
 
162
  QMap<QString, QVariant> settings;
 
163
  const QString host = accountConfig.readEntry("receive_server");
 
164
  settings.insert( QLatin1String( "Host" ), host );
 
165
  
 
166
  const QString name = accountConfig.readEntry( QLatin1String( "name" ) );
 
167
  const QString inbox = adaptFolder(accountConfig.readEntry(QLatin1String("inbox")));
 
168
  settings.insert(QLatin1String("TargetCollection"), inbox);
 
169
  int port = 0;
 
170
  if ( readConfig( QLatin1String( "pop_port" ), accountConfig, port, true ) )
 
171
    settings.insert( QLatin1String( "Port" ), port );
 
172
  if ( accountConfig.hasKey( QLatin1String( "ssl_pop" ) ) ) {
 
173
    const int sslPop = accountConfig.readEntry( QLatin1String( "ssl_pop" ), 0 );
 
174
    switch(sslPop) {
 
175
      case 0:
 
176
        //Nothing
 
177
        break;
 
178
      case 1:
 
179
        settings.insert( QLatin1String( "UseSSL" ), true );
 
180
        break;
 
181
      case 2:
 
182
        settings.insert( QLatin1String( "UseTLS" ), true );
 
183
        break;
 
184
      default:
 
185
        qDebug()<<" unknown ssl_pop value "<<sslPop;
 
186
    }
 
187
  }
 
188
  if ( accountConfig.hasKey( QLatin1String( "remove_mail" ) ) ){
 
189
    const bool removeMail = (accountConfig.readEntry( QLatin1String( "remove_mail" ), 1)==1);
 
190
    settings.insert(QLatin1String("LeaveOnServer"),removeMail);
 
191
  }
 
192
 
 
193
  if ( accountConfig.hasKey( QLatin1String( "message_leave_time" ) ) ){
 
194
    settings.insert( QLatin1String( "LeaveOnServerDays" ), accountConfig.readEntry( QLatin1String( "message_leave_time" ) ) );
 
195
  }
 
196
  const QString user = accountConfig.readEntry( QLatin1String( "user_id" ) );
 
197
  settings.insert( QLatin1String( "Login" ), user );
 
198
 
 
199
  const QString password = accountConfig.readEntry( QLatin1String( "password" ) );
 
200
  settings.insert( QLatin1String( "Password" ), password );
 
201
  
 
202
  //use_apop_auth
 
203
  if ( accountConfig.hasKey( QLatin1String( "use_apop_auth" ) ) ){
 
204
    const bool useApop = (accountConfig.readEntry( QLatin1String( "use_apop_auth" ), 1)==1);
 
205
    if(useApop) {
 
206
      settings.insert(QLatin1String( "AuthenticationMethod" ), MailTransport::Transport::EnumAuthenticationType::APOP);
 
207
    }
 
208
  }
 
209
 
 
210
  const QString agentIdentifyName = AbstractBase::createResource( "akonadi_pop3_resource", name, settings );
 
211
  addCheckMailOnStartup(agentIdentifyName,checkMailOnStartup);
 
212
}
 
213
 
 
214
void SylpheedSettings::readImapAccount( const KConfigGroup& accountConfig, bool checkMailOnStartup )
 
215
{
 
216
  QMap<QString, QVariant> settings;
 
217
  const QString name = accountConfig.readEntry( QLatin1String( "name" ) );
 
218
  const int sslimap = accountConfig.readEntry( QLatin1String( "ssl_imap" ), 0);
 
219
  switch(sslimap) {
 
220
  case 0:
 
221
    //None
 
222
    settings.insert( QLatin1String( "Safety" ), QLatin1String( "NONE" ) );
 
223
    break;
 
224
  case 1:
 
225
    //SSL
 
226
    settings.insert( QLatin1String( "Safety" ), QLatin1String( "SSL" ) );
 
227
    break;
 
228
  case 2:
 
229
    settings.insert( QLatin1String( "Safety" ), QLatin1String( "STARTTLS" ) );
 
230
    //TLS
 
231
  default:
 
232
    qDebug()<<" sslimap unknown "<<sslimap;
 
233
    break;
 
234
  }
 
235
 
 
236
  int port = 0;
 
237
  if ( readConfig( QLatin1String( "imap_port" ), accountConfig, port, true ) )
 
238
    settings.insert( QLatin1String( "ImapPort" ), port );
 
239
 
 
240
  QString trashFolder;
 
241
  if ( readConfig( QLatin1String( "trash_folder" ), accountConfig, trashFolder, false ) )
 
242
    settings.insert( QLatin1String( "TrashCollection" ), adaptFolderId( trashFolder ) );
 
243
 
 
244
  const int auth = accountConfig.readEntry(QLatin1String("imap_auth_method"),0);
 
245
  switch(auth) {
 
246
    case 0:
 
247
      break;
 
248
    case 1: //Login
 
249
      settings.insert(QLatin1String("Authentication"), MailTransport::Transport::EnumAuthenticationType::LOGIN);
 
250
      break;
 
251
    case 2: //Cram-md5
 
252
      settings.insert(QLatin1String("Authentication"),MailTransport::Transport::EnumAuthenticationType::CRAM_MD5);
 
253
      break;
 
254
    case 4: //Plain
 
255
      settings.insert(QLatin1String("Authentication"),MailTransport::Transport::EnumAuthenticationType::PLAIN);
 
256
    default:
 
257
      qDebug()<<" imap auth unknown "<<auth;
 
258
      break;
 
259
  }
 
260
  const QString password = accountConfig.readEntry( QLatin1String( "password" ) );
 
261
  settings.insert( QLatin1String( "Password" ), password );
 
262
 
 
263
  const QString agentIdentifyName = AbstractBase::createResource( "akonadi_imap_resource", name,settings );
 
264
  addCheckMailOnStartup(agentIdentifyName,checkMailOnStartup);
 
265
}
 
266
 
 
267
 
 
268
void SylpheedSettings::readAccount(const KConfigGroup& accountConfig , bool checkMailOnStartup)
 
269
{
 
270
  if ( accountConfig.hasKey( QLatin1String( "protocol" ) ) )
 
271
  {
 
272
    const int protocol = accountConfig.readEntry( QLatin1String( "protocol" ), 0 );
 
273
    switch( protocol )
 
274
    {
 
275
      case 0:
 
276
        readPop3Account( accountConfig, checkMailOnStartup );
 
277
        break;
 
278
      case 3:
 
279
        //imap
 
280
        readImapAccount(accountConfig, checkMailOnStartup);
 
281
        break;
 
282
      case 4:
 
283
        qDebug()<<" Add it when nntp resource will implemented";
 
284
        //news
 
285
        break;
 
286
      case 5:
 
287
        //local
 
288
        break;
 
289
      default:
 
290
        qDebug()<<" protocol not defined"<<protocol;
 
291
    }
 
292
  }  
 
293
}
 
294
  
 
295
void SylpheedSettings::readIdentity( const KConfigGroup& accountConfig )
 
296
{
 
297
  KPIMIdentities::Identity* identity  = createIdentity();
 
298
  const QString name = accountConfig.readEntry( QLatin1String( "name" ) );
 
299
  identity->setFullName( name );
 
300
  identity->setIdentityName( name );
 
301
  const QString organization = accountConfig.readEntry( QLatin1String( "organization" ), QString() );
 
302
  identity->setOrganization(organization);
 
303
  const QString email = accountConfig.readEntry( QLatin1String( "address" ) );
 
304
  identity->setPrimaryEmailAddress(email);
 
305
 
 
306
  QString value;
 
307
  if ( readConfig( QLatin1String("auto_bcc") , accountConfig, value, true ) )
 
308
    identity->setBcc(value);
 
309
  if ( readConfig( QLatin1String("auto_cc") , accountConfig, value, true ) )
 
310
    identity->setCc(value);
 
311
  if ( readConfig( QLatin1String("auto_replyto") , accountConfig, value, true ) )
 
312
    identity->setReplyToAddr(value);
 
313
  
 
314
  if ( readConfig( QLatin1String("daft_folder") , accountConfig, value, false ) )
 
315
    identity->setDrafts(adaptFolder(value));
 
316
 
 
317
  if ( readConfig( QLatin1String("sent_folder") , accountConfig, value, false ) )
 
318
    identity->setFcc(adaptFolder(value));
 
319
 
 
320
  const QString transportId = readTransport(accountConfig);
 
321
  if(!transportId.isEmpty())
 
322
  {
 
323
    identity->setTransport(transportId);
 
324
  }
 
325
  readSignature( accountConfig, identity );
 
326
  storeIdentity(identity);
 
327
}
 
328
  
 
329
QString SylpheedSettings::readTransport( const KConfigGroup& accountConfig )
 
330
{
 
331
  const QString smtpserver = accountConfig.readEntry("smtp_server");
 
332
  
 
333
  if(!smtpserver.isEmpty()) {
 
334
    MailTransport::Transport *mt = createTransport();
 
335
    mt->setName( smtpserver );
 
336
    mt->setHost(smtpserver);
 
337
    int port = 0;
 
338
    if ( readConfig( QLatin1String( "smtp_port" ), accountConfig, port, true ) )
 
339
      mt->setPort( port );
 
340
    const QString user = accountConfig.readEntry( QLatin1String( "smtp_user_id" ) );
 
341
    
 
342
    if ( !user.isEmpty() ) {
 
343
      mt->setUserName( user );
 
344
      mt->setRequiresAuthentication( true );
 
345
    }
 
346
    const QString password = accountConfig.readEntry( QLatin1String( "smtp_password" ) );
 
347
    if ( !password.isEmpty() ) {
 
348
      mt->setStorePassword( true );
 
349
      mt->setPassword( password );
 
350
    }
 
351
    if ( accountConfig.readEntry( QLatin1String( "use_smtp_auth" ), 0 )==1 ) {
 
352
      const int authMethod = accountConfig.readEntry( QLatin1String( "smtp_auth_method" ), 0 );
 
353
      switch( authMethod ) {
 
354
      case 0: //Automatic:
 
355
        mt->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::PLAIN); //????
 
356
        break;
 
357
      case 1: //Login
 
358
        mt->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::LOGIN);
 
359
        break;
 
360
      case 2: //Cram-MD5
 
361
        mt->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::CRAM_MD5);
 
362
        break;
 
363
      case 8: //Plain
 
364
        mt->setAuthenticationType(MailTransport::Transport::EnumAuthenticationType::PLAIN);
 
365
        break;
 
366
      default:
 
367
        qDebug()<<" smtp authentification unknown :"<<authMethod;
 
368
      }
 
369
    }
 
370
    const int sslSmtp = accountConfig.readEntry( QLatin1String( "ssl_smtp" ), 0 );
 
371
    switch( sslSmtp ) {
 
372
    case 0:
 
373
      mt->setEncryption( MailTransport::Transport::EnumEncryption::None ); 
 
374
      break;
 
375
    case 1:
 
376
      mt->setEncryption( MailTransport::Transport::EnumEncryption::SSL );
 
377
      break;
 
378
    case 2:
 
379
      mt->setEncryption( MailTransport::Transport::EnumEncryption::TLS );
 
380
      break;
 
381
    default:
 
382
      qDebug()<<" smtp ssl config unknown :"<<sslSmtp;
 
383
        
 
384
    }
 
385
    QString domainName;
 
386
    if ( readConfig( QLatin1String( "domain" ), accountConfig, domainName, false ) )
 
387
      mt->setLocalHostname( domainName );
 
388
 
 
389
    storeTransport( mt, true );
 
390
    return QString::number(mt->id()); //TODO verify
 
391
  }
 
392
  return QString();
 
393
}