LCOV - code coverage report
Current view: top level - sw/source/ui/fldui - changedb.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 0 133 0.0 %
Date: 2014-11-03 Functions: 0 16 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10