aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/pop3/camel-pop3-folder.c
blob: ad856cc2bed67ea620f8e51e42f77f587578aa3d (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-pop3-folder.c : class for a pop3 folder */

/* 
 * Authors:
 *   Dan Winship <danw@helixcode.com>
 *
 * Copyright (C) 2000 Helix Code, Inc. (www.helixcode.com)
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "camel-pop3-folder.h"
#include "camel-pop3-store.h"
#include "camel-exception.h"
#include "camel-stream-mem.h"
#include "camel-mime-message.h"

#include <stdlib.h>

#define CF_CLASS(o) (CAMEL_FOLDER_CLASS (GTK_OBJECT (o)->klass))

static gboolean has_message_number_capability (CamelFolder *folder);
static CamelMimeMessage *get_message_by_number (CamelFolder *folder, 
                        gint number, 
                        CamelException *ex);
static gint get_message_count (CamelFolder *folder, CamelException *ex);


static void
camel_pop3_folder_class_init (CamelPop3FolderClass *camel_pop3_folder_class)
{
    CamelFolderClass *camel_folder_class =
        CAMEL_FOLDER_CLASS (camel_pop3_folder_class);
    
    /* virtual method overload */
    camel_folder_class->has_message_number_capability =
        has_message_number_capability;
    camel_folder_class->get_message_by_number =
        get_message_by_number;
    camel_folder_class->get_message_count =
        get_message_count;
}



static void
camel_pop3_folder_init (gpointer object, gpointer klass)
{
    CamelFolder *folder = CAMEL_FOLDER (object);

    folder->can_hold_messages = TRUE;
    folder->can_hold_folders = FALSE;

    /* Hi. I'm CamelPop3Folder. I'm useless. */
    folder->has_summary_capability = FALSE;
    folder->has_uid_capability = FALSE;
    folder->has_search_capability = FALSE;
}




GtkType
camel_pop3_folder_get_type (void)
{
    static GtkType camel_pop3_folder_type = 0;

    if (!camel_pop3_folder_type) {
        GtkTypeInfo camel_pop3_folder_info =    
        {
            "CamelPop3Folder",
            sizeof (CamelPop3Folder),
            sizeof (CamelPop3FolderClass),
            (GtkClassInitFunc) camel_pop3_folder_class_init,
            (GtkObjectInitFunc) camel_pop3_folder_init,
                /* reserved_1 */ NULL,
                /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };

        camel_pop3_folder_type = gtk_type_unique (CAMEL_FOLDER_TYPE, &camel_pop3_folder_info);
    }

    return camel_pop3_folder_type;
}


CamelFolder *camel_pop3_folder_new (CamelStore *parent, CamelException *ex)
{
    CamelFolder *folder =
        CAMEL_FOLDER (gtk_object_new (camel_pop3_folder_get_type (),
                          NULL));

    CF_CLASS (folder)->init (folder, parent, NULL, "inbox", '/', ex);
    return folder;
}

static gboolean
has_message_number_capability (CamelFolder *folder)
{
    return TRUE;
}

static CamelMimeMessage *
get_message_by_number (CamelFolder *folder, gint number, CamelException *ex)
{
    int status;
    char *result, *body;
    CamelStream *msgstream;
    CamelMimeMessage *msg;

    status = camel_pop3_command (CAMEL_POP3_STORE (folder->parent_store),
                     &result, "RETR %d", number);
    if (status != CAMEL_POP3_OK) {
        CamelService *service = CAMEL_SERVICE (folder->parent_store);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                      "Could not retrieve message from POP "
                      "server %s: %s.", service->url->host,
                      status == CAMEL_POP3_ERR ? result :
                      "Unknown error");
        g_free (result);
        return NULL;
    }
    g_free (result);

    body = camel_pop3_command_get_additional_data (CAMEL_POP3_STORE (folder->parent_store));
    if (!body) {
        CamelService *service = CAMEL_SERVICE (folder->parent_store);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                      "Could not retrieve message from POP "
                      "server %s.", service->url->host);
        return NULL;
    }

    msgstream = camel_stream_mem_new_with_buffer (body, strlen (body),
                              CAMEL_STREAM_MEM_READ);
    msg = camel_mime_message_new_with_session (camel_service_get_session (CAMEL_SERVICE (folder->parent_store)));
    camel_data_wrapper_set_input_stream (CAMEL_DATA_WRAPPER (msg),
                         msgstream);

    return msg;
}


static gint get_message_count (CamelFolder *folder, CamelException *ex)
{
    int status, count;
    char *result;

    status = camel_pop3_command (CAMEL_POP3_STORE (folder->parent_store),
                     &result, "STAT");
    if (status != CAMEL_POP3_OK) {
        CamelService *service = CAMEL_SERVICE (folder->parent_store);
        camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
                      "Could not get message count from POP "
                      "server %s: %s.", service->url->host,
                      status == CAMEL_POP3_ERR ? result :
                      "Unknown error");
        g_free (result);
        return -1;
    }

    count = atoi (result);
    g_free (result);
    return count;
}