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 <globals.hrc>
43 : #include <utlui.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 : Description: edit insert-field
54 : --------------------------------------------------------------------*/
55 0 : SwChangeDBDlg::SwChangeDBDlg(SwView& rVw)
56 0 : : SvxStandardDialog(&rVw.GetViewFrame()->GetWindow(), "ExchangeDatabasesDialog",
57 : "modules/swriter/ui/exchangedatabases.ui")
58 : , aImageList(SW_RES(ILIST_DB_DLG))
59 0 : , pSh(rVw.GetWrtShellPtr())
60 0 : , pMgr( new SwFldMgr() )
61 : {
62 0 : get(m_pUsedDBTLB, "inuselb");
63 0 : get(m_pAvailDBTLB, "availablelb");
64 0 : get(m_pAddDBPB, "browse");
65 0 : get(m_pDocDBNameFT, "dbnameft");
66 0 : get(m_pDefineBT, "define");
67 0 : m_pAvailDBTLB->SetWrtShell(*pSh);
68 0 : FillDBPopup();
69 :
70 0 : ShowDBName(pSh->GetDBData());
71 0 : m_pDefineBT->SetClickHdl(LINK(this, SwChangeDBDlg, ButtonHdl));
72 0 : m_pAddDBPB->SetClickHdl(LINK(this, SwChangeDBDlg, AddDBHdl));
73 :
74 0 : m_pUsedDBTLB->SetSelectionMode(MULTIPLE_SELECTION);
75 0 : m_pUsedDBTLB->SetStyle(m_pUsedDBTLB->GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
76 0 : m_pUsedDBTLB->SetSpaceBetweenEntries(0);
77 0 : m_pUsedDBTLB->SetNodeBitmaps( aImageList.GetImage(IMG_COLLAPSE), aImageList.GetImage(IMG_EXPAND));
78 :
79 0 : Link aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
80 :
81 0 : m_pUsedDBTLB->SetSelectHdl(aLink);
82 0 : m_pUsedDBTLB->SetDeselectHdl(aLink);
83 0 : m_pAvailDBTLB->SetSelectHdl(aLink);
84 0 : m_pAvailDBTLB->SetSelectHdl(aLink);
85 0 : TreeSelectHdl();
86 0 : }
87 :
88 : /*--------------------------------------------------------------------
89 : Description: initialise database listboxes
90 : --------------------------------------------------------------------*/
91 0 : void SwChangeDBDlg::FillDBPopup()
92 : {
93 0 : Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
94 0 : Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
95 :
96 0 : const SwDBData& rDBData = pSh->GetDBData();
97 0 : m_pAvailDBTLB->Select(rDBData.sDataSource, rDBData.sCommand, aEmptyOUStr);
98 :
99 0 : std::vector<OUString> aAllDBNames;
100 :
101 0 : Sequence< OUString > aDBNames = xDBContext->getElementNames();
102 0 : const OUString* pDBNames = aDBNames.getConstArray();
103 0 : sal_Int32 nDBCount = aDBNames.getLength();
104 0 : for(sal_Int32 i = 0; i < nDBCount; i++)
105 : {
106 0 : aAllDBNames.push_back(pDBNames[i]);
107 : }
108 :
109 0 : std::vector<OUString> aDBNameList;
110 0 : pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
111 :
112 0 : size_t nCount = aDBNameList.size();
113 0 : m_pUsedDBTLB->Clear();
114 0 : SvTreeListEntry *pFirst = 0;
115 0 : SvTreeListEntry *pLast = 0;
116 :
117 0 : for(size_t k = 0; k < nCount; k++)
118 : {
119 0 : pLast = Insert(aDBNameList[k].getToken(0, ';'));
120 0 : if (!pFirst)
121 0 : pFirst = pLast;
122 : }
123 :
124 0 : if (pFirst)
125 : {
126 0 : m_pUsedDBTLB->MakeVisible(pFirst);
127 0 : m_pUsedDBTLB->Select(pFirst);
128 0 : }
129 :
130 0 : }
131 :
132 0 : SvTreeListEntry* SwChangeDBDlg::Insert(const OUString& rDBName)
133 : {
134 0 : const OUString sDBName(rDBName.getToken(0, DB_DELIM));
135 0 : const OUString sTableName(rDBName.getToken(1, DB_DELIM));
136 0 : sal_IntPtr nCommandType = rDBName.getToken(2, DB_DELIM).toInt32();
137 : SvTreeListEntry* pParent;
138 : SvTreeListEntry* pChild;
139 :
140 0 : sal_uLong nParent = 0;
141 0 : sal_uLong nChild = 0;
142 :
143 0 : Image aTableImg = aImageList.GetImage(IMG_DBTABLE);
144 0 : Image aDBImg = aImageList.GetImage(IMG_DB);
145 0 : Image aQueryImg = aImageList.GetImage(IMG_DBQUERY);
146 0 : Image& rToInsert = nCommandType ? aQueryImg : aTableImg;
147 0 : while ((pParent = m_pUsedDBTLB->GetEntry(nParent++)) != NULL)
148 : {
149 0 : if (sDBName == m_pUsedDBTLB->GetEntryText(pParent))
150 : {
151 0 : while ((pChild = m_pUsedDBTLB->GetEntry(pParent, nChild++)) != NULL)
152 : {
153 0 : if (sTableName == m_pUsedDBTLB->GetEntryText(pChild))
154 0 : return pChild;
155 : }
156 0 : SvTreeListEntry* pRet = m_pUsedDBTLB->InsertEntry(sTableName, rToInsert, rToInsert, pParent);
157 0 : pRet->SetUserData((void*)nCommandType);
158 0 : return pRet;
159 : }
160 : }
161 0 : pParent = m_pUsedDBTLB->InsertEntry(sDBName, aDBImg, aDBImg);
162 :
163 0 : SvTreeListEntry* pRet = m_pUsedDBTLB->InsertEntry(sTableName, rToInsert, rToInsert, pParent);
164 0 : pRet->SetUserData((void*)nCommandType);
165 0 : return pRet;
166 : }
167 :
168 : /*--------------------------------------------------------------------
169 : Description: destroy dialog
170 : --------------------------------------------------------------------*/
171 0 : SwChangeDBDlg::~SwChangeDBDlg()
172 : {
173 0 : delete pMgr;
174 0 : }
175 :
176 : /*--------------------------------------------------------------------
177 : Description: close
178 : --------------------------------------------------------------------*/
179 0 : void SwChangeDBDlg::Apply()
180 : {
181 0 : UpdateFlds();
182 0 : }
183 :
184 0 : void SwChangeDBDlg::UpdateFlds()
185 : {
186 0 : std::vector<OUString> aDBNames;
187 0 : aDBNames.reserve(m_pUsedDBTLB->GetSelectionCount());
188 0 : SvTreeListEntry* pEntry = m_pUsedDBTLB->FirstSelected();
189 :
190 0 : while( pEntry )
191 : {
192 0 : if( m_pUsedDBTLB->GetParent( pEntry ))
193 : {
194 0 : OUString sTmp(m_pUsedDBTLB->GetEntryText( m_pUsedDBTLB->GetParent( pEntry )) +
195 0 : OUString(DB_DELIM) + m_pUsedDBTLB->GetEntryText( pEntry ) + OUString(DB_DELIM) +
196 0 : OUString::number((int)(sal_uLong)pEntry->GetUserData()));
197 0 : aDBNames.push_back(sTmp);
198 : }
199 0 : pEntry = m_pUsedDBTLB->NextSelected(pEntry);
200 : }
201 :
202 0 : pSh->StartAllAction();
203 0 : OUString sTableName;
204 0 : OUString sColumnName;
205 0 : sal_Bool bIsTable = sal_False;
206 : const OUString sTemp = m_pAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable)
207 0 : + OUString(DB_DELIM)
208 0 : + sTableName
209 0 : + OUString(DB_DELIM)
210 0 : + OUString(static_cast<sal_Unicode>(bIsTable ? '0' : '1'));
211 0 : pSh->ChangeDBFields( aDBNames, sTemp);
212 0 : pSh->EndAllAction();
213 0 : }
214 :
215 0 : IMPL_LINK_NOARG(SwChangeDBDlg, ButtonHdl)
216 : {
217 0 : OUString sTableName;
218 0 : OUString sColumnName;
219 0 : SwDBData aData;
220 0 : sal_Bool bIsTable = sal_False;
221 0 : aData.sDataSource = m_pAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable);
222 0 : aData.sCommand = sTableName;
223 0 : aData.nCommandType = bIsTable ? 0 : 1;
224 0 : pSh->ChgDBData(aData);
225 0 : ShowDBName(pSh->GetDBData());
226 0 : EndDialog(RET_OK);
227 :
228 0 : return 0;
229 : }
230 :
231 0 : IMPL_LINK_NOARG(SwChangeDBDlg, TreeSelectHdl)
232 : {
233 0 : sal_Bool bEnable = sal_False;
234 :
235 0 : SvTreeListEntry* pEntry = m_pAvailDBTLB->GetCurEntry();
236 :
237 0 : if (pEntry)
238 : {
239 0 : if (m_pAvailDBTLB->GetParent(pEntry))
240 0 : bEnable = sal_True;
241 0 : m_pDefineBT->Enable( bEnable );
242 : }
243 0 : return 0;
244 : }
245 :
246 : /*--------------------------------------------------------------------
247 : Description: convert database name for display
248 : --------------------------------------------------------------------*/
249 0 : void SwChangeDBDlg::ShowDBName(const SwDBData& rDBData)
250 : {
251 0 : if (rDBData.sDataSource.isEmpty() && rDBData.sCommand.isEmpty())
252 : {
253 0 : m_pDocDBNameFT->SetText(SW_RESSTR(SW_STR_NONE));
254 : }
255 : else
256 : {
257 0 : const OUString sName(rDBData.sDataSource + "." + rDBData.sCommand);
258 0 : m_pDocDBNameFT->SetText(sName.replaceAll("~", "~~"));
259 : }
260 0 : }
261 :
262 0 : IMPL_LINK_NOARG(SwChangeDBDlg, AddDBHdl)
263 : {
264 0 : const OUString sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
265 0 : if (!sNewDB.isEmpty())
266 0 : m_pAvailDBTLB->AddDataSource(sNewDB);
267 0 : return 0;
268 : }
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|