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 <sal/config.h>
21 
22 #include <utility>
23 
24 #include "gio_mount.hxx"
26 #include <string.h>
27 
28 #ifdef __GNUC__
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wunused-function"
31 #endif
32 G_DEFINE_TYPE (OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION);
33 #ifdef __GNUC__
34 #pragma GCC diagnostic pop
35 #endif
36 
37 static void ooo_mount_operation_ask_password (GMountOperation *op,
38  const char *message, const char *default_user, const char *default_domain,
39  GAskPasswordFlags flags);
40 
42 {
43  op->m_pPrevPassword = nullptr;
44  op->m_pPrevUsername = nullptr;
45 }
46 
47 static void ooo_mount_operation_finalize (GObject *object)
48 {
49  OOoMountOperation *mount_op = OOO_MOUNT_OPERATION (object);
50  if (mount_op->m_pPrevUsername)
51  free(mount_op->m_pPrevUsername);
52  if (mount_op->m_pPrevPassword)
53  free(mount_op->m_pPrevPassword);
54  mount_op->context.reset();
55 
56  G_OBJECT_CLASS (ooo_mount_operation_parent_class)->finalize (object);
57 }
58 
60 {
61  GObjectClass *object_class = G_OBJECT_CLASS (klass);
62  object_class->finalize = ooo_mount_operation_finalize;
63 
64  GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
65  mount_op_class->ask_password = ooo_mount_operation_ask_password;
66 }
67 
68 namespace {
69 
70 // Temporarily undo the g_main_context_push_thread_default done in the surrounding MountOperation
71 // ctor (in ucb/source/ucp/gio/gio_content.cxx):
72 struct GlibThreadDefaultMainContextScope {
73 public:
74  GlibThreadDefaultMainContextScope(GMainContext * context): context_(context)
75  { g_main_context_push_thread_default(context_); }
76 
77  ~GlibThreadDefaultMainContextScope() { g_main_context_pop_thread_default(context_); }
78 
79 private:
80  GMainContext * context_;
81 };
82 
83 }
84 
85 static void ooo_mount_operation_ask_password (GMountOperation *op,
86  const char * /*message*/, const char *default_user,
87  const char *default_domain, GAskPasswordFlags flags)
88 {
89  css::uno::Reference< css::task::XInteractionHandler > xIH;
90 
91  OOoMountOperation *pThis = reinterpret_cast<OOoMountOperation*>(op);
92  GlibThreadDefaultMainContextScope scope(pThis->context.get());
93 
94  const css::uno::Reference< css::ucb::XCommandEnvironment > &xEnv = *(pThis->pEnv);
95 
96  if (xEnv.is())
97  xIH = xEnv->getInteractionHandler();
98 
99  if (!xIH.is())
100  {
101  g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
102  return;
103  }
104 
105  OUString aDomain, aUserName, aPassword;
106 
108  (flags & G_ASK_PASSWORD_NEED_USERNAME)
111 
112  if (default_user)
113  aUserName = OUString(default_user, strlen(default_user), RTL_TEXTENCODING_UTF8);
114 
116  (flags & G_ASK_PASSWORD_NEED_PASSWORD)
119 
120  OUString aPrevPassword, aPrevUsername;
121  if (pThis->m_pPrevUsername)
122  aPrevUsername = OUString(pThis->m_pPrevUsername, strlen(pThis->m_pPrevUsername), RTL_TEXTENCODING_UTF8);
123  if (pThis->m_pPrevPassword)
124  aPrevPassword = OUString(pThis->m_pPrevPassword, strlen(pThis->m_pPrevPassword), RTL_TEXTENCODING_UTF8);
125 
126  //The damn dialog is stupidly broken, so do like webdav, i.e. "#102871#"
127  if ( aUserName.isEmpty() )
128  aUserName = aPrevUsername;
129 
130  if ( aPassword.isEmpty() )
131  aPassword = aPrevPassword;
132 
134  (flags & G_ASK_PASSWORD_NEED_DOMAIN)
137 
138  if (default_domain)
139  aDomain = OUString(default_domain, strlen(default_domain), RTL_TEXTENCODING_UTF8);
140 
142  = new ucbhelper::SimpleAuthenticationRequest (OUString() /* FIXME: provide URL here */, OUString(), eDomain, aDomain, eUserName, aUserName, ePassword, aPassword);
143 
144  xIH->handle( xRequest.get() );
145 
146  rtl::Reference< ucbhelper::InteractionContinuation > xSelection = xRequest->getSelection();
147 
148  if ( !xSelection.is() )
149  {
150  g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
151  return;
152  }
153 
154  css::uno::Reference< css::task::XInteractionAbort > xAbort(xSelection.get(), css::uno::UNO_QUERY );
155  if ( xAbort.is() )
156  {
157  g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
158  return;
159  }
160 
161  const rtl::Reference< ucbhelper::InteractionSupplyAuthentication > & xSupp = xRequest->getAuthenticationSupplier();
162  aUserName = xSupp->getUserName();
163  aPassword = xSupp->getPassword();
164 
165  if (flags & G_ASK_PASSWORD_NEED_USERNAME)
166  g_mount_operation_set_username(op, OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
167 
168  if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
169  g_mount_operation_set_password(op, OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
170 
171  if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
172  g_mount_operation_set_domain(op, OUStringToOString(xSupp->getRealm(), RTL_TEXTENCODING_UTF8).getStr());
173 
174  switch (xSupp->getRememberPasswordMode())
175  {
176  default:
177  case css::ucb::RememberAuthentication_NO:
178  g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_NEVER);
179  break;
180  case css::ucb::RememberAuthentication_SESSION:
181  g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_FOR_SESSION);
182  break;
183  case css::ucb::RememberAuthentication_PERSISTENT:
184  g_mount_operation_set_password_save(op, G_PASSWORD_SAVE_PERMANENTLY);
185  break;
186  }
187 
188  if (pThis->m_pPrevPassword)
189  free(pThis->m_pPrevPassword);
190  pThis->m_pPrevPassword = strdup(OUStringToOString(aPassword, RTL_TEXTENCODING_UTF8).getStr());
191  if (pThis->m_pPrevUsername)
192  free(pThis->m_pPrevUsername);
193  pThis->m_pPrevUsername = strdup(OUStringToOString(aUserName, RTL_TEXTENCODING_UTF8).getStr());
194  g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
195 }
196 
197 GMountOperation *ooo_mount_operation_new(ucb::ucp::gio::glib::MainContextRef && context, const css::uno::Reference< css::ucb::XCommandEnvironment >& rEnv)
198 {
199  OOoMountOperation *pRet = static_cast<OOoMountOperation*>(g_object_new (OOO_TYPE_MOUNT_OPERATION, nullptr));
200  pRet->context = std::move(context);
201  pRet->pEnv = &rEnv;
202  return &pRet->parent_instance;
203 }
204 
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define OOO_MOUNT_OPERATION(o)
Definition: gio_mount.hxx:33
G_DEFINE_TYPE(OOoMountOperation, ooo_mount_operation, G_TYPE_MOUNT_OPERATION)
#define OOO_TYPE_MOUNT_OPERATION
Definition: gio_mount.hxx:32
std::unique_ptr< GMainContext, detail::MainContextUnref > MainContextRef
Definition: gio_mount.hxx:53
GMountOperation * ooo_mount_operation_new(ucb::ucp::gio::glib::MainContextRef &&context, const css::uno::Reference< css::ucb::XCommandEnvironment > &rEnv)
Definition: gio_mount.cxx:197
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
static void ooo_mount_operation_class_init(OOoMountOperationClass *klass)
Definition: gio_mount.cxx:59
char * m_pPrevUsername
Definition: gio_mount.hxx:63
const css::uno::Reference< css::ucb::XCommandEnvironment > * pEnv
Definition: gio_mount.hxx:62
static void ooo_mount_operation_init(OOoMountOperation *op)
Definition: gio_mount.cxx:41
ucb::ucp::gio::glib::MainContextRef context
Definition: gio_mount.hxx:61
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:85
GMountOperation parent_instance
Definition: gio_mount.hxx:59
static void ooo_mount_operation_finalize(GObject *object)
Definition: gio_mount.cxx:47
char * m_pPrevPassword
Definition: gio_mount.hxx:64