1/*
2 * Copyright 2004-2012, Haiku Inc. All Rights Reserved.
3 * Copyright 2001-2002 Dr. Zoidberg Enterprises. All rights reserved.
4 *
5 * Distributed under the terms of the MIT License.
6 */
7
8
9#include <MailDaemon.h>
10
11#include <List.h>
12#include <MailSettings.h>
13#include <Messenger.h>
14#include <Message.h>
15#include <Roster.h>
16
17#include <MailPrivate.h>
18
19
20using namespace BPrivate;
21
22
23BMailDaemon::BMailDaemon()
24	:
25	fDaemon(B_MAIL_DAEMON_SIGNATURE)
26{
27}
28
29
30BMailDaemon::~BMailDaemon()
31{
32}
33
34
35bool
36BMailDaemon::IsRunning()
37{
38	return fDaemon.IsValid();
39}
40
41
42status_t
43BMailDaemon::CheckMail(int32 accountID)
44{
45	if (!fDaemon.IsValid())
46		return B_MAIL_NO_DAEMON;
47
48	BMessage message(kMsgCheckMessage);
49	message.AddInt32("account", accountID);
50	return fDaemon.SendMessage(&message);
51}
52
53
54status_t
55BMailDaemon::CheckAndSendQueuedMail(int32 accountID)
56{
57	if (!fDaemon.IsValid())
58		return B_MAIL_NO_DAEMON;
59
60	BMessage message(kMsgCheckAndSend);
61	message.AddInt32("account", accountID);
62	return fDaemon.SendMessage(&message);
63}
64
65
66status_t
67BMailDaemon::SendQueuedMail()
68{
69	if (!fDaemon.IsValid())
70		return B_MAIL_NO_DAEMON;
71
72	return fDaemon.SendMessage(kMsgSendMessages);
73}
74
75
76int32
77BMailDaemon::CountNewMessages(bool waitForFetchCompletion)
78{
79	if (!fDaemon.IsValid())
80		return B_MAIL_NO_DAEMON;
81
82	BMessage reply;
83	BMessage first(kMsgCountNewMessages);
84
85	if (waitForFetchCompletion)
86		first.AddBool("wait_for_fetch_done",true);
87
88	fDaemon.SendMessage(&first, &reply);
89
90	return reply.FindInt32("num_new_messages");
91}
92
93
94status_t
95BMailDaemon::MarkAsRead(int32 account, const entry_ref& ref, read_flags flag)
96{
97	if (!fDaemon.IsValid())
98		return B_MAIL_NO_DAEMON;
99
100	BMessage message(kMsgMarkMessageAsRead);
101	message.AddInt32("account", account);
102	message.AddRef("ref", &ref);
103	message.AddInt32("read", flag);
104
105	return fDaemon.SendMessage(&message);
106}
107
108
109status_t
110BMailDaemon::FetchBody(const entry_ref& ref, BMessenger* listener)
111{
112	if (!fDaemon.IsValid())
113		return B_MAIL_NO_DAEMON;
114
115	BMessage message(kMsgFetchBody);
116	message.AddRef("refs", &ref);
117	if (listener != NULL)
118		message.AddMessenger("target", *listener);
119
120	BMessage reply;
121	return fDaemon.SendMessage(&message, &reply);
122}
123
124
125status_t
126BMailDaemon::Quit()
127{
128	if (!fDaemon.IsValid())
129		return B_MAIL_NO_DAEMON;
130
131	return fDaemon.SendMessage(B_QUIT_REQUESTED);
132}
133
134
135status_t
136BMailDaemon::Launch()
137{
138	status_t status = be_roster->Launch(B_MAIL_DAEMON_SIGNATURE);
139	if (status == B_OK)
140		fDaemon = BMessenger(B_MAIL_DAEMON_SIGNATURE);
141
142	return status;
143}
144