Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "gio_mount.hxx"
21 : #include <ucbhelper/simpleauthenticationrequest.hxx>
22 : #include <stdio.h>
23 : #include <string.h>
24 :
25 0 : G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION);
26 :
27 : static void ooo_mount_operation_ask_password (GMountOperation *op,
28 : const char *message, const char *default_user, const char *default_domain,
29 : GAskPasswordFlags flags);
30 :
31 0 : static void ooo_mount_operation_init (OOoMountOperation *op)
32 : {
33 0 : op->m_pPrevPassword = NULL;
34 0 : op->m_pPrevUsername = NULL;
35 0 : }
36 :
37 0 : static void ooo_mount_operation_finalize (GObject *object)
38 : {
39 0 : OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object);
40 0 : if (mount_op->m_pPrevUsername)
41 0 : free(mount_op->m_pPrevUsername);
42 0 : if (mount_op->m_pPrevPassword)
43 0 : free(mount_op->m_pPrevPassword);
44 :
45 0 : G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object);
46 0 : }
47 :
48 0 : static void ooo_mount_operation_class_init (OOoMountOperationClass *klass)
49 : {
50 0 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
51 0 : object_class->finalize = ooo_mount_operation_finalize;
52 :
53 0 : GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
54 0 : mount_op_class->ask_password = ooo_mount_operation_ask_password;
55 0 : }
56 :
57 : using namespace com::sun::star;
58 :
59 0 : static void ooo_mount_operation_ask_password (GMountOperation *op,
60 : const char * /*message*/, const char *default_user,
61 : const char *default_domain, GAskPasswordFlags flags)
62 : {
63 0 : uno::Reference< task::XInteractionHandler > xIH;
64 :
65 0 : OOoMountOperation *pThis = (OOoMountOperation*)op;
66 :
67 0 : const com::sun::star::uno::Reference< com::sun::star::ucb::XCommandEnvironment > &xEnv = *(pThis->pEnv);
68 :
69 0 : if (xEnv.is())
70 0 : xIH = xEnv->getInteractionHandler();
71 :
72 0 : if (!xIH.is())
73 : {
74 0 : g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
75 0 : return;
76 : }
77 :
78 0 : OUString aHostName, aDomain, aUserName, aPassword;
79 :
80 : ucbhelper::SimpleAuthenticationRequest::EntityType eUserName =
81 0 : (flags & G_ASK_PASSWORD_NEED_USERNAME)
82 : ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
83 0 : : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
84 :
85 0 : if (default_user)
86 0 : aUserName = OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8);
87 :
88 : ucbhelper::SimpleAuthenticationRequest::EntityType ePassword =
89 0 : (flags & G_ASK_PASSWORD_NEED_PASSWORD)
90 : ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
91 0 : : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
92 :
93 0 : OUString aPrevPassword, aPrevUsername;
94 0 : if (pThis->m_pPrevUsername)
95 0 : aPrevUsername = OUString(pThis->m_pPrevUsername, strlen(pThis->m_pPrevUsername), RTL_TEXTENCODING_UTF8);
96 0 : if (pThis->m_pPrevPassword)
97 0 : aPrevPassword = OUString(pThis->m_pPrevPassword, strlen(pThis->m_pPrevPassword), RTL_TEXTENCODING_UTF8);
98 :
99 : //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
100 0 : if ( aUserName.isEmpty() )
101 0 : aUserName = aPrevUsername;
102 :
103 0 : if ( aPassword.isEmpty() )
104 0 : aPassword = aPrevPassword;
105 :
106 : ucbhelper::SimpleAuthenticationRequest::EntityType eDomain =
107 0 : (flags & G_ASK_PASSWORD_NEED_DOMAIN)
108 : ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
109 0 : : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
110 :
111 0 : if (default_domain)
112 0 : aDomain = OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8);
113 :
114 : uno::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
115 0 : = new ucbhelper::SimpleAuthenticationRequest (OUString() /* FIXME: provide URL here */, aHostName, eDomain, aDomain, eUserName, aUserName, ePassword, aPassword);
116 :
117 0 : xIH->handle( xRequest.get() );
118 :
119 0 : rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection();
120 :
121 0 : if ( !xSelection.is() )
122 : {
123 0 : g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
124 0 : return;
125 : }
126 :
127 0 : uno::Reference< task::XInteractionAbort > xAbort(xSelection.get(), uno::UNO_QUERY );
128 0 : if ( xAbort.is() )
129 : {
130 0 : g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
131 0 : return;
132 : }
133 :
134 0 : const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier();
135 0 : aUserName = xSupp->getUserName();
136 0 : aPassword = xSupp->getPassword();
137 :
138 0 : if (flags & G_ASK_PASSWORD_NEED_USERNAME)
139 0 : g_mount_operation_set_username(op, OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
140 :
141 0 : if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
142 0 : g_mount_operation_set_password(op, OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
143 :
144 0 : if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
145 0 : g_mount_operation_set_domain(op, OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr());
146 :
147 0 : switch (xSupp->getRememberPasswordMode())
148 : {
149 : default:
150 : case ucb::RememberAuthentication_NO:
151 0 : g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER);
152 0 : break;
153 : case ucb::RememberAuthentication_SESSION:
154 0 : g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION);
155 0 : break;
156 : case ucb::RememberAuthentication_PERSISTENT:
157 0 : g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY);
158 0 : break;
159 : }
160 :
161 0 : if (pThis->m_pPrevPassword)
162 0 : free(pThis->m_pPrevPassword);
163 0 : pThis->m_pPrevPassword = strdup(OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
164 0 : if (pThis->m_pPrevUsername)
165 0 : free(pThis->m_pPrevUsername);
166 0 : pThis->m_pPrevUsername = strdup(OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
167 0 : g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
168 : }
169 :
170 0 : GMountOperation *ooo_mount_operation_new(const uno::Reference< ucb::XCommandEnvironment >& rEnv)
171 : {
172 0 : OOoMountOperation *pRet = (OOoMountOperation*)g_object_new (OOO_TYPE_MOUNT_OPERATION, NULL);
173 0 : pRet->pEnv = &rEnv;
174 0 : return (GMountOperation*)pRet;
175 : }
176 :
177 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|