summaryrefslogtreecommitdiff
path: root/setup_native/source/win32/customactions/reg4msdoc/register.cxx
blob: 77d091806acd8e3b87b5c79a5b4664d5015d895a (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include "register.hxx"
#include "registryexception.hxx"
#include "registrationcontextinformation.hxx"
#include "userregistrar.hxx"
#include "windowsregistry.hxx"
#include "stringconverter.hxx"
#include "msihelper.hxx"

#ifdef _MSC_VER
#pragma warning(push, 1) /* disable warnings within system headers */
#pragma warning(disable: 4917)
#endif
#include <shlobj.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif


#include <assert.h>
#ifdef _MSC_VER
#pragma warning(disable: 4350)
#endif

typedef std::auto_ptr<Registrar> RegistrarPtr;

namespace /* private */
{
    RegistrarPtr CreateRegistrar(bool InstallForAllUser, const RegistrationContextInformation& RegCtx)
    {
        RegistrarPtr RegPtr;

        if (InstallForAllUser)
            RegPtr = RegistrarPtr(new Registrar(RegCtx));
        else
            RegPtr = RegistrarPtr(new UserRegistrar(RegCtx));

        return RegPtr;
    }
} // namespace private

bool query_preselect_registration_for_ms_application(MSIHANDLE handle, int Register)
{
    bool preselect = false;

    try
    {
        RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
        RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);

        if (Register & MSWORD)
            preselect = CurrentRegistrar->QueryPreselectMsWordRegistration();
        else if (Register & MSEXCEL)
            preselect = CurrentRegistrar->QueryPreselectMsExcelRegistration();
        else if (Register & MSPOWERPOINT)
            preselect = CurrentRegistrar->QueryPreselectMsPowerPointRegistration();
    }
    catch(RegistryException&)
    {
        assert(false);
    }
    return preselect;
}

//-----------------------------------------
// registers StarOffice for MS document
// types and as default HTML editor if
// specified
//-----------------------------------------

void Register4MsDoc(MSIHANDLE handle, int Register)
{
    try
    {
        RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
        RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);

        if ((Register & MSWORD))
            CurrentRegistrar->RegisterForMsWord();

        if ((Register & MSEXCEL))
            CurrentRegistrar->RegisterForMsExcel();

        if ((Register & MSPOWERPOINT))
            CurrentRegistrar->RegisterForMsPowerPoint();

        if ((Register & HTML_EDITOR))
            CurrentRegistrar->RegisterAsHtmlEditorForInternetExplorer();

        if ((Register & DEFAULT_SHELL_HTML_EDITOR))
        {
            CurrentRegistrar->RegisterAsDefaultHtmlEditorForInternetExplorer();
            CurrentRegistrar->RegisterAsDefaultShellHtmlEditor();
        }
    }
    catch(RegistryException&)
    {
        assert(false);
    }

    if (Register)
        SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
}

void Unregister4MsDoc(MSIHANDLE handle, int Unregister)
{
    try
    {
        RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
        RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);

        if ((Unregister & MSWORD) && CurrentRegistrar->IsRegisteredFor(MSWORD))
            CurrentRegistrar->UnregisterForMsWord();

        if ((Unregister & HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(HTML_EDITOR))
            CurrentRegistrar->UnregisterAsHtmlEditorForInternetExplorer();

        if ((Unregister & MSEXCEL) && CurrentRegistrar->IsRegisteredFor(MSEXCEL))
            CurrentRegistrar->UnregisterForMsExcel();

        if ((Unregister & MSPOWERPOINT) && CurrentRegistrar->IsRegisteredFor(MSPOWERPOINT))
            CurrentRegistrar->UnregisterForMsPowerPoint();

        if ((Unregister & DEFAULT_HTML_EDITOR_FOR_IE) && CurrentRegistrar->IsRegisteredFor(DEFAULT_HTML_EDITOR_FOR_IE))
            CurrentRegistrar->UnregisterAsDefaultHtmlEditorForInternetExplorer();

        if ((Unregister & DEFAULT_SHELL_HTML_EDITOR) && CurrentRegistrar->IsRegisteredFor(DEFAULT_SHELL_HTML_EDITOR))
            CurrentRegistrar->UnregisterAsDefaultShellHtmlEditor();
    }
    catch(RegistryException&)
    {
        assert(false);
    }

    if (Unregister)
        SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
}

//-----------------------------------------
// restores the entries for the selected
// registry entries
// Algorithm:
//
// 1.
// Target key exist (e.g. '.doc')
// Default value == soffice.?
// Backup key != empty
// Action: Replace Default value with backup
// key
//
// 2.
// Target key exist
// Default value == soffice.?
// Backup key == empty
// Action: delete default value
//
// 3.
// Target key exist
// Default value != soffice.?
// Action: nop
//
// 4.
// Target key does not exist
// Action: nop
//-----------------------------------------

void Unregister4MsDocAll(MSIHANDLE handle)
{
    try
    {
        RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
        RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);

        CurrentRegistrar->UnregisterAllAndCleanUpRegistry();
    }
    catch(RegistryException&)
    {
        assert(false);
    }
}

//-----------------------------------------
// restores lost settings formerly made
// with Register4MsDoc
//-----------------------------------------

void RepairRegister4MsDocSettings(MSIHANDLE handle)
{
    try
    {
        RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
        RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);

        CurrentRegistrar->RepairRegistrationState();
    }
    catch(RegistryException&)
    {
        assert(false);
    }
}

bool IsRegisteredFor(MSIHANDLE handle, int State)
{
    bool Registered = false;

    try
    {
        RegistrationContextInformation RegContext(handle, GetOfficeExecutablePath(handle));
        RegistrarPtr CurrentRegistrar = CreateRegistrar(IsAllUserInstallation(handle), RegContext);

        Registered = CurrentRegistrar->IsRegisteredFor(State);
    }
    catch(RegistryException&)
    {
        assert(false);
    }
    return Registered;
}

#define SO60_UNINSTALL_KEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\StarOffice 6.0"
#define SO_BACKUP_KEY      L"soffice6.bak"
#define REGMSDOCSTATE      L"Reg4MsDocState"
#define SOFTWARE_CLASSES   L"Software\\Classes"

int FixReturnRegistrationState(MSIHANDLE handle)
{
    int registration_state = 0;

    try
    {
        WindowsRegistry registry;

        RegistryValue rv_regmsdocstate = RegistryValue(
            new RegistryValueImpl(REGMSDOCSTATE, 0));

        RegistryKey so_bak_key;

        if (IsAllUserInstallation(handle))
        {
            RegistryKey hkcr_key = registry.GetClassesRootKey();

            if (hkcr_key->HasSubKey(SO_BACKUP_KEY))
                so_bak_key = hkcr_key->OpenSubKey(SO_BACKUP_KEY);
            else
                so_bak_key = hkcr_key->CreateSubKey(SO_BACKUP_KEY);

            if (!so_bak_key->HasValue(REGMSDOCSTATE))
            {
                // set a defined value
                so_bak_key->SetValue(rv_regmsdocstate);

                RegistryKey hklm_key = registry.GetLocalMachineKey();

                if (hklm_key->HasSubKey(SO60_UNINSTALL_KEY))
                {
                    RegistryKey so_uninst_key =
                        hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);

                    if (so_uninst_key->HasValue(REGMSDOCSTATE))
                        so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
                }
            }
        }
        else
        {
            RegistryKey hkcu_classes_key =
                registry.GetCurrentUserKey()->OpenSubKey(SOFTWARE_CLASSES);

            so_bak_key = hkcu_classes_key->CreateSubKey(SO_BACKUP_KEY);

            if (!so_bak_key->HasValue(REGMSDOCSTATE))
            {
                // set a defined value
                so_bak_key->SetValue(rv_regmsdocstate);

                RegistryKey hklm_sftw_classes =
                    registry.GetLocalMachineKey()->OpenSubKey(SOFTWARE_CLASSES, false);

                RegistryKey so_bak_key_old;

                if (hklm_sftw_classes->HasSubKey(SO_BACKUP_KEY))
                {
                    so_bak_key_old = hklm_sftw_classes->OpenSubKey(SO_BACKUP_KEY, false);

                    if (so_bak_key_old->HasValue(REGMSDOCSTATE))
                        so_bak_key->CopyValue(so_bak_key_old, REGMSDOCSTATE);
                }
                else // try the uninstall key
                {
                    RegistryKey hklm_key = registry.GetLocalMachineKey();

                    if (hklm_key->HasSubKey(SO60_UNINSTALL_KEY))
                    {
                        RegistryKey so_uninst_key =
                            hklm_key->OpenSubKey(SO60_UNINSTALL_KEY);

                        if (so_uninst_key->HasValue(REGMSDOCSTATE))
                            so_bak_key->CopyValue(so_uninst_key, REGMSDOCSTATE);
                    }
                }
            }
        }

        rv_regmsdocstate = so_bak_key->GetValue(REGMSDOCSTATE);
        registration_state = rv_regmsdocstate->GetDataAsInt();
    }
    catch(RegistryException&)
    {
        registration_state = 0;
    }

    return registration_state;
}