From 618cc43b64d2acb8614c7f49eaadf8028d606539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 14 Jan 2016 20:42:48 +0100 Subject: [PATCH] IMAP: New folders will now adopt default mail attributes. * Added a function CopyMailFolderAttributes() that copies the attribute layout from the text/x-email default query folder. * This using the new CopyAttributes() method in libshared that is pretty much a copy of a similar method from copyattr. However, I did not replace the latter, as that one allows for more fine grained error reporting (and attribute filtering). * Closes ticket #3498. --- headers/private/mail/mail_util.h | 4 +- headers/private/shared/AttributeUtilities.h | 24 +++++++ .../inbound_protocols/imap/IMAPProtocol.cpp | 4 ++ src/kits/mail/Jamfile | 2 +- src/kits/mail/mail_util.cpp | 20 ++++++ src/kits/shared/AttributeUtilities.cpp | 66 +++++++++++++++++++ src/kits/shared/Jamfile | 1 + 7 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 headers/private/shared/AttributeUtilities.h create mode 100644 src/kits/shared/AttributeUtilities.cpp diff --git a/headers/private/mail/mail_util.h b/headers/private/mail/mail_util.h index 021d069636..eafcda9406 100644 --- a/headers/private/mail/mail_util.h +++ b/headers/private/mail/mail_util.h @@ -1,5 +1,5 @@ /* - * Copyright 2011, Haiku, Inc. All rights reserved. + * Copyright 2011-2016, Haiku, Inc. All rights reserved. * Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved. */ #ifndef ZOIDBERG_GARGOYLE_MAIL_UTIL_H @@ -102,5 +102,7 @@ void extract_address_name(BString &address); void get_address_list(BList &list, const char *string, void (*cleanupFunc)(BString &) = NULL); +status_t CopyMailFolderAttributes(const char* targetPath); + #endif /* ZOIDBERG_GARGOYLE_MAIL_UTIL_H */ diff --git a/headers/private/shared/AttributeUtilities.h b/headers/private/shared/AttributeUtilities.h new file mode 100644 index 0000000000..85bb3dc9a1 --- /dev/null +++ b/headers/private/shared/AttributeUtilities.h @@ -0,0 +1,24 @@ +/* + * Copyright 2016 Haiku Inc. All rights reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef ATTRIBUTE_UTILITIES_H +#define ATTRIBUTE_UTILITIES_H + + +#include + + +class BNode; + + +namespace BPrivate { + + +status_t CopyAttributes(BNode& source, BNode& destination); + + +} // namespace BPrivate + + +#endif // ATTRIBUTE_UTILITIES_H diff --git a/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPProtocol.cpp b/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPProtocol.cpp index b25ba73301..8ad0f54f2b 100644 --- a/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPProtocol.cpp +++ b/src/add-ons/mail_daemon/inbound_protocols/imap/IMAPProtocol.cpp @@ -13,6 +13,8 @@ #include "IMAPFolder.h" #include "Utilities.h" +#include + IMAPProtocol::IMAPProtocol(const BMailAccountSettings& settings) : @@ -280,6 +282,8 @@ IMAPProtocol::_CreateFolder(const BString& mailbox, const BString& separator) return NULL; } + CopyMailFolderAttributes(path.Path()); + entry_ref ref; status = get_ref_for_path(path.Path(), &ref); if (status != B_OK) { diff --git a/src/kits/mail/Jamfile b/src/kits/mail/Jamfile index 7eb8a177a4..9d8b93e865 100644 --- a/src/kits/mail/Jamfile +++ b/src/kits/mail/Jamfile @@ -58,7 +58,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { SharedLibrary $(libmail) : $(sources) : - be bnetapi textencoding tracker + be bnetapi textencoding tracker libshared.a [ TargetLibstdc++ ] $(TARGET_NETWORK_LIBS) $(TARGET_SELECT_UNAME_ETC_LIB) diff --git a/src/kits/mail/mail_util.cpp b/src/kits/mail/mail_util.cpp index 8fec8828d4..1ae92c7ac8 100644 --- a/src/kits/mail/mail_util.cpp +++ b/src/kits/mail/mail_util.cpp @@ -14,14 +14,17 @@ #include #include +#include #include #include #include +#include #include #include #include +#include #include #include @@ -1547,3 +1550,20 @@ get_address_list(BList &list, const char *string, } } + +status_t +CopyMailFolderAttributes(const char* targetPath) +{ + BPath path; + status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); + if (status != B_OK) + return status; + + path.Append("Tracker"); + path.Append("DefaultQueryTemplates"); + path.Append("text_x-email"); + + BNode source(path.Path()); + BNode target(targetPath); + return BPrivate::CopyAttributes(source, target); +} diff --git a/src/kits/shared/AttributeUtilities.cpp b/src/kits/shared/AttributeUtilities.cpp new file mode 100644 index 0000000000..76f6f27eb4 --- /dev/null +++ b/src/kits/shared/AttributeUtilities.cpp @@ -0,0 +1,66 @@ +/* + * Copyright 2005-2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Copyright 2016, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include "AttributeUtilities.h" + +#include +#include +#include + + +static const int kCopyBufferSize = 64 * 1024; + // 64 KB + + +namespace BPrivate { + + +status_t +CopyAttributes(BNode& source, BNode& destination) +{ + char attrName[B_ATTR_NAME_LENGTH]; + while (source.GetNextAttrName(attrName) == B_OK) { + // get attr info + attr_info attrInfo; + status_t status = source.GetAttrInfo(attrName, &attrInfo); + if (status != B_OK) + return status; + + // copy the attribute + char buffer[kCopyBufferSize]; + off_t offset = 0; + off_t bytesLeft = attrInfo.size; + + // Go at least once through the loop, so that an empty attribute will be + // created as well + do { + size_t toRead = kCopyBufferSize; + if ((off_t)toRead > bytesLeft) + toRead = bytesLeft; + + // read + ssize_t bytesRead = source.ReadAttr(attrName, attrInfo.type, + offset, buffer, toRead); + if (bytesRead < 0) + return bytesRead; + + // write + ssize_t bytesWritten = destination.WriteAttr(attrName, + attrInfo.type, offset, buffer, bytesRead); + if (bytesWritten < 0) + return bytesWritten; + + bytesLeft -= bytesRead; + offset += bytesRead; + } while (bytesLeft > 0); + } + return B_OK; +} + + +} // namespace BPrivate + diff --git a/src/kits/shared/Jamfile b/src/kits/shared/Jamfile index fed99a5342..a5a2daebb7 100644 --- a/src/kits/shared/Jamfile +++ b/src/kits/shared/Jamfile @@ -29,6 +29,7 @@ for architectureObject in [ MultiArchSubDirSetup ] { StaticLibrary [ MultiArchDefaultGristFiles libshared.a ] : AboutMenuItem.cpp ArgumentVector.cpp + AttributeUtilities.cpp CalendarView.cpp ColorQuantizer.cpp CommandPipe.cpp