xref: /trunk/main/uui/source/newerverwarn.cxx (revision 859212d1)
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/XSystemShellExecute.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 
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 
77 NewerVersionWarningDialog::~NewerVersionWarningDialog()
78 {
79 }
80 
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< lang::XMultiServiceFactory > xSMGR =
102                 ::comphelper::getProcessServiceFactory();
103             uno::Reference< XSystemShellExecute > xSystemShell(
104                 xSMGR->createInstance( ::rtl::OUString(
105                     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.system.SystemShellExecute" ) ) ),
106                 uno::UNO_QUERY_THROW );
107             sNotifyURL += m_sVersion;
108             if ( xSystemShell.is() && sNotifyURL.getLength() )
109             {
110                 xSystemShell->execute(
111                     sNotifyURL, ::rtl::OUString(), SystemShellExecuteFlags::DEFAULTS );
112             }
113         }
114         else
115         {
116             ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() );
117 
118             uno::Reference < container::XNameReplace > xUpdateConfig(
119                 aContext.createComponent( "com.sun.star.setup.UpdateCheckConfig" ), uno::UNO_QUERY_THROW );
120 
121             sal_Bool bUpdateCheckEnabled = sal_False;
122             OSL_VERIFY( xUpdateConfig->getByName( DEFINE_CONST_UNICODE( "AutoCheckEnabled" ) ) >>= bUpdateCheckEnabled );
123 
124             // TODO: do we need to respect the bUpdateCheckEnabled flag? Finally, its meaning is "are automatic
125             // updates enabled", but this here is not an automatic update, but one triggered explicitly by the user.
126 
127             uno::Any aVal = ::comphelper::ConfigurationHelper::readDirectKey(
128                                     aContext.getLegacyServiceFactory(),
129                                     DEFINE_CONST_UNICODE("org.openoffice.Office.Addons/"),
130                                     DEFINE_CONST_UNICODE("AddonUI/OfficeHelp/UpdateCheckJob"),
131                                     DEFINE_CONST_UNICODE("URL"),
132                                     ::comphelper::ConfigurationHelper::E_READONLY );
133             util::URL aURL;
134             if ( aVal >>= aURL.Complete )
135             {
136                 uno::Reference< util::XURLTransformer > xTransformer(
137                     aContext.createComponent( "com.sun.star.util.URLTransformer" ), uno::UNO_QUERY_THROW );
138                 xTransformer->parseStrict( aURL );
139 
140                 uno::Reference < frame::XDesktop > xDesktop(
141                     aContext.createComponent( "com.sun.star.frame.Desktop" ), uno::UNO_QUERY_THROW );
142 
143                 uno::Reference< frame::XDispatchProvider > xDispatchProvider(
144                     xDesktop->getCurrentFrame(), uno::UNO_QUERY );
145 			    if ( !xDispatchProvider.is() )
146 				    xDispatchProvider = uno::Reference < frame::XDispatchProvider > ( xDesktop, uno::UNO_QUERY );
147 
148 			    uno::Reference< frame::XDispatch > xDispatch =
149 				    xDispatchProvider->queryDispatch( aURL, rtl::OUString(), 0 );
150                 if ( xDispatch.is() )
151 	                xDispatch->dispatch( aURL, uno::Sequence< beans::PropertyValue >() );
152             }
153         }
154     }
155     catch( const uno::Exception& )
156     {
157         DBG_UNHANDLED_EXCEPTION();
158     }
159 
160 	EndDialog( RET_OK );
161     return 0;
162 }
163 
164 IMPL_LINK( NewerVersionWarningDialog, LaterHdl, CancelButton*, EMPTYARG )
165 {
166     EndDialog( RET_ASK_LATER );
167     return 0;
168 }
169 
170 void NewerVersionWarningDialog::InitButtonWidth()
171 {
172     // one button too small for its text?
173     long nBtnTextWidth = m_aUpdateBtn.GetCtrlTextWidth( m_aUpdateBtn.GetText() );
174     long nTemp = m_aLaterBtn.GetCtrlTextWidth( m_aLaterBtn.GetText() );
175     if ( nTemp > nBtnTextWidth )
176         nBtnTextWidth = nTemp;
177     nBtnTextWidth = nBtnTextWidth * 115 / 100; // a little offset
178     long nMaxBtnWidth = LogicToPixel( Size( MAX_BUTTON_WIDTH, 0 ), MAP_APPFONT ).Width();
179     nBtnTextWidth = std::min( nBtnTextWidth, nMaxBtnWidth );
180     long nButtonWidth = m_aUpdateBtn .GetSizePixel().Width();
181 
182     if ( nBtnTextWidth > nButtonWidth )
183     {
184         long nDelta = nBtnTextWidth - nButtonWidth;
185         Point aNewPos = m_aUpdateBtn.GetPosPixel();
186         aNewPos.X() -= 2*nDelta;
187         Size aNewSize = m_aUpdateBtn.GetSizePixel();
188         aNewSize.Width() += nDelta;
189         m_aUpdateBtn.SetPosSizePixel( aNewPos, aNewSize );
190         aNewPos = m_aLaterBtn.GetPosPixel();
191         aNewPos.X() -= nDelta;
192         m_aLaterBtn.SetPosSizePixel( aNewPos, aNewSize );
193     }
194 }
195 
196 } // end of namespace uui
197 
198