xref: /trunk/main/uui/source/newerverwarn.cxx (revision 9807c9de)
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 #include "newerverwarn.hxx"
25 #include "newerverwarn.hrc"
26 #include "ids.hrc"
27 
28 #include <com/sun/star/frame/XDesktop.hpp>
29 #include <com/sun/star/frame/XDispatchProvider.hpp>
30 #include <com/sun/star/system/SystemShellExecute.hpp>
31 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
32 #include <com/sun/star/util/XURLTransformer.hpp>
33 #include <com/sun/star/container/XNameReplace.hpp>
34 
35 #include <comphelper/processfactory.hxx>
36 #include <comphelper/configurationhelper.hxx>
37 #include <comphelper/componentcontext.hxx>
38 #include <rtl/bootstrap.hxx>
39 #include <tools/diagnose_ex.h>
40 #include <vcl/msgbox.hxx>
41 #include <osl/process.h>
42 
43 namespace beans     = ::com::sun::star::beans;
44 namespace frame     = ::com::sun::star::frame;
45 namespace lang      = ::com::sun::star::lang;
46 namespace uno       = ::com::sun::star::uno;
47 namespace util      = ::com::sun::star::util;
48 namespace container = ::com::sun::star::container;
49 
50 using namespace com::sun::star::system;
51 
52 #define	DEFINE_CONST_UNICODE( CONSTASCII )  ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CONSTASCII ) )
53 
54 namespace uui
55 {
56 
NewerVersionWarningDialog(Window * pParent,const::rtl::OUString & rVersion,ResMgr & rResMgr)57 NewerVersionWarningDialog::NewerVersionWarningDialog(
58     Window* pParent, const ::rtl::OUString& rVersion, ResMgr& rResMgr ) :
59 
60     ModalDialog( pParent, ResId( RID_DLG_NEWER_VERSION_WARNING, rResMgr ) ),
61 
62     m_aImage        ( this, ResId( FI_IMAGE, rResMgr ) ),
63     m_aInfoText     ( this, ResId( FT_INFO, rResMgr ) ),
64     m_aButtonLine   ( this, ResId( FL_BUTTON, rResMgr ) ),
65     m_aUpdateBtn    ( this, ResId( PB_UPDATE, rResMgr ) ),
66     m_aLaterBtn     ( this, ResId( PB_LATER, rResMgr ) ),
67     m_sVersion      ( rVersion )
68 {
69     FreeResource();
70 
71     m_aUpdateBtn.SetClickHdl( LINK( this, NewerVersionWarningDialog, UpdateHdl ) );
72     m_aLaterBtn.SetClickHdl( LINK( this, NewerVersionWarningDialog, LaterHdl ) );
73 
74     InitButtonWidth();
75 }
76 
~NewerVersionWarningDialog()77 NewerVersionWarningDialog::~NewerVersionWarningDialog()
78 {
79 }
80 
IMPL_LINK(NewerVersionWarningDialog,UpdateHdl,PushButton *,EMPTYARG)81 IMPL_LINK( NewerVersionWarningDialog, UpdateHdl, PushButton*, EMPTYARG )
82 {
83     // detect execute path
84     ::rtl::OUString sProgramPath;
85     osl_getExecutableFile( &sProgramPath.pData );
86     sal_uInt32 nLastIndex = sProgramPath.lastIndexOf( '/' );
87     if ( nLastIndex > 0 )
88         sProgramPath = sProgramPath.copy( 0, nLastIndex + 1 );
89 
90     // read keys from soffice.ini (sofficerc)
91     ::rtl::OUString sIniFileName = sProgramPath;
92     sIniFileName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SAL_CONFIGFILE( "version" ) ) );
93     ::rtl::Bootstrap aIniFile( sIniFileName );
94     ::rtl::OUString sNotifyURL;
95     aIniFile.getFrom( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ODFNotifyURL" ) ), sNotifyURL );
96 
97     try
98     {
99         if ( ( sNotifyURL.getLength() > 0 ) && ( m_sVersion.getLength() > 0 ) )
100         {
101             uno::Reference< XSystemShellExecute > xSystemShell(
102                 com::sun::star::system::SystemShellExecute::create(
103                     ::comphelper::getProcessComponentContext() ) );
104             sNotifyURL += m_sVersion;
105             if ( xSystemShell.is() && sNotifyURL.getLength() )
106             {
107                 xSystemShell->execute(
108                     sNotifyURL, ::rtl::OUString(), SystemShellExecuteFlags::DEFAULTS );
109             }
110         }
111         else
112         {
113             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
114 
115             uno::Reference < container::XNameReplace > xUpdateConfig(
116                 aContext.createComponent( "com.sun.star.setup.UpdateCheckConfig" ), uno::UNO_QUERY_THROW );
117 
118             sal_Bool bUpdateCheckEnabled = sal_False;
119             OSL_VERIFY( xUpdateConfig->getByName( DEFINE_CONST_UNICODE( "AutoCheckEnabled" ) ) >>= bUpdateCheckEnabled );
120 
121             // TODO: do we need to respect the bUpdateCheckEnabled flag? Finally, its meaning is "are automatic
122             // updates enabled", but this here is not an automatic update, but one triggered explicitly by the user.
123 
124             uno::Any aVal = ::comphelper::ConfigurationHelper::readDirectKey(
125                                     aContext.getLegacyServiceFactory(),
126                                     DEFINE_CONST_UNICODE("org.openoffice.Office.Addons/"),
127                                     DEFINE_CONST_UNICODE("AddonUI/OfficeHelp/UpdateCheckJob"),
128                                     DEFINE_CONST_UNICODE("URL"),
129                                     ::comphelper::ConfigurationHelper::E_READONLY );
130             util::URL aURL;
131             if ( aVal >>= aURL.Complete )
132             {
133                 uno::Reference< util::XURLTransformer > xTransformer(
134                     aContext.createComponent( "com.sun.star.util.URLTransformer" ), uno::UNO_QUERY_THROW );
135                 xTransformer->parseStrict( aURL );
136 
137                 uno::Reference < frame::XDesktop > xDesktop(
138                     aContext.createComponent( "com.sun.star.frame.Desktop" ), uno::UNO_QUERY_THROW );
139 
140                 uno::Reference< frame::XDispatchProvider > xDispatchProvider(
141                     xDesktop->getCurrentFrame(), uno::UNO_QUERY );
142 			    if ( !xDispatchProvider.is() )
143 				    xDispatchProvider = uno::Reference < frame::XDispatchProvider > ( xDesktop, uno::UNO_QUERY );
144 
145 			    uno::Reference< frame::XDispatch > xDispatch =
146 				    xDispatchProvider->queryDispatch( aURL, rtl::OUString(), 0 );
147                 if ( xDispatch.is() )
148 	                xDispatch->dispatch( aURL, uno::Sequence< beans::PropertyValue >() );
149             }
150         }
151     }
152     catch( const uno::Exception& )
153     {
154         DBG_UNHANDLED_EXCEPTION();
155     }
156 
157 	EndDialog( RET_OK );
158     return 0;
159 }
160 
IMPL_LINK(NewerVersionWarningDialog,LaterHdl,CancelButton *,EMPTYARG)161 IMPL_LINK( NewerVersionWarningDialog, LaterHdl, CancelButton*, EMPTYARG )
162 {
163     EndDialog( RET_ASK_LATER );
164     return 0;
165 }
166 
InitButtonWidth()167 void NewerVersionWarningDialog::InitButtonWidth()
168 {
169     // one button too small for its text?
170     long nBtnTextWidth = m_aUpdateBtn.GetCtrlTextWidth( m_aUpdateBtn.GetText() );
171     long nTemp = m_aLaterBtn.GetCtrlTextWidth( m_aLaterBtn.GetText() );
172     if ( nTemp > nBtnTextWidth )
173         nBtnTextWidth = nTemp;
174     nBtnTextWidth = nBtnTextWidth * 115 / 100; // a little offset
175     long nMaxBtnWidth = LogicToPixel( Size( MAX_BUTTON_WIDTH, 0 ), MAP_APPFONT ).Width();
176     nBtnTextWidth = std::min( nBtnTextWidth, nMaxBtnWidth );
177     long nButtonWidth = m_aUpdateBtn .GetSizePixel().Width();
178 
179     if ( nBtnTextWidth > nButtonWidth )
180     {
181         long nDelta = nBtnTextWidth - nButtonWidth;
182         Point aNewPos = m_aUpdateBtn.GetPosPixel();
183         aNewPos.X() -= 2*nDelta;
184         Size aNewSize = m_aUpdateBtn.GetSizePixel();
185         aNewSize.Width() += nDelta;
186         m_aUpdateBtn.SetPosSizePixel( aNewPos, aNewSize );
187         aNewPos = m_aLaterBtn.GetPosPixel();
188         aNewPos.X() -= nDelta;
189         m_aLaterBtn.SetPosSizePixel( aNewPos, aNewSize );
190     }
191 }
192 
193 } // end of namespace uui
194 
195