summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/migration/src/nsProfileMigrator.cpp
blob: c6fa2bc867621ac00b0725a577fcc7eed4346aec (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
/* -*- 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 "nsIFile.h"
#include "mozIDOMWindow.h"
#include "nsIProfileMigrator.h"
#include "nsIPrefService.h"
#include "nsIServiceManager.h"
#include "nsIToolkitProfile.h"
#include "nsIToolkitProfileService.h"
#include "nsIWindowWatcher.h"
#include "nsISupportsPrimitives.h"
#include "nsIMutableArray.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsIProperties.h"
#include "nsDirectoryServiceDefs.h"
#include "nsProfileMigrator.h"

#ifdef XP_WIN
#  include <windows.h>
#else
#  include <limits.h>
#endif

NS_IMPL_ISUPPORTS(nsProfileMigrator, nsIProfileMigrator)

#define MIGRATION_WIZARD_FE_URL \
  "chrome://messenger/content/migration/migration.xhtml"_ns
#define MIGRATION_WIZARD_FE_FEATURES "chrome,dialog,modal,centerscreen"_ns

NS_IMETHODIMP
nsProfileMigrator::Migrate(nsIProfileStartup* aStartup, const nsACString& aKey,
                           const nsACString& aProfileName) {
  nsAutoCString key;
  nsCOMPtr<nsIMailProfileMigrator> mailMigrator;
  nsresult rv = GetDefaultMailMigratorKey(key, mailMigrator);
  NS_ENSURE_SUCCESS(rv, rv);  // abort migration if we failed to get a
                              // mailMigrator (if we were supposed to)

  nsCOMPtr<nsISupportsCString> cstr(
      do_CreateInstance("@mozilla.org/supports-cstring;1"));
  NS_ENSURE_TRUE(cstr, NS_ERROR_OUT_OF_MEMORY);
  cstr->SetData(key);

  // By opening the Migration FE with a supplied mailMigrator, it will
  // automatically migrate from it.
  nsCOMPtr<nsIWindowWatcher> ww(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
  nsCOMPtr<nsIMutableArray> params(do_CreateInstance(NS_ARRAY_CONTRACTID));
  if (!ww || !params) return NS_ERROR_FAILURE;

  params->AppendElement(cstr);
  params->AppendElement(mailMigrator);
  params->AppendElement(aStartup);

  nsCOMPtr<mozIDOMWindowProxy> migrateWizard;
  return ww->OpenWindow(nullptr, MIGRATION_WIZARD_FE_URL, "_blank"_ns,
                        MIGRATION_WIZARD_FE_FEATURES, params,
                        getter_AddRefs(migrateWizard));
}

#ifdef XP_WIN
typedef struct {
  WORD wLanguage;
  WORD wCodePage;
} LANGANDCODEPAGE;

#  define INTERNAL_NAME_THUNDERBIRD "Thunderbird"
#  define INTERNAL_NAME_SEAMONKEY "Mozilla"
#endif

nsresult nsProfileMigrator::GetDefaultMailMigratorKey(
    nsACString& aKey, nsCOMPtr<nsIMailProfileMigrator>& mailMigrator) {
  // look up the value of profile.force.migration in case we are supposed to
  // force migration using a particular migrator....
  nsresult rv = NS_OK;
  nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCString forceMigrationType;
  prefs->GetCharPref("profile.force.migration", forceMigrationType);

  // if we are being forced to migrate to a particular migration type, then
  // create an instance of that migrator and return it.
  nsAutoCString migratorID;
  if (!forceMigrationType.IsEmpty()) {
    bool exists = false;
    migratorID.AppendLiteral("@mozilla.org/messenger/server;1?type=");
    migratorID.Append(forceMigrationType);
    mailMigrator = do_CreateInstance(migratorID.get());
    if (!mailMigrator) return NS_ERROR_NOT_AVAILABLE;

    mailMigrator->GetSourceExists(&exists);
    /* trying to force migration on a source which doesn't
     * have any profiles.
     */
    if (!exists) return NS_ERROR_NOT_AVAILABLE;
    aKey = forceMigrationType;
    return NS_OK;
  }

#define MAX_SOURCE_LENGTH 10
  const char sources[][MAX_SOURCE_LENGTH] = {"seamonkey", "outlook", ""};
  for (uint32_t i = 0; sources[i][0]; ++i) {
    migratorID.AssignLiteral("@mozilla.org/messenger/server;1?type=");
    migratorID.Append(sources[i]);
    mailMigrator = do_CreateInstance(migratorID.get());
    if (!mailMigrator) continue;

    bool exists = false;
    mailMigrator->GetSourceExists(&exists);
    if (exists) {
      mailMigrator = nullptr;
      return NS_OK;
    }
  }

  return NS_ERROR_NOT_AVAILABLE;
}