diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /comm/mailnews/extensions/mailviews | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | comm/mailnews/extensions/mailviews/components.conf | 12 | ||||
-rw-r--r-- | comm/mailnews/extensions/mailviews/mailViews.dat | 22 | ||||
-rw-r--r-- | comm/mailnews/extensions/mailviews/moz.build | 25 | ||||
-rw-r--r-- | comm/mailnews/extensions/mailviews/nsIMsgMailView.idl | 24 | ||||
-rw-r--r-- | comm/mailnews/extensions/mailviews/nsIMsgMailViewList.idl | 28 | ||||
-rw-r--r-- | comm/mailnews/extensions/mailviews/nsMsgMailViewList.cpp | 281 | ||||
-rw-r--r-- | comm/mailnews/extensions/mailviews/nsMsgMailViewList.h | 51 |
7 files changed, 443 insertions, 0 deletions
diff --git a/comm/mailnews/extensions/mailviews/components.conf b/comm/mailnews/extensions/mailviews/components.conf new file mode 100644 index 0000000000..757d1eca93 --- /dev/null +++ b/comm/mailnews/extensions/mailviews/components.conf @@ -0,0 +1,12 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, you can obtain one at http://mozilla.org/MPL/2.0/. + +Classes = [ + { + "cid": "{a0258267-44fd-4886-a858-8192615178ec}", + "contract_ids": ["@mozilla.org/messenger/mailviewlist;1"], + "type": "nsMsgMailViewList", + "headers": ["/comm/mailnews/extensions/mailviews/nsMsgMailViewList.h"], + }, +] diff --git a/comm/mailnews/extensions/mailviews/mailViews.dat b/comm/mailnews/extensions/mailviews/mailViews.dat new file mode 100644 index 0000000000..3c5af3831c --- /dev/null +++ b/comm/mailnews/extensions/mailviews/mailViews.dat @@ -0,0 +1,22 @@ +version="9" +logging="no" +name="People I Know" +enabled="yes" +type="1" +condition="AND (from,is in ab,jsaddrbook://abook.sqlite)" +name="Recent Mail" +enabled="yes" +type="1" +condition="AND (age in days,is less than,1)" +name="Last 5 Days" +enabled="yes" +type="1" +condition="AND (age in days,is less than,5)" +name="Not Junk" +enabled="yes" +type="1" +condition="AND (junk status,isn't,2)" +name="Has Attachments" +enabled="yes" +type="1" +condition="AND (has attachment status,is,true)" diff --git a/comm/mailnews/extensions/mailviews/moz.build b/comm/mailnews/extensions/mailviews/moz.build new file mode 100644 index 0000000000..de2df9ec3a --- /dev/null +++ b/comm/mailnews/extensions/mailviews/moz.build @@ -0,0 +1,25 @@ +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +SOURCES += [ + "nsMsgMailViewList.cpp", +] + +FINAL_LIBRARY = "mail" + +XPIDL_SOURCES += [ + "nsIMsgMailView.idl", + "nsIMsgMailViewList.idl", +] + +XPIDL_MODULE = "mailview" + +FINAL_TARGET_FILES.defaults.messenger += [ + "mailViews.dat", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] diff --git a/comm/mailnews/extensions/mailviews/nsIMsgMailView.idl b/comm/mailnews/extensions/mailviews/nsIMsgMailView.idl new file mode 100644 index 0000000000..730cdcbc39 --- /dev/null +++ b/comm/mailnews/extensions/mailviews/nsIMsgMailView.idl @@ -0,0 +1,24 @@ +/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +#include "nsISupports.idl" + +interface nsIMsgSearchTerm; + +[scriptable, uuid(28AC84DF-CBE5-430d-A5C0-4FA63B5424DF)] +interface nsIMsgMailView : nsISupports { + attribute wstring mailViewName; + readonly attribute wstring prettyName; // localized pretty name + + // the array of search terms + attribute Array<nsIMsgSearchTerm> searchTerms; + + // these two helper methods are required to allow searchTermsOverlay.js to + // manipulate a mail view without knowing it is dealing with a mail view. nsIMsgFilter + // and nsIMsgSearchSession have the same two methods....we should probably make an interface around them. + void appendTerm(in nsIMsgSearchTerm term); + nsIMsgSearchTerm createTerm(); +}; diff --git a/comm/mailnews/extensions/mailviews/nsIMsgMailViewList.idl b/comm/mailnews/extensions/mailviews/nsIMsgMailViewList.idl new file mode 100644 index 0000000000..cc8b2f6d4b --- /dev/null +++ b/comm/mailnews/extensions/mailviews/nsIMsgMailViewList.idl @@ -0,0 +1,28 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsISupports.idl" +#include "nsIMsgMailView.idl" + +/////////////////////////////////////////////////////////////////////////////// +// A mail view list is a list of mail views a particular implementor provides +/////////////////////////////////////////////////////////////////////////////// + +typedef long nsMsgMailViewListFileAttribValue; + +[scriptable, uuid(6DD798D7-9528-49e6-9447-3AAF14D2D36F)] +interface nsIMsgMailViewList : nsISupports { + + readonly attribute unsigned long mailViewCount; + + nsIMsgMailView getMailViewAt(in unsigned long mailViewIndex); + + void addMailView(in nsIMsgMailView mailView); + void removeMailView(in nsIMsgMailView mailView); + + nsIMsgMailView createMailView(); + + void save(); +}; diff --git a/comm/mailnews/extensions/mailviews/nsMsgMailViewList.cpp b/comm/mailnews/extensions/mailviews/nsMsgMailViewList.cpp new file mode 100644 index 0000000000..753061a540 --- /dev/null +++ b/comm/mailnews/extensions/mailviews/nsMsgMailViewList.cpp @@ -0,0 +1,281 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "nsMsgMailViewList.h" +#include "nsArray.h" +#include "nsIMsgFilterService.h" +#include "nsIMsgMailSession.h" +#include "nsIMsgSearchTerm.h" +#include "nsAppDirectoryServiceDefs.h" +#include "nsDirectoryServiceUtils.h" +#include "nsIFile.h" +#include "nsComponentManagerUtils.h" +#include "mozilla/Components.h" +#include "nsIMsgFilter.h" + +#define kDefaultViewPeopleIKnow "People I Know" +#define kDefaultViewRecent "Recent Mail" +#define kDefaultViewFiveDays "Last 5 Days" +#define kDefaultViewNotJunk "Not Junk" +#define kDefaultViewHasAttachments "Has Attachments" + +nsMsgMailView::nsMsgMailView() {} + +NS_IMPL_ADDREF(nsMsgMailView) +NS_IMPL_RELEASE(nsMsgMailView) +NS_IMPL_QUERY_INTERFACE(nsMsgMailView, nsIMsgMailView) + +nsMsgMailView::~nsMsgMailView() {} + +NS_IMETHODIMP nsMsgMailView::GetMailViewName(char16_t** aMailViewName) { + NS_ENSURE_ARG_POINTER(aMailViewName); + + *aMailViewName = ToNewUnicode(mName); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailView::SetMailViewName(const char16_t* aMailViewName) { + mName = aMailViewName; + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailView::GetPrettyName(char16_t** aMailViewName) { + NS_ENSURE_ARG_POINTER(aMailViewName); + + nsresult rv = NS_OK; + if (!mBundle) { + nsCOMPtr<nsIStringBundleService> bundleService = + mozilla::components::StringBundle::Service(); + NS_ENSURE_TRUE(bundleService, NS_ERROR_UNEXPECTED); + bundleService->CreateBundle( + "chrome://messenger/locale/mailviews.properties", + getter_AddRefs(mBundle)); + } + + NS_ENSURE_TRUE(mBundle, NS_ERROR_FAILURE); + + // see if mName has an associated pretty name inside our string bundle and if + // so, use that as the pretty name otherwise just return mName + nsAutoString mailViewName; + if (mName.EqualsLiteral(kDefaultViewPeopleIKnow)) { + rv = mBundle->GetStringFromName("mailViewPeopleIKnow", mailViewName); + *aMailViewName = ToNewUnicode(mailViewName); + } else if (mName.EqualsLiteral(kDefaultViewRecent)) { + rv = mBundle->GetStringFromName("mailViewRecentMail", mailViewName); + *aMailViewName = ToNewUnicode(mailViewName); + } else if (mName.EqualsLiteral(kDefaultViewFiveDays)) { + rv = mBundle->GetStringFromName("mailViewLastFiveDays", mailViewName); + *aMailViewName = ToNewUnicode(mailViewName); + } else if (mName.EqualsLiteral(kDefaultViewNotJunk)) { + rv = mBundle->GetStringFromName("mailViewNotJunk", mailViewName); + *aMailViewName = ToNewUnicode(mailViewName); + } else if (mName.EqualsLiteral(kDefaultViewHasAttachments)) { + rv = mBundle->GetStringFromName("mailViewHasAttachments", mailViewName); + *aMailViewName = ToNewUnicode(mailViewName); + } else { + *aMailViewName = ToNewUnicode(mName); + } + + return rv; +} + +NS_IMETHODIMP nsMsgMailView::GetSearchTerms( + nsTArray<RefPtr<nsIMsgSearchTerm>>& terms) { + terms = mViewSearchTerms.Clone(); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailView::SetSearchTerms( + nsTArray<RefPtr<nsIMsgSearchTerm>> const& terms) { + mViewSearchTerms = terms.Clone(); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailView::AppendTerm(nsIMsgSearchTerm* aTerm) { + NS_ENSURE_TRUE(aTerm, NS_ERROR_NULL_POINTER); + + mViewSearchTerms.AppendElement(aTerm); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailView::CreateTerm(nsIMsgSearchTerm** aResult) { + NS_ENSURE_ARG_POINTER(aResult); + nsCOMPtr<nsIMsgSearchTerm> searchTerm = + do_CreateInstance("@mozilla.org/messenger/searchTerm;1"); + searchTerm.forget(aResult); + return NS_OK; +} + +///////////////////////////////////////////////////////////////////////////// +// nsMsgMailViewList implementation +///////////////////////////////////////////////////////////////////////////// +nsMsgMailViewList::nsMsgMailViewList() { LoadMailViews(); } + +NS_IMPL_ADDREF(nsMsgMailViewList) +NS_IMPL_RELEASE(nsMsgMailViewList) +NS_IMPL_QUERY_INTERFACE(nsMsgMailViewList, nsIMsgMailViewList) + +nsMsgMailViewList::~nsMsgMailViewList() {} + +NS_IMETHODIMP nsMsgMailViewList::GetMailViewCount(uint32_t* aCount) { + NS_ENSURE_ARG_POINTER(aCount); + + *aCount = m_mailViews.Length(); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailViewList::GetMailViewAt(uint32_t aMailViewIndex, + nsIMsgMailView** aMailView) { + NS_ENSURE_ARG_POINTER(aMailView); + + uint32_t mailViewCount = m_mailViews.Length(); + + NS_ENSURE_ARG(mailViewCount > aMailViewIndex); + + NS_IF_ADDREF(*aMailView = m_mailViews[aMailViewIndex]); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailViewList::AddMailView(nsIMsgMailView* aMailView) { + NS_ENSURE_ARG_POINTER(aMailView); + + m_mailViews.AppendElement(aMailView); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailViewList::RemoveMailView(nsIMsgMailView* aMailView) { + NS_ENSURE_ARG_POINTER(aMailView); + + m_mailViews.RemoveElement(aMailView); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailViewList::CreateMailView(nsIMsgMailView** aMailView) { + NS_ENSURE_ARG_POINTER(aMailView); + NS_ADDREF(*aMailView = new nsMsgMailView); + return NS_OK; +} + +NS_IMETHODIMP nsMsgMailViewList::Save() { + // brute force...remove all the old filters in our filter list, then we'll + // re-add our current list + nsCOMPtr<nsIMsgFilter> msgFilter; + uint32_t numFilters = 0; + if (mFilterList) mFilterList->GetFilterCount(&numFilters); + while (numFilters) { + mFilterList->RemoveFilterAt(numFilters - 1); + numFilters--; + } + + // now convert our mail view list into a filter list and save it + ConvertMailViewListToFilterList(); + + // now save the filters to our file + return mFilterList ? mFilterList->SaveToDefaultFile() : NS_ERROR_FAILURE; +} + +nsresult nsMsgMailViewList::ConvertMailViewListToFilterList() { + uint32_t mailViewCount = m_mailViews.Length(); + nsCOMPtr<nsIMsgMailView> mailView; + nsCOMPtr<nsIMsgFilter> newMailFilter; + nsString mailViewName; + for (uint32_t index = 0; index < mailViewCount; index++) { + GetMailViewAt(index, getter_AddRefs(mailView)); + if (!mailView) continue; + mailView->GetMailViewName(getter_Copies(mailViewName)); + mFilterList->CreateFilter(mailViewName, getter_AddRefs(newMailFilter)); + if (!newMailFilter) continue; + + nsTArray<RefPtr<nsIMsgSearchTerm>> searchTerms; + mailView->GetSearchTerms(searchTerms); + newMailFilter->SetSearchTerms(searchTerms); + mFilterList->InsertFilterAt(index, newMailFilter); + } + + return NS_OK; +} + +nsresult nsMsgMailViewList::LoadMailViews() { + nsCOMPtr<nsIFile> file; + nsresult rv = + NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = file->AppendNative("mailViews.dat"_ns); + + // if the file doesn't exist, we should try to get it from the defaults + // directory and copy it over + bool exists = false; + file->Exists(&exists); + if (!exists) { + nsCOMPtr<nsIMsgMailSession> mailSession = + do_GetService("@mozilla.org/messenger/services/session;1", &rv); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr<nsIFile> defaultMessagesFile; + nsCOMPtr<nsIFile> profileDir; + rv = mailSession->GetDataFilesDir("messenger", + getter_AddRefs(defaultMessagesFile)); + rv = defaultMessagesFile->AppendNative("mailViews.dat"_ns); + + // get the profile directory + rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, + getter_AddRefs(profileDir)); + + // now copy the file over to the profile directory + defaultMessagesFile->CopyToNative(profileDir, EmptyCString()); + } + // this is kind of a hack but I think it will be an effective hack. The filter + // service already knows how to take a nsIFile and parse the contents into + // filters which are very similar to mail views. Instead of re-writing all of + // that dirty parsing code, let's just re-use it then convert the results into + // a data structure we wish to give to our consumers. + + nsCOMPtr<nsIMsgFilterService> filterService = + do_GetService("@mozilla.org/messenger/services/filters;1", &rv); + nsCOMPtr<nsIMsgFilterList> mfilterList; + + rv = filterService->OpenFilterList(file, nullptr, nullptr, + getter_AddRefs(mFilterList)); + NS_ENSURE_SUCCESS(rv, rv); + + return ConvertFilterListToMailViews(); +} +/** + * Converts the filter list into our mail view objects, + * stripping out just the info we need. + */ +nsresult nsMsgMailViewList::ConvertFilterListToMailViews() { + nsresult rv = NS_OK; + m_mailViews.Clear(); + + // iterate over each filter in the list + uint32_t numFilters = 0; + mFilterList->GetFilterCount(&numFilters); + for (uint32_t index = 0; index < numFilters; index++) { + nsCOMPtr<nsIMsgFilter> msgFilter; + rv = mFilterList->GetFilterAt(index, getter_AddRefs(msgFilter)); + if (NS_FAILED(rv) || !msgFilter) continue; + + // create a new nsIMsgMailView for this item + nsCOMPtr<nsIMsgMailView> newMailView; + rv = CreateMailView(getter_AddRefs(newMailView)); + NS_ENSURE_SUCCESS(rv, rv); + + nsString filterName; + msgFilter->GetFilterName(filterName); + newMailView->SetMailViewName(filterName.get()); + + nsTArray<RefPtr<nsIMsgSearchTerm>> filterSearchTerms; + rv = msgFilter->GetSearchTerms(filterSearchTerms); + NS_ENSURE_SUCCESS(rv, rv); + rv = newMailView->SetSearchTerms(filterSearchTerms); + NS_ENSURE_SUCCESS(rv, rv); + + // now append this new mail view to our global list view + m_mailViews.AppendElement(newMailView); + } + + return rv; +} diff --git a/comm/mailnews/extensions/mailviews/nsMsgMailViewList.h b/comm/mailnews/extensions/mailviews/nsMsgMailViewList.h new file mode 100644 index 0000000000..19e283681c --- /dev/null +++ b/comm/mailnews/extensions/mailviews/nsMsgMailViewList.h @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef _nsMsgMailViewList_H_ +#define _nsMsgMailViewList_H_ + +#include "nscore.h" +#include "nsIMsgMailViewList.h" +#include "nsCOMPtr.h" +#include "nsCOMArray.h" +#include "nsIStringBundle.h" +#include "nsString.h" +#include "nsIMsgFilterList.h" + +// a mail View is just a name and an array of search terms +class nsMsgMailView : public nsIMsgMailView { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIMSGMAILVIEW + + nsMsgMailView(); + + protected: + virtual ~nsMsgMailView(); + nsString mName; + nsCOMPtr<nsIStringBundle> mBundle; + nsTArray<RefPtr<nsIMsgSearchTerm>> mViewSearchTerms; +}; + +class nsMsgMailViewList : public nsIMsgMailViewList { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIMSGMAILVIEWLIST + + nsMsgMailViewList(); + + protected: + virtual ~nsMsgMailViewList(); + nsresult + LoadMailViews(); // reads in user defined mail views from our default file + nsresult ConvertFilterListToMailViews(); + nsresult ConvertMailViewListToFilterList(); + + nsCOMArray<nsIMsgMailView> m_mailViews; + nsCOMPtr<nsIMsgFilterList> + mFilterList; // our internal filter list representation +}; + +#endif |