summaryrefslogtreecommitdiffstats
path: root/kmailcvt/filter_sylpheed.cxx
blob: 1fc008d426b1846f11c188c76551ceacd971b853 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/***************************************************************************
            filter_sylpheed.hxx  -  Sylpheed maildir mail import
                             -------------------
    begin                : April 07 2005
   copyright            : (C) 2005 by Danny Kukawka
   email                : [email protected]
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "filter_sylpheed.hxx"

#include <config.h>
#include <tdelocale.h>
#include <tdefiledialog.h>
#include <kdebug.h>

/** Default constructor. */
FilterSylpheed::FilterSylpheed( void ) :
        Filter( i18n( "Import Sylpheed Maildirs and Folder Structure" ),
                "Danny Kukawka",
                i18n( "<p><b>Sylpheed import filter</b></p>"
                      "<p>Select the base directory of the Sylpheed mailfolder you want to import "
                      "(usually: ~/Mail ).</p>"
                      "<p>Since it is possible to recreate the folder structure, the folders "
                      "will be stored under: \"Sylpheed-Import\" in your local folder.</p>" 
                      "<p>This filter also recreates the status of message, e.g. new or forwarded.") )
{}

/** Destructor. */
FilterSylpheed::~FilterSylpheed( void )
{
}

/** Recursive import of Sylpheed maildir. */
void FilterSylpheed::import( FilterInfo *info )
{

    TQString _homeDir = TQDir::homeDirPath();

    KFileDialog *kfd;
    kfd = new KFileDialog( _homeDir, "", 0, "tdefiledialog", true );
    kfd->setMode( KFile::Directory | KFile::LocalOnly );
    kfd->exec();
    mailDir = kfd->selectedFile();
    delete kfd;

    if ( mailDir.isEmpty() ) {
        info->alert( i18n( "No directory selected." ) );
    }
    /**
     * If the user only select homedir no import needed because 
     * there should be no files and we surely import wrong files.
     */
    else if ( mailDir == TQDir::homeDirPath() || mailDir == ( TQDir::homeDirPath() + "/" ) ) {
        info->addLog( i18n( "No files found for import." ) );
    } else {
        info->setOverall(0);

        /** Recursive import of the MailFolders */
        TQDir dir(mailDir);
        TQStringList rootSubDirs = dir.entryList("[^\\.]*", TQDir::Dirs , TQDir::Name);
        int currentDir = 1, numSubDirs = rootSubDirs.size();
        for(TQStringList::Iterator filename = rootSubDirs.begin() ; filename != rootSubDirs.end() ; ++filename, ++currentDir) {
            if(info->shouldTerminate()) break;
            importDirContents(info, dir.filePath(*filename));
            info->setOverall((int) ((float) currentDir / numSubDirs * 100));
        }
    }

    info->addLog( i18n("Finished importing emails from %1").arg( mailDir ));
    if (count_duplicates > 0) {
        info->addLog( i18n("1 duplicate message not imported", "%n duplicate messages not imported", count_duplicates));
    }
    if (info->shouldTerminate()) info->addLog( i18n("Finished import, canceled by user."));
    count_duplicates = 0;
    info->setCurrent(100);
    info->setOverall(100);
}

/**
 * Import of a directory contents.
 * @param info Information storage for the operation.
 * @param dirName The name of the directory to import.
 */
void FilterSylpheed::importDirContents( FilterInfo *info, const TQString& dirName)
{
    if(info->shouldTerminate()) return;
    
    /** Here Import all archives in the current dir */
    importFiles(info, dirName);

    /** If there are subfolders, we import them one by one */
    TQDir subfolders(dirName);
    TQStringList subDirs = subfolders.entryList("[^\\.]*", TQDir::Dirs , TQDir::Name);
    for(TQStringList::Iterator filename = subDirs.begin() ; filename != subDirs.end() ; ++filename) {
        if(info->shouldTerminate()) return;
        importDirContents(info, subfolders.filePath(*filename));
    }
}


/**
 * Import the files within a Folder.
 * @param info Information storage for the operation.
 * @param dirName The name of the directory to import.
 */
void FilterSylpheed::importFiles( FilterInfo *info, const TQString& dirName)
{
    TQDir dir(dirName);
    TQString _path;
    bool generatedPath = false;

    TQDict<unsigned long> msgflags;
    msgflags.setAutoDelete(true);

    TQDir importDir (dirName);
    TQStringList files = importDir.entryList("[^\\.]*", TQDir::Files, TQDir::Name);
    int currentFile = 1, numFiles = files.size();
    
    readMarkFile(info, dir.filePath(".sylpheed_mark"), msgflags);
    
    for ( TQStringList::Iterator mailFile = files.begin(); mailFile != files.end(); ++mailFile, ++currentFile) {
        if(info->shouldTerminate()) return;
        TQString _mfile = *mailFile;
        if (!(_mfile.endsWith(".sylpheed_cache") || _mfile.endsWith(".sylpheed_mark") || _mfile.endsWith(".mh_sequences") )) {
            if(!generatedPath) {
                _path = "Sylpheed-Import/";
                TQString _tmp = dir.filePath(*mailFile);
                _tmp = _tmp.remove(_tmp.length() - _mfile.length() -1, _mfile.length()+1);
                _path += _tmp.remove( mailDir ,TRUE);
                TQString _info = _path;
                info->addLog(i18n("Import folder %1...").arg(_info.remove(0,15)));

                info->setFrom(_info);
                info->setTo(_path);
                generatedPath = true;
            }

            TQString flags;
            if (msgflags[_mfile])
                flags = msgFlagsToString(*(msgflags[_mfile]));
             
            if(info->removeDupMsg) {
                if(! addMessage( info, _path, dir.filePath(*mailFile), flags )) {
                    info->addLog( i18n("Could not import %1").arg( *mailFile ) );
                }
                info->setCurrent((int) ((float) currentFile / numFiles * 100));
            } else {
                if(! addMessage_fastImport( info, _path, dir.filePath(*mailFile), flags )) {
                    info->addLog( i18n("Could not import %1").arg( *mailFile ) );
                }
                info->setCurrent((int) ((float) currentFile / numFiles * 100));
            }
        }
    }
}


void FilterSylpheed::readMarkFile( FilterInfo *info, const TQString &path, TQDict<unsigned long> &dict )
{
    /* Each sylpheed mail directory contains a .sylpheed_mark file which
     * contains all the flags for each messages. The layout of this file
     * is documented in the source code of sylpheed: in procmsg.h for
     * the flag bits, and procmsg.c.
     *
     * Note that the mark file stores 32 bit unsigned integers in the
     * platform's native "endianness". 
     *
     * The mark file starts with a 32 bit unsigned integer with a version
     * number. It is then followed by pairs of 32 bit unsigned integers,
     * the first one with the message file name (which is a number), 
     * and the second one with the actual message flags */

    TQ_UINT32 in, flags;
    TQFile file(path);

    if (!file.open(IO_ReadOnly)) 
        return;
    
    TQDataStream stream(&file);

    if (Q_BYTE_ORDER == Q_LITTLE_ENDIAN)
        stream.setByteOrder(TQDataStream::LittleEndian);



    /* Read version; if the value is reasonably too big, we're looking
     * at a file created on another platform. I don't have any test 
     * marks/folders, so just ignoring this case */
    stream >> in;
    if (in > (TQ_UINT32) 0xffff) 
        return;

    while (!stream.atEnd()) {
        if(info->shouldTerminate()){
            file.close();
            return;
        }
        stream >> in;
        stream >> flags;
        TQString s;
        s.setNum((uint) in);
        dict.insert(s, new unsigned long(flags));
    }
}

TQString FilterSylpheed::msgFlagsToString(unsigned long flags)
{
    TQString status;
    
    /* see sylpheed's procmsg.h */
    if (flags & 1UL) status += 'N';
    if (flags & 2UL) status += 'U';
    if ((flags & 3UL) == 0UL) status += 'R';
    if (flags & 8UL) status += 'D';
    if (flags & 16UL) status += 'A';
    if (flags & 32UL) status += 'F';
    
    return status;
}