1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbui.hxx"
26 
27 
28 #if defined(WNT)
29 #if defined _MSC_VER
30 #pragma warning(push, 1)
31 #pragma warning(disable: 4917)
32 #endif
33 #include "msdasc.h"        // OLE DB Service Component header
34 #if defined _MSC_VER
35 #pragma warning(push, 1)
36 #endif
37 #include "stdio.h"
38 
39 #include <initguid.h>  // Include only once in your application
40 #include <adoid.h>     // needed for CLSID_CADOConnection
41 #include <adoint.h>    // needed for ADOConnection
42 
43 #ifndef _DBAUI_ADO_DATALINK_HXX_
44 #include "adodatalinks.hxx"
45 #endif
46 
47 BSTR PromptEdit(long hWnd,BSTR connstr);
48 BSTR PromptNew(long hWnd);
49 
getAdoDatalink(long hWnd,::rtl::OUString & oldLink)50 ::rtl::OUString getAdoDatalink(long hWnd,::rtl::OUString& oldLink)
51 {
52     ::rtl::OUString dataLink;
53 	if (oldLink.getLength())
54 	{
55 		dataLink=reinterpret_cast<sal_Unicode *>(PromptEdit(hWnd,(BSTR)oldLink.getStr()));
56 	}
57 	else
58 		dataLink=reinterpret_cast<sal_Unicode *>(PromptNew(hWnd));
59     return dataLink;
60 }
PromptNew(long hWnd)61 BSTR PromptNew(long hWnd)
62 {
63 	BSTR connstr=NULL;
64     HRESULT hr;
65     IDataSourceLocator* dlPrompt = NULL;
66     ADOConnection* piTmpConnection = NULL;
67     BSTR _result=NULL;
68 
69      // Initialize COM
70      ::CoInitialize( NULL );
71 
72     // Instantiate DataLinks object.
73   	hr = CoCreateInstance(
74 					CLSID_DataLinks,				//clsid -- Data Links UI
75 					NULL,							//pUnkOuter
76 					CLSCTX_INPROC_SERVER,			//dwClsContext
77 					IID_IDataSourceLocator,		//riid
78 					(void**)&dlPrompt	//ppvObj
79 					);
80     if( FAILED( hr ) )
81     {
82         piTmpConnection->Release( );
83         dlPrompt->Release( );
84         return connstr;
85     }
86 
87     dlPrompt->put_hWnd(hWnd);
88     if( FAILED( hr ) )
89     {
90         piTmpConnection->Release( );
91         dlPrompt->Release( );
92         return connstr;
93     }
94 
95     // Prompt for connection information.
96     hr = dlPrompt->PromptNew((IDispatch **)&piTmpConnection);
97 
98     if( FAILED( hr ) || !piTmpConnection )
99     {
100         dlPrompt->Release( );
101         return connstr;
102     }
103 
104     hr = piTmpConnection->get_ConnectionString(&_result);
105     if( FAILED( hr ) )
106     {
107         piTmpConnection->Release( );
108         dlPrompt->Release( );
109         return connstr;
110     }
111 
112     piTmpConnection->Release( );
113     dlPrompt->Release( );
114     CoUninitialize();
115     return _result;
116 }
117 
PromptEdit(long hWnd,BSTR connstr)118 BSTR PromptEdit(long hWnd,BSTR connstr)
119 {
120     HRESULT hr;
121     IDataSourceLocator* dlPrompt = NULL;
122     ADOConnection* piTmpConnection = NULL;
123     BSTR _result=NULL;
124 
125      // Initialize COM
126      ::CoInitialize( NULL );
127 
128      hr = CoCreateInstance(CLSID_CADOConnection,
129                 NULL,
130                 CLSCTX_INPROC_SERVER,
131                 IID_IADOConnection,
132                 (LPVOID *)&piTmpConnection);
133     if( FAILED( hr ) )
134     {
135         piTmpConnection->Release( );
136         return connstr;
137     }
138 
139 
140     hr = piTmpConnection->put_ConnectionString(connstr);
141     if( FAILED( hr ) )
142     {
143         piTmpConnection->Release( );
144         return connstr;
145     }
146 
147     // Instantiate DataLinks object.
148   	hr = CoCreateInstance(
149 					CLSID_DataLinks,				//clsid -- Data Links UI
150 					NULL,							//pUnkOuter
151 					CLSCTX_INPROC_SERVER,			//dwClsContext
152 					IID_IDataSourceLocator,		//riid
153 					(void**)&dlPrompt	//ppvObj
154 					);
155     if( FAILED( hr ) )
156     {
157         piTmpConnection->Release( );
158         dlPrompt->Release( );
159         return connstr;
160     }
161 
162     dlPrompt->put_hWnd(hWnd);
163     if( FAILED( hr ) )
164     {
165         piTmpConnection->Release( );
166         dlPrompt->Release( );
167         return connstr;
168     }
169 
170     VARIANT_BOOL pbSuccess;
171 
172     // Prompt for connection information.
173     hr = dlPrompt->PromptEdit((IDispatch **)&piTmpConnection,&pbSuccess);
174     if( SUCCEEDED( hr ) && sal_False == pbSuccess ) //if user press cancel then sal_False == pbSuccess
175     {
176         piTmpConnection->Release( );
177         dlPrompt->Release( );
178         return connstr;
179     }
180 
181     if( FAILED( hr ) )
182     {
183         // Prompt for new connection information.
184         piTmpConnection->Release( );
185         piTmpConnection = NULL;
186         hr = dlPrompt->PromptNew((IDispatch **)&piTmpConnection);
187         if(  FAILED( hr ) || !piTmpConnection )
188         {
189             dlPrompt->Release( );
190             return connstr;
191         }
192     }
193 
194     hr = piTmpConnection->get_ConnectionString(&_result);
195     if( FAILED( hr ) )
196     {
197         piTmpConnection->Release( );
198         dlPrompt->Release( );
199         return connstr;
200     }
201 
202     piTmpConnection->Release( );
203     dlPrompt->Release( );
204     CoUninitialize();
205     return _result;
206 }
207 #endif
208