summaryrefslogtreecommitdiffabout
path: root/kmicromail/libmailwrapper/abstractmail.cpp
blob: f876235233ea6503ad93b6120f29f36d737e46d9 (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
// CHANGED 2004-09-31 Lutz Rogowski
#include "abstractmail.h"
#include "imapwrapper.h"
#include "pop3wrapper.h"
#include "nntpwrapper.h"
#include "mhwrapper.h"
#include "mailtypes.h"
#include <qpe/global.h>


#include <qprogressbar.h>
#include <qapplication.h>

#include <kdecore/kstandarddirs.h>
#include <qfile.h>
#include <qtextstream.h>
#include <stdlib.h>
#include <libetpan/mailmime_content.h>
#include <libetpan/mailmime.h>

using namespace Opie::Core;
AbstractMail* AbstractMail::getWrapper(IMAPaccount *a)
{
    return new IMAPwrapper(a);
}

AbstractMail* AbstractMail::getWrapper(POP3account *a)
{
    return new POP3wrapper(a);
}

AbstractMail* AbstractMail::getWrapper(NNTPaccount *a)
{
    return new NNTPwrapper(a);
}

AbstractMail* AbstractMail::getWrapper(const QString&a,const QString&name)
{
    return new MHwrapper(a,name);
}

AbstractMail* AbstractMail::getWrapper(Account*a)
{
    if (!a) return 0;
    switch (a->getType()) {
        case MAILLIB::A_IMAP:
            return new IMAPwrapper((IMAPaccount*)a);
            break;
        case MAILLIB::A_POP3:
            return new POP3wrapper((POP3account*)a);
            break;
        case MAILLIB::A_NNTP:
            return new NNTPwrapper((NNTPaccount*)a);
            break;
        default:
            return 0;
    }
}

encodedString* AbstractMail::decode_String(const encodedString*text,const QString&enc)
{
    // odebug << "Decode string start" << oendl; 
    char*result_text;
    size_t index = 0;
    /* reset for recursive use! */
    size_t target_length = 0;
    result_text = 0;
    int mimetype = MAILMIME_MECHANISM_7BIT;
    if (enc.lower()=="quoted-printable") {
        mimetype = MAILMIME_MECHANISM_QUOTED_PRINTABLE;
    } else if (enc.lower()=="base64") {
        mimetype = MAILMIME_MECHANISM_BASE64;
    } else if (enc.lower()=="8bit") {
        mimetype = MAILMIME_MECHANISM_8BIT;
    } else if (enc.lower()=="binary") {
        mimetype = MAILMIME_MECHANISM_BINARY;
    }

    int err = mailmime_part_parse(text->Content(),text->Length(),&index,mimetype,
    &result_text,&target_length);

    encodedString* result = new encodedString();
    if (err == MAILIMF_NO_ERROR) {
        result->setContent(result_text,target_length);
    }
    //odebug << "Decode string finished" << oendl; 
    return result;
}

QString AbstractMail::convert_String(const char*text)
{
    //size_t index = 0;
    char*res = 0;
    int err = MAILIMF_NO_ERROR;

    QString result(text);

    /* due a bug in libetpan it isn't usable this moment */
/*    int err = mailmime_encoded_phrase_parse("iso-8859-1",
        text, strlen(text),&index, "iso-8859-1",&res);*/
    //odebug << "Input: " << text << "" << oendl; 
    if (err == MAILIMF_NO_ERROR && res && strlen(res)) {
//        result = QString(res);
//        odebug << "Res: " << res << ", length: " << strlen(res) << "" << oendl; 
    }
    if (res) free(res);
    return result;
}

/* cp & paste from launcher */
QString AbstractMail::gen_attachment_id()
{
    QFile file( "/proc/sys/kernel/random/uuid" );
    if (!file.open(IO_ReadOnly ) )
        return QString::null;

    QTextStream stream(&file);

    return "{" + stream.read().stripWhiteSpace() + "}";
}

int AbstractMail::createMbox(const QString&,const FolderP&,const QString& ,bool)
{
    return 0;
}

QString AbstractMail::defaultLocalfolder()
{
    //  QString f = getenv( "HOME" );
    QString f = locateLocal( "data", "kopiemail/localmail");
    //  f += "/Applications/opiemail/localmail";
    return f;
}

QString AbstractMail::draftFolder()
{
    return QString("Drafts");
}

/* temporary - will be removed when implemented in all classes */
void AbstractMail::deleteMails(const QString &,const QValueList<Opie::Core::OSmartPointer<RecMail> > &)
{
}
void AbstractMail::deleteMailList(const QValueList<RecMailP>&target)
{
    //qDebug("AbstractMail::deleteMailList:: Please reimplement! ");
    // this is currently re-implemented in pop3wrapper and imapwrapper
    int iii = 0;
    int count = target.count(); 
    QWidget wid;
    wid.show();
    while (iii < count ) {   
        Global::statusMessage(tr("Delete message %1 of %2").arg(iii).arg(count));
        wid.raise();
        qApp->processEvents();
        RecMailP mail = (*target.at( iii ));
        deleteMail(mail);
        ++iii;
    }
}
void AbstractMail::mvcpAllMails(const FolderP&fromFolder,
    const QString&targetFolder,AbstractMail*targetWrapper,bool moveit, int maxSizeInKb)
{
 QValueList<RecMailP> t;
 listMessages(fromFolder->getName(),t, maxSizeInKb);
 mvcpMailList( t,targetFolder,targetWrapper,moveit);

}
void AbstractMail::mvcpMailList(const QValueList<RecMailP>& t,
                                const QString&targetFolder,AbstractMail*targetWrapper,bool moveit)
{
    encodedString*st = 0;
    int iii = 0;
    int count = t.count();
    if ( count == 0 )
        return;
    // wel, processevents is qite strange, we need a widget for getting  
    // Global::statusMessage(tr("Copy2 message %1").arg(iii)); displayed
    QWidget wid;
    wid.show();
    while (iii < count ) {  
        Global::statusMessage(tr("Copy message %1 of %2").arg(iii).arg(count));
        wid.raise();
        qApp->processEvents();
        RecMailP r = (*t.at( iii ));
        st = fetchRawBody(r);
        if (st) {
            targetWrapper->storeMessage(st->Content(),st->Length(),targetFolder);
            delete st;
        }
        ++iii;
    } 
    if (moveit) {
        deleteMailList( t );
        //deleteAllMail(fromFolder);
    }
}

void AbstractMail::mvcpMail(const RecMailP&mail,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit)
{
    encodedString*st = 0;
    st = fetchRawBody(mail);
    if (st) {
        targetWrapper->storeMessage(st->Content(),st->Length(),targetFolder);
        delete st;
    }
    if (moveit) {
        deleteMail(mail);
    }
}