1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include "gtk3_kde5_filepicker_ipc.hxx"
28 #include <system_error>
30 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
32 #include <vcl/svapp.hxx>
33 #include <vcl/sysdata.hxx>
34 #include <vcl/weld.hxx>
37 #include <osl/process.h>
41 #include <boost/filesystem/path.hpp>
43 #include <fpicker/fpsofficeResMgr.hxx>
45 using namespace ::com::sun::star::ui::dialogs
;
51 OUString
applicationDirPath()
53 OUString applicationFilePath
;
54 osl_getExecutableFile(&applicationFilePath
.pData
);
55 OUString applicationSystemPath
;
56 osl_getSystemPathFromFileURL(applicationFilePath
.pData
, &applicationSystemPath
.pData
);
57 const auto utf8Path
= applicationSystemPath
.toUtf8();
58 auto ret
= boost::filesystem::path(utf8Path
.getStr(), utf8Path
.getStr() + utf8Path
.getLength());
59 ret
.remove_filename();
60 return OUString::fromUtf8(std::string_view(ret
.c_str()));
63 OUString
findPickerExecutable()
65 const auto path
= applicationDirPath();
67 osl_searchFileURL(u
"lo_kde5filepicker"_ustr
.pData
, path
.pData
, &ret
.pData
);
69 throw std::system_error(std::make_error_code(std::errc::no_such_file_or_directory
),
70 "could not find lo_kde5filepicker executable");
75 void readIpcArg(std::istream
& stream
, OUString
& str
)
77 const auto buffer
= readIpcStringArg(stream
);
78 str
= OUString::fromUtf8(std::string_view(buffer
.data(), buffer
.size()));
81 void readIpcArg(std::istream
& stream
, css::uno::Sequence
<OUString
>& seq
)
83 uint32_t numFiles
= 0;
85 stream
.ignore(); // skip space;
86 seq
.realloc(numFiles
);
87 OUString
* pSeq
= seq
.getArray();
88 for (size_t i
= 0; i
< numFiles
; ++i
)
90 readIpcArg(stream
, pSeq
[i
]);
94 void sendIpcArg(std::ostream
& stream
, const OUString
& string
)
96 const auto utf8
= string
.toUtf8();
97 sendIpcStringArg(stream
, utf8
.getLength(), utf8
.getStr());
100 OUString
getResString(TranslateId pResId
)
105 return FpsResId(pResId
);
108 // handles the IPC commands for dialog execution and ends the dummy Gtk dialog once the IPC response is there
109 static void handleIpcForExecute(Gtk3KDE5FilePickerIpc
* pFilePickerIpc
, GtkWidget
* pDummyDialog
,
112 auto id
= pFilePickerIpc
->sendCommand(Commands::Execute
);
113 pFilePickerIpc
->readResponse(id
, *bResult
);
115 // end the dummy dialog
116 gtk_widget_hide(pDummyDialog
);
119 // Gtk3KDE5FilePicker
121 Gtk3KDE5FilePickerIpc::Gtk3KDE5FilePickerIpc()
123 const auto exe
= findPickerExecutable();
124 oslProcessError result
;
125 oslSecurity pSecurity
= osl_getCurrentSecurity();
126 result
= osl_executeProcess_WithRedirectedIO(exe
.pData
, nullptr, 0, osl_Process_NORMAL
,
127 pSecurity
, nullptr, nullptr, 0, &m_process
,
128 &m_inputWrite
, &m_outputRead
, nullptr);
129 osl_freeSecurityHandle(pSecurity
);
130 if (result
!= osl_Process_E_None
)
131 throw std::system_error(std::make_error_code(std::errc::no_such_process
),
132 "could not start lo_kde5filepicker executable");
135 Gtk3KDE5FilePickerIpc::~Gtk3KDE5FilePickerIpc()
140 sendCommand(Commands::Quit
);
141 osl_joinProcess(m_process
);
144 osl_closeFile(m_inputWrite
);
146 osl_closeFile(m_outputRead
);
147 osl_freeProcessHandle(m_process
);
150 sal_Int16
Gtk3KDE5FilePickerIpc::execute()
152 auto restoreMainWindow
= blockMainWindow();
154 // dummy gtk dialog that will take care of processing events,
155 // not meant to be actually seen by user
156 GtkWidget
* pDummyDialog
= gtk_dialog_new();
158 bool accepted
= false;
160 // send IPC command and read response in a separate thread
161 std::thread
aIpcHandler(&handleIpcForExecute
, this, pDummyDialog
, &accepted
);
163 // make dummy dialog not to be seen by user
164 gtk_window_set_decorated(GTK_WINDOW(pDummyDialog
), false);
165 gtk_window_set_default_size(GTK_WINDOW(pDummyDialog
), 0, 0);
166 gtk_window_set_accept_focus(GTK_WINDOW(pDummyDialog
), false);
167 // gtk_widget_set_opacity() only has the desired effect when widget is already shown
168 gtk_widget_show(pDummyDialog
);
169 gtk_widget_set_opacity(pDummyDialog
, 0);
170 // run dialog, leaving event processing to GTK
171 // dialog will be closed by the separate 'aIpcHandler' thread once the IPC response is there
172 gtk_dialog_run(GTK_DIALOG(pDummyDialog
));
176 gtk_widget_destroy(pDummyDialog
);
178 if (restoreMainWindow
)
181 return accepted
? ExecutableDialogResults::OK
: ExecutableDialogResults::CANCEL
;
184 static gboolean
ignoreDeleteEvent(GtkWidget
* /*widget*/, GdkEvent
* /*event*/,
185 gpointer
/*user_data*/)
190 std::function
<void()> Gtk3KDE5FilePickerIpc::blockMainWindow()
192 weld::Window
* pParentWin
= Application::GetDefDialogParent();
196 const SystemEnvData aSysData
= pParentWin
->get_system_data();
197 auto* pMainWindow
= static_cast<GtkWidget
*>(aSysData
.pWidget
);
201 sendCommand(Commands::SetWinId
, aSysData
.GetWindowHandle(aSysData
.pSalFrame
));
203 SolarMutexGuard guard
;
204 auto deleteEventSignalId
= g_signal_lookup("delete_event", gtk_widget_get_type());
206 // disable the mainwindow
207 gtk_widget_set_sensitive(pMainWindow
, false);
209 // block the GtkSalFrame delete_event handler
210 auto blockedHandler
= g_signal_handler_find(
211 pMainWindow
, static_cast<GSignalMatchType
>(G_SIGNAL_MATCH_ID
| G_SIGNAL_MATCH_DATA
),
212 deleteEventSignalId
, 0, nullptr, nullptr, aSysData
.pSalFrame
);
213 g_signal_handler_block(pMainWindow
, blockedHandler
);
215 // prevent the window from being closed
216 auto ignoreDeleteEventHandler
217 = g_signal_connect(pMainWindow
, "delete_event", G_CALLBACK(ignoreDeleteEvent
), nullptr);
219 return [pMainWindow
, ignoreDeleteEventHandler
, blockedHandler
] {
220 SolarMutexGuard cleanupGuard
;
222 gtk_widget_set_sensitive(pMainWindow
, true);
224 // allow it to be closed again
225 g_signal_handler_disconnect(pMainWindow
, ignoreDeleteEventHandler
);
227 // unblock the GtkSalFrame handler
228 g_signal_handler_unblock(pMainWindow
, blockedHandler
);
232 void Gtk3KDE5FilePickerIpc::writeResponseLine(const std::string
& line
)
234 sal_uInt64 bytesWritten
= 0;
235 osl_writeFile(m_inputWrite
, line
.c_str(), line
.size(), &bytesWritten
);
238 std::string
Gtk3KDE5FilePickerIpc::readResponseLine()
240 if (!m_responseBuffer
.empty()) // check whether we have a line in our buffer
242 std::size_t it
= m_responseBuffer
.find('\n');
243 if (it
!= std::string::npos
)
245 auto ret
= m_responseBuffer
.substr(0, it
);
246 m_responseBuffer
.erase(0, it
+ 1);
251 const sal_uInt64 BUF_SIZE
= 1024;
252 char buffer
[BUF_SIZE
];
255 sal_uInt64 bytesRead
= 0;
256 auto err
= osl_readFile(m_outputRead
, buffer
, BUF_SIZE
, &bytesRead
);
257 auto it
= std::find(buffer
, buffer
+ bytesRead
, '\n');
258 if (it
!= buffer
+ bytesRead
) // check whether the chunk we read contains an EOL
260 // if so, append that part to the buffer and return it
261 std::string ret
= m_responseBuffer
.append(buffer
, it
);
262 // but keep anything else we may have read in our buffer
264 m_responseBuffer
.assign(it
, buffer
+ bytesRead
);
267 // otherwise append everything we read to the buffer and try again
268 m_responseBuffer
.append(buffer
, bytesRead
);
270 if (err
!= osl_File_E_None
&& err
!= osl_File_E_AGAIN
)
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */