bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / fldui / changedb.cxx
blobbed40d1b8c6f408b860d5299b0703a9f7b0759e8
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 .
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"
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>
41 #include <fldui.hrc>
42 #include <globals.hrc>
43 #include <utlui.hrc>
45 #include <unomid.h>
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;
53 /*--------------------------------------------------------------------
54 Description: edit insert-field
55 --------------------------------------------------------------------*/
56 SwChangeDBDlg::SwChangeDBDlg(SwView& rVw)
57 : SvxStandardDialog(&rVw.GetViewFrame()->GetWindow(), "ExchangeDatabasesDialog",
58 "modules/swriter/ui/exchangedatabases.ui")
59 , aImageList(SW_RES(ILIST_DB_DLG))
60 , pSh(rVw.GetWrtShellPtr())
61 , pMgr( new SwFldMgr() )
63 get(m_pUsedDBTLB, "inuselb");
64 get(m_pAvailDBTLB, "availablelb");
65 get(m_pAddDBPB, "browse");
66 get(m_pDocDBNameFT, "dbnameft");
67 get(m_pDefineBT, "define");
68 m_pAvailDBTLB->SetWrtShell(*pSh);
69 FillDBPopup();
71 ShowDBName(pSh->GetDBData());
72 m_pDefineBT->SetClickHdl(LINK(this, SwChangeDBDlg, ButtonHdl));
73 m_pAddDBPB->SetClickHdl(LINK(this, SwChangeDBDlg, AddDBHdl));
75 m_pUsedDBTLB->SetSelectionMode(MULTIPLE_SELECTION);
76 m_pUsedDBTLB->SetStyle(m_pUsedDBTLB->GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
77 m_pUsedDBTLB->SetSpaceBetweenEntries(0);
78 m_pUsedDBTLB->SetNodeBitmaps( aImageList.GetImage(IMG_COLLAPSE), aImageList.GetImage(IMG_EXPAND));
80 Link aLink = LINK(this, SwChangeDBDlg, TreeSelectHdl);
82 m_pUsedDBTLB->SetSelectHdl(aLink);
83 m_pUsedDBTLB->SetDeselectHdl(aLink);
84 m_pAvailDBTLB->SetSelectHdl(aLink);
85 m_pAvailDBTLB->SetSelectHdl(aLink);
86 TreeSelectHdl();
89 /*--------------------------------------------------------------------
90 Description: initialise database listboxes
91 --------------------------------------------------------------------*/
92 void SwChangeDBDlg::FillDBPopup()
94 Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
95 Reference<XDatabaseContext> xDBContext = DatabaseContext::create(xContext);
97 const SwDBData& rDBData = pSh->GetDBData();
98 String sDBName(rDBData.sDataSource);
99 String sTableName(rDBData.sCommand);
100 m_pAvailDBTLB->Select(sDBName, sTableName, aEmptyStr);
102 std::vector<String> aAllDBNames;
104 Sequence< OUString > aDBNames = xDBContext->getElementNames();
105 const OUString* pDBNames = aDBNames.getConstArray();
106 sal_Int32 nDBCount = aDBNames.getLength();
107 for(sal_Int32 i = 0; i < nDBCount; i++)
109 aAllDBNames.push_back(pDBNames[i]);
112 std::vector<String> aDBNameList;
113 pSh->GetAllUsedDB( aDBNameList, &aAllDBNames );
115 size_t nCount = aDBNameList.size();
116 m_pUsedDBTLB->Clear();
117 SvTreeListEntry *pFirst = 0;
118 SvTreeListEntry *pLast = 0;
120 for(size_t k = 0; k < nCount; k++)
122 sDBName = aDBNameList[k];
123 sDBName = sDBName.GetToken(0);
124 pLast = Insert(sDBName);
125 if (!pFirst)
126 pFirst = pLast;
129 if (pFirst)
131 m_pUsedDBTLB->MakeVisible(pFirst);
132 m_pUsedDBTLB->Select(pFirst);
137 SvTreeListEntry* SwChangeDBDlg::Insert(const String& rDBName)
139 String sDBName(rDBName.GetToken(0, DB_DELIM));
140 String sTableName(rDBName.GetToken(1, DB_DELIM));
141 sal_IntPtr nCommandType = rDBName.GetToken(2, DB_DELIM).ToInt32();
142 SvTreeListEntry* pParent;
143 SvTreeListEntry* pChild;
145 sal_uInt16 nParent = 0;
146 sal_uInt16 nChild = 0;
148 Image aTableImg = aImageList.GetImage(IMG_DBTABLE);
149 Image aDBImg = aImageList.GetImage(IMG_DB);
150 Image aQueryImg = aImageList.GetImage(IMG_DBQUERY);
151 Image& rToInsert = nCommandType ? aQueryImg : aTableImg;
152 while ((pParent = m_pUsedDBTLB->GetEntry(nParent++)) != NULL)
154 if (sDBName == m_pUsedDBTLB->GetEntryText(pParent))
156 while ((pChild = m_pUsedDBTLB->GetEntry(pParent, nChild++)) != NULL)
158 if (sTableName == m_pUsedDBTLB->GetEntryText(pChild))
159 return pChild;
161 SvTreeListEntry* pRet = m_pUsedDBTLB->InsertEntry(sTableName, rToInsert, rToInsert, pParent);
162 pRet->SetUserData((void*)nCommandType);
163 return pRet;
166 pParent = m_pUsedDBTLB->InsertEntry(sDBName, aDBImg, aDBImg);
168 SvTreeListEntry* pRet = m_pUsedDBTLB->InsertEntry(sTableName, rToInsert, rToInsert, pParent);
169 pRet->SetUserData((void*)nCommandType);
170 return pRet;
173 /*--------------------------------------------------------------------
174 Description: destroy dialog
175 --------------------------------------------------------------------*/
176 SwChangeDBDlg::~SwChangeDBDlg()
178 delete pMgr;
181 /*--------------------------------------------------------------------
182 Description: close
183 --------------------------------------------------------------------*/
184 void SwChangeDBDlg::Apply()
186 UpdateFlds();
189 void SwChangeDBDlg::UpdateFlds()
191 std::vector<String> aDBNames;
192 aDBNames.reserve(m_pUsedDBTLB->GetSelectionCount());
193 SvTreeListEntry* pEntry = m_pUsedDBTLB->FirstSelected();
195 while( pEntry )
197 if( m_pUsedDBTLB->GetParent( pEntry ))
199 OUString sTmp(m_pUsedDBTLB->GetEntryText( m_pUsedDBTLB->GetParent( pEntry )) +
200 OUString(DB_DELIM) + m_pUsedDBTLB->GetEntryText( pEntry ) + OUString(DB_DELIM) +
201 OUString::number((int)(sal_uLong)pEntry->GetUserData()));
202 aDBNames.push_back(sTmp);
204 pEntry = m_pUsedDBTLB->NextSelected(pEntry);
207 pSh->StartAllAction();
208 String sTableName, sColumnName;
209 sal_Bool bIsTable = sal_False;
210 String sTemp(m_pAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable));
211 sTemp += DB_DELIM;
212 sTemp += sTableName;
213 sTemp += DB_DELIM;
214 sTemp += bIsTable ? '0' : '1';
215 pSh->ChangeDBFields( aDBNames, sTemp);
216 pSh->EndAllAction();
219 IMPL_LINK_NOARG(SwChangeDBDlg, ButtonHdl)
221 String sTableName, sColumnName;
222 SwDBData aData;
223 sal_Bool bIsTable = sal_False;
224 aData.sDataSource = m_pAvailDBTLB->GetDBName(sTableName, sColumnName, &bIsTable);
225 aData.sCommand = sTableName;
226 aData.nCommandType = bIsTable ? 0 : 1;
227 pSh->ChgDBData(aData);
228 ShowDBName(pSh->GetDBData());
229 EndDialog(RET_OK);
231 return 0;
234 IMPL_LINK_NOARG(SwChangeDBDlg, TreeSelectHdl)
236 sal_Bool bEnable = sal_False;
238 SvTreeListEntry* pEntry = m_pAvailDBTLB->GetCurEntry();
240 if (pEntry)
242 if (m_pAvailDBTLB->GetParent(pEntry))
243 bEnable = sal_True;
244 m_pDefineBT->Enable( bEnable );
246 return 0;
249 /*--------------------------------------------------------------------
250 Description: convert database name for display
251 --------------------------------------------------------------------*/
252 void SwChangeDBDlg::ShowDBName(const SwDBData& rDBData)
254 String sTmp(rDBData.sDataSource);
255 String sName;
256 sTmp += '.';
257 sTmp += (String)rDBData.sCommand;
259 for (sal_uInt16 i = 0; i < sTmp.Len(); i++)
261 sName += sTmp.GetChar(i);
262 if (sTmp.GetChar(i) == '~')
263 sName += '~';
266 if (sName.EqualsAscii(".")) //empty
267 sName = SW_RESSTR(SW_STR_NONE);
269 m_pDocDBNameFT->SetText(sName);
272 IMPL_LINK_NOARG(SwChangeDBDlg, AddDBHdl)
274 OUString sNewDB = SwNewDBMgr::LoadAndRegisterDataSource();
275 if (!sNewDB.isEmpty())
276 m_pAvailDBTLB->AddDataSource(sNewDB);
277 return 0;
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */