LibreOffice Module dbaccess (master) 1
adodatalinks.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
21#if defined(_WIN32)
22// LO/windows.h conflict
23#undef WB_LEFT
24#undef WB_RIGHT
25#include <msdasc.h>
26
29#include <systools/win32/comtools.hxx>
30#include <systools/win32/oleauto.hxx>
31
32#include <initguid.h>
33#include <adoid.h>
34#include <adoint.h>
35
36#include "adodatalinks.hxx"
37
38namespace {
39
40OUString PromptNew(sal_IntPtr hWnd)
41{
42 try
43 {
44 // Initialize COM
45 sal::systools::CoInitializeGuard aGuard(COINIT_APARTMENTTHREADED);
46
47 // Instantiate DataLinks object.
48 sal::systools::COMReference<IDataSourceLocator> dlPrompt;
49 dlPrompt.CoCreateInstance(CLSID_DataLinks, //clsid -- Data Links UI
50 nullptr, //pUnkOuter
51 CLSCTX_INPROC_SERVER); //dwClsContext
52
53 sal::systools::ThrowIfFailed(dlPrompt->put_hWnd(hWnd), "put_hWnd failed");
54
55 // Prompt for connection information.
56 sal::systools::COMReference<IDispatch> piDispatch;
57 sal::systools::ThrowIfFailed(dlPrompt->PromptNew(&piDispatch), "PromptNew failed");
58 sal::systools::COMReference<ADOConnection> piTmpConnection(piDispatch,
59 sal::systools::COM_QUERY_THROW);
60
61 sal::systools::BStr _result;
62 sal::systools::ThrowIfFailed(piTmpConnection->get_ConnectionString(&_result),
63 "get_ConnectionString failed");
64
65 return OUString(_result);
66 }
67 catch (const sal::systools::ComError&)
68 {
69 return OUString();
70 }
71}
72
73OUString PromptEdit(sal_IntPtr hWnd, OUString const & connstr)
74{
75 try
76 {
77 // Initialize COM
78 sal::systools::CoInitializeGuard aGuard(COINIT_APARTMENTTHREADED);
79
80 sal::systools::COMReference<ADOConnection> piTmpConnection;
81 piTmpConnection.CoCreateInstance(CLSID_CADOConnection, nullptr, CLSCTX_INPROC_SERVER);
82
83 sal::systools::ThrowIfFailed(
84 piTmpConnection->put_ConnectionString(sal::systools::BStr(connstr)),
85 "put_ConnectionString failed");
86
87 // Instantiate DataLinks object.
88 sal::systools::COMReference<IDataSourceLocator> dlPrompt;
89 dlPrompt.CoCreateInstance(CLSID_DataLinks, //clsid -- Data Links UI
90 nullptr, //pUnkOuter
91 CLSCTX_INPROC_SERVER); //dwClsContext
92
93 sal::systools::ThrowIfFailed(dlPrompt->put_hWnd(hWnd), "put_hWnd failed");
94
95 try
96 {
97 // Prompt for connection information.
98 IDispatch* piDispatch = piTmpConnection.get();
99 VARIANT_BOOL pbSuccess;
100 sal::systools::ThrowIfFailed(dlPrompt->PromptEdit(&piDispatch, &pbSuccess),
101 "PromptEdit failed");
102 if (!pbSuccess) //if user press cancel then sal_False == pbSuccess
103 return connstr;
104 }
105 catch (const sal::systools::ComError&)
106 {
107 // Prompt for new connection information.
108 sal::systools::COMReference<IDispatch> piDispatch;
109 sal::systools::ThrowIfFailed(dlPrompt->PromptNew(&piDispatch), "PromptNew failed");
110 piTmpConnection.set(piDispatch, sal::systools::COM_QUERY_THROW);
111 }
112
113 sal::systools::BStr _result;
114 sal::systools::ThrowIfFailed(piTmpConnection->get_ConnectionString(&_result),
115 "get_ConnectionString failed");
116
117 return OUString(_result);
118 }
119 catch (const sal::systools::ComError&)
120 {
121 return connstr;
122 }
123}
124
125}
126
127OUString getAdoDatalink(sal_IntPtr hWnd,OUString const & oldLink)
128{
129 OUString dataLink;
130 if (!oldLink.isEmpty())
131 {
132 dataLink=PromptEdit(hWnd,oldLink);
133 }
134 else
135 dataLink=PromptNew(hWnd);
136 return dataLink;
137}
138
139#endif
140
141/* vim:set shiftwidth=4 softtabstop=4 expandtab: */