<commit_before>/*
	Empath - Mailer for KDE
	
	Copyright (C) 1998 <NAME> <EMAIL>
	
	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.
*/

// FIXME: The writeNewMail method needs to find a way of telling the
// caller whether message write was successful.

// Qt includes
#include <qfile.h>
#include <qdatastream.h>
#include <qregexp.h>
#include <qdatetime.h>
#include <qapplication.h>

// KDE includes
#include <klocale.h>
#include <kapp.h>
#include <kconfig.h>

// Local includes
#include "EmpathMailboxMaildir.h"
#include "EmpathFolderList.h"
#include "EmpathMessageList.h"
#include "EmpathConfig.h"
#include "EmpathEnum.h"
#include "Empath.h"

EmpathMailboxMaildir::EmpathMailboxMaildir(const QString & name)
	:	EmpathMailbox(name)
{
	empathDebug("ctor");
	type_ = Maildir;
	seq_ = 0;
	boxList_.setAutoDelete(true);
}

EmpathMailboxMaildir::~EmpathMailboxMaildir()
{
	empathDebug("dtor");
}

	bool
EmpathMailboxMaildir::mark(const EmpathURL & message, RMM::MessageStatus s)
{
	EmpathMaildir * m = _box(message);
	if (m == 0) return false;
	return m->mark(message, s);
}

	void
EmpathMailboxMaildir::syncIndex(const EmpathURL & url)
{
	empathDebug("syncIndex(" + url.asString() + ") called");
	EmpathMaildirListIterator it(boxList_);
	
	for (; it.current(); ++it) {
		empathDebug("Looking at \"" + it.current()->path() + "\"");
		if (it.current()->url() == url)
			it.current()->sync(url);
	}
}
	
	void
EmpathMailboxMaildir::_setupDefaultFolders()
{
	empathDebug("_setupDefaultFolders() called");
	EmpathURL urlInbox	(url_.mailboxName(), i18n("Inbox"),		QString::null);
	EmpathURL urlOutbox	(url_.mailboxName(), i18n("Outbox"),	QString::null);
	EmpathURL urlTrash	(url_.mailboxName(), i18n("Trash"),		QString::null);
	EmpathURL urlSent	(url_.mailboxName(), i18n("Sent"),		QString::null);
	EmpathURL urlDrafts	(url_.mailboxName(), i18n("Drafts"),	QString::null);
	
	EmpathFolder * folder_inbox = new EmpathFolder(urlInbox);
	CHECK_PTR(folder_inbox);
		
	EmpathMaildir * box_inbox =
		new EmpathMaildir(path_, urlInbox);
	CHECK_PTR(box_inbox);
	
	EmpathFolder * folder_outbox = new EmpathFolder(urlOutbox);
	CHECK_PTR(folder_outbox);
	
	EmpathMaildir * box_outbox =
		new EmpathMaildir(path_, urlOutbox);
	CHECK_PTR(box_outbox);
	
	EmpathFolder * folder_drafts = new EmpathFolder(urlDrafts);
	CHECK_PTR(folder_drafts);
	
	EmpathMaildir * box_drafts =
		new EmpathMaildir(path_, urlDrafts);
	CHECK_PTR(box_drafts);
	
	EmpathFolder * folder_sent = new EmpathFolder(urlSent);
	CHECK_PTR(folder_sent);
	
	EmpathMaildir * box_sent =
		new EmpathMaildir(path_, urlSent);
	CHECK_PTR(box_sent);
	
	EmpathFolder * folder_trash = new EmpathFolder(urlTrash);
	CHECK_PTR(folder_trash);
	
	EmpathMaildir * box_trash =
		new EmpathMaildir(path_, urlTrash);
	CHECK_PTR(box_trash);
	
	folderList_.append(folder_trash);
	folderList_.append(folder_sent);
	folderList_.append(folder_drafts);
	folderList_.append(folder_outbox);
	folderList_.append(folder_inbox);
	
	boxList_.append(box_trash);
	boxList_.append(box_sent);
	boxList_.append(box_drafts);
	boxList_.append(box_outbox);
	boxList_.append(box_inbox);
	
	saveConfig();
}
	
	bool
EmpathMailboxMaildir::writeMessage(const EmpathURL & folder, RMessage & m)
{
	empathDebug("writeMessage() called");
	
	EmpathMaildir * box = _box(folder);
	if (box == 0) return 0;
	box->writeMessage(m);
	
	return true;
}

	bool
EmpathMailboxMaildir::newMail() const
{
	return false;
}

	void
EmpathMailboxMaildir::saveConfig()
{
	empathDebug("saveConfig() called - my name is " + url_.asString());
	
	KConfig * c = kapp->getConfig();
	c->setGroup(EmpathConfig::GROUP_MAILBOX + url_.mailboxName());
	
	c->writeEntry(EmpathConfig::KEY_MAILBOX_TYPE, type_);
	c->writeEntry(EmpathConfig::KEY_LOCAL_MAILBOX_PATH, path_);
	
	c->writeEntry(EmpathConfig::KEY_CHECK_MAIL, checkMail_);
	c->writeEntry(EmpathConfig::KEY_CHECK_MAIL_INTERVAL, checkMailInterval_);
	c->sync();
}

	void
EmpathMailboxMaildir::readConfig()
{
	empathDebug("readConfig() called");

	KConfig * c = kapp->getConfig();
	c->setGroup(EmpathConfig::GROUP_MAILBOX + url_.mailboxName());
	
	checkMail_ = c->readUnsignedNumEntry(EmpathConfig::KEY_CHECK_MAIL);
	checkMailInterval_ =
		c->readUnsignedNumEntry(EmpathConfig::KEY_CHECK_MAIL_INTERVAL);
	
	folderList_.clear();
	boxList_.clear();
	
	path_ = c->readEntry(EmpathConfig::KEY_LOCAL_MAILBOX_PATH);
	if (path_.at(path_.length()) != '/') path_ += '/';
	_recursiveReadFolders(path_);
}

	void
EmpathMailboxMaildir::_recursiveReadFolders(const QString & currentDir)
{
	empathDebug("_recursiveReadFolders(" + currentDir + ") called");
	// We need to look at the maildir base directory, and go recursively
	// through subdirs. Any subdir that has cur, tmp and new is a Maildir
	// folder.

	QDir d(currentDir);
	d.setFilter(QDir::Dirs);
	
	if (d.count() == 0) {
		empathDebug("No folders in maildir");
		return;
	}
	
	QStringList l(d.entryList());
	
	QStringList::Iterator it(l.begin());
	
	bool hasCur, hasNew, hasTmp;
	hasCur = hasNew = hasTmp = false;
	
	for (; it != l.end(); ++it) {
		
		if ((*it).left(1) == ".")
			continue;
		
		if (*it == "cur") {
			hasCur = true;
			continue;
		}
		
		if (*it == "new") {
			hasNew = true;
			continue;
		}
		
		if (*it == "tmp") {
			hasTmp = true;
			continue;
		}
		
		_recursiveReadFolders(currentDir + "/" + *it);
	}
	
	if (hasCur && hasNew && hasTmp) {
		
		// This dir is itself a maildir folder.
		
		QString s(d.absPath());
		s.remove(0, path_.length());
		s.remove(0, 1);
		empathDebug("Folder path is " + s);
		
		EmpathURL url(url_.mailboxName(), s, QString::null);
		EmpathFolder * f = new EmpathFolder(url);
		
		CHECK_PTR(f);

		folderList_.append(f);
		
		EmpathMaildir * m = new EmpathMaildir(path_, url);
		CHECK_PTR(m);
		
		boxList_.append(m);
		emit(updateFolderLists());
		
		m->init();<commit_msg>You really didn't want to do that did you Rik ? No, not really. People might have got upset, and I could have been fired. Oh well, you're used to it. Too right matey. Mmm. Cheesecake.<commit_after>/*
	Empath - Mailer for KDE
	
	Copyright (C) 1998 <NAME> <EMAIL>
	
	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.
*/

// FIXME: The writeNewMail method needs to find a way of telling the
// caller whether message write was successful.

// Qt includes
#include <qfile.h>
#include <qdatastream.h>
#include <qregexp.h>
#include <qdatetime.h>
#include <qapplication.h>

// KDE includes
#include <klocale.h>
#include <kapp.h>
#include <kconfig.h>

// Local includes
#include "EmpathMailboxMaildir.h"
#include "EmpathFolderList.h"
#include "EmpathMessageList.h"
#include "EmpathConfig.h"
#include "EmpathEnum.h"
#include "Empath.h"

EmpathMailboxMaildir::EmpathMailboxMaildir(const QString & name)
	:	EmpathMailbox(name)
{
	empathDebug("ctor");
	type_ = Maildir;
	seq_ = 0;
	boxList_.setAutoDelete(true);
}

EmpathMailboxMaildir::~EmpathMailboxMaildir()
{
	empathDebug("dtor");
}

	bool
EmpathMailboxMaildir::mark(const EmpathURL & message, RMM::MessageStatus s)
{
	EmpathMaildir * m = _box(message);
	if (m == 0) return false;
	return m->mark(message, s);
}

	void
EmpathMailboxMaildir::syncIndex(const EmpathURL & url)
{
	empathDebug("syncIndex(" + url.asString() + ") called");
	EmpathMaildirListIterator it(boxList_);
	
	for (; it.current(); ++it) {
		empathDebug("Looking at \"" + it.current()->path() + "\"");
		if (it.current()->url() == url)
			it.current()->sync(url);
	}
}
	
	void
EmpathMailboxMaildir::_setupDefaultFolders()
{
	empathDebug("_setupDefaultFolders() called");
	EmpathURL urlInbox	(url_.mailboxName(), i18n("Inbox"),		QString::null);
	EmpathURL urlOutbox	(url_.mailboxName(), i18n("Outbox"),	QString::null);
	EmpathURL urlTrash	(url_.mailboxName(), i18n("Trash"),		QString::null);
	EmpathURL urlSent	(url_.mailboxName(), i18n("Sent"),		QString::null);
	EmpathURL urlDrafts	(url_.mailboxName(), i18n("Drafts"),	QString::null);
	
	EmpathFolder * folder_inbox = new EmpathFolder(urlInbox);
	CHECK_PTR(folder_inbox);
		
	EmpathMaildir * box_inbox =
		new EmpathMaildir(path_, urlInbox);
	CHECK_PTR(box_inbox);
	
	EmpathFolder * folder_outbox = new EmpathFolder(urlOutbox);
	CHECK_PTR(folder_outbox);
	
	EmpathMaildir * box_outbox =
		new EmpathMaildir(path_, urlOutbox);
	CHECK_PTR(box_outbox);
	
	EmpathFolder * folder_drafts = new EmpathFolder(urlDrafts);
	CHECK_PTR(folder_drafts);
	
	EmpathMaildir * box_drafts =
		new EmpathMaildir(path_, urlDrafts);
	CHECK_PTR(box_drafts);
	
	EmpathFolder * folder_sent = new EmpathFolder(urlSent);
	CHECK_PTR(folder_sent);
	
	EmpathMaildir * box_sent =
		new EmpathMaildir(path_, urlSent);
	CHECK_PTR(box_sent);
	
	EmpathFolder * folder_trash = new EmpathFolder(urlTrash);
	CHECK_PTR(folder_trash);
	
	EmpathMaildir * box_trash =
		new EmpathMaildir(path_, urlTrash);
	CHECK_PTR(box_trash);
	
	folderList_.append(folder_trash);
	folderList_.append(folder_sent);
	folderList_.append(folder_drafts);
	folderList_.append(folder_outbox);
	folderList_.append(folder_inbox);
	
	boxList_.append(box_trash);
	boxList_.append(box_sent);
	boxList_.append(box_drafts);
	boxList_.append(box_outbox);
	boxList_.append(box_inbox);
	
	saveConfig();
}
	
	bool
EmpathMailboxMaildir::writeMessage(const EmpathURL & folder, RMessage & m)
{
	empathDebug("writeMessage() called");
	
	EmpathMaildir * box = _box(folder);
	if (box == 0) return 0;
	box->writeMessage(m);
	
	return true;
}

	bool
EmpathMailboxMaildir::newMail() const
{
	return false;
}

	void
EmpathMailboxMaildir::saveConfig()
{
	empathDebug("saveConfig() called - my name is " + url_.asString());
	
	KConfig * c = kapp->getConfig();
	c->setGroup(EmpathConfig::GROUP_MAILBOX + url_.mailboxName());
	
	c->writeEntry(EmpathConfig::KEY_MAILBOX_TYPE, type_);
	c->writeEntry(EmpathConfig::KEY_LOCAL_MAILBOX_PATH, path_);
	
	c->writeEntry(EmpathConfig::KEY_CHECK_MAIL, checkMail_);
	c->writeEntry(EmpathConfig::KEY_CHECK_MAIL_INTERVAL, checkMailInterval_);
	c->sync();
}

	void
EmpathMailboxMaildir::readConfig()
{
	empathDebug("readConfig() called");

	KConfig * c = kapp->getConfig();
	c->setGroup(EmpathConfig::GROUP_MAILBOX + url_.mailboxName());
	
	checkMail_ = c->readUnsignedNumEntry(EmpathConfig::KEY_CHECK_MAIL);
	checkMailInterval_ =
		c->readUnsignedNumEntry(EmpathConfig::KEY_CHECK_MAIL_INTERVAL);
	
	folderList_.clear();
	boxList_.clear();
	
	path_ = c->readEntry(EmpathConfig::KEY_LOCAL_MAILBOX_PATH);
	if (path_.at(path_.length()) != '/') path_ += '/';
	_recursiveReadFolders(path_);
}

	void
EmpathMailboxMaildir::_recursiveReadFolders(const QString & currentDir)
{
	empathDebug("_recursiveReadFolders(" + currentDir + ") called");
	// We need to look at the maildir base directory, and go recursively
	// through subdirs. Any subdir that has cur, tmp and new is a Maildir
	// folder.

	QDir d(currentDir);
	d.setFilter(QDir::Dirs);
	
	if (d.count() == 0) {
		empathDebug("No folders in maildir");
		return;
	}
	
	QStringList l(d.entryList());
	
	QStringList::Iterator it(l.begin());
	
	bool hasCur, hasNew, hasTmp;
	hasCur = hasNew = hasTmp = false;
	
	for (; it != l.end(); ++it) {
		
		if ((*it).left(1) == ".")
			continue;
		
		if (*it == "cur") {
			hasCur = true;
			continue;
		}
		
		if (*it == "new") {
			hasNew = true;
			continue;
		}
		
		if (*it == "tmp") {
			hasTmp = true;
			continue;
		}
		
		_recursiveReadFolders(currentDir + "/" + *it);
	}
	
	if (hasCur && hasNew && hasTmp) {
		
		// This dir is itself a maildir folder.
		
		QString s(d.absPath());
		s.remove(0, path_.length());
		empathDebug("Folder path is " + s);
		
		EmpathURL url(url_.mailboxName(), s, QString::null);
		EmpathFolder * f = new EmpathFolder(url);
		
		CHECK_PTR(f);

		folderList_.append(f);
		
		EmpathMaildir * m = new EmpathMaildir(path_, url);
		CHECK_PTR(m);
		
		boxList_.append(m);
		emit(updateFolderLists());
		
		m->init();
{'fraction_non_alphanumeric': 0.1345619987661937, 'fraction_numerical': 0.004780999383096854, 'mean_word_length': 4.979253112033195, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1668908', 'n_tokens_mistral': 4934, 'n_tokens_neox': 4571, 'n_words': 1043}