Line data Source code
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 <svtools/stdctrl.hxx>
21 : #include <vcl/msgbox.hxx>
22 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 : #include <com/sun/star/container/XNameAccess.hpp>
24 : #include <com/sun/star/sdb/DatabaseContext.hpp>
25 : #include <com/sun/star/sdb/XDatabaseAccess.hpp>
26 : #include <comphelper/processfactory.hxx>
27 : #include <sfx2/viewfrm.hxx>
28 : #include "svtools/treelistentry.hxx"
29 :
30 : #include <view.hxx>
31 : #include <wrtsh.hxx>
32 : #include <dbmgr.hxx>
33 : #include <fldmgr.hxx>
34 : #include <expfld.hxx>
35 : #include <txtatr.hxx>
36 : #include <ndtxt.hxx>
37 : #include <fldbas.hxx>
38 : #include <dbfld.hxx>
39 : #include <changedb.hxx>
40 :
41 : #include <fldui.hrc>
42 : #include <utlui.hrc>
43 : #include <changedb.hrc>
44 :
45 : #include <unomid.h>
46 :
47 : using namespace ::com::sun::star::container;
48 : using namespace ::com::sun::star::lang;
49 : using namespace ::com::sun::star::sdb;
50 : using namespace ::com::sun::star::uno;
51 :
52 :
53 : /*--------------------------------------------------------------------
54 : Description: edit insert-field
55 : --------------------------------------------------------------------*/
56 0 : SwChangeDBDlg::SwChangeDBDlg(SwView& rVw) :
57 0 : SvxStandardDialog(&rVw.GetViewFrame()->GetWindow(), SW_RES(DLG_CHANGE_DB)),
58 :
59 : aDBListFL (this, SW_RES(FL_DBLIST )),
60 : aUsedDBFT (this, SW_RES(FT_USEDDB )),
61 : aAvailDBFT (this, SW_RES(FT_AVAILDB )),
62 : aUsedDBTLB (this, SW_RES(TLB_USEDDB )),
63 : aAvailDBTLB (this, SW_RES(TLB_AVAILDB ), 0),
64 : aAddDBPB (this, SW_RES(PB_ADDDB)),
65 : aDescFT (this, SW_RES(FT_DESC )),
66 : aDocDBTextFT(this, SW_RES(FT_DOCDBTEXT )),
67 : aDocDBNameFT(this, SW_RES(FT_DOCDBNAME )),
68 : aOKBT (this, SW_RES(BT_OK )),
69 : aCancelBT (this, SW_RES(BT_CANCEL )),
70 : aHelpBT (this, SW_RES(BT_HELP )),
71 : aImageList (SW_RES(ILIST_DB_DLG )),
72 0 : pSh(rVw.GetWrtShellPtr()),
73 0 : pMgr( new SwFldMgr() )
74 : {
75 0 : aAvailDBTLB.SetWrtShell(*pSh);
76 0 : FillDBPopup();
77 :
78 0 : FreeResource();
79 :
80 0 : ShowDBName(pSh->GetDBData());
81 0 : aOKBT.SetClickHdl(LINK(this, SwChangeDBDlg, ButtonHdl));
82 0 : aAddDBPB.SetClickHdl(LINK(this, SwChangeDBDlg, AddDBHdl));
83 :
84 0 : aUsedDBTLB.SetSelectionMode(MULTIPLE_SELECTION);
85 0 : aUsedDBTLB.SetStyle(aUsedDBTLB.GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
86 0 : aUsedDBTLB.SetSpaceBetweenEntries(0);
87 0 : aUsedDBTLB.SetNodeBitmaps( aImageList.GetImage(IMG_COLLAPSE), aImageList.GetImage(IMG_EXPAND));
88 :
89 0 : Link aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
90 :
91 0 : aUsedDBTLB.SetSelectHdl(aLink);
92 0 : aUsedDBTLB.SetDeselectHdl(aLink);
93 0 : aAvailDBTLB.SetSelectHdl(aLink);
94 0 : aAvailDBTLB.SetDeselectHdl(aLink);
95 0 : TreeSelectHdl();
96 0 : }
97 :
98 : /*--------------------------------------------------------------------
99 : Description: initialise database listboxes
100 : --------------------------------------------------------------------*/
101 0 : void SwChangeDBDlg::FillDBPopup()
102 : {
103 0 : Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
104 0 : Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
105 :
106 0 : const SwDBData& rDBData = pSh->GetDBData();
107 0 : String sDBName(rDBData.sDataSource);
108 0 : String sTableName(rDBData.sCommand);
109 0 : aAvailDBTLB.Select(sDBName, sTableName, aEmptyStr);
110 :
111 0 : std::vector<String> aAllDBNames;
112 :
113 0 : Sequence< ::rtl::OUString > aDBNames = xDBContext->getElementNames();
114 0 : const ::rtl::OUString* pDBNames = aDBNames.getConstArray();
115 0 : sal_Int32 nDBCount = aDBNames.getLength();
116 0 : for(sal_Int32 i = 0; i < nDBCount; i++)
117 : {
118 0 : aAllDBNames.push_back(pDBNames[i]);
119 : }
120 :
121 0 : std::vector<String> aDBNameList;
122 0 : pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
123 :
124 0 : size_t nCount = aDBNameList.size();
125 0 : aUsedDBTLB.Clear();
126 0 : SvTreeListEntry *pFirst = 0;
127 0 : SvTreeListEntry *pLast = 0;
128 :
129 0 : for(size_t k = 0; k < nCount; k++)
130 : {
131 0 : sDBName = aDBNameList[k];
132 0 : sDBName = sDBName.GetToken(0);
133 0 : pLast = Insert(sDBName);
134 0 : if (!pFirst)
135 0 : pFirst = pLast;
136 : }
137 :
138 0 : if (pFirst)
139 : {
140 0 : aUsedDBTLB.MakeVisible(pFirst);
141 0 : aUsedDBTLB.Select(pFirst);
142 0 : }
143 :
144 0 : }
145 :
146 0 : SvTreeListEntry* SwChangeDBDlg::Insert(const String& rDBName)
147 : {
148 0 : String sDBName(rDBName.GetToken(0, DB_DELIM));
149 0 : String sTableName(rDBName.GetToken(1, DB_DELIM));
150 0 : sal_IntPtr nCommandType = rDBName.GetToken(2, DB_DELIM).ToInt32();
151 : SvTreeListEntry* pParent;
152 : SvTreeListEntry* pChild;
153 :
154 0 : sal_uInt16 nParent = 0;
155 0 : sal_uInt16 nChild = 0;
156 :
157 0 : Image aTableImg = aImageList.GetImage(IMG_DBTABLE);
158 0 : Image aDBImg = aImageList.GetImage(IMG_DB);
159 0 : Image aQueryImg = aImageList.GetImage(IMG_DBQUERY);
160 0 : Image& rToInsert = nCommandType ? aQueryImg : aTableImg;
161 0 : while ((pParent = aUsedDBTLB.GetEntry(nParent++)) != NULL)
162 : {
163 0 : if (sDBName == aUsedDBTLB.GetEntryText(pParent))
164 : {
165 0 : while ((pChild = aUsedDBTLB.GetEntry(pParent, nChild++)) != NULL)
166 : {
167 0 : if (sTableName == aUsedDBTLB.GetEntryText(pChild))
168 0 : return pChild;
169 : }
170 0 : SvTreeListEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
171 0 : pRet->SetUserData((void*)nCommandType);
172 0 : return pRet;
173 : }
174 : }
175 0 : pParent = aUsedDBTLB.InsertEntry(sDBName, aDBImg, aDBImg);
176 :
177 0 : SvTreeListEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
178 0 : pRet->SetUserData((void*)nCommandType);
179 0 : return pRet;
180 : }
181 :
182 : /*--------------------------------------------------------------------
183 : Description: destroy dialog
184 : --------------------------------------------------------------------*/
185 0 : SwChangeDBDlg::~SwChangeDBDlg()
186 : {
187 0 : delete pMgr;
188 0 : }
189 :
190 : /*--------------------------------------------------------------------
191 : Description: close
192 : --------------------------------------------------------------------*/
193 0 : void SwChangeDBDlg::Apply()
194 : {
195 0 : UpdateFlds();
196 0 : }
197 :
198 0 : void SwChangeDBDlg::UpdateFlds()
199 : {
200 0 : std::vector<String> aDBNames;
201 0 : aDBNames.reserve(aUsedDBTLB.GetSelectionCount());
202 0 : SvTreeListEntry* pEntry = aUsedDBTLB.FirstSelected();
203 :
204 0 : while( pEntry )
205 : {
206 0 : if( aUsedDBTLB.GetParent( pEntry ))
207 : {
208 : String* pTmp = new String( aUsedDBTLB.GetEntryText(
209 0 : aUsedDBTLB.GetParent( pEntry )));
210 0 : *pTmp += DB_DELIM;
211 0 : *pTmp += aUsedDBTLB.GetEntryText( pEntry );
212 0 : *pTmp += DB_DELIM;
213 0 : int nCommandType = (int)(sal_uLong)pEntry->GetUserData();
214 0 : *pTmp += String::CreateFromInt32(nCommandType);
215 0 : aDBNames.push_back(*pTmp);
216 : }
217 0 : pEntry = aUsedDBTLB.NextSelected(pEntry);
218 : }
219 :
220 0 : pSh->StartAllAction();
221 0 : String sTableName, sColumnName;
222 0 : sal_Bool bIsTable = sal_False;
223 0 : String sTemp(aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable));
224 0 : sTemp += DB_DELIM;
225 0 : sTemp += sTableName;
226 0 : sTemp += DB_DELIM;
227 0 : sTemp += bIsTable ? '0' : '1';
228 0 : pSh->ChangeDBFields( aDBNames, sTemp);
229 0 : pSh->EndAllAction();
230 0 : }
231 :
232 0 : IMPL_LINK_NOARG(SwChangeDBDlg, ButtonHdl)
233 : {
234 0 : String sTableName, sColumnName;
235 0 : SwDBData aData;
236 0 : sal_Bool bIsTable = sal_False;
237 0 : aData.sDataSource = aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable);
238 0 : aData.sCommand = sTableName;
239 0 : aData.nCommandType = bIsTable ? 0 : 1;
240 0 : pSh->ChgDBData(aData);
241 0 : ShowDBName(pSh->GetDBData());
242 0 : EndDialog(RET_OK);
243 :
244 0 : return 0;
245 : }
246 :
247 0 : IMPL_LINK_NOARG(SwChangeDBDlg, TreeSelectHdl)
248 : {
249 0 : sal_Bool bEnable = sal_False;
250 :
251 0 : SvTreeListEntry* pEntry = aAvailDBTLB.GetCurEntry();
252 :
253 0 : if (pEntry)
254 : {
255 0 : if (aAvailDBTLB.GetParent(pEntry))
256 0 : bEnable = sal_True;
257 0 : aOKBT.Enable( bEnable );
258 : }
259 0 : return 0;
260 : }
261 :
262 : /*--------------------------------------------------------------------
263 : Description: convert database name for display
264 : --------------------------------------------------------------------*/
265 0 : void SwChangeDBDlg::ShowDBName(const SwDBData& rDBData)
266 : {
267 0 : String sTmp(rDBData.sDataSource);
268 0 : String sName;
269 0 : sTmp += '.';
270 0 : sTmp += (String)rDBData.sCommand;
271 :
272 0 : for (sal_uInt16 i = 0; i < sTmp.Len(); i++)
273 : {
274 0 : sName += sTmp.GetChar(i);
275 0 : if (sTmp.GetChar(i) == '~')
276 0 : sName += '~';
277 : }
278 :
279 0 : aDocDBNameFT.SetText(sName);
280 0 : }
281 :
282 0 : IMPL_LINK_NOARG(SwChangeDBDlg, AddDBHdl)
283 : {
284 0 : String sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
285 0 : if(sNewDB.Len())
286 0 : aAvailDBTLB.AddDataSource(sNewDB);
287 0 : return 0;
288 : }
289 :
290 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|