summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/import/src/nsWMImport.cpp
blob: a3b5dd45b262950d767f2d1690fc23d19015855d (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
/* -*- 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/. */

/*
 * Windows Live Mail (Win32) import mail and addressbook interfaces
 */

#include "nscore.h"
#include "nsString.h"
#include "nsMsgUtils.h"
#include "nsWMImport.h"
#include "nsIImportMail.h"
#include "nsIImportMailboxDescriptor.h"
#include "nsXPCOM.h"
#include "nsWMSettings.h"
#include "nsTextFormatter.h"
#include "nsWMStringBundle.h"
#include "nsUnicharUtils.h"

#include "ImportDebug.h"

class ImportWMMailImpl : public nsIImportMail {
 public:
  ImportWMMailImpl();

  static nsresult Create(nsIImportMail** aImport);

  // nsISupports interface
  NS_DECL_THREADSAFE_ISUPPORTS

  // nsIImportmail interface

  /* void GetDefaultLocation (out nsIFile location, out boolean found, out
   * boolean userVerify); */
  NS_IMETHOD GetDefaultLocation(nsIFile** location, bool* found,
                                bool* userVerify);

  /* nsIArray FindMailboxes (in nsIFile location); */
  NS_IMETHOD FindMailboxes(nsIFile* location,
                           nsTArray<RefPtr<nsIImportMailboxDescriptor>>& boxes);

  NS_IMETHOD ImportMailbox(nsIImportMailboxDescriptor* source,
                           nsIMsgFolder* dstFolder, char16_t** pErrorLog,
                           char16_t** pSuccessLog, bool* fatalError);

  /* unsigned long GetImportProgress (); */
  NS_IMETHOD GetImportProgress(uint32_t* _retval);

  NS_IMETHOD TranslateFolderName(const nsAString& aFolderName,
                                 nsAString& _retval);

 public:
  static void ReportSuccess(nsString& name, int32_t count, nsString* pStream);
  static void ReportError(int32_t errorNum, nsString& name, nsString* pStream);
  static void AddLinebreak(nsString* pStream);
  static void SetLogs(nsString& success, nsString& error, char16_t** pError,
                      char16_t** pSuccess);

 private:
  virtual ~ImportWMMailImpl();
};

nsWMImport::nsWMImport() {
  IMPORT_LOG0("nsWMImport Module Created\n");
  nsWMStringBundle::GetStringBundle();
}

nsWMImport::~nsWMImport() { IMPORT_LOG0("nsWMImport Module Deleted\n"); }

NS_IMPL_ISUPPORTS(nsWMImport, nsIImportModule)

NS_IMETHODIMP nsWMImport::GetName(char16_t** name) {
  NS_ENSURE_ARG_POINTER(name);
  // nsString  title = "Windows Live Mail";
  // *name = ToNewUnicode(title);
  *name = nsWMStringBundle::GetStringByID(WMIMPORT_NAME);

  return NS_OK;
}

NS_IMETHODIMP nsWMImport::GetDescription(char16_t** name) {
  NS_ENSURE_ARG_POINTER(name);

  // nsString  desc = "Windows Live Mail mail and address books";
  // *name = ToNewUnicode(desc);
  *name = nsWMStringBundle::GetStringByID(WMIMPORT_DESCRIPTION);
  return NS_OK;
}

NS_IMETHODIMP nsWMImport::GetSupports(char** supports) {
  NS_ASSERTION(supports != nullptr, "null ptr");
  if (!supports) return NS_ERROR_NULL_POINTER;

  *supports = strdup(kWMSupportsString);
  return NS_OK;
}

NS_IMETHODIMP nsWMImport::GetSupportsUpgrade(bool* pUpgrade) {
  NS_ASSERTION(pUpgrade != nullptr, "null ptr");
  if (!pUpgrade) return NS_ERROR_NULL_POINTER;

  *pUpgrade = true;
  return NS_OK;
}

NS_IMETHODIMP nsWMImport::GetImportInterface(const char* pImportType,
                                             nsISupports** ppInterface) {
  NS_ENSURE_ARG_POINTER(pImportType);
  NS_ENSURE_ARG_POINTER(ppInterface);

  *ppInterface = nullptr;
  nsresult rv;

  if (!strcmp(pImportType, "settings")) {
    nsCOMPtr<nsIImportSettings> pSettings;
    rv = nsWMSettings::Create(getter_AddRefs(pSettings));
    if (NS_SUCCEEDED(rv)) {
      nsCOMPtr<nsISupports> pInterface(do_QueryInterface(pSettings));
      pInterface.forget(ppInterface);
    }
    return rv;
  }

  return NS_ERROR_NOT_AVAILABLE;
}

/////////////////////////////////////////////////////////////////////////////////
nsresult ImportWMMailImpl::Create(nsIImportMail** aImport) {
  NS_ENSURE_ARG_POINTER(aImport);
  NS_ADDREF(*aImport = new ImportWMMailImpl());
  return NS_OK;
}

ImportWMMailImpl::ImportWMMailImpl() {}

ImportWMMailImpl::~ImportWMMailImpl() {}

NS_IMPL_ISUPPORTS(ImportWMMailImpl, nsIImportMail)

NS_IMETHODIMP ImportWMMailImpl::TranslateFolderName(
    const nsAString& aFolderName, nsAString& _retval) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP ImportWMMailImpl::GetDefaultLocation(nsIFile** ppLoc, bool* found,
                                                   bool* userVerify) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP ImportWMMailImpl::FindMailboxes(
    nsIFile* pLoc, nsTArray<RefPtr<nsIImportMailboxDescriptor>>& boxes) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

void ImportWMMailImpl::AddLinebreak(nsString* pStream) {
  if (pStream) pStream->Append(char16_t('\n'));
}

void ImportWMMailImpl::ReportSuccess(nsString& name, int32_t count,
                                     nsString* pStream) {
  if (!pStream) return;
  // load the success string
  char16_t* pFmt = nsWMStringBundle::GetStringByID(WMIMPORT_MAILBOX_SUCCESS);
  nsString pText;
  nsTextFormatter::ssprintf(pText, pFmt, name.get(), count);
  pStream->Append(pText);
  nsWMStringBundle::FreeString(pFmt);
  AddLinebreak(pStream);
}

void ImportWMMailImpl::ReportError(int32_t errorNum, nsString& name,
                                   nsString* pStream) {
  if (!pStream) return;
  // load the error string
  char16_t* pFmt = nsWMStringBundle::GetStringByID(errorNum);
  nsString pText;
  nsTextFormatter::ssprintf(pText, pFmt, name.get());
  pStream->Append(pText);
  nsWMStringBundle::FreeString(pFmt);
  AddLinebreak(pStream);
}

void ImportWMMailImpl::SetLogs(nsString& success, nsString& error,
                               char16_t** pError, char16_t** pSuccess) {
  if (pError) *pError = ToNewUnicode(error);
  if (pSuccess) *pSuccess = ToNewUnicode(success);
}

NS_IMETHODIMP ImportWMMailImpl::ImportMailbox(
    nsIImportMailboxDescriptor* pSource, nsIMsgFolder* pDstFolder,
    char16_t** pErrorLog, char16_t** pSuccessLog, bool* fatalError) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP ImportWMMailImpl::GetImportProgress(uint32_t* pDoneSoFar) {
  return NS_ERROR_NOT_IMPLEMENTED;
}