summaryrefslogtreecommitdiffstats
path: root/kmailcvt/filter_mailapp.cxx
blob: a93519770ed7d8a5e9dc038b06a8c39f44b47b9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/***************************************************************************
                          filter_mailapp.cxx  -  OS X Mail App import
                             -------------------
    copyright            : (C) 2004 by Chris Howells
    email                : howells@kde.org
 
    Derived from code by:
    copyright            : (C) 2003 by Laurence Anderson
    email                : l.d.anderson@warwick.ac.uk
 
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include <config.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <ktempfile.h>
#include <kdebug.h>

#include "filter_mailapp.hxx"

FilterMailApp::FilterMailApp() :
        Filter( i18n("Import From OS X Mail"),
                "Chris Howells<br /><br />Filter accelerated by Danny Kukawka )",
                i18n("<p><b>OS X Mail Import Filter</b></p>"
                     "<p>This filter imports e-mails from the Mail client in Apple Mac OS X.</p>"))
{}

FilterMailApp::~FilterMailApp()
{
}

void FilterMailApp::import(FilterInfo *info)
{
    int currentFile = 1;
    int overall_status = 0;
    bool first_msg = true;

    TQString directory = KFileDialog::getExistingDirectory( TQDir::homeDirPath(), info->parent() );

    //qDebug("starting by looking in directory: %s", directory.latin1());
    if ( directory.isEmpty() ) {
        info->addLog( i18n("No directory selected."));
        info->addLog( i18n("No files found for import."));
    } else {
        info->setOverall(0);
        traverseDirectory(info, directory);

        for ( TQStringList::Iterator filename = mMboxFiles.begin(); filename != mMboxFiles.end(); ++filename, ++currentFile) {
            if ( info->shouldTerminate() ) break;
            TQFile mbox( *filename );
            if (! mbox.open( IO_ReadOnly ) ) {
                info->alert( i18n("Unable to open %1, skipping").arg( *filename ) );
            } else {
                TQFileInfo filenameInfo( *filename );
                kdDebug() << "importing filename " << *filename << endl;
                TQStringList name = TQStringList::split("/", *filename);
                TQString folderName(name[name.count() - 2]);

                info->setCurrent(0);
                info->addLog( i18n("Importing emails from %1...").arg( *filename ) );
                info->setFrom( *filename );
                info->setTo( folderName );

                TQByteArray input(MAX_LINE);
                long l = 0;

                while ( ! mbox.atEnd() ) {
                    KTempFile tmp;
                    /* comment by Danny:
                    * Don't use TQTextStream to read from mbox, etter use TQDataStream. TQTextStream only 
                    * support Unicode/Latin1/Locale. So you lost information from emails with 
                    * charset!=Unicode/Latin1/Locale (e.g. KOI8-R) and Content-Transfer-Encoding != base64 
                    * (e.g. 8Bit). It also not help to convert the TQTextStream to Unicode. By this you
                    * get Unicode/UTF-email but KMail can't detect the correct charset.
                    */
                    TQCString seperate;

                    if(!first_msg)
                        tmp.file()->writeBlock( input, l );
                    l = mbox.readLine( input.data(),MAX_LINE); // read the first line, prevent "From "
                    tmp.file()->writeBlock( input, l );

                    while ( ! mbox.atEnd() &&  (l = mbox.readLine(input.data(),MAX_LINE)) && ((seperate = input.data()).left(5) != "From ")) {
                        tmp.file()->writeBlock( input, l );
                    }
                    tmp.close();
                    first_msg = false;

                    // force stop if user chancel the import
                    if ( info->shouldTerminate() ) {
                    tmp.unlink(); 
                    break;
                    }

                    /* comment by Danny Kukawka:
                    * addMessage() == old function, need more time and check for duplicates
                    * addMessage_fastImport == new function, faster and no check for duplicates
                    */
                    if(info->removeDupMsg)
                        addMessage( info, folderName, tmp.name() );
                    else
                        addMessage_fastImport( info, folderName, tmp.name() );

                    tmp.unlink();

                    int currentPercentage = (int) ( ( (float) mbox.at() / filenameInfo.size() ) * 100 );
                    info->setCurrent( currentPercentage );
                    if (currentFile == 1)
                        overall_status = (int)( currentPercentage*((float)currentFile/mMboxFiles.count()));
                    else
                        overall_status = (int)(((currentFile-1)*(100.0/(float)mMboxFiles.count()))+(currentPercentage*(1.0/(float)mMboxFiles.count())));
                    info->setOverall( overall_status );
                    if ( info->shouldTerminate() ) break;
                }

                info->addLog( i18n("Finished importing emails from %1").arg( *filename ) );
                if (count_duplicates > 0) {
                    info->addLog( i18n("1 duplicate message not imported to folder %1 in KMail", 
                                    "%n duplicate messages not imported to folder %1 in KMail", count_duplicates).arg(folderName));
                }
                count_duplicates = 0;
                mbox.close();
            }
        }
    }
    
    if (info->shouldTerminate()) info->addLog( i18n("Finished import, canceled by user."));
    info->setCurrent(100);
    info->setOverall(100);
}

void FilterMailApp::traverseDirectory(FilterInfo *info, const TQString &dirName)
{
    TQDir dir(dirName);
    dir.setFilter(TQDir::Dirs | TQDir::Files);

    const TQFileInfoList *fileinfolist = dir.entryInfoList();
    TQFileInfoListIterator it(*fileinfolist);
    TQFileInfo *fi;

    if ( info->shouldTerminate() ) return;

    while ((fi = it.current())) {
        if (fi->fileName() == "." || fi->fileName() == "..") {
            ++it;
            continue;
        }
        if (fi->isDir() && fi->isReadable()) {
            traverseDirectory(info, fi->filePath());
        } else {
            if (!fi->isDir() && fi->fileName() == "mbox") {
                kdDebug() << "adding the file " << fi->filePath() << endl;
                mMboxFiles.append(fi->filePath());
            }
        }
        ++it;
    }
}