xref: /AOO41X/main/uui/source/newerverwarn.cxx (revision 859212d11457d0fe6c8750969b32b9412d9ba923)
1*859212d1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*859212d1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*859212d1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*859212d1SAndrew Rist  * distributed with this work for additional information
6*859212d1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*859212d1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*859212d1SAndrew Rist  * "License"); you may not use this file except in compliance
9*859212d1SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*859212d1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*859212d1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*859212d1SAndrew Rist  * software distributed under the License is distributed on an
15*859212d1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*859212d1SAndrew Rist  * KIND, either express or implied.  See the License for the
17*859212d1SAndrew Rist  * specific language governing permissions and limitations
18*859212d1SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*859212d1SAndrew Rist  *************************************************************/
21*859212d1SAndrew Rist 
22*859212d1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "newerverwarn.hxx"
25cdf0e10cSrcweir #include "newerverwarn.hrc"
26cdf0e10cSrcweir #include "ids.hrc"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
29cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp>
30cdf0e10cSrcweir #include <com/sun/star/system/XSystemShellExecute.hpp>
31cdf0e10cSrcweir #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
32cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
33cdf0e10cSrcweir #include <com/sun/star/container/XNameReplace.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
36cdf0e10cSrcweir #include <comphelper/configurationhelper.hxx>
37cdf0e10cSrcweir #include <comphelper/componentcontext.hxx>
38cdf0e10cSrcweir #include <rtl/bootstrap.hxx>
39cdf0e10cSrcweir #include <tools/diagnose_ex.h>
40cdf0e10cSrcweir #include <vcl/msgbox.hxx>
41cdf0e10cSrcweir #include <osl/process.h>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace beans     = ::com::sun::star::beans;
44cdf0e10cSrcweir namespace frame     = ::com::sun::star::frame;
45cdf0e10cSrcweir namespace lang      = ::com::sun::star::lang;
46cdf0e10cSrcweir namespace uno       = ::com::sun::star::uno;
47cdf0e10cSrcweir namespace util      = ::com::sun::star::util;
48cdf0e10cSrcweir namespace container = ::com::sun::star::container;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir using namespace com::sun::star::system;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir #define	DEFINE_CONST_UNICODE( CONSTASCII )  ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CONSTASCII ) )
53cdf0e10cSrcweir 
54cdf0e10cSrcweir namespace uui
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir NewerVersionWarningDialog::NewerVersionWarningDialog(
58cdf0e10cSrcweir     Window* pParent, const ::rtl::OUString& rVersion, ResMgr& rResMgr ) :
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     ModalDialog( pParent, ResId( RID_DLG_NEWER_VERSION_WARNING, rResMgr ) ),
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     m_aImage        ( this, ResId( FI_IMAGE, rResMgr ) ),
63cdf0e10cSrcweir     m_aInfoText     ( this, ResId( FT_INFO, rResMgr ) ),
64cdf0e10cSrcweir     m_aButtonLine   ( this, ResId( FL_BUTTON, rResMgr ) ),
65cdf0e10cSrcweir     m_aUpdateBtn    ( this, ResId( PB_UPDATE, rResMgr ) ),
66cdf0e10cSrcweir     m_aLaterBtn     ( this, ResId( PB_LATER, rResMgr ) ),
67cdf0e10cSrcweir     m_sVersion      ( rVersion )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     FreeResource();
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     m_aUpdateBtn.SetClickHdl( LINK( this, NewerVersionWarningDialog, UpdateHdl ) );
72cdf0e10cSrcweir     m_aLaterBtn.SetClickHdl( LINK( this, NewerVersionWarningDialog, LaterHdl ) );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     InitButtonWidth();
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir NewerVersionWarningDialog::~NewerVersionWarningDialog()
78cdf0e10cSrcweir {
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir IMPL_LINK( NewerVersionWarningDialog, UpdateHdl, PushButton*, EMPTYARG )
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     // detect execute path
84cdf0e10cSrcweir     ::rtl::OUString sProgramPath;
85cdf0e10cSrcweir     osl_getExecutableFile( &sProgramPath.pData );
86cdf0e10cSrcweir     sal_uInt32 nLastIndex = sProgramPath.lastIndexOf( '/' );
87cdf0e10cSrcweir     if ( nLastIndex > 0 )
88cdf0e10cSrcweir         sProgramPath = sProgramPath.copy( 0, nLastIndex + 1 );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     // read keys from soffice.ini (sofficerc)
91cdf0e10cSrcweir     ::rtl::OUString sIniFileName = sProgramPath;
92cdf0e10cSrcweir     sIniFileName += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SAL_CONFIGFILE( "version" ) ) );
93cdf0e10cSrcweir     ::rtl::Bootstrap aIniFile( sIniFileName );
94cdf0e10cSrcweir     ::rtl::OUString sNotifyURL;
95cdf0e10cSrcweir     aIniFile.getFrom( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ODFNotifyURL" ) ), sNotifyURL );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     try
98cdf0e10cSrcweir     {
99cdf0e10cSrcweir         if ( ( sNotifyURL.getLength() > 0 ) && ( m_sVersion.getLength() > 0 ) )
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir             uno::Reference< lang::XMultiServiceFactory > xSMGR =
102cdf0e10cSrcweir                 ::comphelper::getProcessServiceFactory();
103cdf0e10cSrcweir             uno::Reference< XSystemShellExecute > xSystemShell(
104cdf0e10cSrcweir                 xSMGR->createInstance( ::rtl::OUString(
105cdf0e10cSrcweir                     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.system.SystemShellExecute" ) ) ),
106cdf0e10cSrcweir                 uno::UNO_QUERY_THROW );
107cdf0e10cSrcweir             sNotifyURL += m_sVersion;
108cdf0e10cSrcweir             if ( xSystemShell.is() && sNotifyURL.getLength() )
109cdf0e10cSrcweir             {
110cdf0e10cSrcweir                 xSystemShell->execute(
111cdf0e10cSrcweir                     sNotifyURL, ::rtl::OUString(), SystemShellExecuteFlags::DEFAULTS );
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         else
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             uno::Reference < container::XNameReplace > xUpdateConfig(
119cdf0e10cSrcweir                 aContext.createComponent( "com.sun.star.setup.UpdateCheckConfig" ), uno::UNO_QUERY_THROW );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir             sal_Bool bUpdateCheckEnabled = sal_False;
122cdf0e10cSrcweir             OSL_VERIFY( xUpdateConfig->getByName( DEFINE_CONST_UNICODE( "AutoCheckEnabled" ) ) >>= bUpdateCheckEnabled );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             // TODO: do we need to respect the bUpdateCheckEnabled flag? Finally, its meaning is "are automatic
125cdf0e10cSrcweir             // updates enabled", but this here is not an automatic update, but one triggered explicitly by the user.
126cdf0e10cSrcweir 
127cdf0e10cSrcweir             uno::Any aVal = ::comphelper::ConfigurationHelper::readDirectKey(
128cdf0e10cSrcweir                                     aContext.getLegacyServiceFactory(),
129cdf0e10cSrcweir                                     DEFINE_CONST_UNICODE("org.openoffice.Office.Addons/"),
130cdf0e10cSrcweir                                     DEFINE_CONST_UNICODE("AddonUI/OfficeHelp/UpdateCheckJob"),
131cdf0e10cSrcweir                                     DEFINE_CONST_UNICODE("URL"),
132cdf0e10cSrcweir                                     ::comphelper::ConfigurationHelper::E_READONLY );
133cdf0e10cSrcweir             util::URL aURL;
134cdf0e10cSrcweir             if ( aVal >>= aURL.Complete )
135cdf0e10cSrcweir             {
136cdf0e10cSrcweir                 uno::Reference< util::XURLTransformer > xTransformer(
137cdf0e10cSrcweir                     aContext.createComponent( "com.sun.star.util.URLTransformer" ), uno::UNO_QUERY_THROW );
138cdf0e10cSrcweir                 xTransformer->parseStrict( aURL );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir                 uno::Reference < frame::XDesktop > xDesktop(
141cdf0e10cSrcweir                     aContext.createComponent( "com.sun.star.frame.Desktop" ), uno::UNO_QUERY_THROW );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir                 uno::Reference< frame::XDispatchProvider > xDispatchProvider(
144cdf0e10cSrcweir                     xDesktop->getCurrentFrame(), uno::UNO_QUERY );
145cdf0e10cSrcweir 			    if ( !xDispatchProvider.is() )
146cdf0e10cSrcweir 				    xDispatchProvider = uno::Reference < frame::XDispatchProvider > ( xDesktop, uno::UNO_QUERY );
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 			    uno::Reference< frame::XDispatch > xDispatch =
149cdf0e10cSrcweir 				    xDispatchProvider->queryDispatch( aURL, rtl::OUString(), 0 );
150cdf0e10cSrcweir                 if ( xDispatch.is() )
151cdf0e10cSrcweir 	                xDispatch->dispatch( aURL, uno::Sequence< beans::PropertyValue >() );
152cdf0e10cSrcweir             }
153cdf0e10cSrcweir         }
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir     catch( const uno::Exception& )
156cdf0e10cSrcweir     {
157cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	EndDialog( RET_OK );
161cdf0e10cSrcweir     return 0;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir IMPL_LINK( NewerVersionWarningDialog, LaterHdl, CancelButton*, EMPTYARG )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     EndDialog( RET_ASK_LATER );
167cdf0e10cSrcweir     return 0;
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir void NewerVersionWarningDialog::InitButtonWidth()
171cdf0e10cSrcweir {
172cdf0e10cSrcweir     // one button too small for its text?
173cdf0e10cSrcweir     long nBtnTextWidth = m_aUpdateBtn.GetCtrlTextWidth( m_aUpdateBtn.GetText() );
174cdf0e10cSrcweir     long nTemp = m_aLaterBtn.GetCtrlTextWidth( m_aLaterBtn.GetText() );
175cdf0e10cSrcweir     if ( nTemp > nBtnTextWidth )
176cdf0e10cSrcweir         nBtnTextWidth = nTemp;
177cdf0e10cSrcweir     nBtnTextWidth = nBtnTextWidth * 115 / 100; // a little offset
178cdf0e10cSrcweir     long nMaxBtnWidth = LogicToPixel( Size( MAX_BUTTON_WIDTH, 0 ), MAP_APPFONT ).Width();
179cdf0e10cSrcweir     nBtnTextWidth = std::min( nBtnTextWidth, nMaxBtnWidth );
180cdf0e10cSrcweir     long nButtonWidth = m_aUpdateBtn .GetSizePixel().Width();
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     if ( nBtnTextWidth > nButtonWidth )
183cdf0e10cSrcweir     {
184cdf0e10cSrcweir         long nDelta = nBtnTextWidth - nButtonWidth;
185cdf0e10cSrcweir         Point aNewPos = m_aUpdateBtn.GetPosPixel();
186cdf0e10cSrcweir         aNewPos.X() -= 2*nDelta;
187cdf0e10cSrcweir         Size aNewSize = m_aUpdateBtn.GetSizePixel();
188cdf0e10cSrcweir         aNewSize.Width() += nDelta;
189cdf0e10cSrcweir         m_aUpdateBtn.SetPosSizePixel( aNewPos, aNewSize );
190cdf0e10cSrcweir         aNewPos = m_aLaterBtn.GetPosPixel();
191cdf0e10cSrcweir         aNewPos.X() -= nDelta;
192cdf0e10cSrcweir         m_aLaterBtn.SetPosSizePixel( aNewPos, aNewSize );
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
196cdf0e10cSrcweir } // end of namespace uui
197cdf0e10cSrcweir 
198