src/mn-sylpheed-message.gob (2720B) - raw
1 /*
2 * Mail Notification
3 * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 %headertop{
21 #include "mn-vfs-message.h"
22 %}
23
24 %{
25 #include <stdlib.h>
26 #include <glib/gi18n.h>
27 #include "mn-util.h"
28 #include "mn-sylpheed-mailbox-backend-private.h"
29 #include "mn-message-private.h"
30 %}
31
32 class MN:Sylpheed:Message from MN:VFS:Message
33 {
34 override (MN:Message) void
35 builtin_mark_as_read (MNMessage *message, MNMessageActionRequest *request)
36 {
37 mn_message_perform_action_in_thread(request, self_builtin_mark_as_read_cb, NULL);
38 }
39
40 private GError *
41 builtin_mark_as_read_cb (MNMessage *message, gpointer data)
42 {
43 MNVFSMessage *vmessage = MN_VFS_MESSAGE(message);
44 char *filename;
45 guint32 num;
46 guint32 flags;
47 GError *tmp_err = NULL;
48 GHashTable *marks;
49 GError *err = NULL;
50
51 filename = gnome_vfs_uri_extract_short_name(vmessage->vfs_uri);
52 g_assert(mn_str_isnumeric(filename));
53 num = atoi(filename);
54 g_free(filename);
55
56 /*
57 * We hold the mailbox lock to prevent this:
58 *
59 * thread A: read mark file
60 * thread B: read mark file
61 * thread A: write new mark file
62 * thread B: write new mark file, A's modifications are overwritten
63 */
64 mn_vfs_mailbox_lock(MN_VFS_MAILBOX(message->mailbox));
65
66 marks = mn_sylpheed_mailbox_backend_read_marks(MN_VFS_MAILBOX(message->mailbox)->vfs_uri, &tmp_err);
67 if (! marks)
68 {
69 g_set_error(&err, 0, 0, _("Unable to read %s: %s."), SYLPHEED_MARK_FILE, tmp_err->message);
70 g_error_free(tmp_err);
71 goto end;
72 }
73
74 flags = GPOINTER_TO_UINT(g_hash_table_lookup(marks, GUINT_TO_POINTER(num)));
75 flags &= ~(SYLPHEED_MSG_NEW | SYLPHEED_MSG_UNREAD);
76 g_hash_table_insert(marks, GUINT_TO_POINTER(num), GUINT_TO_POINTER(flags));
77
78 mn_sylpheed_mailbox_backend_write_marks(MN_VFS_MAILBOX(message->mailbox)->vfs_uri, marks, &err);
79 g_hash_table_destroy(marks);
80
81 end:
82 mn_vfs_mailbox_unlock(MN_VFS_MAILBOX(message->mailbox));
83
84 return err;
85 }
86 }