xref: /trunk/main/sw/source/ui/envelp/labelexp.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 #ifdef SW_DLLIMPLEMENTATION
31 #undef SW_DLLIMPLEMENTATION
32 #endif
33 
34 
35 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
36 #include <com/sun/star/util/XRefreshable.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <comphelper/processfactory.hxx>
39 #include <swtypes.hxx>
40 #include <labfmt.hxx>
41 #include <unotools.hxx>
42 #include <unoatxt.hxx>
43 #include <unomid.h>
44 #include <unoprnms.hxx>
45 
46 
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::container;
50 using namespace ::com::sun::star::uno;
51 using namespace ::comphelper;
52 using ::rtl::OUString;
53 
54 #ifdef SW_PROP_NAME_STR
55 #undef SW_PROP_NAME_STR
56 #endif
57 #define SW_PROP_NAME_STR(nId) SwGetPropName((nId)).pName
58 
59 /* -----------------08.07.99 15:15-------------------
60 
61  --------------------------------------------------*/
62 void SwVisitingCardPage::InitFrameControl()
63 {
64     Link aLink(LINK(this, SwVisitingCardPage, FrameControlInitializedHdl));
65     pExampleFrame = new SwOneExampleFrame( aExampleWIN,
66                                     EX_SHOW_BUSINESS_CARDS, &aLink );
67 
68     uno::Reference< lang::XMultiServiceFactory >  xMgr =
69                                             getProcessServiceFactory();
70     //now the AutoText ListBoxes have to be filled
71 
72     uno::Reference< uno::XInterface >  xAText =
73         xMgr->createInstance( C2U("com.sun.star.text.AutoTextContainer") );
74     _xAutoText = uno::Reference< container::XNameAccess >(xAText, uno::UNO_QUERY);
75 
76     uno::Sequence<OUString> aNames = _xAutoText->getElementNames();
77     const OUString* pGroups = aNames.getConstArray();
78     OUString uTitleName( C2U(SW_PROP_NAME_STR(UNO_NAME_TITLE)) );
79 
80     for(sal_uInt16 i = 0; i < aNames.getLength(); i++)
81     {
82         uno::Any aGroup = _xAutoText->getByName(pGroups[i]);
83         uno::Reference< text::XAutoTextGroup >  xGroup;
84         aGroup >>= xGroup;
85         uno::Reference< container::XIndexAccess >  xIdxAcc(xGroup, uno::UNO_QUERY);
86         try
87         {
88             if(!xIdxAcc.is() || xIdxAcc->getCount())
89             {
90                 uno::Reference< beans::XPropertySet >  xPrSet(xGroup, uno::UNO_QUERY);
91                 uno::Any aTitle = xPrSet->getPropertyValue( uTitleName );
92                 OUString uTitle;
93                 aTitle >>= uTitle;
94                 String sGroup(pGroups[i]);
95                 sal_uInt16 nEntry = aAutoTextGroupLB.InsertEntry(uTitle);
96                 aAutoTextGroupLB.SetEntryData(nEntry, new String(sGroup));
97             }
98         }
99         catch(Exception&)
100         {
101         }
102     }
103     if(aAutoTextGroupLB.GetEntryCount())
104     {
105         if(LISTBOX_ENTRY_NOTFOUND == aAutoTextGroupLB.GetSelectEntryPos())
106             aAutoTextGroupLB.SelectEntryPos(0);
107         String sCurGroupName(
108             *(String*)aAutoTextGroupLB.GetEntryData(aAutoTextGroupLB.GetSelectEntryPos()));
109         if(_xAutoText->hasByName(sCurGroupName))
110         {
111             uno::Any aGroup = _xAutoText->getByName(sCurGroupName);
112             try
113             {
114                 uno::Reference< text::XAutoTextGroup >  xGroup;
115                 aGroup >>= xGroup;
116                 uno::Sequence< OUString > aBlockNames = xGroup->getElementNames();
117                 uno::Sequence< OUString > aTitles = xGroup->getTitles() ;
118 
119                 SetUserData( aBlockNames.getLength(), aTitles.getConstArray(),
120                             aBlockNames.getConstArray() );
121             }
122             catch( uno::RuntimeException& )
123             {
124                 // we'll be her if path settings were wrong
125             }
126         }
127     }
128 }
129 /* -----------------01.10.99 13:19-------------------
130 
131  --------------------------------------------------*/
132 IMPL_LINK( SwVisitingCardPage, FrameControlInitializedHdl, void*, EMPTYARG )
133 {
134     SvLBoxEntry* pSel = aAutoTextLB.FirstSelected();
135     String sEntry;
136     if( pSel )
137         sEntry = *(String*)pSel->GetUserData();
138     uno::Reference< text::XTextCursor > & xCrsr = pExampleFrame->GetTextCursor();
139     OUString uEntry(sEntry);
140 
141     if(LISTBOX_ENTRY_NOTFOUND != aAutoTextGroupLB.GetSelectEntryPos())
142     {
143         String sGroup( *(String*)aAutoTextGroupLB.GetEntryData(
144                                     aAutoTextGroupLB.GetSelectEntryPos() ) );
145         uno::Any aGroup = _xAutoText->getByName(sGroup);
146         uno::Reference< text::XAutoTextGroup >  xGroup;
147         aGroup >>= xGroup;
148 
149         if( sEntry.Len() && xGroup->hasByName( uEntry ) )
150         {
151             uno::Any aEntry(xGroup->getByName(uEntry));
152             uno::Reference< text::XAutoTextEntry >  xEntry;
153             aEntry >>= xEntry;
154             if(xEntry.is())
155             {
156                 uno::Reference< text::XTextRange >  xRange(xCrsr, uno::UNO_QUERY);
157                 xEntry->applyTo(xRange);
158             }
159             UpdateFields();
160         }
161     }
162     return 0;
163 }
164 /* -----------------22.07.99 11:06-------------------
165 
166  --------------------------------------------------*/
167 IMPL_LINK( SwVisitingCardPage, AutoTextSelectHdl, void*, pBox )
168 {
169     if(_xAutoText.is())
170     {
171         if( &aAutoTextGroupLB == pBox )
172         {
173             String sGroup( *(String*)aAutoTextGroupLB.GetEntryData(
174                                     aAutoTextGroupLB.GetSelectEntryPos()));
175             uno::Any aGroup = _xAutoText->getByName(sGroup);
176             uno::Reference< text::XAutoTextGroup >  xGroup;
177             aGroup >>= xGroup;
178 
179             ClearUserData();
180             aAutoTextLB.Clear();
181 
182             uno::Sequence<OUString> aBlockNames = xGroup->getElementNames();
183             uno::Sequence< OUString > aTitles = xGroup->getTitles() ;
184             SetUserData( aBlockNames.getLength(), aTitles.getConstArray(),
185                         aBlockNames.getConstArray() );
186         }
187         if(pExampleFrame->IsInitialized())
188             pExampleFrame->ClearDocument( sal_True );
189     }
190     return 0;
191 }
192 
193 /* -----------------01.10.99 11:59-------------------
194 
195  --------------------------------------------------*/
196 void SwVisitingCardPage::UpdateFields()
197 {
198     uno::Reference< frame::XModel >  xModel;
199     if( pExampleFrame && (xModel = pExampleFrame->GetModel()).is())
200     {
201         SwLabDlg::UpdateFieldInformation(xModel, aLabItem);
202     }
203 }
204 /* -----------------01.10.99 15:16-------------------
205 
206  --------------------------------------------------*/
207 void SwLabDlg::UpdateFieldInformation(uno::Reference< frame::XModel > & xModel, const SwLabItem& rItem)
208 {
209     uno::Reference< text::XTextFieldsSupplier >  xFlds(xModel, uno::UNO_QUERY);
210     uno::Reference< container::XNameAccess >  xFldMasters = xFlds->getTextFieldMasters();
211 
212     static const struct _SwLabItemMap {
213         const char* pName;
214         rtl::OUString SwLabItem:: *pValue;
215     }  aArr[] = {
216         { "BC_PRIV_FIRSTNAME"  , &SwLabItem::aPrivFirstName },
217         { "BC_PRIV_NAME"       , &SwLabItem::aPrivName },
218         { "BC_PRIV_INITIALS"   , &SwLabItem::aPrivShortCut },
219         { "BC_PRIV_FIRSTNAME_2", &SwLabItem::aPrivFirstName2 },
220         { "BC_PRIV_NAME_2"     , &SwLabItem::aPrivName2 },
221         { "BC_PRIV_INITIALS_2" , &SwLabItem::aPrivShortCut2 },
222         { "BC_PRIV_STREET"     , &SwLabItem::aPrivStreet },
223         { "BC_PRIV_ZIP"        , &SwLabItem::aPrivZip },
224         { "BC_PRIV_CITY"       , &SwLabItem::aPrivCity },
225         { "BC_PRIV_COUNTRY"    , &SwLabItem::aPrivCountry },
226         { "BC_PRIV_STATE"      , &SwLabItem::aPrivState },
227         { "BC_PRIV_TITLE"      , &SwLabItem::aPrivTitle },
228         { "BC_PRIV_PROFESSION" , &SwLabItem::aPrivProfession },
229         { "BC_PRIV_PHONE"      , &SwLabItem::aPrivPhone },
230         { "BC_PRIV_MOBILE"     , &SwLabItem::aPrivMobile },
231         { "BC_PRIV_FAX"        , &SwLabItem::aPrivFax },
232         { "BC_PRIV_WWW"        , &SwLabItem::aPrivWWW },
233         { "BC_PRIV_MAIL"       , &SwLabItem::aPrivMail },
234         { "BC_COMP_COMPANY"    , &SwLabItem::aCompCompany },
235         { "BC_COMP_COMPANYEXT" , &SwLabItem::aCompCompanyExt },
236         { "BC_COMP_SLOGAN"     , &SwLabItem::aCompSlogan },
237         { "BC_COMP_STREET"     , &SwLabItem::aCompStreet },
238         { "BC_COMP_ZIP"        , &SwLabItem::aCompZip },
239         { "BC_COMP_CITY"       , &SwLabItem::aCompCity },
240         { "BC_COMP_COUNTRY"    , &SwLabItem::aCompCountry },
241         { "BC_COMP_STATE"      , &SwLabItem::aCompState },
242         { "BC_COMP_POSITION"   , &SwLabItem::aCompPosition },
243         { "BC_COMP_PHONE"      , &SwLabItem::aCompPhone },
244         { "BC_COMP_MOBILE"     , &SwLabItem::aCompMobile },
245         { "BC_COMP_FAX"        , &SwLabItem::aCompFax },
246         { "BC_COMP_WWW"        , &SwLabItem::aCompWWW },
247         { "BC_COMP_MAIL"       , &SwLabItem::aCompMail },
248         { 0, 0 }
249     };
250 
251     try
252     {
253         String sFldName( String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM(
254                             "com.sun.star.text.FieldMaster.User." )));
255         OUString uCntName( C2U( SW_PROP_NAME_STR(UNO_NAME_CONTENT )));
256         for( const _SwLabItemMap* p = aArr; p->pName; ++p )
257         {
258             String sCurFldName( sFldName );
259             sCurFldName.AppendAscii( p->pName );
260             OUString uFldName( sCurFldName );
261             if( xFldMasters->hasByName( uFldName ))
262             {
263                 uno::Any aFirstName = xFldMasters->getByName( uFldName );
264                 uno::Reference< beans::XPropertySet >  xFld;
265                 aFirstName >>= xFld;
266                 uno::Any aContent;
267                 aContent <<= rItem.*p->pValue;
268                 xFld->setPropertyValue( uCntName, aContent );
269             }
270         }
271     }
272     catch( uno::RuntimeException&)
273     {
274         //
275     }
276 
277     uno::Reference< container::XEnumerationAccess >  xFldAcc = xFlds->getTextFields();
278     uno::Reference< util::XRefreshable >  xRefresh(xFldAcc, uno::UNO_QUERY);
279     xRefresh->refresh();
280 }
281 
282