1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gio_mount.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "gio_mount.hxx"
32 #include <ucbhelper/simpleauthenticationrequest.hxx>
36 G_DEFINE_TYPE (OOoMountOperation
, ooo_mount_operation
, G_TYPE_MOUNT_OPERATION
);
38 static void ooo_mount_operation_ask_password (GMountOperation
*op
,
39 const char *message
, const char *default_user
, const char *default_domain
,
40 GAskPasswordFlags flags
);
42 static void ooo_mount_operation_init (OOoMountOperation
*op
)
44 op
->m_pPrevPassword
= NULL
;
45 op
->m_pPrevUsername
= NULL
;
48 static void ooo_mount_operation_finalize (GObject
*object
)
50 OOoMountOperation
*mount_op
= OOO_MOUNT_OPERATION (object
);
51 if (mount_op
->m_pPrevUsername
)
52 free(mount_op
->m_pPrevUsername
);
53 if (mount_op
->m_pPrevPassword
)
54 free(mount_op
->m_pPrevPassword
);
56 G_OBJECT_CLASS (ooo_mount_operation_parent_class
)->finalize (object
);
59 static void ooo_mount_operation_class_init (OOoMountOperationClass
*klass
)
61 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
62 object_class
->finalize
= ooo_mount_operation_finalize
;
64 GMountOperationClass
*mount_op_class
= G_MOUNT_OPERATION_CLASS (klass
);
65 mount_op_class
->ask_password
= ooo_mount_operation_ask_password
;
68 using namespace com::sun::star
;
70 static void ooo_mount_operation_ask_password (GMountOperation
*op
,
71 const char *message
, const char *default_user
,
72 const char *default_domain
, GAskPasswordFlags flags
)
74 uno::Reference
< task::XInteractionHandler
> xIH
;
76 OOoMountOperation
*pThis
= (OOoMountOperation
*)op
;
78 const com::sun::star::uno::Reference
< com::sun::star::ucb::XCommandEnvironment
> &xEnv
= *(pThis
->pEnv
);
81 xIH
= xEnv
->getInteractionHandler();
85 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
89 ::rtl::OUString aHostName
, aDomain
, aUserName
, aPassword
;
91 ucbhelper::SimpleAuthenticationRequest::EntityType eUserName
=
92 (flags
& G_ASK_PASSWORD_NEED_USERNAME
)
93 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
94 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA
;
97 aUserName
= rtl::OUString(default_user
, strlen(default_user
), RTL_TEXTENCODING_UTF8
);
99 ucbhelper::SimpleAuthenticationRequest::EntityType ePassword
=
100 (flags
& G_ASK_PASSWORD_NEED_PASSWORD
)
101 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
102 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA
;
104 rtl::OUString aPrevPassword
, aPrevUsername
;
105 if (pThis
->m_pPrevUsername
)
106 aPrevUsername
= rtl::OUString(pThis
->m_pPrevUsername
, strlen(pThis
->m_pPrevUsername
), RTL_TEXTENCODING_UTF8
);
107 if (pThis
->m_pPrevPassword
)
108 aPrevPassword
= rtl::OUString(pThis
->m_pPrevPassword
, strlen(pThis
->m_pPrevPassword
), RTL_TEXTENCODING_UTF8
);
110 //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
111 if ( aUserName
.getLength() == 0 )
112 aUserName
= aPrevUsername
;
114 if ( aPassword
.getLength() == 0 )
115 aPassword
= aPrevPassword
;
117 ucbhelper::SimpleAuthenticationRequest::EntityType eDomain
=
118 (flags
& G_ASK_PASSWORD_NEED_DOMAIN
)
119 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
120 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA
;
123 aDomain
= rtl::OUString(default_domain
, strlen(default_domain
), RTL_TEXTENCODING_UTF8
);
125 uno::Reference
< ucbhelper::SimpleAuthenticationRequest
> xRequest
126 = new ucbhelper::SimpleAuthenticationRequest (aHostName
, eDomain
, aDomain
, eUserName
, aUserName
, ePassword
, aPassword
);
128 xIH
->handle( xRequest
.get() );
130 rtl::Reference
< ucbhelper::InteractionContinuation
> xSelection
= xRequest
->getSelection();
132 if ( !xSelection
.is() )
134 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
138 uno::Reference
< task::XInteractionAbort
> xAbort(xSelection
.get(), uno::UNO_QUERY
);
141 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
145 const rtl::Reference
< ucbhelper::InteractionSupplyAuthentication
> & xSupp
= xRequest
->getAuthenticationSupplier();
146 aUserName
= xSupp
->getUserName();
147 aPassword
= xSupp
->getPassword();
149 if (flags
& G_ASK_PASSWORD_NEED_USERNAME
)
150 g_mount_operation_set_username(op
, rtl::OUStringToOString(aUserName
, RTL_TEXTENCODING_UTF8
).getStr());
152 if (flags
& G_ASK_PASSWORD_NEED_PASSWORD
)
153 g_mount_operation_set_password(op
, rtl::OUStringToOString(aPassword
, RTL_TEXTENCODING_UTF8
).getStr());
155 if (flags
& G_ASK_PASSWORD_NEED_DOMAIN
)
156 g_mount_operation_set_domain(op
, rtl::OUStringToOString(xSupp
->getRealm(), RTL_TEXTENCODING_UTF8
).getStr());
158 switch (xSupp
->getRememberPasswordMode())
161 case ucb::RememberAuthentication_NO
:
162 g_mount_operation_set_password_save(op
, G_PASSWORD_SAVE_NEVER
);
164 case ucb::RememberAuthentication_SESSION
:
165 g_mount_operation_set_password_save(op
, G_PASSWORD_SAVE_FOR_SESSION
);
167 case ucb::RememberAuthentication_PERSISTENT
:
168 g_mount_operation_set_password_save(op
, G_PASSWORD_SAVE_PERMANENTLY
);
172 if (pThis
->m_pPrevPassword
)
173 free(pThis
->m_pPrevPassword
);
174 pThis
->m_pPrevPassword
= strdup(rtl::OUStringToOString(aPassword
, RTL_TEXTENCODING_UTF8
).getStr());
175 if (pThis
->m_pPrevUsername
)
176 free(pThis
->m_pPrevUsername
);
177 pThis
->m_pPrevUsername
= strdup(rtl::OUStringToOString(aUserName
, RTL_TEXTENCODING_UTF8
).getStr());
178 g_mount_operation_reply (op
, G_MOUNT_OPERATION_HANDLED
);
181 GMountOperation
*ooo_mount_operation_new(const uno::Reference
< ucb::XCommandEnvironment
>& rEnv
)
183 OOoMountOperation
*pRet
= (OOoMountOperation
*)g_object_new (OOO_TYPE_MOUNT_OPERATION
, NULL
);
185 return (GMountOperation
*)pRet
;