Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / dbaccess / source / ui / dlg / adodatalinks.cxx
blob82af63688cc1a6cfa33b6bf63b25a7415ee41515
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 .
21 #if defined(_WIN32)
22 // LO/windows.h conflict
23 #undef WB_LEFT
24 #undef WB_RIGHT
25 #include <msdasc.h>
27 #include <comphelper/scopeguard.hxx>
28 #include <o3tl/char16_t2wchar_t.hxx>
29 #include <systools/win32/comtools.hxx>
30 #include <systools/win32/oleauto.hxx>
32 #include <initguid.h>
33 #include <adoid.h>
34 #include <adoint.h>
36 #include "adodatalinks.hxx"
38 namespace {
40 OUString PromptNew(sal_IntPtr hWnd)
42 try
44 // Initialize COM
45 sal::systools::CoInitializeGuard aGuard(COINIT_APARTMENTTHREADED);
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
53 sal::systools::ThrowIfFailed(dlPrompt->put_hWnd(hWnd), "put_hWnd failed");
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);
61 sal::systools::BStr _result;
62 sal::systools::ThrowIfFailed(piTmpConnection->get_ConnectionString(&_result),
63 "get_ConnectionString failed");
65 return OUString(_result);
67 catch (const sal::systools::ComError&)
69 return OUString();
73 OUString PromptEdit(sal_IntPtr hWnd, OUString const & connstr)
75 try
77 // Initialize COM
78 sal::systools::CoInitializeGuard aGuard(COINIT_APARTMENTTHREADED);
80 sal::systools::COMReference<ADOConnection> piTmpConnection;
81 piTmpConnection.CoCreateInstance(CLSID_CADOConnection, nullptr, CLSCTX_INPROC_SERVER);
83 sal::systools::ThrowIfFailed(
84 piTmpConnection->put_ConnectionString(sal::systools::BStr(connstr)),
85 "put_ConnectionString failed");
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
93 sal::systools::ThrowIfFailed(dlPrompt->put_hWnd(hWnd), "put_hWnd failed");
95 try
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;
105 catch (const sal::systools::ComError&)
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);
113 sal::systools::BStr _result;
114 sal::systools::ThrowIfFailed(piTmpConnection->get_ConnectionString(&_result),
115 "get_ConnectionString failed");
117 return OUString(_result);
119 catch (const sal::systools::ComError&)
121 return connstr;
127 OUString getAdoDatalink(sal_IntPtr hWnd,OUString const & oldLink)
129 OUString dataLink;
130 if (!oldLink.isEmpty())
132 dataLink=PromptEdit(hWnd,oldLink);
134 else
135 dataLink=PromptNew(hWnd);
136 return dataLink;
139 #endif
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */