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 "gio_mount.hxx"
21 #include <ucbhelper/simpleauthenticationrequest.hxx>
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wunused-function"
28 G_DEFINE_TYPE (OOoMountOperation
, ooo_mount_operation
, G_TYPE_MOUNT_OPERATION
);
30 #pragma GCC diagnostic pop
33 static void ooo_mount_operation_ask_password (GMountOperation
*op
,
34 const char *message
, const char *default_user
, const char *default_domain
,
35 GAskPasswordFlags flags
);
37 static void ooo_mount_operation_init (OOoMountOperation
*op
)
39 op
->m_pPrevPassword
= NULL
;
40 op
->m_pPrevUsername
= NULL
;
43 static void ooo_mount_operation_finalize (GObject
*object
)
45 OOoMountOperation
*mount_op
= OOO_MOUNT_OPERATION (object
);
46 if (mount_op
->m_pPrevUsername
)
47 free(mount_op
->m_pPrevUsername
);
48 if (mount_op
->m_pPrevPassword
)
49 free(mount_op
->m_pPrevPassword
);
51 G_OBJECT_CLASS (ooo_mount_operation_parent_class
)->finalize (object
);
54 static void ooo_mount_operation_class_init (OOoMountOperationClass
*klass
)
56 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
57 object_class
->finalize
= ooo_mount_operation_finalize
;
59 GMountOperationClass
*mount_op_class
= G_MOUNT_OPERATION_CLASS (klass
);
60 mount_op_class
->ask_password
= ooo_mount_operation_ask_password
;
63 using namespace com::sun::star
;
65 static void ooo_mount_operation_ask_password (GMountOperation
*op
,
66 const char * /*message*/, const char *default_user
,
67 const char *default_domain
, GAskPasswordFlags flags
)
69 uno::Reference
< task::XInteractionHandler
> xIH
;
71 OOoMountOperation
*pThis
= reinterpret_cast<OOoMountOperation
*>(op
);
73 const com::sun::star::uno::Reference
< com::sun::star::ucb::XCommandEnvironment
> &xEnv
= *(pThis
->pEnv
);
76 xIH
= xEnv
->getInteractionHandler();
80 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
84 OUString aHostName
, aDomain
, aUserName
, aPassword
;
86 ucbhelper::SimpleAuthenticationRequest::EntityType eUserName
=
87 (flags
& G_ASK_PASSWORD_NEED_USERNAME
)
88 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
89 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA
;
92 aUserName
= OUString(default_user
, strlen(default_user
), RTL_TEXTENCODING_UTF8
);
94 ucbhelper::SimpleAuthenticationRequest::EntityType ePassword
=
95 (flags
& G_ASK_PASSWORD_NEED_PASSWORD
)
96 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
97 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA
;
99 OUString aPrevPassword
, aPrevUsername
;
100 if (pThis
->m_pPrevUsername
)
101 aPrevUsername
= OUString(pThis
->m_pPrevUsername
, strlen(pThis
->m_pPrevUsername
), RTL_TEXTENCODING_UTF8
);
102 if (pThis
->m_pPrevPassword
)
103 aPrevPassword
= OUString(pThis
->m_pPrevPassword
, strlen(pThis
->m_pPrevPassword
), RTL_TEXTENCODING_UTF8
);
105 //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
106 if ( aUserName
.isEmpty() )
107 aUserName
= aPrevUsername
;
109 if ( aPassword
.isEmpty() )
110 aPassword
= aPrevPassword
;
112 ucbhelper::SimpleAuthenticationRequest::EntityType eDomain
=
113 (flags
& G_ASK_PASSWORD_NEED_DOMAIN
)
114 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
115 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA
;
118 aDomain
= OUString(default_domain
, strlen(default_domain
), RTL_TEXTENCODING_UTF8
);
120 uno::Reference
< ucbhelper::SimpleAuthenticationRequest
> xRequest
121 = new ucbhelper::SimpleAuthenticationRequest (OUString() /* FIXME: provide URL here */, aHostName
, eDomain
, aDomain
, eUserName
, aUserName
, ePassword
, aPassword
);
123 xIH
->handle( xRequest
.get() );
125 rtl::Reference
< ucbhelper::InteractionContinuation
> xSelection
= xRequest
->getSelection();
127 if ( !xSelection
.is() )
129 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
133 uno::Reference
< task::XInteractionAbort
> xAbort(xSelection
.get(), uno::UNO_QUERY
);
136 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
140 const rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> & xSupp
= xRequest
->getAuthenticationSupplier();
141 aUserName
= xSupp
->getUserName();
142 aPassword
= xSupp
->getPassword();
144 if (flags
& G_ASK_PASSWORD_NEED_USERNAME
)
145 g_mount_operation_set_username(op
, OUStringToOString(aUserName
, RTL_TEXTENCODING_UTF8
).getStr());
147 if (flags
& G_ASK_PASSWORD_NEED_PASSWORD
)
148 g_mount_operation_set_password(op
, OUStringToOString(aPassword
, RTL_TEXTENCODING_UTF8
).getStr());
150 if (flags
& G_ASK_PASSWORD_NEED_DOMAIN
)
151 g_mount_operation_set_domain(op
, OUStringToOString(xSupp
->getRealm(), RTL_TEXTENCODING_UTF8
).getStr());
153 switch (xSupp
->getRememberPasswordMode())
156 case ucb::RememberAuthentication_NO
:
157 g_mount_operation_set_password_save(op
, G_PASSWORD_SAVE_NEVER
);
159 case ucb::RememberAuthentication_SESSION
:
160 g_mount_operation_set_password_save(op
, G_PASSWORD_SAVE_FOR_SESSION
);
162 case ucb::RememberAuthentication_PERSISTENT
:
163 g_mount_operation_set_password_save(op
, G_PASSWORD_SAVE_PERMANENTLY
);
167 if (pThis
->m_pPrevPassword
)
168 free(pThis
->m_pPrevPassword
);
169 pThis
->m_pPrevPassword
= strdup(OUStringToOString(aPassword
, RTL_TEXTENCODING_UTF8
).getStr());
170 if (pThis
->m_pPrevUsername
)
171 free(pThis
->m_pPrevUsername
);
172 pThis
->m_pPrevUsername
= strdup(OUStringToOString(aUserName
, RTL_TEXTENCODING_UTF8
).getStr());
173 g_mount_operation_reply (op
, G_MOUNT_OPERATION_HANDLED
);
176 GMountOperation
*ooo_mount_operation_new(const uno::Reference
< ucb::XCommandEnvironment
>& rEnv
)
178 OOoMountOperation
*pRet
= static_cast<OOoMountOperation
*>(g_object_new (OOO_TYPE_MOUNT_OPERATION
, NULL
));
180 return &pRet
->parent_instance
;
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */