Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / ucb / source / ucp / gio / gio_mount.cxx
blobb78817c670e231eef1b89492adf20078ce8281b7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <utility>
24 #include "gio_mount.hxx"
25 #include <ucbhelper/simpleauthenticationrequest.hxx>
26 #include <string.h>
28 #ifdef __GNUC__
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wunused-function"
31 #if defined __clang__
32 #if __has_warning("-Wdeprecated-volatile")
33 #pragma clang diagnostic ignored "-Wdeprecated-volatile"
34 #endif
35 #endif
36 #endif
37 G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION);
38 #ifdef __GNUC__
39 #pragma GCC diagnostic pop
40 #endif
42 static void ooo_mount_operation_ask_password (GMountOperation *op,
43 const char *message, const char *default_user, const char *default_domain,
44 GAskPasswordFlags flags);
46 static void ooo_mount_operation_init (OOoMountOperation *op)
48 op->m_pPrevPassword = nullptr;
49 op->m_pPrevUsername = nullptr;
52 static void ooo_mount_operation_finalize (GObject *object)
54 OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object);
55 if (mount_op->m_pPrevUsername)
56 free(mount_op->m_pPrevUsername);
57 if (mount_op->m_pPrevPassword)
58 free(mount_op->m_pPrevPassword);
59 mount_op->context.reset();
61 G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object);
64 static void ooo_mount_operation_class_init (OOoMountOperationClass *klass)
66 GObjectClass *object_class = G_OBJECT_CLASS (klass);
67 object_class->finalize = ooo_mount_operation_finalize;
69 GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
70 mount_op_class->ask_password = ooo_mount_operation_ask_password;
73 namespace {
75 // Temporarily undo the g_main_context_push_thread_default done in the surrounding MountOperation
76 // ctor (in ucb/source/ucp/gio/gio_content.cxx):
77 struct GlibThreadDefaultMainContextScope {
78 public:
79 GlibThreadDefaultMainContextScope(GMainContext * context): context_(context)
80 { g_main_context_push_thread_default(context_); }
82 ~GlibThreadDefaultMainContextScope() { g_main_context_pop_thread_default(context_); }
84 private:
85 GMainContext * context_;
90 static void ooo_mount_operation_ask_password (GMountOperation *op,
91 const char * /*message*/, const char *default_user,
92 const char *default_domain, GAskPasswordFlags flags)
94 css::uno::Reference< css::task::XInteractionHandler > xIH;
96 OOoMountOperation *pThis = reinterpret_cast<OOoMountOperation*>(op);
97 GlibThreadDefaultMainContextScope scope(pThis->context.get());
99 const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv = *(pThis->pEnv);
101 if (xEnv.is())
102 xIH = xEnv->getInteractionHandler();
104 if (!xIH.is())
106 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
107 return;
110 OUString aDomain, aUserName, aPassword;
112 ucbhelper::SimpleAuthenticationRequest::EntityType eUserName =
113 (flags & G_ASK_PASSWORD_NEED_USERNAME)
114 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
115 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
117 if (default_user)
118 aUserName = OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8);
120 ucbhelper::SimpleAuthenticationRequest::EntityType ePassword =
121 (flags & G_ASK_PASSWORD_NEED_PASSWORD)
122 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
123 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
125 OUString aPrevPassword, aPrevUsername;
126 if (pThis->m_pPrevUsername)
127 aPrevUsername = OUString(pThis->m_pPrevUsername, strlen(pThis->m_pPrevUsername), RTL_TEXTENCODING_UTF8);
128 if (pThis->m_pPrevPassword)
129 aPrevPassword = OUString(pThis->m_pPrevPassword, strlen(pThis->m_pPrevPassword), RTL_TEXTENCODING_UTF8);
131 //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
132 if ( aUserName.isEmpty() )
133 aUserName = aPrevUsername;
135 if ( aPassword.isEmpty() )
136 aPassword = aPrevPassword;
138 ucbhelper::SimpleAuthenticationRequest::EntityType eDomain =
139 (flags & G_ASK_PASSWORD_NEED_DOMAIN)
140 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
141 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
143 if (default_domain)
144 aDomain = OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8);
146 rtl::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
147 = new ucbhelper::SimpleAuthenticationRequest (OUString() /* FIXME: provide URL here */, OUString(), eDomain, aDomain, eUserName, aUserName, ePassword, aPassword);
149 xIH->handle( xRequest.get() );
151 rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection();
153 if ( !xSelection.is() )
155 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
156 return;
159 css::uno::Reference< css::task::XInteractionAbort > xAbort(xSelection.get(), css::uno::UNO_QUERY );
160 if ( xAbort.is() )
162 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
163 return;
166 const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier();
167 aUserName = xSupp->getUserName();
168 aPassword = xSupp->getPassword();
170 if (flags & G_ASK_PASSWORD_NEED_USERNAME)
171 g_mount_operation_set_username(op, OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
173 if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
174 g_mount_operation_set_password(op, OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
176 if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
177 g_mount_operation_set_domain(op, OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr());
179 switch (xSupp->getRememberPasswordMode())
181 default:
182 case css::ucb::RememberAuthentication_NO:
183 g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER);
184 break;
185 case css::ucb::RememberAuthentication_SESSION:
186 g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION);
187 break;
188 case css::ucb::RememberAuthentication_PERSISTENT:
189 g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY);
190 break;
193 if (pThis->m_pPrevPassword)
194 free(pThis->m_pPrevPassword);
195 pThis->m_pPrevPassword = strdup(OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
196 if (pThis->m_pPrevUsername)
197 free(pThis->m_pPrevUsername);
198 pThis->m_pPrevUsername = strdup(OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
199 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
202 GMountOperation *ooo_mount_operation_new(ucb::ucp::gio::glib::MainContextRef && context, const css::uno::Reference< css::ucb::XCommandEnvironment >& rEnv)
204 OOoMountOperation *pRet = static_cast<OOoMountOperation*>(g_object_new (OOO_TYPE_MOUNT_OPERATION, nullptr));
205 pRet->context = std::move(context);
206 pRet->pEnv = &rEnv;
207 return &pRet->parent_instance;
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */