summaryrefslogtreecommitdiff
path: root/mailnews/jsaccount/src/JaUrl.cpp
blob: a8e93bc122135fed45d2ad57c06128b18e58e16e (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
/* -*- Mode: C++; tab-width: 8; 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 "JaUrl.h"
#include "nsComponentManagerUtils.h"
#include "nsIFile.h"
#include "nsIMessenger.h"
#include "nsIMsgHdr.h"
#include "nsISupportsUtils.h"
#include "nsMsgBaseCID.h"
#include "nsMsgUtils.h"

// This file contains an implementation of mailnews URLs in JsAccount.

namespace mozilla {
namespace mailnews {

NS_IMPL_ISUPPORTS_INHERITED(JaBaseCppUrl, nsMsgMailNewsUrl,
                            nsIMsgMessageUrl,
                            nsIInterfaceRequestor,
                            nsISupportsWeakReference)

// nsIMsgMailNewsUrl overrides
NS_IMETHODIMP JaBaseCppUrl::GetFolder(nsIMsgFolder **aFolder)
{
  NS_ENSURE_ARG_POINTER(aFolder);
  NS_IF_ADDREF(*aFolder = mFolder);
  return NS_OK;
}

NS_IMETHODIMP JaBaseCppUrl::SetFolder(nsIMsgFolder *aFolder)
{
  mFolder = aFolder;
  return NS_OK;
}

// nsIMsgMessageUrl implementation
NS_IMETHODIMP JaBaseCppUrl::GetUri(char **aUri)
{
  if (!mUri.IsEmpty())
    *aUri = ToNewCString(mUri);
  else
    return NS_ERROR_NOT_INITIALIZED;
  return NS_OK;
}
NS_IMETHODIMP JaBaseCppUrl::SetUri(const char *aUri)
{
  mUri = aUri;
  return NS_OK;
}

NS_IMETHODIMP JaBaseCppUrl::GetMessageFile(nsIFile **aMessageFile)
{
  NS_ENSURE_ARG_POINTER(aMessageFile);
  NS_IF_ADDREF(*aMessageFile = mMessageFile);
  return NS_OK;
}
NS_IMETHODIMP JaBaseCppUrl::SetMessageFile(nsIFile *aMessageFile)
{
  mMessageFile = aMessageFile;
  return NS_OK;
}

NS_IMETHODIMP JaBaseCppUrl::GetAddDummyEnvelope(bool *aAddDummyEnvelope)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP JaBaseCppUrl::SetAddDummyEnvelope(bool aAddDummyEnvelope)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP JaBaseCppUrl::GetCanonicalLineEnding(bool *aCanonicalLineEnding)
{
  NS_ENSURE_ARG_POINTER(aCanonicalLineEnding);
  *aCanonicalLineEnding = mCanonicalLineEnding;
  return NS_OK;
}
NS_IMETHODIMP JaBaseCppUrl::SetCanonicalLineEnding(bool aCanonicalLineEnding)
{
  mCanonicalLineEnding = aCanonicalLineEnding;
  return NS_OK;
}

NS_IMETHODIMP JaBaseCppUrl::GetOriginalSpec(char **aOriginalSpec)
{
  if (!aOriginalSpec || mOriginalSpec.IsEmpty())
    return NS_ERROR_NULL_POINTER;
  *aOriginalSpec = ToNewCString(mOriginalSpec);
  return NS_OK;
}
NS_IMETHODIMP JaBaseCppUrl::SetOriginalSpec(const char *aOriginalSpec)
{
  mOriginalSpec = aOriginalSpec;
  return NS_OK;
}

NS_IMETHODIMP JaBaseCppUrl::GetPrincipalSpec(nsACString& aPrincipalSpec)
{
  // URLs contain a lot of query parts. We want need a normalised form:
  // scheme://server/folder?number=123
  nsCOMPtr<nsIMsgMailNewsUrl> mailnewsURL;
  QueryInterface(NS_GET_IID(nsIMsgMailNewsUrl), getter_AddRefs(mailnewsURL));

  nsAutoCString spec;
  mailnewsURL->GetSpecIgnoringRef(spec);

  nsAutoCString queryPart = MsgExtractQueryPart(spec, "number=");

  // Strip any query part beginning with ? or /;
  int32_t ind = spec.Find("/;");
  if (ind != kNotFound)
    spec.SetLength(ind);

  ind = spec.FindChar('?');
  if (ind != kNotFound)
    spec.SetLength(ind);

  if (!queryPart.IsEmpty())
    spec += NS_LITERAL_CSTRING("?") + queryPart;

  aPrincipalSpec.Assign(spec);
  return NS_OK;
}

NS_IMETHODIMP JaBaseCppUrl::GetMessageHeader(nsIMsgDBHdr **aMessageHeader)
{
  // This routine does a lookup using messenger, assumming that the message URI
  // has been set in mUri.
  NS_ENSURE_TRUE(!mUri.IsEmpty(), NS_ERROR_NOT_INITIALIZED);
  nsresult rv;
  nsCOMPtr<nsIMessenger> messenger(do_CreateInstance(NS_MESSENGER_CONTRACTID, &rv));
  NS_ENSURE_SUCCESS(rv, rv);
  nsCOMPtr<nsIMsgDBHdr> msgHdr;
  rv = messenger->MsgHdrFromURI(mUri, getter_AddRefs(msgHdr));
  NS_ENSURE_SUCCESS(rv, rv);
  msgHdr.forget(aMessageHeader);
  return NS_OK;
}

NS_IMETHODIMP JaBaseCppUrl::SetMessageHeader(nsIMsgDBHdr *aMsgHdr)
{
  return NS_ERROR_NOT_IMPLEMENTED;
}

// nsIInterfaceRequestor implementation
NS_IMETHODIMP JaBaseCppUrl::GetInterface(const nsIID & aIID, void **aSink)
{
  return QueryInterface(aIID, aSink);
}

// Delegator
NS_IMPL_ISUPPORTS_INHERITED(JaCppUrlDelegator,
                            JaBaseCppUrl,
                            msgIOverride)

// Delegator object to bypass JS method override.
NS_IMPL_ISUPPORTS(JaCppUrlDelegator::Super,
                  nsIMsgMailNewsUrl,
                  nsIMsgMessageUrl,
                  nsIURI,
                  nsIURL,
                  nsIInterfaceRequestor)

JaCppUrlDelegator::JaCppUrlDelegator() :
  mCppBase(new Super(this)),
  mMethods(nullptr)
{ }

NS_IMETHODIMP JaCppUrlDelegator::SetMethodsToDelegate(msgIDelegateList *aDelegateList)
{
  if (!aDelegateList)
  {
    NS_WARNING("Null delegate list");
    return NS_ERROR_NULL_POINTER;
  }
  // We static_cast since we want to use the hash object directly.
  mDelegateList = static_cast<DelegateList*> (aDelegateList);
  mMethods = &(mDelegateList->mMethods);
  return NS_OK;
}
NS_IMETHODIMP JaCppUrlDelegator::GetMethodsToDelegate(msgIDelegateList **aDelegateList)
{
  if (!mDelegateList)
    mDelegateList = new DelegateList("mozilla::mailnews::JaCppUrlDelegator::");
  mMethods = &(mDelegateList->mMethods);
  NS_ADDREF(*aDelegateList = mDelegateList);
  return NS_OK;
}

NS_IMETHODIMP JaCppUrlDelegator::SetJsDelegate(nsISupports *aJsDelegate)
{
  // If these QIs fail, then overrides are not provided for methods in that
  // interface, which is OK.
  mJsISupports = aJsDelegate;
  mJsIMsgMailNewsUrl = do_QueryInterface(aJsDelegate);
  mJsIURI = do_QueryInterface(aJsDelegate);
  mJsIURL = do_QueryInterface(aJsDelegate);
  mJsIMsgMessageUrl = do_QueryInterface(aJsDelegate);
  mJsIInterfaceRequestor = do_QueryInterface(aJsDelegate);
  return NS_OK;
}
NS_IMETHODIMP JaCppUrlDelegator::GetJsDelegate(nsISupports **aJsDelegate)
{
  NS_ENSURE_ARG_POINTER(aJsDelegate);
  if (mJsISupports)
  {
    NS_ADDREF(*aJsDelegate = mJsISupports);
    return NS_OK;
  }
  return NS_ERROR_NOT_INITIALIZED;
}

NS_IMETHODIMP JaCppUrlDelegator::GetCppBase(nsISupports **aCppBase)
{
  nsCOMPtr<nsISupports> cppBaseSupports;
  cppBaseSupports = NS_ISUPPORTS_CAST(nsIMsgMailNewsUrl*, mCppBase);
  NS_ENSURE_STATE(cppBaseSupports);
  cppBaseSupports.forget(aCppBase);

  return NS_OK;
}

} // namespace mailnews
} // namespace mozilla