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