| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <sal/config.h> |
| 21 |
|
| 22 |
#include <string_view> |
| 23 |
|
| 24 |
#include "gtk3_kde5_filepicker_ipc.hxx" |
| 25 |
|
| 26 |
#undef Region |
| 27 |
|
| 28 |
#include <system_error> |
| 29 |
|
| 30 |
#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp> |
| 31 |
|
| 32 |
#include <vcl/svapp.hxx> |
| 33 |
#include <vcl/sysdata.hxx> |
| 34 |
#include <vcl/weld.hxx> |
| 35 |
|
| 36 |
#include <osl/file.h> |
| 37 |
#include <osl/process.h> |
| 38 |
|
| 39 |
#include <gtk/gtk.h> |
| 40 |
|
| 41 |
#include <boost/filesystem/path.hpp> |
| 42 |
|
| 43 |
#include <fpicker/fpsofficeResMgr.hxx> |
| 44 |
|
| 45 |
using namespace ::com::sun::star::ui::dialogs; |
| 46 |
|
| 47 |
// helper functions |
| 48 |
|
| 49 |
namespace |
| 50 |
{ |
| 51 |
OUString applicationDirPath() |
| 52 |
{ |
| 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())); |
| 61 |
} |
| 62 |
|
| 63 |
OUString findPickerExecutable() |
| 64 |
{ |
| 65 |
const auto path = applicationDirPath(); |
| 66 |
OUString ret; |
| 67 |
osl_searchFileURL(u"lo_kde5filepicker"_ustr.pData, path.pData, &ret.pData); |
| 68 |
if (ret.isEmpty()) |
| 69 |
throw std::system_error(std::make_error_code(std::errc::no_such_file_or_directory), |
| 70 |
"could not find lo_kde5filepicker executable"); |
| 71 |
return ret; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void readIpcArg(std::istream& stream, OUString& str) |
| 76 |
{ |
| 77 |
const auto buffer = readIpcStringArg(stream); |
| 78 |
str = OUString::fromUtf8(std::string_view(buffer.data(), buffer.size())); |
| 79 |
} |
| 80 |
|
| 81 |
void readIpcArg(std::istream& stream, css::uno::Sequence<OUString>& seq) |
| 82 |
{ |
| 83 |
uint32_t numFiles = 0; |
| 84 |
stream >> numFiles; |
| 85 |
stream.ignore(); // skip space; |
| 86 |
seq.realloc(numFiles); |
| 87 |
OUString* pSeq = seq.getArray(); |
| 88 |
for (size_t i = 0; i < numFiles; ++i) |
| 89 |
{ |
| 90 |
readIpcArg(stream, pSeq[i]); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void sendIpcArg(std::ostream& stream, const OUString& string) |
| 95 |
{ |
| 96 |
const auto utf8 = string.toUtf8(); |
| 97 |
sendIpcStringArg(stream, utf8.getLength(), utf8.getStr()); |
| 98 |
} |
| 99 |
|
| 100 |
OUString getResString(TranslateId pResId) |
| 101 |
{ |
| 102 |
if (!pResId) |
| 103 |
return {}; |
| 104 |
|
| 105 |
return FpsResId(pResId); |
| 106 |
} |
| 107 |
|
| 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, |
| 110 |
bool* bResult) |
| 111 |
{ |
| 112 |
auto id = pFilePickerIpc->sendCommand(Commands::Execute); |
| 113 |
pFilePickerIpc->readResponse(id, *bResult); |
| 114 |
|
| 115 |
// end the dummy dialog |
| 116 |
gtk_widget_set_visible(pDummyDialog, false); |
| 117 |
} |
| 118 |
|
| 119 |
// Gtk3KDE5FilePicker |
| 120 |
|
| 121 |
Gtk3KDE5FilePickerIpc::Gtk3KDE5FilePickerIpc() |
| 122 |
{ |
| 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"); |
| 133 |
} |
| 134 |
|
| 135 |
Gtk3KDE5FilePickerIpc::~Gtk3KDE5FilePickerIpc() |
| 136 |
{ |
| 137 |
if (!m_process) |
| 138 |
return; |
| 139 |
|
| 140 |
sendCommand(Commands::Quit); |
| 141 |
osl_joinProcess(m_process); |
| 142 |
|
| 143 |
if (m_inputWrite) |
| 144 |
osl_closeFile(m_inputWrite); |
| 145 |
if (m_outputRead) |
| 146 |
osl_closeFile(m_outputRead); |
| 147 |
osl_freeProcessHandle(m_process); |
| 148 |
} |
| 149 |
|
| 150 |
sal_Int16 Gtk3KDE5FilePickerIpc::execute() |
| 151 |
{ |
| 152 |
auto restoreMainWindow = blockMainWindow(); |
| 153 |
|
| 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(); |
| 157 |
|
| 158 |
bool accepted = false; |
| 159 |
|
| 160 |
// send IPC command and read response in a separate thread |
| 161 |
std::thread aIpcHandler(&handleIpcForExecute, this, pDummyDialog, &accepted); |
| 162 |
|
| 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_set_visible(pDummyDialog, true); |
| 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)); |
| 173 |
|
| 174 |
aIpcHandler.join(); |
| 175 |
|
| 176 |
gtk_widget_destroy(pDummyDialog); |
| 177 |
|
| 178 |
if (restoreMainWindow) |
| 179 |
restoreMainWindow(); |
| 180 |
|
| 181 |
return accepted ? ExecutableDialogResults::OK : ExecutableDialogResults::CANCEL; |
| 182 |
} |
| 183 |
|
| 184 |
static gboolean ignoreDeleteEvent(GtkWidget* /*widget*/, GdkEvent* /*event*/, |
| 185 |
gpointer /*user_data*/) |
| 186 |
{ |
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
std::function<void()> Gtk3KDE5FilePickerIpc::blockMainWindow() |
| 191 |
{ |
| 192 |
weld::Window* pParentWin = Application::GetDefDialogParent(); |
| 193 |
if (!pParentWin) |
| 194 |
return {}; |
| 195 |
|
| 196 |
const SystemEnvData aSysData = pParentWin->get_system_data(); |
| 197 |
auto* pMainWindow = static_cast<GtkWidget*>(aSysData.pWidget); |
| 198 |
if (!pMainWindow) |
| 199 |
return {}; |
| 200 |
|
| 201 |
sendCommand(Commands::SetWinId, aSysData.GetWindowHandle(aSysData.pSalFrame)); |
| 202 |
|
| 203 |
SolarMutexGuard guard; |
| 204 |
auto deleteEventSignalId = g_signal_lookup("delete_event", gtk_widget_get_type()); |
| 205 |
|
| 206 |
// disable the mainwindow |
| 207 |
gtk_widget_set_sensitive(pMainWindow, false); |
| 208 |
|
| 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); |
| 214 |
|
| 215 |
// prevent the window from being closed |
| 216 |
auto ignoreDeleteEventHandler |
| 217 |
= g_signal_connect(pMainWindow, "delete_event", G_CALLBACK(ignoreDeleteEvent), nullptr); |
| 218 |
|
| 219 |
return [pMainWindow, ignoreDeleteEventHandler, blockedHandler] { |
| 220 |
SolarMutexGuard cleanupGuard; |
| 221 |
// re-enable window |
| 222 |
gtk_widget_set_sensitive(pMainWindow, true); |
| 223 |
|
| 224 |
// allow it to be closed again |
| 225 |
g_signal_handler_disconnect(pMainWindow, ignoreDeleteEventHandler); |
| 226 |
|
| 227 |
// unblock the GtkSalFrame handler |
| 228 |
g_signal_handler_unblock(pMainWindow, blockedHandler); |
| 229 |
}; |
| 230 |
} |
| 231 |
|
| 232 |
void Gtk3KDE5FilePickerIpc::writeResponseLine(const std::string& line) |
| 233 |
{ |
| 234 |
sal_uInt64 bytesWritten = 0; |
| 235 |
osl_writeFile(m_inputWrite, line.c_str(), line.size(), &bytesWritten); |
| 236 |
} |
| 237 |
|
| 238 |
std::string Gtk3KDE5FilePickerIpc::readResponseLine() |
| 239 |
{ |
| 240 |
if (!m_responseBuffer.empty()) // check whether we have a line in our buffer |
| 241 |
{ |
| 242 |
std::size_t it = m_responseBuffer.find('\n'); |
| 243 |
if (it != std::string::npos) |
| 244 |
{ |
| 245 |
auto ret = m_responseBuffer.substr(0, it); |
| 246 |
m_responseBuffer.erase(0, it + 1); |
| 247 |
return ret; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
const sal_uInt64 BUF_SIZE = 1024; |
| 252 |
char buffer[BUF_SIZE]; |
| 253 |
while (true) |
| 254 |
{ |
| 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 |
| 259 |
{ |
| 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 |
| 263 |
++it; |
| 264 |
m_responseBuffer.assign(it, buffer + bytesRead); |
| 265 |
return ret; |
| 266 |
} |
| 267 |
// otherwise append everything we read to the buffer and try again |
| 268 |
m_responseBuffer.append(buffer, bytesRead); |
| 269 |
|
| 270 |
if (err != osl_File_E_None && err != osl_File_E_AGAIN) |
| 271 |
break; |
| 272 |
} |
| 273 |
return {}; |
| 274 |
} |
| 275 |
|
| 276 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |