xref: /trunk/main/ucb/source/ucp/gio/gio_mount.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "gio_mount.hxx"
29 #include <ucbhelper/simpleauthenticationrequest.hxx>
30 #include <stdio.h>
31 #include <string.h>
32 
33 G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION);
34 
35 static void ooo_mount_operation_ask_password (GMountOperation   *op,
36     const char *message, const char *default_user, const char *default_domain,
37     GAskPasswordFlags flags);
38 
39 static void ooo_mount_operation_init (OOoMountOperation *op)
40 {
41     op->m_pPrevPassword = NULL;
42     op->m_pPrevUsername = NULL;
43 }
44 
45 static void ooo_mount_operation_finalize (GObject *object)
46 {
47     OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object);
48     if (mount_op->m_pPrevUsername)
49         free(mount_op->m_pPrevUsername);
50     if (mount_op->m_pPrevPassword)
51         free(mount_op->m_pPrevPassword);
52 
53     G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object);
54 }
55 
56 static void ooo_mount_operation_class_init (OOoMountOperationClass *klass)
57 {
58     GObjectClass *object_class = G_OBJECT_CLASS (klass);
59     object_class->finalize = ooo_mount_operation_finalize;
60 
61     GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
62     mount_op_class->ask_password = ooo_mount_operation_ask_password;
63 }
64 
65 using namespace com::sun::star;
66 
67 static void ooo_mount_operation_ask_password (GMountOperation *op,
68     const char * /*message*/, const char *default_user,
69     const char *default_domain, GAskPasswordFlags flags)
70 {
71     uno::Reference< task::XInteractionHandler > xIH;
72 
73     OOoMountOperation *pThis = (OOoMountOperation*)op;
74 
75     const com::sun::star::uno::Reference< com::sun::star::ucb::XCommandEnvironment > &xEnv = *(pThis->pEnv);
76 
77     if (xEnv.is())
78       xIH = xEnv->getInteractionHandler();
79 
80     if (!xIH.is())
81     {
82         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
83         return;
84     }
85 
86     ::rtl::OUString aHostName, aDomain, aUserName, aPassword;
87 
88     ucbhelper::SimpleAuthenticationRequest::EntityType eUserName =
89         (flags & G_ASK_PASSWORD_NEED_USERNAME)
90           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
91           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
92 
93     if (default_user)
94         aUserName = rtl::OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8);
95 
96     ucbhelper::SimpleAuthenticationRequest::EntityType ePassword =
97         (flags & G_ASK_PASSWORD_NEED_PASSWORD)
98           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
99           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
100 
101     rtl::OUString aPrevPassword, aPrevUsername;
102     if (pThis->m_pPrevUsername)
103         aPrevUsername = rtl::OUString(pThis->m_pPrevUsername, strlen(pThis->m_pPrevUsername), RTL_TEXTENCODING_UTF8);
104     if (pThis->m_pPrevPassword)
105         aPrevPassword = rtl::OUString(pThis->m_pPrevPassword, strlen(pThis->m_pPrevPassword), RTL_TEXTENCODING_UTF8);
106 
107     //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
108     if ( aUserName.getLength() == 0 )
109         aUserName = aPrevUsername;
110 
111     if ( aPassword.getLength() == 0 )
112         aPassword = aPrevPassword;
113 
114     ucbhelper::SimpleAuthenticationRequest::EntityType eDomain =
115         (flags & G_ASK_PASSWORD_NEED_DOMAIN)
116           ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
117           : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
118 
119     if (default_domain)
120         aDomain = rtl::OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8);
121 
122     uno::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
123         = new ucbhelper::SimpleAuthenticationRequest (rtl::OUString() /* FIXME: provide URL here */, aHostName, eDomain, aDomain, eUserName, aUserName, ePassword, aPassword);
124 
125     xIH->handle( xRequest.get() );
126 
127     rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection();
128 
129     if ( !xSelection.is() )
130     {
131         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
132         return;
133     }
134 
135     uno::Reference< task::XInteractionAbort > xAbort(xSelection.get(), uno::UNO_QUERY );
136     if ( xAbort.is() )
137     {
138         g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
139         return;
140     }
141 
142     const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier();
143     aUserName = xSupp->getUserName();
144     aPassword = xSupp->getPassword();
145 
146     if (flags & G_ASK_PASSWORD_NEED_USERNAME)
147         g_mount_operation_set_username(op, rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
148 
149     if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
150         g_mount_operation_set_password(op, rtl::OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
151 
152     if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
153         g_mount_operation_set_domain(op, rtl::OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr());
154 
155     switch (xSupp->getRememberPasswordMode())
156     {
157 	default:
158         case ucb::RememberAuthentication_NO:
159             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER);
160             break;
161         case ucb::RememberAuthentication_SESSION:
162             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION);
163             break;
164         case ucb::RememberAuthentication_PERSISTENT:
165             g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY);
166             break;
167     }
168 
169     if (pThis->m_pPrevPassword)
170         free(pThis->m_pPrevPassword);
171     pThis->m_pPrevPassword = strdup(rtl::OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
172     if (pThis->m_pPrevUsername)
173         free(pThis->m_pPrevUsername);
174     pThis->m_pPrevUsername = strdup(rtl::OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
175     g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
176 }
177 
178 GMountOperation *ooo_mount_operation_new(const uno::Reference< ucb::XCommandEnvironment >& rEnv)
179 {
180     OOoMountOperation *pRet = (OOoMountOperation*)g_object_new (OOO_TYPE_MOUNT_OPERATION, NULL);
181     pRet->pEnv = &rEnv;
182     return (GMountOperation*)pRet;
183 }
184