1/*
2 * Copyright 2011, Haiku, Inc. All rights reserved.
3 * Copyright 2011, Clemens Zeidler <haiku@clemens-zeidler.de>
4 * Distributed under the terms of the MIT License.
5 */
6
7
8#include "IMAPFolders.h"
9
10
11IMAPFolders::IMAPFolders()
12{
13	fHandlerList.AddItem(&fCapabilityHandler);
14}
15
16
17IMAPFolders::IMAPFolders(IMAPProtocol& connection)
18	:
19	IMAPProtocol(connection)
20{
21	fHandlerList.AddItem(&fCapabilityHandler);
22}
23
24
25status_t
26IMAPFolders::GetFolders(FolderList& folders)
27{
28	StringList allFolders;
29	status_t status = _GetAllFolders(allFolders);
30	if (status != B_OK)
31		return status;
32	StringList subscribedFolders;
33	status = _GetSubscribedFolders(subscribedFolders);
34	if (status != B_OK)
35		return status;
36
37	for (unsigned int i = 0; i < allFolders.size(); i++) {
38		FolderInfo info;
39		info.folder = allFolders[i];
40		for (unsigned int a = 0; a < subscribedFolders.size(); a++) {
41			if (allFolders[i] == subscribedFolders[a]
42				|| allFolders[i].ICompare("INBOX") == 0) {
43				info.subscribed = true;
44				break;
45			}
46		}
47		folders.push_back(info);
48	}
49
50	// you could be subscribed to a folder which not exist currently, add them:
51	for (unsigned int a = 0; a < subscribedFolders.size(); a++) {
52		bool isInlist = false;
53		for (unsigned int i = 0; i < allFolders.size(); i++) {
54			if (subscribedFolders[a] == allFolders[i]) {
55				isInlist = true;
56				break;
57			}
58		}
59		if (isInlist)
60			continue;
61
62		FolderInfo info;
63		info.folder = subscribedFolders[a];
64		info.subscribed = true;
65		folders.push_back(info);
66	}
67
68	return B_OK;
69}
70
71
72status_t
73IMAPFolders::SubscribeFolder(const char* folder)
74{
75	SubscribeCommand command(folder);
76	return ProcessCommand(&command);
77}
78
79
80status_t
81IMAPFolders::UnsubscribeFolder(const char* folder)
82{
83	UnsubscribeCommand command(folder);
84	return ProcessCommand(&command);
85}
86
87
88status_t
89IMAPFolders::GetQuota(uint64& used, uint64& total)
90{
91	if (fCapabilityHandler.Capabilities() == "")
92		ProcessCommand(fCapabilityHandler.Command());
93	if (fCapabilityHandler.Capabilities().FindFirst("QUOTA") < 0)
94		return B_ERROR;
95
96	GetQuotaCommand quotaCommand;
97	status_t status = ProcessCommand(&quotaCommand);
98	if (status != B_OK)
99		return status;
100
101	used = quotaCommand.UsedStorage();
102	total = quotaCommand.TotalStorage();
103	return B_OK;
104}
105
106
107status_t
108IMAPFolders::_GetAllFolders(StringList& folders)
109{
110	ListCommand listCommand;
111	status_t status = ProcessCommand(&listCommand);
112	if (status != B_OK)
113		return status;
114
115	folders = listCommand.FolderList();
116	return status;
117}
118
119
120status_t
121IMAPFolders::_GetSubscribedFolders(StringList& folders)
122{
123	ListSubscribedCommand listSubscribedCommand;
124	status_t status = ProcessCommand(&listSubscribedCommand);
125	if (status != B_OK)
126		return status;
127
128	folders = listSubscribedCommand.FolderList();
129	return status;
130}
131