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

« back to all changes in this revision

Viewing changes to mailimporter/filter_mailapp.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
                          filter_mailapp.cxx  -  OS X Mail App import
 
3
                             -------------------
 
4
    copyright            : (C) 2004 by Chris Howells
 
5
    email                : howells@kde.org
 
6
 
 
7
    Derived from code by:
 
8
    copyright            : (C) 2003 by Laurence Anderson
 
9
    email                : l.d.anderson@warwick.ac.uk
 
10
 
 
11
 ***************************************************************************/
 
12
 
 
13
/***************************************************************************
 
14
 *                                                                         *
 
15
 *   This program is free software; you can redistribute it and/or modify  *
 
16
 *   it under the terms of the GNU General Public License as published by  *
 
17
 *   the Free Software Foundation; either version 2 of the License, or     *
 
18
 *   (at your option) any later version.                                   *
 
19
 *                                                                         *
 
20
 ***************************************************************************/
 
21
 
 
22
 
 
23
#include <klocale.h>
 
24
#include <kfiledialog.h>
 
25
#include <ktemporaryfile.h>
 
26
#include <kdebug.h>
 
27
 
 
28
#include "filter_mailapp.h"
 
29
 
 
30
using namespace MailImporter;
 
31
 
 
32
 
 
33
FilterMailApp::FilterMailApp() :
 
34
  Filter( i18n("Import From OS X Mail"),
 
35
          "Chris Howells<br /><br />Filter accelerated by Danny Kukawka )",
 
36
          i18n("<p><b>OS X Mail Import Filter</b></p>"
 
37
               "<p>This filter imports e-mails from the Mail client in Apple Mac OS X.</p>"))
 
38
{
 
39
}
 
40
 
 
41
FilterMailApp::~FilterMailApp()
 
42
{
 
43
}
 
44
 
 
45
 
 
46
 
 
47
void FilterMailApp::import()
 
48
{
 
49
  const QString directory = KFileDialog::getExistingDirectory( QDir::homePath(), filterInfo()->parent() );
 
50
  importMails( directory );
 
51
}
 
52
 
 
53
void FilterMailApp::importMails( const QString & maildir )
 
54
{
 
55
  int currentFile = 1;
 
56
  int overall_status = 0;
 
57
  bool first_msg = true;
 
58
 
 
59
  setMailDir(maildir);
 
60
  if ( mailDir().isEmpty() )
 
61
  {
 
62
    filterInfo()->alert(i18n("No files selected."));
 
63
    return;
 
64
  }
 
65
    
 
66
  filterInfo()->setOverall(0);
 
67
 
 
68
  //   kDebug() <<"starting by looking in directory" << directory;
 
69
  traverseDirectory(mailDir());
 
70
 
 
71
  QStringList::ConstIterator end( mMboxFiles.constEnd() );
 
72
  for ( QStringList::ConstIterator filename = mMboxFiles.constBegin(); filename != end; ++filename, ++currentFile) {
 
73
    if ( filterInfo()->shouldTerminate() )
 
74
      break;
 
75
    QFile mbox( *filename );
 
76
    if (! mbox.open( QIODevice::ReadOnly ) ) {
 
77
      filterInfo()->alert( i18n("Unable to open %1, skipping", *filename ) );
 
78
    } else {
 
79
      QFileInfo filenameInfo( *filename );
 
80
      kDebug() <<"importing filename" << *filename;
 
81
      QStringList name = (*filename).split('/', QString::SkipEmptyParts);
 
82
      QString folderName(name[name.count() - 2]);
 
83
 
 
84
      filterInfo()->setCurrent(0);
 
85
      filterInfo()->addInfoLogEntry( i18n("Importing emails from %1...", *filename ) );
 
86
      filterInfo()->setFrom( *filename );
 
87
      filterInfo()->setTo( folderName );
 
88
 
 
89
      QByteArray input(MAX_LINE,'\0');
 
90
      long l = 0;
 
91
 
 
92
      while ( ! mbox.atEnd() ) {
 
93
        KTemporaryFile tmp;
 
94
        tmp.open();
 
95
        /* comment by Danny:
 
96
         * Don't use QTextStream to read from mbox, better use QDataStream. QTextStream only 
 
97
         * support Unicode/Latin1/Locale. So you lost information from emails with 
 
98
         * charset!=Unicode/Latin1/Locale (e.g. KOI8-R) and Content-Transfer-Encoding != base64 
 
99
         * (e.g. 8Bit). It also not help to convert the QTextStream to Unicode. By this you
 
100
         * get Unicode/UTF-email but KMail can't detect the correct charset.
 
101
         */
 
102
        QByteArray separate;
 
103
 
 
104
        if(!first_msg)
 
105
          tmp.write( input, l );
 
106
        l = mbox.readLine( input.data(),MAX_LINE); // read the first line, prevent "From "
 
107
        tmp.write( input, l );
 
108
 
 
109
        while ( ! mbox.atEnd() &&  (l = mbox.readLine(input.data(),MAX_LINE)) && ((separate = input.data()).left(5) != "From ")) {
 
110
          tmp.write( input, l );
 
111
        }
 
112
        tmp.flush();
 
113
        first_msg = false;
 
114
 
 
115
        /* comment by Danny Kukawka:
 
116
         * addMessage() == old function, need more time and check for duplicates
 
117
         * addMessage_fastImport == new function, faster and no check for duplicates
 
118
         */
 
119
        if(filterInfo()->removeDupMessage())
 
120
          addMessage( folderName, tmp.fileName() );
 
121
        else
 
122
          addMessage_fastImport( folderName, tmp.fileName() );
 
123
 
 
124
        int currentPercentage = (int) ( ( (float) mbox.pos() / filenameInfo.size() ) * 100 );
 
125
        filterInfo()->setCurrent( currentPercentage );
 
126
        if (currentFile == 1)
 
127
          overall_status = (int)( currentPercentage*((float)currentFile/mMboxFiles.count()));
 
128
        else
 
129
          overall_status = (int)(((currentFile-1)*(100.0/(float)mMboxFiles.count()))+(currentPercentage*(1.0/(float)mMboxFiles.count())));
 
130
        filterInfo()->setOverall( overall_status );
 
131
        if ( filterInfo()->shouldTerminate() ) break;
 
132
      }
 
133
 
 
134
      filterInfo()->addInfoLogEntry( i18n("Finished importing emails from %1", *filename ) );
 
135
      if (countDuplicates() > 0) {
 
136
        filterInfo()->addInfoLogEntry( i18np("1 duplicate message not imported to folder %2 in KMail", 
 
137
                                    "%1 duplicate messages not imported to folder %2 in KMail", countDuplicates(), folderName));
 
138
      }
 
139
      setCountDuplicates(0);
 
140
      mbox.close();
 
141
    }
 
142
  }
 
143
  if (filterInfo()->shouldTerminate())
 
144
    filterInfo()->addInfoLogEntry( i18n("Finished import, canceled by user."));
 
145
}
 
146
 
 
147
void FilterMailApp::traverseDirectory(const QString &dirName)
 
148
{
 
149
  QDir dir(dirName);
 
150
  dir.setFilter(QDir::Dirs | QDir::Files);
 
151
 
 
152
  const QFileInfoList fileinfolist = dir.entryInfoList();
 
153
  Q_FOREACH( const QFileInfo &fi, fileinfolist ) {
 
154
    if (fi.fileName() == QLatin1String( "." ) || fi.fileName() == QLatin1String( ".." ) ) {
 
155
      continue;
 
156
    }
 
157
    if (fi.isDir() && fi.isReadable()) {
 
158
      traverseDirectory(fi.filePath());
 
159
    } else {
 
160
      if (!fi.isDir() && fi.fileName() == QLatin1String( "mbox" )) {
 
161
        kDebug() <<"adding the file" << fi.filePath();
 
162
        mMboxFiles.append(fi.filePath());
 
163
      }
 
164
    }
 
165
  }
 
166
}