xref: /aoo42x/main/ucb/source/ucp/gio/gio_mount.cxx (revision 2f86921c)
1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2f86921cSAndrew Rist  * distributed with this work for additional information
6*2f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2f86921cSAndrew Rist  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2f86921cSAndrew Rist  *
13*2f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist  * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2f86921cSAndrew Rist  * specific language governing permissions and limitations
18*2f86921cSAndrew Rist  * under the License.
19*2f86921cSAndrew Rist  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "gio_mount.hxx"
25cdf0e10cSrcweir #include <ucbhelper/simpleauthenticationrequest.hxx>
26cdf0e10cSrcweir #include <stdio.h>
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION);
30cdf0e10cSrcweir 
31cdf0e10cSrcweir static void ooo_mount_operation_ask_password (GMountOperation   *op,
32cdf0e10cSrcweir     const char *message, const char *default_user, const char *default_domain,
33cdf0e10cSrcweir     GAskPasswordFlags flags);
34cdf0e10cSrcweir 
ooo_mount_operation_init(OOoMountOperation * op)35cdf0e10cSrcweir static void ooo_mount_operation_init (OOoMountOperation *op)
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     op->m_pPrevPassword = NULL;
38cdf0e10cSrcweir     op->m_pPrevUsername = NULL;
39cdf0e10cSrcweir }
40cdf0e10cSrcweir 
ooo_mount_operation_finalize(GObject * object)41cdf0e10cSrcweir static void ooo_mount_operation_finalize (GObject *object)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object);
44cdf0e10cSrcweir     if (mount_op->m_pPrevUsername)
45cdf0e10cSrcweir         free(mount_op->m_pPrevUsername);
46cdf0e10cSrcweir     if (mount_op->m_pPrevPassword)
47cdf0e10cSrcweir         free(mount_op->m_pPrevPassword);
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object);
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
ooo_mount_operation_class_init(OOoMountOperationClass * klass)52cdf0e10cSrcweir static void ooo_mount_operation_class_init (OOoMountOperationClass *klass)
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     GObjectClass *object_class = G_OBJECT_CLASS (klass);
55cdf0e10cSrcweir     object_class->finalize = ooo_mount_operation_finalize;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
58cdf0e10cSrcweir     mount_op_class->ask_password = ooo_mount_operation_ask_password;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir using namespace com::sun::star;
62cdf0e10cSrcweir 
ooo_mount_operation_ask_password(GMountOperation * op,const char *,const char * default_user,const char * default_domain,GAskPasswordFlags flags)63cdf0e10cSrcweir static void ooo_mount_operation_ask_password (GMountOperation *op,
64cdf0e10cSrcweir     const char * /*message*/, const char *default_user,
65cdf0e10cSrcweir     const char *default_domain, GAskPasswordFlags flags)
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     uno::Reference< task::XInteractionHandler > xIH;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     OOoMountOperation *pThis = (OOoMountOperation*)op;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     const com::sun::star::uno::Reference< com::sun::star::ucb::XCommandEnvironment > &xEnv = *(pThis->pEnv);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     if (xEnv.is())
74cdf0e10cSrcweir       xIH = xEnv->getInteractionHandler();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     if (!xIH.is())
77cdf0e10cSrcweir     {
78cdf0e10cSrcweir         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
79cdf0e10cSrcweir         return;
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     ::rtl::OUString aHostName, aDomain, aUserName, aPassword;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     ucbhelper::SimpleAuthenticationRequest::EntityType eUserName =
85cdf0e10cSrcweir         (flags & G_ASK_PASSWORD_NEED_USERNAME)
86cdf0e10cSrcweir           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
87cdf0e10cSrcweir           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     if (default_user)
90cdf0e10cSrcweir         aUserName = rtl::OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     ucbhelper::SimpleAuthenticationRequest::EntityType ePassword =
93cdf0e10cSrcweir         (flags & G_ASK_PASSWORD_NEED_PASSWORD)
94cdf0e10cSrcweir           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
95cdf0e10cSrcweir           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     rtl::OUString aPrevPassword, aPrevUsername;
98cdf0e10cSrcweir     if (pThis->m_pPrevUsername)
99cdf0e10cSrcweir         aPrevUsername = rtl::OUString(pThis->m_pPrevUsername, strlen(pThis->m_pPrevUsername), RTL_TEXTENCODING_UTF8);
100cdf0e10cSrcweir     if (pThis->m_pPrevPassword)
101cdf0e10cSrcweir         aPrevPassword = rtl::OUString(pThis->m_pPrevPassword, strlen(pThis->m_pPrevPassword), RTL_TEXTENCODING_UTF8);
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
104cdf0e10cSrcweir     if ( aUserName.getLength() == 0 )
105cdf0e10cSrcweir         aUserName = aPrevUsername;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir     if ( aPassword.getLength() == 0 )
108cdf0e10cSrcweir         aPassword = aPrevPassword;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     ucbhelper::SimpleAuthenticationRequest::EntityType eDomain =
111cdf0e10cSrcweir         (flags & G_ASK_PASSWORD_NEED_DOMAIN)
112cdf0e10cSrcweir           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
113cdf0e10cSrcweir           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     if (default_domain)
116cdf0e10cSrcweir         aDomain = rtl::OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8);
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     uno::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
119cdf0e10cSrcweir         = new ucbhelper::SimpleAuthenticationRequest (rtl::OUString() /* FIXME: provide URL here */, aHostName, eDomain, aDomain, eUserName, aUserName, ePassword, aPassword);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     xIH->handle( xRequest.get() );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection();
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     if ( !xSelection.is() )
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
128cdf0e10cSrcweir         return;
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     uno::Reference< task::XInteractionAbort > xAbort(xSelection.get(), uno::UNO_QUERY );
132cdf0e10cSrcweir     if ( xAbort.is() )
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
135cdf0e10cSrcweir         return;
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir     const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier();
139cdf0e10cSrcweir     aUserName = xSupp->getUserName();
140cdf0e10cSrcweir     aPassword = xSupp->getPassword();
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     if (flags & G_ASK_PASSWORD_NEED_USERNAME)
143cdf0e10cSrcweir         g_mount_operation_set_username(op, rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
146cdf0e10cSrcweir         g_mount_operation_set_password(op, rtl::OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
149cdf0e10cSrcweir         g_mount_operation_set_domain(op, rtl::OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr());
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     switch (xSupp->getRememberPasswordMode())
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir 	default:
154cdf0e10cSrcweir         case ucb::RememberAuthentication_NO:
155cdf0e10cSrcweir             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER);
156cdf0e10cSrcweir             break;
157cdf0e10cSrcweir         case ucb::RememberAuthentication_SESSION:
158cdf0e10cSrcweir             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION);
159cdf0e10cSrcweir             break;
160cdf0e10cSrcweir         case ucb::RememberAuthentication_PERSISTENT:
161cdf0e10cSrcweir             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY);
162cdf0e10cSrcweir             break;
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     if (pThis->m_pPrevPassword)
166cdf0e10cSrcweir         free(pThis->m_pPrevPassword);
167cdf0e10cSrcweir     pThis->m_pPrevPassword = strdup(rtl::OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
168cdf0e10cSrcweir     if (pThis->m_pPrevUsername)
169cdf0e10cSrcweir         free(pThis->m_pPrevUsername);
170cdf0e10cSrcweir     pThis->m_pPrevUsername = strdup(rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
171cdf0e10cSrcweir     g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
ooo_mount_operation_new(const uno::Reference<ucb::XCommandEnvironment> & rEnv)174cdf0e10cSrcweir GMountOperation *ooo_mount_operation_new(const uno::Reference< ucb::XCommandEnvironment >& rEnv)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     OOoMountOperation *pRet = (OOoMountOperation*)g_object_new (OOO_TYPE_MOUNT_OPERATION, NULL);
177cdf0e10cSrcweir     pRet->pEnv = &rEnv;
178cdf0e10cSrcweir     return (GMountOperation*)pRet;
179cdf0e10cSrcweir }
180