16#include <osl/file.hxx>
17#include <rtl/bootstrap.hxx>
18#include <spsuppStrings.hrc>
30const OUString& GetSofficeExe()
32 static const OUString s_sPath = []() {
35 if (GetModuleFileNameW(
nullptr, sPath, MAX_PATH) == 0)
37 wchar_t* pSlashPos = wcsrchr(sPath, L
'\\');
38 if (pSlashPos ==
nullptr)
40 wcscpy(pSlashPos + 1, L
"soffice.exe");
49 static const std::locale s_pLocale = [] {
51 OUString sPath = GetSofficeExe();
52 if (
const sal_Int32 nDotPos = sPath.lastIndexOf(
'.'); nDotPos >= 0)
54 sPath = sPath.replaceAt(nDotPos, sPath.getLength() - nDotPos,
SAL_CONFIGFILE(u
""));
55 if (osl::FileBase::getFileURLFromSystemPath(sPath, sPath) == osl::FileBase::E_None)
56 rtl::Bootstrap::setIniFilename(sPath);
63INT_PTR
CALLBACK EditOrRODlgproc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
69 if (
const wchar_t* sFilePath =
reinterpret_cast<const wchar_t*
>(lParam))
71 OUString sMsg =
GetString(RID_STR_SP_VIEW_OR_EDIT_TITLE);
72 SetWindowTextW(hDlg, o3tl::toW(sMsg.getStr()));
73 sMsg =
GetString(RID_STR_SP_VIEW_OR_EDIT_MESSAGE)
74 .replaceFirst(
"%DOCNAME", o3tl::toU(sFilePath));
75 SetWindowTextW(GetDlgItem(hDlg,
IDC_EDIT_OR_RO), o3tl::toW(sMsg.getStr()));
76 sMsg =
GetString(RID_STR_SP_VIEW_OR_EDIT_VIEW);
77 SetWindowTextW(GetDlgItem(hDlg,
ID_RO), o3tl::toW(sMsg.getStr()));
78 sMsg =
GetString(RID_STR_SP_VIEW_OR_EDIT_EDIT);
79 SetWindowTextW(GetDlgItem(hDlg,
ID_EDIT), o3tl::toW(sMsg.getStr()));
80 sMsg =
GetString(RID_STR_SP_VIEW_OR_EDIT_CANCEL);
81 SetWindowTextW(GetDlgItem(hDlg, IDCANCEL), o3tl::toW(sMsg.getStr()));
109Answer AskIfUserWantsToEdit(
const wchar_t* sFilePath)
111 Answer res = Answer::Cancel;
112 INT_PTR nResult = DialogBoxParamW(
nullptr, MAKEINTRESOURCEW(
IDD_EDIT_OR_RO),
nullptr,
113 EditOrRODlgproc,
reinterpret_cast<LPARAM
>(sFilePath));
114 if (nResult ==
ID_RO)
115 res = Answer::ReadOnly;
122DWORD LOStart(
const wchar_t* sModeArg,
const wchar_t* sFilePath)
124 OUString sCmdLine =
"\"" + GetSofficeExe() +
"\" " + o3tl::toU(sModeArg) +
" \""
125 + o3tl::toU(sFilePath) +
"\"";
126 LPWSTR pCmdLine =
const_cast<LPWSTR
>(o3tl::toW(sCmdLine.getStr()));
128 STARTUPINFOW si = {};
130 si.dwFlags = STARTF_USESHOWWINDOW;
131 si.wShowWindow = SW_SHOW;
132 PROCESS_INFORMATION pi{};
133 if (!CreateProcessW(
nullptr, pCmdLine,
nullptr,
nullptr, FALSE, 0,
nullptr,
nullptr, &si, &pi))
135 DWORD dwError = GetLastError();
136 const OUString sErrorMsg =
"Could not start LibreOffice. Error is 0x"
137 + OUString::number(dwError, 16) +
":\n\n"
138 + WindowsErrorString(dwError);
141 MessageBoxW(
nullptr, o3tl::toW(sErrorMsg.getStr()),
nullptr, MB_ICONERROR);
144 CloseHandle(pi.hProcess);
145 CloseHandle(pi.hThread);
146 return ERROR_SUCCESS;
149int CreateNewDocument(LPCWSTR TemplateLocation, LPCWSTR )
152 DWORD nResult = LOStart(L
"-n", TemplateLocation);
153 return nResult == ERROR_SUCCESS ? 0 : 2;
157int EditDocument(LPCWSTR DocumentLocation, LPCWSTR , LPCWSTR )
160 DWORD nResult = LOStart(L
"-o", DocumentLocation);
161 return nResult == ERROR_SUCCESS ? 0 : 2;
171int ViewDocument(LPCWSTR DocumentLocation, LPCWSTR OpenType, LPCWSTR varProgID)
173 if (wcscmp(OpenType, L
"0") == 0)
175 switch (AskIfUserWantsToEdit(DocumentLocation))
179 case Answer::ReadOnly:
182 return EditDocument(DocumentLocation, L
"0", varProgID);
186 DWORD nResult = LOStart(L
"--view", DocumentLocation);
187 return nResult == ERROR_SUCCESS ? 0 : 2;
192int WINAPI
wWinMain(HINSTANCE, HINSTANCE, LPWSTR,
int)
195 const LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
199 if (wcscmp(argv[1], L
"CreateNewDocument") == 0)
203 return CreateNewDocument(argv[2], argv[3]);
206 if (wcscmp(argv[1], L
"ViewDocument") == 0)
208 if (argc != 4 && argc != 5)
210 LPCWSTR pProgId = argc == 5 ? argv[4] :
nullptr;
211 return ViewDocument(argv[2], argv[3], pProgId);
214 if (wcscmp(argv[1], L
"EditDocument") == 0)
216 if (argc != 4 && argc != 5)
218 LPCWSTR pProgId = argc == 5 ? argv[4] :
nullptr;
219 return EditDocument(argv[2], argv[3], pProgId);
#define SAL_CONFIGFILE(name)
virtual void EndDialog(sal_Int32 nResult) override
OUString GetString(int nId)
std::locale Create(std::string_view aPrefixName, const LanguageTag &rLocale)
OUString get(TranslateId sContextAndId, const std::locale &loc)
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)