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_extensions.hxx"
30 #include "oemwiz.hxx"
31 #include "componentmodule.hxx"
32 #include <tools/debug.hxx>
33 #include <sfx2/docfile.hxx>
34 #include <svl/itemset.hxx>
35 #include <svl/itempool.hxx>
36 #include <vcl/msgbox.hxx>
37 #include <unotools/pathoptions.hxx>
38 #ifndef _EXTENSIONS_PRELOAD_PRELOAD_HRC_
39 #include "preload.hrc"
40 #endif
41 #ifndef _SVX_SVXIDS_HRC
42 #include <svx/svxids.hrc>
43 #endif
44 #include <sfx2/app.hxx>
45 #include <svtools/xtextedt.hxx>
46 #include "osl/diagnose.h"
47 #include "tools/urlobj.hxx"
48 
49 #include <sfx2/sfxdlg.hxx>
50 #include <sfx2/tabdlg.hxx>
51 #include <svx/dialogs.hrc>
52 
53 //.........................................................................
54 namespace preload
55 {
56 //.........................................................................
57 
58 	using namespace ::com::sun::star::uno;
59 	using namespace ::com::sun::star::lang;
60     using namespace ::com::sun::star::beans;
61 //.........................................................................
62 
63     struct OEMPreloadDialog_Impl
64     {
65         SfxItemSet*     pSet;
66         TabPage* pWelcomePage;
67         TabPage* pLicensePage;
68         TabPage* pUserDataPage;
69 
70         OEMPreloadDialog_Impl(OEMPreloadDialog* pDialog);
71         ~OEMPreloadDialog_Impl()
72         {
73             delete pWelcomePage;
74             delete pLicensePage;
75             delete pUserDataPage;
76             delete pSet;
77         }
78         void WriteUserData();
79     };
80 /* -----------------------------14.11.2001 11:33------------------------------
81 
82  ---------------------------------------------------------------------------*/
83     OEMPreloadDialog_Impl::OEMPreloadDialog_Impl(OEMPreloadDialog* pDialog)
84         {
85             SfxItemPool& rPool = SFX_APP()->GetPool();
86             pSet = new SfxItemSet(rPool, SID_FIELD_GRABFOCUS, SID_FIELD_GRABFOCUS);
87 			SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
88 			if ( pFact )
89 			{
90 				CreateTabPage pFunc = pFact->GetTabPageCreatorFunc(RID_SFXPAGE_GENERAL);
91             	pUserDataPage = (*pFunc)(pDialog, *pSet);
92             	((SfxTabPage*)pUserDataPage)->Reset(*pSet);
93 			}
94 			else
95 				pUserDataPage = NULL;
96             pWelcomePage = new OEMWelcomeTabPage(pDialog);
97             pLicensePage = new OEMLicenseTabPage(pDialog);
98         }
99 /* -----------------------------14.11.2001 11:33------------------------------
100 
101  ---------------------------------------------------------------------------*/
102         void OEMPreloadDialog_Impl::WriteUserData()
103         {
104 			if ( pUserDataPage )
105             	((SfxTabPage*)pUserDataPage)->FillItemSet(*pSet);
106         }
107 
108 /* -----------------------------14.11.2001 11:33------------------------------
109 
110  ---------------------------------------------------------------------------*/
111     //=====================================================================
112     //= OEMPreloadDialog
113 	//=====================================================================
114 	//---------------------------------------------------------------------
115     OEMPreloadDialog::OEMPreloadDialog( Window* _pParent,
116 			const Reference< XPropertySet >& /*_rxObjectModel*/, const Reference< XMultiServiceFactory >& /*_rxORB*/ )
117         :WizardDialog(_pParent, ModuleRes(RID_DLG_OEMWIZARD)/*, _rxObjectModel, _rxORB*/)
118         ,aPrevPB(this,      ModuleRes(PB_PREV   ))
119         ,aNextPB(this,      ModuleRes(PB_NEXT   ))
120         ,aCancelPB(this,    ModuleRes(PB_CANCEL ))
121         ,aAcceptST(ModuleRes(ST_ACCEPT))
122         ,aFinishST(ModuleRes(ST_FINISH))
123         ,aLicense(ModuleRes(ST_LICENSE_AGREEMENT))
124         ,aUserData(ModuleRes(ST_INSERT_USER_DATA))
125         ,pImpl(new OEMPreloadDialog_Impl(this))
126 	{
127           FreeResource();
128           aDlgTitle = GetText();
129           aPrevPB.Enable(sal_False);
130           aNextST = aNextPB.GetText();
131           aPrevPB.SetClickHdl(LINK(this, OEMPreloadDialog, NextPrevPageHdl));
132           aNextPB.SetClickHdl(LINK(this, OEMPreloadDialog, NextPrevPageHdl));
133           AddButton( &aPrevPB, WIZARDDIALOG_BUTTON_STDOFFSET_X );
134           AddButton( &aNextPB, WIZARDDIALOG_BUTTON_STDOFFSET_X );
135           AddButton( &aCancelPB, WIZARDDIALOG_BUTTON_STDOFFSET_X );
136 
137           SetPrevButton(&aPrevPB);
138           SetNextButton(&aNextPB);
139 
140           AddPage( pImpl->pWelcomePage );
141           AddPage( pImpl->pLicensePage );
142 		  if ( pImpl->pUserDataPage )
143           	AddPage( pImpl->pUserDataPage );
144           SetPage( OEM_WELCOME, pImpl->pWelcomePage );
145           SetPage( OEM_LICENSE, pImpl->pLicensePage );
146 		  if ( pImpl->pUserDataPage )
147           	SetPage( OEM_USERDATA, pImpl->pUserDataPage );
148           ShowPage( OEM_WELCOME );
149     }
150 /* -----------------------------14.11.2001 11:33------------------------------
151 
152  ---------------------------------------------------------------------------*/
153     OEMPreloadDialog::~OEMPreloadDialog( )
154     {
155         delete pImpl;
156     }
157 /* -----------------------------14.11.2001 11:33------------------------------
158 
159  ---------------------------------------------------------------------------*/
160     IMPL_LINK(OEMPreloadDialog, NextPrevPageHdl, PushButton*, pButton)
161     {
162         if(pButton == &aPrevPB)
163         {
164             if(GetCurLevel())
165                 ShowPage(GetCurLevel() - 1);
166         }
167            else if(OEM_USERDATA > GetCurLevel())
168             ShowPage(GetCurLevel() + 1);
169 		else
170 		{
171             pImpl->WriteUserData();
172             Finnish(RET_OK);
173 		}
174 
175         String sTitle(aDlgTitle);
176 
177         switch(GetCurLevel())
178         {
179             case OEM_WELCOME:
180                 aNextPB.SetText(aNextST);
181                 aNextPB.Enable(sal_True);
182             break;
183             case OEM_LICENSE:
184                 sTitle += aLicense;
185                 aNextPB.SetText(aNextST);
186                 aCancelPB.GrabFocus();
187             break;
188             case OEM_USERDATA:
189                 sTitle += aUserData;
190                 aNextPB.SetText(aFinishST);
191             break;
192         }
193         SetText(sTitle);
194         aPrevPB.Enable(GetCurLevel() != OEM_WELCOME);
195         return 0;
196     }
197 /* -----------------------------14.11.2001 11:33------------------------------
198 
199  ---------------------------------------------------------------------------*/
200     sal_Bool OEMPreloadDialog::LoadFromLocalFile(const String& rFileName, String& rContent)
201     {
202         SvtPathOptions aPathOpt;
203         String sFileName = aPathOpt.GetUserConfigPath();//GetModulePath();
204         INetURLObject aURLObject(sFileName);
205         OSL_ASSERT(aURLObject.getSegmentCount() >= 2);
206         aURLObject.removeSegment(); //remove '/config'
207         aURLObject.removeSegment(); //remove '/user'
208         sFileName = aURLObject.GetMainURL(INetURLObject::DECODE_TO_IURI);
209         sFileName += rFileName;
210 
211         SfxMedium aMedium( sFileName,STREAM_READ, sal_True );
212         SvStream* pInStream = aMedium.GetInStream();
213         if( !pInStream )
214             return sal_False;
215 
216         pInStream->ReadCString( rContent, RTL_TEXTENCODING_UTF8 );
217 
218         xub_StrLen nPos;
219         while ( ( nPos = rContent.Search( 12 )) != STRING_NOTFOUND )
220             rContent.Erase( nPos, 1 );
221         return sal_True;
222     }
223 
224 
225     void OEMPreloadDialog::SetCancelString( const String& rText )
226     {
227         aCancelPB.SetText(rText);
228     }
229 
230 /* -----------------------------13.11.2001 12:29------------------------------
231 
232  ---------------------------------------------------------------------------*/
233     OEMWelcomeTabPage::OEMWelcomeTabPage(Window* pParent) :
234         TabPage(pParent, ModuleRes(RID_TP_WELCOME)),
235         aInfoFT(this, ModuleRes(FT_INFO))
236     {
237         FreeResource();
238     }
239 /* -----------------------------13.11.2001 12:30------------------------------
240 
241  ---------------------------------------------------------------------------*/
242     OEMWelcomeTabPage::~OEMWelcomeTabPage()
243     {
244     }
245 /* -----------------------------13.11.2001 12:29------------------------------
246 
247  ---------------------------------------------------------------------------*/
248     OEMLicenseTabPage::OEMLicenseTabPage(OEMPreloadDialog* pParent) :
249         TabPage(pParent, ModuleRes(RID_TP_LICENSE)),
250         aLicenseML(this, ModuleRes(ML_LICENSE)),
251         aInfo1FT(this, ModuleRes(FT_INFO1)),
252         aInfo2FT(this, ModuleRes(FT_INFO2)),
253         aInfo3FT(this, ModuleRes(FT_INFO3)),
254         aInfo2_1FT(this, ModuleRes(FT_INFO2_1)),
255         aInfo3_1FT(this, ModuleRes(FT_INFO3_1)),
256         aCBAccept(this, ModuleRes(CB_ACCEPT)),
257         aPBPageDown(this, ModuleRes(PB_PAGEDOWN)),
258         aArrow(this, ModuleRes(IMG_ARROW)),
259         aStrAccept( ModuleRes(LICENCE_ACCEPT) ),
260 	    aStrNotAccept( ModuleRes(LICENCE_NOTACCEPT) ),
261         bEndReached(sal_False),
262         pPreloadDialog(pParent)
263     {
264         FreeResource();
265 
266         aLicenseML.SetEndReachedHdl( LINK(this, OEMLicenseTabPage, EndReachedHdl) );
267         aLicenseML.SetScrolledHdl( LINK(this, OEMLicenseTabPage, ScrolledHdl) );
268 
269         aPBPageDown.SetClickHdl( LINK(this, OEMLicenseTabPage, PageDownHdl) );
270         aCBAccept.SetClickHdl( LINK(this, OEMLicenseTabPage, AcceptHdl) );
271 
272         // We want a automatic repeating page down button
273         WinBits aStyle = aPBPageDown.GetStyle();
274         aStyle |= WB_REPEAT;
275         aPBPageDown.SetStyle( aStyle );
276 
277         aOldCancelText = pPreloadDialog->GetCancelString();
278         pPreloadDialog->SetCancelString( aStrNotAccept );
279 
280         String aText = aInfo2FT.GetText();
281         aText.SearchAndReplaceAll( UniString::CreateFromAscii("%PAGEDOWN"), aPBPageDown.GetText() );
282         aInfo2FT.SetText( aText );
283     }
284 /* -----------------------------13.11.2001 12:30------------------------------
285 
286  ---------------------------------------------------------------------------*/
287     OEMLicenseTabPage::~OEMLicenseTabPage()
288     {
289     }
290 /* ---------------------------14.11.2001 11:24----------------------------
291 
292     -----------------------------------------------------------------------*/
293     void OEMLicenseTabPage::ActivatePage()
294     {
295         if(!aLicenseML.GetText().Len())
296         {
297         aLicenseML.SetLeftMargin( 5 );
298         String sLicense;
299 #ifdef UNX
300         OEMPreloadDialog::LoadFromLocalFile(String::CreateFromAscii("LICENSE"), sLicense);
301 #else
302         OEMPreloadDialog::LoadFromLocalFile(String::CreateFromAscii("license.txt"), sLicense);
303 #endif
304         aLicenseML.SetText( sLicense );
305         }
306 
307         EnableControls();
308     }
309 
310     //------------------------------------------------------------------------
311     IMPL_LINK( OEMLicenseTabPage, AcceptHdl, CheckBox *, EMPTYARG )
312     {
313         EnableControls();
314         return 0;
315     }
316 
317     //------------------------------------------------------------------------
318     IMPL_LINK( OEMLicenseTabPage, PageDownHdl, PushButton *, EMPTYARG )
319     {
320         aLicenseML.ScrollDown( SCROLL_PAGEDOWN );
321         return 0;
322     }
323 
324     //------------------------------------------------------------------------
325     IMPL_LINK( OEMLicenseTabPage, EndReachedHdl, LicenceView *, EMPTYARG )
326     {
327         bEndReached = sal_True;
328 
329         EnableControls();
330         aCBAccept.GrabFocus();
331 
332         return 0;
333     }
334 
335     //------------------------------------------------------------------------
336     IMPL_LINK( OEMLicenseTabPage, ScrolledHdl, LicenceView *, EMPTYARG )
337     {
338         EnableControls();
339 
340         return 0;
341     }
342 
343     //------------------------------------------------------------------------
344     void OEMLicenseTabPage::EnableControls()
345     {
346         if( !bEndReached &&
347             ( aLicenseML.IsEndReached() || !aLicenseML.GetText().Len() ) )
348             bEndReached = sal_True;
349 
350         if ( bEndReached )
351         {
352             Point aPos( 0, aInfo3_1FT.GetPosPixel().Y() );
353             aArrow.SetPosPixel( aPos );
354             aCBAccept.Enable();
355         }
356         else
357         {
358             Point aPos( 0, aInfo2_1FT.GetPosPixel().Y() );
359             aArrow.SetPosPixel( aPos );
360             aCBAccept.Disable();
361         }
362 
363         if ( aLicenseML.IsEndReached() )
364             aPBPageDown.Disable();
365         else
366             aPBPageDown.Enable();
367 
368         if ( aCBAccept.IsChecked() )
369         {
370             PushButton *pNext = pPreloadDialog->GetNextButton();
371             if ( ! pNext->IsEnabled() )
372             {
373                 pPreloadDialog->SetCancelString( aOldCancelText );
374                 pNext->Enable(sal_True);
375             }
376         }
377         else
378         {
379             PushButton *pNext = pPreloadDialog->GetNextButton();
380             if ( pNext->IsEnabled() )
381             {
382                 pPreloadDialog->SetCancelString( aStrNotAccept );
383                 pNext->Enable(sal_False);
384             }
385         }
386     }
387 
388     //------------------------------------------------------------------------
389     //------------------------------------------------------------------------
390     //------------------------------------------------------------------------
391     LicenceView::LicenceView( Window* pParent, const ResId& rResId )
392         : MultiLineEdit( pParent, rResId )
393     {
394         SetLeftMargin( 5 );
395 
396         mbEndReached = IsEndReached();
397 
398 	    StartListening( *GetTextEngine() );
399     }
400 
401     //------------------------------------------------------------------------
402     LicenceView::~LicenceView()
403     {
404         maEndReachedHdl = Link();
405         maScrolledHdl   = Link();
406 
407         EndListeningAll();
408     }
409 
410     //------------------------------------------------------------------------
411     void LicenceView::ScrollDown( ScrollType eScroll )
412     {
413         ScrollBar*  pScroll = GetVScrollBar();
414 
415         if ( pScroll )
416             pScroll->DoScrollAction( eScroll );
417     }
418 
419     //------------------------------------------------------------------------
420     sal_Bool LicenceView::IsEndReached() const
421     {
422         sal_Bool bEndReached;
423 
424         ExtTextView*    pView = GetTextView();
425         ExtTextEngine*  pEdit = GetTextEngine();
426         sal_uLong           nHeight = pEdit->GetTextHeight();
427         Size            aOutSize = pView->GetWindow()->GetOutputSizePixel();
428         Point           aBottom( 0, aOutSize.Height() );
429 
430         if ( (sal_uLong) pView->GetDocPos( aBottom ).Y() >= nHeight - 1 )
431             bEndReached = sal_True;
432         else
433             bEndReached = sal_False;
434 
435         return bEndReached;
436     }
437 
438     //------------------------------------------------------------------------
439     void LicenceView::Notify( SfxBroadcaster& /*rBC*/, const SfxHint& rHint )
440     {
441         if ( rHint.IsA( TYPE(TextHint) ) )
442         {
443             sal_Bool    bLastVal = EndReached();
444             sal_uLong   nId = ((const TextHint&)rHint).GetId();
445 
446             if ( nId == TEXT_HINT_PARAINSERTED )
447             {
448                 if ( bLastVal )
449                     mbEndReached = IsEndReached();
450             }
451             else if ( nId == TEXT_HINT_VIEWSCROLLED )
452             {
453                 if ( ! mbEndReached )
454                     mbEndReached = IsEndReached();
455                 maScrolledHdl.Call( this );
456             }
457 
458             if ( EndReached() && !bLastVal )
459             {
460                 maEndReachedHdl.Call( this );
461             }
462         }
463     }
464 
465     //------------------------------------------------------------------------
466 
467 //.........................................................................
468 }   // namespace preload
469 //.........................................................................
470 
471 
472