Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include <svtools/stdctrl.hxx>
30 : : #include <vcl/msgbox.hxx>
31 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 : : #include <com/sun/star/container/XNameAccess.hpp>
33 : : #include <com/sun/star/sdb/XDatabaseAccess.hpp>
34 : : #include <comphelper/processfactory.hxx>
35 : : #include <sfx2/viewfrm.hxx>
36 : :
37 : : #include <view.hxx>
38 : : #include <wrtsh.hxx>
39 : : #include <dbmgr.hxx>
40 : : #include <fldmgr.hxx>
41 : : #include <expfld.hxx>
42 : : #include <txtatr.hxx>
43 : : #include <ndtxt.hxx>
44 : : #include <fldbas.hxx>
45 : : #include <dbfld.hxx>
46 : : #include <changedb.hxx>
47 : :
48 : : #include <fldui.hrc>
49 : : #include <utlui.hrc>
50 : : #include <changedb.hrc>
51 : :
52 : : #include <unomid.h>
53 : :
54 : : using namespace ::com::sun::star::uno;
55 : : using namespace ::com::sun::star::container;
56 : : using namespace ::com::sun::star::lang;
57 : :
58 : :
59 : : /*--------------------------------------------------------------------
60 : : Description: edit insert-field
61 : : --------------------------------------------------------------------*/
62 : 0 : SwChangeDBDlg::SwChangeDBDlg(SwView& rVw) :
63 : 0 : SvxStandardDialog(&rVw.GetViewFrame()->GetWindow(), SW_RES(DLG_CHANGE_DB)),
64 : :
65 : : aDBListFL (this, SW_RES(FL_DBLIST )),
66 : : aUsedDBFT (this, SW_RES(FT_USEDDB )),
67 : : aAvailDBFT (this, SW_RES(FT_AVAILDB )),
68 : : aUsedDBTLB (this, SW_RES(TLB_USEDDB )),
69 : : aAvailDBTLB (this, SW_RES(TLB_AVAILDB ), 0),
70 : : aAddDBPB (this, SW_RES(PB_ADDDB)),
71 : : aDescFT (this, SW_RES(FT_DESC )),
72 : : aDocDBTextFT(this, SW_RES(FT_DOCDBTEXT )),
73 : : aDocDBNameFT(this, SW_RES(FT_DOCDBNAME )),
74 : : aOKBT (this, SW_RES(BT_OK )),
75 : : aCancelBT (this, SW_RES(BT_CANCEL )),
76 : : aHelpBT (this, SW_RES(BT_HELP )),
77 : : aImageList (SW_RES(ILIST_DB_DLG )),
78 : 0 : pSh(rVw.GetWrtShellPtr()),
79 : 0 : pMgr( new SwFldMgr() )
80 : : {
81 : 0 : aAvailDBTLB.SetWrtShell(*pSh);
82 : 0 : FillDBPopup();
83 : :
84 : 0 : FreeResource();
85 : :
86 : 0 : ShowDBName(pSh->GetDBData());
87 : 0 : aOKBT.SetClickHdl(LINK(this, SwChangeDBDlg, ButtonHdl));
88 : 0 : aAddDBPB.SetClickHdl(LINK(this, SwChangeDBDlg, AddDBHdl));
89 : :
90 : 0 : aUsedDBTLB.SetSelectionMode(MULTIPLE_SELECTION);
91 : 0 : aUsedDBTLB.SetStyle(aUsedDBTLB.GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
92 : 0 : aUsedDBTLB.SetSpaceBetweenEntries(0);
93 : 0 : aUsedDBTLB.SetNodeBitmaps( aImageList.GetImage(IMG_COLLAPSE), aImageList.GetImage(IMG_EXPAND));
94 : :
95 : 0 : Link aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
96 : :
97 : 0 : aUsedDBTLB.SetSelectHdl(aLink);
98 : 0 : aUsedDBTLB.SetDeselectHdl(aLink);
99 : 0 : aAvailDBTLB.SetSelectHdl(aLink);
100 : 0 : aAvailDBTLB.SetDeselectHdl(aLink);
101 : 0 : TreeSelectHdl();
102 : 0 : }
103 : :
104 : : /*--------------------------------------------------------------------
105 : : Description: initialise database listboxes
106 : : --------------------------------------------------------------------*/
107 : 0 : void SwChangeDBDlg::FillDBPopup()
108 : : {
109 : 0 : Reference<XNameAccess> xDBContext;
110 : 0 : Reference< XMultiServiceFactory > xMgr( ::comphelper::getProcessServiceFactory() );
111 : 0 : if( xMgr.is() )
112 : : {
113 : 0 : Reference<XInterface> xInstance = xMgr->createInstance( C2U( "com.sun.star.sdb.DatabaseContext" ));
114 : 0 : xDBContext = Reference<XNameAccess>(xInstance, UNO_QUERY) ;
115 : : }
116 : : OSL_ENSURE(xDBContext.is(), "com.sun.star.sdb.DataBaseContext: service not available");
117 : :
118 : 0 : const SwDBData& rDBData = pSh->GetDBData();
119 : 0 : String sDBName(rDBData.sDataSource);
120 : 0 : String sTableName(rDBData.sCommand);
121 : 0 : aAvailDBTLB.Select(sDBName, sTableName, aEmptyStr);
122 : :
123 : 0 : std::vector<String> aAllDBNames;
124 : :
125 : 0 : Sequence< ::rtl::OUString > aDBNames = xDBContext->getElementNames();
126 : 0 : const ::rtl::OUString* pDBNames = aDBNames.getConstArray();
127 : 0 : sal_Int32 nDBCount = aDBNames.getLength();
128 : 0 : for(sal_Int32 i = 0; i < nDBCount; i++)
129 : : {
130 : 0 : aAllDBNames.push_back(pDBNames[i]);
131 : : }
132 : :
133 : 0 : std::vector<String> aDBNameList;
134 : 0 : pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
135 : :
136 : 0 : size_t nCount = aDBNameList.size();
137 : 0 : aUsedDBTLB.Clear();
138 : 0 : SvLBoxEntry *pFirst = 0;
139 : 0 : SvLBoxEntry *pLast = 0;
140 : :
141 : 0 : for(size_t k = 0; k < nCount; k++)
142 : : {
143 : 0 : sDBName = aDBNameList[k];
144 : 0 : sDBName = sDBName.GetToken(0);
145 : 0 : pLast = Insert(sDBName);
146 : 0 : if (!pFirst)
147 : 0 : pFirst = pLast;
148 : : }
149 : :
150 : 0 : if (pFirst)
151 : : {
152 : 0 : aUsedDBTLB.MakeVisible(pFirst);
153 : 0 : aUsedDBTLB.Select(pFirst);
154 : 0 : }
155 : :
156 : 0 : }
157 : :
158 : 0 : SvLBoxEntry* SwChangeDBDlg::Insert(const String& rDBName)
159 : : {
160 : 0 : String sDBName(rDBName.GetToken(0, DB_DELIM));
161 : 0 : String sTableName(rDBName.GetToken(1, DB_DELIM));
162 : 0 : sal_IntPtr nCommandType = rDBName.GetToken(2, DB_DELIM).ToInt32();
163 : : SvLBoxEntry* pParent;
164 : : SvLBoxEntry* pChild;
165 : :
166 : 0 : sal_uInt16 nParent = 0;
167 : 0 : sal_uInt16 nChild = 0;
168 : :
169 : 0 : Image aTableImg = aImageList.GetImage(IMG_DBTABLE);
170 : 0 : Image aDBImg = aImageList.GetImage(IMG_DB);
171 : 0 : Image aQueryImg = aImageList.GetImage(IMG_DBQUERY);
172 : 0 : Image& rToInsert = nCommandType ? aQueryImg : aTableImg;
173 : 0 : while ((pParent = aUsedDBTLB.GetEntry(nParent++)) != NULL)
174 : : {
175 : 0 : if (sDBName == aUsedDBTLB.GetEntryText(pParent))
176 : : {
177 : 0 : while ((pChild = aUsedDBTLB.GetEntry(pParent, nChild++)) != NULL)
178 : : {
179 : 0 : if (sTableName == aUsedDBTLB.GetEntryText(pChild))
180 : 0 : return pChild;
181 : : }
182 : 0 : SvLBoxEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
183 : 0 : pRet->SetUserData((void*)nCommandType);
184 : 0 : return pRet;
185 : : }
186 : : }
187 : 0 : pParent = aUsedDBTLB.InsertEntry(sDBName, aDBImg, aDBImg);
188 : :
189 : 0 : SvLBoxEntry* pRet = aUsedDBTLB.InsertEntry(sTableName, rToInsert, rToInsert, pParent);
190 : 0 : pRet->SetUserData((void*)nCommandType);
191 : 0 : return pRet;
192 : : }
193 : :
194 : : /*--------------------------------------------------------------------
195 : : Description: destroy dialog
196 : : --------------------------------------------------------------------*/
197 : 0 : SwChangeDBDlg::~SwChangeDBDlg()
198 : : {
199 : 0 : delete pMgr;
200 : 0 : }
201 : :
202 : : /*--------------------------------------------------------------------
203 : : Description: close
204 : : --------------------------------------------------------------------*/
205 : 0 : void SwChangeDBDlg::Apply()
206 : : {
207 : 0 : UpdateFlds();
208 : 0 : }
209 : :
210 : 0 : void SwChangeDBDlg::UpdateFlds()
211 : : {
212 : 0 : std::vector<String> aDBNames;
213 : 0 : aDBNames.reserve(aUsedDBTLB.GetSelectionCount());
214 : 0 : SvLBoxEntry* pEntry = aUsedDBTLB.FirstSelected();
215 : :
216 : 0 : while( pEntry )
217 : : {
218 : 0 : if( aUsedDBTLB.GetParent( pEntry ))
219 : : {
220 : : String* pTmp = new String( aUsedDBTLB.GetEntryText(
221 : 0 : aUsedDBTLB.GetParent( pEntry )));
222 : 0 : *pTmp += DB_DELIM;
223 : 0 : *pTmp += aUsedDBTLB.GetEntryText( pEntry );
224 : 0 : *pTmp += DB_DELIM;
225 : 0 : int nCommandType = (int)(sal_uLong)pEntry->GetUserData();
226 : 0 : *pTmp += String::CreateFromInt32(nCommandType);
227 : 0 : aDBNames.push_back(*pTmp);
228 : : }
229 : 0 : pEntry = aUsedDBTLB.NextSelected(pEntry);
230 : : }
231 : :
232 : 0 : pSh->StartAllAction();
233 : 0 : String sTableName, sColumnName;
234 : 0 : sal_Bool bIsTable = sal_False;
235 : 0 : String sTemp(aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable));
236 : 0 : sTemp += DB_DELIM;
237 : 0 : sTemp += sTableName;
238 : 0 : sTemp += DB_DELIM;
239 : 0 : sTemp += bIsTable ? '0' : '1';
240 : 0 : pSh->ChangeDBFields( aDBNames, sTemp);
241 : 0 : pSh->EndAllAction();
242 : 0 : }
243 : :
244 : 0 : IMPL_LINK_NOARG(SwChangeDBDlg, ButtonHdl)
245 : : {
246 : 0 : String sTableName, sColumnName;
247 : 0 : SwDBData aData;
248 : 0 : sal_Bool bIsTable = sal_False;
249 : 0 : aData.sDataSource = aAvailDBTLB.GetDBName(sTableName, sColumnName, &bIsTable);
250 : 0 : aData.sCommand = sTableName;
251 : 0 : aData.nCommandType = bIsTable ? 0 : 1;
252 : 0 : pSh->ChgDBData(aData);
253 : 0 : ShowDBName(pSh->GetDBData());
254 : 0 : EndDialog(RET_OK);
255 : :
256 : 0 : return 0;
257 : : }
258 : :
259 : 0 : IMPL_LINK_NOARG(SwChangeDBDlg, TreeSelectHdl)
260 : : {
261 : 0 : sal_Bool bEnable = sal_False;
262 : :
263 : 0 : SvLBoxEntry* pEntry = aAvailDBTLB.GetCurEntry();
264 : :
265 : 0 : if (pEntry)
266 : : {
267 : 0 : if (aAvailDBTLB.GetParent(pEntry))
268 : 0 : bEnable = sal_True;
269 : 0 : aOKBT.Enable( bEnable );
270 : : }
271 : 0 : return 0;
272 : : }
273 : :
274 : : /*--------------------------------------------------------------------
275 : : Description: convert database name for display
276 : : --------------------------------------------------------------------*/
277 : 0 : void SwChangeDBDlg::ShowDBName(const SwDBData& rDBData)
278 : : {
279 : 0 : String sTmp(rDBData.sDataSource);
280 : 0 : String sName;
281 : 0 : sTmp += '.';
282 : 0 : sTmp += (String)rDBData.sCommand;
283 : :
284 : 0 : for (sal_uInt16 i = 0; i < sTmp.Len(); i++)
285 : : {
286 : 0 : sName += sTmp.GetChar(i);
287 : 0 : if (sTmp.GetChar(i) == '~')
288 : 0 : sName += '~';
289 : : }
290 : :
291 : 0 : aDocDBNameFT.SetText(sName);
292 : 0 : }
293 : :
294 : 0 : IMPL_LINK_NOARG(SwChangeDBDlg, AddDBHdl)
295 : : {
296 : 0 : String sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
297 : 0 : if(sNewDB.Len())
298 : 0 : aAvailDBTLB.AddDataSource(sNewDB);
299 : 0 : return 0;
300 : : }
301 : :
302 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|