aboutsummaryrefslogtreecommitdiffstats
path: root/src/messaging/maildirFolder.cpp
blob: 903de91d31dc185188a6c3a030e3761a01c4a856 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//
// VMime library (http://vmime.sourceforge.net)
// Copyright (C) 2002-2004 Vincent Richard <[email protected]>
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include "maildirFolder.hpp"

#include "maildirStore.hpp"
#include "maildirMessage.hpp"
#include "maildirUtils.hpp"

#include "../exception.hpp"
#include "../platformDependant.hpp"


namespace vmime {
namespace messaging {


maildirFolder::maildirFolder(const folder::path& path, maildirStore* store)
	: m_store(store), m_path(path), m_name(path.last()), m_mode(-1), m_open(false)
{
	m_store->registerFolder(this);
}


maildirFolder::~maildirFolder()
{
	if (m_store)
	{
		if (m_open)
			close(false);

		m_store->unregisterFolder(this);
	}
	else if (m_open)
	{
		close(false);
	}
}


void maildirFolder::onStoreDisconnected()
{
	m_store = NULL;
}


const int maildirFolder::mode() const
{
	if (!isOpen())
		throw exceptions::illegal_state("Folder not open");

	return (m_mode);
}


const int maildirFolder::type()
{
	if (m_path.empty())
		return (TYPE_CONTAINS_FOLDERS);
	else
		return (TYPE_CONTAINS_FOLDERS | TYPE_CONTAINS_MESSAGES);
}


const int maildirFolder::flags()
{
	int flags = 0;

	utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();

	utility::auto_ptr <utility::file> rootDir = fsf->create
		(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_CONTAINER));

	utility::auto_ptr <utility::fileIterator> it = rootDir->getFiles();

	while (it->hasMoreElements())
	{
		utility::auto_ptr <utility::file> file = it->nextElement();

		if (maildirUtils::isSubfolderDirectory(*file))
		{
			flags |= FLAG_CHILDREN; // Contains at least one sub-folder
			break;
		}
	}

	return (flags);
}


const folder::path::component maildirFolder::name() const
{
	return (m_name);
}


const folder::path maildirFolder::fullPath() const
{
	return (m_path);
}


void maildirFolder::open(const int mode, bool /* failIfModeIsNotAvailable */)
{
	if (!m_store)
		throw exceptions::illegal_state("Store disconnected");
	else if (isOpen())
		throw exceptions::illegal_state("Folder is already open");
	else if (!exists())
		throw exceptions::illegal_state("Folder already exists");

	m_open = true;
	m_mode = mode;
}


void maildirFolder::close(const bool expunge)
{
	// TODO
}


void maildirFolder::onClose()
{
	// TODO
}


void maildirFolder::create(const int type)
{
	if (!m_store)
		throw exceptions::illegal_state("Store disconnected");
	else if (isOpen())
		throw exceptions::illegal_state("Folder is open");
	else if (exists())
		throw exceptions::illegal_state("Folder already exists");

	// Folder name cannot start with '.'
	if (!m_path.empty())
	{
		const path::component& comp = m_path.last();

		const int length = comp.buffer().length();
		int pos = 0;

		while ((pos < length) && (comp.buffer()[pos] == '.'))
			++pos;

		if (pos != 0)
			throw exceptions::invalid_folder_name("Name cannot start with '.'");
	}

	// Create directory on file system
	try
	{
		utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();

		utility::auto_ptr <utility::file> rootDir = fsf->create
			(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_ROOT));

		utility::auto_ptr <utility::file> newDir = fsf->create
			(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_NEW));
		utility::auto_ptr <utility::file> tmpDir = fsf->create
			(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_TMP));
		utility::auto_ptr <utility::file> curDir = fsf->create
			(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_CUR));

		rootDir->createDirectory(true);

		newDir->createDirectory(false);
		tmpDir->createDirectory(false);
		curDir->createDirectory(false);
	}
	catch (exceptions::filesystem_exception& e)
	{
		throw exceptions::command_error("CREATE", e.what(), "File system exception");
	}

	// Notify folder created
	events::folderEvent event(this, events::folderEvent::TYPE_CREATED, m_path, m_path);
	notifyFolder(event);
}


const bool maildirFolder::exists()
{
	utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();

	utility::auto_ptr <utility::file> rootDir = fsf->create
		(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_ROOT));

	utility::auto_ptr <utility::file> newDir = fsf->create
		(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_NEW));
	utility::auto_ptr <utility::file> tmpDir = fsf->create
		(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_TMP));
	utility::auto_ptr <utility::file> curDir = fsf->create
		(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_CUR));

	return (rootDir->exists() && rootDir->isDirectory() &&
	        newDir->exists() && newDir->isDirectory() &&
	        tmpDir->exists() && tmpDir->isDirectory() &&
	        curDir->exists() && curDir->isDirectory());
}


const bool maildirFolder::isOpen() const
{
	return (m_open);
}


void maildirFolder::scanFolder()
{
	if (!isOpen())
		throw exceptions::illegal_state("Folder not open");

	try
	{
		utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();

		utility::auto_ptr <utility::file> newDir = fsf->create
			(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_NEW));
		utility::auto_ptr <utility::file> curDir = fsf->create
			(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_CUR));

		// Unread messages (new/)
		utility::auto_ptr <utility::fileIterator> nit = newDir->getFiles();
		std::vector <utility::path::component> unreadMessageFilenames;

		while (nit->hasMoreElements())
		{
			utility::auto_ptr <utility::file> file = nit->nextElement();
			unreadMessageFilenames.push_back(file->fullPath().last());
		}

		// Seen messages (cur/)
		utility::auto_ptr <utility::fileIterator> cit = curDir->getFiles();
		std::vector <utility::path::component> messageFilenames;

		while (cit->hasMoreElements())
		{
			utility::auto_ptr <utility::file> file = cit->nextElement();
			messageFilenames.push_back(file->fullPath().last());
		}

		// TODO: update m_messageFilenames
		// TODO: what to do with files which name has changed? (flag change, message deletion...)

		m_unreadMessageCount = unreadMessageFilenames.size();
		m_messageCount = messageFilenames.size();
	}
	catch (exceptions::filesystem_exception&)
	{
		// Should not happen...
	}

	/*
	int m_unreadMessageCount;
	int m_messageCount;

	std::vector <folder::path::component> m_unreadMessageFilenames;
	std::vector <folder::path::component> m_messageFilenames;

	if (0)
	{
		m_messageFilenames.clear();

		for (...)
		{
			m_messageFilenames.push_back(...);
		}
	}
	*/
}


message* maildirFolder::getMessage(const int num)
{
	if (!isOpen())
		throw exceptions::illegal_state("Folder not open");

	if (num < 1 || num > m_messageCount)
		throw exceptions::message_not_found();

	return new maildirMessage(this, num);
}


std::vector <message*> maildirFolder::getMessages(const int from, const int to)
{
	if (!isOpen())
		throw exceptions::illegal_state("Folder not open");

	std::vector <message*> v;

	for (int i = from ; i <= to ; ++i)
		v.push_back(new maildirMessage(this, i));

	return (v);
}


std::vector <message*> maildirFolder::getMessages(const std::vector <int>& nums)
{
	if (!isOpen())
		throw exceptions::illegal_state("Folder not open");

	std::vector <message*> v;

	for (std::vector <int>::const_iterator it = nums.begin() ; it != nums.end() ; ++it)
		v.push_back(new maildirMessage(this, *it));

	return (v);
}


const int maildirFolder::getMessageCount()
{
	return (m_messageCount);
}


folder* maildirFolder::getFolder(const folder::path::component& name)
{
	if (!m_store)
		throw exceptions::illegal_state("Store disconnected");

	return new maildirFolder(m_path / name, m_store);
}


std::vector <folder*> maildirFolder::getFolders(const bool recursive)
{
	if (!isOpen() && !m_store)
		throw exceptions::illegal_state("Store disconnected");

	std::vector <folder*> list;

	try
	{
		listFolders(list, recursive);
	}
	catch (std::exception&)
	{
		for (std::vector <folder*>::iterator it = list.begin() ; it != list.end() ; ++it)
			delete (*it);

		throw;
	}

	return (list);
}


void maildirFolder::listFolders(std::vector <folder*>& list, const bool recursive)
{
	try
	{
		utility::fileSystemFactory* fsf = platformDependant::getHandler()->getFileSystemFactory();

		utility::auto_ptr <utility::file> rootDir = fsf->create
			(maildirUtils::getFolderFSPath(m_store, m_path, maildirUtils::FOLDER_PATH_CONTAINER));
		utility::auto_ptr <utility::fileIterator> it = rootDir->getFiles();

		while (it->hasMoreElements())
		{
			utility::auto_ptr <utility::file> file = it->nextElement();

			if (maildirUtils::isSubfolderDirectory(*file))
			{
				const utility::path subPath = m_path / file->fullPath().last();
				maildirFolder* subFolder = new maildirFolder(subPath, m_store);

				list.push_back(subFolder);

				if (recursive)
					subFolder->listFolders(list, true);
			}
		}
	}
	catch (exceptions::filesystem_exception& e)
	{
		throw exceptions::command_error("LIST", e.what());
	}
}


void maildirFolder::rename(const folder::path& newPath)
{
	// TODO
}


void maildirFolder::deleteMessage(const int num)
{
	// TODO
}


void maildirFolder::deleteMessages(const int from, const int to)
{
	// TODO
}


void maildirFolder::deleteMessages(const std::vector <int>& nums)
{
	// TODO
}


void maildirFolder::setMessageFlags
	(const int from, const int to, const int flags, const int mode)
{
	// TODO
}


void maildirFolder::setMessageFlags
	(const std::vector <int>& nums, const int flags, const int mode)
{
	// TODO
}


void maildirFolder::addMessage(vmime::message* msg, const int flags,
	vmime::datetime* date, progressionListener* progress)
{
	// TODO
}


void maildirFolder::addMessage(utility::inputStream& is, const int size,
	const int flags, vmime::datetime* date, progressionListener* progress)
{
	// TODO
}


void maildirFolder::copyMessage(const folder::path& dest, const int num)
{
	// TODO
}


void maildirFolder::copyMessages(const folder::path& dest, const int from, const int to)
{
	// TODO
}


void maildirFolder::copyMessages(const folder::path& dest, const std::vector <int>& nums)
{
	// TODO
}


void maildirFolder::status(int& count, int& unseen)
{
	const int oldCount = m_messageCount;

	scanFolder();

	count = m_messageCount;
	unseen = m_unreadMessageCount;

	// Notify message count changed (new messages)
	if (count > oldCount)
	{
		std::vector <int> nums;
		nums.reserve(count - oldCount);

		for (int i = oldCount + 1, j = 0 ; i <= count ; ++i, ++j)
			nums[j] = i;

		events::messageCountEvent event(this, events::messageCountEvent::TYPE_ADDED, nums);

		for (std::list <maildirFolder*>::iterator it = m_store->m_folders.begin() ;
		     it != m_store->m_folders.end() ; ++it)
		{
			if ((*it)->fullPath() == m_path)
			{
				(*it)->m_messageCount = count;
				(*it)->notifyMessageCount(event);
			}
		}
	}
}


void maildirFolder::expunge()
{
	// TODO
}


folder* maildirFolder::getParent()
{
	return (m_path.empty() ? NULL : new maildirFolder(m_path.parent(), m_store));
}


const class store& maildirFolder::store() const
{
	return (*m_store);
}


class store& maildirFolder::store()
{
	return (*m_store);
}


void maildirFolder::fetchMessages(std::vector <message*>& msg,
	const int options, progressionListener* progress)
{
	// TODO
}


void maildirFolder::fetchMessage(message* msg, const int options)
{
	// TODO
}


const int maildirFolder::getFetchCapabilities() const
{
	return (FETCH_ENVELOPE | FETCH_STRUCTURE | FETCH_CONTENT_INFO |
	        FETCH_FLAGS | FETCH_SIZE | FETCH_FULL_HEADER | FETCH_UID);
}


} // messaging
} // vmime