summaryrefslogtreecommitdiffstats
path: root/mailnews/mapi/mapihook/src/msgMapiSupport.cpp
blob: 5c57f087dbd4fa21044907d59f76f34feee0f5a8 (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
/* 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 "nsCOMPtr.h"
#include "objbase.h"
#include "nsISupports.h"

#include "mozilla/ModuleUtils.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "nsIAppStartupNotifier.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsICategoryManager.h"
#include "Registry.h"
#include "msgMapiSupport.h"

#include "msgMapiImp.h"

/** Implementation of the nsIMapiSupport interface.
 *  Use standard implementation of nsISupports stuff.
 */

NS_IMPL_ISUPPORTS(nsMapiSupport, nsIMapiSupport, nsIObserver)

NS_IMETHODIMP
nsMapiSupport::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData)
{
    nsresult rv = NS_OK ;

    if (!strcmp(aTopic, "profile-after-change"))
        return InitializeMAPISupport();

    if (!strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID))
        return ShutdownMAPISupport();

    nsCOMPtr<nsIObserverService> observerService =
      mozilla::services::GetObserverService();
    NS_ENSURE_TRUE(observerService, NS_ERROR_UNEXPECTED);
 
    rv = observerService->AddObserver(this,"profile-after-change", false);
    if (NS_FAILED(rv)) return rv;

    rv = observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
    if (NS_FAILED(rv))  return rv;

    return rv;
}


nsMapiSupport::nsMapiSupport()
: m_dwRegister(0),
  m_nsMapiFactory(nullptr)
{
}

nsMapiSupport::~nsMapiSupport()
{
}

NS_IMETHODIMP
nsMapiSupport::InitializeMAPISupport()
{
    ::OleInitialize(nullptr) ;

    if (m_nsMapiFactory == nullptr)    // No Registering if already done.  Sanity Check!!
    {
        m_nsMapiFactory = new CMapiFactory();

        if (m_nsMapiFactory != nullptr)
        {
            HRESULT hr = ::CoRegisterClassObject(CLSID_CMapiImp, \
                                                 m_nsMapiFactory, \
                                                 CLSCTX_LOCAL_SERVER, \
                                                 REGCLS_MULTIPLEUSE, \
                                                 &m_dwRegister);

            if (FAILED(hr))
            {
                m_nsMapiFactory->Release() ;
                m_nsMapiFactory = nullptr;
                return NS_ERROR_FAILURE;
            }
        }
    }

    return NS_OK;
}

NS_IMETHODIMP
nsMapiSupport::ShutdownMAPISupport()
{
    if (m_dwRegister != 0)
        ::CoRevokeClassObject(m_dwRegister);

    if (m_nsMapiFactory != nullptr)
    {
        m_nsMapiFactory->Release();
        m_nsMapiFactory = nullptr;
    }

    ::OleUninitialize();

    return NS_OK ;
}

NS_IMETHODIMP
nsMapiSupport::RegisterServer()
{
  // TODO: Figure out what kind of error propogation to pass back
  ::RegisterServer(CLSID_CMapiImp, "Mozilla MAPI", "MozillaMapi", "MozillaMapi.1");
  return NS_OK;
}

NS_IMETHODIMP
nsMapiSupport::UnRegisterServer()
{
  // TODO: Figure out what kind of error propogation to pass back
  ::UnregisterServer(CLSID_CMapiImp, "MozillaMapi", "MozillaMapi.1");
  return NS_OK;
}

NS_DEFINE_NAMED_CID(NS_IMAPISUPPORT_CID);

NS_GENERIC_FACTORY_CONSTRUCTOR(nsMapiSupport)

static const mozilla::Module::CategoryEntry kMAPICategories[] = {
  { APPSTARTUP_CATEGORY, "Mapi Support", "service," NS_IMAPISUPPORT_CONTRACTID, },
  { NULL }
};

const mozilla::Module::CIDEntry kMAPICIDs[] = {
  { &kNS_IMAPISUPPORT_CID, false, NULL, nsMapiSupportConstructor },
  { NULL }
};

const mozilla::Module::ContractIDEntry kMAPIContracts[] = {
  { NS_IMAPISUPPORT_CONTRACTID, &kNS_IMAPISUPPORT_CID },
  { NULL }
};

static const mozilla::Module kMAPIModule = {
    mozilla::Module::kVersion,
    kMAPICIDs,
    kMAPIContracts,
    kMAPICategories
};

NSMODULE_DEFN(msgMapiModule) = &kMAPIModule;