xref: /trunk/main/cui/source/dialogs/hlmailtp.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_cui.hxx"
30 
31 #include <com/sun/star/frame/XDispatchProvider.hpp>
32 #include <com/sun/star/util/XURLTransformer.hpp>
33 #include <com/sun/star/frame/FrameSearchFlag.hpp>
34 #include <sfx2/request.hxx>
35 
36 #include <comphelper/processfactory.hxx>
37 #include <sfx2/viewfrm.hxx>
38 #include <unotools/pathoptions.hxx>
39 #include <unotools/moduleoptions.hxx>
40 
41 #include "hlmailtp.hxx"
42 #include "hyperdlg.hrc"
43 
44 using namespace ::rtl;
45 using namespace ::com::sun::star;
46 
47 /*************************************************************************
48 |*
49 |* Contructor / Destructor
50 |*
51 |************************************************************************/
52 
53 SvxHyperlinkMailTp::SvxHyperlinkMailTp ( Window *pParent, const SfxItemSet& rItemSet)
54 :   SvxHyperlinkTabPageBase ( pParent, CUI_RES( RID_SVXPAGE_HYPERLINK_MAIL ),
55                               rItemSet ),
56     maGrpMailNews   ( this, CUI_RES (GRP_MAILNEWS) ),
57     maRbtMail       ( this, CUI_RES (RB_LINKTYP_MAIL) ),
58     maRbtNews       ( this, CUI_RES (RB_LINKTYP_NEWS) ),
59     maFtReceiver    ( this, CUI_RES (FT_RECEIVER) ),
60     maCbbReceiver   ( this, INET_PROT_MAILTO ),
61     maBtAdrBook     ( this, CUI_RES (BTN_ADRESSBOOK) ),
62     maFtSubject     ( this, CUI_RES (FT_SUBJECT) ),
63     maEdSubject     ( this, CUI_RES (ED_SUBJECT) )
64 {
65     // Set HC bitmaps and disable display of bitmap names.
66     maBtAdrBook.SetModeImage( Image( CUI_RES( IMG_ADRESSBOOK_HC ) ), BMP_COLOR_HIGHCONTRAST );
67     maBtAdrBook.EnableTextDisplay (sal_False);
68 
69     InitStdControls();
70     FreeResource();
71 
72     // Init URL-Box (pos&size, Open-Handler)
73     maCbbReceiver.SetPosSizePixel ( LogicToPixel( Point( COL_2, 25 ), MAP_APPFONT ),
74                                     LogicToPixel( Size ( 176 - COL_DIFF, 60), MAP_APPFONT ) );
75 
76     maCbbReceiver.Show();
77     maCbbReceiver.SetHelpId( HID_HYPERDLG_MAIL_PATH );
78 
79     SetExchangeSupport ();
80 
81     // set defaults
82     maRbtMail.Check ();
83 
84     // overload handlers
85     maRbtMail.SetClickHdl        ( LINK ( this, SvxHyperlinkMailTp, Click_SmartProtocol_Impl ) );
86     maRbtNews.SetClickHdl        ( LINK ( this, SvxHyperlinkMailTp, Click_SmartProtocol_Impl ) );
87     maBtAdrBook.SetClickHdl      ( LINK ( this, SvxHyperlinkMailTp, ClickAdrBookHdl_Impl ) );
88     maCbbReceiver.SetModifyHdl   ( LINK ( this, SvxHyperlinkMailTp, ModifiedReceiverHdl_Impl) );
89 
90     if ( !SvtModuleOptions().IsModuleInstalled( SvtModuleOptions::E_SDATABASE ) )
91         maBtAdrBook.Hide();
92 
93     maBtAdrBook.SetAccessibleRelationMemberOf( &maGrpMailNews );
94     maBtAdrBook.SetAccessibleRelationLabeledBy( &maFtReceiver );
95 }
96 
97 SvxHyperlinkMailTp::~SvxHyperlinkMailTp ()
98 {
99 }
100 
101 /*************************************************************************
102 |*
103 |* Fill the all dialog-controls except controls in groupbox "more..."
104 |*
105 |************************************************************************/
106 
107 void SvxHyperlinkMailTp::FillDlgFields ( String& aStrURL )
108 {
109     const sal_Char sMailtoScheme[] = INET_MAILTO_SCHEME;
110 
111     INetURLObject aURL( aStrURL );
112     String aStrScheme = GetSchemeFromURL( aStrURL );
113 
114     // set URL-field and additional controls
115     String aStrURLc ( aStrURL );
116     // set additional controls for EMail:
117     if ( aStrScheme.SearchAscii( sMailtoScheme ) == 0 )
118     {
119         // Find mail-subject
120         String aStrSubject, aStrTmp ( aStrURLc );
121 
122         const sal_Char sSubject[] = "subject";
123         xub_StrLen nPos = aStrTmp.ToLowerAscii().SearchAscii( sSubject, 0 );
124         nPos = aStrTmp.Search( sal_Unicode( '=' ), nPos );
125 
126         if ( nPos != STRING_NOTFOUND )
127             aStrSubject = aStrURLc.Copy( nPos+1, aStrURLc.Len() );
128 
129         nPos = aStrURLc.Search ( sal_Unicode( '?' ), 0);
130 
131         aStrURLc = aStrURLc.Copy( 0, ( nPos == STRING_NOTFOUND ?
132                                            aStrURLc.Len() : nPos ) );
133 
134         maEdSubject.SetText ( aStrSubject );
135     }
136     else
137     {
138         maEdSubject.SetText (aEmptyStr);
139     }
140 
141     maCbbReceiver.SetText ( aStrURLc );
142 
143     SetScheme( aStrScheme );
144 }
145 
146 /*************************************************************************
147 |*
148 |* retrieve and prepare data from dialog-fields
149 |*
150 |************************************************************************/
151 
152 void SvxHyperlinkMailTp::GetCurentItemData ( String& aStrURL, String& aStrName,
153                                              String& aStrIntName, String& aStrFrame,
154                                              SvxLinkInsertMode& eMode )
155 {
156     aStrURL = CreateAbsoluteURL();
157     GetDataFromCommonFields( aStrName, aStrIntName, aStrFrame, eMode );
158 }
159 
160 String SvxHyperlinkMailTp::CreateAbsoluteURL() const
161 {
162     String aStrURL = maCbbReceiver.GetText();
163     INetURLObject aURL(aStrURL);
164 
165     if( aURL.GetProtocol() == INET_PROT_NOT_VALID )
166     {
167         aURL.SetSmartProtocol( GetSmartProtocolFromButtons() );
168         aURL.SetSmartURL(aStrURL);
169     }
170 
171     // subject for EMail-url
172     if( aURL.GetProtocol() == INET_PROT_MAILTO )
173     {
174         if ( maEdSubject.GetText() != aEmptyStr )
175         {
176             String aQuery = UniString::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM( "subject=" ) );
177             aQuery.Append( maEdSubject.GetText() );
178             aURL.SetParam(aQuery);
179         }
180     }
181 
182     if ( aURL.GetProtocol() != INET_PROT_NOT_VALID )
183         return aURL.GetMainURL( INetURLObject::DECODE_WITH_CHARSET );
184     else //#105788# always create a URL even if it is not valid
185         return aStrURL;
186 }
187 
188 /*************************************************************************
189 |*
190 |* static method to create Tabpage
191 |*
192 |************************************************************************/
193 
194 IconChoicePage* SvxHyperlinkMailTp::Create( Window* pWindow, const SfxItemSet& rItemSet )
195 {
196     return( new SvxHyperlinkMailTp( pWindow, rItemSet ) );
197 }
198 
199 /*************************************************************************
200 |*
201 |* Set initial focus
202 |*
203 |************************************************************************/
204 
205 void SvxHyperlinkMailTp::SetInitFocus()
206 {
207     maCbbReceiver.GrabFocus();
208 }
209 
210 /*************************************************************************
211 |************************************************************************/
212 
213 void SvxHyperlinkMailTp::SetScheme( const String& aScheme )
214 {
215     //if  aScheme is empty or unknown the default beaviour is like it where MAIL
216     const sal_Char sNewsScheme[]   = INET_NEWS_SCHEME;
217 
218     sal_Bool bMail = aScheme.SearchAscii( sNewsScheme ) != 0;
219 
220     //update protocol button selection:
221     maRbtMail.Check(bMail);
222     maRbtNews.Check(!bMail);
223 
224     //update target:
225     RemoveImproperProtocol(aScheme);
226     maCbbReceiver.SetSmartProtocol( GetSmartProtocolFromButtons() );
227 
228     //show/hide  special fields for MAIL:
229     maFtSubject.Enable(bMail);
230     maEdSubject.Enable(bMail);
231 }
232 
233 /*************************************************************************
234 |*
235 |* Remove protocol if it does not fit to the current button selection
236 |*
237 |************************************************************************/
238 
239 void SvxHyperlinkMailTp::RemoveImproperProtocol(const String& aProperScheme)
240 {
241     String aStrURL ( maCbbReceiver.GetText() );
242     if ( aStrURL != aEmptyStr )
243     {
244         String aStrScheme = GetSchemeFromURL( aStrURL );
245         if ( aStrScheme != aEmptyStr && aStrScheme != aProperScheme )
246         {
247             aStrURL.Erase ( 0, aStrScheme.Len() );
248             maCbbReceiver.SetText ( aStrURL );
249         }
250     }
251 }
252 
253 String SvxHyperlinkMailTp::GetSchemeFromButtons() const
254 {
255     if( maRbtNews.IsChecked() )
256     {
257         return String::CreateFromAscii( INET_NEWS_SCHEME );
258     }
259     return String::CreateFromAscii( INET_MAILTO_SCHEME );
260 }
261 
262 INetProtocol SvxHyperlinkMailTp::GetSmartProtocolFromButtons() const
263 {
264     if( maRbtNews.IsChecked() )
265     {
266         return INET_PROT_NEWS;
267     }
268     return INET_PROT_MAILTO;
269 }
270 
271 /*************************************************************************
272 |*
273 |* Click on radiobutton : Type EMail
274 |*
275 |************************************************************************/
276 
277 IMPL_LINK ( SvxHyperlinkMailTp, Click_SmartProtocol_Impl, void *, EMPTYARG )
278 {
279     String aScheme = GetSchemeFromButtons();
280     SetScheme( aScheme );
281     return( 0L );
282 }
283 
284 /*************************************************************************
285 |*
286 |* Contens of editfield "receiver" modified
287 |*
288 |************************************************************************/
289 
290 IMPL_LINK ( SvxHyperlinkMailTp, ModifiedReceiverHdl_Impl, void *, EMPTYARG )
291 {
292     String aScheme = GetSchemeFromURL( maCbbReceiver.GetText() );
293     if(aScheme.Len()!=0)
294         SetScheme( aScheme );
295 
296     return( 0L );
297 }
298 
299 /*************************************************************************
300 |*
301 |* Click on imagebutton : addressbook
302 |*
303 |************************************************************************/
304 
305 IMPL_LINK ( SvxHyperlinkMailTp, ClickAdrBookHdl_Impl, void *, EMPTYARG )
306 {
307     SfxViewFrame* pViewFrame = SfxViewFrame::Current();
308     if( pViewFrame )
309     {
310         SfxItemPool &rPool = pViewFrame->GetPool();
311         SfxRequest aReq(SID_VIEW_DATA_SOURCE_BROWSER, 0, rPool);
312         pViewFrame->ExecuteSlot( aReq, sal_True );
313     }
314 
315 
316 /*  uno::Reference< frame::XDispatchProvider > xProv( pViewFrame->GetFrame().GetFrameInterface(), uno::UNO_QUERY );
317     if ( xProv.is() )
318     {
319 !!! (pb) we need a new config item here
320         SfxAppIniManagerProperty aProp;
321         GetpApp()->Property( aProp );
322         if( !aProp.GetIniManager() )
323             return ( 0L );
324 
325         String aAddressBook = aProp.GetIniManager()->Get( SFX_KEY_ADDRESSBOOK );
326         INetURLObject aObj;
327         aObj.SetSmartProtocol( INET_PROT_FILE );
328         aObj.SetURL( aAddressBook.GetToken( 0, sal_Unicode( ';' ) ) );
329 
330         String aMark( RTL_CONSTASCII_USTRINGPARAM( "db:Table;" ) );
331         aMark += aAddressBook.GetToken( 1, sal_Unicode( ';' ) );
332         aObj.SetMark( aMark );
333 
334         util::URL aURL;
335         aURL.Complete = ::rtl::OUString( aObj.GetMainURL( INetURLObject::NO_DECODE ) );
336 
337         uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
338         if( xFactory.is() )
339         {
340             uno::Reference< util::XURLTransformer > xTrans( xFactory->createInstance
341                 ( OUString::createFromAscii( "com.sun.star.util.URLTransformer" ) ),
342                                   uno::UNO_QUERY);
343             xTrans->parseStrict( aURL );
344 
345             uno::Reference< frame::XDispatch > aDisp = xProv->queryDispatch( aURL,
346                                                         OUString::createFromAscii( "_beamer" ),
347                                                         frame::FrameSearchFlag::GLOBAL |
348                                                         frame::FrameSearchFlag::CREATE );
349             if ( aDisp.is() )
350             {
351                 uno::Sequence< beans::PropertyValue > aArgs(1);
352                 beans::PropertyValue* pArg = aArgs.getArray();
353                 pArg[0].Name = DEFINE_CONST_UNICODE("Referer");
354                 pArg[0].Value = uno::makeAny( OUString( DEFINE_CONST_UNICODE("private:user") ) );
355                 aDisp->dispatch( aURL, aArgs );
356             }
357         }
358     }
359 */
360 
361     return( 0L );
362 }
363 
364 
365