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