bump product version to 4.1.6.2
[LibreOffice.git] / ucb / source / ucp / gio / gio_mount.cxx
blob30c97e29795f85f8a484710eb78ade79cbc55766
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "gio_mount.hxx"
21 #include <ucbhelper/simpleauthenticationrequest.hxx>
22 #include <stdio.h>
23 #include <string.h>
25 G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION);
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);
31 static void ooo_mount_operation_init (OOoMountOperation *op)
33 op->m_pPrevPassword = NULL;
34 op->m_pPrevUsername = NULL;
37 static void ooo_mount_operation_finalize (GObject *object)
39 OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object);
40 if (mount_op->m_pPrevUsername)
41 free(mount_op->m_pPrevUsername);
42 if (mount_op->m_pPrevPassword)
43 free(mount_op->m_pPrevPassword);
45 G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object);
48 static void ooo_mount_operation_class_init (OOoMountOperationClass *klass)
50 GObjectClass *object_class = G_OBJECT_CLASS (klass);
51 object_class->finalize = ooo_mount_operation_finalize;
53 GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
54 mount_op_class->ask_password = ooo_mount_operation_ask_password;
57 using namespace com::sun::star;
59 static void ooo_mount_operation_ask_password (GMountOperation *op,
60 const char * /*message*/, const char *default_user,
61 const char *default_domain, GAskPasswordFlags flags)
63 uno::Reference< task::XInteractionHandler > xIH;
65 OOoMountOperation *pThis = (OOoMountOperation*)op;
67 const com::sun::star::uno::Reference< com::sun::star::ucb::XCommandEnvironment > &xEnv = *(pThis->pEnv);
69 if (xEnv.is())
70 xIH = xEnv->getInteractionHandler();
72 if (!xIH.is())
74 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
75 return;
78 OUString aHostName, aDomain, aUserName, aPassword;
80 ucbhelper::SimpleAuthenticationRequest::EntityType eUserName =
81 (flags & G_ASK_PASSWORD_NEED_USERNAME)
82 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
83 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
85 if (default_user)
86 aUserName = OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8);
88 ucbhelper::SimpleAuthenticationRequest::EntityType ePassword =
89 (flags & G_ASK_PASSWORD_NEED_PASSWORD)
90 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
91 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
93 OUString aPrevPassword, aPrevUsername;
94 if (pThis->m_pPrevUsername)
95 aPrevUsername = OUString(pThis->m_pPrevUsername, strlen(pThis->m_pPrevUsername), RTL_TEXTENCODING_UTF8);
96 if (pThis->m_pPrevPassword)
97 aPrevPassword = OUString(pThis->m_pPrevPassword, strlen(pThis->m_pPrevPassword), RTL_TEXTENCODING_UTF8);
99 //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
100 if ( aUserName.isEmpty() )
101 aUserName = aPrevUsername;
103 if ( aPassword.isEmpty() )
104 aPassword = aPrevPassword;
106 ucbhelper::SimpleAuthenticationRequest::EntityType eDomain =
107 (flags & G_ASK_PASSWORD_NEED_DOMAIN)
108 ? ucbhelper::SimpleAuthenticationRequest::ENTITY_MODIFY
109 : ucbhelper::SimpleAuthenticationRequest::ENTITY_NA;
111 if (default_domain)
112 aDomain = OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8);
114 uno::Reference< ucbhelper::SimpleAuthenticationRequest > xRequest
115 = new ucbhelper::SimpleAuthenticationRequest (OUString() /* FIXME: provide URL here */, aHostName, eDomain, aDomain, eUserName, aUserName, ePassword, aPassword);
117 xIH->handle( xRequest.get() );
119 rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection();
121 if ( !xSelection.is() )
123 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
124 return;
127 uno::Reference< task::XInteractionAbort > xAbort(xSelection.get(), uno::UNO_QUERY );
128 if ( xAbort.is() )
130 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
131 return;
134 const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier();
135 aUserName = xSupp->getUserName();
136 aPassword = xSupp->getPassword();
138 if (flags & G_ASK_PASSWORD_NEED_USERNAME)
139 g_mount_operation_set_username(op, OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
141 if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
142 g_mount_operation_set_password(op, OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
144 if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
145 g_mount_operation_set_domain(op, OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr());
147 switch (xSupp->getRememberPasswordMode())
149 default:
150 case ucb::RememberAuthentication_NO:
151 g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER);
152 break;
153 case ucb::RememberAuthentication_SESSION:
154 g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION);
155 break;
156 case ucb::RememberAuthentication_PERSISTENT:
157 g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY);
158 break;
161 if (pThis->m_pPrevPassword)
162 free(pThis->m_pPrevPassword);
163 pThis->m_pPrevPassword = strdup(OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
164 if (pThis->m_pPrevUsername)
165 free(pThis->m_pPrevUsername);
166 pThis->m_pPrevUsername = strdup(OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
167 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
170 GMountOperation *ooo_mount_operation_new(const uno::Reference< ucb::XCommandEnvironment >& rEnv)
172 OOoMountOperation *pRet = (OOoMountOperation*)g_object_new (OOO_TYPE_MOUNT_OPERATION, NULL);
173 pRet->pEnv = &rEnv;
174 return (GMountOperation*)pRet;
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */