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