summaryrefslogtreecommitdiffstats
path: root/shell/source/win32/spsupp/spsuppHelper.cxx
blob: af3509b87c8b9697076c5e50f287ae347f622339 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* 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 <prewin.h>
#include <postwin.h>

#include <comphelper/windowserrorstring.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <o3tl/char16_t2wchar_t.hxx>
#include <osl/file.hxx>
#include <rtl/bootstrap.hxx>
#include <spsuppStrings.hrc>
#include <unotools/resmgr.hxx>
#include "res/spsuppDlg.h"

// Since we need to show localized messages to user before starting LibreOffice, we need to
// bootstrap part of LO (l10n machinery). This implies loading some LO libraries, and since
// there are ActiveX controls for both x86 and x64 for use in corresponding clients, they
// can't both load the libraries that exist only for one architecture, like sal. Thus we need
// a dedicated helper process, which is launched by ActiveX, and handle user interactions.

namespace
{
const OUString& GetSofficeExe()
{
    static const OUString s_sPath = []() {
        OUString result;
        wchar_t sPath[MAX_PATH];
        if (GetModuleFileNameW(nullptr, sPath, MAX_PATH) == 0)
            return result;
        wchar_t* pSlashPos = wcsrchr(sPath, L'\\');
        if (pSlashPos == nullptr)
            return result;
        wcscpy(pSlashPos + 1, L"soffice.exe");
        result = o3tl::toU(sPath);
        return result;
    }();
    return s_sPath;
}

OUString GetString(TranslateId pResId)
{
    static const std::locale s_pLocale = [] {
        // Initialize soffice bootstrap: see getIniFileName_Impl for reference
        OUString sPath = GetSofficeExe();
        if (const sal_Int32 nDotPos = sPath.lastIndexOf('.'); nDotPos >= 0)
        {
            sPath = sPath.replaceAt(nDotPos, sPath.getLength() - nDotPos, SAL_CONFIGFILE(u""));
            if (osl::FileBase::getFileURLFromSystemPath(sPath, sPath) == osl::FileBase::E_None)
                rtl::Bootstrap::setIniFilename(sPath);
        }
        return Translate::Create("shell", LanguageTag("")); // Use system language
    }();
    return Translate::get(pResId, s_pLocale);
}

INT_PTR CALLBACK EditOrRODlgproc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
    switch (Msg)
    {
        case WM_INITDIALOG:
        {
            if (const wchar_t* sFilePath = reinterpret_cast<const wchar_t*>(lParam))
            {
                OUString sMsg = GetString(RID_STR_SP_VIEW_OR_EDIT_TITLE);
                SetWindowTextW(hDlg, o3tl::toW(sMsg.getStr()));
                sMsg = GetString(RID_STR_SP_VIEW_OR_EDIT_MESSAGE)
                           .replaceFirst("%DOCNAME", o3tl::toU(sFilePath));
                SetWindowTextW(GetDlgItem(hDlg, IDC_EDIT_OR_RO), o3tl::toW(sMsg.getStr()));
                sMsg = GetString(RID_STR_SP_VIEW_OR_EDIT_VIEW);
                SetWindowTextW(GetDlgItem(hDlg, ID_RO), o3tl::toW(sMsg.getStr()));
                sMsg = GetString(RID_STR_SP_VIEW_OR_EDIT_EDIT);
                SetWindowTextW(GetDlgItem(hDlg, ID_EDIT), o3tl::toW(sMsg.getStr()));
                sMsg = GetString(RID_STR_SP_VIEW_OR_EDIT_CANCEL);
                SetWindowTextW(GetDlgItem(hDlg, IDCANCEL), o3tl::toW(sMsg.getStr()));
            }
            return TRUE; // set default focus
        }
        case WM_COMMAND:
        {
            WORD nId = LOWORD(wParam);
            switch (nId)
            {
                case IDCANCEL:
                case ID_RO:
                case ID_EDIT:
                    EndDialog(hDlg, nId);
                    return TRUE;
            }
            break;
        }
    }
    return FALSE;
}

enum class Answer
{
    Cancel,
    ReadOnly,
    Edit
};

Answer AskIfUserWantsToEdit(const wchar_t* sFilePath)
{
    Answer res = Answer::Cancel;
    INT_PTR nResult = DialogBoxParamW(nullptr, MAKEINTRESOURCEW(IDD_EDIT_OR_RO), nullptr,
                                      EditOrRODlgproc, reinterpret_cast<LPARAM>(sFilePath));
    if (nResult == ID_RO)
        res = Answer::ReadOnly;
    else if (nResult == ID_EDIT)
        res = Answer::Edit;
    return res;
}

// Returns ERROR_SUCCESS or Win32 error code
DWORD LOStart(const wchar_t* sModeArg, const wchar_t* sFilePath)
{
    OUString sCmdLine = "\"" + GetSofficeExe() + "\" " + o3tl::toU(sModeArg) + " \""
                        + o3tl::toU(sFilePath) + "\"";
    LPWSTR pCmdLine = const_cast<LPWSTR>(o3tl::toW(sCmdLine.getStr()));

    STARTUPINFOW si = {};
    si.cb = sizeof si;
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOW;
    PROCESS_INFORMATION pi{};
    if (!CreateProcessW(nullptr, pCmdLine, nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi))
    {
        DWORD dwError = GetLastError();
        const OUString sErrorMsg = "Could not start LibreOffice. Error is 0x"
                                   + OUString::number(dwError, 16) + ":\n\n"
                                   + WindowsErrorString(dwError);

        // Report the error to user and return error
        MessageBoxW(nullptr, o3tl::toW(sErrorMsg.getStr()), nullptr, MB_ICONERROR);
        return dwError;
    }
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    return ERROR_SUCCESS;
}

int CreateNewDocument(LPCWSTR TemplateLocation, LPCWSTR /*DefaultSaveLocation*/)
{
    // TODO: resolve the program from varProgID (nullptr -> default?)
    DWORD nResult = LOStart(L"-n", TemplateLocation);
    return nResult == ERROR_SUCCESS ? 0 : 2;
}

// UseLocalCopy would be either "0" or "1", denoting boolean value
int EditDocument(LPCWSTR DocumentLocation, LPCWSTR /*UseLocalCopy*/, LPCWSTR /*varProgID*/)
{
    // TODO: resolve the program from varProgID (nullptr -> default?)
    DWORD nResult = LOStart(L"-o", DocumentLocation);
    return nResult == ERROR_SUCCESS ? 0 : 2;
}

// Possible values for OpenType
//
// "0" When checked out, or when the document library does not require check out, the user can read or edit the document
// "1" When another user has checked it out, the user can only read the document
// "2" When the current user has checked it out, the user can only edit the document
// "3" When the document is not checked out and the document library requires that documents be checked out to be edited, the user can only read the document, or check it out and edit it
// "4" When the current user has checked it out, the user can only edit the local copy of the document
int ViewDocument(LPCWSTR DocumentLocation, LPCWSTR OpenType, LPCWSTR varProgID)
{
    if (wcscmp(OpenType, L"0") == 0)
    {
        switch (AskIfUserWantsToEdit(DocumentLocation))
        {
            case Answer::Cancel:
                return 1;
            case Answer::ReadOnly:
                break;
            case Answer::Edit:
                return EditDocument(DocumentLocation, L"0", varProgID);
        }
    }
    // TODO: resolve the program from varProgID (nullptr -> default?)
    DWORD nResult = LOStart(L"--view", DocumentLocation);
    return nResult == ERROR_SUCCESS ? 0 : 2;
}
} // namespace

// Returns 0 on success, 1 when operation wasn't performed because user cancelled, 2 on an error
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
{
    int argc = 0;
    const LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
    if (argc < 2)
        return 2; // Wrong argument count

    if (wcscmp(argv[1], L"CreateNewDocument") == 0)
    {
        if (argc != 4)
            return 2; // Wrong argument count
        return CreateNewDocument(argv[2], argv[3]);
    }

    if (wcscmp(argv[1], L"ViewDocument") == 0)
    {
        if (argc != 4 && argc != 5)
            return 2; // Wrong argument count
        LPCWSTR pProgId = argc == 5 ? argv[4] : nullptr;
        return ViewDocument(argv[2], argv[3], pProgId);
    }

    if (wcscmp(argv[1], L"EditDocument") == 0)
    {
        if (argc != 4 && argc != 5)
            return 2; // Wrong argument count
        LPCWSTR pProgId = argc == 5 ? argv[4] : nullptr;
        return EditDocument(argv[2], argv[3], pProgId);
    }

    return 2; // Wrong command
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */